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