… | |
… | |
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 | { |
|
|
226 | ev_tstamp ev_rt_now; |
222 | # define VAR(name,decl) decl; |
227 | #define VAR(name,decl) decl; |
223 | # include "ev_vars.h" |
228 | #include "ev_vars.h" |
224 | }; |
|
|
225 | # undef VAR |
229 | #undef VAR |
|
|
230 | }; |
226 | # include "ev_wrap.h" |
231 | #include "ev_wrap.h" |
|
|
232 | |
|
|
233 | struct ev_loop default_loop_struct; |
|
|
234 | static struct ev_loop *default_loop; |
227 | |
235 | |
228 | #else |
236 | #else |
229 | |
237 | |
|
|
238 | ev_tstamp ev_rt_now; |
230 | # define VAR(name,decl) static decl; |
239 | #define VAR(name,decl) static decl; |
231 | # include "ev_vars.h" |
240 | #include "ev_vars.h" |
232 | # undef VAR |
241 | #undef VAR |
|
|
242 | |
|
|
243 | static int default_loop; |
233 | |
244 | |
234 | #endif |
245 | #endif |
235 | |
246 | |
236 | /*****************************************************************************/ |
247 | /*****************************************************************************/ |
237 | |
248 | |
… | |
… | |
262 | #endif |
273 | #endif |
263 | |
274 | |
264 | return ev_time (); |
275 | return ev_time (); |
265 | } |
276 | } |
266 | |
277 | |
|
|
278 | #if EV_MULTIPLICITY |
267 | ev_tstamp |
279 | ev_tstamp |
268 | ev_now (EV_P) |
280 | ev_now (EV_P) |
269 | { |
281 | { |
270 | return rt_now; |
282 | return ev_rt_now; |
271 | } |
283 | } |
|
|
284 | #endif |
272 | |
285 | |
273 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
286 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
274 | |
287 | |
275 | #define array_needsize(type,base,cur,cnt,init) \ |
288 | #define array_needsize(type,base,cur,cnt,init) \ |
276 | if (expect_false ((cnt) > cur)) \ |
289 | if (expect_false ((cnt) > cur)) \ |
… | |
… | |
316 | |
329 | |
317 | ++base; |
330 | ++base; |
318 | } |
331 | } |
319 | } |
332 | } |
320 | |
333 | |
321 | static void |
334 | void |
322 | event (EV_P_ W w, int events) |
335 | ev_feed_event (EV_P_ void *w, int revents) |
323 | { |
336 | { |
|
|
337 | W w_ = (W)w; |
|
|
338 | |
324 | if (w->pending) |
339 | if (w_->pending) |
325 | { |
340 | { |
326 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
341 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
327 | return; |
342 | return; |
328 | } |
343 | } |
329 | |
344 | |
330 | w->pending = ++pendingcnt [ABSPRI (w)]; |
345 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
331 | array_needsize (ANPENDING, pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); |
346 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); |
332 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
347 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
333 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
348 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
334 | } |
349 | } |
335 | |
350 | |
336 | static void |
351 | static void |
337 | queue_events (EV_P_ W *events, int eventcnt, int type) |
352 | queue_events (EV_P_ W *events, int eventcnt, int type) |
338 | { |
353 | { |
339 | int i; |
354 | int i; |
340 | |
355 | |
341 | for (i = 0; i < eventcnt; ++i) |
356 | for (i = 0; i < eventcnt; ++i) |
342 | event (EV_A_ events [i], type); |
357 | ev_feed_event (EV_A_ events [i], type); |
343 | } |
358 | } |
344 | |
359 | |
345 | static void |
360 | inline void |
346 | fd_event (EV_P_ int fd, int events) |
361 | fd_event (EV_P_ int fd, int revents) |
347 | { |
362 | { |
348 | ANFD *anfd = anfds + fd; |
363 | ANFD *anfd = anfds + fd; |
349 | struct ev_io *w; |
364 | struct ev_io *w; |
350 | |
365 | |
351 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
366 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
352 | { |
367 | { |
353 | int ev = w->events & events; |
368 | int ev = w->events & revents; |
354 | |
369 | |
355 | if (ev) |
370 | if (ev) |
356 | event (EV_A_ (W)w, ev); |
371 | ev_feed_event (EV_A_ (W)w, ev); |
357 | } |
372 | } |
|
|
373 | } |
|
|
374 | |
|
|
375 | void |
|
|
376 | ev_feed_fd_event (EV_P_ int fd, int revents) |
|
|
377 | { |
|
|
378 | fd_event (EV_A_ fd, revents); |
358 | } |
379 | } |
359 | |
380 | |
360 | /*****************************************************************************/ |
381 | /*****************************************************************************/ |
361 | |
382 | |
362 | static void |
383 | static void |
… | |
… | |
403 | struct ev_io *w; |
424 | struct ev_io *w; |
404 | |
425 | |
405 | while ((w = (struct ev_io *)anfds [fd].head)) |
426 | while ((w = (struct ev_io *)anfds [fd].head)) |
406 | { |
427 | { |
407 | ev_io_stop (EV_A_ w); |
428 | ev_io_stop (EV_A_ w); |
408 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
429 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
409 | } |
430 | } |
410 | } |
431 | } |
411 | |
432 | |
412 | static int |
433 | static int |
413 | fd_valid (int fd) |
434 | fd_valid (int fd) |
… | |
… | |
501 | |
522 | |
502 | heap [k] = w; |
523 | heap [k] = w; |
503 | ((W)heap [k])->active = k + 1; |
524 | ((W)heap [k])->active = k + 1; |
504 | } |
525 | } |
505 | |
526 | |
|
|
527 | inline void |
|
|
528 | adjustheap (WT *heap, int N, int k, ev_tstamp at) |
|
|
529 | { |
|
|
530 | ev_tstamp old_at = heap [k]->at; |
|
|
531 | heap [k]->at = at; |
|
|
532 | |
|
|
533 | if (old_at < at) |
|
|
534 | downheap (heap, N, k); |
|
|
535 | else |
|
|
536 | upheap (heap, k); |
|
|
537 | } |
|
|
538 | |
506 | /*****************************************************************************/ |
539 | /*****************************************************************************/ |
507 | |
540 | |
508 | typedef struct |
541 | typedef struct |
509 | { |
542 | { |
510 | WL head; |
543 | WL head; |
… | |
… | |
541 | |
574 | |
542 | if (!gotsig) |
575 | if (!gotsig) |
543 | { |
576 | { |
544 | int old_errno = errno; |
577 | int old_errno = errno; |
545 | gotsig = 1; |
578 | gotsig = 1; |
|
|
579 | #ifdef WIN32 |
|
|
580 | send (sigpipe [1], &signum, 1, MSG_DONTWAIT); |
|
|
581 | #else |
546 | write (sigpipe [1], &signum, 1); |
582 | write (sigpipe [1], &signum, 1); |
|
|
583 | #endif |
547 | errno = old_errno; |
584 | errno = old_errno; |
548 | } |
585 | } |
549 | } |
586 | } |
550 | |
587 | |
|
|
588 | void |
|
|
589 | ev_feed_signal_event (EV_P_ int signum) |
|
|
590 | { |
|
|
591 | WL w; |
|
|
592 | |
|
|
593 | #if EV_MULTIPLICITY |
|
|
594 | assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
|
|
595 | #endif |
|
|
596 | |
|
|
597 | --signum; |
|
|
598 | |
|
|
599 | if (signum < 0 || signum >= signalmax) |
|
|
600 | return; |
|
|
601 | |
|
|
602 | signals [signum].gotsig = 0; |
|
|
603 | |
|
|
604 | for (w = signals [signum].head; w; w = w->next) |
|
|
605 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
|
|
606 | } |
|
|
607 | |
551 | static void |
608 | static void |
552 | sigcb (EV_P_ struct ev_io *iow, int revents) |
609 | sigcb (EV_P_ struct ev_io *iow, int revents) |
553 | { |
610 | { |
554 | WL w; |
|
|
555 | int signum; |
611 | int signum; |
556 | |
612 | |
|
|
613 | #ifdef WIN32 |
|
|
614 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
|
|
615 | #else |
557 | read (sigpipe [0], &revents, 1); |
616 | read (sigpipe [0], &revents, 1); |
|
|
617 | #endif |
558 | gotsig = 0; |
618 | gotsig = 0; |
559 | |
619 | |
560 | for (signum = signalmax; signum--; ) |
620 | for (signum = signalmax; signum--; ) |
561 | if (signals [signum].gotsig) |
621 | if (signals [signum].gotsig) |
562 | { |
622 | ev_feed_signal_event (EV_A_ signum + 1); |
563 | signals [signum].gotsig = 0; |
|
|
564 | |
|
|
565 | for (w = signals [signum].head; w; w = w->next) |
|
|
566 | event (EV_A_ (W)w, EV_SIGNAL); |
|
|
567 | } |
|
|
568 | } |
623 | } |
569 | |
624 | |
570 | static void |
625 | static void |
571 | siginit (EV_P) |
626 | siginit (EV_P) |
572 | { |
627 | { |
… | |
… | |
605 | if (w->pid == pid || !w->pid) |
660 | if (w->pid == pid || !w->pid) |
606 | { |
661 | { |
607 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
662 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
608 | w->rpid = pid; |
663 | w->rpid = pid; |
609 | w->rstatus = status; |
664 | w->rstatus = status; |
610 | event (EV_A_ (W)w, EV_CHILD); |
665 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
611 | } |
666 | } |
612 | } |
667 | } |
613 | |
668 | |
614 | static void |
669 | static void |
615 | childcb (EV_P_ struct ev_signal *sw, int revents) |
670 | childcb (EV_P_ struct ev_signal *sw, int revents) |
… | |
… | |
617 | int pid, status; |
672 | int pid, status; |
618 | |
673 | |
619 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
674 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
620 | { |
675 | { |
621 | /* make sure we are called again until all childs have been reaped */ |
676 | /* make sure we are called again until all childs have been reaped */ |
622 | event (EV_A_ (W)sw, EV_SIGNAL); |
677 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
623 | |
678 | |
624 | child_reap (EV_A_ sw, pid, pid, status); |
679 | child_reap (EV_A_ sw, pid, pid, status); |
625 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
680 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
626 | } |
681 | } |
627 | } |
682 | } |
… | |
… | |
684 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
739 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
685 | have_monotonic = 1; |
740 | have_monotonic = 1; |
686 | } |
741 | } |
687 | #endif |
742 | #endif |
688 | |
743 | |
689 | rt_now = ev_time (); |
744 | ev_rt_now = ev_time (); |
690 | mn_now = get_clock (); |
745 | mn_now = get_clock (); |
691 | now_floor = mn_now; |
746 | now_floor = mn_now; |
692 | rtmn_diff = rt_now - mn_now; |
747 | rtmn_diff = ev_rt_now - mn_now; |
693 | |
748 | |
694 | if (methods == EVMETHOD_AUTO) |
749 | if (methods == EVMETHOD_AUTO) |
695 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
750 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
696 | methods = atoi (getenv ("LIBEV_METHODS")); |
751 | methods = atoi (getenv ("LIBEV_METHODS")); |
697 | else |
752 | else |
… | |
… | |
712 | #endif |
767 | #endif |
713 | #if EV_USE_SELECT |
768 | #if EV_USE_SELECT |
714 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
769 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
715 | #endif |
770 | #endif |
716 | |
771 | |
717 | ev_watcher_init (&sigev, sigcb); |
772 | ev_init (&sigev, sigcb); |
718 | ev_set_priority (&sigev, EV_MAXPRI); |
773 | ev_set_priority (&sigev, EV_MAXPRI); |
719 | } |
774 | } |
720 | } |
775 | } |
721 | |
776 | |
722 | void |
777 | void |
… | |
… | |
812 | } |
867 | } |
813 | |
868 | |
814 | #endif |
869 | #endif |
815 | |
870 | |
816 | #if EV_MULTIPLICITY |
871 | #if EV_MULTIPLICITY |
817 | struct ev_loop default_loop_struct; |
|
|
818 | static struct ev_loop *default_loop; |
|
|
819 | |
|
|
820 | struct ev_loop * |
872 | struct ev_loop * |
821 | #else |
873 | #else |
822 | static int default_loop; |
|
|
823 | |
|
|
824 | int |
874 | int |
825 | #endif |
875 | #endif |
826 | ev_default_loop (int methods) |
876 | ev_default_loop (int methods) |
827 | { |
877 | { |
828 | if (sigpipe [0] == sigpipe [1]) |
878 | if (sigpipe [0] == sigpipe [1]) |
… | |
… | |
889 | postfork = 1; |
939 | postfork = 1; |
890 | } |
940 | } |
891 | |
941 | |
892 | /*****************************************************************************/ |
942 | /*****************************************************************************/ |
893 | |
943 | |
|
|
944 | static int |
|
|
945 | any_pending (EV_P) |
|
|
946 | { |
|
|
947 | int pri; |
|
|
948 | |
|
|
949 | for (pri = NUMPRI; pri--; ) |
|
|
950 | if (pendingcnt [pri]) |
|
|
951 | return 1; |
|
|
952 | |
|
|
953 | return 0; |
|
|
954 | } |
|
|
955 | |
894 | static void |
956 | static void |
895 | call_pending (EV_P) |
957 | call_pending (EV_P) |
896 | { |
958 | { |
897 | int pri; |
959 | int pri; |
898 | |
960 | |
… | |
… | |
902 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
964 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
903 | |
965 | |
904 | if (p->w) |
966 | if (p->w) |
905 | { |
967 | { |
906 | p->w->pending = 0; |
968 | p->w->pending = 0; |
907 | p->w->cb (EV_A_ p->w, p->events); |
969 | EV_CB_INVOKE (p->w, p->events); |
908 | } |
970 | } |
909 | } |
971 | } |
910 | } |
972 | } |
911 | |
973 | |
912 | static void |
974 | static void |
… | |
… | |
926 | downheap ((WT *)timers, timercnt, 0); |
988 | downheap ((WT *)timers, timercnt, 0); |
927 | } |
989 | } |
928 | else |
990 | else |
929 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
991 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
930 | |
992 | |
931 | event (EV_A_ (W)w, EV_TIMEOUT); |
993 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
932 | } |
994 | } |
933 | } |
995 | } |
934 | |
996 | |
935 | static void |
997 | static void |
936 | periodics_reify (EV_P) |
998 | periodics_reify (EV_P) |
937 | { |
999 | { |
938 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
1000 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
939 | { |
1001 | { |
940 | struct ev_periodic *w = periodics [0]; |
1002 | struct ev_periodic *w = periodics [0]; |
941 | |
1003 | |
942 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1004 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
943 | |
1005 | |
944 | /* first reschedule or stop timer */ |
1006 | /* first reschedule or stop timer */ |
945 | if (w->interval) |
1007 | if (w->reschedule_cb) |
946 | { |
1008 | { |
|
|
1009 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
|
|
1010 | |
|
|
1011 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
|
|
1012 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
1013 | } |
|
|
1014 | else if (w->interval) |
|
|
1015 | { |
947 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1016 | ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
948 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
1017 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); |
949 | downheap ((WT *)periodics, periodiccnt, 0); |
1018 | downheap ((WT *)periodics, periodiccnt, 0); |
950 | } |
1019 | } |
951 | else |
1020 | else |
952 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1021 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
953 | |
1022 | |
954 | event (EV_A_ (W)w, EV_PERIODIC); |
1023 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
955 | } |
1024 | } |
956 | } |
1025 | } |
957 | |
1026 | |
958 | static void |
1027 | static void |
959 | periodics_reschedule (EV_P) |
1028 | periodics_reschedule (EV_P) |
… | |
… | |
963 | /* adjust periodics after time jump */ |
1032 | /* adjust periodics after time jump */ |
964 | for (i = 0; i < periodiccnt; ++i) |
1033 | for (i = 0; i < periodiccnt; ++i) |
965 | { |
1034 | { |
966 | struct ev_periodic *w = periodics [i]; |
1035 | struct ev_periodic *w = periodics [i]; |
967 | |
1036 | |
|
|
1037 | if (w->reschedule_cb) |
|
|
1038 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
968 | if (w->interval) |
1039 | else if (w->interval) |
969 | { |
|
|
970 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1040 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
971 | |
|
|
972 | if (fabs (diff) >= 1e-4) |
|
|
973 | { |
|
|
974 | ev_periodic_stop (EV_A_ w); |
|
|
975 | ev_periodic_start (EV_A_ w); |
|
|
976 | |
|
|
977 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
978 | } |
|
|
979 | } |
|
|
980 | } |
1041 | } |
|
|
1042 | |
|
|
1043 | /* now rebuild the heap */ |
|
|
1044 | for (i = periodiccnt >> 1; i--; ) |
|
|
1045 | downheap ((WT *)periodics, periodiccnt, i); |
981 | } |
1046 | } |
982 | |
1047 | |
983 | inline int |
1048 | inline int |
984 | time_update_monotonic (EV_P) |
1049 | time_update_monotonic (EV_P) |
985 | { |
1050 | { |
986 | mn_now = get_clock (); |
1051 | mn_now = get_clock (); |
987 | |
1052 | |
988 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1053 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
989 | { |
1054 | { |
990 | rt_now = rtmn_diff + mn_now; |
1055 | ev_rt_now = rtmn_diff + mn_now; |
991 | return 0; |
1056 | return 0; |
992 | } |
1057 | } |
993 | else |
1058 | else |
994 | { |
1059 | { |
995 | now_floor = mn_now; |
1060 | now_floor = mn_now; |
996 | rt_now = ev_time (); |
1061 | ev_rt_now = ev_time (); |
997 | return 1; |
1062 | return 1; |
998 | } |
1063 | } |
999 | } |
1064 | } |
1000 | |
1065 | |
1001 | static void |
1066 | static void |
… | |
… | |
1010 | { |
1075 | { |
1011 | ev_tstamp odiff = rtmn_diff; |
1076 | ev_tstamp odiff = rtmn_diff; |
1012 | |
1077 | |
1013 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1078 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1014 | { |
1079 | { |
1015 | rtmn_diff = rt_now - mn_now; |
1080 | rtmn_diff = ev_rt_now - mn_now; |
1016 | |
1081 | |
1017 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1082 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1018 | return; /* all is well */ |
1083 | return; /* all is well */ |
1019 | |
1084 | |
1020 | rt_now = ev_time (); |
1085 | ev_rt_now = ev_time (); |
1021 | mn_now = get_clock (); |
1086 | mn_now = get_clock (); |
1022 | now_floor = mn_now; |
1087 | now_floor = mn_now; |
1023 | } |
1088 | } |
1024 | |
1089 | |
1025 | periodics_reschedule (EV_A); |
1090 | periodics_reschedule (EV_A); |
… | |
… | |
1028 | } |
1093 | } |
1029 | } |
1094 | } |
1030 | else |
1095 | else |
1031 | #endif |
1096 | #endif |
1032 | { |
1097 | { |
1033 | rt_now = ev_time (); |
1098 | ev_rt_now = ev_time (); |
1034 | |
1099 | |
1035 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1100 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1036 | { |
1101 | { |
1037 | periodics_reschedule (EV_A); |
1102 | periodics_reschedule (EV_A); |
1038 | |
1103 | |
1039 | /* adjust timers. this is easy, as the offset is the same for all */ |
1104 | /* adjust timers. this is easy, as the offset is the same for all */ |
1040 | for (i = 0; i < timercnt; ++i) |
1105 | for (i = 0; i < timercnt; ++i) |
1041 | ((WT)timers [i])->at += rt_now - mn_now; |
1106 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1042 | } |
1107 | } |
1043 | |
1108 | |
1044 | mn_now = rt_now; |
1109 | mn_now = ev_rt_now; |
1045 | } |
1110 | } |
1046 | } |
1111 | } |
1047 | |
1112 | |
1048 | void |
1113 | void |
1049 | ev_ref (EV_P) |
1114 | ev_ref (EV_P) |
… | |
… | |
1081 | /* update fd-related kernel structures */ |
1146 | /* update fd-related kernel structures */ |
1082 | fd_reify (EV_A); |
1147 | fd_reify (EV_A); |
1083 | |
1148 | |
1084 | /* calculate blocking time */ |
1149 | /* calculate blocking time */ |
1085 | |
1150 | |
1086 | /* we only need this for !monotonic clockor timers, but as we basically |
1151 | /* we only need this for !monotonic clock or timers, but as we basically |
1087 | always have timers, we just calculate it always */ |
1152 | always have timers, we just calculate it always */ |
1088 | #if EV_USE_MONOTONIC |
1153 | #if EV_USE_MONOTONIC |
1089 | if (expect_true (have_monotonic)) |
1154 | if (expect_true (have_monotonic)) |
1090 | time_update_monotonic (EV_A); |
1155 | time_update_monotonic (EV_A); |
1091 | else |
1156 | else |
1092 | #endif |
1157 | #endif |
1093 | { |
1158 | { |
1094 | rt_now = ev_time (); |
1159 | ev_rt_now = ev_time (); |
1095 | mn_now = rt_now; |
1160 | mn_now = ev_rt_now; |
1096 | } |
1161 | } |
1097 | |
1162 | |
1098 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1163 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1099 | block = 0.; |
1164 | block = 0.; |
1100 | else |
1165 | else |
… | |
… | |
1107 | if (block > to) block = to; |
1172 | if (block > to) block = to; |
1108 | } |
1173 | } |
1109 | |
1174 | |
1110 | if (periodiccnt) |
1175 | if (periodiccnt) |
1111 | { |
1176 | { |
1112 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
1177 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1113 | if (block > to) block = to; |
1178 | if (block > to) block = to; |
1114 | } |
1179 | } |
1115 | |
1180 | |
1116 | if (block < 0.) block = 0.; |
1181 | if (block < 0.) block = 0.; |
1117 | } |
1182 | } |
1118 | |
1183 | |
1119 | method_poll (EV_A_ block); |
1184 | method_poll (EV_A_ block); |
1120 | |
1185 | |
1121 | /* update rt_now, do magic */ |
1186 | /* update ev_rt_now, do magic */ |
1122 | time_update (EV_A); |
1187 | time_update (EV_A); |
1123 | |
1188 | |
1124 | /* queue pending timers and reschedule them */ |
1189 | /* queue pending timers and reschedule them */ |
1125 | timers_reify (EV_A); /* relative timers called last */ |
1190 | timers_reify (EV_A); /* relative timers called last */ |
1126 | periodics_reify (EV_A); /* absolute timers called first */ |
1191 | periodics_reify (EV_A); /* absolute timers called first */ |
1127 | |
1192 | |
1128 | /* queue idle watchers unless io or timers are pending */ |
1193 | /* queue idle watchers unless io or timers are pending */ |
1129 | if (!pendingcnt) |
1194 | if (idlecnt && !any_pending (EV_A)) |
1130 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1195 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1131 | |
1196 | |
1132 | /* queue check watchers, to be executed first */ |
1197 | /* queue check watchers, to be executed first */ |
1133 | if (checkcnt) |
1198 | if (checkcnt) |
1134 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1199 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
… | |
… | |
1272 | ev_timer_again (EV_P_ struct ev_timer *w) |
1337 | ev_timer_again (EV_P_ struct ev_timer *w) |
1273 | { |
1338 | { |
1274 | if (ev_is_active (w)) |
1339 | if (ev_is_active (w)) |
1275 | { |
1340 | { |
1276 | if (w->repeat) |
1341 | if (w->repeat) |
1277 | { |
|
|
1278 | ((WT)w)->at = mn_now + w->repeat; |
|
|
1279 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1342 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); |
1280 | } |
|
|
1281 | else |
1343 | else |
1282 | ev_timer_stop (EV_A_ w); |
1344 | ev_timer_stop (EV_A_ w); |
1283 | } |
1345 | } |
1284 | else if (w->repeat) |
1346 | else if (w->repeat) |
1285 | ev_timer_start (EV_A_ w); |
1347 | ev_timer_start (EV_A_ w); |
… | |
… | |
1289 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1351 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1290 | { |
1352 | { |
1291 | if (ev_is_active (w)) |
1353 | if (ev_is_active (w)) |
1292 | return; |
1354 | return; |
1293 | |
1355 | |
|
|
1356 | if (w->reschedule_cb) |
|
|
1357 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
|
|
1358 | else if (w->interval) |
|
|
1359 | { |
1294 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1360 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1295 | |
|
|
1296 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1361 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1297 | if (w->interval) |
|
|
1298 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1362 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
|
|
1363 | } |
1299 | |
1364 | |
1300 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1365 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1301 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1366 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1302 | periodics [periodiccnt - 1] = w; |
1367 | periodics [periodiccnt - 1] = w; |
1303 | upheap ((WT *)periodics, periodiccnt - 1); |
1368 | upheap ((WT *)periodics, periodiccnt - 1); |
… | |
… | |
1319 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1384 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1320 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1385 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1321 | } |
1386 | } |
1322 | |
1387 | |
1323 | ev_stop (EV_A_ (W)w); |
1388 | ev_stop (EV_A_ (W)w); |
|
|
1389 | } |
|
|
1390 | |
|
|
1391 | void |
|
|
1392 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
|
|
1393 | { |
|
|
1394 | /* TODO: use adjustheap and recalculation */ |
|
|
1395 | ev_periodic_stop (EV_A_ w); |
|
|
1396 | ev_periodic_start (EV_A_ w); |
1324 | } |
1397 | } |
1325 | |
1398 | |
1326 | void |
1399 | void |
1327 | ev_idle_start (EV_P_ struct ev_idle *w) |
1400 | ev_idle_start (EV_P_ struct ev_idle *w) |
1328 | { |
1401 | { |
… | |
… | |
1505 | else |
1578 | else |
1506 | { |
1579 | { |
1507 | once->cb = cb; |
1580 | once->cb = cb; |
1508 | once->arg = arg; |
1581 | once->arg = arg; |
1509 | |
1582 | |
1510 | ev_watcher_init (&once->io, once_cb_io); |
1583 | ev_init (&once->io, once_cb_io); |
1511 | if (fd >= 0) |
1584 | if (fd >= 0) |
1512 | { |
1585 | { |
1513 | ev_io_set (&once->io, fd, events); |
1586 | ev_io_set (&once->io, fd, events); |
1514 | ev_io_start (EV_A_ &once->io); |
1587 | ev_io_start (EV_A_ &once->io); |
1515 | } |
1588 | } |
1516 | |
1589 | |
1517 | ev_watcher_init (&once->to, once_cb_to); |
1590 | ev_init (&once->to, once_cb_to); |
1518 | if (timeout >= 0.) |
1591 | if (timeout >= 0.) |
1519 | { |
1592 | { |
1520 | ev_timer_set (&once->to, timeout, 0.); |
1593 | ev_timer_set (&once->to, timeout, 0.); |
1521 | ev_timer_start (EV_A_ &once->to); |
1594 | ev_timer_start (EV_A_ &once->to); |
1522 | } |
1595 | } |