… | |
… | |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
30 | */ |
|
|
31 | |
|
|
32 | #ifdef __cplusplus |
|
|
33 | extern "C" { |
|
|
34 | #endif |
|
|
35 | |
31 | #ifndef EV_STANDALONE |
36 | #ifndef EV_STANDALONE |
32 | # include "config.h" |
37 | # include "config.h" |
33 | |
38 | |
34 | # if HAVE_CLOCK_GETTIME |
39 | # if HAVE_CLOCK_GETTIME |
35 | # define EV_USE_MONOTONIC 1 |
40 | # define EV_USE_MONOTONIC 1 |
… | |
… | |
46 | |
51 | |
47 | # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
52 | # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
48 | # define EV_USE_EPOLL 1 |
53 | # define EV_USE_EPOLL 1 |
49 | # endif |
54 | # endif |
50 | |
55 | |
51 | # if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H |
56 | # if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H |
52 | # define EV_USE_KQUEUE 1 |
57 | # define EV_USE_KQUEUE 1 |
53 | # endif |
58 | # endif |
54 | |
59 | |
55 | #endif |
60 | #endif |
56 | |
61 | |
… | |
… | |
126 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
131 | #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) */ |
132 | #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 */ |
133 | #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 */ |
134 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
130 | |
135 | |
|
|
136 | #ifdef EV_H |
|
|
137 | # include EV_H |
|
|
138 | #else |
131 | #include "ev.h" |
139 | # include "ev.h" |
|
|
140 | #endif |
132 | |
141 | |
133 | #if __GNUC__ >= 3 |
142 | #if __GNUC__ >= 3 |
134 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
143 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
135 | # define inline inline |
144 | # define inline inline |
136 | #else |
145 | #else |
… | |
… | |
215 | int events; |
224 | int events; |
216 | } ANPENDING; |
225 | } ANPENDING; |
217 | |
226 | |
218 | #if EV_MULTIPLICITY |
227 | #if EV_MULTIPLICITY |
219 | |
228 | |
220 | struct ev_loop |
229 | struct ev_loop |
221 | { |
230 | { |
|
|
231 | ev_tstamp ev_rt_now; |
222 | # define VAR(name,decl) decl; |
232 | #define VAR(name,decl) decl; |
223 | # include "ev_vars.h" |
233 | #include "ev_vars.h" |
224 | }; |
|
|
225 | # undef VAR |
234 | #undef VAR |
|
|
235 | }; |
226 | # include "ev_wrap.h" |
236 | #include "ev_wrap.h" |
|
|
237 | |
|
|
238 | struct ev_loop default_loop_struct; |
|
|
239 | static struct ev_loop *default_loop; |
227 | |
240 | |
228 | #else |
241 | #else |
229 | |
242 | |
|
|
243 | ev_tstamp ev_rt_now; |
230 | # define VAR(name,decl) static decl; |
244 | #define VAR(name,decl) static decl; |
231 | # include "ev_vars.h" |
245 | #include "ev_vars.h" |
232 | # undef VAR |
246 | #undef VAR |
|
|
247 | |
|
|
248 | static int default_loop; |
233 | |
249 | |
234 | #endif |
250 | #endif |
235 | |
251 | |
236 | /*****************************************************************************/ |
252 | /*****************************************************************************/ |
237 | |
253 | |
… | |
… | |
262 | #endif |
278 | #endif |
263 | |
279 | |
264 | return ev_time (); |
280 | return ev_time (); |
265 | } |
281 | } |
266 | |
282 | |
|
|
283 | #if EV_MULTIPLICITY |
267 | ev_tstamp |
284 | ev_tstamp |
268 | ev_now (EV_P) |
285 | ev_now (EV_P) |
269 | { |
286 | { |
270 | return rt_now; |
287 | return ev_rt_now; |
271 | } |
288 | } |
|
|
289 | #endif |
272 | |
290 | |
273 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
291 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
274 | |
292 | |
275 | #define array_needsize(type,base,cur,cnt,init) \ |
293 | #define array_needsize(type,base,cur,cnt,init) \ |
276 | if (expect_false ((cnt) > cur)) \ |
294 | if (expect_false ((cnt) > cur)) \ |
… | |
… | |
316 | |
334 | |
317 | ++base; |
335 | ++base; |
318 | } |
336 | } |
319 | } |
337 | } |
320 | |
338 | |
321 | static void |
339 | void |
322 | event (EV_P_ W w, int events) |
340 | ev_feed_event (EV_P_ void *w, int revents) |
323 | { |
341 | { |
|
|
342 | W w_ = (W)w; |
|
|
343 | |
324 | if (w->pending) |
344 | if (w_->pending) |
325 | { |
345 | { |
326 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
346 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
327 | return; |
347 | return; |
328 | } |
348 | } |
329 | |
349 | |
330 | w->pending = ++pendingcnt [ABSPRI (w)]; |
350 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
331 | array_needsize (ANPENDING, pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); |
351 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); |
332 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
352 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
333 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
353 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
334 | } |
354 | } |
335 | |
355 | |
336 | static void |
356 | static void |
337 | queue_events (EV_P_ W *events, int eventcnt, int type) |
357 | queue_events (EV_P_ W *events, int eventcnt, int type) |
338 | { |
358 | { |
339 | int i; |
359 | int i; |
340 | |
360 | |
341 | for (i = 0; i < eventcnt; ++i) |
361 | for (i = 0; i < eventcnt; ++i) |
342 | event (EV_A_ events [i], type); |
362 | ev_feed_event (EV_A_ events [i], type); |
343 | } |
363 | } |
344 | |
364 | |
345 | static void |
365 | inline void |
346 | fd_event (EV_P_ int fd, int events) |
366 | fd_event (EV_P_ int fd, int revents) |
347 | { |
367 | { |
348 | ANFD *anfd = anfds + fd; |
368 | ANFD *anfd = anfds + fd; |
349 | struct ev_io *w; |
369 | struct ev_io *w; |
350 | |
370 | |
351 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
371 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
352 | { |
372 | { |
353 | int ev = w->events & events; |
373 | int ev = w->events & revents; |
354 | |
374 | |
355 | if (ev) |
375 | if (ev) |
356 | event (EV_A_ (W)w, ev); |
376 | ev_feed_event (EV_A_ (W)w, ev); |
357 | } |
377 | } |
|
|
378 | } |
|
|
379 | |
|
|
380 | void |
|
|
381 | ev_feed_fd_event (EV_P_ int fd, int revents) |
|
|
382 | { |
|
|
383 | fd_event (EV_A_ fd, revents); |
358 | } |
384 | } |
359 | |
385 | |
360 | /*****************************************************************************/ |
386 | /*****************************************************************************/ |
361 | |
387 | |
362 | static void |
388 | static void |
… | |
… | |
403 | struct ev_io *w; |
429 | struct ev_io *w; |
404 | |
430 | |
405 | while ((w = (struct ev_io *)anfds [fd].head)) |
431 | while ((w = (struct ev_io *)anfds [fd].head)) |
406 | { |
432 | { |
407 | ev_io_stop (EV_A_ w); |
433 | ev_io_stop (EV_A_ w); |
408 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
434 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
409 | } |
435 | } |
410 | } |
436 | } |
411 | |
437 | |
412 | static int |
438 | static int |
413 | fd_valid (int fd) |
439 | fd_valid (int fd) |
… | |
… | |
501 | |
527 | |
502 | heap [k] = w; |
528 | heap [k] = w; |
503 | ((W)heap [k])->active = k + 1; |
529 | ((W)heap [k])->active = k + 1; |
504 | } |
530 | } |
505 | |
531 | |
|
|
532 | inline void |
|
|
533 | adjustheap (WT *heap, int N, int k, ev_tstamp at) |
|
|
534 | { |
|
|
535 | ev_tstamp old_at = heap [k]->at; |
|
|
536 | heap [k]->at = at; |
|
|
537 | |
|
|
538 | if (old_at < at) |
|
|
539 | downheap (heap, N, k); |
|
|
540 | else |
|
|
541 | upheap (heap, k); |
|
|
542 | } |
|
|
543 | |
506 | /*****************************************************************************/ |
544 | /*****************************************************************************/ |
507 | |
545 | |
508 | typedef struct |
546 | typedef struct |
509 | { |
547 | { |
510 | WL head; |
548 | WL head; |
… | |
… | |
550 | #endif |
588 | #endif |
551 | errno = old_errno; |
589 | errno = old_errno; |
552 | } |
590 | } |
553 | } |
591 | } |
554 | |
592 | |
|
|
593 | void |
|
|
594 | ev_feed_signal_event (EV_P_ int signum) |
|
|
595 | { |
|
|
596 | WL w; |
|
|
597 | |
|
|
598 | #if EV_MULTIPLICITY |
|
|
599 | assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
|
|
600 | #endif |
|
|
601 | |
|
|
602 | --signum; |
|
|
603 | |
|
|
604 | if (signum < 0 || signum >= signalmax) |
|
|
605 | return; |
|
|
606 | |
|
|
607 | signals [signum].gotsig = 0; |
|
|
608 | |
|
|
609 | for (w = signals [signum].head; w; w = w->next) |
|
|
610 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
|
|
611 | } |
|
|
612 | |
555 | static void |
613 | static void |
556 | sigcb (EV_P_ struct ev_io *iow, int revents) |
614 | sigcb (EV_P_ struct ev_io *iow, int revents) |
557 | { |
615 | { |
558 | WL w; |
|
|
559 | int signum; |
616 | int signum; |
560 | |
617 | |
561 | #ifdef WIN32 |
618 | #ifdef WIN32 |
562 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
619 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
563 | #else |
620 | #else |
… | |
… | |
565 | #endif |
622 | #endif |
566 | gotsig = 0; |
623 | gotsig = 0; |
567 | |
624 | |
568 | for (signum = signalmax; signum--; ) |
625 | for (signum = signalmax; signum--; ) |
569 | if (signals [signum].gotsig) |
626 | if (signals [signum].gotsig) |
570 | { |
627 | 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 | } |
628 | } |
577 | |
629 | |
578 | static void |
630 | static void |
579 | siginit (EV_P) |
631 | siginit (EV_P) |
580 | { |
632 | { |
… | |
… | |
613 | if (w->pid == pid || !w->pid) |
665 | if (w->pid == pid || !w->pid) |
614 | { |
666 | { |
615 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
667 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
616 | w->rpid = pid; |
668 | w->rpid = pid; |
617 | w->rstatus = status; |
669 | w->rstatus = status; |
618 | event (EV_A_ (W)w, EV_CHILD); |
670 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
619 | } |
671 | } |
620 | } |
672 | } |
621 | |
673 | |
622 | static void |
674 | static void |
623 | childcb (EV_P_ struct ev_signal *sw, int revents) |
675 | childcb (EV_P_ struct ev_signal *sw, int revents) |
… | |
… | |
625 | int pid, status; |
677 | int pid, status; |
626 | |
678 | |
627 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
679 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
628 | { |
680 | { |
629 | /* make sure we are called again until all childs have been reaped */ |
681 | /* make sure we are called again until all childs have been reaped */ |
630 | event (EV_A_ (W)sw, EV_SIGNAL); |
682 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
631 | |
683 | |
632 | child_reap (EV_A_ sw, pid, pid, status); |
684 | 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 */ |
685 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
634 | } |
686 | } |
635 | } |
687 | } |
… | |
… | |
692 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
744 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
693 | have_monotonic = 1; |
745 | have_monotonic = 1; |
694 | } |
746 | } |
695 | #endif |
747 | #endif |
696 | |
748 | |
697 | rt_now = ev_time (); |
749 | ev_rt_now = ev_time (); |
698 | mn_now = get_clock (); |
750 | mn_now = get_clock (); |
699 | now_floor = mn_now; |
751 | now_floor = mn_now; |
700 | rtmn_diff = rt_now - mn_now; |
752 | rtmn_diff = ev_rt_now - mn_now; |
701 | |
753 | |
702 | if (methods == EVMETHOD_AUTO) |
754 | if (methods == EVMETHOD_AUTO) |
703 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
755 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
704 | methods = atoi (getenv ("LIBEV_METHODS")); |
756 | methods = atoi (getenv ("LIBEV_METHODS")); |
705 | else |
757 | else |
… | |
… | |
720 | #endif |
772 | #endif |
721 | #if EV_USE_SELECT |
773 | #if EV_USE_SELECT |
722 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
774 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
723 | #endif |
775 | #endif |
724 | |
776 | |
725 | ev_watcher_init (&sigev, sigcb); |
777 | ev_init (&sigev, sigcb); |
726 | ev_set_priority (&sigev, EV_MAXPRI); |
778 | ev_set_priority (&sigev, EV_MAXPRI); |
727 | } |
779 | } |
728 | } |
780 | } |
729 | |
781 | |
730 | void |
782 | void |
… | |
… | |
820 | } |
872 | } |
821 | |
873 | |
822 | #endif |
874 | #endif |
823 | |
875 | |
824 | #if EV_MULTIPLICITY |
876 | #if EV_MULTIPLICITY |
825 | struct ev_loop default_loop_struct; |
|
|
826 | static struct ev_loop *default_loop; |
|
|
827 | |
|
|
828 | struct ev_loop * |
877 | struct ev_loop * |
829 | #else |
878 | #else |
830 | static int default_loop; |
|
|
831 | |
|
|
832 | int |
879 | int |
833 | #endif |
880 | #endif |
834 | ev_default_loop (int methods) |
881 | ev_default_loop (int methods) |
835 | { |
882 | { |
836 | if (sigpipe [0] == sigpipe [1]) |
883 | if (sigpipe [0] == sigpipe [1]) |
… | |
… | |
922 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
969 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
923 | |
970 | |
924 | if (p->w) |
971 | if (p->w) |
925 | { |
972 | { |
926 | p->w->pending = 0; |
973 | p->w->pending = 0; |
927 | p->w->cb (EV_A_ p->w, p->events); |
974 | EV_CB_INVOKE (p->w, p->events); |
928 | } |
975 | } |
929 | } |
976 | } |
930 | } |
977 | } |
931 | |
978 | |
932 | static void |
979 | static void |
… | |
… | |
946 | downheap ((WT *)timers, timercnt, 0); |
993 | downheap ((WT *)timers, timercnt, 0); |
947 | } |
994 | } |
948 | else |
995 | else |
949 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
996 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
950 | |
997 | |
951 | event (EV_A_ (W)w, EV_TIMEOUT); |
998 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
952 | } |
999 | } |
953 | } |
1000 | } |
954 | |
1001 | |
955 | static void |
1002 | static void |
956 | periodics_reify (EV_P) |
1003 | periodics_reify (EV_P) |
957 | { |
1004 | { |
958 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
1005 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
959 | { |
1006 | { |
960 | struct ev_periodic *w = periodics [0]; |
1007 | struct ev_periodic *w = periodics [0]; |
961 | |
1008 | |
962 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1009 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
963 | |
1010 | |
964 | /* first reschedule or stop timer */ |
1011 | /* first reschedule or stop timer */ |
965 | if (w->reschedule_cb) |
1012 | if (w->reschedule_cb) |
966 | { |
1013 | { |
967 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); |
1014 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
968 | |
1015 | |
969 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); |
1016 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
970 | downheap ((WT *)periodics, periodiccnt, 0); |
1017 | downheap ((WT *)periodics, periodiccnt, 0); |
971 | } |
1018 | } |
972 | else if (w->interval) |
1019 | else if (w->interval) |
973 | { |
1020 | { |
974 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1021 | ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
975 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
1022 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); |
976 | downheap ((WT *)periodics, periodiccnt, 0); |
1023 | downheap ((WT *)periodics, periodiccnt, 0); |
977 | } |
1024 | } |
978 | else |
1025 | else |
979 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1026 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
980 | |
1027 | |
981 | event (EV_A_ (W)w, EV_PERIODIC); |
1028 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
982 | } |
1029 | } |
983 | } |
1030 | } |
984 | |
1031 | |
985 | static void |
1032 | static void |
986 | periodics_reschedule (EV_P) |
1033 | periodics_reschedule (EV_P) |
… | |
… | |
991 | for (i = 0; i < periodiccnt; ++i) |
1038 | for (i = 0; i < periodiccnt; ++i) |
992 | { |
1039 | { |
993 | struct ev_periodic *w = periodics [i]; |
1040 | struct ev_periodic *w = periodics [i]; |
994 | |
1041 | |
995 | if (w->reschedule_cb) |
1042 | if (w->reschedule_cb) |
996 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
1043 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
997 | else if (w->interval) |
1044 | else if (w->interval) |
998 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1045 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
999 | } |
1046 | } |
1000 | |
1047 | |
1001 | /* now rebuild the heap */ |
1048 | /* now rebuild the heap */ |
1002 | for (i = periodiccnt >> 1; i--; ) |
1049 | for (i = periodiccnt >> 1; i--; ) |
1003 | downheap ((WT *)periodics, periodiccnt, i); |
1050 | downheap ((WT *)periodics, periodiccnt, i); |
… | |
… | |
1008 | { |
1055 | { |
1009 | mn_now = get_clock (); |
1056 | mn_now = get_clock (); |
1010 | |
1057 | |
1011 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1058 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1012 | { |
1059 | { |
1013 | rt_now = rtmn_diff + mn_now; |
1060 | ev_rt_now = rtmn_diff + mn_now; |
1014 | return 0; |
1061 | return 0; |
1015 | } |
1062 | } |
1016 | else |
1063 | else |
1017 | { |
1064 | { |
1018 | now_floor = mn_now; |
1065 | now_floor = mn_now; |
1019 | rt_now = ev_time (); |
1066 | ev_rt_now = ev_time (); |
1020 | return 1; |
1067 | return 1; |
1021 | } |
1068 | } |
1022 | } |
1069 | } |
1023 | |
1070 | |
1024 | static void |
1071 | static void |
… | |
… | |
1033 | { |
1080 | { |
1034 | ev_tstamp odiff = rtmn_diff; |
1081 | ev_tstamp odiff = rtmn_diff; |
1035 | |
1082 | |
1036 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1083 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1037 | { |
1084 | { |
1038 | rtmn_diff = rt_now - mn_now; |
1085 | rtmn_diff = ev_rt_now - mn_now; |
1039 | |
1086 | |
1040 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1087 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1041 | return; /* all is well */ |
1088 | return; /* all is well */ |
1042 | |
1089 | |
1043 | rt_now = ev_time (); |
1090 | ev_rt_now = ev_time (); |
1044 | mn_now = get_clock (); |
1091 | mn_now = get_clock (); |
1045 | now_floor = mn_now; |
1092 | now_floor = mn_now; |
1046 | } |
1093 | } |
1047 | |
1094 | |
1048 | periodics_reschedule (EV_A); |
1095 | periodics_reschedule (EV_A); |
… | |
… | |
1051 | } |
1098 | } |
1052 | } |
1099 | } |
1053 | else |
1100 | else |
1054 | #endif |
1101 | #endif |
1055 | { |
1102 | { |
1056 | rt_now = ev_time (); |
1103 | ev_rt_now = ev_time (); |
1057 | |
1104 | |
1058 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1105 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1059 | { |
1106 | { |
1060 | periodics_reschedule (EV_A); |
1107 | periodics_reschedule (EV_A); |
1061 | |
1108 | |
1062 | /* adjust timers. this is easy, as the offset is the same for all */ |
1109 | /* adjust timers. this is easy, as the offset is the same for all */ |
1063 | for (i = 0; i < timercnt; ++i) |
1110 | for (i = 0; i < timercnt; ++i) |
1064 | ((WT)timers [i])->at += rt_now - mn_now; |
1111 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1065 | } |
1112 | } |
1066 | |
1113 | |
1067 | mn_now = rt_now; |
1114 | mn_now = ev_rt_now; |
1068 | } |
1115 | } |
1069 | } |
1116 | } |
1070 | |
1117 | |
1071 | void |
1118 | void |
1072 | ev_ref (EV_P) |
1119 | ev_ref (EV_P) |
… | |
… | |
1112 | if (expect_true (have_monotonic)) |
1159 | if (expect_true (have_monotonic)) |
1113 | time_update_monotonic (EV_A); |
1160 | time_update_monotonic (EV_A); |
1114 | else |
1161 | else |
1115 | #endif |
1162 | #endif |
1116 | { |
1163 | { |
1117 | rt_now = ev_time (); |
1164 | ev_rt_now = ev_time (); |
1118 | mn_now = rt_now; |
1165 | mn_now = ev_rt_now; |
1119 | } |
1166 | } |
1120 | |
1167 | |
1121 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1168 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1122 | block = 0.; |
1169 | block = 0.; |
1123 | else |
1170 | else |
… | |
… | |
1130 | if (block > to) block = to; |
1177 | if (block > to) block = to; |
1131 | } |
1178 | } |
1132 | |
1179 | |
1133 | if (periodiccnt) |
1180 | if (periodiccnt) |
1134 | { |
1181 | { |
1135 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
1182 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1136 | if (block > to) block = to; |
1183 | if (block > to) block = to; |
1137 | } |
1184 | } |
1138 | |
1185 | |
1139 | if (block < 0.) block = 0.; |
1186 | if (block < 0.) block = 0.; |
1140 | } |
1187 | } |
1141 | |
1188 | |
1142 | method_poll (EV_A_ block); |
1189 | method_poll (EV_A_ block); |
1143 | |
1190 | |
1144 | /* update rt_now, do magic */ |
1191 | /* update ev_rt_now, do magic */ |
1145 | time_update (EV_A); |
1192 | time_update (EV_A); |
1146 | |
1193 | |
1147 | /* queue pending timers and reschedule them */ |
1194 | /* queue pending timers and reschedule them */ |
1148 | timers_reify (EV_A); /* relative timers called last */ |
1195 | timers_reify (EV_A); /* relative timers called last */ |
1149 | periodics_reify (EV_A); /* absolute timers called first */ |
1196 | periodics_reify (EV_A); /* absolute timers called first */ |
… | |
… | |
1245 | { |
1292 | { |
1246 | ev_clear_pending (EV_A_ (W)w); |
1293 | ev_clear_pending (EV_A_ (W)w); |
1247 | if (!ev_is_active (w)) |
1294 | if (!ev_is_active (w)) |
1248 | return; |
1295 | return; |
1249 | |
1296 | |
|
|
1297 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
|
|
1298 | |
1250 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1299 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1251 | ev_stop (EV_A_ (W)w); |
1300 | ev_stop (EV_A_ (W)w); |
1252 | |
1301 | |
1253 | fd_change (EV_A_ w->fd); |
1302 | fd_change (EV_A_ w->fd); |
1254 | } |
1303 | } |
… | |
… | |
1295 | ev_timer_again (EV_P_ struct ev_timer *w) |
1344 | ev_timer_again (EV_P_ struct ev_timer *w) |
1296 | { |
1345 | { |
1297 | if (ev_is_active (w)) |
1346 | if (ev_is_active (w)) |
1298 | { |
1347 | { |
1299 | if (w->repeat) |
1348 | if (w->repeat) |
1300 | { |
|
|
1301 | ((WT)w)->at = mn_now + w->repeat; |
|
|
1302 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1349 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); |
1303 | } |
|
|
1304 | else |
1350 | else |
1305 | ev_timer_stop (EV_A_ w); |
1351 | ev_timer_stop (EV_A_ w); |
1306 | } |
1352 | } |
1307 | else if (w->repeat) |
1353 | else if (w->repeat) |
1308 | ev_timer_start (EV_A_ w); |
1354 | ev_timer_start (EV_A_ w); |
… | |
… | |
1313 | { |
1359 | { |
1314 | if (ev_is_active (w)) |
1360 | if (ev_is_active (w)) |
1315 | return; |
1361 | return; |
1316 | |
1362 | |
1317 | if (w->reschedule_cb) |
1363 | if (w->reschedule_cb) |
1318 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
1364 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1319 | else if (w->interval) |
1365 | else if (w->interval) |
1320 | { |
1366 | { |
1321 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1367 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1322 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1368 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1323 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1369 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1324 | } |
1370 | } |
1325 | |
1371 | |
1326 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1372 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1327 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1373 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1328 | periodics [periodiccnt - 1] = w; |
1374 | periodics [periodiccnt - 1] = w; |
… | |
… | |
1350 | } |
1396 | } |
1351 | |
1397 | |
1352 | void |
1398 | void |
1353 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
1399 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
1354 | { |
1400 | { |
|
|
1401 | /* TODO: use adjustheap and recalculation */ |
1355 | ev_periodic_stop (EV_A_ w); |
1402 | ev_periodic_stop (EV_A_ w); |
1356 | ev_periodic_start (EV_A_ w); |
1403 | ev_periodic_start (EV_A_ w); |
1357 | } |
1404 | } |
1358 | |
1405 | |
1359 | void |
1406 | void |
… | |
… | |
1538 | else |
1585 | else |
1539 | { |
1586 | { |
1540 | once->cb = cb; |
1587 | once->cb = cb; |
1541 | once->arg = arg; |
1588 | once->arg = arg; |
1542 | |
1589 | |
1543 | ev_watcher_init (&once->io, once_cb_io); |
1590 | ev_init (&once->io, once_cb_io); |
1544 | if (fd >= 0) |
1591 | if (fd >= 0) |
1545 | { |
1592 | { |
1546 | ev_io_set (&once->io, fd, events); |
1593 | ev_io_set (&once->io, fd, events); |
1547 | ev_io_start (EV_A_ &once->io); |
1594 | ev_io_start (EV_A_ &once->io); |
1548 | } |
1595 | } |
1549 | |
1596 | |
1550 | ev_watcher_init (&once->to, once_cb_to); |
1597 | ev_init (&once->to, once_cb_to); |
1551 | if (timeout >= 0.) |
1598 | if (timeout >= 0.) |
1552 | { |
1599 | { |
1553 | ev_timer_set (&once->to, timeout, 0.); |
1600 | ev_timer_set (&once->to, timeout, 0.); |
1554 | ev_timer_start (EV_A_ &once->to); |
1601 | ev_timer_start (EV_A_ &once->to); |
1555 | } |
1602 | } |
1556 | } |
1603 | } |
1557 | } |
1604 | } |
1558 | |
1605 | |
|
|
1606 | #ifdef __cplusplus |
|
|
1607 | } |
|
|
1608 | #endif |
|
|
1609 | |