… | |
… | |
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]) |
… | |
… | |
946 | downheap ((WT *)timers, timercnt, 0); |
972 | downheap ((WT *)timers, timercnt, 0); |
947 | } |
973 | } |
948 | else |
974 | else |
949 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
975 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
950 | |
976 | |
951 | event (EV_A_ (W)w, EV_TIMEOUT); |
977 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
952 | } |
978 | } |
953 | } |
979 | } |
954 | |
980 | |
955 | static void |
981 | static void |
956 | periodics_reify (EV_P) |
982 | periodics_reify (EV_P) |
… | |
… | |
960 | struct ev_periodic *w = periodics [0]; |
986 | struct ev_periodic *w = periodics [0]; |
961 | |
987 | |
962 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
988 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
963 | |
989 | |
964 | /* 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 | } |
965 | if (w->interval) |
998 | else if (w->interval) |
966 | { |
999 | { |
967 | ((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; |
968 | 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)); |
969 | downheap ((WT *)periodics, periodiccnt, 0); |
1002 | downheap ((WT *)periodics, periodiccnt, 0); |
970 | } |
1003 | } |
971 | else |
1004 | else |
972 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1005 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
973 | |
1006 | |
974 | event (EV_A_ (W)w, EV_PERIODIC); |
1007 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
975 | } |
1008 | } |
976 | } |
1009 | } |
977 | |
1010 | |
978 | static void |
1011 | static void |
979 | periodics_reschedule (EV_P) |
1012 | periodics_reschedule (EV_P) |
… | |
… | |
983 | /* adjust periodics after time jump */ |
1016 | /* adjust periodics after time jump */ |
984 | for (i = 0; i < periodiccnt; ++i) |
1017 | for (i = 0; i < periodiccnt; ++i) |
985 | { |
1018 | { |
986 | struct ev_periodic *w = periodics [i]; |
1019 | struct ev_periodic *w = periodics [i]; |
987 | |
1020 | |
|
|
1021 | if (w->reschedule_cb) |
|
|
1022 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
988 | if (w->interval) |
1023 | else if (w->interval) |
989 | { |
|
|
990 | 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; |
991 | |
|
|
992 | if (fabs (diff) >= 1e-4) |
|
|
993 | { |
|
|
994 | ev_periodic_stop (EV_A_ w); |
|
|
995 | ev_periodic_start (EV_A_ w); |
|
|
996 | |
|
|
997 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
998 | } |
|
|
999 | } |
|
|
1000 | } |
1025 | } |
|
|
1026 | |
|
|
1027 | /* now rebuild the heap */ |
|
|
1028 | for (i = periodiccnt >> 1; i--; ) |
|
|
1029 | downheap ((WT *)periodics, periodiccnt, i); |
1001 | } |
1030 | } |
1002 | |
1031 | |
1003 | inline int |
1032 | inline int |
1004 | time_update_monotonic (EV_P) |
1033 | time_update_monotonic (EV_P) |
1005 | { |
1034 | { |
… | |
… | |
1309 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1338 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1310 | { |
1339 | { |
1311 | if (ev_is_active (w)) |
1340 | if (ev_is_active (w)) |
1312 | return; |
1341 | return; |
1313 | |
1342 | |
|
|
1343 | if (w->reschedule_cb) |
|
|
1344 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
|
|
1345 | else if (w->interval) |
|
|
1346 | { |
1314 | 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.)); |
1315 | |
|
|
1316 | /* 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 */ |
1317 | if (w->interval) |
|
|
1318 | ((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 | } |
1319 | |
1351 | |
1320 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1352 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1321 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1353 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1322 | periodics [periodiccnt - 1] = w; |
1354 | periodics [periodiccnt - 1] = w; |
1323 | upheap ((WT *)periodics, periodiccnt - 1); |
1355 | upheap ((WT *)periodics, periodiccnt - 1); |
… | |
… | |
1339 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1371 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1340 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1372 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1341 | } |
1373 | } |
1342 | |
1374 | |
1343 | 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); |
1344 | } |
1383 | } |
1345 | |
1384 | |
1346 | void |
1385 | void |
1347 | ev_idle_start (EV_P_ struct ev_idle *w) |
1386 | ev_idle_start (EV_P_ struct ev_idle *w) |
1348 | { |
1387 | { |