… | |
… | |
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 | static void |
342 | fd_event (EV_P_ int fd, int events) |
348 | fd_event (EV_P_ int fd, int events) |
343 | { |
349 | { |
… | |
… | |
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 & events; |
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 | } |
354 | } |
360 | } |
355 | |
361 | |
356 | /*****************************************************************************/ |
362 | /*****************************************************************************/ |
357 | |
363 | |
… | |
… | |
387 | return; |
393 | return; |
388 | |
394 | |
389 | anfds [fd].reify = 1; |
395 | anfds [fd].reify = 1; |
390 | |
396 | |
391 | ++fdchangecnt; |
397 | ++fdchangecnt; |
392 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
398 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); |
393 | fdchanges [fdchangecnt - 1] = fd; |
399 | fdchanges [fdchangecnt - 1] = fd; |
394 | } |
400 | } |
395 | |
401 | |
396 | static void |
402 | static void |
397 | fd_kill (EV_P_ int fd) |
403 | fd_kill (EV_P_ int fd) |
… | |
… | |
399 | struct ev_io *w; |
405 | struct ev_io *w; |
400 | |
406 | |
401 | while ((w = (struct ev_io *)anfds [fd].head)) |
407 | while ((w = (struct ev_io *)anfds [fd].head)) |
402 | { |
408 | { |
403 | ev_io_stop (EV_A_ w); |
409 | ev_io_stop (EV_A_ w); |
404 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
410 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
405 | } |
411 | } |
|
|
412 | } |
|
|
413 | |
|
|
414 | static int |
|
|
415 | fd_valid (int fd) |
|
|
416 | { |
|
|
417 | #ifdef WIN32 |
|
|
418 | return !!win32_get_osfhandle (fd); |
|
|
419 | #else |
|
|
420 | return fcntl (fd, F_GETFD) != -1; |
|
|
421 | #endif |
406 | } |
422 | } |
407 | |
423 | |
408 | /* called on EBADF to verify fds */ |
424 | /* called on EBADF to verify fds */ |
409 | static void |
425 | static void |
410 | fd_ebadf (EV_P) |
426 | fd_ebadf (EV_P) |
411 | { |
427 | { |
412 | int fd; |
428 | int fd; |
413 | |
429 | |
414 | for (fd = 0; fd < anfdmax; ++fd) |
430 | for (fd = 0; fd < anfdmax; ++fd) |
415 | if (anfds [fd].events) |
431 | if (anfds [fd].events) |
416 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
432 | if (!fd_valid (fd) == -1 && errno == EBADF) |
417 | fd_kill (EV_A_ fd); |
433 | fd_kill (EV_A_ fd); |
418 | } |
434 | } |
419 | |
435 | |
420 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
436 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
421 | static void |
437 | static void |
… | |
… | |
527 | |
543 | |
528 | if (!gotsig) |
544 | if (!gotsig) |
529 | { |
545 | { |
530 | int old_errno = errno; |
546 | int old_errno = errno; |
531 | gotsig = 1; |
547 | gotsig = 1; |
|
|
548 | #ifdef WIN32 |
|
|
549 | send (sigpipe [1], &signum, 1, MSG_DONTWAIT); |
|
|
550 | #else |
532 | write (sigpipe [1], &signum, 1); |
551 | write (sigpipe [1], &signum, 1); |
|
|
552 | #endif |
533 | errno = old_errno; |
553 | errno = old_errno; |
534 | } |
554 | } |
535 | } |
555 | } |
536 | |
556 | |
537 | static void |
557 | static void |
538 | sigcb (EV_P_ struct ev_io *iow, int revents) |
558 | sigcb (EV_P_ struct ev_io *iow, int revents) |
539 | { |
559 | { |
540 | WL w; |
560 | WL w; |
541 | int signum; |
561 | int signum; |
542 | |
562 | |
|
|
563 | #ifdef WIN32 |
|
|
564 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
|
|
565 | #else |
543 | read (sigpipe [0], &revents, 1); |
566 | read (sigpipe [0], &revents, 1); |
|
|
567 | #endif |
544 | gotsig = 0; |
568 | gotsig = 0; |
545 | |
569 | |
546 | for (signum = signalmax; signum--; ) |
570 | for (signum = signalmax; signum--; ) |
547 | if (signals [signum].gotsig) |
571 | if (signals [signum].gotsig) |
548 | { |
572 | { |
549 | signals [signum].gotsig = 0; |
573 | signals [signum].gotsig = 0; |
550 | |
574 | |
551 | for (w = signals [signum].head; w; w = w->next) |
575 | for (w = signals [signum].head; w; w = w->next) |
552 | event (EV_A_ (W)w, EV_SIGNAL); |
576 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
553 | } |
577 | } |
554 | } |
578 | } |
555 | |
579 | |
556 | static void |
580 | static void |
557 | siginit (EV_P) |
581 | siginit (EV_P) |
… | |
… | |
570 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
594 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
571 | } |
595 | } |
572 | |
596 | |
573 | /*****************************************************************************/ |
597 | /*****************************************************************************/ |
574 | |
598 | |
|
|
599 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
600 | |
575 | #ifndef WIN32 |
601 | #ifndef WIN32 |
576 | |
602 | |
577 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
578 | static struct ev_signal childev; |
603 | static struct ev_signal childev; |
579 | |
604 | |
580 | #ifndef WCONTINUED |
605 | #ifndef WCONTINUED |
581 | # define WCONTINUED 0 |
606 | # define WCONTINUED 0 |
582 | #endif |
607 | #endif |
… | |
… | |
590 | if (w->pid == pid || !w->pid) |
615 | if (w->pid == pid || !w->pid) |
591 | { |
616 | { |
592 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
617 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
593 | w->rpid = pid; |
618 | w->rpid = pid; |
594 | w->rstatus = status; |
619 | w->rstatus = status; |
595 | event (EV_A_ (W)w, EV_CHILD); |
620 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
596 | } |
621 | } |
597 | } |
622 | } |
598 | |
623 | |
599 | static void |
624 | static void |
600 | childcb (EV_P_ struct ev_signal *sw, int revents) |
625 | childcb (EV_P_ struct ev_signal *sw, int revents) |
… | |
… | |
602 | int pid, status; |
627 | int pid, status; |
603 | |
628 | |
604 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
629 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
605 | { |
630 | { |
606 | /* make sure we are called again until all childs have been reaped */ |
631 | /* make sure we are called again until all childs have been reaped */ |
607 | event (EV_A_ (W)sw, EV_SIGNAL); |
632 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
608 | |
633 | |
609 | child_reap (EV_A_ sw, pid, pid, status); |
634 | 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 */ |
635 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
611 | } |
636 | } |
612 | } |
637 | } |
… | |
… | |
726 | #endif |
751 | #endif |
727 | |
752 | |
728 | for (i = NUMPRI; i--; ) |
753 | for (i = NUMPRI; i--; ) |
729 | array_free (pending, [i]); |
754 | array_free (pending, [i]); |
730 | |
755 | |
|
|
756 | /* have to use the microsoft-never-gets-it-right macro */ |
731 | array_free (fdchange, ); |
757 | array_free_microshit (fdchange); |
732 | array_free (timer, ); |
758 | array_free_microshit (timer); |
733 | array_free (periodic, ); |
759 | array_free_microshit (periodic); |
734 | array_free (idle, ); |
760 | array_free_microshit (idle); |
735 | array_free (prepare, ); |
761 | array_free_microshit (prepare); |
736 | array_free (check, ); |
762 | array_free_microshit (check); |
737 | |
763 | |
738 | method = 0; |
764 | method = 0; |
739 | } |
765 | } |
740 | |
766 | |
741 | static void |
767 | static void |
… | |
… | |
846 | { |
872 | { |
847 | #if EV_MULTIPLICITY |
873 | #if EV_MULTIPLICITY |
848 | struct ev_loop *loop = default_loop; |
874 | struct ev_loop *loop = default_loop; |
849 | #endif |
875 | #endif |
850 | |
876 | |
|
|
877 | #ifndef WIN32 |
851 | ev_ref (EV_A); /* child watcher */ |
878 | ev_ref (EV_A); /* child watcher */ |
852 | ev_signal_stop (EV_A_ &childev); |
879 | ev_signal_stop (EV_A_ &childev); |
|
|
880 | #endif |
853 | |
881 | |
854 | ev_ref (EV_A); /* signal watcher */ |
882 | ev_ref (EV_A); /* signal watcher */ |
855 | ev_io_stop (EV_A_ &sigev); |
883 | ev_io_stop (EV_A_ &sigev); |
856 | |
884 | |
857 | close (sigpipe [0]); sigpipe [0] = 0; |
885 | close (sigpipe [0]); sigpipe [0] = 0; |
… | |
… | |
870 | if (method) |
898 | if (method) |
871 | postfork = 1; |
899 | postfork = 1; |
872 | } |
900 | } |
873 | |
901 | |
874 | /*****************************************************************************/ |
902 | /*****************************************************************************/ |
|
|
903 | |
|
|
904 | static int |
|
|
905 | any_pending (EV_P) |
|
|
906 | { |
|
|
907 | int pri; |
|
|
908 | |
|
|
909 | for (pri = NUMPRI; pri--; ) |
|
|
910 | if (pendingcnt [pri]) |
|
|
911 | return 1; |
|
|
912 | |
|
|
913 | return 0; |
|
|
914 | } |
875 | |
915 | |
876 | static void |
916 | static void |
877 | call_pending (EV_P) |
917 | call_pending (EV_P) |
878 | { |
918 | { |
879 | int pri; |
919 | int pri; |
… | |
… | |
908 | downheap ((WT *)timers, timercnt, 0); |
948 | downheap ((WT *)timers, timercnt, 0); |
909 | } |
949 | } |
910 | else |
950 | else |
911 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
951 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
912 | |
952 | |
913 | event (EV_A_ (W)w, EV_TIMEOUT); |
953 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
914 | } |
954 | } |
915 | } |
955 | } |
916 | |
956 | |
917 | static void |
957 | static void |
918 | periodics_reify (EV_P) |
958 | periodics_reify (EV_P) |
… | |
… | |
922 | struct ev_periodic *w = periodics [0]; |
962 | struct ev_periodic *w = periodics [0]; |
923 | |
963 | |
924 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
964 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
925 | |
965 | |
926 | /* first reschedule or stop timer */ |
966 | /* first reschedule or stop timer */ |
|
|
967 | if (w->reschedule_cb) |
|
|
968 | { |
|
|
969 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); |
|
|
970 | |
|
|
971 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); |
|
|
972 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
973 | } |
927 | if (w->interval) |
974 | else if (w->interval) |
928 | { |
975 | { |
929 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
976 | ((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)); |
977 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
931 | downheap ((WT *)periodics, periodiccnt, 0); |
978 | downheap ((WT *)periodics, periodiccnt, 0); |
932 | } |
979 | } |
933 | else |
980 | else |
934 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
981 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
935 | |
982 | |
936 | event (EV_A_ (W)w, EV_PERIODIC); |
983 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
937 | } |
984 | } |
938 | } |
985 | } |
939 | |
986 | |
940 | static void |
987 | static void |
941 | periodics_reschedule (EV_P) |
988 | periodics_reschedule (EV_P) |
… | |
… | |
945 | /* adjust periodics after time jump */ |
992 | /* adjust periodics after time jump */ |
946 | for (i = 0; i < periodiccnt; ++i) |
993 | for (i = 0; i < periodiccnt; ++i) |
947 | { |
994 | { |
948 | struct ev_periodic *w = periodics [i]; |
995 | struct ev_periodic *w = periodics [i]; |
949 | |
996 | |
|
|
997 | if (w->reschedule_cb) |
|
|
998 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
950 | if (w->interval) |
999 | else if (w->interval) |
951 | { |
|
|
952 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1000 | ((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 | } |
1001 | } |
|
|
1002 | |
|
|
1003 | /* now rebuild the heap */ |
|
|
1004 | for (i = periodiccnt >> 1; i--; ) |
|
|
1005 | downheap ((WT *)periodics, periodiccnt, i); |
963 | } |
1006 | } |
964 | |
1007 | |
965 | inline int |
1008 | inline int |
966 | time_update_monotonic (EV_P) |
1009 | time_update_monotonic (EV_P) |
967 | { |
1010 | { |
… | |
… | |
1063 | /* update fd-related kernel structures */ |
1106 | /* update fd-related kernel structures */ |
1064 | fd_reify (EV_A); |
1107 | fd_reify (EV_A); |
1065 | |
1108 | |
1066 | /* calculate blocking time */ |
1109 | /* calculate blocking time */ |
1067 | |
1110 | |
1068 | /* we only need this for !monotonic clockor timers, but as we basically |
1111 | /* we only need this for !monotonic clock or timers, but as we basically |
1069 | always have timers, we just calculate it always */ |
1112 | always have timers, we just calculate it always */ |
1070 | #if EV_USE_MONOTONIC |
1113 | #if EV_USE_MONOTONIC |
1071 | if (expect_true (have_monotonic)) |
1114 | if (expect_true (have_monotonic)) |
1072 | time_update_monotonic (EV_A); |
1115 | time_update_monotonic (EV_A); |
1073 | else |
1116 | else |
… | |
… | |
1106 | /* queue pending timers and reschedule them */ |
1149 | /* queue pending timers and reschedule them */ |
1107 | timers_reify (EV_A); /* relative timers called last */ |
1150 | timers_reify (EV_A); /* relative timers called last */ |
1108 | periodics_reify (EV_A); /* absolute timers called first */ |
1151 | periodics_reify (EV_A); /* absolute timers called first */ |
1109 | |
1152 | |
1110 | /* queue idle watchers unless io or timers are pending */ |
1153 | /* queue idle watchers unless io or timers are pending */ |
1111 | if (!pendingcnt) |
1154 | if (idlecnt && !any_pending (EV_A)) |
1112 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1155 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1113 | |
1156 | |
1114 | /* queue check watchers, to be executed first */ |
1157 | /* queue check watchers, to be executed first */ |
1115 | if (checkcnt) |
1158 | if (checkcnt) |
1116 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1159 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
… | |
… | |
1191 | return; |
1234 | return; |
1192 | |
1235 | |
1193 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1236 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1194 | |
1237 | |
1195 | ev_start (EV_A_ (W)w, 1); |
1238 | ev_start (EV_A_ (W)w, 1); |
1196 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
1239 | array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); |
1197 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1240 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1198 | |
1241 | |
1199 | fd_change (EV_A_ fd); |
1242 | fd_change (EV_A_ fd); |
1200 | } |
1243 | } |
1201 | |
1244 | |
… | |
… | |
1221 | ((WT)w)->at += mn_now; |
1264 | ((WT)w)->at += mn_now; |
1222 | |
1265 | |
1223 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1266 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1224 | |
1267 | |
1225 | ev_start (EV_A_ (W)w, ++timercnt); |
1268 | ev_start (EV_A_ (W)w, ++timercnt); |
1226 | array_needsize (timers, timermax, timercnt, ); |
1269 | array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); |
1227 | timers [timercnt - 1] = w; |
1270 | timers [timercnt - 1] = w; |
1228 | upheap ((WT *)timers, timercnt - 1); |
1271 | upheap ((WT *)timers, timercnt - 1); |
1229 | |
1272 | |
1230 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1273 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1231 | } |
1274 | } |
… | |
… | |
1271 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1314 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1272 | { |
1315 | { |
1273 | if (ev_is_active (w)) |
1316 | if (ev_is_active (w)) |
1274 | return; |
1317 | return; |
1275 | |
1318 | |
|
|
1319 | if (w->reschedule_cb) |
|
|
1320 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
|
|
1321 | else if (w->interval) |
|
|
1322 | { |
1276 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1323 | 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 */ |
1324 | /* 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; |
1325 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
|
|
1326 | } |
1281 | |
1327 | |
1282 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1328 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1283 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1329 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1284 | periodics [periodiccnt - 1] = w; |
1330 | periodics [periodiccnt - 1] = w; |
1285 | upheap ((WT *)periodics, periodiccnt - 1); |
1331 | upheap ((WT *)periodics, periodiccnt - 1); |
1286 | |
1332 | |
1287 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1333 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1288 | } |
1334 | } |
… | |
… | |
1304 | |
1350 | |
1305 | ev_stop (EV_A_ (W)w); |
1351 | ev_stop (EV_A_ (W)w); |
1306 | } |
1352 | } |
1307 | |
1353 | |
1308 | void |
1354 | void |
|
|
1355 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
|
|
1356 | { |
|
|
1357 | ev_periodic_stop (EV_A_ w); |
|
|
1358 | ev_periodic_start (EV_A_ w); |
|
|
1359 | } |
|
|
1360 | |
|
|
1361 | void |
1309 | ev_idle_start (EV_P_ struct ev_idle *w) |
1362 | ev_idle_start (EV_P_ struct ev_idle *w) |
1310 | { |
1363 | { |
1311 | if (ev_is_active (w)) |
1364 | if (ev_is_active (w)) |
1312 | return; |
1365 | return; |
1313 | |
1366 | |
1314 | ev_start (EV_A_ (W)w, ++idlecnt); |
1367 | ev_start (EV_A_ (W)w, ++idlecnt); |
1315 | array_needsize (idles, idlemax, idlecnt, ); |
1368 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); |
1316 | idles [idlecnt - 1] = w; |
1369 | idles [idlecnt - 1] = w; |
1317 | } |
1370 | } |
1318 | |
1371 | |
1319 | void |
1372 | void |
1320 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1373 | ev_idle_stop (EV_P_ struct ev_idle *w) |
… | |
… | |
1332 | { |
1385 | { |
1333 | if (ev_is_active (w)) |
1386 | if (ev_is_active (w)) |
1334 | return; |
1387 | return; |
1335 | |
1388 | |
1336 | ev_start (EV_A_ (W)w, ++preparecnt); |
1389 | ev_start (EV_A_ (W)w, ++preparecnt); |
1337 | array_needsize (prepares, preparemax, preparecnt, ); |
1390 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); |
1338 | prepares [preparecnt - 1] = w; |
1391 | prepares [preparecnt - 1] = w; |
1339 | } |
1392 | } |
1340 | |
1393 | |
1341 | void |
1394 | void |
1342 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1395 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
… | |
… | |
1354 | { |
1407 | { |
1355 | if (ev_is_active (w)) |
1408 | if (ev_is_active (w)) |
1356 | return; |
1409 | return; |
1357 | |
1410 | |
1358 | ev_start (EV_A_ (W)w, ++checkcnt); |
1411 | ev_start (EV_A_ (W)w, ++checkcnt); |
1359 | array_needsize (checks, checkmax, checkcnt, ); |
1412 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); |
1360 | checks [checkcnt - 1] = w; |
1413 | checks [checkcnt - 1] = w; |
1361 | } |
1414 | } |
1362 | |
1415 | |
1363 | void |
1416 | void |
1364 | ev_check_stop (EV_P_ struct ev_check *w) |
1417 | ev_check_stop (EV_P_ struct ev_check *w) |
… | |
… | |
1385 | return; |
1438 | return; |
1386 | |
1439 | |
1387 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1440 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1388 | |
1441 | |
1389 | ev_start (EV_A_ (W)w, 1); |
1442 | ev_start (EV_A_ (W)w, 1); |
1390 | array_needsize (signals, signalmax, w->signum, signals_init); |
1443 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1391 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1444 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1392 | |
1445 | |
1393 | if (!((WL)w)->next) |
1446 | if (!((WL)w)->next) |
1394 | { |
1447 | { |
1395 | #if WIN32 |
1448 | #if WIN32 |
… | |
… | |
1478 | } |
1531 | } |
1479 | |
1532 | |
1480 | void |
1533 | void |
1481 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1534 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1482 | { |
1535 | { |
1483 | struct ev_once *once = ev_malloc (sizeof (struct ev_once)); |
1536 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1484 | |
1537 | |
1485 | if (!once) |
1538 | if (!once) |
1486 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1539 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1487 | else |
1540 | else |
1488 | { |
1541 | { |