… | |
… | |
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 | |
238 | inline ev_tstamp |
254 | ev_tstamp |
239 | ev_time (void) |
255 | ev_time (void) |
240 | { |
256 | { |
241 | #if EV_USE_REALTIME |
257 | #if EV_USE_REALTIME |
242 | struct timespec ts; |
258 | struct timespec ts; |
243 | clock_gettime (CLOCK_REALTIME, &ts); |
259 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
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; |
… | |
… | |
541 | |
579 | |
542 | if (!gotsig) |
580 | if (!gotsig) |
543 | { |
581 | { |
544 | int old_errno = errno; |
582 | int old_errno = errno; |
545 | gotsig = 1; |
583 | gotsig = 1; |
|
|
584 | #ifdef WIN32 |
|
|
585 | send (sigpipe [1], &signum, 1, MSG_DONTWAIT); |
|
|
586 | #else |
546 | write (sigpipe [1], &signum, 1); |
587 | write (sigpipe [1], &signum, 1); |
|
|
588 | #endif |
547 | errno = old_errno; |
589 | errno = old_errno; |
548 | } |
590 | } |
549 | } |
591 | } |
550 | |
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 | |
551 | static void |
613 | static void |
552 | sigcb (EV_P_ struct ev_io *iow, int revents) |
614 | sigcb (EV_P_ struct ev_io *iow, int revents) |
553 | { |
615 | { |
554 | WL w; |
|
|
555 | int signum; |
616 | int signum; |
556 | |
617 | |
|
|
618 | #ifdef WIN32 |
|
|
619 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
|
|
620 | #else |
557 | read (sigpipe [0], &revents, 1); |
621 | read (sigpipe [0], &revents, 1); |
|
|
622 | #endif |
558 | gotsig = 0; |
623 | gotsig = 0; |
559 | |
624 | |
560 | for (signum = signalmax; signum--; ) |
625 | for (signum = signalmax; signum--; ) |
561 | if (signals [signum].gotsig) |
626 | if (signals [signum].gotsig) |
562 | { |
627 | 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 | } |
628 | } |
569 | |
629 | |
570 | static void |
630 | static void |
571 | siginit (EV_P) |
631 | siginit (EV_P) |
572 | { |
632 | { |
… | |
… | |
605 | if (w->pid == pid || !w->pid) |
665 | if (w->pid == pid || !w->pid) |
606 | { |
666 | { |
607 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
667 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
608 | w->rpid = pid; |
668 | w->rpid = pid; |
609 | w->rstatus = status; |
669 | w->rstatus = status; |
610 | event (EV_A_ (W)w, EV_CHILD); |
670 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
611 | } |
671 | } |
612 | } |
672 | } |
613 | |
673 | |
614 | static void |
674 | static void |
615 | childcb (EV_P_ struct ev_signal *sw, int revents) |
675 | childcb (EV_P_ struct ev_signal *sw, int revents) |
… | |
… | |
617 | int pid, status; |
677 | int pid, status; |
618 | |
678 | |
619 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
679 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
620 | { |
680 | { |
621 | /* 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 */ |
622 | event (EV_A_ (W)sw, EV_SIGNAL); |
682 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
623 | |
683 | |
624 | child_reap (EV_A_ sw, pid, pid, status); |
684 | 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 */ |
685 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
626 | } |
686 | } |
627 | } |
687 | } |
… | |
… | |
684 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
744 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
685 | have_monotonic = 1; |
745 | have_monotonic = 1; |
686 | } |
746 | } |
687 | #endif |
747 | #endif |
688 | |
748 | |
689 | rt_now = ev_time (); |
749 | ev_rt_now = ev_time (); |
690 | mn_now = get_clock (); |
750 | mn_now = get_clock (); |
691 | now_floor = mn_now; |
751 | now_floor = mn_now; |
692 | rtmn_diff = rt_now - mn_now; |
752 | rtmn_diff = ev_rt_now - mn_now; |
693 | |
753 | |
694 | if (methods == EVMETHOD_AUTO) |
754 | if (methods == EVMETHOD_AUTO) |
695 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
755 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
696 | methods = atoi (getenv ("LIBEV_METHODS")); |
756 | methods = atoi (getenv ("LIBEV_METHODS")); |
697 | else |
757 | else |
… | |
… | |
712 | #endif |
772 | #endif |
713 | #if EV_USE_SELECT |
773 | #if EV_USE_SELECT |
714 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
774 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
715 | #endif |
775 | #endif |
716 | |
776 | |
717 | ev_watcher_init (&sigev, sigcb); |
777 | ev_init (&sigev, sigcb); |
718 | ev_set_priority (&sigev, EV_MAXPRI); |
778 | ev_set_priority (&sigev, EV_MAXPRI); |
719 | } |
779 | } |
720 | } |
780 | } |
721 | |
781 | |
722 | void |
782 | void |
… | |
… | |
744 | array_free (pending, [i]); |
804 | array_free (pending, [i]); |
745 | |
805 | |
746 | /* have to use the microsoft-never-gets-it-right macro */ |
806 | /* have to use the microsoft-never-gets-it-right macro */ |
747 | array_free_microshit (fdchange); |
807 | array_free_microshit (fdchange); |
748 | array_free_microshit (timer); |
808 | array_free_microshit (timer); |
|
|
809 | #if EV_PERIODICS |
749 | array_free_microshit (periodic); |
810 | array_free_microshit (periodic); |
|
|
811 | #endif |
750 | array_free_microshit (idle); |
812 | array_free_microshit (idle); |
751 | array_free_microshit (prepare); |
813 | array_free_microshit (prepare); |
752 | array_free_microshit (check); |
814 | array_free_microshit (check); |
753 | |
815 | |
754 | method = 0; |
816 | method = 0; |
… | |
… | |
812 | } |
874 | } |
813 | |
875 | |
814 | #endif |
876 | #endif |
815 | |
877 | |
816 | #if EV_MULTIPLICITY |
878 | #if EV_MULTIPLICITY |
817 | struct ev_loop default_loop_struct; |
|
|
818 | static struct ev_loop *default_loop; |
|
|
819 | |
|
|
820 | struct ev_loop * |
879 | struct ev_loop * |
821 | #else |
880 | #else |
822 | static int default_loop; |
|
|
823 | |
|
|
824 | int |
881 | int |
825 | #endif |
882 | #endif |
826 | ev_default_loop (int methods) |
883 | ev_default_loop (int methods) |
827 | { |
884 | { |
828 | if (sigpipe [0] == sigpipe [1]) |
885 | if (sigpipe [0] == sigpipe [1]) |
… | |
… | |
889 | postfork = 1; |
946 | postfork = 1; |
890 | } |
947 | } |
891 | |
948 | |
892 | /*****************************************************************************/ |
949 | /*****************************************************************************/ |
893 | |
950 | |
|
|
951 | static int |
|
|
952 | any_pending (EV_P) |
|
|
953 | { |
|
|
954 | int pri; |
|
|
955 | |
|
|
956 | for (pri = NUMPRI; pri--; ) |
|
|
957 | if (pendingcnt [pri]) |
|
|
958 | return 1; |
|
|
959 | |
|
|
960 | return 0; |
|
|
961 | } |
|
|
962 | |
894 | static void |
963 | static void |
895 | call_pending (EV_P) |
964 | call_pending (EV_P) |
896 | { |
965 | { |
897 | int pri; |
966 | int pri; |
898 | |
967 | |
… | |
… | |
902 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
971 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
903 | |
972 | |
904 | if (p->w) |
973 | if (p->w) |
905 | { |
974 | { |
906 | p->w->pending = 0; |
975 | p->w->pending = 0; |
907 | p->w->cb (EV_A_ p->w, p->events); |
976 | EV_CB_INVOKE (p->w, p->events); |
908 | } |
977 | } |
909 | } |
978 | } |
910 | } |
979 | } |
911 | |
980 | |
912 | static void |
981 | static void |
… | |
… | |
920 | |
989 | |
921 | /* first reschedule or stop timer */ |
990 | /* first reschedule or stop timer */ |
922 | if (w->repeat) |
991 | if (w->repeat) |
923 | { |
992 | { |
924 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
993 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
|
|
994 | |
925 | ((WT)w)->at = mn_now + w->repeat; |
995 | ((WT)w)->at += w->repeat; |
|
|
996 | if (((WT)w)->at < mn_now) |
|
|
997 | ((WT)w)->at = mn_now; |
|
|
998 | |
926 | downheap ((WT *)timers, timercnt, 0); |
999 | downheap ((WT *)timers, timercnt, 0); |
927 | } |
1000 | } |
928 | else |
1001 | else |
929 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1002 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
930 | |
1003 | |
931 | event (EV_A_ (W)w, EV_TIMEOUT); |
1004 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
932 | } |
1005 | } |
933 | } |
1006 | } |
934 | |
1007 | |
|
|
1008 | #if EV_PERIODICS |
935 | static void |
1009 | static void |
936 | periodics_reify (EV_P) |
1010 | periodics_reify (EV_P) |
937 | { |
1011 | { |
938 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
1012 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
939 | { |
1013 | { |
940 | struct ev_periodic *w = periodics [0]; |
1014 | struct ev_periodic *w = periodics [0]; |
941 | |
1015 | |
942 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1016 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
943 | |
1017 | |
944 | /* first reschedule or stop timer */ |
1018 | /* first reschedule or stop timer */ |
945 | if (w->interval) |
1019 | if (w->reschedule_cb) |
946 | { |
1020 | { |
|
|
1021 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
|
|
1022 | |
|
|
1023 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
|
|
1024 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
1025 | } |
|
|
1026 | else if (w->interval) |
|
|
1027 | { |
947 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1028 | ((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)); |
1029 | 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); |
1030 | downheap ((WT *)periodics, periodiccnt, 0); |
950 | } |
1031 | } |
951 | else |
1032 | else |
952 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1033 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
953 | |
1034 | |
954 | event (EV_A_ (W)w, EV_PERIODIC); |
1035 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
955 | } |
1036 | } |
956 | } |
1037 | } |
957 | |
1038 | |
958 | static void |
1039 | static void |
959 | periodics_reschedule (EV_P) |
1040 | periodics_reschedule (EV_P) |
… | |
… | |
963 | /* adjust periodics after time jump */ |
1044 | /* adjust periodics after time jump */ |
964 | for (i = 0; i < periodiccnt; ++i) |
1045 | for (i = 0; i < periodiccnt; ++i) |
965 | { |
1046 | { |
966 | struct ev_periodic *w = periodics [i]; |
1047 | struct ev_periodic *w = periodics [i]; |
967 | |
1048 | |
|
|
1049 | if (w->reschedule_cb) |
|
|
1050 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
968 | if (w->interval) |
1051 | else if (w->interval) |
969 | { |
|
|
970 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1052 | ((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 | } |
1053 | } |
|
|
1054 | |
|
|
1055 | /* now rebuild the heap */ |
|
|
1056 | for (i = periodiccnt >> 1; i--; ) |
|
|
1057 | downheap ((WT *)periodics, periodiccnt, i); |
981 | } |
1058 | } |
|
|
1059 | #endif |
982 | |
1060 | |
983 | inline int |
1061 | inline int |
984 | time_update_monotonic (EV_P) |
1062 | time_update_monotonic (EV_P) |
985 | { |
1063 | { |
986 | mn_now = get_clock (); |
1064 | mn_now = get_clock (); |
987 | |
1065 | |
988 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1066 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
989 | { |
1067 | { |
990 | rt_now = rtmn_diff + mn_now; |
1068 | ev_rt_now = rtmn_diff + mn_now; |
991 | return 0; |
1069 | return 0; |
992 | } |
1070 | } |
993 | else |
1071 | else |
994 | { |
1072 | { |
995 | now_floor = mn_now; |
1073 | now_floor = mn_now; |
996 | rt_now = ev_time (); |
1074 | ev_rt_now = ev_time (); |
997 | return 1; |
1075 | return 1; |
998 | } |
1076 | } |
999 | } |
1077 | } |
1000 | |
1078 | |
1001 | static void |
1079 | static void |
… | |
… | |
1010 | { |
1088 | { |
1011 | ev_tstamp odiff = rtmn_diff; |
1089 | ev_tstamp odiff = rtmn_diff; |
1012 | |
1090 | |
1013 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1091 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1014 | { |
1092 | { |
1015 | rtmn_diff = rt_now - mn_now; |
1093 | rtmn_diff = ev_rt_now - mn_now; |
1016 | |
1094 | |
1017 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1095 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1018 | return; /* all is well */ |
1096 | return; /* all is well */ |
1019 | |
1097 | |
1020 | rt_now = ev_time (); |
1098 | ev_rt_now = ev_time (); |
1021 | mn_now = get_clock (); |
1099 | mn_now = get_clock (); |
1022 | now_floor = mn_now; |
1100 | now_floor = mn_now; |
1023 | } |
1101 | } |
1024 | |
1102 | |
|
|
1103 | # if EV_PERIODICS |
1025 | periodics_reschedule (EV_A); |
1104 | periodics_reschedule (EV_A); |
|
|
1105 | # endif |
1026 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1106 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1027 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1107 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1028 | } |
1108 | } |
1029 | } |
1109 | } |
1030 | else |
1110 | else |
1031 | #endif |
1111 | #endif |
1032 | { |
1112 | { |
1033 | rt_now = ev_time (); |
1113 | ev_rt_now = ev_time (); |
1034 | |
1114 | |
1035 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1115 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1036 | { |
1116 | { |
|
|
1117 | #if EV_PERIODICS |
1037 | periodics_reschedule (EV_A); |
1118 | periodics_reschedule (EV_A); |
|
|
1119 | #endif |
1038 | |
1120 | |
1039 | /* adjust timers. this is easy, as the offset is the same for all */ |
1121 | /* adjust timers. this is easy, as the offset is the same for all */ |
1040 | for (i = 0; i < timercnt; ++i) |
1122 | for (i = 0; i < timercnt; ++i) |
1041 | ((WT)timers [i])->at += rt_now - mn_now; |
1123 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1042 | } |
1124 | } |
1043 | |
1125 | |
1044 | mn_now = rt_now; |
1126 | mn_now = ev_rt_now; |
1045 | } |
1127 | } |
1046 | } |
1128 | } |
1047 | |
1129 | |
1048 | void |
1130 | void |
1049 | ev_ref (EV_P) |
1131 | ev_ref (EV_P) |
… | |
… | |
1081 | /* update fd-related kernel structures */ |
1163 | /* update fd-related kernel structures */ |
1082 | fd_reify (EV_A); |
1164 | fd_reify (EV_A); |
1083 | |
1165 | |
1084 | /* calculate blocking time */ |
1166 | /* calculate blocking time */ |
1085 | |
1167 | |
1086 | /* we only need this for !monotonic clockor timers, but as we basically |
1168 | /* we only need this for !monotonic clock or timers, but as we basically |
1087 | always have timers, we just calculate it always */ |
1169 | always have timers, we just calculate it always */ |
1088 | #if EV_USE_MONOTONIC |
1170 | #if EV_USE_MONOTONIC |
1089 | if (expect_true (have_monotonic)) |
1171 | if (expect_true (have_monotonic)) |
1090 | time_update_monotonic (EV_A); |
1172 | time_update_monotonic (EV_A); |
1091 | else |
1173 | else |
1092 | #endif |
1174 | #endif |
1093 | { |
1175 | { |
1094 | rt_now = ev_time (); |
1176 | ev_rt_now = ev_time (); |
1095 | mn_now = rt_now; |
1177 | mn_now = ev_rt_now; |
1096 | } |
1178 | } |
1097 | |
1179 | |
1098 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1180 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1099 | block = 0.; |
1181 | block = 0.; |
1100 | else |
1182 | else |
… | |
… | |
1105 | { |
1187 | { |
1106 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1188 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1107 | if (block > to) block = to; |
1189 | if (block > to) block = to; |
1108 | } |
1190 | } |
1109 | |
1191 | |
|
|
1192 | #if EV_PERIODICS |
1110 | if (periodiccnt) |
1193 | if (periodiccnt) |
1111 | { |
1194 | { |
1112 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
1195 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1113 | if (block > to) block = to; |
1196 | if (block > to) block = to; |
1114 | } |
1197 | } |
|
|
1198 | #endif |
1115 | |
1199 | |
1116 | if (block < 0.) block = 0.; |
1200 | if (block < 0.) block = 0.; |
1117 | } |
1201 | } |
1118 | |
1202 | |
1119 | method_poll (EV_A_ block); |
1203 | method_poll (EV_A_ block); |
1120 | |
1204 | |
1121 | /* update rt_now, do magic */ |
1205 | /* update ev_rt_now, do magic */ |
1122 | time_update (EV_A); |
1206 | time_update (EV_A); |
1123 | |
1207 | |
1124 | /* queue pending timers and reschedule them */ |
1208 | /* queue pending timers and reschedule them */ |
1125 | timers_reify (EV_A); /* relative timers called last */ |
1209 | timers_reify (EV_A); /* relative timers called last */ |
|
|
1210 | #if EV_PERIODICS |
1126 | periodics_reify (EV_A); /* absolute timers called first */ |
1211 | periodics_reify (EV_A); /* absolute timers called first */ |
|
|
1212 | #endif |
1127 | |
1213 | |
1128 | /* queue idle watchers unless io or timers are pending */ |
1214 | /* queue idle watchers unless io or timers are pending */ |
1129 | if (!pendingcnt) |
1215 | if (idlecnt && !any_pending (EV_A)) |
1130 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1216 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1131 | |
1217 | |
1132 | /* queue check watchers, to be executed first */ |
1218 | /* queue check watchers, to be executed first */ |
1133 | if (checkcnt) |
1219 | if (checkcnt) |
1134 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1220 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
… | |
… | |
1222 | { |
1308 | { |
1223 | ev_clear_pending (EV_A_ (W)w); |
1309 | ev_clear_pending (EV_A_ (W)w); |
1224 | if (!ev_is_active (w)) |
1310 | if (!ev_is_active (w)) |
1225 | return; |
1311 | return; |
1226 | |
1312 | |
|
|
1313 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
|
|
1314 | |
1227 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1315 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1228 | ev_stop (EV_A_ (W)w); |
1316 | ev_stop (EV_A_ (W)w); |
1229 | |
1317 | |
1230 | fd_change (EV_A_ w->fd); |
1318 | fd_change (EV_A_ w->fd); |
1231 | } |
1319 | } |
… | |
… | |
1261 | { |
1349 | { |
1262 | timers [((W)w)->active - 1] = timers [timercnt]; |
1350 | timers [((W)w)->active - 1] = timers [timercnt]; |
1263 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1351 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1264 | } |
1352 | } |
1265 | |
1353 | |
1266 | ((WT)w)->at = w->repeat; |
1354 | ((WT)w)->at -= mn_now; |
1267 | |
1355 | |
1268 | ev_stop (EV_A_ (W)w); |
1356 | ev_stop (EV_A_ (W)w); |
1269 | } |
1357 | } |
1270 | |
1358 | |
1271 | void |
1359 | void |
1272 | ev_timer_again (EV_P_ struct ev_timer *w) |
1360 | ev_timer_again (EV_P_ struct ev_timer *w) |
1273 | { |
1361 | { |
1274 | if (ev_is_active (w)) |
1362 | if (ev_is_active (w)) |
1275 | { |
1363 | { |
1276 | if (w->repeat) |
1364 | if (w->repeat) |
1277 | { |
|
|
1278 | ((WT)w)->at = mn_now + w->repeat; |
|
|
1279 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1365 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); |
1280 | } |
|
|
1281 | else |
1366 | else |
1282 | ev_timer_stop (EV_A_ w); |
1367 | ev_timer_stop (EV_A_ w); |
1283 | } |
1368 | } |
1284 | else if (w->repeat) |
1369 | else if (w->repeat) |
1285 | ev_timer_start (EV_A_ w); |
1370 | ev_timer_start (EV_A_ w); |
1286 | } |
1371 | } |
1287 | |
1372 | |
|
|
1373 | #if EV_PERIODICS |
1288 | void |
1374 | void |
1289 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1375 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1290 | { |
1376 | { |
1291 | if (ev_is_active (w)) |
1377 | if (ev_is_active (w)) |
1292 | return; |
1378 | return; |
1293 | |
1379 | |
|
|
1380 | if (w->reschedule_cb) |
|
|
1381 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
|
|
1382 | else if (w->interval) |
|
|
1383 | { |
1294 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1384 | 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 */ |
1385 | /* 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; |
1386 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
|
|
1387 | } |
1299 | |
1388 | |
1300 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1389 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1301 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1390 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1302 | periodics [periodiccnt - 1] = w; |
1391 | periodics [periodiccnt - 1] = w; |
1303 | upheap ((WT *)periodics, periodiccnt - 1); |
1392 | upheap ((WT *)periodics, periodiccnt - 1); |
… | |
… | |
1320 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1409 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1321 | } |
1410 | } |
1322 | |
1411 | |
1323 | ev_stop (EV_A_ (W)w); |
1412 | ev_stop (EV_A_ (W)w); |
1324 | } |
1413 | } |
|
|
1414 | |
|
|
1415 | void |
|
|
1416 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
|
|
1417 | { |
|
|
1418 | /* TODO: use adjustheap and recalculation */ |
|
|
1419 | ev_periodic_stop (EV_A_ w); |
|
|
1420 | ev_periodic_start (EV_A_ w); |
|
|
1421 | } |
|
|
1422 | #endif |
1325 | |
1423 | |
1326 | void |
1424 | void |
1327 | ev_idle_start (EV_P_ struct ev_idle *w) |
1425 | ev_idle_start (EV_P_ struct ev_idle *w) |
1328 | { |
1426 | { |
1329 | if (ev_is_active (w)) |
1427 | if (ev_is_active (w)) |
… | |
… | |
1505 | else |
1603 | else |
1506 | { |
1604 | { |
1507 | once->cb = cb; |
1605 | once->cb = cb; |
1508 | once->arg = arg; |
1606 | once->arg = arg; |
1509 | |
1607 | |
1510 | ev_watcher_init (&once->io, once_cb_io); |
1608 | ev_init (&once->io, once_cb_io); |
1511 | if (fd >= 0) |
1609 | if (fd >= 0) |
1512 | { |
1610 | { |
1513 | ev_io_set (&once->io, fd, events); |
1611 | ev_io_set (&once->io, fd, events); |
1514 | ev_io_start (EV_A_ &once->io); |
1612 | ev_io_start (EV_A_ &once->io); |
1515 | } |
1613 | } |
1516 | |
1614 | |
1517 | ev_watcher_init (&once->to, once_cb_to); |
1615 | ev_init (&once->to, once_cb_to); |
1518 | if (timeout >= 0.) |
1616 | if (timeout >= 0.) |
1519 | { |
1617 | { |
1520 | ev_timer_set (&once->to, timeout, 0.); |
1618 | ev_timer_set (&once->to, timeout, 0.); |
1521 | ev_timer_start (EV_A_ &once->to); |
1619 | ev_timer_start (EV_A_ &once->to); |
1522 | } |
1620 | } |
1523 | } |
1621 | } |
1524 | } |
1622 | } |
1525 | |
1623 | |
|
|
1624 | #ifdef __cplusplus |
|
|
1625 | } |
|
|
1626 | #endif |
|
|
1627 | |