… | |
… | |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
30 | */ |
31 | #if EV_USE_CONFIG_H |
31 | #ifndef EV_STANDALONE |
32 | # include "config.h" |
32 | # include "config.h" |
33 | #endif |
33 | #endif |
34 | |
34 | |
35 | #include <math.h> |
35 | #include <math.h> |
36 | #include <stdlib.h> |
36 | #include <stdlib.h> |
… | |
… | |
58 | |
58 | |
59 | #ifndef EV_USE_SELECT |
59 | #ifndef EV_USE_SELECT |
60 | # define EV_USE_SELECT 1 |
60 | # define EV_USE_SELECT 1 |
61 | #endif |
61 | #endif |
62 | |
62 | |
63 | #ifndef EV_USE_POLL |
63 | #ifndef EV_USEV_POLL |
64 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
64 | # define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ |
65 | #endif |
65 | #endif |
66 | |
66 | |
67 | #ifndef EV_USE_EPOLL |
67 | #ifndef EV_USE_EPOLL |
68 | # define EV_USE_EPOLL 0 |
68 | # define EV_USE_EPOLL 0 |
69 | #endif |
69 | #endif |
… | |
… | |
113 | |
113 | |
114 | typedef struct ev_watcher *W; |
114 | typedef struct ev_watcher *W; |
115 | typedef struct ev_watcher_list *WL; |
115 | typedef struct ev_watcher_list *WL; |
116 | typedef struct ev_watcher_time *WT; |
116 | typedef struct ev_watcher_time *WT; |
117 | |
117 | |
118 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
118 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
119 | ev_tstamp ev_now; |
|
|
120 | int ev_method; |
|
|
121 | |
|
|
122 | static int have_monotonic; /* runtime */ |
|
|
123 | |
|
|
124 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
|
|
125 | static void (*method_modify)(int fd, int oev, int nev); |
|
|
126 | static void (*method_poll)(ev_tstamp timeout); |
|
|
127 | |
119 | |
128 | /*****************************************************************************/ |
120 | /*****************************************************************************/ |
129 | |
121 | |
130 | ev_tstamp |
122 | typedef struct |
|
|
123 | { |
|
|
124 | struct ev_watcher_list *head; |
|
|
125 | unsigned char events; |
|
|
126 | unsigned char reify; |
|
|
127 | } ANFD; |
|
|
128 | |
|
|
129 | typedef struct |
|
|
130 | { |
|
|
131 | W w; |
|
|
132 | int events; |
|
|
133 | } ANPENDING; |
|
|
134 | |
|
|
135 | #if EV_MULTIPLICITY |
|
|
136 | |
|
|
137 | struct ev_loop |
|
|
138 | { |
|
|
139 | # define VAR(name,decl) decl; |
|
|
140 | # include "ev_vars.h" |
|
|
141 | }; |
|
|
142 | # undef VAR |
|
|
143 | # include "ev_wrap.h" |
|
|
144 | |
|
|
145 | #else |
|
|
146 | |
|
|
147 | # define VAR(name,decl) static decl; |
|
|
148 | # include "ev_vars.h" |
|
|
149 | # undef VAR |
|
|
150 | |
|
|
151 | #endif |
|
|
152 | |
|
|
153 | /*****************************************************************************/ |
|
|
154 | |
|
|
155 | inline ev_tstamp |
131 | ev_time (void) |
156 | ev_time (void) |
132 | { |
157 | { |
133 | #if EV_USE_REALTIME |
158 | #if EV_USE_REALTIME |
134 | struct timespec ts; |
159 | struct timespec ts; |
135 | clock_gettime (CLOCK_REALTIME, &ts); |
160 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
139 | gettimeofday (&tv, 0); |
164 | gettimeofday (&tv, 0); |
140 | return tv.tv_sec + tv.tv_usec * 1e-6; |
165 | return tv.tv_sec + tv.tv_usec * 1e-6; |
141 | #endif |
166 | #endif |
142 | } |
167 | } |
143 | |
168 | |
144 | static ev_tstamp |
169 | inline ev_tstamp |
145 | get_clock (void) |
170 | get_clock (void) |
146 | { |
171 | { |
147 | #if EV_USE_MONOTONIC |
172 | #if EV_USE_MONOTONIC |
148 | if (expect_true (have_monotonic)) |
173 | if (expect_true (have_monotonic)) |
149 | { |
174 | { |
… | |
… | |
152 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
177 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
153 | } |
178 | } |
154 | #endif |
179 | #endif |
155 | |
180 | |
156 | return ev_time (); |
181 | return ev_time (); |
|
|
182 | } |
|
|
183 | |
|
|
184 | ev_tstamp |
|
|
185 | ev_now (EV_P) |
|
|
186 | { |
|
|
187 | return rt_now; |
157 | } |
188 | } |
158 | |
189 | |
159 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
190 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
160 | |
191 | |
161 | #define array_needsize(base,cur,cnt,init) \ |
192 | #define array_needsize(base,cur,cnt,init) \ |
… | |
… | |
173 | cur = newcnt; \ |
204 | cur = newcnt; \ |
174 | } |
205 | } |
175 | |
206 | |
176 | /*****************************************************************************/ |
207 | /*****************************************************************************/ |
177 | |
208 | |
178 | typedef struct |
|
|
179 | { |
|
|
180 | struct ev_io *head; |
|
|
181 | unsigned char events; |
|
|
182 | unsigned char reify; |
|
|
183 | } ANFD; |
|
|
184 | |
|
|
185 | static ANFD *anfds; |
|
|
186 | static int anfdmax; |
|
|
187 | |
|
|
188 | static void |
209 | static void |
189 | anfds_init (ANFD *base, int count) |
210 | anfds_init (ANFD *base, int count) |
190 | { |
211 | { |
191 | while (count--) |
212 | while (count--) |
192 | { |
213 | { |
… | |
… | |
196 | |
217 | |
197 | ++base; |
218 | ++base; |
198 | } |
219 | } |
199 | } |
220 | } |
200 | |
221 | |
201 | typedef struct |
|
|
202 | { |
|
|
203 | W w; |
|
|
204 | int events; |
|
|
205 | } ANPENDING; |
|
|
206 | |
|
|
207 | static ANPENDING *pendings [NUMPRI]; |
|
|
208 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
|
|
209 | |
|
|
210 | static void |
222 | static void |
211 | event (W w, int events) |
223 | event (EV_P_ W w, int events) |
212 | { |
224 | { |
213 | if (w->pending) |
225 | if (w->pending) |
214 | { |
226 | { |
215 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
227 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
216 | return; |
228 | return; |
… | |
… | |
221 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
233 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
222 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
234 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
223 | } |
235 | } |
224 | |
236 | |
225 | static void |
237 | static void |
226 | queue_events (W *events, int eventcnt, int type) |
238 | queue_events (EV_P_ W *events, int eventcnt, int type) |
227 | { |
239 | { |
228 | int i; |
240 | int i; |
229 | |
241 | |
230 | for (i = 0; i < eventcnt; ++i) |
242 | for (i = 0; i < eventcnt; ++i) |
231 | event (events [i], type); |
243 | event (EV_A_ events [i], type); |
232 | } |
244 | } |
233 | |
245 | |
234 | static void |
246 | static void |
235 | fd_event (int fd, int events) |
247 | fd_event (EV_P_ int fd, int events) |
236 | { |
248 | { |
237 | ANFD *anfd = anfds + fd; |
249 | ANFD *anfd = anfds + fd; |
238 | struct ev_io *w; |
250 | struct ev_io *w; |
239 | |
251 | |
240 | for (w = anfd->head; w; w = w->next) |
252 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
241 | { |
253 | { |
242 | int ev = w->events & events; |
254 | int ev = w->events & events; |
243 | |
255 | |
244 | if (ev) |
256 | if (ev) |
245 | event ((W)w, ev); |
257 | event (EV_A_ (W)w, ev); |
246 | } |
258 | } |
247 | } |
259 | } |
248 | |
260 | |
249 | /*****************************************************************************/ |
261 | /*****************************************************************************/ |
250 | |
262 | |
251 | static int *fdchanges; |
|
|
252 | static int fdchangemax, fdchangecnt; |
|
|
253 | |
|
|
254 | static void |
263 | static void |
255 | fd_reify (void) |
264 | fd_reify (EV_P) |
256 | { |
265 | { |
257 | int i; |
266 | int i; |
258 | |
267 | |
259 | for (i = 0; i < fdchangecnt; ++i) |
268 | for (i = 0; i < fdchangecnt; ++i) |
260 | { |
269 | { |
… | |
… | |
262 | ANFD *anfd = anfds + fd; |
271 | ANFD *anfd = anfds + fd; |
263 | struct ev_io *w; |
272 | struct ev_io *w; |
264 | |
273 | |
265 | int events = 0; |
274 | int events = 0; |
266 | |
275 | |
267 | for (w = anfd->head; w; w = w->next) |
276 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
268 | events |= w->events; |
277 | events |= w->events; |
269 | |
278 | |
270 | anfd->reify = 0; |
279 | anfd->reify = 0; |
271 | |
280 | |
272 | if (anfd->events != events) |
281 | if (anfd->events != events) |
273 | { |
282 | { |
274 | method_modify (fd, anfd->events, events); |
283 | method_modify (EV_A_ fd, anfd->events, events); |
275 | anfd->events = events; |
284 | anfd->events = events; |
276 | } |
285 | } |
277 | } |
286 | } |
278 | |
287 | |
279 | fdchangecnt = 0; |
288 | fdchangecnt = 0; |
280 | } |
289 | } |
281 | |
290 | |
282 | static void |
291 | static void |
283 | fd_change (int fd) |
292 | fd_change (EV_P_ int fd) |
284 | { |
293 | { |
285 | if (anfds [fd].reify || fdchangecnt < 0) |
294 | if (anfds [fd].reify || fdchangecnt < 0) |
286 | return; |
295 | return; |
287 | |
296 | |
288 | anfds [fd].reify = 1; |
297 | anfds [fd].reify = 1; |
… | |
… | |
291 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
300 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
292 | fdchanges [fdchangecnt - 1] = fd; |
301 | fdchanges [fdchangecnt - 1] = fd; |
293 | } |
302 | } |
294 | |
303 | |
295 | static void |
304 | static void |
296 | fd_kill (int fd) |
305 | fd_kill (EV_P_ int fd) |
297 | { |
306 | { |
298 | struct ev_io *w; |
307 | struct ev_io *w; |
299 | |
308 | |
300 | printf ("killing fd %d\n", fd);//D |
|
|
301 | while ((w = anfds [fd].head)) |
309 | while ((w = (struct ev_io *)anfds [fd].head)) |
302 | { |
310 | { |
303 | ev_io_stop (w); |
311 | ev_io_stop (EV_A_ w); |
304 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
312 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
305 | } |
313 | } |
306 | } |
314 | } |
307 | |
315 | |
308 | /* called on EBADF to verify fds */ |
316 | /* called on EBADF to verify fds */ |
309 | static void |
317 | static void |
310 | fd_ebadf (void) |
318 | fd_ebadf (EV_P) |
311 | { |
319 | { |
312 | int fd; |
320 | int fd; |
313 | |
321 | |
314 | for (fd = 0; fd < anfdmax; ++fd) |
322 | for (fd = 0; fd < anfdmax; ++fd) |
315 | if (anfds [fd].events) |
323 | if (anfds [fd].events) |
316 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
324 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
317 | fd_kill (fd); |
325 | fd_kill (EV_A_ fd); |
318 | } |
326 | } |
319 | |
327 | |
320 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
328 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
321 | static void |
329 | static void |
322 | fd_enomem (void) |
330 | fd_enomem (EV_P) |
323 | { |
331 | { |
324 | int fd = anfdmax; |
332 | int fd = anfdmax; |
325 | |
333 | |
326 | while (fd--) |
334 | while (fd--) |
327 | if (anfds [fd].events) |
335 | if (anfds [fd].events) |
328 | { |
336 | { |
329 | close (fd); |
337 | close (fd); |
330 | fd_kill (fd); |
338 | fd_kill (EV_A_ fd); |
331 | return; |
339 | return; |
332 | } |
340 | } |
333 | } |
341 | } |
334 | |
342 | |
|
|
343 | /* susually called after fork if method needs to re-arm all fds from scratch */ |
|
|
344 | static void |
|
|
345 | fd_rearm_all (EV_P) |
|
|
346 | { |
|
|
347 | int fd; |
|
|
348 | |
|
|
349 | /* this should be highly optimised to not do anything but set a flag */ |
|
|
350 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
351 | if (anfds [fd].events) |
|
|
352 | { |
|
|
353 | anfds [fd].events = 0; |
|
|
354 | fd_change (fd); |
|
|
355 | } |
|
|
356 | } |
|
|
357 | |
335 | /*****************************************************************************/ |
358 | /*****************************************************************************/ |
336 | |
359 | |
337 | static struct ev_timer **timers; |
|
|
338 | static int timermax, timercnt; |
|
|
339 | |
|
|
340 | static struct ev_periodic **periodics; |
|
|
341 | static int periodicmax, periodiccnt; |
|
|
342 | |
|
|
343 | static void |
360 | static void |
344 | upheap (WT *timers, int k) |
361 | upheap (WT *heap, int k) |
345 | { |
362 | { |
346 | WT w = timers [k]; |
363 | WT w = heap [k]; |
347 | |
364 | |
348 | while (k && timers [k >> 1]->at > w->at) |
365 | while (k && heap [k >> 1]->at > w->at) |
349 | { |
366 | { |
350 | timers [k] = timers [k >> 1]; |
367 | heap [k] = heap [k >> 1]; |
351 | timers [k]->active = k + 1; |
368 | heap [k]->active = k + 1; |
352 | k >>= 1; |
369 | k >>= 1; |
353 | } |
370 | } |
354 | |
371 | |
355 | timers [k] = w; |
372 | heap [k] = w; |
356 | timers [k]->active = k + 1; |
373 | heap [k]->active = k + 1; |
357 | |
374 | |
358 | } |
375 | } |
359 | |
376 | |
360 | static void |
377 | static void |
361 | downheap (WT *timers, int N, int k) |
378 | downheap (WT *heap, int N, int k) |
362 | { |
379 | { |
363 | WT w = timers [k]; |
380 | WT w = heap [k]; |
364 | |
381 | |
365 | while (k < (N >> 1)) |
382 | while (k < (N >> 1)) |
366 | { |
383 | { |
367 | int j = k << 1; |
384 | int j = k << 1; |
368 | |
385 | |
369 | if (j + 1 < N && timers [j]->at > timers [j + 1]->at) |
386 | if (j + 1 < N && heap [j]->at > heap [j + 1]->at) |
370 | ++j; |
387 | ++j; |
371 | |
388 | |
372 | if (w->at <= timers [j]->at) |
389 | if (w->at <= heap [j]->at) |
373 | break; |
390 | break; |
374 | |
391 | |
375 | timers [k] = timers [j]; |
392 | heap [k] = heap [j]; |
376 | timers [k]->active = k + 1; |
393 | heap [k]->active = k + 1; |
377 | k = j; |
394 | k = j; |
378 | } |
395 | } |
379 | |
396 | |
380 | timers [k] = w; |
397 | heap [k] = w; |
381 | timers [k]->active = k + 1; |
398 | heap [k]->active = k + 1; |
382 | } |
399 | } |
383 | |
400 | |
384 | /*****************************************************************************/ |
401 | /*****************************************************************************/ |
385 | |
402 | |
386 | typedef struct |
403 | typedef struct |
387 | { |
404 | { |
388 | struct ev_signal *head; |
405 | struct ev_watcher_list *head; |
389 | sig_atomic_t volatile gotsig; |
406 | sig_atomic_t volatile gotsig; |
390 | } ANSIG; |
407 | } ANSIG; |
391 | |
408 | |
392 | static ANSIG *signals; |
409 | static ANSIG *signals; |
393 | static int signalmax; |
410 | static int signalmax; |
394 | |
411 | |
395 | static int sigpipe [2]; |
412 | static int sigpipe [2]; |
396 | static sig_atomic_t volatile gotsig; |
413 | static sig_atomic_t volatile gotsig; |
397 | static struct ev_io sigev; |
|
|
398 | |
414 | |
399 | static void |
415 | static void |
400 | signals_init (ANSIG *base, int count) |
416 | signals_init (ANSIG *base, int count) |
401 | { |
417 | { |
402 | while (count--) |
418 | while (count--) |
… | |
… | |
413 | { |
429 | { |
414 | signals [signum - 1].gotsig = 1; |
430 | signals [signum - 1].gotsig = 1; |
415 | |
431 | |
416 | if (!gotsig) |
432 | if (!gotsig) |
417 | { |
433 | { |
|
|
434 | int old_errno = errno; |
418 | gotsig = 1; |
435 | gotsig = 1; |
419 | write (sigpipe [1], &signum, 1); |
436 | write (sigpipe [1], &signum, 1); |
|
|
437 | errno = old_errno; |
420 | } |
438 | } |
421 | } |
439 | } |
422 | |
440 | |
423 | static void |
441 | static void |
424 | sigcb (struct ev_io *iow, int revents) |
442 | sigcb (EV_P_ struct ev_io *iow, int revents) |
425 | { |
443 | { |
426 | struct ev_signal *w; |
444 | struct ev_watcher_list *w; |
427 | int signum; |
445 | int signum; |
428 | |
446 | |
429 | read (sigpipe [0], &revents, 1); |
447 | read (sigpipe [0], &revents, 1); |
430 | gotsig = 0; |
448 | gotsig = 0; |
431 | |
449 | |
… | |
… | |
433 | if (signals [signum].gotsig) |
451 | if (signals [signum].gotsig) |
434 | { |
452 | { |
435 | signals [signum].gotsig = 0; |
453 | signals [signum].gotsig = 0; |
436 | |
454 | |
437 | for (w = signals [signum].head; w; w = w->next) |
455 | for (w = signals [signum].head; w; w = w->next) |
438 | event ((W)w, EV_SIGNAL); |
456 | event (EV_A_ (W)w, EV_SIGNAL); |
439 | } |
457 | } |
440 | } |
458 | } |
441 | |
459 | |
442 | static void |
460 | static void |
443 | siginit (void) |
461 | siginit (EV_P) |
444 | { |
462 | { |
445 | #ifndef WIN32 |
463 | #ifndef WIN32 |
446 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
464 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
447 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
465 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
448 | |
466 | |
… | |
… | |
450 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
468 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
451 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
469 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
452 | #endif |
470 | #endif |
453 | |
471 | |
454 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
472 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
455 | ev_io_start (&sigev); |
473 | ev_io_start (EV_A_ &sigev); |
|
|
474 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
456 | } |
475 | } |
457 | |
476 | |
458 | /*****************************************************************************/ |
477 | /*****************************************************************************/ |
459 | |
|
|
460 | static struct ev_idle **idles; |
|
|
461 | static int idlemax, idlecnt; |
|
|
462 | |
|
|
463 | static struct ev_prepare **prepares; |
|
|
464 | static int preparemax, preparecnt; |
|
|
465 | |
|
|
466 | static struct ev_check **checks; |
|
|
467 | static int checkmax, checkcnt; |
|
|
468 | |
|
|
469 | /*****************************************************************************/ |
|
|
470 | |
|
|
471 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
472 | static struct ev_signal childev; |
|
|
473 | |
478 | |
474 | #ifndef WIN32 |
479 | #ifndef WIN32 |
475 | |
480 | |
476 | #ifndef WCONTINUED |
481 | #ifndef WCONTINUED |
477 | # define WCONTINUED 0 |
482 | # define WCONTINUED 0 |
478 | #endif |
483 | #endif |
479 | |
484 | |
480 | static void |
485 | static void |
481 | child_reap (struct ev_signal *sw, int chain, int pid, int status) |
486 | child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) |
482 | { |
487 | { |
483 | struct ev_child *w; |
488 | struct ev_child *w; |
484 | |
489 | |
485 | for (w = childs [chain & (PID_HASHSIZE - 1)]; w; w = w->next) |
490 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
486 | if (w->pid == pid || !w->pid) |
491 | if (w->pid == pid || !w->pid) |
487 | { |
492 | { |
488 | w->priority = sw->priority; /* need to do it *now* */ |
493 | w->priority = sw->priority; /* need to do it *now* */ |
489 | w->rpid = pid; |
494 | w->rpid = pid; |
490 | w->rstatus = status; |
495 | w->rstatus = status; |
491 | printf ("rpid %p %d %d\n", w, pid, w->pid);//D |
|
|
492 | event ((W)w, EV_CHILD); |
496 | event (EV_A_ (W)w, EV_CHILD); |
493 | } |
497 | } |
494 | } |
498 | } |
495 | |
499 | |
496 | static void |
500 | static void |
497 | childcb (struct ev_signal *sw, int revents) |
501 | childcb (EV_P_ struct ev_signal *sw, int revents) |
498 | { |
502 | { |
499 | int pid, status; |
503 | int pid, status; |
500 | |
504 | |
501 | printf ("chld %x\n", revents);//D |
|
|
502 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
505 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
503 | { |
506 | { |
504 | /* make sure we are called again until all childs have been reaped */ |
507 | /* make sure we are called again until all childs have been reaped */ |
505 | event ((W)sw, EV_SIGNAL); |
508 | event (EV_A_ (W)sw, EV_SIGNAL); |
506 | |
509 | |
507 | child_reap (sw, pid, pid, status); |
510 | child_reap (EV_A_ sw, pid, pid, status); |
508 | child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
511 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
509 | } |
512 | } |
510 | } |
513 | } |
511 | |
514 | |
512 | #endif |
515 | #endif |
513 | |
516 | |
… | |
… | |
517 | # include "ev_kqueue.c" |
520 | # include "ev_kqueue.c" |
518 | #endif |
521 | #endif |
519 | #if EV_USE_EPOLL |
522 | #if EV_USE_EPOLL |
520 | # include "ev_epoll.c" |
523 | # include "ev_epoll.c" |
521 | #endif |
524 | #endif |
522 | #if EV_USE_POLL |
525 | #if EV_USEV_POLL |
523 | # include "ev_poll.c" |
526 | # include "ev_poll.c" |
524 | #endif |
527 | #endif |
525 | #if EV_USE_SELECT |
528 | #if EV_USE_SELECT |
526 | # include "ev_select.c" |
529 | # include "ev_select.c" |
527 | #endif |
530 | #endif |
… | |
… | |
536 | ev_version_minor (void) |
539 | ev_version_minor (void) |
537 | { |
540 | { |
538 | return EV_VERSION_MINOR; |
541 | return EV_VERSION_MINOR; |
539 | } |
542 | } |
540 | |
543 | |
541 | /* return true if we are running with elevated privileges and ignore env variables */ |
544 | /* return true if we are running with elevated privileges and should ignore env variables */ |
542 | static int |
545 | static int |
543 | enable_secure () |
546 | enable_secure (void) |
544 | { |
547 | { |
|
|
548 | #ifdef WIN32 |
|
|
549 | return 0; |
|
|
550 | #else |
545 | return getuid () != geteuid () |
551 | return getuid () != geteuid () |
546 | || getgid () != getegid (); |
552 | || getgid () != getegid (); |
|
|
553 | #endif |
547 | } |
554 | } |
548 | |
555 | |
549 | int ev_init (int methods) |
556 | int |
|
|
557 | ev_method (EV_P) |
550 | { |
558 | { |
|
|
559 | return method; |
|
|
560 | } |
|
|
561 | |
|
|
562 | static void |
|
|
563 | loop_init (EV_P_ int methods) |
|
|
564 | { |
551 | if (!ev_method) |
565 | if (!method) |
552 | { |
566 | { |
553 | #if EV_USE_MONOTONIC |
567 | #if EV_USE_MONOTONIC |
554 | { |
568 | { |
555 | struct timespec ts; |
569 | struct timespec ts; |
556 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
570 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
557 | have_monotonic = 1; |
571 | have_monotonic = 1; |
558 | } |
572 | } |
559 | #endif |
573 | #endif |
560 | |
574 | |
561 | ev_now = ev_time (); |
575 | rt_now = ev_time (); |
562 | now = get_clock (); |
576 | mn_now = get_clock (); |
563 | now_floor = now; |
577 | now_floor = mn_now; |
564 | diff = ev_now - now; |
578 | rtmn_diff = rt_now - mn_now; |
565 | |
|
|
566 | if (pipe (sigpipe)) |
|
|
567 | return 0; |
|
|
568 | |
579 | |
569 | if (methods == EVMETHOD_AUTO) |
580 | if (methods == EVMETHOD_AUTO) |
570 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
581 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
571 | methods = atoi (getenv ("LIBEV_METHODS")); |
582 | methods = atoi (getenv ("LIBEV_METHODS")); |
572 | else |
583 | else |
573 | methods = EVMETHOD_ANY; |
584 | methods = EVMETHOD_ANY; |
574 | |
585 | |
575 | ev_method = 0; |
586 | method = 0; |
576 | #if EV_USE_KQUEUE |
587 | #if EV_USE_KQUEUE |
577 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
588 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
578 | #endif |
589 | #endif |
579 | #if EV_USE_EPOLL |
590 | #if EV_USE_EPOLL |
580 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
591 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
581 | #endif |
592 | #endif |
582 | #if EV_USE_POLL |
593 | #if EV_USEV_POLL |
583 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
594 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
584 | #endif |
595 | #endif |
585 | #if EV_USE_SELECT |
596 | #if EV_USE_SELECT |
586 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
597 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
587 | #endif |
598 | #endif |
|
|
599 | } |
|
|
600 | } |
588 | |
601 | |
|
|
602 | void |
|
|
603 | loop_destroy (EV_P) |
|
|
604 | { |
|
|
605 | #if EV_USE_KQUEUE |
|
|
606 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
|
|
607 | #endif |
|
|
608 | #if EV_USE_EPOLL |
|
|
609 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
|
|
610 | #endif |
|
|
611 | #if EV_USEV_POLL |
|
|
612 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
|
|
613 | #endif |
|
|
614 | #if EV_USE_SELECT |
|
|
615 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
|
|
616 | #endif |
|
|
617 | |
|
|
618 | method = 0; |
|
|
619 | /*TODO*/ |
|
|
620 | } |
|
|
621 | |
|
|
622 | void |
|
|
623 | loop_fork (EV_P) |
|
|
624 | { |
|
|
625 | /*TODO*/ |
|
|
626 | #if EV_USE_EPOLL |
|
|
627 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
|
|
628 | #endif |
|
|
629 | #if EV_USE_KQUEUE |
|
|
630 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
|
|
631 | #endif |
|
|
632 | } |
|
|
633 | |
|
|
634 | #if EV_MULTIPLICITY |
|
|
635 | struct ev_loop * |
|
|
636 | ev_loop_new (int methods) |
|
|
637 | { |
|
|
638 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
|
|
639 | |
|
|
640 | loop_init (EV_A_ methods); |
|
|
641 | |
|
|
642 | if (ev_methods (EV_A)) |
|
|
643 | return loop; |
|
|
644 | |
|
|
645 | return 0; |
|
|
646 | } |
|
|
647 | |
|
|
648 | void |
|
|
649 | ev_loop_destroy (EV_P) |
|
|
650 | { |
|
|
651 | loop_destroy (EV_A); |
|
|
652 | free (loop); |
|
|
653 | } |
|
|
654 | |
|
|
655 | void |
|
|
656 | ev_loop_fork (EV_P) |
|
|
657 | { |
|
|
658 | loop_fork (EV_A); |
|
|
659 | } |
|
|
660 | |
|
|
661 | #endif |
|
|
662 | |
|
|
663 | #if EV_MULTIPLICITY |
|
|
664 | struct ev_loop default_loop_struct; |
|
|
665 | static struct ev_loop *default_loop; |
|
|
666 | |
|
|
667 | struct ev_loop * |
|
|
668 | #else |
|
|
669 | static int default_loop; |
|
|
670 | |
|
|
671 | int |
|
|
672 | #endif |
|
|
673 | ev_default_loop (int methods) |
|
|
674 | { |
|
|
675 | if (sigpipe [0] == sigpipe [1]) |
|
|
676 | if (pipe (sigpipe)) |
|
|
677 | return 0; |
|
|
678 | |
|
|
679 | if (!default_loop) |
|
|
680 | { |
|
|
681 | #if EV_MULTIPLICITY |
|
|
682 | struct ev_loop *loop = default_loop = &default_loop_struct; |
|
|
683 | #else |
|
|
684 | default_loop = 1; |
|
|
685 | #endif |
|
|
686 | |
|
|
687 | loop_init (EV_A_ methods); |
|
|
688 | |
589 | if (ev_method) |
689 | if (ev_method (EV_A)) |
590 | { |
690 | { |
591 | ev_watcher_init (&sigev, sigcb); |
691 | ev_watcher_init (&sigev, sigcb); |
592 | ev_set_priority (&sigev, EV_MAXPRI); |
692 | ev_set_priority (&sigev, EV_MAXPRI); |
593 | siginit (); |
693 | siginit (EV_A); |
594 | |
694 | |
595 | #ifndef WIN32 |
695 | #ifndef WIN32 |
596 | ev_signal_init (&childev, childcb, SIGCHLD); |
696 | ev_signal_init (&childev, childcb, SIGCHLD); |
597 | ev_set_priority (&childev, EV_MAXPRI); |
697 | ev_set_priority (&childev, EV_MAXPRI); |
598 | ev_signal_start (&childev); |
698 | ev_signal_start (EV_A_ &childev); |
|
|
699 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
599 | #endif |
700 | #endif |
600 | } |
701 | } |
|
|
702 | else |
|
|
703 | default_loop = 0; |
601 | } |
704 | } |
602 | |
705 | |
603 | return ev_method; |
706 | return default_loop; |
604 | } |
707 | } |
605 | |
708 | |
606 | /*****************************************************************************/ |
|
|
607 | |
|
|
608 | void |
709 | void |
609 | ev_fork_prepare (void) |
710 | ev_default_destroy (void) |
610 | { |
711 | { |
611 | /* nop */ |
712 | #if EV_MULTIPLICITY |
612 | } |
713 | struct ev_loop *loop = default_loop; |
613 | |
|
|
614 | void |
|
|
615 | ev_fork_parent (void) |
|
|
616 | { |
|
|
617 | /* nop */ |
|
|
618 | } |
|
|
619 | |
|
|
620 | void |
|
|
621 | ev_fork_child (void) |
|
|
622 | { |
|
|
623 | #if EV_USE_EPOLL |
|
|
624 | if (ev_method == EVMETHOD_EPOLL) |
|
|
625 | epoll_postfork_child (); |
|
|
626 | #endif |
714 | #endif |
627 | |
715 | |
|
|
716 | ev_ref (EV_A); /* child watcher */ |
|
|
717 | ev_signal_stop (EV_A_ &childev); |
|
|
718 | |
|
|
719 | ev_ref (EV_A); /* signal watcher */ |
628 | ev_io_stop (&sigev); |
720 | ev_io_stop (EV_A_ &sigev); |
|
|
721 | |
|
|
722 | close (sigpipe [0]); sigpipe [0] = 0; |
|
|
723 | close (sigpipe [1]); sigpipe [1] = 0; |
|
|
724 | |
|
|
725 | loop_destroy (EV_A); |
|
|
726 | } |
|
|
727 | |
|
|
728 | void |
|
|
729 | ev_default_fork (EV_P) |
|
|
730 | { |
|
|
731 | loop_fork (EV_A); |
|
|
732 | |
|
|
733 | ev_io_stop (EV_A_ &sigev); |
629 | close (sigpipe [0]); |
734 | close (sigpipe [0]); |
630 | close (sigpipe [1]); |
735 | close (sigpipe [1]); |
631 | pipe (sigpipe); |
736 | pipe (sigpipe); |
|
|
737 | |
|
|
738 | ev_ref (EV_A); /* signal watcher */ |
632 | siginit (); |
739 | siginit (EV_A); |
633 | } |
740 | } |
634 | |
741 | |
635 | /*****************************************************************************/ |
742 | /*****************************************************************************/ |
636 | |
743 | |
637 | static void |
744 | static void |
638 | call_pending (void) |
745 | call_pending (EV_P) |
639 | { |
746 | { |
640 | int pri; |
747 | int pri; |
641 | |
748 | |
642 | for (pri = NUMPRI; pri--; ) |
749 | for (pri = NUMPRI; pri--; ) |
643 | while (pendingcnt [pri]) |
750 | while (pendingcnt [pri]) |
… | |
… | |
645 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
752 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
646 | |
753 | |
647 | if (p->w) |
754 | if (p->w) |
648 | { |
755 | { |
649 | p->w->pending = 0; |
756 | p->w->pending = 0; |
650 | p->w->cb (p->w, p->events); |
757 | p->w->cb (EV_A_ p->w, p->events); |
651 | } |
758 | } |
652 | } |
759 | } |
653 | } |
760 | } |
654 | |
761 | |
655 | static void |
762 | static void |
656 | timers_reify (void) |
763 | timers_reify (EV_P) |
657 | { |
764 | { |
658 | while (timercnt && timers [0]->at <= now) |
765 | while (timercnt && timers [0]->at <= mn_now) |
659 | { |
766 | { |
660 | struct ev_timer *w = timers [0]; |
767 | struct ev_timer *w = timers [0]; |
661 | |
768 | |
662 | /* first reschedule or stop timer */ |
769 | /* first reschedule or stop timer */ |
663 | if (w->repeat) |
770 | if (w->repeat) |
664 | { |
771 | { |
665 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
772 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
666 | w->at = now + w->repeat; |
773 | w->at = mn_now + w->repeat; |
667 | downheap ((WT *)timers, timercnt, 0); |
774 | downheap ((WT *)timers, timercnt, 0); |
668 | } |
775 | } |
669 | else |
776 | else |
670 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
777 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
671 | |
778 | |
672 | event ((W)w, EV_TIMEOUT); |
779 | event (EV_A_ (W)w, EV_TIMEOUT); |
673 | } |
780 | } |
674 | } |
781 | } |
675 | |
782 | |
676 | static void |
783 | static void |
677 | periodics_reify (void) |
784 | periodics_reify (EV_P) |
678 | { |
785 | { |
679 | while (periodiccnt && periodics [0]->at <= ev_now) |
786 | while (periodiccnt && periodics [0]->at <= rt_now) |
680 | { |
787 | { |
681 | struct ev_periodic *w = periodics [0]; |
788 | struct ev_periodic *w = periodics [0]; |
682 | |
789 | |
683 | /* first reschedule or stop timer */ |
790 | /* first reschedule or stop timer */ |
684 | if (w->interval) |
791 | if (w->interval) |
685 | { |
792 | { |
686 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
793 | w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; |
687 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
794 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); |
688 | downheap ((WT *)periodics, periodiccnt, 0); |
795 | downheap ((WT *)periodics, periodiccnt, 0); |
689 | } |
796 | } |
690 | else |
797 | else |
691 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
798 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
692 | |
799 | |
693 | event ((W)w, EV_PERIODIC); |
800 | event (EV_A_ (W)w, EV_PERIODIC); |
694 | } |
801 | } |
695 | } |
802 | } |
696 | |
803 | |
697 | static void |
804 | static void |
698 | periodics_reschedule (ev_tstamp diff) |
805 | periodics_reschedule (EV_P) |
699 | { |
806 | { |
700 | int i; |
807 | int i; |
701 | |
808 | |
702 | /* adjust periodics after time jump */ |
809 | /* adjust periodics after time jump */ |
703 | for (i = 0; i < periodiccnt; ++i) |
810 | for (i = 0; i < periodiccnt; ++i) |
704 | { |
811 | { |
705 | struct ev_periodic *w = periodics [i]; |
812 | struct ev_periodic *w = periodics [i]; |
706 | |
813 | |
707 | if (w->interval) |
814 | if (w->interval) |
708 | { |
815 | { |
709 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
816 | ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; |
710 | |
817 | |
711 | if (fabs (diff) >= 1e-4) |
818 | if (fabs (diff) >= 1e-4) |
712 | { |
819 | { |
713 | ev_periodic_stop (w); |
820 | ev_periodic_stop (EV_A_ w); |
714 | ev_periodic_start (w); |
821 | ev_periodic_start (EV_A_ w); |
715 | |
822 | |
716 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
823 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
717 | } |
824 | } |
718 | } |
825 | } |
719 | } |
826 | } |
720 | } |
827 | } |
721 | |
828 | |
722 | static int |
829 | inline int |
723 | time_update_monotonic (void) |
830 | time_update_monotonic (EV_P) |
724 | { |
831 | { |
725 | now = get_clock (); |
832 | mn_now = get_clock (); |
726 | |
833 | |
727 | if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
834 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
728 | { |
835 | { |
729 | ev_now = now + diff; |
836 | rt_now = rtmn_diff + mn_now; |
730 | return 0; |
837 | return 0; |
731 | } |
838 | } |
732 | else |
839 | else |
733 | { |
840 | { |
734 | now_floor = now; |
841 | now_floor = mn_now; |
735 | ev_now = ev_time (); |
842 | rt_now = ev_time (); |
736 | return 1; |
843 | return 1; |
737 | } |
844 | } |
738 | } |
845 | } |
739 | |
846 | |
740 | static void |
847 | static void |
741 | time_update (void) |
848 | time_update (EV_P) |
742 | { |
849 | { |
743 | int i; |
850 | int i; |
744 | |
851 | |
745 | #if EV_USE_MONOTONIC |
852 | #if EV_USE_MONOTONIC |
746 | if (expect_true (have_monotonic)) |
853 | if (expect_true (have_monotonic)) |
747 | { |
854 | { |
748 | if (time_update_monotonic ()) |
855 | if (time_update_monotonic (EV_A)) |
749 | { |
856 | { |
750 | ev_tstamp odiff = diff; |
857 | ev_tstamp odiff = rtmn_diff; |
751 | |
858 | |
752 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
859 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
753 | { |
860 | { |
754 | diff = ev_now - now; |
861 | rtmn_diff = rt_now - mn_now; |
755 | |
862 | |
756 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
863 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
757 | return; /* all is well */ |
864 | return; /* all is well */ |
758 | |
865 | |
759 | ev_now = ev_time (); |
866 | rt_now = ev_time (); |
760 | now = get_clock (); |
867 | mn_now = get_clock (); |
761 | now_floor = now; |
868 | now_floor = mn_now; |
762 | } |
869 | } |
763 | |
870 | |
764 | periodics_reschedule (diff - odiff); |
871 | periodics_reschedule (EV_A); |
765 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
872 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
873 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
766 | } |
874 | } |
767 | } |
875 | } |
768 | else |
876 | else |
769 | #endif |
877 | #endif |
770 | { |
878 | { |
771 | ev_now = ev_time (); |
879 | rt_now = ev_time (); |
772 | |
880 | |
773 | if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
881 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
774 | { |
882 | { |
775 | periodics_reschedule (ev_now - now); |
883 | periodics_reschedule (EV_A); |
776 | |
884 | |
777 | /* adjust timers. this is easy, as the offset is the same for all */ |
885 | /* adjust timers. this is easy, as the offset is the same for all */ |
778 | for (i = 0; i < timercnt; ++i) |
886 | for (i = 0; i < timercnt; ++i) |
779 | timers [i]->at += diff; |
887 | timers [i]->at += rt_now - mn_now; |
780 | } |
888 | } |
781 | |
889 | |
782 | now = ev_now; |
890 | mn_now = rt_now; |
783 | } |
891 | } |
784 | } |
892 | } |
785 | |
893 | |
786 | int ev_loop_done; |
894 | void |
|
|
895 | ev_ref (EV_P) |
|
|
896 | { |
|
|
897 | ++activecnt; |
|
|
898 | } |
787 | |
899 | |
|
|
900 | void |
|
|
901 | ev_unref (EV_P) |
|
|
902 | { |
|
|
903 | --activecnt; |
|
|
904 | } |
|
|
905 | |
|
|
906 | static int loop_done; |
|
|
907 | |
|
|
908 | void |
788 | void ev_loop (int flags) |
909 | ev_loop (EV_P_ int flags) |
789 | { |
910 | { |
790 | double block; |
911 | double block; |
791 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
912 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
792 | |
913 | |
793 | do |
914 | do |
794 | { |
915 | { |
795 | /* queue check watchers (and execute them) */ |
916 | /* queue check watchers (and execute them) */ |
796 | if (expect_false (preparecnt)) |
917 | if (expect_false (preparecnt)) |
797 | { |
918 | { |
798 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
919 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
799 | call_pending (); |
920 | call_pending (EV_A); |
800 | } |
921 | } |
801 | |
922 | |
802 | /* update fd-related kernel structures */ |
923 | /* update fd-related kernel structures */ |
803 | fd_reify (); |
924 | fd_reify (EV_A); |
804 | |
925 | |
805 | /* calculate blocking time */ |
926 | /* calculate blocking time */ |
806 | |
927 | |
807 | /* we only need this for !monotonic clockor timers, but as we basically |
928 | /* we only need this for !monotonic clockor timers, but as we basically |
808 | always have timers, we just calculate it always */ |
929 | always have timers, we just calculate it always */ |
809 | #if EV_USE_MONOTONIC |
930 | #if EV_USE_MONOTONIC |
810 | if (expect_true (have_monotonic)) |
931 | if (expect_true (have_monotonic)) |
811 | time_update_monotonic (); |
932 | time_update_monotonic (EV_A); |
812 | else |
933 | else |
813 | #endif |
934 | #endif |
814 | { |
935 | { |
815 | ev_now = ev_time (); |
936 | rt_now = ev_time (); |
816 | now = ev_now; |
937 | mn_now = rt_now; |
817 | } |
938 | } |
818 | |
939 | |
819 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
940 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
820 | block = 0.; |
941 | block = 0.; |
821 | else |
942 | else |
822 | { |
943 | { |
823 | block = MAX_BLOCKTIME; |
944 | block = MAX_BLOCKTIME; |
824 | |
945 | |
825 | if (timercnt) |
946 | if (timercnt) |
826 | { |
947 | { |
827 | ev_tstamp to = timers [0]->at - now + method_fudge; |
948 | ev_tstamp to = timers [0]->at - mn_now + method_fudge; |
828 | if (block > to) block = to; |
949 | if (block > to) block = to; |
829 | } |
950 | } |
830 | |
951 | |
831 | if (periodiccnt) |
952 | if (periodiccnt) |
832 | { |
953 | { |
833 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
954 | ev_tstamp to = periodics [0]->at - rt_now + method_fudge; |
834 | if (block > to) block = to; |
955 | if (block > to) block = to; |
835 | } |
956 | } |
836 | |
957 | |
837 | if (block < 0.) block = 0.; |
958 | if (block < 0.) block = 0.; |
838 | } |
959 | } |
839 | |
960 | |
840 | method_poll (block); |
961 | method_poll (EV_A_ block); |
841 | |
962 | |
842 | /* update ev_now, do magic */ |
963 | /* update rt_now, do magic */ |
843 | time_update (); |
964 | time_update (EV_A); |
844 | |
965 | |
845 | /* queue pending timers and reschedule them */ |
966 | /* queue pending timers and reschedule them */ |
846 | timers_reify (); /* relative timers called last */ |
967 | timers_reify (EV_A); /* relative timers called last */ |
847 | periodics_reify (); /* absolute timers called first */ |
968 | periodics_reify (EV_A); /* absolute timers called first */ |
848 | |
969 | |
849 | /* queue idle watchers unless io or timers are pending */ |
970 | /* queue idle watchers unless io or timers are pending */ |
850 | if (!pendingcnt) |
971 | if (!pendingcnt) |
851 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
972 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
852 | |
973 | |
853 | /* queue check watchers, to be executed first */ |
974 | /* queue check watchers, to be executed first */ |
854 | if (checkcnt) |
975 | if (checkcnt) |
855 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
976 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
856 | |
977 | |
857 | call_pending (); |
978 | call_pending (EV_A); |
858 | } |
979 | } |
859 | while (!ev_loop_done); |
980 | while (activecnt && !loop_done); |
860 | |
981 | |
861 | if (ev_loop_done != 2) |
982 | if (loop_done != 2) |
862 | ev_loop_done = 0; |
983 | loop_done = 0; |
|
|
984 | } |
|
|
985 | |
|
|
986 | void |
|
|
987 | ev_unloop (EV_P_ int how) |
|
|
988 | { |
|
|
989 | loop_done = how; |
863 | } |
990 | } |
864 | |
991 | |
865 | /*****************************************************************************/ |
992 | /*****************************************************************************/ |
866 | |
993 | |
867 | static void |
994 | inline void |
868 | wlist_add (WL *head, WL elem) |
995 | wlist_add (WL *head, WL elem) |
869 | { |
996 | { |
870 | elem->next = *head; |
997 | elem->next = *head; |
871 | *head = elem; |
998 | *head = elem; |
872 | } |
999 | } |
873 | |
1000 | |
874 | static void |
1001 | inline void |
875 | wlist_del (WL *head, WL elem) |
1002 | wlist_del (WL *head, WL elem) |
876 | { |
1003 | { |
877 | while (*head) |
1004 | while (*head) |
878 | { |
1005 | { |
879 | if (*head == elem) |
1006 | if (*head == elem) |
… | |
… | |
884 | |
1011 | |
885 | head = &(*head)->next; |
1012 | head = &(*head)->next; |
886 | } |
1013 | } |
887 | } |
1014 | } |
888 | |
1015 | |
889 | static void |
1016 | inline void |
890 | ev_clear_pending (W w) |
1017 | ev_clear_pending (EV_P_ W w) |
891 | { |
1018 | { |
892 | if (w->pending) |
1019 | if (w->pending) |
893 | { |
1020 | { |
894 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
1021 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
895 | w->pending = 0; |
1022 | w->pending = 0; |
896 | } |
1023 | } |
897 | } |
1024 | } |
898 | |
1025 | |
899 | static void |
1026 | inline void |
900 | ev_start (W w, int active) |
1027 | ev_start (EV_P_ W w, int active) |
901 | { |
1028 | { |
902 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
1029 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
903 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
1030 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
904 | |
1031 | |
905 | w->active = active; |
1032 | w->active = active; |
|
|
1033 | ev_ref (EV_A); |
906 | } |
1034 | } |
907 | |
1035 | |
908 | static void |
1036 | inline void |
909 | ev_stop (W w) |
1037 | ev_stop (EV_P_ W w) |
910 | { |
1038 | { |
|
|
1039 | ev_unref (EV_A); |
911 | w->active = 0; |
1040 | w->active = 0; |
912 | } |
1041 | } |
913 | |
1042 | |
914 | /*****************************************************************************/ |
1043 | /*****************************************************************************/ |
915 | |
1044 | |
916 | void |
1045 | void |
917 | ev_io_start (struct ev_io *w) |
1046 | ev_io_start (EV_P_ struct ev_io *w) |
918 | { |
1047 | { |
919 | int fd = w->fd; |
1048 | int fd = w->fd; |
920 | |
1049 | |
921 | if (ev_is_active (w)) |
1050 | if (ev_is_active (w)) |
922 | return; |
1051 | return; |
923 | |
1052 | |
924 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1053 | assert (("ev_io_start called with negative fd", fd >= 0)); |
925 | |
1054 | |
926 | ev_start ((W)w, 1); |
1055 | ev_start (EV_A_ (W)w, 1); |
927 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
1056 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
928 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1057 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
929 | |
1058 | |
930 | fd_change (fd); |
1059 | fd_change (EV_A_ fd); |
931 | } |
1060 | } |
932 | |
1061 | |
933 | void |
1062 | void |
934 | ev_io_stop (struct ev_io *w) |
1063 | ev_io_stop (EV_P_ struct ev_io *w) |
935 | { |
1064 | { |
936 | ev_clear_pending ((W)w); |
1065 | ev_clear_pending (EV_A_ (W)w); |
937 | if (!ev_is_active (w)) |
1066 | if (!ev_is_active (w)) |
938 | return; |
1067 | return; |
939 | |
1068 | |
940 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1069 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
941 | ev_stop ((W)w); |
1070 | ev_stop (EV_A_ (W)w); |
942 | |
1071 | |
943 | fd_change (w->fd); |
1072 | fd_change (EV_A_ w->fd); |
944 | } |
1073 | } |
945 | |
1074 | |
946 | void |
1075 | void |
947 | ev_timer_start (struct ev_timer *w) |
1076 | ev_timer_start (EV_P_ struct ev_timer *w) |
948 | { |
1077 | { |
949 | if (ev_is_active (w)) |
1078 | if (ev_is_active (w)) |
950 | return; |
1079 | return; |
951 | |
1080 | |
952 | w->at += now; |
1081 | w->at += mn_now; |
953 | |
1082 | |
954 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1083 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
955 | |
1084 | |
956 | ev_start ((W)w, ++timercnt); |
1085 | ev_start (EV_A_ (W)w, ++timercnt); |
957 | array_needsize (timers, timermax, timercnt, ); |
1086 | array_needsize (timers, timermax, timercnt, ); |
958 | timers [timercnt - 1] = w; |
1087 | timers [timercnt - 1] = w; |
959 | upheap ((WT *)timers, timercnt - 1); |
1088 | upheap ((WT *)timers, timercnt - 1); |
960 | } |
1089 | } |
961 | |
1090 | |
962 | void |
1091 | void |
963 | ev_timer_stop (struct ev_timer *w) |
1092 | ev_timer_stop (EV_P_ struct ev_timer *w) |
964 | { |
1093 | { |
965 | ev_clear_pending ((W)w); |
1094 | ev_clear_pending (EV_A_ (W)w); |
966 | if (!ev_is_active (w)) |
1095 | if (!ev_is_active (w)) |
967 | return; |
1096 | return; |
968 | |
1097 | |
969 | if (w->active < timercnt--) |
1098 | if (w->active < timercnt--) |
970 | { |
1099 | { |
… | |
… | |
972 | downheap ((WT *)timers, timercnt, w->active - 1); |
1101 | downheap ((WT *)timers, timercnt, w->active - 1); |
973 | } |
1102 | } |
974 | |
1103 | |
975 | w->at = w->repeat; |
1104 | w->at = w->repeat; |
976 | |
1105 | |
977 | ev_stop ((W)w); |
1106 | ev_stop (EV_A_ (W)w); |
978 | } |
1107 | } |
979 | |
1108 | |
980 | void |
1109 | void |
981 | ev_timer_again (struct ev_timer *w) |
1110 | ev_timer_again (EV_P_ struct ev_timer *w) |
982 | { |
1111 | { |
983 | if (ev_is_active (w)) |
1112 | if (ev_is_active (w)) |
984 | { |
1113 | { |
985 | if (w->repeat) |
1114 | if (w->repeat) |
986 | { |
1115 | { |
987 | w->at = now + w->repeat; |
1116 | w->at = mn_now + w->repeat; |
988 | downheap ((WT *)timers, timercnt, w->active - 1); |
1117 | downheap ((WT *)timers, timercnt, w->active - 1); |
989 | } |
1118 | } |
990 | else |
1119 | else |
991 | ev_timer_stop (w); |
1120 | ev_timer_stop (EV_A_ w); |
992 | } |
1121 | } |
993 | else if (w->repeat) |
1122 | else if (w->repeat) |
994 | ev_timer_start (w); |
1123 | ev_timer_start (EV_A_ w); |
995 | } |
1124 | } |
996 | |
1125 | |
997 | void |
1126 | void |
998 | ev_periodic_start (struct ev_periodic *w) |
1127 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
999 | { |
1128 | { |
1000 | if (ev_is_active (w)) |
1129 | if (ev_is_active (w)) |
1001 | return; |
1130 | return; |
1002 | |
1131 | |
1003 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1132 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1004 | |
1133 | |
1005 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1134 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1006 | if (w->interval) |
1135 | if (w->interval) |
1007 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
1136 | w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; |
1008 | |
1137 | |
1009 | ev_start ((W)w, ++periodiccnt); |
1138 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1010 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1139 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1011 | periodics [periodiccnt - 1] = w; |
1140 | periodics [periodiccnt - 1] = w; |
1012 | upheap ((WT *)periodics, periodiccnt - 1); |
1141 | upheap ((WT *)periodics, periodiccnt - 1); |
1013 | } |
1142 | } |
1014 | |
1143 | |
1015 | void |
1144 | void |
1016 | ev_periodic_stop (struct ev_periodic *w) |
1145 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1017 | { |
1146 | { |
1018 | ev_clear_pending ((W)w); |
1147 | ev_clear_pending (EV_A_ (W)w); |
1019 | if (!ev_is_active (w)) |
1148 | if (!ev_is_active (w)) |
1020 | return; |
1149 | return; |
1021 | |
1150 | |
1022 | if (w->active < periodiccnt--) |
1151 | if (w->active < periodiccnt--) |
1023 | { |
1152 | { |
1024 | periodics [w->active - 1] = periodics [periodiccnt]; |
1153 | periodics [w->active - 1] = periodics [periodiccnt]; |
1025 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1154 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1026 | } |
1155 | } |
1027 | |
1156 | |
1028 | ev_stop ((W)w); |
1157 | ev_stop (EV_A_ (W)w); |
|
|
1158 | } |
|
|
1159 | |
|
|
1160 | void |
|
|
1161 | ev_idle_start (EV_P_ struct ev_idle *w) |
|
|
1162 | { |
|
|
1163 | if (ev_is_active (w)) |
|
|
1164 | return; |
|
|
1165 | |
|
|
1166 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1167 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
1168 | idles [idlecnt - 1] = w; |
|
|
1169 | } |
|
|
1170 | |
|
|
1171 | void |
|
|
1172 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1173 | { |
|
|
1174 | ev_clear_pending (EV_A_ (W)w); |
|
|
1175 | if (ev_is_active (w)) |
|
|
1176 | return; |
|
|
1177 | |
|
|
1178 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
1179 | ev_stop (EV_A_ (W)w); |
|
|
1180 | } |
|
|
1181 | |
|
|
1182 | void |
|
|
1183 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1184 | { |
|
|
1185 | if (ev_is_active (w)) |
|
|
1186 | return; |
|
|
1187 | |
|
|
1188 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1189 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
1190 | prepares [preparecnt - 1] = w; |
|
|
1191 | } |
|
|
1192 | |
|
|
1193 | void |
|
|
1194 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1195 | { |
|
|
1196 | ev_clear_pending (EV_A_ (W)w); |
|
|
1197 | if (ev_is_active (w)) |
|
|
1198 | return; |
|
|
1199 | |
|
|
1200 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
1201 | ev_stop (EV_A_ (W)w); |
|
|
1202 | } |
|
|
1203 | |
|
|
1204 | void |
|
|
1205 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1206 | { |
|
|
1207 | if (ev_is_active (w)) |
|
|
1208 | return; |
|
|
1209 | |
|
|
1210 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1211 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
1212 | checks [checkcnt - 1] = w; |
|
|
1213 | } |
|
|
1214 | |
|
|
1215 | void |
|
|
1216 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1217 | { |
|
|
1218 | ev_clear_pending (EV_A_ (W)w); |
|
|
1219 | if (ev_is_active (w)) |
|
|
1220 | return; |
|
|
1221 | |
|
|
1222 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
1223 | ev_stop (EV_A_ (W)w); |
1029 | } |
1224 | } |
1030 | |
1225 | |
1031 | #ifndef SA_RESTART |
1226 | #ifndef SA_RESTART |
1032 | # define SA_RESTART 0 |
1227 | # define SA_RESTART 0 |
1033 | #endif |
1228 | #endif |
1034 | |
1229 | |
1035 | void |
1230 | void |
1036 | ev_signal_start (struct ev_signal *w) |
1231 | ev_signal_start (EV_P_ struct ev_signal *w) |
1037 | { |
1232 | { |
|
|
1233 | #if EV_MULTIPLICITY |
|
|
1234 | assert (("signal watchers are only supported in the default loop", loop == default_loop)); |
|
|
1235 | #endif |
1038 | if (ev_is_active (w)) |
1236 | if (ev_is_active (w)) |
1039 | return; |
1237 | return; |
1040 | |
1238 | |
1041 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1239 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1042 | |
1240 | |
1043 | ev_start ((W)w, 1); |
1241 | ev_start (EV_A_ (W)w, 1); |
1044 | array_needsize (signals, signalmax, w->signum, signals_init); |
1242 | array_needsize (signals, signalmax, w->signum, signals_init); |
1045 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1243 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1046 | |
1244 | |
1047 | if (!w->next) |
1245 | if (!w->next) |
1048 | { |
1246 | { |
… | |
… | |
1053 | sigaction (w->signum, &sa, 0); |
1251 | sigaction (w->signum, &sa, 0); |
1054 | } |
1252 | } |
1055 | } |
1253 | } |
1056 | |
1254 | |
1057 | void |
1255 | void |
1058 | ev_signal_stop (struct ev_signal *w) |
1256 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1059 | { |
1257 | { |
1060 | ev_clear_pending ((W)w); |
1258 | ev_clear_pending (EV_A_ (W)w); |
1061 | if (!ev_is_active (w)) |
1259 | if (!ev_is_active (w)) |
1062 | return; |
1260 | return; |
1063 | |
1261 | |
1064 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1262 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1065 | ev_stop ((W)w); |
1263 | ev_stop (EV_A_ (W)w); |
1066 | |
1264 | |
1067 | if (!signals [w->signum - 1].head) |
1265 | if (!signals [w->signum - 1].head) |
1068 | signal (w->signum, SIG_DFL); |
1266 | signal (w->signum, SIG_DFL); |
1069 | } |
1267 | } |
1070 | |
1268 | |
1071 | void |
1269 | void |
1072 | ev_idle_start (struct ev_idle *w) |
1270 | ev_child_start (EV_P_ struct ev_child *w) |
1073 | { |
1271 | { |
|
|
1272 | #if EV_MULTIPLICITY |
|
|
1273 | assert (("child watchers are only supported in the default loop", loop == default_loop)); |
|
|
1274 | #endif |
1074 | if (ev_is_active (w)) |
1275 | if (ev_is_active (w)) |
1075 | return; |
1276 | return; |
1076 | |
1277 | |
1077 | ev_start ((W)w, ++idlecnt); |
1278 | ev_start (EV_A_ (W)w, 1); |
1078 | array_needsize (idles, idlemax, idlecnt, ); |
1279 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1079 | idles [idlecnt - 1] = w; |
|
|
1080 | } |
1280 | } |
1081 | |
1281 | |
1082 | void |
1282 | void |
1083 | ev_idle_stop (struct ev_idle *w) |
1283 | ev_child_stop (EV_P_ struct ev_child *w) |
1084 | { |
1284 | { |
1085 | ev_clear_pending ((W)w); |
1285 | ev_clear_pending (EV_A_ (W)w); |
1086 | if (ev_is_active (w)) |
1286 | if (ev_is_active (w)) |
1087 | return; |
1287 | return; |
1088 | |
1288 | |
1089 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
1090 | ev_stop ((W)w); |
|
|
1091 | } |
|
|
1092 | |
|
|
1093 | void |
|
|
1094 | ev_prepare_start (struct ev_prepare *w) |
|
|
1095 | { |
|
|
1096 | if (ev_is_active (w)) |
|
|
1097 | return; |
|
|
1098 | |
|
|
1099 | ev_start ((W)w, ++preparecnt); |
|
|
1100 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
1101 | prepares [preparecnt - 1] = w; |
|
|
1102 | } |
|
|
1103 | |
|
|
1104 | void |
|
|
1105 | ev_prepare_stop (struct ev_prepare *w) |
|
|
1106 | { |
|
|
1107 | ev_clear_pending ((W)w); |
|
|
1108 | if (ev_is_active (w)) |
|
|
1109 | return; |
|
|
1110 | |
|
|
1111 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
1112 | ev_stop ((W)w); |
|
|
1113 | } |
|
|
1114 | |
|
|
1115 | void |
|
|
1116 | ev_check_start (struct ev_check *w) |
|
|
1117 | { |
|
|
1118 | if (ev_is_active (w)) |
|
|
1119 | return; |
|
|
1120 | |
|
|
1121 | ev_start ((W)w, ++checkcnt); |
|
|
1122 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
1123 | checks [checkcnt - 1] = w; |
|
|
1124 | } |
|
|
1125 | |
|
|
1126 | void |
|
|
1127 | ev_check_stop (struct ev_check *w) |
|
|
1128 | { |
|
|
1129 | ev_clear_pending ((W)w); |
|
|
1130 | if (ev_is_active (w)) |
|
|
1131 | return; |
|
|
1132 | |
|
|
1133 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
1134 | ev_stop ((W)w); |
|
|
1135 | } |
|
|
1136 | |
|
|
1137 | void |
|
|
1138 | ev_child_start (struct ev_child *w) |
|
|
1139 | { |
|
|
1140 | if (ev_is_active (w)) |
|
|
1141 | return; |
|
|
1142 | |
|
|
1143 | ev_start ((W)w, 1); |
|
|
1144 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
1145 | } |
|
|
1146 | |
|
|
1147 | void |
|
|
1148 | ev_child_stop (struct ev_child *w) |
|
|
1149 | { |
|
|
1150 | ev_clear_pending ((W)w); |
|
|
1151 | if (ev_is_active (w)) |
|
|
1152 | return; |
|
|
1153 | |
|
|
1154 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1289 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1155 | ev_stop ((W)w); |
1290 | ev_stop (EV_A_ (W)w); |
1156 | } |
1291 | } |
1157 | |
1292 | |
1158 | /*****************************************************************************/ |
1293 | /*****************************************************************************/ |
1159 | |
1294 | |
1160 | struct ev_once |
1295 | struct ev_once |
… | |
… | |
1164 | void (*cb)(int revents, void *arg); |
1299 | void (*cb)(int revents, void *arg); |
1165 | void *arg; |
1300 | void *arg; |
1166 | }; |
1301 | }; |
1167 | |
1302 | |
1168 | static void |
1303 | static void |
1169 | once_cb (struct ev_once *once, int revents) |
1304 | once_cb (EV_P_ struct ev_once *once, int revents) |
1170 | { |
1305 | { |
1171 | void (*cb)(int revents, void *arg) = once->cb; |
1306 | void (*cb)(int revents, void *arg) = once->cb; |
1172 | void *arg = once->arg; |
1307 | void *arg = once->arg; |
1173 | |
1308 | |
1174 | ev_io_stop (&once->io); |
1309 | ev_io_stop (EV_A_ &once->io); |
1175 | ev_timer_stop (&once->to); |
1310 | ev_timer_stop (EV_A_ &once->to); |
1176 | free (once); |
1311 | free (once); |
1177 | |
1312 | |
1178 | cb (revents, arg); |
1313 | cb (revents, arg); |
1179 | } |
1314 | } |
1180 | |
1315 | |
1181 | static void |
1316 | static void |
1182 | once_cb_io (struct ev_io *w, int revents) |
1317 | once_cb_io (EV_P_ struct ev_io *w, int revents) |
1183 | { |
1318 | { |
1184 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1319 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1185 | } |
1320 | } |
1186 | |
1321 | |
1187 | static void |
1322 | static void |
1188 | once_cb_to (struct ev_timer *w, int revents) |
1323 | once_cb_to (EV_P_ struct ev_timer *w, int revents) |
1189 | { |
1324 | { |
1190 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1325 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1191 | } |
1326 | } |
1192 | |
1327 | |
1193 | void |
1328 | void |
1194 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1329 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1195 | { |
1330 | { |
1196 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1331 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1197 | |
1332 | |
1198 | if (!once) |
1333 | if (!once) |
1199 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1334 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
… | |
… | |
1204 | |
1339 | |
1205 | ev_watcher_init (&once->io, once_cb_io); |
1340 | ev_watcher_init (&once->io, once_cb_io); |
1206 | if (fd >= 0) |
1341 | if (fd >= 0) |
1207 | { |
1342 | { |
1208 | ev_io_set (&once->io, fd, events); |
1343 | ev_io_set (&once->io, fd, events); |
1209 | ev_io_start (&once->io); |
1344 | ev_io_start (EV_A_ &once->io); |
1210 | } |
1345 | } |
1211 | |
1346 | |
1212 | ev_watcher_init (&once->to, once_cb_to); |
1347 | ev_watcher_init (&once->to, once_cb_to); |
1213 | if (timeout >= 0.) |
1348 | if (timeout >= 0.) |
1214 | { |
1349 | { |
1215 | ev_timer_set (&once->to, timeout, 0.); |
1350 | ev_timer_set (&once->to, timeout, 0.); |
1216 | ev_timer_start (&once->to); |
1351 | ev_timer_start (EV_A_ &once->to); |
1217 | } |
1352 | } |
1218 | } |
1353 | } |
1219 | } |
1354 | } |
1220 | |
1355 | |
1221 | /*****************************************************************************/ |
|
|
1222 | |
|
|
1223 | #if 0 |
|
|
1224 | |
|
|
1225 | struct ev_io wio; |
|
|
1226 | |
|
|
1227 | static void |
|
|
1228 | sin_cb (struct ev_io *w, int revents) |
|
|
1229 | { |
|
|
1230 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
|
|
1231 | } |
|
|
1232 | |
|
|
1233 | static void |
|
|
1234 | ocb (struct ev_timer *w, int revents) |
|
|
1235 | { |
|
|
1236 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
|
|
1237 | ev_timer_stop (w); |
|
|
1238 | ev_timer_start (w); |
|
|
1239 | } |
|
|
1240 | |
|
|
1241 | static void |
|
|
1242 | scb (struct ev_signal *w, int revents) |
|
|
1243 | { |
|
|
1244 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
1245 | ev_io_stop (&wio); |
|
|
1246 | ev_io_start (&wio); |
|
|
1247 | } |
|
|
1248 | |
|
|
1249 | static void |
|
|
1250 | gcb (struct ev_signal *w, int revents) |
|
|
1251 | { |
|
|
1252 | fprintf (stderr, "generic %x\n", revents); |
|
|
1253 | |
|
|
1254 | } |
|
|
1255 | |
|
|
1256 | int main (void) |
|
|
1257 | { |
|
|
1258 | ev_init (0); |
|
|
1259 | |
|
|
1260 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
|
|
1261 | ev_io_start (&wio); |
|
|
1262 | |
|
|
1263 | struct ev_timer t[10000]; |
|
|
1264 | |
|
|
1265 | #if 0 |
|
|
1266 | int i; |
|
|
1267 | for (i = 0; i < 10000; ++i) |
|
|
1268 | { |
|
|
1269 | struct ev_timer *w = t + i; |
|
|
1270 | ev_watcher_init (w, ocb, i); |
|
|
1271 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
|
|
1272 | ev_timer_start (w); |
|
|
1273 | if (drand48 () < 0.5) |
|
|
1274 | ev_timer_stop (w); |
|
|
1275 | } |
|
|
1276 | #endif |
|
|
1277 | |
|
|
1278 | struct ev_timer t1; |
|
|
1279 | ev_timer_init (&t1, ocb, 5, 10); |
|
|
1280 | ev_timer_start (&t1); |
|
|
1281 | |
|
|
1282 | struct ev_signal sig; |
|
|
1283 | ev_signal_init (&sig, scb, SIGQUIT); |
|
|
1284 | ev_signal_start (&sig); |
|
|
1285 | |
|
|
1286 | struct ev_check cw; |
|
|
1287 | ev_check_init (&cw, gcb); |
|
|
1288 | ev_check_start (&cw); |
|
|
1289 | |
|
|
1290 | struct ev_idle iw; |
|
|
1291 | ev_idle_init (&iw, gcb); |
|
|
1292 | ev_idle_start (&iw); |
|
|
1293 | |
|
|
1294 | ev_loop (0); |
|
|
1295 | |
|
|
1296 | return 0; |
|
|
1297 | } |
|
|
1298 | |
|
|
1299 | #endif |
|
|
1300 | |
|
|
1301 | |
|
|
1302 | |
|
|
1303 | |
|
|