… | |
… | |
111 | #include <time.h> |
111 | #include <time.h> |
112 | |
112 | |
113 | #include <signal.h> |
113 | #include <signal.h> |
114 | |
114 | |
115 | #ifndef _WIN32 |
115 | #ifndef _WIN32 |
116 | # include <unistd.h> |
|
|
117 | # include <sys/time.h> |
116 | # include <sys/time.h> |
118 | # include <sys/wait.h> |
117 | # include <sys/wait.h> |
|
|
118 | # include <unistd.h> |
119 | #else |
119 | #else |
120 | # define WIN32_LEAN_AND_MEAN |
120 | # define WIN32_LEAN_AND_MEAN |
121 | # include <windows.h> |
121 | # include <windows.h> |
122 | # ifndef EV_SELECT_IS_WINSOCKET |
122 | # ifndef EV_SELECT_IS_WINSOCKET |
123 | # define EV_SELECT_IS_WINSOCKET 1 |
123 | # define EV_SELECT_IS_WINSOCKET 1 |
… | |
… | |
156 | |
156 | |
157 | #ifndef EV_USE_PORT |
157 | #ifndef EV_USE_PORT |
158 | # define EV_USE_PORT 0 |
158 | # define EV_USE_PORT 0 |
159 | #endif |
159 | #endif |
160 | |
160 | |
|
|
161 | #ifndef EV_PID_HASHSIZE |
|
|
162 | # if EV_MINIMAL |
|
|
163 | # define EV_PID_HASHSIZE 1 |
|
|
164 | # else |
|
|
165 | # define EV_PID_HASHSIZE 16 |
|
|
166 | # endif |
|
|
167 | #endif |
|
|
168 | |
161 | /**/ |
169 | /**/ |
162 | |
170 | |
163 | #ifndef CLOCK_MONOTONIC |
171 | #ifndef CLOCK_MONOTONIC |
164 | # undef EV_USE_MONOTONIC |
172 | # undef EV_USE_MONOTONIC |
165 | # define EV_USE_MONOTONIC 0 |
173 | # define EV_USE_MONOTONIC 0 |
… | |
… | |
176 | |
184 | |
177 | /**/ |
185 | /**/ |
178 | |
186 | |
179 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
187 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
180 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
188 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
181 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
|
|
182 | /*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
189 | /*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
183 | |
190 | |
184 | #ifdef EV_H |
191 | #ifdef EV_H |
185 | # include EV_H |
192 | # include EV_H |
186 | #else |
193 | #else |
187 | # include "ev.h" |
194 | # include "ev.h" |
188 | #endif |
195 | #endif |
189 | |
196 | |
190 | #if __GNUC__ >= 3 |
197 | #if __GNUC__ >= 3 |
191 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
198 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
199 | # define inline_size static inline /* inline for codesize */ |
|
|
200 | # if EV_MINIMAL |
|
|
201 | # define noinline __attribute__ ((noinline)) |
|
|
202 | # define inline_speed static noinline |
|
|
203 | # else |
|
|
204 | # define noinline |
192 | # define inline static inline |
205 | # define inline_speed static inline |
|
|
206 | # endif |
193 | #else |
207 | #else |
194 | # define expect(expr,value) (expr) |
208 | # define expect(expr,value) (expr) |
|
|
209 | # define inline_speed static |
195 | # define inline static |
210 | # define inline_size static |
|
|
211 | # define noinline |
196 | #endif |
212 | #endif |
197 | |
213 | |
198 | #define expect_false(expr) expect ((expr) != 0, 0) |
214 | #define expect_false(expr) expect ((expr) != 0, 0) |
199 | #define expect_true(expr) expect ((expr) != 0, 1) |
215 | #define expect_true(expr) expect ((expr) != 0, 1) |
200 | |
216 | |
… | |
… | |
202 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
218 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
203 | |
219 | |
204 | #define EMPTY0 /* required for microsofts broken pseudo-c compiler */ |
220 | #define EMPTY0 /* required for microsofts broken pseudo-c compiler */ |
205 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
221 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
206 | |
222 | |
207 | typedef struct ev_watcher *W; |
223 | typedef ev_watcher *W; |
208 | typedef struct ev_watcher_list *WL; |
224 | typedef ev_watcher_list *WL; |
209 | typedef struct ev_watcher_time *WT; |
225 | typedef ev_watcher_time *WT; |
210 | |
226 | |
211 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
227 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
212 | |
228 | |
213 | #ifdef _WIN32 |
229 | #ifdef _WIN32 |
214 | # include "ev_win32.c" |
230 | # include "ev_win32.c" |
… | |
… | |
216 | |
232 | |
217 | /*****************************************************************************/ |
233 | /*****************************************************************************/ |
218 | |
234 | |
219 | static void (*syserr_cb)(const char *msg); |
235 | static void (*syserr_cb)(const char *msg); |
220 | |
236 | |
|
|
237 | void |
221 | void ev_set_syserr_cb (void (*cb)(const char *msg)) |
238 | ev_set_syserr_cb (void (*cb)(const char *msg)) |
222 | { |
239 | { |
223 | syserr_cb = cb; |
240 | syserr_cb = cb; |
224 | } |
241 | } |
225 | |
242 | |
226 | static void |
243 | static void noinline |
227 | syserr (const char *msg) |
244 | syserr (const char *msg) |
228 | { |
245 | { |
229 | if (!msg) |
246 | if (!msg) |
230 | msg = "(libev) system error"; |
247 | msg = "(libev) system error"; |
231 | |
248 | |
… | |
… | |
236 | perror (msg); |
253 | perror (msg); |
237 | abort (); |
254 | abort (); |
238 | } |
255 | } |
239 | } |
256 | } |
240 | |
257 | |
241 | static void *(*alloc)(void *ptr, long size); |
258 | static void *(*alloc)(void *ptr, size_t size) = realloc; |
242 | |
259 | |
|
|
260 | void |
243 | void ev_set_allocator (void *(*cb)(void *ptr, long size)) |
261 | ev_set_allocator (void *(*cb)(void *ptr, size_t size)) |
244 | { |
262 | { |
245 | alloc = cb; |
263 | alloc = cb; |
246 | } |
264 | } |
247 | |
265 | |
248 | static void * |
266 | inline_speed void * |
249 | ev_realloc (void *ptr, long size) |
267 | ev_realloc (void *ptr, size_t size) |
250 | { |
268 | { |
251 | ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); |
269 | ptr = alloc (ptr, size); |
252 | |
270 | |
253 | if (!ptr && size) |
271 | if (!ptr && size) |
254 | { |
272 | { |
255 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
273 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", (long)size); |
256 | abort (); |
274 | abort (); |
257 | } |
275 | } |
258 | |
276 | |
259 | return ptr; |
277 | return ptr; |
260 | } |
278 | } |
… | |
… | |
320 | gettimeofday (&tv, 0); |
338 | gettimeofday (&tv, 0); |
321 | return tv.tv_sec + tv.tv_usec * 1e-6; |
339 | return tv.tv_sec + tv.tv_usec * 1e-6; |
322 | #endif |
340 | #endif |
323 | } |
341 | } |
324 | |
342 | |
325 | inline ev_tstamp |
343 | ev_tstamp inline_size |
326 | get_clock (void) |
344 | get_clock (void) |
327 | { |
345 | { |
328 | #if EV_USE_MONOTONIC |
346 | #if EV_USE_MONOTONIC |
329 | if (expect_true (have_monotonic)) |
347 | if (expect_true (have_monotonic)) |
330 | { |
348 | { |
… | |
… | |
373 | #define array_free(stem, idx) \ |
391 | #define array_free(stem, idx) \ |
374 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
392 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
375 | |
393 | |
376 | /*****************************************************************************/ |
394 | /*****************************************************************************/ |
377 | |
395 | |
378 | static void |
396 | void noinline |
379 | anfds_init (ANFD *base, int count) |
|
|
380 | { |
|
|
381 | while (count--) |
|
|
382 | { |
|
|
383 | base->head = 0; |
|
|
384 | base->events = EV_NONE; |
|
|
385 | base->reify = 0; |
|
|
386 | |
|
|
387 | ++base; |
|
|
388 | } |
|
|
389 | } |
|
|
390 | |
|
|
391 | void |
|
|
392 | ev_feed_event (EV_P_ void *w, int revents) |
397 | ev_feed_event (EV_P_ void *w, int revents) |
393 | { |
398 | { |
394 | W w_ = (W)w; |
399 | W w_ = (W)w; |
395 | |
400 | |
396 | if (expect_false (w_->pending)) |
401 | if (expect_false (w_->pending)) |
… | |
… | |
403 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); |
408 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); |
404 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
409 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
405 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
410 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
406 | } |
411 | } |
407 | |
412 | |
408 | static void |
413 | void inline_size |
409 | queue_events (EV_P_ W *events, int eventcnt, int type) |
414 | queue_events (EV_P_ W *events, int eventcnt, int type) |
410 | { |
415 | { |
411 | int i; |
416 | int i; |
412 | |
417 | |
413 | for (i = 0; i < eventcnt; ++i) |
418 | for (i = 0; i < eventcnt; ++i) |
414 | ev_feed_event (EV_A_ events [i], type); |
419 | ev_feed_event (EV_A_ events [i], type); |
415 | } |
420 | } |
416 | |
421 | |
417 | inline void |
422 | /*****************************************************************************/ |
|
|
423 | |
|
|
424 | void inline_size |
|
|
425 | anfds_init (ANFD *base, int count) |
|
|
426 | { |
|
|
427 | while (count--) |
|
|
428 | { |
|
|
429 | base->head = 0; |
|
|
430 | base->events = EV_NONE; |
|
|
431 | base->reify = 0; |
|
|
432 | |
|
|
433 | ++base; |
|
|
434 | } |
|
|
435 | } |
|
|
436 | |
|
|
437 | void inline_speed |
418 | fd_event (EV_P_ int fd, int revents) |
438 | fd_event (EV_P_ int fd, int revents) |
419 | { |
439 | { |
420 | ANFD *anfd = anfds + fd; |
440 | ANFD *anfd = anfds + fd; |
421 | struct ev_io *w; |
441 | ev_io *w; |
422 | |
442 | |
423 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
443 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
424 | { |
444 | { |
425 | int ev = w->events & revents; |
445 | int ev = w->events & revents; |
426 | |
446 | |
427 | if (ev) |
447 | if (ev) |
428 | ev_feed_event (EV_A_ (W)w, ev); |
448 | ev_feed_event (EV_A_ (W)w, ev); |
… | |
… | |
433 | ev_feed_fd_event (EV_P_ int fd, int revents) |
453 | ev_feed_fd_event (EV_P_ int fd, int revents) |
434 | { |
454 | { |
435 | fd_event (EV_A_ fd, revents); |
455 | fd_event (EV_A_ fd, revents); |
436 | } |
456 | } |
437 | |
457 | |
438 | /*****************************************************************************/ |
458 | void inline_size |
439 | |
|
|
440 | inline void |
|
|
441 | fd_reify (EV_P) |
459 | fd_reify (EV_P) |
442 | { |
460 | { |
443 | int i; |
461 | int i; |
444 | |
462 | |
445 | for (i = 0; i < fdchangecnt; ++i) |
463 | for (i = 0; i < fdchangecnt; ++i) |
446 | { |
464 | { |
447 | int fd = fdchanges [i]; |
465 | int fd = fdchanges [i]; |
448 | ANFD *anfd = anfds + fd; |
466 | ANFD *anfd = anfds + fd; |
449 | struct ev_io *w; |
467 | ev_io *w; |
450 | |
468 | |
451 | int events = 0; |
469 | int events = 0; |
452 | |
470 | |
453 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
471 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
454 | events |= w->events; |
472 | events |= w->events; |
455 | |
473 | |
456 | #if EV_SELECT_IS_WINSOCKET |
474 | #if EV_SELECT_IS_WINSOCKET |
457 | if (events) |
475 | if (events) |
458 | { |
476 | { |
… | |
… | |
469 | } |
487 | } |
470 | |
488 | |
471 | fdchangecnt = 0; |
489 | fdchangecnt = 0; |
472 | } |
490 | } |
473 | |
491 | |
474 | static void |
492 | void inline_size |
475 | fd_change (EV_P_ int fd) |
493 | fd_change (EV_P_ int fd) |
476 | { |
494 | { |
477 | if (expect_false (anfds [fd].reify)) |
495 | if (expect_false (anfds [fd].reify)) |
478 | return; |
496 | return; |
479 | |
497 | |
… | |
… | |
482 | ++fdchangecnt; |
500 | ++fdchangecnt; |
483 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
501 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
484 | fdchanges [fdchangecnt - 1] = fd; |
502 | fdchanges [fdchangecnt - 1] = fd; |
485 | } |
503 | } |
486 | |
504 | |
487 | static void |
505 | void inline_speed |
488 | fd_kill (EV_P_ int fd) |
506 | fd_kill (EV_P_ int fd) |
489 | { |
507 | { |
490 | struct ev_io *w; |
508 | ev_io *w; |
491 | |
509 | |
492 | while ((w = (struct ev_io *)anfds [fd].head)) |
510 | while ((w = (ev_io *)anfds [fd].head)) |
493 | { |
511 | { |
494 | ev_io_stop (EV_A_ w); |
512 | ev_io_stop (EV_A_ w); |
495 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
513 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
496 | } |
514 | } |
497 | } |
515 | } |
498 | |
516 | |
499 | inline int |
517 | int inline_size |
500 | fd_valid (int fd) |
518 | fd_valid (int fd) |
501 | { |
519 | { |
502 | #ifdef _WIN32 |
520 | #ifdef _WIN32 |
503 | return _get_osfhandle (fd) != -1; |
521 | return _get_osfhandle (fd) != -1; |
504 | #else |
522 | #else |
505 | return fcntl (fd, F_GETFD) != -1; |
523 | return fcntl (fd, F_GETFD) != -1; |
506 | #endif |
524 | #endif |
507 | } |
525 | } |
508 | |
526 | |
509 | /* called on EBADF to verify fds */ |
527 | /* called on EBADF to verify fds */ |
510 | static void |
528 | static void noinline |
511 | fd_ebadf (EV_P) |
529 | fd_ebadf (EV_P) |
512 | { |
530 | { |
513 | int fd; |
531 | int fd; |
514 | |
532 | |
515 | for (fd = 0; fd < anfdmax; ++fd) |
533 | for (fd = 0; fd < anfdmax; ++fd) |
… | |
… | |
517 | if (!fd_valid (fd) == -1 && errno == EBADF) |
535 | if (!fd_valid (fd) == -1 && errno == EBADF) |
518 | fd_kill (EV_A_ fd); |
536 | fd_kill (EV_A_ fd); |
519 | } |
537 | } |
520 | |
538 | |
521 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
539 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
522 | static void |
540 | static void noinline |
523 | fd_enomem (EV_P) |
541 | fd_enomem (EV_P) |
524 | { |
542 | { |
525 | int fd; |
543 | int fd; |
526 | |
544 | |
527 | for (fd = anfdmax; fd--; ) |
545 | for (fd = anfdmax; fd--; ) |
… | |
… | |
531 | return; |
549 | return; |
532 | } |
550 | } |
533 | } |
551 | } |
534 | |
552 | |
535 | /* usually called after fork if backend needs to re-arm all fds from scratch */ |
553 | /* usually called after fork if backend needs to re-arm all fds from scratch */ |
536 | static void |
554 | static void noinline |
537 | fd_rearm_all (EV_P) |
555 | fd_rearm_all (EV_P) |
538 | { |
556 | { |
539 | int fd; |
557 | int fd; |
540 | |
558 | |
541 | /* this should be highly optimised to not do anything but set a flag */ |
559 | /* this should be highly optimised to not do anything but set a flag */ |
… | |
… | |
547 | } |
565 | } |
548 | } |
566 | } |
549 | |
567 | |
550 | /*****************************************************************************/ |
568 | /*****************************************************************************/ |
551 | |
569 | |
552 | static void |
570 | void inline_speed |
553 | upheap (WT *heap, int k) |
571 | upheap (WT *heap, int k) |
554 | { |
572 | { |
555 | WT w = heap [k]; |
573 | WT w = heap [k]; |
556 | |
574 | |
557 | while (k && heap [k >> 1]->at > w->at) |
575 | while (k && heap [k >> 1]->at > w->at) |
… | |
… | |
564 | heap [k] = w; |
582 | heap [k] = w; |
565 | ((W)heap [k])->active = k + 1; |
583 | ((W)heap [k])->active = k + 1; |
566 | |
584 | |
567 | } |
585 | } |
568 | |
586 | |
569 | static void |
587 | void inline_speed |
570 | downheap (WT *heap, int N, int k) |
588 | downheap (WT *heap, int N, int k) |
571 | { |
589 | { |
572 | WT w = heap [k]; |
590 | WT w = heap [k]; |
573 | |
591 | |
574 | while (k < (N >> 1)) |
592 | while (k < (N >> 1)) |
… | |
… | |
588 | |
606 | |
589 | heap [k] = w; |
607 | heap [k] = w; |
590 | ((W)heap [k])->active = k + 1; |
608 | ((W)heap [k])->active = k + 1; |
591 | } |
609 | } |
592 | |
610 | |
593 | inline void |
611 | void inline_size |
594 | adjustheap (WT *heap, int N, int k) |
612 | adjustheap (WT *heap, int N, int k) |
595 | { |
613 | { |
596 | upheap (heap, k); |
614 | upheap (heap, k); |
597 | downheap (heap, N, k); |
615 | downheap (heap, N, k); |
598 | } |
616 | } |
… | |
… | |
608 | static ANSIG *signals; |
626 | static ANSIG *signals; |
609 | static int signalmax; |
627 | static int signalmax; |
610 | |
628 | |
611 | static int sigpipe [2]; |
629 | static int sigpipe [2]; |
612 | static sig_atomic_t volatile gotsig; |
630 | static sig_atomic_t volatile gotsig; |
613 | static struct ev_io sigev; |
631 | static ev_io sigev; |
614 | |
632 | |
615 | static void |
633 | void inline_size |
616 | signals_init (ANSIG *base, int count) |
634 | signals_init (ANSIG *base, int count) |
617 | { |
635 | { |
618 | while (count--) |
636 | while (count--) |
619 | { |
637 | { |
620 | base->head = 0; |
638 | base->head = 0; |
… | |
… | |
640 | write (sigpipe [1], &signum, 1); |
658 | write (sigpipe [1], &signum, 1); |
641 | errno = old_errno; |
659 | errno = old_errno; |
642 | } |
660 | } |
643 | } |
661 | } |
644 | |
662 | |
645 | void |
663 | void noinline |
646 | ev_feed_signal_event (EV_P_ int signum) |
664 | ev_feed_signal_event (EV_P_ int signum) |
647 | { |
665 | { |
648 | WL w; |
666 | WL w; |
649 | |
667 | |
650 | #if EV_MULTIPLICITY |
668 | #if EV_MULTIPLICITY |
… | |
… | |
661 | for (w = signals [signum].head; w; w = w->next) |
679 | for (w = signals [signum].head; w; w = w->next) |
662 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
680 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
663 | } |
681 | } |
664 | |
682 | |
665 | static void |
683 | static void |
666 | sigcb (EV_P_ struct ev_io *iow, int revents) |
684 | sigcb (EV_P_ ev_io *iow, int revents) |
667 | { |
685 | { |
668 | int signum; |
686 | int signum; |
669 | |
687 | |
670 | read (sigpipe [0], &revents, 1); |
688 | read (sigpipe [0], &revents, 1); |
671 | gotsig = 0; |
689 | gotsig = 0; |
… | |
… | |
673 | for (signum = signalmax; signum--; ) |
691 | for (signum = signalmax; signum--; ) |
674 | if (signals [signum].gotsig) |
692 | if (signals [signum].gotsig) |
675 | ev_feed_signal_event (EV_A_ signum + 1); |
693 | ev_feed_signal_event (EV_A_ signum + 1); |
676 | } |
694 | } |
677 | |
695 | |
678 | static void |
696 | void inline_size |
679 | fd_intern (int fd) |
697 | fd_intern (int fd) |
680 | { |
698 | { |
681 | #ifdef _WIN32 |
699 | #ifdef _WIN32 |
682 | int arg = 1; |
700 | int arg = 1; |
683 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
701 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
… | |
… | |
685 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
703 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
686 | fcntl (fd, F_SETFL, O_NONBLOCK); |
704 | fcntl (fd, F_SETFL, O_NONBLOCK); |
687 | #endif |
705 | #endif |
688 | } |
706 | } |
689 | |
707 | |
690 | static void |
708 | static void noinline |
691 | siginit (EV_P) |
709 | siginit (EV_P) |
692 | { |
710 | { |
693 | fd_intern (sigpipe [0]); |
711 | fd_intern (sigpipe [0]); |
694 | fd_intern (sigpipe [1]); |
712 | fd_intern (sigpipe [1]); |
695 | |
713 | |
… | |
… | |
698 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
716 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
699 | } |
717 | } |
700 | |
718 | |
701 | /*****************************************************************************/ |
719 | /*****************************************************************************/ |
702 | |
720 | |
703 | static struct ev_child *childs [PID_HASHSIZE]; |
721 | static ev_child *childs [EV_PID_HASHSIZE]; |
704 | |
722 | |
705 | #ifndef _WIN32 |
723 | #ifndef _WIN32 |
706 | |
724 | |
707 | static struct ev_signal childev; |
725 | static ev_signal childev; |
708 | |
726 | |
709 | #ifndef WCONTINUED |
727 | void inline_speed |
710 | # define WCONTINUED 0 |
|
|
711 | #endif |
|
|
712 | |
|
|
713 | static void |
|
|
714 | child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) |
728 | child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status) |
715 | { |
729 | { |
716 | struct ev_child *w; |
730 | ev_child *w; |
717 | |
731 | |
718 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
732 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
719 | if (w->pid == pid || !w->pid) |
733 | if (w->pid == pid || !w->pid) |
720 | { |
734 | { |
721 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
735 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
722 | w->rpid = pid; |
736 | w->rpid = pid; |
723 | w->rstatus = status; |
737 | w->rstatus = status; |
724 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
738 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
725 | } |
739 | } |
726 | } |
740 | } |
727 | |
741 | |
|
|
742 | #ifndef WCONTINUED |
|
|
743 | # define WCONTINUED 0 |
|
|
744 | #endif |
|
|
745 | |
728 | static void |
746 | static void |
729 | childcb (EV_P_ struct ev_signal *sw, int revents) |
747 | childcb (EV_P_ ev_signal *sw, int revents) |
730 | { |
748 | { |
731 | int pid, status; |
749 | int pid, status; |
732 | |
750 | |
|
|
751 | /* some systems define WCONTINUED but then fail to support it (linux 2.4) */ |
733 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
752 | if (0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
734 | { |
753 | if (!WCONTINUED |
|
|
754 | || errno != EINVAL |
|
|
755 | || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED))) |
|
|
756 | return; |
|
|
757 | |
735 | /* make sure we are called again until all childs have been reaped */ |
758 | /* make sure we are called again until all childs have been reaped */ |
736 | /* we need to do it this way so that the callback gets called before we continue */ |
759 | /* we need to do it this way so that the callback gets called before we continue */ |
737 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
760 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
738 | |
761 | |
739 | child_reap (EV_A_ sw, pid, pid, status); |
762 | child_reap (EV_A_ sw, pid, pid, status); |
|
|
763 | if (EV_PID_HASHSIZE > 1) |
740 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */ |
764 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */ |
741 | } |
|
|
742 | } |
765 | } |
743 | |
766 | |
744 | #endif |
767 | #endif |
745 | |
768 | |
746 | /*****************************************************************************/ |
769 | /*****************************************************************************/ |
… | |
… | |
772 | { |
795 | { |
773 | return EV_VERSION_MINOR; |
796 | return EV_VERSION_MINOR; |
774 | } |
797 | } |
775 | |
798 | |
776 | /* return true if we are running with elevated privileges and should ignore env variables */ |
799 | /* return true if we are running with elevated privileges and should ignore env variables */ |
777 | static int |
800 | int inline_size |
778 | enable_secure (void) |
801 | enable_secure (void) |
779 | { |
802 | { |
780 | #ifdef _WIN32 |
803 | #ifdef _WIN32 |
781 | return 0; |
804 | return 0; |
782 | #else |
805 | #else |
… | |
… | |
816 | |
839 | |
817 | return flags; |
840 | return flags; |
818 | } |
841 | } |
819 | |
842 | |
820 | unsigned int |
843 | unsigned int |
|
|
844 | ev_embeddable_backends (void) |
|
|
845 | { |
|
|
846 | return EVBACKEND_EPOLL |
|
|
847 | | EVBACKEND_KQUEUE |
|
|
848 | | EVBACKEND_PORT; |
|
|
849 | } |
|
|
850 | |
|
|
851 | unsigned int |
821 | ev_backend (EV_P) |
852 | ev_backend (EV_P) |
822 | { |
853 | { |
823 | return backend; |
854 | return backend; |
824 | } |
855 | } |
825 | |
856 | |
… | |
… | |
896 | array_free (pending, [i]); |
927 | array_free (pending, [i]); |
897 | |
928 | |
898 | /* have to use the microsoft-never-gets-it-right macro */ |
929 | /* have to use the microsoft-never-gets-it-right macro */ |
899 | array_free (fdchange, EMPTY0); |
930 | array_free (fdchange, EMPTY0); |
900 | array_free (timer, EMPTY0); |
931 | array_free (timer, EMPTY0); |
901 | #if EV_PERIODICS |
932 | #if EV_PERIODIC_ENABLE |
902 | array_free (periodic, EMPTY0); |
933 | array_free (periodic, EMPTY0); |
903 | #endif |
934 | #endif |
904 | array_free (idle, EMPTY0); |
935 | array_free (idle, EMPTY0); |
905 | array_free (prepare, EMPTY0); |
936 | array_free (prepare, EMPTY0); |
906 | array_free (check, EMPTY0); |
937 | array_free (check, EMPTY0); |
… | |
… | |
1042 | postfork = 1; |
1073 | postfork = 1; |
1043 | } |
1074 | } |
1044 | |
1075 | |
1045 | /*****************************************************************************/ |
1076 | /*****************************************************************************/ |
1046 | |
1077 | |
1047 | static int |
1078 | int inline_size |
1048 | any_pending (EV_P) |
1079 | any_pending (EV_P) |
1049 | { |
1080 | { |
1050 | int pri; |
1081 | int pri; |
1051 | |
1082 | |
1052 | for (pri = NUMPRI; pri--; ) |
1083 | for (pri = NUMPRI; pri--; ) |
… | |
… | |
1054 | return 1; |
1085 | return 1; |
1055 | |
1086 | |
1056 | return 0; |
1087 | return 0; |
1057 | } |
1088 | } |
1058 | |
1089 | |
1059 | inline void |
1090 | void inline_speed |
1060 | call_pending (EV_P) |
1091 | call_pending (EV_P) |
1061 | { |
1092 | { |
1062 | int pri; |
1093 | int pri; |
1063 | |
1094 | |
1064 | for (pri = NUMPRI; pri--; ) |
1095 | for (pri = NUMPRI; pri--; ) |
… | |
… | |
1066 | { |
1097 | { |
1067 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1098 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1068 | |
1099 | |
1069 | if (expect_true (p->w)) |
1100 | if (expect_true (p->w)) |
1070 | { |
1101 | { |
|
|
1102 | assert (("non-pending watcher on pending list", p->w->pending)); |
|
|
1103 | |
1071 | p->w->pending = 0; |
1104 | p->w->pending = 0; |
1072 | EV_CB_INVOKE (p->w, p->events); |
1105 | EV_CB_INVOKE (p->w, p->events); |
1073 | } |
1106 | } |
1074 | } |
1107 | } |
1075 | } |
1108 | } |
1076 | |
1109 | |
1077 | inline void |
1110 | void inline_size |
1078 | timers_reify (EV_P) |
1111 | timers_reify (EV_P) |
1079 | { |
1112 | { |
1080 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
1113 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
1081 | { |
1114 | { |
1082 | struct ev_timer *w = timers [0]; |
1115 | ev_timer *w = timers [0]; |
1083 | |
1116 | |
1084 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
1117 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
1085 | |
1118 | |
1086 | /* first reschedule or stop timer */ |
1119 | /* first reschedule or stop timer */ |
1087 | if (w->repeat) |
1120 | if (w->repeat) |
… | |
… | |
1099 | |
1132 | |
1100 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1133 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1101 | } |
1134 | } |
1102 | } |
1135 | } |
1103 | |
1136 | |
1104 | #if EV_PERIODICS |
1137 | #if EV_PERIODIC_ENABLE |
1105 | inline void |
1138 | void inline_size |
1106 | periodics_reify (EV_P) |
1139 | periodics_reify (EV_P) |
1107 | { |
1140 | { |
1108 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1141 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1109 | { |
1142 | { |
1110 | struct ev_periodic *w = periodics [0]; |
1143 | ev_periodic *w = periodics [0]; |
1111 | |
1144 | |
1112 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1145 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1113 | |
1146 | |
1114 | /* first reschedule or stop timer */ |
1147 | /* first reschedule or stop timer */ |
1115 | if (w->reschedule_cb) |
1148 | if (w->reschedule_cb) |
… | |
… | |
1129 | |
1162 | |
1130 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
1163 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
1131 | } |
1164 | } |
1132 | } |
1165 | } |
1133 | |
1166 | |
1134 | static void |
1167 | static void noinline |
1135 | periodics_reschedule (EV_P) |
1168 | periodics_reschedule (EV_P) |
1136 | { |
1169 | { |
1137 | int i; |
1170 | int i; |
1138 | |
1171 | |
1139 | /* adjust periodics after time jump */ |
1172 | /* adjust periodics after time jump */ |
1140 | for (i = 0; i < periodiccnt; ++i) |
1173 | for (i = 0; i < periodiccnt; ++i) |
1141 | { |
1174 | { |
1142 | struct ev_periodic *w = periodics [i]; |
1175 | ev_periodic *w = periodics [i]; |
1143 | |
1176 | |
1144 | if (w->reschedule_cb) |
1177 | if (w->reschedule_cb) |
1145 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1178 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1146 | else if (w->interval) |
1179 | else if (w->interval) |
1147 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1180 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
… | |
… | |
1151 | for (i = periodiccnt >> 1; i--; ) |
1184 | for (i = periodiccnt >> 1; i--; ) |
1152 | downheap ((WT *)periodics, periodiccnt, i); |
1185 | downheap ((WT *)periodics, periodiccnt, i); |
1153 | } |
1186 | } |
1154 | #endif |
1187 | #endif |
1155 | |
1188 | |
1156 | inline int |
1189 | int inline_size |
1157 | time_update_monotonic (EV_P) |
1190 | time_update_monotonic (EV_P) |
1158 | { |
1191 | { |
1159 | mn_now = get_clock (); |
1192 | mn_now = get_clock (); |
1160 | |
1193 | |
1161 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1194 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
… | |
… | |
1169 | ev_rt_now = ev_time (); |
1202 | ev_rt_now = ev_time (); |
1170 | return 1; |
1203 | return 1; |
1171 | } |
1204 | } |
1172 | } |
1205 | } |
1173 | |
1206 | |
1174 | inline void |
1207 | void inline_size |
1175 | time_update (EV_P) |
1208 | time_update (EV_P) |
1176 | { |
1209 | { |
1177 | int i; |
1210 | int i; |
1178 | |
1211 | |
1179 | #if EV_USE_MONOTONIC |
1212 | #if EV_USE_MONOTONIC |
… | |
… | |
1181 | { |
1214 | { |
1182 | if (time_update_monotonic (EV_A)) |
1215 | if (time_update_monotonic (EV_A)) |
1183 | { |
1216 | { |
1184 | ev_tstamp odiff = rtmn_diff; |
1217 | ev_tstamp odiff = rtmn_diff; |
1185 | |
1218 | |
1186 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1219 | /* loop a few times, before making important decisions. |
|
|
1220 | * on the choice of "4": one iteration isn't enough, |
|
|
1221 | * in case we get preempted during the calls to |
|
|
1222 | * ev_time and get_clock. a second call is almost guarenteed |
|
|
1223 | * to succeed in that case, though. and looping a few more times |
|
|
1224 | * doesn't hurt either as we only do this on time-jumps or |
|
|
1225 | * in the unlikely event of getting preempted here. |
|
|
1226 | */ |
|
|
1227 | for (i = 4; --i; ) |
1187 | { |
1228 | { |
1188 | rtmn_diff = ev_rt_now - mn_now; |
1229 | rtmn_diff = ev_rt_now - mn_now; |
1189 | |
1230 | |
1190 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1231 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1191 | return; /* all is well */ |
1232 | return; /* all is well */ |
… | |
… | |
1193 | ev_rt_now = ev_time (); |
1234 | ev_rt_now = ev_time (); |
1194 | mn_now = get_clock (); |
1235 | mn_now = get_clock (); |
1195 | now_floor = mn_now; |
1236 | now_floor = mn_now; |
1196 | } |
1237 | } |
1197 | |
1238 | |
1198 | # if EV_PERIODICS |
1239 | # if EV_PERIODIC_ENABLE |
1199 | periodics_reschedule (EV_A); |
1240 | periodics_reschedule (EV_A); |
1200 | # endif |
1241 | # endif |
1201 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1242 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1202 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1243 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1203 | } |
1244 | } |
… | |
… | |
1207 | { |
1248 | { |
1208 | ev_rt_now = ev_time (); |
1249 | ev_rt_now = ev_time (); |
1209 | |
1250 | |
1210 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1251 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1211 | { |
1252 | { |
1212 | #if EV_PERIODICS |
1253 | #if EV_PERIODIC_ENABLE |
1213 | periodics_reschedule (EV_A); |
1254 | periodics_reschedule (EV_A); |
1214 | #endif |
1255 | #endif |
1215 | |
1256 | |
1216 | /* adjust timers. this is easy, as the offset is the same for all */ |
1257 | /* adjust timers. this is easy, as the offset is the same for all */ |
1217 | for (i = 0; i < timercnt; ++i) |
1258 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
1237 | static int loop_done; |
1278 | static int loop_done; |
1238 | |
1279 | |
1239 | void |
1280 | void |
1240 | ev_loop (EV_P_ int flags) |
1281 | ev_loop (EV_P_ int flags) |
1241 | { |
1282 | { |
1242 | double block; |
|
|
1243 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
1283 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) |
|
|
1284 | ? EVUNLOOP_ONE |
|
|
1285 | : EVUNLOOP_CANCEL; |
1244 | |
1286 | |
1245 | while (activecnt) |
1287 | while (activecnt) |
1246 | { |
1288 | { |
|
|
1289 | /* we might have forked, so reify kernel state if necessary */ |
|
|
1290 | #if EV_FORK_ENABLE |
|
|
1291 | if (expect_false (postfork)) |
|
|
1292 | if (forkcnt) |
|
|
1293 | { |
|
|
1294 | queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); |
|
|
1295 | call_pending (EV_A); |
|
|
1296 | } |
|
|
1297 | #endif |
|
|
1298 | |
1247 | /* queue check watchers (and execute them) */ |
1299 | /* queue check watchers (and execute them) */ |
1248 | if (expect_false (preparecnt)) |
1300 | if (expect_false (preparecnt)) |
1249 | { |
1301 | { |
1250 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1302 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1251 | call_pending (EV_A); |
1303 | call_pending (EV_A); |
… | |
… | |
1257 | |
1309 | |
1258 | /* update fd-related kernel structures */ |
1310 | /* update fd-related kernel structures */ |
1259 | fd_reify (EV_A); |
1311 | fd_reify (EV_A); |
1260 | |
1312 | |
1261 | /* calculate blocking time */ |
1313 | /* calculate blocking time */ |
|
|
1314 | { |
|
|
1315 | double block; |
1262 | |
1316 | |
1263 | /* we only need this for !monotonic clock or timers, but as we basically |
1317 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1264 | always have timers, we just calculate it always */ |
1318 | block = 0.; /* do not block at all */ |
|
|
1319 | else |
|
|
1320 | { |
|
|
1321 | /* update time to cancel out callback processing overhead */ |
1265 | #if EV_USE_MONOTONIC |
1322 | #if EV_USE_MONOTONIC |
1266 | if (expect_true (have_monotonic)) |
1323 | if (expect_true (have_monotonic)) |
1267 | time_update_monotonic (EV_A); |
1324 | time_update_monotonic (EV_A); |
1268 | else |
1325 | else |
1269 | #endif |
1326 | #endif |
1270 | { |
1327 | { |
1271 | ev_rt_now = ev_time (); |
1328 | ev_rt_now = ev_time (); |
1272 | mn_now = ev_rt_now; |
1329 | mn_now = ev_rt_now; |
1273 | } |
1330 | } |
1274 | |
1331 | |
1275 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
|
|
1276 | block = 0.; |
|
|
1277 | else |
|
|
1278 | { |
|
|
1279 | block = MAX_BLOCKTIME; |
1332 | block = MAX_BLOCKTIME; |
1280 | |
1333 | |
1281 | if (timercnt) |
1334 | if (timercnt) |
1282 | { |
1335 | { |
1283 | ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; |
1336 | ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; |
1284 | if (block > to) block = to; |
1337 | if (block > to) block = to; |
1285 | } |
1338 | } |
1286 | |
1339 | |
1287 | #if EV_PERIODICS |
1340 | #if EV_PERIODIC_ENABLE |
1288 | if (periodiccnt) |
1341 | if (periodiccnt) |
1289 | { |
1342 | { |
1290 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; |
1343 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; |
1291 | if (block > to) block = to; |
1344 | if (block > to) block = to; |
1292 | } |
1345 | } |
1293 | #endif |
1346 | #endif |
1294 | |
1347 | |
1295 | if (expect_false (block < 0.)) block = 0.; |
1348 | if (expect_false (block < 0.)) block = 0.; |
1296 | } |
1349 | } |
1297 | |
1350 | |
1298 | backend_poll (EV_A_ block); |
1351 | backend_poll (EV_A_ block); |
|
|
1352 | } |
1299 | |
1353 | |
1300 | /* update ev_rt_now, do magic */ |
1354 | /* update ev_rt_now, do magic */ |
1301 | time_update (EV_A); |
1355 | time_update (EV_A); |
1302 | |
1356 | |
1303 | /* queue pending timers and reschedule them */ |
1357 | /* queue pending timers and reschedule them */ |
1304 | timers_reify (EV_A); /* relative timers called last */ |
1358 | timers_reify (EV_A); /* relative timers called last */ |
1305 | #if EV_PERIODICS |
1359 | #if EV_PERIODIC_ENABLE |
1306 | periodics_reify (EV_A); /* absolute timers called first */ |
1360 | periodics_reify (EV_A); /* absolute timers called first */ |
1307 | #endif |
1361 | #endif |
1308 | |
1362 | |
1309 | /* queue idle watchers unless io or timers are pending */ |
1363 | /* queue idle watchers unless other events are pending */ |
1310 | if (idlecnt && !any_pending (EV_A)) |
1364 | if (idlecnt && !any_pending (EV_A)) |
1311 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1365 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1312 | |
1366 | |
1313 | /* queue check watchers, to be executed first */ |
1367 | /* queue check watchers, to be executed first */ |
1314 | if (expect_false (checkcnt)) |
1368 | if (expect_false (checkcnt)) |
… | |
… | |
1318 | |
1372 | |
1319 | if (expect_false (loop_done)) |
1373 | if (expect_false (loop_done)) |
1320 | break; |
1374 | break; |
1321 | } |
1375 | } |
1322 | |
1376 | |
1323 | if (loop_done != 2) |
1377 | if (loop_done == EVUNLOOP_ONE) |
1324 | loop_done = 0; |
1378 | loop_done = EVUNLOOP_CANCEL; |
1325 | } |
1379 | } |
1326 | |
1380 | |
1327 | void |
1381 | void |
1328 | ev_unloop (EV_P_ int how) |
1382 | ev_unloop (EV_P_ int how) |
1329 | { |
1383 | { |
1330 | loop_done = how; |
1384 | loop_done = how; |
1331 | } |
1385 | } |
1332 | |
1386 | |
1333 | /*****************************************************************************/ |
1387 | /*****************************************************************************/ |
1334 | |
1388 | |
1335 | inline void |
1389 | void inline_size |
1336 | wlist_add (WL *head, WL elem) |
1390 | wlist_add (WL *head, WL elem) |
1337 | { |
1391 | { |
1338 | elem->next = *head; |
1392 | elem->next = *head; |
1339 | *head = elem; |
1393 | *head = elem; |
1340 | } |
1394 | } |
1341 | |
1395 | |
1342 | inline void |
1396 | void inline_size |
1343 | wlist_del (WL *head, WL elem) |
1397 | wlist_del (WL *head, WL elem) |
1344 | { |
1398 | { |
1345 | while (*head) |
1399 | while (*head) |
1346 | { |
1400 | { |
1347 | if (*head == elem) |
1401 | if (*head == elem) |
… | |
… | |
1352 | |
1406 | |
1353 | head = &(*head)->next; |
1407 | head = &(*head)->next; |
1354 | } |
1408 | } |
1355 | } |
1409 | } |
1356 | |
1410 | |
1357 | inline void |
1411 | void inline_speed |
1358 | ev_clear_pending (EV_P_ W w) |
1412 | ev_clear_pending (EV_P_ W w) |
1359 | { |
1413 | { |
1360 | if (w->pending) |
1414 | if (w->pending) |
1361 | { |
1415 | { |
1362 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
1416 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
1363 | w->pending = 0; |
1417 | w->pending = 0; |
1364 | } |
1418 | } |
1365 | } |
1419 | } |
1366 | |
1420 | |
1367 | inline void |
1421 | void inline_speed |
1368 | ev_start (EV_P_ W w, int active) |
1422 | ev_start (EV_P_ W w, int active) |
1369 | { |
1423 | { |
1370 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
1424 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
1371 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
1425 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
1372 | |
1426 | |
1373 | w->active = active; |
1427 | w->active = active; |
1374 | ev_ref (EV_A); |
1428 | ev_ref (EV_A); |
1375 | } |
1429 | } |
1376 | |
1430 | |
1377 | inline void |
1431 | void inline_size |
1378 | ev_stop (EV_P_ W w) |
1432 | ev_stop (EV_P_ W w) |
1379 | { |
1433 | { |
1380 | ev_unref (EV_A); |
1434 | ev_unref (EV_A); |
1381 | w->active = 0; |
1435 | w->active = 0; |
1382 | } |
1436 | } |
1383 | |
1437 | |
1384 | /*****************************************************************************/ |
1438 | /*****************************************************************************/ |
1385 | |
1439 | |
1386 | void |
1440 | void |
1387 | ev_io_start (EV_P_ struct ev_io *w) |
1441 | ev_io_start (EV_P_ ev_io *w) |
1388 | { |
1442 | { |
1389 | int fd = w->fd; |
1443 | int fd = w->fd; |
1390 | |
1444 | |
1391 | if (expect_false (ev_is_active (w))) |
1445 | if (expect_false (ev_is_active (w))) |
1392 | return; |
1446 | return; |
… | |
… | |
1399 | |
1453 | |
1400 | fd_change (EV_A_ fd); |
1454 | fd_change (EV_A_ fd); |
1401 | } |
1455 | } |
1402 | |
1456 | |
1403 | void |
1457 | void |
1404 | ev_io_stop (EV_P_ struct ev_io *w) |
1458 | ev_io_stop (EV_P_ ev_io *w) |
1405 | { |
1459 | { |
1406 | ev_clear_pending (EV_A_ (W)w); |
1460 | ev_clear_pending (EV_A_ (W)w); |
1407 | if (expect_false (!ev_is_active (w))) |
1461 | if (expect_false (!ev_is_active (w))) |
1408 | return; |
1462 | return; |
1409 | |
1463 | |
… | |
… | |
1414 | |
1468 | |
1415 | fd_change (EV_A_ w->fd); |
1469 | fd_change (EV_A_ w->fd); |
1416 | } |
1470 | } |
1417 | |
1471 | |
1418 | void |
1472 | void |
1419 | ev_timer_start (EV_P_ struct ev_timer *w) |
1473 | ev_timer_start (EV_P_ ev_timer *w) |
1420 | { |
1474 | { |
1421 | if (expect_false (ev_is_active (w))) |
1475 | if (expect_false (ev_is_active (w))) |
1422 | return; |
1476 | return; |
1423 | |
1477 | |
1424 | ((WT)w)->at += mn_now; |
1478 | ((WT)w)->at += mn_now; |
1425 | |
1479 | |
1426 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1480 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1427 | |
1481 | |
1428 | ev_start (EV_A_ (W)w, ++timercnt); |
1482 | ev_start (EV_A_ (W)w, ++timercnt); |
1429 | array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); |
1483 | array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2); |
1430 | timers [timercnt - 1] = w; |
1484 | timers [timercnt - 1] = w; |
1431 | upheap ((WT *)timers, timercnt - 1); |
1485 | upheap ((WT *)timers, timercnt - 1); |
1432 | |
1486 | |
1433 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1487 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1434 | } |
1488 | } |
1435 | |
1489 | |
1436 | void |
1490 | void |
1437 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1491 | ev_timer_stop (EV_P_ ev_timer *w) |
1438 | { |
1492 | { |
1439 | ev_clear_pending (EV_A_ (W)w); |
1493 | ev_clear_pending (EV_A_ (W)w); |
1440 | if (expect_false (!ev_is_active (w))) |
1494 | if (expect_false (!ev_is_active (w))) |
1441 | return; |
1495 | return; |
1442 | |
1496 | |
… | |
… | |
1452 | |
1506 | |
1453 | ev_stop (EV_A_ (W)w); |
1507 | ev_stop (EV_A_ (W)w); |
1454 | } |
1508 | } |
1455 | |
1509 | |
1456 | void |
1510 | void |
1457 | ev_timer_again (EV_P_ struct ev_timer *w) |
1511 | ev_timer_again (EV_P_ ev_timer *w) |
1458 | { |
1512 | { |
1459 | if (ev_is_active (w)) |
1513 | if (ev_is_active (w)) |
1460 | { |
1514 | { |
1461 | if (w->repeat) |
1515 | if (w->repeat) |
1462 | { |
1516 | { |
… | |
… | |
1471 | w->at = w->repeat; |
1525 | w->at = w->repeat; |
1472 | ev_timer_start (EV_A_ w); |
1526 | ev_timer_start (EV_A_ w); |
1473 | } |
1527 | } |
1474 | } |
1528 | } |
1475 | |
1529 | |
1476 | #if EV_PERIODICS |
1530 | #if EV_PERIODIC_ENABLE |
1477 | void |
1531 | void |
1478 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1532 | ev_periodic_start (EV_P_ ev_periodic *w) |
1479 | { |
1533 | { |
1480 | if (expect_false (ev_is_active (w))) |
1534 | if (expect_false (ev_is_active (w))) |
1481 | return; |
1535 | return; |
1482 | |
1536 | |
1483 | if (w->reschedule_cb) |
1537 | if (w->reschedule_cb) |
… | |
… | |
1488 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1542 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1489 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1543 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1490 | } |
1544 | } |
1491 | |
1545 | |
1492 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1546 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1493 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1547 | array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1494 | periodics [periodiccnt - 1] = w; |
1548 | periodics [periodiccnt - 1] = w; |
1495 | upheap ((WT *)periodics, periodiccnt - 1); |
1549 | upheap ((WT *)periodics, periodiccnt - 1); |
1496 | |
1550 | |
1497 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1551 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1498 | } |
1552 | } |
1499 | |
1553 | |
1500 | void |
1554 | void |
1501 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1555 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1502 | { |
1556 | { |
1503 | ev_clear_pending (EV_A_ (W)w); |
1557 | ev_clear_pending (EV_A_ (W)w); |
1504 | if (expect_false (!ev_is_active (w))) |
1558 | if (expect_false (!ev_is_active (w))) |
1505 | return; |
1559 | return; |
1506 | |
1560 | |
… | |
… | |
1514 | |
1568 | |
1515 | ev_stop (EV_A_ (W)w); |
1569 | ev_stop (EV_A_ (W)w); |
1516 | } |
1570 | } |
1517 | |
1571 | |
1518 | void |
1572 | void |
1519 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
1573 | ev_periodic_again (EV_P_ ev_periodic *w) |
1520 | { |
1574 | { |
1521 | /* TODO: use adjustheap and recalculation */ |
1575 | /* TODO: use adjustheap and recalculation */ |
1522 | ev_periodic_stop (EV_A_ w); |
1576 | ev_periodic_stop (EV_A_ w); |
1523 | ev_periodic_start (EV_A_ w); |
1577 | ev_periodic_start (EV_A_ w); |
1524 | } |
1578 | } |
1525 | #endif |
1579 | #endif |
1526 | |
1580 | |
1527 | void |
|
|
1528 | ev_idle_start (EV_P_ struct ev_idle *w) |
|
|
1529 | { |
|
|
1530 | if (expect_false (ev_is_active (w))) |
|
|
1531 | return; |
|
|
1532 | |
|
|
1533 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1534 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
|
|
1535 | idles [idlecnt - 1] = w; |
|
|
1536 | } |
|
|
1537 | |
|
|
1538 | void |
|
|
1539 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1540 | { |
|
|
1541 | ev_clear_pending (EV_A_ (W)w); |
|
|
1542 | if (expect_false (!ev_is_active (w))) |
|
|
1543 | return; |
|
|
1544 | |
|
|
1545 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
|
|
1546 | ev_stop (EV_A_ (W)w); |
|
|
1547 | } |
|
|
1548 | |
|
|
1549 | void |
|
|
1550 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1551 | { |
|
|
1552 | if (expect_false (ev_is_active (w))) |
|
|
1553 | return; |
|
|
1554 | |
|
|
1555 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1556 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); |
|
|
1557 | prepares [preparecnt - 1] = w; |
|
|
1558 | } |
|
|
1559 | |
|
|
1560 | void |
|
|
1561 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1562 | { |
|
|
1563 | ev_clear_pending (EV_A_ (W)w); |
|
|
1564 | if (expect_false (!ev_is_active (w))) |
|
|
1565 | return; |
|
|
1566 | |
|
|
1567 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
|
|
1568 | ev_stop (EV_A_ (W)w); |
|
|
1569 | } |
|
|
1570 | |
|
|
1571 | void |
|
|
1572 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1573 | { |
|
|
1574 | if (expect_false (ev_is_active (w))) |
|
|
1575 | return; |
|
|
1576 | |
|
|
1577 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1578 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); |
|
|
1579 | checks [checkcnt - 1] = w; |
|
|
1580 | } |
|
|
1581 | |
|
|
1582 | void |
|
|
1583 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1584 | { |
|
|
1585 | ev_clear_pending (EV_A_ (W)w); |
|
|
1586 | if (expect_false (!ev_is_active (w))) |
|
|
1587 | return; |
|
|
1588 | |
|
|
1589 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
|
|
1590 | ev_stop (EV_A_ (W)w); |
|
|
1591 | } |
|
|
1592 | |
|
|
1593 | #ifndef SA_RESTART |
1581 | #ifndef SA_RESTART |
1594 | # define SA_RESTART 0 |
1582 | # define SA_RESTART 0 |
1595 | #endif |
1583 | #endif |
1596 | |
1584 | |
1597 | void |
1585 | void |
1598 | ev_signal_start (EV_P_ struct ev_signal *w) |
1586 | ev_signal_start (EV_P_ ev_signal *w) |
1599 | { |
1587 | { |
1600 | #if EV_MULTIPLICITY |
1588 | #if EV_MULTIPLICITY |
1601 | assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1589 | assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1602 | #endif |
1590 | #endif |
1603 | if (expect_false (ev_is_active (w))) |
1591 | if (expect_false (ev_is_active (w))) |
… | |
… | |
1622 | #endif |
1610 | #endif |
1623 | } |
1611 | } |
1624 | } |
1612 | } |
1625 | |
1613 | |
1626 | void |
1614 | void |
1627 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1615 | ev_signal_stop (EV_P_ ev_signal *w) |
1628 | { |
1616 | { |
1629 | ev_clear_pending (EV_A_ (W)w); |
1617 | ev_clear_pending (EV_A_ (W)w); |
1630 | if (expect_false (!ev_is_active (w))) |
1618 | if (expect_false (!ev_is_active (w))) |
1631 | return; |
1619 | return; |
1632 | |
1620 | |
… | |
… | |
1636 | if (!signals [w->signum - 1].head) |
1624 | if (!signals [w->signum - 1].head) |
1637 | signal (w->signum, SIG_DFL); |
1625 | signal (w->signum, SIG_DFL); |
1638 | } |
1626 | } |
1639 | |
1627 | |
1640 | void |
1628 | void |
1641 | ev_child_start (EV_P_ struct ev_child *w) |
1629 | ev_child_start (EV_P_ ev_child *w) |
1642 | { |
1630 | { |
1643 | #if EV_MULTIPLICITY |
1631 | #if EV_MULTIPLICITY |
1644 | assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1632 | assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1645 | #endif |
1633 | #endif |
1646 | if (expect_false (ev_is_active (w))) |
1634 | if (expect_false (ev_is_active (w))) |
1647 | return; |
1635 | return; |
1648 | |
1636 | |
1649 | ev_start (EV_A_ (W)w, 1); |
1637 | ev_start (EV_A_ (W)w, 1); |
1650 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1638 | wlist_add ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); |
1651 | } |
1639 | } |
1652 | |
1640 | |
1653 | void |
1641 | void |
1654 | ev_child_stop (EV_P_ struct ev_child *w) |
1642 | ev_child_stop (EV_P_ ev_child *w) |
1655 | { |
1643 | { |
1656 | ev_clear_pending (EV_A_ (W)w); |
1644 | ev_clear_pending (EV_A_ (W)w); |
1657 | if (expect_false (!ev_is_active (w))) |
1645 | if (expect_false (!ev_is_active (w))) |
1658 | return; |
1646 | return; |
1659 | |
1647 | |
1660 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1648 | wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); |
1661 | ev_stop (EV_A_ (W)w); |
1649 | ev_stop (EV_A_ (W)w); |
1662 | } |
1650 | } |
1663 | |
1651 | |
|
|
1652 | #if EV_STAT_ENABLE |
|
|
1653 | |
|
|
1654 | # ifdef _WIN32 |
|
|
1655 | # undef lstat |
|
|
1656 | # define lstat(a,b) _stati64 (a,b) |
|
|
1657 | # endif |
|
|
1658 | |
|
|
1659 | #define DEF_STAT_INTERVAL 5.0074891 |
|
|
1660 | #define MIN_STAT_INTERVAL 0.1074891 |
|
|
1661 | |
|
|
1662 | void |
|
|
1663 | ev_stat_stat (EV_P_ ev_stat *w) |
|
|
1664 | { |
|
|
1665 | if (lstat (w->path, &w->attr) < 0) |
|
|
1666 | w->attr.st_nlink = 0; |
|
|
1667 | else if (!w->attr.st_nlink) |
|
|
1668 | w->attr.st_nlink = 1; |
|
|
1669 | } |
|
|
1670 | |
|
|
1671 | static void |
|
|
1672 | stat_timer_cb (EV_P_ ev_timer *w_, int revents) |
|
|
1673 | { |
|
|
1674 | ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); |
|
|
1675 | |
|
|
1676 | /* we copy this here each the time so that */ |
|
|
1677 | /* prev has the old value when the callback gets invoked */ |
|
|
1678 | w->prev = w->attr; |
|
|
1679 | ev_stat_stat (EV_A_ w); |
|
|
1680 | |
|
|
1681 | if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata))) |
|
|
1682 | ev_feed_event (EV_A_ w, EV_STAT); |
|
|
1683 | } |
|
|
1684 | |
|
|
1685 | void |
|
|
1686 | ev_stat_start (EV_P_ ev_stat *w) |
|
|
1687 | { |
|
|
1688 | if (expect_false (ev_is_active (w))) |
|
|
1689 | return; |
|
|
1690 | |
|
|
1691 | /* since we use memcmp, we need to clear any padding data etc. */ |
|
|
1692 | memset (&w->prev, 0, sizeof (ev_statdata)); |
|
|
1693 | memset (&w->attr, 0, sizeof (ev_statdata)); |
|
|
1694 | |
|
|
1695 | ev_stat_stat (EV_A_ w); |
|
|
1696 | |
|
|
1697 | if (w->interval < MIN_STAT_INTERVAL) |
|
|
1698 | w->interval = w->interval ? MIN_STAT_INTERVAL : DEF_STAT_INTERVAL; |
|
|
1699 | |
|
|
1700 | ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval); |
|
|
1701 | ev_set_priority (&w->timer, ev_priority (w)); |
|
|
1702 | ev_timer_start (EV_A_ &w->timer); |
|
|
1703 | |
|
|
1704 | ev_start (EV_A_ (W)w, 1); |
|
|
1705 | } |
|
|
1706 | |
|
|
1707 | void |
|
|
1708 | ev_stat_stop (EV_P_ ev_stat *w) |
|
|
1709 | { |
|
|
1710 | ev_clear_pending (EV_A_ (W)w); |
|
|
1711 | if (expect_false (!ev_is_active (w))) |
|
|
1712 | return; |
|
|
1713 | |
|
|
1714 | ev_timer_stop (EV_A_ &w->timer); |
|
|
1715 | |
|
|
1716 | ev_stop (EV_A_ (W)w); |
|
|
1717 | } |
|
|
1718 | #endif |
|
|
1719 | |
|
|
1720 | void |
|
|
1721 | ev_idle_start (EV_P_ ev_idle *w) |
|
|
1722 | { |
|
|
1723 | if (expect_false (ev_is_active (w))) |
|
|
1724 | return; |
|
|
1725 | |
|
|
1726 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1727 | array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
|
|
1728 | idles [idlecnt - 1] = w; |
|
|
1729 | } |
|
|
1730 | |
|
|
1731 | void |
|
|
1732 | ev_idle_stop (EV_P_ ev_idle *w) |
|
|
1733 | { |
|
|
1734 | ev_clear_pending (EV_A_ (W)w); |
|
|
1735 | if (expect_false (!ev_is_active (w))) |
|
|
1736 | return; |
|
|
1737 | |
|
|
1738 | { |
|
|
1739 | int active = ((W)w)->active; |
|
|
1740 | idles [active - 1] = idles [--idlecnt]; |
|
|
1741 | ((W)idles [active - 1])->active = active; |
|
|
1742 | } |
|
|
1743 | |
|
|
1744 | ev_stop (EV_A_ (W)w); |
|
|
1745 | } |
|
|
1746 | |
|
|
1747 | void |
|
|
1748 | ev_prepare_start (EV_P_ ev_prepare *w) |
|
|
1749 | { |
|
|
1750 | if (expect_false (ev_is_active (w))) |
|
|
1751 | return; |
|
|
1752 | |
|
|
1753 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1754 | array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); |
|
|
1755 | prepares [preparecnt - 1] = w; |
|
|
1756 | } |
|
|
1757 | |
|
|
1758 | void |
|
|
1759 | ev_prepare_stop (EV_P_ ev_prepare *w) |
|
|
1760 | { |
|
|
1761 | ev_clear_pending (EV_A_ (W)w); |
|
|
1762 | if (expect_false (!ev_is_active (w))) |
|
|
1763 | return; |
|
|
1764 | |
|
|
1765 | { |
|
|
1766 | int active = ((W)w)->active; |
|
|
1767 | prepares [active - 1] = prepares [--preparecnt]; |
|
|
1768 | ((W)prepares [active - 1])->active = active; |
|
|
1769 | } |
|
|
1770 | |
|
|
1771 | ev_stop (EV_A_ (W)w); |
|
|
1772 | } |
|
|
1773 | |
|
|
1774 | void |
|
|
1775 | ev_check_start (EV_P_ ev_check *w) |
|
|
1776 | { |
|
|
1777 | if (expect_false (ev_is_active (w))) |
|
|
1778 | return; |
|
|
1779 | |
|
|
1780 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1781 | array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2); |
|
|
1782 | checks [checkcnt - 1] = w; |
|
|
1783 | } |
|
|
1784 | |
|
|
1785 | void |
|
|
1786 | ev_check_stop (EV_P_ ev_check *w) |
|
|
1787 | { |
|
|
1788 | ev_clear_pending (EV_A_ (W)w); |
|
|
1789 | if (expect_false (!ev_is_active (w))) |
|
|
1790 | return; |
|
|
1791 | |
|
|
1792 | { |
|
|
1793 | int active = ((W)w)->active; |
|
|
1794 | checks [active - 1] = checks [--checkcnt]; |
|
|
1795 | ((W)checks [active - 1])->active = active; |
|
|
1796 | } |
|
|
1797 | |
|
|
1798 | ev_stop (EV_A_ (W)w); |
|
|
1799 | } |
|
|
1800 | |
|
|
1801 | #if EV_EMBED_ENABLE |
|
|
1802 | void noinline |
|
|
1803 | ev_embed_sweep (EV_P_ ev_embed *w) |
|
|
1804 | { |
|
|
1805 | ev_loop (w->loop, EVLOOP_NONBLOCK); |
|
|
1806 | } |
|
|
1807 | |
|
|
1808 | static void |
|
|
1809 | embed_cb (EV_P_ ev_io *io, int revents) |
|
|
1810 | { |
|
|
1811 | ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io)); |
|
|
1812 | |
|
|
1813 | if (ev_cb (w)) |
|
|
1814 | ev_feed_event (EV_A_ (W)w, EV_EMBED); |
|
|
1815 | else |
|
|
1816 | ev_embed_sweep (loop, w); |
|
|
1817 | } |
|
|
1818 | |
|
|
1819 | void |
|
|
1820 | ev_embed_start (EV_P_ ev_embed *w) |
|
|
1821 | { |
|
|
1822 | if (expect_false (ev_is_active (w))) |
|
|
1823 | return; |
|
|
1824 | |
|
|
1825 | { |
|
|
1826 | struct ev_loop *loop = w->loop; |
|
|
1827 | assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); |
|
|
1828 | ev_io_init (&w->io, embed_cb, backend_fd, EV_READ); |
|
|
1829 | } |
|
|
1830 | |
|
|
1831 | ev_set_priority (&w->io, ev_priority (w)); |
|
|
1832 | ev_io_start (EV_A_ &w->io); |
|
|
1833 | |
|
|
1834 | ev_start (EV_A_ (W)w, 1); |
|
|
1835 | } |
|
|
1836 | |
|
|
1837 | void |
|
|
1838 | ev_embed_stop (EV_P_ ev_embed *w) |
|
|
1839 | { |
|
|
1840 | ev_clear_pending (EV_A_ (W)w); |
|
|
1841 | if (expect_false (!ev_is_active (w))) |
|
|
1842 | return; |
|
|
1843 | |
|
|
1844 | ev_io_stop (EV_A_ &w->io); |
|
|
1845 | |
|
|
1846 | ev_stop (EV_A_ (W)w); |
|
|
1847 | } |
|
|
1848 | #endif |
|
|
1849 | |
|
|
1850 | #if EV_FORK_ENABLE |
|
|
1851 | void |
|
|
1852 | ev_fork_start (EV_P_ ev_fork *w) |
|
|
1853 | { |
|
|
1854 | if (expect_false (ev_is_active (w))) |
|
|
1855 | return; |
|
|
1856 | |
|
|
1857 | ev_start (EV_A_ (W)w, ++forkcnt); |
|
|
1858 | array_needsize (ev_fork *, forks, forkmax, forkcnt, EMPTY2); |
|
|
1859 | forks [forkcnt - 1] = w; |
|
|
1860 | } |
|
|
1861 | |
|
|
1862 | void |
|
|
1863 | ev_fork_stop (EV_P_ ev_fork *w) |
|
|
1864 | { |
|
|
1865 | ev_clear_pending (EV_A_ (W)w); |
|
|
1866 | if (expect_false (!ev_is_active (w))) |
|
|
1867 | return; |
|
|
1868 | |
|
|
1869 | { |
|
|
1870 | int active = ((W)w)->active; |
|
|
1871 | forks [active - 1] = forks [--forkcnt]; |
|
|
1872 | ((W)forks [active - 1])->active = active; |
|
|
1873 | } |
|
|
1874 | |
|
|
1875 | ev_stop (EV_A_ (W)w); |
|
|
1876 | } |
|
|
1877 | #endif |
|
|
1878 | |
1664 | /*****************************************************************************/ |
1879 | /*****************************************************************************/ |
1665 | |
1880 | |
1666 | struct ev_once |
1881 | struct ev_once |
1667 | { |
1882 | { |
1668 | struct ev_io io; |
1883 | ev_io io; |
1669 | struct ev_timer to; |
1884 | ev_timer to; |
1670 | void (*cb)(int revents, void *arg); |
1885 | void (*cb)(int revents, void *arg); |
1671 | void *arg; |
1886 | void *arg; |
1672 | }; |
1887 | }; |
1673 | |
1888 | |
1674 | static void |
1889 | static void |
… | |
… | |
1683 | |
1898 | |
1684 | cb (revents, arg); |
1899 | cb (revents, arg); |
1685 | } |
1900 | } |
1686 | |
1901 | |
1687 | static void |
1902 | static void |
1688 | once_cb_io (EV_P_ struct ev_io *w, int revents) |
1903 | once_cb_io (EV_P_ ev_io *w, int revents) |
1689 | { |
1904 | { |
1690 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1905 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1691 | } |
1906 | } |
1692 | |
1907 | |
1693 | static void |
1908 | static void |
1694 | once_cb_to (EV_P_ struct ev_timer *w, int revents) |
1909 | once_cb_to (EV_P_ ev_timer *w, int revents) |
1695 | { |
1910 | { |
1696 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1911 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1697 | } |
1912 | } |
1698 | |
1913 | |
1699 | void |
1914 | void |