… | |
… | |
316 | |
316 | |
317 | ++base; |
317 | ++base; |
318 | } |
318 | } |
319 | } |
319 | } |
320 | |
320 | |
321 | static void |
321 | void |
322 | event (EV_P_ W w, int events) |
322 | ev_feed_event (EV_P_ void *w, int revents) |
323 | { |
323 | { |
|
|
324 | W w_ = (W)w; |
|
|
325 | |
324 | if (w->pending) |
326 | if (w_->pending) |
325 | { |
327 | { |
326 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
328 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
327 | return; |
329 | return; |
328 | } |
330 | } |
329 | |
331 | |
330 | w->pending = ++pendingcnt [ABSPRI (w)]; |
332 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
331 | array_needsize (ANPENDING, pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); |
333 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); |
332 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
334 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
333 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
335 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
334 | } |
336 | } |
335 | |
337 | |
336 | static void |
338 | static void |
337 | queue_events (EV_P_ W *events, int eventcnt, int type) |
339 | queue_events (EV_P_ W *events, int eventcnt, int type) |
338 | { |
340 | { |
339 | int i; |
341 | int i; |
340 | |
342 | |
341 | for (i = 0; i < eventcnt; ++i) |
343 | for (i = 0; i < eventcnt; ++i) |
342 | event (EV_A_ events [i], type); |
344 | ev_feed_event (EV_A_ events [i], type); |
343 | } |
345 | } |
344 | |
346 | |
345 | static void |
347 | static void |
346 | fd_event (EV_P_ int fd, int events) |
348 | fd_event (EV_P_ int fd, int events) |
347 | { |
349 | { |
… | |
… | |
351 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
353 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
352 | { |
354 | { |
353 | int ev = w->events & events; |
355 | int ev = w->events & events; |
354 | |
356 | |
355 | if (ev) |
357 | if (ev) |
356 | event (EV_A_ (W)w, ev); |
358 | ev_feed_event (EV_A_ (W)w, ev); |
357 | } |
359 | } |
358 | } |
360 | } |
359 | |
361 | |
360 | /*****************************************************************************/ |
362 | /*****************************************************************************/ |
361 | |
363 | |
… | |
… | |
403 | struct ev_io *w; |
405 | struct ev_io *w; |
404 | |
406 | |
405 | while ((w = (struct ev_io *)anfds [fd].head)) |
407 | while ((w = (struct ev_io *)anfds [fd].head)) |
406 | { |
408 | { |
407 | ev_io_stop (EV_A_ w); |
409 | ev_io_stop (EV_A_ w); |
408 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
410 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
409 | } |
411 | } |
410 | } |
412 | } |
411 | |
413 | |
412 | static int |
414 | static int |
413 | fd_valid (int fd) |
415 | fd_valid (int fd) |
… | |
… | |
569 | if (signals [signum].gotsig) |
571 | if (signals [signum].gotsig) |
570 | { |
572 | { |
571 | signals [signum].gotsig = 0; |
573 | signals [signum].gotsig = 0; |
572 | |
574 | |
573 | for (w = signals [signum].head; w; w = w->next) |
575 | for (w = signals [signum].head; w; w = w->next) |
574 | event (EV_A_ (W)w, EV_SIGNAL); |
576 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
575 | } |
577 | } |
576 | } |
578 | } |
577 | |
579 | |
578 | static void |
580 | static void |
579 | siginit (EV_P) |
581 | siginit (EV_P) |
… | |
… | |
613 | if (w->pid == pid || !w->pid) |
615 | if (w->pid == pid || !w->pid) |
614 | { |
616 | { |
615 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
617 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
616 | w->rpid = pid; |
618 | w->rpid = pid; |
617 | w->rstatus = status; |
619 | w->rstatus = status; |
618 | event (EV_A_ (W)w, EV_CHILD); |
620 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
619 | } |
621 | } |
620 | } |
622 | } |
621 | |
623 | |
622 | static void |
624 | static void |
623 | childcb (EV_P_ struct ev_signal *sw, int revents) |
625 | childcb (EV_P_ struct ev_signal *sw, int revents) |
… | |
… | |
625 | int pid, status; |
627 | int pid, status; |
626 | |
628 | |
627 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
629 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
628 | { |
630 | { |
629 | /* make sure we are called again until all childs have been reaped */ |
631 | /* make sure we are called again until all childs have been reaped */ |
630 | event (EV_A_ (W)sw, EV_SIGNAL); |
632 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
631 | |
633 | |
632 | child_reap (EV_A_ sw, pid, pid, status); |
634 | 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 */ |
635 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
634 | } |
636 | } |
635 | } |
637 | } |
… | |
… | |
896 | if (method) |
898 | if (method) |
897 | postfork = 1; |
899 | postfork = 1; |
898 | } |
900 | } |
899 | |
901 | |
900 | /*****************************************************************************/ |
902 | /*****************************************************************************/ |
|
|
903 | |
|
|
904 | static int |
|
|
905 | any_pending (EV_P) |
|
|
906 | { |
|
|
907 | int pri; |
|
|
908 | |
|
|
909 | for (pri = NUMPRI; pri--; ) |
|
|
910 | if (pendingcnt [pri]) |
|
|
911 | return 1; |
|
|
912 | |
|
|
913 | return 0; |
|
|
914 | } |
901 | |
915 | |
902 | static void |
916 | static void |
903 | call_pending (EV_P) |
917 | call_pending (EV_P) |
904 | { |
918 | { |
905 | int pri; |
919 | int pri; |
… | |
… | |
934 | downheap ((WT *)timers, timercnt, 0); |
948 | downheap ((WT *)timers, timercnt, 0); |
935 | } |
949 | } |
936 | else |
950 | else |
937 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
951 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
938 | |
952 | |
939 | event (EV_A_ (W)w, EV_TIMEOUT); |
953 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
940 | } |
954 | } |
941 | } |
955 | } |
942 | |
956 | |
943 | static void |
957 | static void |
944 | periodics_reify (EV_P) |
958 | periodics_reify (EV_P) |
… | |
… | |
948 | struct ev_periodic *w = periodics [0]; |
962 | struct ev_periodic *w = periodics [0]; |
949 | |
963 | |
950 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
964 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
951 | |
965 | |
952 | /* first reschedule or stop timer */ |
966 | /* first reschedule or stop timer */ |
|
|
967 | if (w->reschedule_cb) |
|
|
968 | { |
|
|
969 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); |
|
|
970 | |
|
|
971 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); |
|
|
972 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
973 | } |
953 | if (w->interval) |
974 | else if (w->interval) |
954 | { |
975 | { |
955 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
976 | ((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)); |
977 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
957 | downheap ((WT *)periodics, periodiccnt, 0); |
978 | downheap ((WT *)periodics, periodiccnt, 0); |
958 | } |
979 | } |
959 | else |
980 | else |
960 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
981 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
961 | |
982 | |
962 | event (EV_A_ (W)w, EV_PERIODIC); |
983 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
963 | } |
984 | } |
964 | } |
985 | } |
965 | |
986 | |
966 | static void |
987 | static void |
967 | periodics_reschedule (EV_P) |
988 | periodics_reschedule (EV_P) |
… | |
… | |
971 | /* adjust periodics after time jump */ |
992 | /* adjust periodics after time jump */ |
972 | for (i = 0; i < periodiccnt; ++i) |
993 | for (i = 0; i < periodiccnt; ++i) |
973 | { |
994 | { |
974 | struct ev_periodic *w = periodics [i]; |
995 | struct ev_periodic *w = periodics [i]; |
975 | |
996 | |
|
|
997 | if (w->reschedule_cb) |
|
|
998 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
976 | if (w->interval) |
999 | else if (w->interval) |
977 | { |
|
|
978 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1000 | ((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 | } |
1001 | } |
|
|
1002 | |
|
|
1003 | /* now rebuild the heap */ |
|
|
1004 | for (i = periodiccnt >> 1; i--; ) |
|
|
1005 | downheap ((WT *)periodics, periodiccnt, i); |
989 | } |
1006 | } |
990 | |
1007 | |
991 | inline int |
1008 | inline int |
992 | time_update_monotonic (EV_P) |
1009 | time_update_monotonic (EV_P) |
993 | { |
1010 | { |
… | |
… | |
1089 | /* update fd-related kernel structures */ |
1106 | /* update fd-related kernel structures */ |
1090 | fd_reify (EV_A); |
1107 | fd_reify (EV_A); |
1091 | |
1108 | |
1092 | /* calculate blocking time */ |
1109 | /* calculate blocking time */ |
1093 | |
1110 | |
1094 | /* we only need this for !monotonic clockor timers, but as we basically |
1111 | /* we only need this for !monotonic clock or timers, but as we basically |
1095 | always have timers, we just calculate it always */ |
1112 | always have timers, we just calculate it always */ |
1096 | #if EV_USE_MONOTONIC |
1113 | #if EV_USE_MONOTONIC |
1097 | if (expect_true (have_monotonic)) |
1114 | if (expect_true (have_monotonic)) |
1098 | time_update_monotonic (EV_A); |
1115 | time_update_monotonic (EV_A); |
1099 | else |
1116 | else |
… | |
… | |
1132 | /* queue pending timers and reschedule them */ |
1149 | /* queue pending timers and reschedule them */ |
1133 | timers_reify (EV_A); /* relative timers called last */ |
1150 | timers_reify (EV_A); /* relative timers called last */ |
1134 | periodics_reify (EV_A); /* absolute timers called first */ |
1151 | periodics_reify (EV_A); /* absolute timers called first */ |
1135 | |
1152 | |
1136 | /* queue idle watchers unless io or timers are pending */ |
1153 | /* queue idle watchers unless io or timers are pending */ |
1137 | if (!pendingcnt) |
1154 | if (idlecnt && !any_pending (EV_A)) |
1138 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1155 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1139 | |
1156 | |
1140 | /* queue check watchers, to be executed first */ |
1157 | /* queue check watchers, to be executed first */ |
1141 | if (checkcnt) |
1158 | if (checkcnt) |
1142 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1159 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
… | |
… | |
1297 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1314 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1298 | { |
1315 | { |
1299 | if (ev_is_active (w)) |
1316 | if (ev_is_active (w)) |
1300 | return; |
1317 | return; |
1301 | |
1318 | |
|
|
1319 | if (w->reschedule_cb) |
|
|
1320 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
|
|
1321 | else if (w->interval) |
|
|
1322 | { |
1302 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1323 | 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 */ |
1324 | /* 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; |
1325 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
|
|
1326 | } |
1307 | |
1327 | |
1308 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1328 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1309 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1329 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1310 | periodics [periodiccnt - 1] = w; |
1330 | periodics [periodiccnt - 1] = w; |
1311 | upheap ((WT *)periodics, periodiccnt - 1); |
1331 | upheap ((WT *)periodics, periodiccnt - 1); |
… | |
… | |
1327 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1347 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1328 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1348 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1329 | } |
1349 | } |
1330 | |
1350 | |
1331 | ev_stop (EV_A_ (W)w); |
1351 | ev_stop (EV_A_ (W)w); |
|
|
1352 | } |
|
|
1353 | |
|
|
1354 | void |
|
|
1355 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
|
|
1356 | { |
|
|
1357 | ev_periodic_stop (EV_A_ w); |
|
|
1358 | ev_periodic_start (EV_A_ w); |
1332 | } |
1359 | } |
1333 | |
1360 | |
1334 | void |
1361 | void |
1335 | ev_idle_start (EV_P_ struct ev_idle *w) |
1362 | ev_idle_start (EV_P_ struct ev_idle *w) |
1336 | { |
1363 | { |