… | |
… | |
54 | |
54 | |
55 | #endif |
55 | #endif |
56 | |
56 | |
57 | #include <math.h> |
57 | #include <math.h> |
58 | #include <stdlib.h> |
58 | #include <stdlib.h> |
59 | #include <unistd.h> |
|
|
60 | #include <fcntl.h> |
59 | #include <fcntl.h> |
61 | #include <signal.h> |
|
|
62 | #include <stddef.h> |
60 | #include <stddef.h> |
63 | |
61 | |
64 | #include <stdio.h> |
62 | #include <stdio.h> |
65 | |
63 | |
66 | #include <assert.h> |
64 | #include <assert.h> |
67 | #include <errno.h> |
65 | #include <errno.h> |
68 | #include <sys/types.h> |
66 | #include <sys/types.h> |
|
|
67 | #include <time.h> |
|
|
68 | |
|
|
69 | #include <signal.h> |
|
|
70 | |
69 | #ifndef WIN32 |
71 | #ifndef WIN32 |
|
|
72 | # include <unistd.h> |
|
|
73 | # include <sys/time.h> |
70 | # include <sys/wait.h> |
74 | # include <sys/wait.h> |
71 | #endif |
75 | #endif |
72 | #include <sys/time.h> |
|
|
73 | #include <time.h> |
|
|
74 | |
|
|
75 | /**/ |
76 | /**/ |
76 | |
77 | |
77 | #ifndef EV_USE_MONOTONIC |
78 | #ifndef EV_USE_MONOTONIC |
78 | # define EV_USE_MONOTONIC 1 |
79 | # define EV_USE_MONOTONIC 1 |
79 | #endif |
80 | #endif |
… | |
… | |
94 | # define EV_USE_KQUEUE 0 |
95 | # define EV_USE_KQUEUE 0 |
95 | #endif |
96 | #endif |
96 | |
97 | |
97 | #ifndef EV_USE_WIN32 |
98 | #ifndef EV_USE_WIN32 |
98 | # ifdef WIN32 |
99 | # ifdef WIN32 |
|
|
100 | # define EV_USE_WIN32 0 /* it does not exist, use select */ |
|
|
101 | # undef EV_USE_SELECT |
99 | # define EV_USE_WIN32 1 |
102 | # define EV_USE_SELECT 1 |
100 | # else |
103 | # else |
101 | # define EV_USE_WIN32 0 |
104 | # define EV_USE_WIN32 0 |
102 | # endif |
105 | # endif |
103 | #endif |
106 | #endif |
104 | |
107 | |
… | |
… | |
145 | typedef struct ev_watcher_list *WL; |
148 | typedef struct ev_watcher_list *WL; |
146 | typedef struct ev_watcher_time *WT; |
149 | typedef struct ev_watcher_time *WT; |
147 | |
150 | |
148 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
151 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
149 | |
152 | |
150 | #if WIN32 |
153 | #include "ev_win32.c" |
151 | /* note: the comment below could not be substantiated, but what would I care */ |
|
|
152 | /* MSDN says this is required to handle SIGFPE */ |
|
|
153 | volatile double SIGFPE_REQ = 0.0f; |
|
|
154 | #endif |
|
|
155 | |
154 | |
156 | /*****************************************************************************/ |
155 | /*****************************************************************************/ |
157 | |
156 | |
158 | static void (*syserr_cb)(const char *msg); |
157 | static void (*syserr_cb)(const char *msg); |
159 | |
158 | |
… | |
… | |
269 | ev_now (EV_P) |
268 | ev_now (EV_P) |
270 | { |
269 | { |
271 | return rt_now; |
270 | return rt_now; |
272 | } |
271 | } |
273 | |
272 | |
274 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
273 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
275 | |
274 | |
276 | #define array_needsize(base,cur,cnt,init) \ |
275 | #define array_needsize(type,base,cur,cnt,init) \ |
277 | if (expect_false ((cnt) > cur)) \ |
276 | if (expect_false ((cnt) > cur)) \ |
278 | { \ |
277 | { \ |
279 | int newcnt = cur; \ |
278 | int newcnt = cur; \ |
280 | do \ |
279 | do \ |
281 | { \ |
280 | { \ |
282 | newcnt = array_roundsize (base, newcnt << 1); \ |
281 | newcnt = array_roundsize (type, newcnt << 1); \ |
283 | } \ |
282 | } \ |
284 | while ((cnt) > newcnt); \ |
283 | while ((cnt) > newcnt); \ |
285 | \ |
284 | \ |
286 | base = ev_realloc (base, sizeof (*base) * (newcnt)); \ |
285 | base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\ |
287 | init (base + cur, newcnt - cur); \ |
286 | init (base + cur, newcnt - cur); \ |
288 | cur = newcnt; \ |
287 | cur = newcnt; \ |
289 | } |
288 | } |
290 | |
289 | |
291 | #define array_slim(stem) \ |
290 | #define array_slim(type,stem) \ |
292 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
291 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
293 | { \ |
292 | { \ |
294 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
293 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
295 | base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ |
294 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
296 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
295 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
297 | } |
296 | } |
|
|
297 | |
|
|
298 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
|
|
299 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
|
|
300 | #define array_free_microshit(stem) \ |
|
|
301 | ev_free (stem ## s); stem ## cnt = stem ## max = 0; |
298 | |
302 | |
299 | #define array_free(stem, idx) \ |
303 | #define array_free(stem, idx) \ |
300 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
304 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
301 | |
305 | |
302 | /*****************************************************************************/ |
306 | /*****************************************************************************/ |
… | |
… | |
312 | |
316 | |
313 | ++base; |
317 | ++base; |
314 | } |
318 | } |
315 | } |
319 | } |
316 | |
320 | |
317 | static void |
321 | void |
318 | event (EV_P_ W w, int events) |
322 | ev_feed_event (EV_P_ void *w, int revents) |
319 | { |
323 | { |
|
|
324 | W w_ = (W)w; |
|
|
325 | |
320 | if (w->pending) |
326 | if (w_->pending) |
321 | { |
327 | { |
322 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
328 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
323 | return; |
329 | return; |
324 | } |
330 | } |
325 | |
331 | |
326 | w->pending = ++pendingcnt [ABSPRI (w)]; |
332 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
327 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
333 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); |
328 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
334 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
329 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
335 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
330 | } |
336 | } |
331 | |
337 | |
332 | static void |
338 | static void |
333 | queue_events (EV_P_ W *events, int eventcnt, int type) |
339 | queue_events (EV_P_ W *events, int eventcnt, int type) |
334 | { |
340 | { |
335 | int i; |
341 | int i; |
336 | |
342 | |
337 | for (i = 0; i < eventcnt; ++i) |
343 | for (i = 0; i < eventcnt; ++i) |
338 | event (EV_A_ events [i], type); |
344 | ev_feed_event (EV_A_ events [i], type); |
339 | } |
345 | } |
340 | |
346 | |
341 | static void |
347 | inline void |
342 | fd_event (EV_P_ int fd, int events) |
348 | fd_event (EV_P_ int fd, int revents) |
343 | { |
349 | { |
344 | ANFD *anfd = anfds + fd; |
350 | ANFD *anfd = anfds + fd; |
345 | struct ev_io *w; |
351 | struct ev_io *w; |
346 | |
352 | |
347 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
353 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
348 | { |
354 | { |
349 | int ev = w->events & events; |
355 | int ev = w->events & revents; |
350 | |
356 | |
351 | if (ev) |
357 | if (ev) |
352 | event (EV_A_ (W)w, ev); |
358 | ev_feed_event (EV_A_ (W)w, ev); |
353 | } |
359 | } |
|
|
360 | } |
|
|
361 | |
|
|
362 | void |
|
|
363 | ev_feed_fd_event (EV_P_ int fd, int revents) |
|
|
364 | { |
|
|
365 | fd_event (EV_A_ fd, revents); |
354 | } |
366 | } |
355 | |
367 | |
356 | /*****************************************************************************/ |
368 | /*****************************************************************************/ |
357 | |
369 | |
358 | static void |
370 | static void |
… | |
… | |
387 | return; |
399 | return; |
388 | |
400 | |
389 | anfds [fd].reify = 1; |
401 | anfds [fd].reify = 1; |
390 | |
402 | |
391 | ++fdchangecnt; |
403 | ++fdchangecnt; |
392 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
404 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); |
393 | fdchanges [fdchangecnt - 1] = fd; |
405 | fdchanges [fdchangecnt - 1] = fd; |
394 | } |
406 | } |
395 | |
407 | |
396 | static void |
408 | static void |
397 | fd_kill (EV_P_ int fd) |
409 | fd_kill (EV_P_ int fd) |
… | |
… | |
399 | struct ev_io *w; |
411 | struct ev_io *w; |
400 | |
412 | |
401 | while ((w = (struct ev_io *)anfds [fd].head)) |
413 | while ((w = (struct ev_io *)anfds [fd].head)) |
402 | { |
414 | { |
403 | ev_io_stop (EV_A_ w); |
415 | ev_io_stop (EV_A_ w); |
404 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
416 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
405 | } |
417 | } |
|
|
418 | } |
|
|
419 | |
|
|
420 | static int |
|
|
421 | fd_valid (int fd) |
|
|
422 | { |
|
|
423 | #ifdef WIN32 |
|
|
424 | return !!win32_get_osfhandle (fd); |
|
|
425 | #else |
|
|
426 | return fcntl (fd, F_GETFD) != -1; |
|
|
427 | #endif |
406 | } |
428 | } |
407 | |
429 | |
408 | /* called on EBADF to verify fds */ |
430 | /* called on EBADF to verify fds */ |
409 | static void |
431 | static void |
410 | fd_ebadf (EV_P) |
432 | fd_ebadf (EV_P) |
411 | { |
433 | { |
412 | int fd; |
434 | int fd; |
413 | |
435 | |
414 | for (fd = 0; fd < anfdmax; ++fd) |
436 | for (fd = 0; fd < anfdmax; ++fd) |
415 | if (anfds [fd].events) |
437 | if (anfds [fd].events) |
416 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
438 | if (!fd_valid (fd) == -1 && errno == EBADF) |
417 | fd_kill (EV_A_ fd); |
439 | fd_kill (EV_A_ fd); |
418 | } |
440 | } |
419 | |
441 | |
420 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
442 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
421 | static void |
443 | static void |
… | |
… | |
527 | |
549 | |
528 | if (!gotsig) |
550 | if (!gotsig) |
529 | { |
551 | { |
530 | int old_errno = errno; |
552 | int old_errno = errno; |
531 | gotsig = 1; |
553 | gotsig = 1; |
|
|
554 | #ifdef WIN32 |
|
|
555 | send (sigpipe [1], &signum, 1, MSG_DONTWAIT); |
|
|
556 | #else |
532 | write (sigpipe [1], &signum, 1); |
557 | write (sigpipe [1], &signum, 1); |
|
|
558 | #endif |
533 | errno = old_errno; |
559 | errno = old_errno; |
534 | } |
560 | } |
|
|
561 | } |
|
|
562 | |
|
|
563 | void |
|
|
564 | ev_feed_signal_event (EV_P_ int signum) |
|
|
565 | { |
|
|
566 | #if EV_MULTIPLICITY |
|
|
567 | assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
|
|
568 | #endif |
|
|
569 | |
|
|
570 | --signum; |
|
|
571 | |
|
|
572 | if (signum < 0 || signum >= signalmax) |
|
|
573 | return; |
|
|
574 | |
|
|
575 | signals [signum].gotsig = 0; |
|
|
576 | |
|
|
577 | for (w = signals [signum].head; w; w = w->next) |
|
|
578 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
535 | } |
579 | } |
536 | |
580 | |
537 | static void |
581 | static void |
538 | sigcb (EV_P_ struct ev_io *iow, int revents) |
582 | sigcb (EV_P_ struct ev_io *iow, int revents) |
539 | { |
583 | { |
540 | WL w; |
584 | WL w; |
541 | int signum; |
585 | int signum; |
542 | |
586 | |
|
|
587 | #ifdef WIN32 |
|
|
588 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
|
|
589 | #else |
543 | read (sigpipe [0], &revents, 1); |
590 | read (sigpipe [0], &revents, 1); |
|
|
591 | #endif |
544 | gotsig = 0; |
592 | gotsig = 0; |
545 | |
593 | |
546 | for (signum = signalmax; signum--; ) |
594 | for (signum = signalmax; signum--; ) |
547 | if (signals [signum].gotsig) |
595 | if (signals [signum].gotsig) |
548 | { |
596 | sigevent (EV_A_ signum + 1); |
549 | signals [signum].gotsig = 0; |
|
|
550 | |
|
|
551 | for (w = signals [signum].head; w; w = w->next) |
|
|
552 | event (EV_A_ (W)w, EV_SIGNAL); |
|
|
553 | } |
|
|
554 | } |
597 | } |
555 | |
598 | |
556 | static void |
599 | static void |
557 | siginit (EV_P) |
600 | siginit (EV_P) |
558 | { |
601 | { |
… | |
… | |
570 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
613 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
571 | } |
614 | } |
572 | |
615 | |
573 | /*****************************************************************************/ |
616 | /*****************************************************************************/ |
574 | |
617 | |
|
|
618 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
619 | |
575 | #ifndef WIN32 |
620 | #ifndef WIN32 |
576 | |
621 | |
577 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
578 | static struct ev_signal childev; |
622 | static struct ev_signal childev; |
579 | |
623 | |
580 | #ifndef WCONTINUED |
624 | #ifndef WCONTINUED |
581 | # define WCONTINUED 0 |
625 | # define WCONTINUED 0 |
582 | #endif |
626 | #endif |
… | |
… | |
590 | if (w->pid == pid || !w->pid) |
634 | if (w->pid == pid || !w->pid) |
591 | { |
635 | { |
592 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
636 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
593 | w->rpid = pid; |
637 | w->rpid = pid; |
594 | w->rstatus = status; |
638 | w->rstatus = status; |
595 | event (EV_A_ (W)w, EV_CHILD); |
639 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
596 | } |
640 | } |
597 | } |
641 | } |
598 | |
642 | |
599 | static void |
643 | static void |
600 | childcb (EV_P_ struct ev_signal *sw, int revents) |
644 | childcb (EV_P_ struct ev_signal *sw, int revents) |
… | |
… | |
602 | int pid, status; |
646 | int pid, status; |
603 | |
647 | |
604 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
648 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
605 | { |
649 | { |
606 | /* make sure we are called again until all childs have been reaped */ |
650 | /* make sure we are called again until all childs have been reaped */ |
607 | event (EV_A_ (W)sw, EV_SIGNAL); |
651 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
608 | |
652 | |
609 | child_reap (EV_A_ sw, pid, pid, status); |
653 | child_reap (EV_A_ sw, pid, pid, status); |
610 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
654 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
611 | } |
655 | } |
612 | } |
656 | } |
… | |
… | |
726 | #endif |
770 | #endif |
727 | |
771 | |
728 | for (i = NUMPRI; i--; ) |
772 | for (i = NUMPRI; i--; ) |
729 | array_free (pending, [i]); |
773 | array_free (pending, [i]); |
730 | |
774 | |
|
|
775 | /* have to use the microsoft-never-gets-it-right macro */ |
731 | array_free (fdchange, ); |
776 | array_free_microshit (fdchange); |
732 | array_free (timer, ); |
777 | array_free_microshit (timer); |
733 | array_free (periodic, ); |
778 | array_free_microshit (periodic); |
734 | array_free (idle, ); |
779 | array_free_microshit (idle); |
735 | array_free (prepare, ); |
780 | array_free_microshit (prepare); |
736 | array_free (check, ); |
781 | array_free_microshit (check); |
737 | |
782 | |
738 | method = 0; |
783 | method = 0; |
739 | } |
784 | } |
740 | |
785 | |
741 | static void |
786 | static void |
… | |
… | |
846 | { |
891 | { |
847 | #if EV_MULTIPLICITY |
892 | #if EV_MULTIPLICITY |
848 | struct ev_loop *loop = default_loop; |
893 | struct ev_loop *loop = default_loop; |
849 | #endif |
894 | #endif |
850 | |
895 | |
|
|
896 | #ifndef WIN32 |
851 | ev_ref (EV_A); /* child watcher */ |
897 | ev_ref (EV_A); /* child watcher */ |
852 | ev_signal_stop (EV_A_ &childev); |
898 | ev_signal_stop (EV_A_ &childev); |
|
|
899 | #endif |
853 | |
900 | |
854 | ev_ref (EV_A); /* signal watcher */ |
901 | ev_ref (EV_A); /* signal watcher */ |
855 | ev_io_stop (EV_A_ &sigev); |
902 | ev_io_stop (EV_A_ &sigev); |
856 | |
903 | |
857 | close (sigpipe [0]); sigpipe [0] = 0; |
904 | close (sigpipe [0]); sigpipe [0] = 0; |
… | |
… | |
870 | if (method) |
917 | if (method) |
871 | postfork = 1; |
918 | postfork = 1; |
872 | } |
919 | } |
873 | |
920 | |
874 | /*****************************************************************************/ |
921 | /*****************************************************************************/ |
|
|
922 | |
|
|
923 | static int |
|
|
924 | any_pending (EV_P) |
|
|
925 | { |
|
|
926 | int pri; |
|
|
927 | |
|
|
928 | for (pri = NUMPRI; pri--; ) |
|
|
929 | if (pendingcnt [pri]) |
|
|
930 | return 1; |
|
|
931 | |
|
|
932 | return 0; |
|
|
933 | } |
875 | |
934 | |
876 | static void |
935 | static void |
877 | call_pending (EV_P) |
936 | call_pending (EV_P) |
878 | { |
937 | { |
879 | int pri; |
938 | int pri; |
… | |
… | |
908 | downheap ((WT *)timers, timercnt, 0); |
967 | downheap ((WT *)timers, timercnt, 0); |
909 | } |
968 | } |
910 | else |
969 | else |
911 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
970 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
912 | |
971 | |
913 | event (EV_A_ (W)w, EV_TIMEOUT); |
972 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
914 | } |
973 | } |
915 | } |
974 | } |
916 | |
975 | |
917 | static void |
976 | static void |
918 | periodics_reify (EV_P) |
977 | periodics_reify (EV_P) |
… | |
… | |
922 | struct ev_periodic *w = periodics [0]; |
981 | struct ev_periodic *w = periodics [0]; |
923 | |
982 | |
924 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
983 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
925 | |
984 | |
926 | /* first reschedule or stop timer */ |
985 | /* first reschedule or stop timer */ |
|
|
986 | if (w->reschedule_cb) |
|
|
987 | { |
|
|
988 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); |
|
|
989 | |
|
|
990 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); |
|
|
991 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
992 | } |
927 | if (w->interval) |
993 | else if (w->interval) |
928 | { |
994 | { |
929 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
995 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
930 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
996 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
931 | downheap ((WT *)periodics, periodiccnt, 0); |
997 | downheap ((WT *)periodics, periodiccnt, 0); |
932 | } |
998 | } |
933 | else |
999 | else |
934 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1000 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
935 | |
1001 | |
936 | event (EV_A_ (W)w, EV_PERIODIC); |
1002 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
937 | } |
1003 | } |
938 | } |
1004 | } |
939 | |
1005 | |
940 | static void |
1006 | static void |
941 | periodics_reschedule (EV_P) |
1007 | periodics_reschedule (EV_P) |
… | |
… | |
945 | /* adjust periodics after time jump */ |
1011 | /* adjust periodics after time jump */ |
946 | for (i = 0; i < periodiccnt; ++i) |
1012 | for (i = 0; i < periodiccnt; ++i) |
947 | { |
1013 | { |
948 | struct ev_periodic *w = periodics [i]; |
1014 | struct ev_periodic *w = periodics [i]; |
949 | |
1015 | |
|
|
1016 | if (w->reschedule_cb) |
|
|
1017 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
950 | if (w->interval) |
1018 | else if (w->interval) |
951 | { |
|
|
952 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1019 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
953 | |
|
|
954 | if (fabs (diff) >= 1e-4) |
|
|
955 | { |
|
|
956 | ev_periodic_stop (EV_A_ w); |
|
|
957 | ev_periodic_start (EV_A_ w); |
|
|
958 | |
|
|
959 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
960 | } |
|
|
961 | } |
|
|
962 | } |
1020 | } |
|
|
1021 | |
|
|
1022 | /* now rebuild the heap */ |
|
|
1023 | for (i = periodiccnt >> 1; i--; ) |
|
|
1024 | downheap ((WT *)periodics, periodiccnt, i); |
963 | } |
1025 | } |
964 | |
1026 | |
965 | inline int |
1027 | inline int |
966 | time_update_monotonic (EV_P) |
1028 | time_update_monotonic (EV_P) |
967 | { |
1029 | { |
… | |
… | |
1063 | /* update fd-related kernel structures */ |
1125 | /* update fd-related kernel structures */ |
1064 | fd_reify (EV_A); |
1126 | fd_reify (EV_A); |
1065 | |
1127 | |
1066 | /* calculate blocking time */ |
1128 | /* calculate blocking time */ |
1067 | |
1129 | |
1068 | /* we only need this for !monotonic clockor timers, but as we basically |
1130 | /* we only need this for !monotonic clock or timers, but as we basically |
1069 | always have timers, we just calculate it always */ |
1131 | always have timers, we just calculate it always */ |
1070 | #if EV_USE_MONOTONIC |
1132 | #if EV_USE_MONOTONIC |
1071 | if (expect_true (have_monotonic)) |
1133 | if (expect_true (have_monotonic)) |
1072 | time_update_monotonic (EV_A); |
1134 | time_update_monotonic (EV_A); |
1073 | else |
1135 | else |
… | |
… | |
1106 | /* queue pending timers and reschedule them */ |
1168 | /* queue pending timers and reschedule them */ |
1107 | timers_reify (EV_A); /* relative timers called last */ |
1169 | timers_reify (EV_A); /* relative timers called last */ |
1108 | periodics_reify (EV_A); /* absolute timers called first */ |
1170 | periodics_reify (EV_A); /* absolute timers called first */ |
1109 | |
1171 | |
1110 | /* queue idle watchers unless io or timers are pending */ |
1172 | /* queue idle watchers unless io or timers are pending */ |
1111 | if (!pendingcnt) |
1173 | if (idlecnt && !any_pending (EV_A)) |
1112 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1174 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1113 | |
1175 | |
1114 | /* queue check watchers, to be executed first */ |
1176 | /* queue check watchers, to be executed first */ |
1115 | if (checkcnt) |
1177 | if (checkcnt) |
1116 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1178 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
… | |
… | |
1191 | return; |
1253 | return; |
1192 | |
1254 | |
1193 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1255 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1194 | |
1256 | |
1195 | ev_start (EV_A_ (W)w, 1); |
1257 | ev_start (EV_A_ (W)w, 1); |
1196 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
1258 | array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); |
1197 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1259 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1198 | |
1260 | |
1199 | fd_change (EV_A_ fd); |
1261 | fd_change (EV_A_ fd); |
1200 | } |
1262 | } |
1201 | |
1263 | |
… | |
… | |
1221 | ((WT)w)->at += mn_now; |
1283 | ((WT)w)->at += mn_now; |
1222 | |
1284 | |
1223 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1285 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1224 | |
1286 | |
1225 | ev_start (EV_A_ (W)w, ++timercnt); |
1287 | ev_start (EV_A_ (W)w, ++timercnt); |
1226 | array_needsize (timers, timermax, timercnt, ); |
1288 | array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); |
1227 | timers [timercnt - 1] = w; |
1289 | timers [timercnt - 1] = w; |
1228 | upheap ((WT *)timers, timercnt - 1); |
1290 | upheap ((WT *)timers, timercnt - 1); |
1229 | |
1291 | |
1230 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1292 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1231 | } |
1293 | } |
… | |
… | |
1271 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1333 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1272 | { |
1334 | { |
1273 | if (ev_is_active (w)) |
1335 | if (ev_is_active (w)) |
1274 | return; |
1336 | return; |
1275 | |
1337 | |
|
|
1338 | if (w->reschedule_cb) |
|
|
1339 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
|
|
1340 | else if (w->interval) |
|
|
1341 | { |
1276 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1342 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1277 | |
|
|
1278 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1343 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1279 | if (w->interval) |
|
|
1280 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1344 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
|
|
1345 | } |
1281 | |
1346 | |
1282 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1347 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1283 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1348 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1284 | periodics [periodiccnt - 1] = w; |
1349 | periodics [periodiccnt - 1] = w; |
1285 | upheap ((WT *)periodics, periodiccnt - 1); |
1350 | upheap ((WT *)periodics, periodiccnt - 1); |
1286 | |
1351 | |
1287 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1352 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1288 | } |
1353 | } |
… | |
… | |
1304 | |
1369 | |
1305 | ev_stop (EV_A_ (W)w); |
1370 | ev_stop (EV_A_ (W)w); |
1306 | } |
1371 | } |
1307 | |
1372 | |
1308 | void |
1373 | void |
|
|
1374 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
|
|
1375 | { |
|
|
1376 | ev_periodic_stop (EV_A_ w); |
|
|
1377 | ev_periodic_start (EV_A_ w); |
|
|
1378 | } |
|
|
1379 | |
|
|
1380 | void |
1309 | ev_idle_start (EV_P_ struct ev_idle *w) |
1381 | ev_idle_start (EV_P_ struct ev_idle *w) |
1310 | { |
1382 | { |
1311 | if (ev_is_active (w)) |
1383 | if (ev_is_active (w)) |
1312 | return; |
1384 | return; |
1313 | |
1385 | |
1314 | ev_start (EV_A_ (W)w, ++idlecnt); |
1386 | ev_start (EV_A_ (W)w, ++idlecnt); |
1315 | array_needsize (idles, idlemax, idlecnt, ); |
1387 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); |
1316 | idles [idlecnt - 1] = w; |
1388 | idles [idlecnt - 1] = w; |
1317 | } |
1389 | } |
1318 | |
1390 | |
1319 | void |
1391 | void |
1320 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1392 | ev_idle_stop (EV_P_ struct ev_idle *w) |
… | |
… | |
1332 | { |
1404 | { |
1333 | if (ev_is_active (w)) |
1405 | if (ev_is_active (w)) |
1334 | return; |
1406 | return; |
1335 | |
1407 | |
1336 | ev_start (EV_A_ (W)w, ++preparecnt); |
1408 | ev_start (EV_A_ (W)w, ++preparecnt); |
1337 | array_needsize (prepares, preparemax, preparecnt, ); |
1409 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); |
1338 | prepares [preparecnt - 1] = w; |
1410 | prepares [preparecnt - 1] = w; |
1339 | } |
1411 | } |
1340 | |
1412 | |
1341 | void |
1413 | void |
1342 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1414 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
… | |
… | |
1354 | { |
1426 | { |
1355 | if (ev_is_active (w)) |
1427 | if (ev_is_active (w)) |
1356 | return; |
1428 | return; |
1357 | |
1429 | |
1358 | ev_start (EV_A_ (W)w, ++checkcnt); |
1430 | ev_start (EV_A_ (W)w, ++checkcnt); |
1359 | array_needsize (checks, checkmax, checkcnt, ); |
1431 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); |
1360 | checks [checkcnt - 1] = w; |
1432 | checks [checkcnt - 1] = w; |
1361 | } |
1433 | } |
1362 | |
1434 | |
1363 | void |
1435 | void |
1364 | ev_check_stop (EV_P_ struct ev_check *w) |
1436 | ev_check_stop (EV_P_ struct ev_check *w) |
… | |
… | |
1385 | return; |
1457 | return; |
1386 | |
1458 | |
1387 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1459 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1388 | |
1460 | |
1389 | ev_start (EV_A_ (W)w, 1); |
1461 | ev_start (EV_A_ (W)w, 1); |
1390 | array_needsize (signals, signalmax, w->signum, signals_init); |
1462 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1391 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1463 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1392 | |
1464 | |
1393 | if (!((WL)w)->next) |
1465 | if (!((WL)w)->next) |
1394 | { |
1466 | { |
1395 | #if WIN32 |
1467 | #if WIN32 |
… | |
… | |
1478 | } |
1550 | } |
1479 | |
1551 | |
1480 | void |
1552 | void |
1481 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1553 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1482 | { |
1554 | { |
1483 | struct ev_once *once = ev_malloc (sizeof (struct ev_once)); |
1555 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1484 | |
1556 | |
1485 | if (!once) |
1557 | if (!once) |
1486 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1558 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1487 | else |
1559 | else |
1488 | { |
1560 | { |