… | |
… | |
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 |
|
|
40 | # ifndef EV_USE_MONOTONIC |
35 | # define EV_USE_MONOTONIC 1 |
41 | # define EV_USE_MONOTONIC 1 |
|
|
42 | # endif |
|
|
43 | # ifndef EV_USE_REALTIME |
36 | # define EV_USE_REALTIME 1 |
44 | # define EV_USE_REALTIME 1 |
|
|
45 | # endif |
37 | # endif |
46 | # endif |
38 | |
47 | |
39 | # if HAVE_SELECT && HAVE_SYS_SELECT_H |
48 | # if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT) |
40 | # define EV_USE_SELECT 1 |
49 | # define EV_USE_SELECT 1 |
41 | # endif |
50 | # endif |
42 | |
51 | |
43 | # if HAVE_POLL && HAVE_POLL_H |
52 | # if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) |
44 | # define EV_USE_POLL 1 |
53 | # define EV_USE_POLL 1 |
45 | # endif |
54 | # endif |
46 | |
55 | |
47 | # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
56 | # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL) |
48 | # define EV_USE_EPOLL 1 |
57 | # define EV_USE_EPOLL 1 |
49 | # endif |
58 | # endif |
50 | |
59 | |
51 | # if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H |
60 | # if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE) |
52 | # define EV_USE_KQUEUE 1 |
61 | # define EV_USE_KQUEUE 1 |
53 | # endif |
62 | # endif |
54 | |
63 | |
55 | #endif |
64 | #endif |
56 | |
65 | |
… | |
… | |
126 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
135 | #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) */ |
136 | #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 */ |
137 | #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 */ |
138 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
130 | |
139 | |
|
|
140 | #ifdef EV_H |
|
|
141 | # include EV_H |
|
|
142 | #else |
131 | #include "ev.h" |
143 | # include "ev.h" |
|
|
144 | #endif |
132 | |
145 | |
133 | #if __GNUC__ >= 3 |
146 | #if __GNUC__ >= 3 |
134 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
147 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
135 | # define inline inline |
148 | # define inline inline |
136 | #else |
149 | #else |
… | |
… | |
148 | typedef struct ev_watcher_list *WL; |
161 | typedef struct ev_watcher_list *WL; |
149 | typedef struct ev_watcher_time *WT; |
162 | typedef struct ev_watcher_time *WT; |
150 | |
163 | |
151 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
164 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
152 | |
165 | |
|
|
166 | #ifdef WIN32 |
153 | #include "ev_win32.c" |
167 | # include "ev_win32.c" |
|
|
168 | #endif |
154 | |
169 | |
155 | /*****************************************************************************/ |
170 | /*****************************************************************************/ |
156 | |
171 | |
157 | static void (*syserr_cb)(const char *msg); |
172 | static void (*syserr_cb)(const char *msg); |
158 | |
173 | |
… | |
… | |
215 | int events; |
230 | int events; |
216 | } ANPENDING; |
231 | } ANPENDING; |
217 | |
232 | |
218 | #if EV_MULTIPLICITY |
233 | #if EV_MULTIPLICITY |
219 | |
234 | |
220 | struct ev_loop |
235 | struct ev_loop |
221 | { |
236 | { |
|
|
237 | ev_tstamp ev_rt_now; |
222 | # define VAR(name,decl) decl; |
238 | #define VAR(name,decl) decl; |
223 | # include "ev_vars.h" |
239 | #include "ev_vars.h" |
224 | }; |
|
|
225 | # undef VAR |
240 | #undef VAR |
|
|
241 | }; |
226 | # include "ev_wrap.h" |
242 | #include "ev_wrap.h" |
|
|
243 | |
|
|
244 | struct ev_loop default_loop_struct; |
|
|
245 | static struct ev_loop *default_loop; |
227 | |
246 | |
228 | #else |
247 | #else |
229 | |
248 | |
|
|
249 | ev_tstamp ev_rt_now; |
230 | # define VAR(name,decl) static decl; |
250 | #define VAR(name,decl) static decl; |
231 | # include "ev_vars.h" |
251 | #include "ev_vars.h" |
232 | # undef VAR |
252 | #undef VAR |
|
|
253 | |
|
|
254 | static int default_loop; |
233 | |
255 | |
234 | #endif |
256 | #endif |
235 | |
257 | |
236 | /*****************************************************************************/ |
258 | /*****************************************************************************/ |
237 | |
259 | |
238 | inline ev_tstamp |
260 | ev_tstamp |
239 | ev_time (void) |
261 | ev_time (void) |
240 | { |
262 | { |
241 | #if EV_USE_REALTIME |
263 | #if EV_USE_REALTIME |
242 | struct timespec ts; |
264 | struct timespec ts; |
243 | clock_gettime (CLOCK_REALTIME, &ts); |
265 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
262 | #endif |
284 | #endif |
263 | |
285 | |
264 | return ev_time (); |
286 | return ev_time (); |
265 | } |
287 | } |
266 | |
288 | |
|
|
289 | #if EV_MULTIPLICITY |
267 | ev_tstamp |
290 | ev_tstamp |
268 | ev_now (EV_P) |
291 | ev_now (EV_P) |
269 | { |
292 | { |
270 | return rt_now; |
293 | return ev_rt_now; |
271 | } |
294 | } |
|
|
295 | #endif |
272 | |
296 | |
273 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
297 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
274 | |
298 | |
275 | #define array_needsize(type,base,cur,cnt,init) \ |
299 | #define array_needsize(type,base,cur,cnt,init) \ |
276 | if (expect_false ((cnt) > cur)) \ |
300 | if (expect_false ((cnt) > cur)) \ |
… | |
… | |
316 | |
340 | |
317 | ++base; |
341 | ++base; |
318 | } |
342 | } |
319 | } |
343 | } |
320 | |
344 | |
321 | static void |
345 | void |
322 | event (EV_P_ W w, int events) |
346 | ev_feed_event (EV_P_ void *w, int revents) |
323 | { |
347 | { |
|
|
348 | W w_ = (W)w; |
|
|
349 | |
324 | if (w->pending) |
350 | if (w_->pending) |
325 | { |
351 | { |
326 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
352 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
327 | return; |
353 | return; |
328 | } |
354 | } |
329 | |
355 | |
330 | w->pending = ++pendingcnt [ABSPRI (w)]; |
356 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
331 | array_needsize (ANPENDING, pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); |
357 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); |
332 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
358 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
333 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
359 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
334 | } |
360 | } |
335 | |
361 | |
336 | static void |
362 | static void |
337 | queue_events (EV_P_ W *events, int eventcnt, int type) |
363 | queue_events (EV_P_ W *events, int eventcnt, int type) |
338 | { |
364 | { |
339 | int i; |
365 | int i; |
340 | |
366 | |
341 | for (i = 0; i < eventcnt; ++i) |
367 | for (i = 0; i < eventcnt; ++i) |
342 | event (EV_A_ events [i], type); |
368 | ev_feed_event (EV_A_ events [i], type); |
343 | } |
369 | } |
344 | |
370 | |
345 | static void |
371 | inline void |
346 | fd_event (EV_P_ int fd, int events) |
372 | fd_event (EV_P_ int fd, int revents) |
347 | { |
373 | { |
348 | ANFD *anfd = anfds + fd; |
374 | ANFD *anfd = anfds + fd; |
349 | struct ev_io *w; |
375 | struct ev_io *w; |
350 | |
376 | |
351 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
377 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
352 | { |
378 | { |
353 | int ev = w->events & events; |
379 | int ev = w->events & revents; |
354 | |
380 | |
355 | if (ev) |
381 | if (ev) |
356 | event (EV_A_ (W)w, ev); |
382 | ev_feed_event (EV_A_ (W)w, ev); |
357 | } |
383 | } |
|
|
384 | } |
|
|
385 | |
|
|
386 | void |
|
|
387 | ev_feed_fd_event (EV_P_ int fd, int revents) |
|
|
388 | { |
|
|
389 | fd_event (EV_A_ fd, revents); |
358 | } |
390 | } |
359 | |
391 | |
360 | /*****************************************************************************/ |
392 | /*****************************************************************************/ |
361 | |
393 | |
362 | static void |
394 | static void |
… | |
… | |
403 | struct ev_io *w; |
435 | struct ev_io *w; |
404 | |
436 | |
405 | while ((w = (struct ev_io *)anfds [fd].head)) |
437 | while ((w = (struct ev_io *)anfds [fd].head)) |
406 | { |
438 | { |
407 | ev_io_stop (EV_A_ w); |
439 | ev_io_stop (EV_A_ w); |
408 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
440 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
409 | } |
441 | } |
410 | } |
442 | } |
411 | |
443 | |
412 | static int |
444 | static int |
413 | fd_valid (int fd) |
445 | fd_valid (int fd) |
… | |
… | |
501 | |
533 | |
502 | heap [k] = w; |
534 | heap [k] = w; |
503 | ((W)heap [k])->active = k + 1; |
535 | ((W)heap [k])->active = k + 1; |
504 | } |
536 | } |
505 | |
537 | |
|
|
538 | inline void |
|
|
539 | adjustheap (WT *heap, int N, int k, ev_tstamp at) |
|
|
540 | { |
|
|
541 | ev_tstamp old_at = heap [k]->at; |
|
|
542 | heap [k]->at = at; |
|
|
543 | |
|
|
544 | if (old_at < at) |
|
|
545 | downheap (heap, N, k); |
|
|
546 | else |
|
|
547 | upheap (heap, k); |
|
|
548 | } |
|
|
549 | |
506 | /*****************************************************************************/ |
550 | /*****************************************************************************/ |
507 | |
551 | |
508 | typedef struct |
552 | typedef struct |
509 | { |
553 | { |
510 | WL head; |
554 | WL head; |
… | |
… | |
550 | #endif |
594 | #endif |
551 | errno = old_errno; |
595 | errno = old_errno; |
552 | } |
596 | } |
553 | } |
597 | } |
554 | |
598 | |
|
|
599 | void |
|
|
600 | ev_feed_signal_event (EV_P_ int signum) |
|
|
601 | { |
|
|
602 | WL w; |
|
|
603 | |
|
|
604 | #if EV_MULTIPLICITY |
|
|
605 | assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
|
|
606 | #endif |
|
|
607 | |
|
|
608 | --signum; |
|
|
609 | |
|
|
610 | if (signum < 0 || signum >= signalmax) |
|
|
611 | return; |
|
|
612 | |
|
|
613 | signals [signum].gotsig = 0; |
|
|
614 | |
|
|
615 | for (w = signals [signum].head; w; w = w->next) |
|
|
616 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
|
|
617 | } |
|
|
618 | |
555 | static void |
619 | static void |
556 | sigcb (EV_P_ struct ev_io *iow, int revents) |
620 | sigcb (EV_P_ struct ev_io *iow, int revents) |
557 | { |
621 | { |
558 | WL w; |
|
|
559 | int signum; |
622 | int signum; |
560 | |
623 | |
561 | #ifdef WIN32 |
624 | #ifdef WIN32 |
562 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
625 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
563 | #else |
626 | #else |
… | |
… | |
565 | #endif |
628 | #endif |
566 | gotsig = 0; |
629 | gotsig = 0; |
567 | |
630 | |
568 | for (signum = signalmax; signum--; ) |
631 | for (signum = signalmax; signum--; ) |
569 | if (signals [signum].gotsig) |
632 | if (signals [signum].gotsig) |
570 | { |
633 | ev_feed_signal_event (EV_A_ signum + 1); |
571 | signals [signum].gotsig = 0; |
|
|
572 | |
|
|
573 | for (w = signals [signum].head; w; w = w->next) |
|
|
574 | event (EV_A_ (W)w, EV_SIGNAL); |
|
|
575 | } |
|
|
576 | } |
634 | } |
577 | |
635 | |
578 | static void |
636 | static void |
579 | siginit (EV_P) |
637 | siginit (EV_P) |
580 | { |
638 | { |
… | |
… | |
613 | if (w->pid == pid || !w->pid) |
671 | if (w->pid == pid || !w->pid) |
614 | { |
672 | { |
615 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
673 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
616 | w->rpid = pid; |
674 | w->rpid = pid; |
617 | w->rstatus = status; |
675 | w->rstatus = status; |
618 | event (EV_A_ (W)w, EV_CHILD); |
676 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
619 | } |
677 | } |
620 | } |
678 | } |
621 | |
679 | |
622 | static void |
680 | static void |
623 | childcb (EV_P_ struct ev_signal *sw, int revents) |
681 | childcb (EV_P_ struct ev_signal *sw, int revents) |
… | |
… | |
625 | int pid, status; |
683 | int pid, status; |
626 | |
684 | |
627 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
685 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
628 | { |
686 | { |
629 | /* make sure we are called again until all childs have been reaped */ |
687 | /* make sure we are called again until all childs have been reaped */ |
630 | event (EV_A_ (W)sw, EV_SIGNAL); |
688 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
631 | |
689 | |
632 | child_reap (EV_A_ sw, pid, pid, status); |
690 | child_reap (EV_A_ sw, pid, pid, status); |
633 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
691 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
634 | } |
692 | } |
635 | } |
693 | } |
… | |
… | |
692 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
750 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
693 | have_monotonic = 1; |
751 | have_monotonic = 1; |
694 | } |
752 | } |
695 | #endif |
753 | #endif |
696 | |
754 | |
697 | rt_now = ev_time (); |
755 | ev_rt_now = ev_time (); |
698 | mn_now = get_clock (); |
756 | mn_now = get_clock (); |
699 | now_floor = mn_now; |
757 | now_floor = mn_now; |
700 | rtmn_diff = rt_now - mn_now; |
758 | rtmn_diff = ev_rt_now - mn_now; |
701 | |
759 | |
702 | if (methods == EVMETHOD_AUTO) |
760 | if (methods == EVMETHOD_AUTO) |
703 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
761 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
704 | methods = atoi (getenv ("LIBEV_METHODS")); |
762 | methods = atoi (getenv ("LIBEV_METHODS")); |
705 | else |
763 | else |
… | |
… | |
720 | #endif |
778 | #endif |
721 | #if EV_USE_SELECT |
779 | #if EV_USE_SELECT |
722 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
780 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
723 | #endif |
781 | #endif |
724 | |
782 | |
725 | ev_watcher_init (&sigev, sigcb); |
783 | ev_init (&sigev, sigcb); |
726 | ev_set_priority (&sigev, EV_MAXPRI); |
784 | ev_set_priority (&sigev, EV_MAXPRI); |
727 | } |
785 | } |
728 | } |
786 | } |
729 | |
787 | |
730 | void |
788 | void |
… | |
… | |
752 | array_free (pending, [i]); |
810 | array_free (pending, [i]); |
753 | |
811 | |
754 | /* have to use the microsoft-never-gets-it-right macro */ |
812 | /* have to use the microsoft-never-gets-it-right macro */ |
755 | array_free_microshit (fdchange); |
813 | array_free_microshit (fdchange); |
756 | array_free_microshit (timer); |
814 | array_free_microshit (timer); |
|
|
815 | #if EV_PERIODICS |
757 | array_free_microshit (periodic); |
816 | array_free_microshit (periodic); |
|
|
817 | #endif |
758 | array_free_microshit (idle); |
818 | array_free_microshit (idle); |
759 | array_free_microshit (prepare); |
819 | array_free_microshit (prepare); |
760 | array_free_microshit (check); |
820 | array_free_microshit (check); |
761 | |
821 | |
762 | method = 0; |
822 | method = 0; |
… | |
… | |
820 | } |
880 | } |
821 | |
881 | |
822 | #endif |
882 | #endif |
823 | |
883 | |
824 | #if EV_MULTIPLICITY |
884 | #if EV_MULTIPLICITY |
825 | struct ev_loop default_loop_struct; |
|
|
826 | static struct ev_loop *default_loop; |
|
|
827 | |
|
|
828 | struct ev_loop * |
885 | struct ev_loop * |
829 | #else |
886 | #else |
830 | static int default_loop; |
|
|
831 | |
|
|
832 | int |
887 | int |
833 | #endif |
888 | #endif |
834 | ev_default_loop (int methods) |
889 | ev_default_loop (int methods) |
835 | { |
890 | { |
836 | if (sigpipe [0] == sigpipe [1]) |
891 | if (sigpipe [0] == sigpipe [1]) |
… | |
… | |
922 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
977 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
923 | |
978 | |
924 | if (p->w) |
979 | if (p->w) |
925 | { |
980 | { |
926 | p->w->pending = 0; |
981 | p->w->pending = 0; |
927 | p->w->cb (EV_A_ p->w, p->events); |
982 | EV_CB_INVOKE (p->w, p->events); |
928 | } |
983 | } |
929 | } |
984 | } |
930 | } |
985 | } |
931 | |
986 | |
932 | static void |
987 | static void |
… | |
… | |
940 | |
995 | |
941 | /* first reschedule or stop timer */ |
996 | /* first reschedule or stop timer */ |
942 | if (w->repeat) |
997 | if (w->repeat) |
943 | { |
998 | { |
944 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
999 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
|
|
1000 | |
945 | ((WT)w)->at = mn_now + w->repeat; |
1001 | ((WT)w)->at += w->repeat; |
|
|
1002 | if (((WT)w)->at < mn_now) |
|
|
1003 | ((WT)w)->at = mn_now; |
|
|
1004 | |
946 | downheap ((WT *)timers, timercnt, 0); |
1005 | downheap ((WT *)timers, timercnt, 0); |
947 | } |
1006 | } |
948 | else |
1007 | else |
949 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1008 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
950 | |
1009 | |
951 | event (EV_A_ (W)w, EV_TIMEOUT); |
1010 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
952 | } |
1011 | } |
953 | } |
1012 | } |
954 | |
1013 | |
|
|
1014 | #if EV_PERIODICS |
955 | static void |
1015 | static void |
956 | periodics_reify (EV_P) |
1016 | periodics_reify (EV_P) |
957 | { |
1017 | { |
958 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
1018 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
959 | { |
1019 | { |
960 | struct ev_periodic *w = periodics [0]; |
1020 | struct ev_periodic *w = periodics [0]; |
961 | |
1021 | |
962 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1022 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
963 | |
1023 | |
964 | /* first reschedule or stop timer */ |
1024 | /* first reschedule or stop timer */ |
965 | if (w->reschedule_cb) |
1025 | if (w->reschedule_cb) |
966 | { |
1026 | { |
967 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); |
1027 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
968 | |
1028 | |
969 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); |
1029 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
970 | downheap ((WT *)periodics, periodiccnt, 0); |
1030 | downheap ((WT *)periodics, periodiccnt, 0); |
971 | } |
1031 | } |
972 | else if (w->interval) |
1032 | else if (w->interval) |
973 | { |
1033 | { |
974 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1034 | ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
975 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
1035 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); |
976 | downheap ((WT *)periodics, periodiccnt, 0); |
1036 | downheap ((WT *)periodics, periodiccnt, 0); |
977 | } |
1037 | } |
978 | else |
1038 | else |
979 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1039 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
980 | |
1040 | |
981 | event (EV_A_ (W)w, EV_PERIODIC); |
1041 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
982 | } |
1042 | } |
983 | } |
1043 | } |
984 | |
1044 | |
985 | static void |
1045 | static void |
986 | periodics_reschedule (EV_P) |
1046 | periodics_reschedule (EV_P) |
… | |
… | |
991 | for (i = 0; i < periodiccnt; ++i) |
1051 | for (i = 0; i < periodiccnt; ++i) |
992 | { |
1052 | { |
993 | struct ev_periodic *w = periodics [i]; |
1053 | struct ev_periodic *w = periodics [i]; |
994 | |
1054 | |
995 | if (w->reschedule_cb) |
1055 | if (w->reschedule_cb) |
996 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
1056 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
997 | else if (w->interval) |
1057 | else if (w->interval) |
998 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1058 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
999 | } |
1059 | } |
1000 | |
1060 | |
1001 | /* now rebuild the heap */ |
1061 | /* now rebuild the heap */ |
1002 | for (i = periodiccnt >> 1; i--; ) |
1062 | for (i = periodiccnt >> 1; i--; ) |
1003 | downheap ((WT *)periodics, periodiccnt, i); |
1063 | downheap ((WT *)periodics, periodiccnt, i); |
1004 | } |
1064 | } |
|
|
1065 | #endif |
1005 | |
1066 | |
1006 | inline int |
1067 | inline int |
1007 | time_update_monotonic (EV_P) |
1068 | time_update_monotonic (EV_P) |
1008 | { |
1069 | { |
1009 | mn_now = get_clock (); |
1070 | mn_now = get_clock (); |
1010 | |
1071 | |
1011 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1072 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1012 | { |
1073 | { |
1013 | rt_now = rtmn_diff + mn_now; |
1074 | ev_rt_now = rtmn_diff + mn_now; |
1014 | return 0; |
1075 | return 0; |
1015 | } |
1076 | } |
1016 | else |
1077 | else |
1017 | { |
1078 | { |
1018 | now_floor = mn_now; |
1079 | now_floor = mn_now; |
1019 | rt_now = ev_time (); |
1080 | ev_rt_now = ev_time (); |
1020 | return 1; |
1081 | return 1; |
1021 | } |
1082 | } |
1022 | } |
1083 | } |
1023 | |
1084 | |
1024 | static void |
1085 | static void |
… | |
… | |
1033 | { |
1094 | { |
1034 | ev_tstamp odiff = rtmn_diff; |
1095 | ev_tstamp odiff = rtmn_diff; |
1035 | |
1096 | |
1036 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1097 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1037 | { |
1098 | { |
1038 | rtmn_diff = rt_now - mn_now; |
1099 | rtmn_diff = ev_rt_now - mn_now; |
1039 | |
1100 | |
1040 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1101 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1041 | return; /* all is well */ |
1102 | return; /* all is well */ |
1042 | |
1103 | |
1043 | rt_now = ev_time (); |
1104 | ev_rt_now = ev_time (); |
1044 | mn_now = get_clock (); |
1105 | mn_now = get_clock (); |
1045 | now_floor = mn_now; |
1106 | now_floor = mn_now; |
1046 | } |
1107 | } |
1047 | |
1108 | |
|
|
1109 | # if EV_PERIODICS |
1048 | periodics_reschedule (EV_A); |
1110 | periodics_reschedule (EV_A); |
|
|
1111 | # endif |
1049 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1112 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1050 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1113 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1051 | } |
1114 | } |
1052 | } |
1115 | } |
1053 | else |
1116 | else |
1054 | #endif |
1117 | #endif |
1055 | { |
1118 | { |
1056 | rt_now = ev_time (); |
1119 | ev_rt_now = ev_time (); |
1057 | |
1120 | |
1058 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1121 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1059 | { |
1122 | { |
|
|
1123 | #if EV_PERIODICS |
1060 | periodics_reschedule (EV_A); |
1124 | periodics_reschedule (EV_A); |
|
|
1125 | #endif |
1061 | |
1126 | |
1062 | /* adjust timers. this is easy, as the offset is the same for all */ |
1127 | /* adjust timers. this is easy, as the offset is the same for all */ |
1063 | for (i = 0; i < timercnt; ++i) |
1128 | for (i = 0; i < timercnt; ++i) |
1064 | ((WT)timers [i])->at += rt_now - mn_now; |
1129 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1065 | } |
1130 | } |
1066 | |
1131 | |
1067 | mn_now = rt_now; |
1132 | mn_now = ev_rt_now; |
1068 | } |
1133 | } |
1069 | } |
1134 | } |
1070 | |
1135 | |
1071 | void |
1136 | void |
1072 | ev_ref (EV_P) |
1137 | ev_ref (EV_P) |
… | |
… | |
1112 | if (expect_true (have_monotonic)) |
1177 | if (expect_true (have_monotonic)) |
1113 | time_update_monotonic (EV_A); |
1178 | time_update_monotonic (EV_A); |
1114 | else |
1179 | else |
1115 | #endif |
1180 | #endif |
1116 | { |
1181 | { |
1117 | rt_now = ev_time (); |
1182 | ev_rt_now = ev_time (); |
1118 | mn_now = rt_now; |
1183 | mn_now = ev_rt_now; |
1119 | } |
1184 | } |
1120 | |
1185 | |
1121 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1186 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1122 | block = 0.; |
1187 | block = 0.; |
1123 | else |
1188 | else |
… | |
… | |
1128 | { |
1193 | { |
1129 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1194 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1130 | if (block > to) block = to; |
1195 | if (block > to) block = to; |
1131 | } |
1196 | } |
1132 | |
1197 | |
|
|
1198 | #if EV_PERIODICS |
1133 | if (periodiccnt) |
1199 | if (periodiccnt) |
1134 | { |
1200 | { |
1135 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
1201 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1136 | if (block > to) block = to; |
1202 | if (block > to) block = to; |
1137 | } |
1203 | } |
|
|
1204 | #endif |
1138 | |
1205 | |
1139 | if (block < 0.) block = 0.; |
1206 | if (block < 0.) block = 0.; |
1140 | } |
1207 | } |
1141 | |
1208 | |
1142 | method_poll (EV_A_ block); |
1209 | method_poll (EV_A_ block); |
1143 | |
1210 | |
1144 | /* update rt_now, do magic */ |
1211 | /* update ev_rt_now, do magic */ |
1145 | time_update (EV_A); |
1212 | time_update (EV_A); |
1146 | |
1213 | |
1147 | /* queue pending timers and reschedule them */ |
1214 | /* queue pending timers and reschedule them */ |
1148 | timers_reify (EV_A); /* relative timers called last */ |
1215 | timers_reify (EV_A); /* relative timers called last */ |
|
|
1216 | #if EV_PERIODICS |
1149 | periodics_reify (EV_A); /* absolute timers called first */ |
1217 | periodics_reify (EV_A); /* absolute timers called first */ |
|
|
1218 | #endif |
1150 | |
1219 | |
1151 | /* queue idle watchers unless io or timers are pending */ |
1220 | /* queue idle watchers unless io or timers are pending */ |
1152 | if (idlecnt && !any_pending (EV_A)) |
1221 | if (idlecnt && !any_pending (EV_A)) |
1153 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1222 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1154 | |
1223 | |
… | |
… | |
1245 | { |
1314 | { |
1246 | ev_clear_pending (EV_A_ (W)w); |
1315 | ev_clear_pending (EV_A_ (W)w); |
1247 | if (!ev_is_active (w)) |
1316 | if (!ev_is_active (w)) |
1248 | return; |
1317 | return; |
1249 | |
1318 | |
|
|
1319 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
|
|
1320 | |
1250 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1321 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1251 | ev_stop (EV_A_ (W)w); |
1322 | ev_stop (EV_A_ (W)w); |
1252 | |
1323 | |
1253 | fd_change (EV_A_ w->fd); |
1324 | fd_change (EV_A_ w->fd); |
1254 | } |
1325 | } |
… | |
… | |
1284 | { |
1355 | { |
1285 | timers [((W)w)->active - 1] = timers [timercnt]; |
1356 | timers [((W)w)->active - 1] = timers [timercnt]; |
1286 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1357 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1287 | } |
1358 | } |
1288 | |
1359 | |
1289 | ((WT)w)->at = w->repeat; |
1360 | ((WT)w)->at -= mn_now; |
1290 | |
1361 | |
1291 | ev_stop (EV_A_ (W)w); |
1362 | ev_stop (EV_A_ (W)w); |
1292 | } |
1363 | } |
1293 | |
1364 | |
1294 | void |
1365 | void |
1295 | ev_timer_again (EV_P_ struct ev_timer *w) |
1366 | ev_timer_again (EV_P_ struct ev_timer *w) |
1296 | { |
1367 | { |
1297 | if (ev_is_active (w)) |
1368 | if (ev_is_active (w)) |
1298 | { |
1369 | { |
1299 | if (w->repeat) |
1370 | if (w->repeat) |
1300 | { |
|
|
1301 | ((WT)w)->at = mn_now + w->repeat; |
|
|
1302 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1371 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); |
1303 | } |
|
|
1304 | else |
1372 | else |
1305 | ev_timer_stop (EV_A_ w); |
1373 | ev_timer_stop (EV_A_ w); |
1306 | } |
1374 | } |
1307 | else if (w->repeat) |
1375 | else if (w->repeat) |
1308 | ev_timer_start (EV_A_ w); |
1376 | ev_timer_start (EV_A_ w); |
1309 | } |
1377 | } |
1310 | |
1378 | |
|
|
1379 | #if EV_PERIODICS |
1311 | void |
1380 | void |
1312 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1381 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1313 | { |
1382 | { |
1314 | if (ev_is_active (w)) |
1383 | if (ev_is_active (w)) |
1315 | return; |
1384 | return; |
1316 | |
1385 | |
1317 | if (w->reschedule_cb) |
1386 | if (w->reschedule_cb) |
1318 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
1387 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1319 | else if (w->interval) |
1388 | else if (w->interval) |
1320 | { |
1389 | { |
1321 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1390 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1322 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1391 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1323 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1392 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1324 | } |
1393 | } |
1325 | |
1394 | |
1326 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1395 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1327 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1396 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1328 | periodics [periodiccnt - 1] = w; |
1397 | periodics [periodiccnt - 1] = w; |
… | |
… | |
1350 | } |
1419 | } |
1351 | |
1420 | |
1352 | void |
1421 | void |
1353 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
1422 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
1354 | { |
1423 | { |
|
|
1424 | /* TODO: use adjustheap and recalculation */ |
1355 | ev_periodic_stop (EV_A_ w); |
1425 | ev_periodic_stop (EV_A_ w); |
1356 | ev_periodic_start (EV_A_ w); |
1426 | ev_periodic_start (EV_A_ w); |
1357 | } |
1427 | } |
|
|
1428 | #endif |
1358 | |
1429 | |
1359 | void |
1430 | void |
1360 | ev_idle_start (EV_P_ struct ev_idle *w) |
1431 | ev_idle_start (EV_P_ struct ev_idle *w) |
1361 | { |
1432 | { |
1362 | if (ev_is_active (w)) |
1433 | if (ev_is_active (w)) |
… | |
… | |
1413 | |
1484 | |
1414 | void |
1485 | void |
1415 | ev_check_stop (EV_P_ struct ev_check *w) |
1486 | ev_check_stop (EV_P_ struct ev_check *w) |
1416 | { |
1487 | { |
1417 | ev_clear_pending (EV_A_ (W)w); |
1488 | ev_clear_pending (EV_A_ (W)w); |
1418 | if (ev_is_active (w)) |
1489 | if (!ev_is_active (w)) |
1419 | return; |
1490 | return; |
1420 | |
1491 | |
1421 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1492 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1422 | ev_stop (EV_A_ (W)w); |
1493 | ev_stop (EV_A_ (W)w); |
1423 | } |
1494 | } |
… | |
… | |
1484 | |
1555 | |
1485 | void |
1556 | void |
1486 | ev_child_stop (EV_P_ struct ev_child *w) |
1557 | ev_child_stop (EV_P_ struct ev_child *w) |
1487 | { |
1558 | { |
1488 | ev_clear_pending (EV_A_ (W)w); |
1559 | ev_clear_pending (EV_A_ (W)w); |
1489 | if (ev_is_active (w)) |
1560 | if (!ev_is_active (w)) |
1490 | return; |
1561 | return; |
1491 | |
1562 | |
1492 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1563 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1493 | ev_stop (EV_A_ (W)w); |
1564 | ev_stop (EV_A_ (W)w); |
1494 | } |
1565 | } |
… | |
… | |
1538 | else |
1609 | else |
1539 | { |
1610 | { |
1540 | once->cb = cb; |
1611 | once->cb = cb; |
1541 | once->arg = arg; |
1612 | once->arg = arg; |
1542 | |
1613 | |
1543 | ev_watcher_init (&once->io, once_cb_io); |
1614 | ev_init (&once->io, once_cb_io); |
1544 | if (fd >= 0) |
1615 | if (fd >= 0) |
1545 | { |
1616 | { |
1546 | ev_io_set (&once->io, fd, events); |
1617 | ev_io_set (&once->io, fd, events); |
1547 | ev_io_start (EV_A_ &once->io); |
1618 | ev_io_start (EV_A_ &once->io); |
1548 | } |
1619 | } |
1549 | |
1620 | |
1550 | ev_watcher_init (&once->to, once_cb_to); |
1621 | ev_init (&once->to, once_cb_to); |
1551 | if (timeout >= 0.) |
1622 | if (timeout >= 0.) |
1552 | { |
1623 | { |
1553 | ev_timer_set (&once->to, timeout, 0.); |
1624 | ev_timer_set (&once->to, timeout, 0.); |
1554 | ev_timer_start (EV_A_ &once->to); |
1625 | ev_timer_start (EV_A_ &once->to); |
1555 | } |
1626 | } |
1556 | } |
1627 | } |
1557 | } |
1628 | } |
1558 | |
1629 | |
|
|
1630 | #ifdef __cplusplus |
|
|
1631 | } |
|
|
1632 | #endif |
|
|
1633 | |