… | |
… | |
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 | { |
228 | # define VAR(name,decl) decl; |
226 | #define VAR(name,decl) decl; |
229 | # include "ev_vars.h" |
227 | #include "ev_vars.h" |
230 | }; |
|
|
231 | # undef VAR |
228 | #undef VAR |
|
|
229 | }; |
232 | # include "ev_wrap.h" |
230 | #include "ev_wrap.h" |
|
|
231 | |
|
|
232 | struct ev_loop default_loop_struct; |
|
|
233 | static struct ev_loop *default_loop; |
233 | |
234 | |
234 | #else |
235 | #else |
235 | |
236 | |
236 | # define VAR(name,decl) static decl; |
237 | #define VAR(name,decl) static decl; |
237 | # include "ev_vars.h" |
238 | #include "ev_vars.h" |
238 | # undef VAR |
239 | #undef VAR |
|
|
240 | |
|
|
241 | static int default_loop; |
239 | |
242 | |
240 | #endif |
243 | #endif |
241 | |
244 | |
242 | /*****************************************************************************/ |
245 | /*****************************************************************************/ |
243 | |
246 | |
… | |
… | |
274 | ev_now (EV_P) |
277 | ev_now (EV_P) |
275 | { |
278 | { |
276 | return rt_now; |
279 | return rt_now; |
277 | } |
280 | } |
278 | |
281 | |
279 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
282 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
280 | |
283 | |
281 | #define array_needsize(base,cur,cnt,init) \ |
284 | #define array_needsize(type,base,cur,cnt,init) \ |
282 | if (expect_false ((cnt) > cur)) \ |
285 | if (expect_false ((cnt) > cur)) \ |
283 | { \ |
286 | { \ |
284 | int newcnt = cur; \ |
287 | int newcnt = cur; \ |
285 | do \ |
288 | do \ |
286 | { \ |
289 | { \ |
287 | newcnt = array_roundsize (base, newcnt << 1); \ |
290 | newcnt = array_roundsize (type, newcnt << 1); \ |
288 | } \ |
291 | } \ |
289 | while ((cnt) > newcnt); \ |
292 | while ((cnt) > newcnt); \ |
290 | \ |
293 | \ |
291 | base = ev_realloc (base, sizeof (*base) * (newcnt)); \ |
294 | base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\ |
292 | init (base + cur, newcnt - cur); \ |
295 | init (base + cur, newcnt - cur); \ |
293 | cur = newcnt; \ |
296 | cur = newcnt; \ |
294 | } |
297 | } |
295 | |
298 | |
296 | #define array_slim(stem) \ |
299 | #define array_slim(type,stem) \ |
297 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
300 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
298 | { \ |
301 | { \ |
299 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
302 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
300 | base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ |
303 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
301 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
304 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
302 | } |
305 | } |
303 | |
306 | |
304 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
307 | /* 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 */ |
308 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
… | |
… | |
322 | |
325 | |
323 | ++base; |
326 | ++base; |
324 | } |
327 | } |
325 | } |
328 | } |
326 | |
329 | |
327 | static void |
330 | void |
328 | event (EV_P_ W w, int events) |
331 | ev_feed_event (EV_P_ void *w, int revents) |
329 | { |
332 | { |
|
|
333 | W w_ = (W)w; |
|
|
334 | |
330 | if (w->pending) |
335 | if (w_->pending) |
331 | { |
336 | { |
332 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
337 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
333 | return; |
338 | return; |
334 | } |
339 | } |
335 | |
340 | |
336 | w->pending = ++pendingcnt [ABSPRI (w)]; |
341 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
337 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); |
342 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); |
338 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
343 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
339 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
344 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
340 | } |
345 | } |
341 | |
346 | |
342 | static void |
347 | static void |
343 | queue_events (EV_P_ W *events, int eventcnt, int type) |
348 | queue_events (EV_P_ W *events, int eventcnt, int type) |
344 | { |
349 | { |
345 | int i; |
350 | int i; |
346 | |
351 | |
347 | for (i = 0; i < eventcnt; ++i) |
352 | for (i = 0; i < eventcnt; ++i) |
348 | event (EV_A_ events [i], type); |
353 | ev_feed_event (EV_A_ events [i], type); |
349 | } |
354 | } |
350 | |
355 | |
351 | static void |
356 | inline void |
352 | fd_event (EV_P_ int fd, int events) |
357 | fd_event (EV_P_ int fd, int revents) |
353 | { |
358 | { |
354 | ANFD *anfd = anfds + fd; |
359 | ANFD *anfd = anfds + fd; |
355 | struct ev_io *w; |
360 | struct ev_io *w; |
356 | |
361 | |
357 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
362 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
358 | { |
363 | { |
359 | int ev = w->events & events; |
364 | int ev = w->events & revents; |
360 | |
365 | |
361 | if (ev) |
366 | if (ev) |
362 | event (EV_A_ (W)w, ev); |
367 | ev_feed_event (EV_A_ (W)w, ev); |
363 | } |
368 | } |
|
|
369 | } |
|
|
370 | |
|
|
371 | void |
|
|
372 | ev_feed_fd_event (EV_P_ int fd, int revents) |
|
|
373 | { |
|
|
374 | fd_event (EV_A_ fd, revents); |
364 | } |
375 | } |
365 | |
376 | |
366 | /*****************************************************************************/ |
377 | /*****************************************************************************/ |
367 | |
378 | |
368 | static void |
379 | static void |
… | |
… | |
397 | return; |
408 | return; |
398 | |
409 | |
399 | anfds [fd].reify = 1; |
410 | anfds [fd].reify = 1; |
400 | |
411 | |
401 | ++fdchangecnt; |
412 | ++fdchangecnt; |
402 | array_needsize (fdchanges, fdchangemax, fdchangecnt, (void)); |
413 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); |
403 | fdchanges [fdchangecnt - 1] = fd; |
414 | fdchanges [fdchangecnt - 1] = fd; |
404 | } |
415 | } |
405 | |
416 | |
406 | static void |
417 | static void |
407 | fd_kill (EV_P_ int fd) |
418 | fd_kill (EV_P_ int fd) |
… | |
… | |
409 | struct ev_io *w; |
420 | struct ev_io *w; |
410 | |
421 | |
411 | while ((w = (struct ev_io *)anfds [fd].head)) |
422 | while ((w = (struct ev_io *)anfds [fd].head)) |
412 | { |
423 | { |
413 | ev_io_stop (EV_A_ w); |
424 | ev_io_stop (EV_A_ w); |
414 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
425 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
415 | } |
426 | } |
416 | } |
427 | } |
417 | |
428 | |
418 | static int |
429 | static int |
419 | fd_valid (int fd) |
430 | fd_valid (int fd) |
… | |
… | |
507 | |
518 | |
508 | heap [k] = w; |
519 | heap [k] = w; |
509 | ((W)heap [k])->active = k + 1; |
520 | ((W)heap [k])->active = k + 1; |
510 | } |
521 | } |
511 | |
522 | |
|
|
523 | inline void |
|
|
524 | adjustheap (WT *heap, int N, int k, ev_tstamp at) |
|
|
525 | { |
|
|
526 | ev_tstamp old_at = heap [k]->at; |
|
|
527 | heap [k]->at = at; |
|
|
528 | |
|
|
529 | if (old_at < at) |
|
|
530 | downheap (heap, N, k); |
|
|
531 | else |
|
|
532 | upheap (heap, k); |
|
|
533 | } |
|
|
534 | |
512 | /*****************************************************************************/ |
535 | /*****************************************************************************/ |
513 | |
536 | |
514 | typedef struct |
537 | typedef struct |
515 | { |
538 | { |
516 | WL head; |
539 | WL head; |
… | |
… | |
547 | |
570 | |
548 | if (!gotsig) |
571 | if (!gotsig) |
549 | { |
572 | { |
550 | int old_errno = errno; |
573 | int old_errno = errno; |
551 | gotsig = 1; |
574 | gotsig = 1; |
|
|
575 | #ifdef WIN32 |
|
|
576 | send (sigpipe [1], &signum, 1, MSG_DONTWAIT); |
|
|
577 | #else |
552 | write (sigpipe [1], &signum, 1); |
578 | write (sigpipe [1], &signum, 1); |
|
|
579 | #endif |
553 | errno = old_errno; |
580 | errno = old_errno; |
554 | } |
581 | } |
555 | } |
582 | } |
556 | |
583 | |
|
|
584 | void |
|
|
585 | ev_feed_signal_event (EV_P_ int signum) |
|
|
586 | { |
|
|
587 | WL w; |
|
|
588 | |
|
|
589 | #if EV_MULTIPLICITY |
|
|
590 | assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
|
|
591 | #endif |
|
|
592 | |
|
|
593 | --signum; |
|
|
594 | |
|
|
595 | if (signum < 0 || signum >= signalmax) |
|
|
596 | return; |
|
|
597 | |
|
|
598 | signals [signum].gotsig = 0; |
|
|
599 | |
|
|
600 | for (w = signals [signum].head; w; w = w->next) |
|
|
601 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
|
|
602 | } |
|
|
603 | |
557 | static void |
604 | static void |
558 | sigcb (EV_P_ struct ev_io *iow, int revents) |
605 | sigcb (EV_P_ struct ev_io *iow, int revents) |
559 | { |
606 | { |
560 | WL w; |
|
|
561 | int signum; |
607 | int signum; |
562 | |
608 | |
|
|
609 | #ifdef WIN32 |
|
|
610 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
|
|
611 | #else |
563 | read (sigpipe [0], &revents, 1); |
612 | read (sigpipe [0], &revents, 1); |
|
|
613 | #endif |
564 | gotsig = 0; |
614 | gotsig = 0; |
565 | |
615 | |
566 | for (signum = signalmax; signum--; ) |
616 | for (signum = signalmax; signum--; ) |
567 | if (signals [signum].gotsig) |
617 | if (signals [signum].gotsig) |
568 | { |
618 | 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 | } |
619 | } |
575 | |
620 | |
576 | static void |
621 | static void |
577 | siginit (EV_P) |
622 | siginit (EV_P) |
578 | { |
623 | { |
… | |
… | |
611 | if (w->pid == pid || !w->pid) |
656 | if (w->pid == pid || !w->pid) |
612 | { |
657 | { |
613 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
658 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
614 | w->rpid = pid; |
659 | w->rpid = pid; |
615 | w->rstatus = status; |
660 | w->rstatus = status; |
616 | event (EV_A_ (W)w, EV_CHILD); |
661 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
617 | } |
662 | } |
618 | } |
663 | } |
619 | |
664 | |
620 | static void |
665 | static void |
621 | childcb (EV_P_ struct ev_signal *sw, int revents) |
666 | childcb (EV_P_ struct ev_signal *sw, int revents) |
… | |
… | |
623 | int pid, status; |
668 | int pid, status; |
624 | |
669 | |
625 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
670 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
626 | { |
671 | { |
627 | /* make sure we are called again until all childs have been reaped */ |
672 | /* make sure we are called again until all childs have been reaped */ |
628 | event (EV_A_ (W)sw, EV_SIGNAL); |
673 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
629 | |
674 | |
630 | child_reap (EV_A_ sw, pid, pid, status); |
675 | 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 */ |
676 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
632 | } |
677 | } |
633 | } |
678 | } |
… | |
… | |
718 | #endif |
763 | #endif |
719 | #if EV_USE_SELECT |
764 | #if EV_USE_SELECT |
720 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
765 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
721 | #endif |
766 | #endif |
722 | |
767 | |
723 | ev_watcher_init (&sigev, sigcb); |
768 | ev_init (&sigev, sigcb); |
724 | ev_set_priority (&sigev, EV_MAXPRI); |
769 | ev_set_priority (&sigev, EV_MAXPRI); |
725 | } |
770 | } |
726 | } |
771 | } |
727 | |
772 | |
728 | void |
773 | void |
… | |
… | |
818 | } |
863 | } |
819 | |
864 | |
820 | #endif |
865 | #endif |
821 | |
866 | |
822 | #if EV_MULTIPLICITY |
867 | #if EV_MULTIPLICITY |
823 | struct ev_loop default_loop_struct; |
|
|
824 | static struct ev_loop *default_loop; |
|
|
825 | |
|
|
826 | struct ev_loop * |
868 | struct ev_loop * |
827 | #else |
869 | #else |
828 | static int default_loop; |
|
|
829 | |
|
|
830 | int |
870 | int |
831 | #endif |
871 | #endif |
832 | ev_default_loop (int methods) |
872 | ev_default_loop (int methods) |
833 | { |
873 | { |
834 | if (sigpipe [0] == sigpipe [1]) |
874 | if (sigpipe [0] == sigpipe [1]) |
… | |
… | |
895 | postfork = 1; |
935 | postfork = 1; |
896 | } |
936 | } |
897 | |
937 | |
898 | /*****************************************************************************/ |
938 | /*****************************************************************************/ |
899 | |
939 | |
|
|
940 | static int |
|
|
941 | any_pending (EV_P) |
|
|
942 | { |
|
|
943 | int pri; |
|
|
944 | |
|
|
945 | for (pri = NUMPRI; pri--; ) |
|
|
946 | if (pendingcnt [pri]) |
|
|
947 | return 1; |
|
|
948 | |
|
|
949 | return 0; |
|
|
950 | } |
|
|
951 | |
900 | static void |
952 | static void |
901 | call_pending (EV_P) |
953 | call_pending (EV_P) |
902 | { |
954 | { |
903 | int pri; |
955 | int pri; |
904 | |
956 | |
… | |
… | |
908 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
960 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
909 | |
961 | |
910 | if (p->w) |
962 | if (p->w) |
911 | { |
963 | { |
912 | p->w->pending = 0; |
964 | p->w->pending = 0; |
913 | p->w->cb (EV_A_ p->w, p->events); |
965 | EV_CB_INVOKE (p->w, p->events); |
914 | } |
966 | } |
915 | } |
967 | } |
916 | } |
968 | } |
917 | |
969 | |
918 | static void |
970 | static void |
… | |
… | |
932 | downheap ((WT *)timers, timercnt, 0); |
984 | downheap ((WT *)timers, timercnt, 0); |
933 | } |
985 | } |
934 | else |
986 | else |
935 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
987 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
936 | |
988 | |
937 | event (EV_A_ (W)w, EV_TIMEOUT); |
989 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
938 | } |
990 | } |
939 | } |
991 | } |
940 | |
992 | |
941 | static void |
993 | static void |
942 | periodics_reify (EV_P) |
994 | periodics_reify (EV_P) |
… | |
… | |
946 | struct ev_periodic *w = periodics [0]; |
998 | struct ev_periodic *w = periodics [0]; |
947 | |
999 | |
948 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1000 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
949 | |
1001 | |
950 | /* first reschedule or stop timer */ |
1002 | /* first reschedule or stop timer */ |
|
|
1003 | if (w->reschedule_cb) |
|
|
1004 | { |
|
|
1005 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); |
|
|
1006 | |
|
|
1007 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); |
|
|
1008 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
1009 | } |
951 | if (w->interval) |
1010 | else if (w->interval) |
952 | { |
1011 | { |
953 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1012 | ((WT)w)->at += floor ((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)); |
1013 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
955 | downheap ((WT *)periodics, periodiccnt, 0); |
1014 | downheap ((WT *)periodics, periodiccnt, 0); |
956 | } |
1015 | } |
957 | else |
1016 | else |
958 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1017 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
959 | |
1018 | |
960 | event (EV_A_ (W)w, EV_PERIODIC); |
1019 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
961 | } |
1020 | } |
962 | } |
1021 | } |
963 | |
1022 | |
964 | static void |
1023 | static void |
965 | periodics_reschedule (EV_P) |
1024 | periodics_reschedule (EV_P) |
… | |
… | |
969 | /* adjust periodics after time jump */ |
1028 | /* adjust periodics after time jump */ |
970 | for (i = 0; i < periodiccnt; ++i) |
1029 | for (i = 0; i < periodiccnt; ++i) |
971 | { |
1030 | { |
972 | struct ev_periodic *w = periodics [i]; |
1031 | struct ev_periodic *w = periodics [i]; |
973 | |
1032 | |
|
|
1033 | if (w->reschedule_cb) |
|
|
1034 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
974 | if (w->interval) |
1035 | else if (w->interval) |
975 | { |
|
|
976 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1036 | ((WT)w)->at += ceil ((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 | } |
1037 | } |
|
|
1038 | |
|
|
1039 | /* now rebuild the heap */ |
|
|
1040 | for (i = periodiccnt >> 1; i--; ) |
|
|
1041 | downheap ((WT *)periodics, periodiccnt, i); |
987 | } |
1042 | } |
988 | |
1043 | |
989 | inline int |
1044 | inline int |
990 | time_update_monotonic (EV_P) |
1045 | time_update_monotonic (EV_P) |
991 | { |
1046 | { |
… | |
… | |
1087 | /* update fd-related kernel structures */ |
1142 | /* update fd-related kernel structures */ |
1088 | fd_reify (EV_A); |
1143 | fd_reify (EV_A); |
1089 | |
1144 | |
1090 | /* calculate blocking time */ |
1145 | /* calculate blocking time */ |
1091 | |
1146 | |
1092 | /* we only need this for !monotonic clockor timers, but as we basically |
1147 | /* we only need this for !monotonic clock or timers, but as we basically |
1093 | always have timers, we just calculate it always */ |
1148 | always have timers, we just calculate it always */ |
1094 | #if EV_USE_MONOTONIC |
1149 | #if EV_USE_MONOTONIC |
1095 | if (expect_true (have_monotonic)) |
1150 | if (expect_true (have_monotonic)) |
1096 | time_update_monotonic (EV_A); |
1151 | time_update_monotonic (EV_A); |
1097 | else |
1152 | else |
… | |
… | |
1130 | /* queue pending timers and reschedule them */ |
1185 | /* queue pending timers and reschedule them */ |
1131 | timers_reify (EV_A); /* relative timers called last */ |
1186 | timers_reify (EV_A); /* relative timers called last */ |
1132 | periodics_reify (EV_A); /* absolute timers called first */ |
1187 | periodics_reify (EV_A); /* absolute timers called first */ |
1133 | |
1188 | |
1134 | /* queue idle watchers unless io or timers are pending */ |
1189 | /* queue idle watchers unless io or timers are pending */ |
1135 | if (!pendingcnt) |
1190 | if (idlecnt && !any_pending (EV_A)) |
1136 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1191 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1137 | |
1192 | |
1138 | /* queue check watchers, to be executed first */ |
1193 | /* queue check watchers, to be executed first */ |
1139 | if (checkcnt) |
1194 | if (checkcnt) |
1140 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1195 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
… | |
… | |
1215 | return; |
1270 | return; |
1216 | |
1271 | |
1217 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1272 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1218 | |
1273 | |
1219 | ev_start (EV_A_ (W)w, 1); |
1274 | ev_start (EV_A_ (W)w, 1); |
1220 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
1275 | array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); |
1221 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1276 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1222 | |
1277 | |
1223 | fd_change (EV_A_ fd); |
1278 | fd_change (EV_A_ fd); |
1224 | } |
1279 | } |
1225 | |
1280 | |
… | |
… | |
1245 | ((WT)w)->at += mn_now; |
1300 | ((WT)w)->at += mn_now; |
1246 | |
1301 | |
1247 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1302 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1248 | |
1303 | |
1249 | ev_start (EV_A_ (W)w, ++timercnt); |
1304 | ev_start (EV_A_ (W)w, ++timercnt); |
1250 | array_needsize (timers, timermax, timercnt, (void)); |
1305 | array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); |
1251 | timers [timercnt - 1] = w; |
1306 | timers [timercnt - 1] = w; |
1252 | upheap ((WT *)timers, timercnt - 1); |
1307 | upheap ((WT *)timers, timercnt - 1); |
1253 | |
1308 | |
1254 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1309 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1255 | } |
1310 | } |
… | |
… | |
1278 | ev_timer_again (EV_P_ struct ev_timer *w) |
1333 | ev_timer_again (EV_P_ struct ev_timer *w) |
1279 | { |
1334 | { |
1280 | if (ev_is_active (w)) |
1335 | if (ev_is_active (w)) |
1281 | { |
1336 | { |
1282 | if (w->repeat) |
1337 | if (w->repeat) |
1283 | { |
|
|
1284 | ((WT)w)->at = mn_now + w->repeat; |
|
|
1285 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1338 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); |
1286 | } |
|
|
1287 | else |
1339 | else |
1288 | ev_timer_stop (EV_A_ w); |
1340 | ev_timer_stop (EV_A_ w); |
1289 | } |
1341 | } |
1290 | else if (w->repeat) |
1342 | else if (w->repeat) |
1291 | ev_timer_start (EV_A_ w); |
1343 | ev_timer_start (EV_A_ w); |
… | |
… | |
1295 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1347 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1296 | { |
1348 | { |
1297 | if (ev_is_active (w)) |
1349 | if (ev_is_active (w)) |
1298 | return; |
1350 | return; |
1299 | |
1351 | |
|
|
1352 | if (w->reschedule_cb) |
|
|
1353 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
|
|
1354 | else if (w->interval) |
|
|
1355 | { |
1300 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1356 | 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 */ |
1357 | /* 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; |
1358 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
|
|
1359 | } |
1305 | |
1360 | |
1306 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1361 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1307 | array_needsize (periodics, periodicmax, periodiccnt, (void)); |
1362 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1308 | periodics [periodiccnt - 1] = w; |
1363 | periodics [periodiccnt - 1] = w; |
1309 | upheap ((WT *)periodics, periodiccnt - 1); |
1364 | upheap ((WT *)periodics, periodiccnt - 1); |
1310 | |
1365 | |
1311 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1366 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1312 | } |
1367 | } |
… | |
… | |
1328 | |
1383 | |
1329 | ev_stop (EV_A_ (W)w); |
1384 | ev_stop (EV_A_ (W)w); |
1330 | } |
1385 | } |
1331 | |
1386 | |
1332 | void |
1387 | void |
|
|
1388 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
|
|
1389 | { |
|
|
1390 | /* TODO: use adjustheap and recalculation */ |
|
|
1391 | ev_periodic_stop (EV_A_ w); |
|
|
1392 | ev_periodic_start (EV_A_ w); |
|
|
1393 | } |
|
|
1394 | |
|
|
1395 | void |
1333 | ev_idle_start (EV_P_ struct ev_idle *w) |
1396 | ev_idle_start (EV_P_ struct ev_idle *w) |
1334 | { |
1397 | { |
1335 | if (ev_is_active (w)) |
1398 | if (ev_is_active (w)) |
1336 | return; |
1399 | return; |
1337 | |
1400 | |
1338 | ev_start (EV_A_ (W)w, ++idlecnt); |
1401 | ev_start (EV_A_ (W)w, ++idlecnt); |
1339 | array_needsize (idles, idlemax, idlecnt, (void)); |
1402 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); |
1340 | idles [idlecnt - 1] = w; |
1403 | idles [idlecnt - 1] = w; |
1341 | } |
1404 | } |
1342 | |
1405 | |
1343 | void |
1406 | void |
1344 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1407 | ev_idle_stop (EV_P_ struct ev_idle *w) |
… | |
… | |
1356 | { |
1419 | { |
1357 | if (ev_is_active (w)) |
1420 | if (ev_is_active (w)) |
1358 | return; |
1421 | return; |
1359 | |
1422 | |
1360 | ev_start (EV_A_ (W)w, ++preparecnt); |
1423 | ev_start (EV_A_ (W)w, ++preparecnt); |
1361 | array_needsize (prepares, preparemax, preparecnt, (void)); |
1424 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); |
1362 | prepares [preparecnt - 1] = w; |
1425 | prepares [preparecnt - 1] = w; |
1363 | } |
1426 | } |
1364 | |
1427 | |
1365 | void |
1428 | void |
1366 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1429 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
… | |
… | |
1378 | { |
1441 | { |
1379 | if (ev_is_active (w)) |
1442 | if (ev_is_active (w)) |
1380 | return; |
1443 | return; |
1381 | |
1444 | |
1382 | ev_start (EV_A_ (W)w, ++checkcnt); |
1445 | ev_start (EV_A_ (W)w, ++checkcnt); |
1383 | array_needsize (checks, checkmax, checkcnt, (void)); |
1446 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); |
1384 | checks [checkcnt - 1] = w; |
1447 | checks [checkcnt - 1] = w; |
1385 | } |
1448 | } |
1386 | |
1449 | |
1387 | void |
1450 | void |
1388 | ev_check_stop (EV_P_ struct ev_check *w) |
1451 | ev_check_stop (EV_P_ struct ev_check *w) |
… | |
… | |
1409 | return; |
1472 | return; |
1410 | |
1473 | |
1411 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1474 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1412 | |
1475 | |
1413 | ev_start (EV_A_ (W)w, 1); |
1476 | ev_start (EV_A_ (W)w, 1); |
1414 | array_needsize (signals, signalmax, w->signum, signals_init); |
1477 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1415 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1478 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1416 | |
1479 | |
1417 | if (!((WL)w)->next) |
1480 | if (!((WL)w)->next) |
1418 | { |
1481 | { |
1419 | #if WIN32 |
1482 | #if WIN32 |
… | |
… | |
1502 | } |
1565 | } |
1503 | |
1566 | |
1504 | void |
1567 | void |
1505 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1568 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1506 | { |
1569 | { |
1507 | struct ev_once *once = ev_malloc (sizeof (struct ev_once)); |
1570 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1508 | |
1571 | |
1509 | if (!once) |
1572 | if (!once) |
1510 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1573 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1511 | else |
1574 | else |
1512 | { |
1575 | { |
1513 | once->cb = cb; |
1576 | once->cb = cb; |
1514 | once->arg = arg; |
1577 | once->arg = arg; |
1515 | |
1578 | |
1516 | ev_watcher_init (&once->io, once_cb_io); |
1579 | ev_init (&once->io, once_cb_io); |
1517 | if (fd >= 0) |
1580 | if (fd >= 0) |
1518 | { |
1581 | { |
1519 | ev_io_set (&once->io, fd, events); |
1582 | ev_io_set (&once->io, fd, events); |
1520 | ev_io_start (EV_A_ &once->io); |
1583 | ev_io_start (EV_A_ &once->io); |
1521 | } |
1584 | } |
1522 | |
1585 | |
1523 | ev_watcher_init (&once->to, once_cb_to); |
1586 | ev_init (&once->to, once_cb_to); |
1524 | if (timeout >= 0.) |
1587 | if (timeout >= 0.) |
1525 | { |
1588 | { |
1526 | ev_timer_set (&once->to, timeout, 0.); |
1589 | ev_timer_set (&once->to, timeout, 0.); |
1527 | ev_timer_start (EV_A_ &once->to); |
1590 | ev_timer_start (EV_A_ &once->to); |
1528 | } |
1591 | } |