… | |
… | |
64 | #include <assert.h> |
64 | #include <assert.h> |
65 | #include <errno.h> |
65 | #include <errno.h> |
66 | #include <sys/types.h> |
66 | #include <sys/types.h> |
67 | #include <time.h> |
67 | #include <time.h> |
68 | |
68 | |
69 | #ifndef PERL |
|
|
70 | # include <signal.h> |
69 | #include <signal.h> |
71 | #endif |
|
|
72 | |
70 | |
73 | #ifndef WIN32 |
71 | #ifndef WIN32 |
74 | # include <unistd.h> |
72 | # include <unistd.h> |
75 | # include <sys/time.h> |
73 | # include <sys/time.h> |
76 | # include <sys/wait.h> |
74 | # include <sys/wait.h> |
… | |
… | |
128 | #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) */ |
129 | #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) */ |
130 | #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 */ |
131 | /*#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 */ |
132 | |
130 | |
|
|
131 | #ifdef EV_H |
|
|
132 | # include EV_H |
|
|
133 | #else |
133 | #include "ev.h" |
134 | # include "ev.h" |
|
|
135 | #endif |
134 | |
136 | |
135 | #if __GNUC__ >= 3 |
137 | #if __GNUC__ >= 3 |
136 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
138 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
137 | # define inline inline |
139 | # define inline inline |
138 | #else |
140 | #else |
… | |
… | |
150 | typedef struct ev_watcher_list *WL; |
152 | typedef struct ev_watcher_list *WL; |
151 | typedef struct ev_watcher_time *WT; |
153 | typedef struct ev_watcher_time *WT; |
152 | |
154 | |
153 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
155 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
154 | |
156 | |
155 | #if WIN32 |
157 | #include "ev_win32.c" |
156 | /* note: the comment below could not be substantiated, but what would I care */ |
|
|
157 | /* MSDN says this is required to handle SIGFPE */ |
|
|
158 | volatile double SIGFPE_REQ = 0.0f; |
|
|
159 | #endif |
|
|
160 | |
158 | |
161 | /*****************************************************************************/ |
159 | /*****************************************************************************/ |
162 | |
160 | |
163 | static void (*syserr_cb)(const char *msg); |
161 | static void (*syserr_cb)(const char *msg); |
164 | |
162 | |
… | |
… | |
221 | int events; |
219 | int events; |
222 | } ANPENDING; |
220 | } ANPENDING; |
223 | |
221 | |
224 | #if EV_MULTIPLICITY |
222 | #if EV_MULTIPLICITY |
225 | |
223 | |
226 | struct ev_loop |
224 | struct ev_loop |
227 | { |
225 | { |
|
|
226 | ev_tstamp ev_rt_now; |
228 | # define VAR(name,decl) decl; |
227 | #define VAR(name,decl) decl; |
229 | # include "ev_vars.h" |
228 | #include "ev_vars.h" |
230 | }; |
|
|
231 | # undef VAR |
229 | #undef VAR |
|
|
230 | }; |
232 | # include "ev_wrap.h" |
231 | #include "ev_wrap.h" |
|
|
232 | |
|
|
233 | struct ev_loop default_loop_struct; |
|
|
234 | static struct ev_loop *default_loop; |
233 | |
235 | |
234 | #else |
236 | #else |
235 | |
237 | |
|
|
238 | ev_tstamp ev_rt_now; |
236 | # define VAR(name,decl) static decl; |
239 | #define VAR(name,decl) static decl; |
237 | # include "ev_vars.h" |
240 | #include "ev_vars.h" |
238 | # undef VAR |
241 | #undef VAR |
|
|
242 | |
|
|
243 | static int default_loop; |
239 | |
244 | |
240 | #endif |
245 | #endif |
241 | |
246 | |
242 | /*****************************************************************************/ |
247 | /*****************************************************************************/ |
243 | |
248 | |
… | |
… | |
268 | #endif |
273 | #endif |
269 | |
274 | |
270 | return ev_time (); |
275 | return ev_time (); |
271 | } |
276 | } |
272 | |
277 | |
|
|
278 | #if EV_MULTIPLICITY |
273 | ev_tstamp |
279 | ev_tstamp |
274 | ev_now (EV_P) |
280 | ev_now (EV_P) |
275 | { |
281 | { |
276 | return rt_now; |
282 | return ev_rt_now; |
277 | } |
283 | } |
|
|
284 | #endif |
278 | |
285 | |
279 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
286 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
280 | |
287 | |
281 | #define array_needsize(base,cur,cnt,init) \ |
288 | #define array_needsize(type,base,cur,cnt,init) \ |
282 | if (expect_false ((cnt) > cur)) \ |
289 | if (expect_false ((cnt) > cur)) \ |
283 | { \ |
290 | { \ |
284 | int newcnt = cur; \ |
291 | int newcnt = cur; \ |
285 | do \ |
292 | do \ |
286 | { \ |
293 | { \ |
287 | newcnt = array_roundsize (base, newcnt << 1); \ |
294 | newcnt = array_roundsize (type, newcnt << 1); \ |
288 | } \ |
295 | } \ |
289 | while ((cnt) > newcnt); \ |
296 | while ((cnt) > newcnt); \ |
290 | \ |
297 | \ |
291 | base = ev_realloc (base, sizeof (*base) * (newcnt)); \ |
298 | base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\ |
292 | init (base + cur, newcnt - cur); \ |
299 | init (base + cur, newcnt - cur); \ |
293 | cur = newcnt; \ |
300 | cur = newcnt; \ |
294 | } |
301 | } |
295 | |
302 | |
296 | #define array_slim(stem) \ |
303 | #define array_slim(type,stem) \ |
297 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
304 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
298 | { \ |
305 | { \ |
299 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
306 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
300 | base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ |
307 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
301 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
308 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
302 | } |
309 | } |
303 | |
310 | |
304 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
311 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
305 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
312 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
… | |
… | |
322 | |
329 | |
323 | ++base; |
330 | ++base; |
324 | } |
331 | } |
325 | } |
332 | } |
326 | |
333 | |
327 | static void |
334 | void |
328 | event (EV_P_ W w, int events) |
335 | ev_feed_event (EV_P_ void *w, int revents) |
329 | { |
336 | { |
|
|
337 | W w_ = (W)w; |
|
|
338 | |
330 | if (w->pending) |
339 | if (w_->pending) |
331 | { |
340 | { |
332 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
341 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
333 | return; |
342 | return; |
334 | } |
343 | } |
335 | |
344 | |
336 | w->pending = ++pendingcnt [ABSPRI (w)]; |
345 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
337 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); |
346 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); |
338 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
347 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
339 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
348 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
340 | } |
349 | } |
341 | |
350 | |
342 | static void |
351 | static void |
343 | queue_events (EV_P_ W *events, int eventcnt, int type) |
352 | queue_events (EV_P_ W *events, int eventcnt, int type) |
344 | { |
353 | { |
345 | int i; |
354 | int i; |
346 | |
355 | |
347 | for (i = 0; i < eventcnt; ++i) |
356 | for (i = 0; i < eventcnt; ++i) |
348 | event (EV_A_ events [i], type); |
357 | ev_feed_event (EV_A_ events [i], type); |
349 | } |
358 | } |
350 | |
359 | |
351 | static void |
360 | inline void |
352 | fd_event (EV_P_ int fd, int events) |
361 | fd_event (EV_P_ int fd, int revents) |
353 | { |
362 | { |
354 | ANFD *anfd = anfds + fd; |
363 | ANFD *anfd = anfds + fd; |
355 | struct ev_io *w; |
364 | struct ev_io *w; |
356 | |
365 | |
357 | 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) |
358 | { |
367 | { |
359 | int ev = w->events & events; |
368 | int ev = w->events & revents; |
360 | |
369 | |
361 | if (ev) |
370 | if (ev) |
362 | event (EV_A_ (W)w, ev); |
371 | ev_feed_event (EV_A_ (W)w, ev); |
363 | } |
372 | } |
|
|
373 | } |
|
|
374 | |
|
|
375 | void |
|
|
376 | ev_feed_fd_event (EV_P_ int fd, int revents) |
|
|
377 | { |
|
|
378 | fd_event (EV_A_ fd, revents); |
364 | } |
379 | } |
365 | |
380 | |
366 | /*****************************************************************************/ |
381 | /*****************************************************************************/ |
367 | |
382 | |
368 | static void |
383 | static void |
… | |
… | |
397 | return; |
412 | return; |
398 | |
413 | |
399 | anfds [fd].reify = 1; |
414 | anfds [fd].reify = 1; |
400 | |
415 | |
401 | ++fdchangecnt; |
416 | ++fdchangecnt; |
402 | array_needsize (fdchanges, fdchangemax, fdchangecnt, (void)); |
417 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); |
403 | fdchanges [fdchangecnt - 1] = fd; |
418 | fdchanges [fdchangecnt - 1] = fd; |
404 | } |
419 | } |
405 | |
420 | |
406 | static void |
421 | static void |
407 | fd_kill (EV_P_ int fd) |
422 | fd_kill (EV_P_ int fd) |
… | |
… | |
409 | struct ev_io *w; |
424 | struct ev_io *w; |
410 | |
425 | |
411 | while ((w = (struct ev_io *)anfds [fd].head)) |
426 | while ((w = (struct ev_io *)anfds [fd].head)) |
412 | { |
427 | { |
413 | ev_io_stop (EV_A_ w); |
428 | ev_io_stop (EV_A_ w); |
414 | 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); |
415 | } |
430 | } |
416 | } |
431 | } |
417 | |
432 | |
418 | static int |
433 | static int |
419 | fd_valid (int fd) |
434 | fd_valid (int fd) |
… | |
… | |
507 | |
522 | |
508 | heap [k] = w; |
523 | heap [k] = w; |
509 | ((W)heap [k])->active = k + 1; |
524 | ((W)heap [k])->active = k + 1; |
510 | } |
525 | } |
511 | |
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 | |
512 | /*****************************************************************************/ |
539 | /*****************************************************************************/ |
513 | |
540 | |
514 | typedef struct |
541 | typedef struct |
515 | { |
542 | { |
516 | WL head; |
543 | WL head; |
… | |
… | |
547 | |
574 | |
548 | if (!gotsig) |
575 | if (!gotsig) |
549 | { |
576 | { |
550 | int old_errno = errno; |
577 | int old_errno = errno; |
551 | gotsig = 1; |
578 | gotsig = 1; |
|
|
579 | #ifdef WIN32 |
|
|
580 | send (sigpipe [1], &signum, 1, MSG_DONTWAIT); |
|
|
581 | #else |
552 | write (sigpipe [1], &signum, 1); |
582 | write (sigpipe [1], &signum, 1); |
|
|
583 | #endif |
553 | errno = old_errno; |
584 | errno = old_errno; |
554 | } |
585 | } |
555 | } |
586 | } |
556 | |
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 | |
557 | static void |
608 | static void |
558 | sigcb (EV_P_ struct ev_io *iow, int revents) |
609 | sigcb (EV_P_ struct ev_io *iow, int revents) |
559 | { |
610 | { |
560 | WL w; |
|
|
561 | int signum; |
611 | int signum; |
562 | |
612 | |
|
|
613 | #ifdef WIN32 |
|
|
614 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
|
|
615 | #else |
563 | read (sigpipe [0], &revents, 1); |
616 | read (sigpipe [0], &revents, 1); |
|
|
617 | #endif |
564 | gotsig = 0; |
618 | gotsig = 0; |
565 | |
619 | |
566 | for (signum = signalmax; signum--; ) |
620 | for (signum = signalmax; signum--; ) |
567 | if (signals [signum].gotsig) |
621 | if (signals [signum].gotsig) |
568 | { |
622 | ev_feed_signal_event (EV_A_ signum + 1); |
569 | signals [signum].gotsig = 0; |
|
|
570 | |
|
|
571 | for (w = signals [signum].head; w; w = w->next) |
|
|
572 | event (EV_A_ (W)w, EV_SIGNAL); |
|
|
573 | } |
|
|
574 | } |
623 | } |
575 | |
624 | |
576 | static void |
625 | static void |
577 | siginit (EV_P) |
626 | siginit (EV_P) |
578 | { |
627 | { |
… | |
… | |
611 | if (w->pid == pid || !w->pid) |
660 | if (w->pid == pid || !w->pid) |
612 | { |
661 | { |
613 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
662 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
614 | w->rpid = pid; |
663 | w->rpid = pid; |
615 | w->rstatus = status; |
664 | w->rstatus = status; |
616 | event (EV_A_ (W)w, EV_CHILD); |
665 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
617 | } |
666 | } |
618 | } |
667 | } |
619 | |
668 | |
620 | static void |
669 | static void |
621 | childcb (EV_P_ struct ev_signal *sw, int revents) |
670 | childcb (EV_P_ struct ev_signal *sw, int revents) |
… | |
… | |
623 | int pid, status; |
672 | int pid, status; |
624 | |
673 | |
625 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
674 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
626 | { |
675 | { |
627 | /* 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 */ |
628 | event (EV_A_ (W)sw, EV_SIGNAL); |
677 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
629 | |
678 | |
630 | child_reap (EV_A_ sw, pid, pid, status); |
679 | child_reap (EV_A_ sw, pid, pid, status); |
631 | 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 */ |
632 | } |
681 | } |
633 | } |
682 | } |
… | |
… | |
690 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
739 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
691 | have_monotonic = 1; |
740 | have_monotonic = 1; |
692 | } |
741 | } |
693 | #endif |
742 | #endif |
694 | |
743 | |
695 | rt_now = ev_time (); |
744 | ev_rt_now = ev_time (); |
696 | mn_now = get_clock (); |
745 | mn_now = get_clock (); |
697 | now_floor = mn_now; |
746 | now_floor = mn_now; |
698 | rtmn_diff = rt_now - mn_now; |
747 | rtmn_diff = ev_rt_now - mn_now; |
699 | |
748 | |
700 | if (methods == EVMETHOD_AUTO) |
749 | if (methods == EVMETHOD_AUTO) |
701 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
750 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
702 | methods = atoi (getenv ("LIBEV_METHODS")); |
751 | methods = atoi (getenv ("LIBEV_METHODS")); |
703 | else |
752 | else |
… | |
… | |
718 | #endif |
767 | #endif |
719 | #if EV_USE_SELECT |
768 | #if EV_USE_SELECT |
720 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
769 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
721 | #endif |
770 | #endif |
722 | |
771 | |
723 | ev_watcher_init (&sigev, sigcb); |
772 | ev_init (&sigev, sigcb); |
724 | ev_set_priority (&sigev, EV_MAXPRI); |
773 | ev_set_priority (&sigev, EV_MAXPRI); |
725 | } |
774 | } |
726 | } |
775 | } |
727 | |
776 | |
728 | void |
777 | void |
… | |
… | |
818 | } |
867 | } |
819 | |
868 | |
820 | #endif |
869 | #endif |
821 | |
870 | |
822 | #if EV_MULTIPLICITY |
871 | #if EV_MULTIPLICITY |
823 | struct ev_loop default_loop_struct; |
|
|
824 | static struct ev_loop *default_loop; |
|
|
825 | |
|
|
826 | struct ev_loop * |
872 | struct ev_loop * |
827 | #else |
873 | #else |
828 | static int default_loop; |
|
|
829 | |
|
|
830 | int |
874 | int |
831 | #endif |
875 | #endif |
832 | ev_default_loop (int methods) |
876 | ev_default_loop (int methods) |
833 | { |
877 | { |
834 | if (sigpipe [0] == sigpipe [1]) |
878 | if (sigpipe [0] == sigpipe [1]) |
… | |
… | |
895 | postfork = 1; |
939 | postfork = 1; |
896 | } |
940 | } |
897 | |
941 | |
898 | /*****************************************************************************/ |
942 | /*****************************************************************************/ |
899 | |
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 | |
900 | static void |
956 | static void |
901 | call_pending (EV_P) |
957 | call_pending (EV_P) |
902 | { |
958 | { |
903 | int pri; |
959 | int pri; |
904 | |
960 | |
… | |
… | |
908 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
964 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
909 | |
965 | |
910 | if (p->w) |
966 | if (p->w) |
911 | { |
967 | { |
912 | p->w->pending = 0; |
968 | p->w->pending = 0; |
913 | p->w->cb (EV_A_ p->w, p->events); |
969 | EV_CB_INVOKE (p->w, p->events); |
914 | } |
970 | } |
915 | } |
971 | } |
916 | } |
972 | } |
917 | |
973 | |
918 | static void |
974 | static void |
… | |
… | |
932 | downheap ((WT *)timers, timercnt, 0); |
988 | downheap ((WT *)timers, timercnt, 0); |
933 | } |
989 | } |
934 | else |
990 | else |
935 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
991 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
936 | |
992 | |
937 | event (EV_A_ (W)w, EV_TIMEOUT); |
993 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
938 | } |
994 | } |
939 | } |
995 | } |
940 | |
996 | |
941 | static void |
997 | static void |
942 | periodics_reify (EV_P) |
998 | periodics_reify (EV_P) |
943 | { |
999 | { |
944 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
1000 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
945 | { |
1001 | { |
946 | struct ev_periodic *w = periodics [0]; |
1002 | struct ev_periodic *w = periodics [0]; |
947 | |
1003 | |
948 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1004 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
949 | |
1005 | |
950 | /* first reschedule or stop timer */ |
1006 | /* first reschedule or stop timer */ |
951 | if (w->interval) |
1007 | if (w->reschedule_cb) |
952 | { |
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 | { |
953 | ((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; |
954 | 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)); |
955 | downheap ((WT *)periodics, periodiccnt, 0); |
1018 | downheap ((WT *)periodics, periodiccnt, 0); |
956 | } |
1019 | } |
957 | else |
1020 | else |
958 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1021 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
959 | |
1022 | |
960 | event (EV_A_ (W)w, EV_PERIODIC); |
1023 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
961 | } |
1024 | } |
962 | } |
1025 | } |
963 | |
1026 | |
964 | static void |
1027 | static void |
965 | periodics_reschedule (EV_P) |
1028 | periodics_reschedule (EV_P) |
… | |
… | |
969 | /* adjust periodics after time jump */ |
1032 | /* adjust periodics after time jump */ |
970 | for (i = 0; i < periodiccnt; ++i) |
1033 | for (i = 0; i < periodiccnt; ++i) |
971 | { |
1034 | { |
972 | struct ev_periodic *w = periodics [i]; |
1035 | struct ev_periodic *w = periodics [i]; |
973 | |
1036 | |
|
|
1037 | if (w->reschedule_cb) |
|
|
1038 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
974 | if (w->interval) |
1039 | else if (w->interval) |
975 | { |
|
|
976 | 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; |
977 | |
|
|
978 | if (fabs (diff) >= 1e-4) |
|
|
979 | { |
|
|
980 | ev_periodic_stop (EV_A_ w); |
|
|
981 | ev_periodic_start (EV_A_ w); |
|
|
982 | |
|
|
983 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
984 | } |
|
|
985 | } |
|
|
986 | } |
1041 | } |
|
|
1042 | |
|
|
1043 | /* now rebuild the heap */ |
|
|
1044 | for (i = periodiccnt >> 1; i--; ) |
|
|
1045 | downheap ((WT *)periodics, periodiccnt, i); |
987 | } |
1046 | } |
988 | |
1047 | |
989 | inline int |
1048 | inline int |
990 | time_update_monotonic (EV_P) |
1049 | time_update_monotonic (EV_P) |
991 | { |
1050 | { |
992 | mn_now = get_clock (); |
1051 | mn_now = get_clock (); |
993 | |
1052 | |
994 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1053 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
995 | { |
1054 | { |
996 | rt_now = rtmn_diff + mn_now; |
1055 | ev_rt_now = rtmn_diff + mn_now; |
997 | return 0; |
1056 | return 0; |
998 | } |
1057 | } |
999 | else |
1058 | else |
1000 | { |
1059 | { |
1001 | now_floor = mn_now; |
1060 | now_floor = mn_now; |
1002 | rt_now = ev_time (); |
1061 | ev_rt_now = ev_time (); |
1003 | return 1; |
1062 | return 1; |
1004 | } |
1063 | } |
1005 | } |
1064 | } |
1006 | |
1065 | |
1007 | static void |
1066 | static void |
… | |
… | |
1016 | { |
1075 | { |
1017 | ev_tstamp odiff = rtmn_diff; |
1076 | ev_tstamp odiff = rtmn_diff; |
1018 | |
1077 | |
1019 | 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 */ |
1020 | { |
1079 | { |
1021 | rtmn_diff = rt_now - mn_now; |
1080 | rtmn_diff = ev_rt_now - mn_now; |
1022 | |
1081 | |
1023 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1082 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1024 | return; /* all is well */ |
1083 | return; /* all is well */ |
1025 | |
1084 | |
1026 | rt_now = ev_time (); |
1085 | ev_rt_now = ev_time (); |
1027 | mn_now = get_clock (); |
1086 | mn_now = get_clock (); |
1028 | now_floor = mn_now; |
1087 | now_floor = mn_now; |
1029 | } |
1088 | } |
1030 | |
1089 | |
1031 | periodics_reschedule (EV_A); |
1090 | periodics_reschedule (EV_A); |
… | |
… | |
1034 | } |
1093 | } |
1035 | } |
1094 | } |
1036 | else |
1095 | else |
1037 | #endif |
1096 | #endif |
1038 | { |
1097 | { |
1039 | rt_now = ev_time (); |
1098 | ev_rt_now = ev_time (); |
1040 | |
1099 | |
1041 | 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)) |
1042 | { |
1101 | { |
1043 | periodics_reschedule (EV_A); |
1102 | periodics_reschedule (EV_A); |
1044 | |
1103 | |
1045 | /* 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 */ |
1046 | for (i = 0; i < timercnt; ++i) |
1105 | for (i = 0; i < timercnt; ++i) |
1047 | ((WT)timers [i])->at += rt_now - mn_now; |
1106 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1048 | } |
1107 | } |
1049 | |
1108 | |
1050 | mn_now = rt_now; |
1109 | mn_now = ev_rt_now; |
1051 | } |
1110 | } |
1052 | } |
1111 | } |
1053 | |
1112 | |
1054 | void |
1113 | void |
1055 | ev_ref (EV_P) |
1114 | ev_ref (EV_P) |
… | |
… | |
1087 | /* update fd-related kernel structures */ |
1146 | /* update fd-related kernel structures */ |
1088 | fd_reify (EV_A); |
1147 | fd_reify (EV_A); |
1089 | |
1148 | |
1090 | /* calculate blocking time */ |
1149 | /* calculate blocking time */ |
1091 | |
1150 | |
1092 | /* 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 |
1093 | always have timers, we just calculate it always */ |
1152 | always have timers, we just calculate it always */ |
1094 | #if EV_USE_MONOTONIC |
1153 | #if EV_USE_MONOTONIC |
1095 | if (expect_true (have_monotonic)) |
1154 | if (expect_true (have_monotonic)) |
1096 | time_update_monotonic (EV_A); |
1155 | time_update_monotonic (EV_A); |
1097 | else |
1156 | else |
1098 | #endif |
1157 | #endif |
1099 | { |
1158 | { |
1100 | rt_now = ev_time (); |
1159 | ev_rt_now = ev_time (); |
1101 | mn_now = rt_now; |
1160 | mn_now = ev_rt_now; |
1102 | } |
1161 | } |
1103 | |
1162 | |
1104 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1163 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1105 | block = 0.; |
1164 | block = 0.; |
1106 | else |
1165 | else |
… | |
… | |
1113 | if (block > to) block = to; |
1172 | if (block > to) block = to; |
1114 | } |
1173 | } |
1115 | |
1174 | |
1116 | if (periodiccnt) |
1175 | if (periodiccnt) |
1117 | { |
1176 | { |
1118 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
1177 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1119 | if (block > to) block = to; |
1178 | if (block > to) block = to; |
1120 | } |
1179 | } |
1121 | |
1180 | |
1122 | if (block < 0.) block = 0.; |
1181 | if (block < 0.) block = 0.; |
1123 | } |
1182 | } |
1124 | |
1183 | |
1125 | method_poll (EV_A_ block); |
1184 | method_poll (EV_A_ block); |
1126 | |
1185 | |
1127 | /* update rt_now, do magic */ |
1186 | /* update ev_rt_now, do magic */ |
1128 | time_update (EV_A); |
1187 | time_update (EV_A); |
1129 | |
1188 | |
1130 | /* queue pending timers and reschedule them */ |
1189 | /* queue pending timers and reschedule them */ |
1131 | timers_reify (EV_A); /* relative timers called last */ |
1190 | timers_reify (EV_A); /* relative timers called last */ |
1132 | periodics_reify (EV_A); /* absolute timers called first */ |
1191 | periodics_reify (EV_A); /* absolute timers called first */ |
1133 | |
1192 | |
1134 | /* queue idle watchers unless io or timers are pending */ |
1193 | /* queue idle watchers unless io or timers are pending */ |
1135 | if (!pendingcnt) |
1194 | if (idlecnt && !any_pending (EV_A)) |
1136 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1195 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1137 | |
1196 | |
1138 | /* queue check watchers, to be executed first */ |
1197 | /* queue check watchers, to be executed first */ |
1139 | if (checkcnt) |
1198 | if (checkcnt) |
1140 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1199 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
… | |
… | |
1215 | return; |
1274 | return; |
1216 | |
1275 | |
1217 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1276 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1218 | |
1277 | |
1219 | ev_start (EV_A_ (W)w, 1); |
1278 | ev_start (EV_A_ (W)w, 1); |
1220 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
1279 | array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); |
1221 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1280 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1222 | |
1281 | |
1223 | fd_change (EV_A_ fd); |
1282 | fd_change (EV_A_ fd); |
1224 | } |
1283 | } |
1225 | |
1284 | |
… | |
… | |
1245 | ((WT)w)->at += mn_now; |
1304 | ((WT)w)->at += mn_now; |
1246 | |
1305 | |
1247 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1306 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1248 | |
1307 | |
1249 | ev_start (EV_A_ (W)w, ++timercnt); |
1308 | ev_start (EV_A_ (W)w, ++timercnt); |
1250 | array_needsize (timers, timermax, timercnt, (void)); |
1309 | array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); |
1251 | timers [timercnt - 1] = w; |
1310 | timers [timercnt - 1] = w; |
1252 | upheap ((WT *)timers, timercnt - 1); |
1311 | upheap ((WT *)timers, timercnt - 1); |
1253 | |
1312 | |
1254 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1313 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1255 | } |
1314 | } |
… | |
… | |
1278 | ev_timer_again (EV_P_ struct ev_timer *w) |
1337 | ev_timer_again (EV_P_ struct ev_timer *w) |
1279 | { |
1338 | { |
1280 | if (ev_is_active (w)) |
1339 | if (ev_is_active (w)) |
1281 | { |
1340 | { |
1282 | if (w->repeat) |
1341 | if (w->repeat) |
1283 | { |
|
|
1284 | ((WT)w)->at = mn_now + w->repeat; |
|
|
1285 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1342 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); |
1286 | } |
|
|
1287 | else |
1343 | else |
1288 | ev_timer_stop (EV_A_ w); |
1344 | ev_timer_stop (EV_A_ w); |
1289 | } |
1345 | } |
1290 | else if (w->repeat) |
1346 | else if (w->repeat) |
1291 | ev_timer_start (EV_A_ w); |
1347 | ev_timer_start (EV_A_ w); |
… | |
… | |
1295 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1351 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1296 | { |
1352 | { |
1297 | if (ev_is_active (w)) |
1353 | if (ev_is_active (w)) |
1298 | return; |
1354 | return; |
1299 | |
1355 | |
|
|
1356 | if (w->reschedule_cb) |
|
|
1357 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
|
|
1358 | else if (w->interval) |
|
|
1359 | { |
1300 | 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.)); |
1301 | |
|
|
1302 | /* 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 */ |
1303 | if (w->interval) |
|
|
1304 | ((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 | } |
1305 | |
1364 | |
1306 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1365 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1307 | array_needsize (periodics, periodicmax, periodiccnt, (void)); |
1366 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1308 | periodics [periodiccnt - 1] = w; |
1367 | periodics [periodiccnt - 1] = w; |
1309 | upheap ((WT *)periodics, periodiccnt - 1); |
1368 | upheap ((WT *)periodics, periodiccnt - 1); |
1310 | |
1369 | |
1311 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1370 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1312 | } |
1371 | } |
… | |
… | |
1328 | |
1387 | |
1329 | ev_stop (EV_A_ (W)w); |
1388 | ev_stop (EV_A_ (W)w); |
1330 | } |
1389 | } |
1331 | |
1390 | |
1332 | void |
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); |
|
|
1397 | } |
|
|
1398 | |
|
|
1399 | void |
1333 | ev_idle_start (EV_P_ struct ev_idle *w) |
1400 | ev_idle_start (EV_P_ struct ev_idle *w) |
1334 | { |
1401 | { |
1335 | if (ev_is_active (w)) |
1402 | if (ev_is_active (w)) |
1336 | return; |
1403 | return; |
1337 | |
1404 | |
1338 | ev_start (EV_A_ (W)w, ++idlecnt); |
1405 | ev_start (EV_A_ (W)w, ++idlecnt); |
1339 | array_needsize (idles, idlemax, idlecnt, (void)); |
1406 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); |
1340 | idles [idlecnt - 1] = w; |
1407 | idles [idlecnt - 1] = w; |
1341 | } |
1408 | } |
1342 | |
1409 | |
1343 | void |
1410 | void |
1344 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1411 | ev_idle_stop (EV_P_ struct ev_idle *w) |
… | |
… | |
1356 | { |
1423 | { |
1357 | if (ev_is_active (w)) |
1424 | if (ev_is_active (w)) |
1358 | return; |
1425 | return; |
1359 | |
1426 | |
1360 | ev_start (EV_A_ (W)w, ++preparecnt); |
1427 | ev_start (EV_A_ (W)w, ++preparecnt); |
1361 | array_needsize (prepares, preparemax, preparecnt, (void)); |
1428 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); |
1362 | prepares [preparecnt - 1] = w; |
1429 | prepares [preparecnt - 1] = w; |
1363 | } |
1430 | } |
1364 | |
1431 | |
1365 | void |
1432 | void |
1366 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1433 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
… | |
… | |
1378 | { |
1445 | { |
1379 | if (ev_is_active (w)) |
1446 | if (ev_is_active (w)) |
1380 | return; |
1447 | return; |
1381 | |
1448 | |
1382 | ev_start (EV_A_ (W)w, ++checkcnt); |
1449 | ev_start (EV_A_ (W)w, ++checkcnt); |
1383 | array_needsize (checks, checkmax, checkcnt, (void)); |
1450 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); |
1384 | checks [checkcnt - 1] = w; |
1451 | checks [checkcnt - 1] = w; |
1385 | } |
1452 | } |
1386 | |
1453 | |
1387 | void |
1454 | void |
1388 | ev_check_stop (EV_P_ struct ev_check *w) |
1455 | ev_check_stop (EV_P_ struct ev_check *w) |
… | |
… | |
1409 | return; |
1476 | return; |
1410 | |
1477 | |
1411 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1478 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1412 | |
1479 | |
1413 | ev_start (EV_A_ (W)w, 1); |
1480 | ev_start (EV_A_ (W)w, 1); |
1414 | array_needsize (signals, signalmax, w->signum, signals_init); |
1481 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1415 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1482 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1416 | |
1483 | |
1417 | if (!((WL)w)->next) |
1484 | if (!((WL)w)->next) |
1418 | { |
1485 | { |
1419 | #if WIN32 |
1486 | #if WIN32 |
… | |
… | |
1502 | } |
1569 | } |
1503 | |
1570 | |
1504 | void |
1571 | void |
1505 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1572 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1506 | { |
1573 | { |
1507 | struct ev_once *once = ev_malloc (sizeof (struct ev_once)); |
1574 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1508 | |
1575 | |
1509 | if (!once) |
1576 | if (!once) |
1510 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1577 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1511 | else |
1578 | else |
1512 | { |
1579 | { |
1513 | once->cb = cb; |
1580 | once->cb = cb; |
1514 | once->arg = arg; |
1581 | once->arg = arg; |
1515 | |
1582 | |
1516 | ev_watcher_init (&once->io, once_cb_io); |
1583 | ev_init (&once->io, once_cb_io); |
1517 | if (fd >= 0) |
1584 | if (fd >= 0) |
1518 | { |
1585 | { |
1519 | ev_io_set (&once->io, fd, events); |
1586 | ev_io_set (&once->io, fd, events); |
1520 | ev_io_start (EV_A_ &once->io); |
1587 | ev_io_start (EV_A_ &once->io); |
1521 | } |
1588 | } |
1522 | |
1589 | |
1523 | ev_watcher_init (&once->to, once_cb_to); |
1590 | ev_init (&once->to, once_cb_to); |
1524 | if (timeout >= 0.) |
1591 | if (timeout >= 0.) |
1525 | { |
1592 | { |
1526 | ev_timer_set (&once->to, timeout, 0.); |
1593 | ev_timer_set (&once->to, timeout, 0.); |
1527 | ev_timer_start (EV_A_ &once->to); |
1594 | ev_timer_start (EV_A_ &once->to); |
1528 | } |
1595 | } |