… | |
… | |
126 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
126 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
127 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
127 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
128 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
128 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
129 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
129 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
130 | |
130 | |
|
|
131 | #ifdef EV_H |
|
|
132 | # include EV_H |
|
|
133 | #else |
131 | #include "ev.h" |
134 | # include "ev.h" |
|
|
135 | #endif |
132 | |
136 | |
133 | #if __GNUC__ >= 3 |
137 | #if __GNUC__ >= 3 |
134 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
138 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
135 | # define inline inline |
139 | # define inline inline |
136 | #else |
140 | #else |
… | |
… | |
215 | int events; |
219 | int events; |
216 | } ANPENDING; |
220 | } ANPENDING; |
217 | |
221 | |
218 | #if EV_MULTIPLICITY |
222 | #if EV_MULTIPLICITY |
219 | |
223 | |
220 | struct ev_loop |
224 | struct ev_loop |
221 | { |
225 | { |
222 | # define VAR(name,decl) decl; |
226 | #define VAR(name,decl) decl; |
223 | # include "ev_vars.h" |
227 | #include "ev_vars.h" |
224 | }; |
|
|
225 | # undef VAR |
228 | #undef VAR |
|
|
229 | }; |
226 | # include "ev_wrap.h" |
230 | #include "ev_wrap.h" |
|
|
231 | |
|
|
232 | struct ev_loop default_loop_struct; |
|
|
233 | static struct ev_loop *default_loop; |
227 | |
234 | |
228 | #else |
235 | #else |
229 | |
236 | |
230 | # define VAR(name,decl) static decl; |
237 | #define VAR(name,decl) static decl; |
231 | # include "ev_vars.h" |
238 | #include "ev_vars.h" |
232 | # undef VAR |
239 | #undef VAR |
|
|
240 | |
|
|
241 | static int default_loop; |
233 | |
242 | |
234 | #endif |
243 | #endif |
235 | |
244 | |
236 | /*****************************************************************************/ |
245 | /*****************************************************************************/ |
237 | |
246 | |
… | |
… | |
316 | |
325 | |
317 | ++base; |
326 | ++base; |
318 | } |
327 | } |
319 | } |
328 | } |
320 | |
329 | |
321 | static void |
330 | void |
322 | event (EV_P_ W w, int events) |
331 | ev_feed_event (EV_P_ void *w, int revents) |
323 | { |
332 | { |
|
|
333 | W w_ = (W)w; |
|
|
334 | |
324 | if (w->pending) |
335 | if (w_->pending) |
325 | { |
336 | { |
326 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
337 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
327 | return; |
338 | return; |
328 | } |
339 | } |
329 | |
340 | |
330 | w->pending = ++pendingcnt [ABSPRI (w)]; |
341 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
331 | array_needsize (ANPENDING, pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); |
342 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); |
332 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
343 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
333 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
344 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
334 | } |
345 | } |
335 | |
346 | |
336 | static void |
347 | static void |
337 | queue_events (EV_P_ W *events, int eventcnt, int type) |
348 | queue_events (EV_P_ W *events, int eventcnt, int type) |
338 | { |
349 | { |
339 | int i; |
350 | int i; |
340 | |
351 | |
341 | for (i = 0; i < eventcnt; ++i) |
352 | for (i = 0; i < eventcnt; ++i) |
342 | event (EV_A_ events [i], type); |
353 | ev_feed_event (EV_A_ events [i], type); |
343 | } |
354 | } |
344 | |
355 | |
345 | static void |
356 | inline void |
346 | fd_event (EV_P_ int fd, int events) |
357 | fd_event (EV_P_ int fd, int revents) |
347 | { |
358 | { |
348 | ANFD *anfd = anfds + fd; |
359 | ANFD *anfd = anfds + fd; |
349 | struct ev_io *w; |
360 | struct ev_io *w; |
350 | |
361 | |
351 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
362 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
352 | { |
363 | { |
353 | int ev = w->events & events; |
364 | int ev = w->events & revents; |
354 | |
365 | |
355 | if (ev) |
366 | if (ev) |
356 | event (EV_A_ (W)w, ev); |
367 | ev_feed_event (EV_A_ (W)w, ev); |
357 | } |
368 | } |
|
|
369 | } |
|
|
370 | |
|
|
371 | void |
|
|
372 | ev_feed_fd_event (EV_P_ int fd, int revents) |
|
|
373 | { |
|
|
374 | fd_event (EV_A_ fd, revents); |
358 | } |
375 | } |
359 | |
376 | |
360 | /*****************************************************************************/ |
377 | /*****************************************************************************/ |
361 | |
378 | |
362 | static void |
379 | static void |
… | |
… | |
403 | struct ev_io *w; |
420 | struct ev_io *w; |
404 | |
421 | |
405 | while ((w = (struct ev_io *)anfds [fd].head)) |
422 | while ((w = (struct ev_io *)anfds [fd].head)) |
406 | { |
423 | { |
407 | ev_io_stop (EV_A_ w); |
424 | ev_io_stop (EV_A_ w); |
408 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
425 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
409 | } |
426 | } |
410 | } |
427 | } |
411 | |
428 | |
412 | static int |
429 | static int |
413 | fd_valid (int fd) |
430 | fd_valid (int fd) |
… | |
… | |
550 | #endif |
567 | #endif |
551 | errno = old_errno; |
568 | errno = old_errno; |
552 | } |
569 | } |
553 | } |
570 | } |
554 | |
571 | |
|
|
572 | void |
|
|
573 | ev_feed_signal_event (EV_P_ int signum) |
|
|
574 | { |
|
|
575 | WL w; |
|
|
576 | |
|
|
577 | #if EV_MULTIPLICITY |
|
|
578 | assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
|
|
579 | #endif |
|
|
580 | |
|
|
581 | --signum; |
|
|
582 | |
|
|
583 | if (signum < 0 || signum >= signalmax) |
|
|
584 | return; |
|
|
585 | |
|
|
586 | signals [signum].gotsig = 0; |
|
|
587 | |
|
|
588 | for (w = signals [signum].head; w; w = w->next) |
|
|
589 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
|
|
590 | } |
|
|
591 | |
555 | static void |
592 | static void |
556 | sigcb (EV_P_ struct ev_io *iow, int revents) |
593 | sigcb (EV_P_ struct ev_io *iow, int revents) |
557 | { |
594 | { |
558 | WL w; |
|
|
559 | int signum; |
595 | int signum; |
560 | |
596 | |
561 | #ifdef WIN32 |
597 | #ifdef WIN32 |
562 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
598 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
563 | #else |
599 | #else |
… | |
… | |
565 | #endif |
601 | #endif |
566 | gotsig = 0; |
602 | gotsig = 0; |
567 | |
603 | |
568 | for (signum = signalmax; signum--; ) |
604 | for (signum = signalmax; signum--; ) |
569 | if (signals [signum].gotsig) |
605 | if (signals [signum].gotsig) |
570 | { |
606 | ev_feed_signal_event (EV_A_ signum + 1); |
571 | signals [signum].gotsig = 0; |
|
|
572 | |
|
|
573 | for (w = signals [signum].head; w; w = w->next) |
|
|
574 | event (EV_A_ (W)w, EV_SIGNAL); |
|
|
575 | } |
|
|
576 | } |
607 | } |
577 | |
608 | |
578 | static void |
609 | static void |
579 | siginit (EV_P) |
610 | siginit (EV_P) |
580 | { |
611 | { |
… | |
… | |
613 | if (w->pid == pid || !w->pid) |
644 | if (w->pid == pid || !w->pid) |
614 | { |
645 | { |
615 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
646 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
616 | w->rpid = pid; |
647 | w->rpid = pid; |
617 | w->rstatus = status; |
648 | w->rstatus = status; |
618 | event (EV_A_ (W)w, EV_CHILD); |
649 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
619 | } |
650 | } |
620 | } |
651 | } |
621 | |
652 | |
622 | static void |
653 | static void |
623 | childcb (EV_P_ struct ev_signal *sw, int revents) |
654 | childcb (EV_P_ struct ev_signal *sw, int revents) |
… | |
… | |
625 | int pid, status; |
656 | int pid, status; |
626 | |
657 | |
627 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
658 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
628 | { |
659 | { |
629 | /* make sure we are called again until all childs have been reaped */ |
660 | /* make sure we are called again until all childs have been reaped */ |
630 | event (EV_A_ (W)sw, EV_SIGNAL); |
661 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
631 | |
662 | |
632 | child_reap (EV_A_ sw, pid, pid, status); |
663 | child_reap (EV_A_ sw, pid, pid, status); |
633 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
664 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
634 | } |
665 | } |
635 | } |
666 | } |
… | |
… | |
820 | } |
851 | } |
821 | |
852 | |
822 | #endif |
853 | #endif |
823 | |
854 | |
824 | #if EV_MULTIPLICITY |
855 | #if EV_MULTIPLICITY |
825 | struct ev_loop default_loop_struct; |
|
|
826 | static struct ev_loop *default_loop; |
|
|
827 | |
|
|
828 | struct ev_loop * |
856 | struct ev_loop * |
829 | #else |
857 | #else |
830 | static int default_loop; |
|
|
831 | |
|
|
832 | int |
858 | int |
833 | #endif |
859 | #endif |
834 | ev_default_loop (int methods) |
860 | ev_default_loop (int methods) |
835 | { |
861 | { |
836 | if (sigpipe [0] == sigpipe [1]) |
862 | if (sigpipe [0] == sigpipe [1]) |
… | |
… | |
897 | postfork = 1; |
923 | postfork = 1; |
898 | } |
924 | } |
899 | |
925 | |
900 | /*****************************************************************************/ |
926 | /*****************************************************************************/ |
901 | |
927 | |
|
|
928 | static int |
|
|
929 | any_pending (EV_P) |
|
|
930 | { |
|
|
931 | int pri; |
|
|
932 | |
|
|
933 | for (pri = NUMPRI; pri--; ) |
|
|
934 | if (pendingcnt [pri]) |
|
|
935 | return 1; |
|
|
936 | |
|
|
937 | return 0; |
|
|
938 | } |
|
|
939 | |
902 | static void |
940 | static void |
903 | call_pending (EV_P) |
941 | call_pending (EV_P) |
904 | { |
942 | { |
905 | int pri; |
943 | int pri; |
906 | |
944 | |
… | |
… | |
910 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
948 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
911 | |
949 | |
912 | if (p->w) |
950 | if (p->w) |
913 | { |
951 | { |
914 | p->w->pending = 0; |
952 | p->w->pending = 0; |
915 | p->w->cb (EV_A_ p->w, p->events); |
953 | EV_CB_INVOKE (p->w, p->events); |
916 | } |
954 | } |
917 | } |
955 | } |
918 | } |
956 | } |
919 | |
957 | |
920 | static void |
958 | static void |
… | |
… | |
934 | downheap ((WT *)timers, timercnt, 0); |
972 | downheap ((WT *)timers, timercnt, 0); |
935 | } |
973 | } |
936 | else |
974 | else |
937 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
975 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
938 | |
976 | |
939 | event (EV_A_ (W)w, EV_TIMEOUT); |
977 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
940 | } |
978 | } |
941 | } |
979 | } |
942 | |
980 | |
943 | static void |
981 | static void |
944 | periodics_reify (EV_P) |
982 | periodics_reify (EV_P) |
… | |
… | |
948 | struct ev_periodic *w = periodics [0]; |
986 | struct ev_periodic *w = periodics [0]; |
949 | |
987 | |
950 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
988 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
951 | |
989 | |
952 | /* first reschedule or stop timer */ |
990 | /* first reschedule or stop timer */ |
|
|
991 | if (w->reschedule_cb) |
|
|
992 | { |
|
|
993 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); |
|
|
994 | |
|
|
995 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); |
|
|
996 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
997 | } |
953 | if (w->interval) |
998 | else if (w->interval) |
954 | { |
999 | { |
955 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1000 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
956 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
1001 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
957 | downheap ((WT *)periodics, periodiccnt, 0); |
1002 | downheap ((WT *)periodics, periodiccnt, 0); |
958 | } |
1003 | } |
959 | else |
1004 | else |
960 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1005 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
961 | |
1006 | |
962 | event (EV_A_ (W)w, EV_PERIODIC); |
1007 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
963 | } |
1008 | } |
964 | } |
1009 | } |
965 | |
1010 | |
966 | static void |
1011 | static void |
967 | periodics_reschedule (EV_P) |
1012 | periodics_reschedule (EV_P) |
… | |
… | |
971 | /* adjust periodics after time jump */ |
1016 | /* adjust periodics after time jump */ |
972 | for (i = 0; i < periodiccnt; ++i) |
1017 | for (i = 0; i < periodiccnt; ++i) |
973 | { |
1018 | { |
974 | struct ev_periodic *w = periodics [i]; |
1019 | struct ev_periodic *w = periodics [i]; |
975 | |
1020 | |
|
|
1021 | if (w->reschedule_cb) |
|
|
1022 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
976 | if (w->interval) |
1023 | else if (w->interval) |
977 | { |
|
|
978 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1024 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
979 | |
|
|
980 | if (fabs (diff) >= 1e-4) |
|
|
981 | { |
|
|
982 | ev_periodic_stop (EV_A_ w); |
|
|
983 | ev_periodic_start (EV_A_ w); |
|
|
984 | |
|
|
985 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
986 | } |
|
|
987 | } |
|
|
988 | } |
1025 | } |
|
|
1026 | |
|
|
1027 | /* now rebuild the heap */ |
|
|
1028 | for (i = periodiccnt >> 1; i--; ) |
|
|
1029 | downheap ((WT *)periodics, periodiccnt, i); |
989 | } |
1030 | } |
990 | |
1031 | |
991 | inline int |
1032 | inline int |
992 | time_update_monotonic (EV_P) |
1033 | time_update_monotonic (EV_P) |
993 | { |
1034 | { |
… | |
… | |
1089 | /* update fd-related kernel structures */ |
1130 | /* update fd-related kernel structures */ |
1090 | fd_reify (EV_A); |
1131 | fd_reify (EV_A); |
1091 | |
1132 | |
1092 | /* calculate blocking time */ |
1133 | /* calculate blocking time */ |
1093 | |
1134 | |
1094 | /* we only need this for !monotonic clockor timers, but as we basically |
1135 | /* we only need this for !monotonic clock or timers, but as we basically |
1095 | always have timers, we just calculate it always */ |
1136 | always have timers, we just calculate it always */ |
1096 | #if EV_USE_MONOTONIC |
1137 | #if EV_USE_MONOTONIC |
1097 | if (expect_true (have_monotonic)) |
1138 | if (expect_true (have_monotonic)) |
1098 | time_update_monotonic (EV_A); |
1139 | time_update_monotonic (EV_A); |
1099 | else |
1140 | else |
… | |
… | |
1132 | /* queue pending timers and reschedule them */ |
1173 | /* queue pending timers and reschedule them */ |
1133 | timers_reify (EV_A); /* relative timers called last */ |
1174 | timers_reify (EV_A); /* relative timers called last */ |
1134 | periodics_reify (EV_A); /* absolute timers called first */ |
1175 | periodics_reify (EV_A); /* absolute timers called first */ |
1135 | |
1176 | |
1136 | /* queue idle watchers unless io or timers are pending */ |
1177 | /* queue idle watchers unless io or timers are pending */ |
1137 | if (!pendingcnt) |
1178 | if (idlecnt && !any_pending (EV_A)) |
1138 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1179 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1139 | |
1180 | |
1140 | /* queue check watchers, to be executed first */ |
1181 | /* queue check watchers, to be executed first */ |
1141 | if (checkcnt) |
1182 | if (checkcnt) |
1142 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1183 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
… | |
… | |
1297 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1338 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1298 | { |
1339 | { |
1299 | if (ev_is_active (w)) |
1340 | if (ev_is_active (w)) |
1300 | return; |
1341 | return; |
1301 | |
1342 | |
|
|
1343 | if (w->reschedule_cb) |
|
|
1344 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
|
|
1345 | else if (w->interval) |
|
|
1346 | { |
1302 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1347 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1303 | |
|
|
1304 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1348 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1305 | if (w->interval) |
|
|
1306 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1349 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
|
|
1350 | } |
1307 | |
1351 | |
1308 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1352 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1309 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1353 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1310 | periodics [periodiccnt - 1] = w; |
1354 | periodics [periodiccnt - 1] = w; |
1311 | upheap ((WT *)periodics, periodiccnt - 1); |
1355 | upheap ((WT *)periodics, periodiccnt - 1); |
… | |
… | |
1327 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1371 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1328 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1372 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1329 | } |
1373 | } |
1330 | |
1374 | |
1331 | ev_stop (EV_A_ (W)w); |
1375 | ev_stop (EV_A_ (W)w); |
|
|
1376 | } |
|
|
1377 | |
|
|
1378 | void |
|
|
1379 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
|
|
1380 | { |
|
|
1381 | ev_periodic_stop (EV_A_ w); |
|
|
1382 | ev_periodic_start (EV_A_ w); |
1332 | } |
1383 | } |
1333 | |
1384 | |
1334 | void |
1385 | void |
1335 | ev_idle_start (EV_P_ struct ev_idle *w) |
1386 | ev_idle_start (EV_P_ struct ev_idle *w) |
1336 | { |
1387 | { |