… | |
… | |
35 | |
35 | |
36 | #ifndef EV_STANDALONE |
36 | #ifndef EV_STANDALONE |
37 | # include "config.h" |
37 | # include "config.h" |
38 | |
38 | |
39 | # if HAVE_CLOCK_GETTIME |
39 | # if HAVE_CLOCK_GETTIME |
|
|
40 | # ifndef EV_USE_MONOTONIC |
40 | # define EV_USE_MONOTONIC 1 |
41 | # define EV_USE_MONOTONIC 1 |
|
|
42 | # endif |
|
|
43 | # ifndef EV_USE_REALTIME |
41 | # define EV_USE_REALTIME 1 |
44 | # define EV_USE_REALTIME 1 |
|
|
45 | # endif |
42 | # endif |
46 | # endif |
43 | |
47 | |
44 | # if HAVE_SELECT && HAVE_SYS_SELECT_H |
48 | # if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT) |
45 | # define EV_USE_SELECT 1 |
49 | # define EV_USE_SELECT 1 |
46 | # endif |
50 | # endif |
47 | |
51 | |
48 | # if HAVE_POLL && HAVE_POLL_H |
52 | # if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) |
49 | # define EV_USE_POLL 1 |
53 | # define EV_USE_POLL 1 |
50 | # endif |
54 | # endif |
51 | |
55 | |
52 | # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
56 | # if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL) |
53 | # define EV_USE_EPOLL 1 |
57 | # define EV_USE_EPOLL 1 |
54 | # endif |
58 | # endif |
55 | |
59 | |
56 | # if HAVE_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) |
57 | # define EV_USE_KQUEUE 1 |
61 | # define EV_USE_KQUEUE 1 |
58 | # endif |
62 | # endif |
59 | |
63 | |
60 | #endif |
64 | #endif |
61 | |
65 | |
… | |
… | |
71 | #include <sys/types.h> |
75 | #include <sys/types.h> |
72 | #include <time.h> |
76 | #include <time.h> |
73 | |
77 | |
74 | #include <signal.h> |
78 | #include <signal.h> |
75 | |
79 | |
76 | #ifndef WIN32 |
80 | #ifndef _WIN32 |
77 | # include <unistd.h> |
81 | # include <unistd.h> |
78 | # include <sys/time.h> |
82 | # include <sys/time.h> |
79 | # include <sys/wait.h> |
83 | # include <sys/wait.h> |
|
|
84 | #else |
|
|
85 | # define WIN32_LEAN_AND_MEAN |
|
|
86 | # include <windows.h> |
|
|
87 | # ifndef EV_SELECT_IS_WINSOCKET |
|
|
88 | # define EV_SELECT_IS_WINSOCKET 1 |
80 | #endif |
89 | # endif |
|
|
90 | #endif |
|
|
91 | |
81 | /**/ |
92 | /**/ |
82 | |
93 | |
83 | #ifndef EV_USE_MONOTONIC |
94 | #ifndef EV_USE_MONOTONIC |
84 | # define EV_USE_MONOTONIC 1 |
95 | # define EV_USE_MONOTONIC 1 |
85 | #endif |
96 | #endif |
86 | |
97 | |
87 | #ifndef EV_USE_SELECT |
98 | #ifndef EV_USE_SELECT |
88 | # define EV_USE_SELECT 1 |
99 | # define EV_USE_SELECT 1 |
|
|
100 | # define EV_SELECT_USE_FD_SET 1 |
89 | #endif |
101 | #endif |
90 | |
102 | |
91 | #ifndef EV_USE_POLL |
103 | #ifndef EV_USE_POLL |
92 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
104 | # ifdef _WIN32 |
|
|
105 | # define EV_USE_POLL 0 |
|
|
106 | # else |
|
|
107 | # define EV_USE_POLL 1 |
|
|
108 | # endif |
93 | #endif |
109 | #endif |
94 | |
110 | |
95 | #ifndef EV_USE_EPOLL |
111 | #ifndef EV_USE_EPOLL |
96 | # define EV_USE_EPOLL 0 |
112 | # define EV_USE_EPOLL 0 |
97 | #endif |
113 | #endif |
98 | |
114 | |
99 | #ifndef EV_USE_KQUEUE |
115 | #ifndef EV_USE_KQUEUE |
100 | # define EV_USE_KQUEUE 0 |
116 | # define EV_USE_KQUEUE 0 |
101 | #endif |
|
|
102 | |
|
|
103 | #ifndef EV_USE_WIN32 |
|
|
104 | # ifdef WIN32 |
|
|
105 | # define EV_USE_WIN32 0 /* it does not exist, use select */ |
|
|
106 | # undef EV_USE_SELECT |
|
|
107 | # define EV_USE_SELECT 1 |
|
|
108 | # else |
|
|
109 | # define EV_USE_WIN32 0 |
|
|
110 | # endif |
|
|
111 | #endif |
117 | #endif |
112 | |
118 | |
113 | #ifndef EV_USE_REALTIME |
119 | #ifndef EV_USE_REALTIME |
114 | # define EV_USE_REALTIME 1 |
120 | # define EV_USE_REALTIME 1 |
115 | #endif |
121 | #endif |
… | |
… | |
122 | #endif |
128 | #endif |
123 | |
129 | |
124 | #ifndef CLOCK_REALTIME |
130 | #ifndef CLOCK_REALTIME |
125 | # undef EV_USE_REALTIME |
131 | # undef EV_USE_REALTIME |
126 | # define EV_USE_REALTIME 0 |
132 | # define EV_USE_REALTIME 0 |
|
|
133 | #endif |
|
|
134 | |
|
|
135 | #if EV_SELECT_IS_WINSOCKET |
|
|
136 | # include <winsock.h> |
127 | #endif |
137 | #endif |
128 | |
138 | |
129 | /**/ |
139 | /**/ |
130 | |
140 | |
131 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
141 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
… | |
… | |
151 | #define expect_true(expr) expect ((expr) != 0, 1) |
161 | #define expect_true(expr) expect ((expr) != 0, 1) |
152 | |
162 | |
153 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
163 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
154 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
164 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
155 | |
165 | |
|
|
166 | #define EMPTY /* required for microsofts broken pseudo-c compiler */ |
|
|
167 | |
156 | typedef struct ev_watcher *W; |
168 | typedef struct ev_watcher *W; |
157 | typedef struct ev_watcher_list *WL; |
169 | typedef struct ev_watcher_list *WL; |
158 | typedef struct ev_watcher_time *WT; |
170 | typedef struct ev_watcher_time *WT; |
159 | |
171 | |
160 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
172 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
161 | |
173 | |
|
|
174 | #ifdef _WIN32 |
162 | #include "ev_win32.c" |
175 | # include "ev_win32.c" |
|
|
176 | #endif |
163 | |
177 | |
164 | /*****************************************************************************/ |
178 | /*****************************************************************************/ |
165 | |
179 | |
166 | static void (*syserr_cb)(const char *msg); |
180 | static void (*syserr_cb)(const char *msg); |
167 | |
181 | |
… | |
… | |
214 | typedef struct |
228 | typedef struct |
215 | { |
229 | { |
216 | WL head; |
230 | WL head; |
217 | unsigned char events; |
231 | unsigned char events; |
218 | unsigned char reify; |
232 | unsigned char reify; |
|
|
233 | #if EV_SELECT_IS_WINSOCKET |
|
|
234 | SOCKET handle; |
|
|
235 | #endif |
219 | } ANFD; |
236 | } ANFD; |
220 | |
237 | |
221 | typedef struct |
238 | typedef struct |
222 | { |
239 | { |
223 | W w; |
240 | W w; |
… | |
… | |
227 | #if EV_MULTIPLICITY |
244 | #if EV_MULTIPLICITY |
228 | |
245 | |
229 | struct ev_loop |
246 | struct ev_loop |
230 | { |
247 | { |
231 | ev_tstamp ev_rt_now; |
248 | ev_tstamp ev_rt_now; |
|
|
249 | #define ev_rt_now ((loop)->ev_rt_now) |
232 | #define VAR(name,decl) decl; |
250 | #define VAR(name,decl) decl; |
233 | #include "ev_vars.h" |
251 | #include "ev_vars.h" |
234 | #undef VAR |
252 | #undef VAR |
235 | }; |
253 | }; |
236 | #include "ev_wrap.h" |
254 | #include "ev_wrap.h" |
… | |
… | |
249 | |
267 | |
250 | #endif |
268 | #endif |
251 | |
269 | |
252 | /*****************************************************************************/ |
270 | /*****************************************************************************/ |
253 | |
271 | |
254 | inline ev_tstamp |
272 | ev_tstamp |
255 | ev_time (void) |
273 | ev_time (void) |
256 | { |
274 | { |
257 | #if EV_USE_REALTIME |
275 | #if EV_USE_REALTIME |
258 | struct timespec ts; |
276 | struct timespec ts; |
259 | clock_gettime (CLOCK_REALTIME, &ts); |
277 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
311 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
329 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
312 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
330 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
313 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
331 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
314 | } |
332 | } |
315 | |
333 | |
316 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
|
|
317 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
|
|
318 | #define array_free_microshit(stem) \ |
|
|
319 | ev_free (stem ## s); stem ## cnt = stem ## max = 0; |
|
|
320 | |
|
|
321 | #define array_free(stem, idx) \ |
334 | #define array_free(stem, idx) \ |
322 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
335 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
323 | |
336 | |
324 | /*****************************************************************************/ |
337 | /*****************************************************************************/ |
325 | |
338 | |
… | |
… | |
399 | int events = 0; |
412 | int events = 0; |
400 | |
413 | |
401 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
414 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
402 | events |= w->events; |
415 | events |= w->events; |
403 | |
416 | |
|
|
417 | #if EV_SELECT_IS_WINSOCKET |
|
|
418 | if (events) |
|
|
419 | { |
|
|
420 | unsigned long argp; |
|
|
421 | anfd->handle = _get_osfhandle (fd); |
|
|
422 | assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0)); |
|
|
423 | } |
|
|
424 | #endif |
|
|
425 | |
404 | anfd->reify = 0; |
426 | anfd->reify = 0; |
405 | |
427 | |
406 | method_modify (EV_A_ fd, anfd->events, events); |
428 | method_modify (EV_A_ fd, anfd->events, events); |
407 | anfd->events = events; |
429 | anfd->events = events; |
408 | } |
430 | } |
… | |
… | |
436 | } |
458 | } |
437 | |
459 | |
438 | static int |
460 | static int |
439 | fd_valid (int fd) |
461 | fd_valid (int fd) |
440 | { |
462 | { |
441 | #ifdef WIN32 |
463 | #ifdef _WIN32 |
442 | return !!win32_get_osfhandle (fd); |
464 | return _get_osfhandle (fd) != -1; |
443 | #else |
465 | #else |
444 | return fcntl (fd, F_GETFD) != -1; |
466 | return fcntl (fd, F_GETFD) != -1; |
445 | #endif |
467 | #endif |
446 | } |
468 | } |
447 | |
469 | |
… | |
… | |
528 | heap [k] = w; |
550 | heap [k] = w; |
529 | ((W)heap [k])->active = k + 1; |
551 | ((W)heap [k])->active = k + 1; |
530 | } |
552 | } |
531 | |
553 | |
532 | inline void |
554 | inline void |
533 | adjustheap (WT *heap, int N, int k, ev_tstamp at) |
555 | adjustheap (WT *heap, int N, int k) |
534 | { |
556 | { |
535 | ev_tstamp old_at = heap [k]->at; |
557 | upheap (heap, k); |
536 | heap [k]->at = at; |
|
|
537 | |
|
|
538 | if (old_at < at) |
|
|
539 | downheap (heap, N, k); |
558 | downheap (heap, N, k); |
540 | else |
|
|
541 | upheap (heap, k); |
|
|
542 | } |
559 | } |
543 | |
560 | |
544 | /*****************************************************************************/ |
561 | /*****************************************************************************/ |
545 | |
562 | |
546 | typedef struct |
563 | typedef struct |
… | |
… | |
569 | } |
586 | } |
570 | |
587 | |
571 | static void |
588 | static void |
572 | sighandler (int signum) |
589 | sighandler (int signum) |
573 | { |
590 | { |
574 | #if WIN32 |
591 | #if _WIN32 |
575 | signal (signum, sighandler); |
592 | signal (signum, sighandler); |
576 | #endif |
593 | #endif |
577 | |
594 | |
578 | signals [signum - 1].gotsig = 1; |
595 | signals [signum - 1].gotsig = 1; |
579 | |
596 | |
580 | if (!gotsig) |
597 | if (!gotsig) |
581 | { |
598 | { |
582 | int old_errno = errno; |
599 | int old_errno = errno; |
583 | gotsig = 1; |
600 | gotsig = 1; |
584 | #ifdef WIN32 |
|
|
585 | send (sigpipe [1], &signum, 1, MSG_DONTWAIT); |
|
|
586 | #else |
|
|
587 | write (sigpipe [1], &signum, 1); |
601 | write (sigpipe [1], &signum, 1); |
588 | #endif |
|
|
589 | errno = old_errno; |
602 | errno = old_errno; |
590 | } |
603 | } |
591 | } |
604 | } |
592 | |
605 | |
593 | void |
606 | void |
… | |
… | |
613 | static void |
626 | static void |
614 | sigcb (EV_P_ struct ev_io *iow, int revents) |
627 | sigcb (EV_P_ struct ev_io *iow, int revents) |
615 | { |
628 | { |
616 | int signum; |
629 | int signum; |
617 | |
630 | |
618 | #ifdef WIN32 |
|
|
619 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
|
|
620 | #else |
|
|
621 | read (sigpipe [0], &revents, 1); |
631 | read (sigpipe [0], &revents, 1); |
622 | #endif |
|
|
623 | gotsig = 0; |
632 | gotsig = 0; |
624 | |
633 | |
625 | for (signum = signalmax; signum--; ) |
634 | for (signum = signalmax; signum--; ) |
626 | if (signals [signum].gotsig) |
635 | if (signals [signum].gotsig) |
627 | ev_feed_signal_event (EV_A_ signum + 1); |
636 | ev_feed_signal_event (EV_A_ signum + 1); |
628 | } |
637 | } |
629 | |
638 | |
|
|
639 | inline void |
|
|
640 | fd_intern (int fd) |
|
|
641 | { |
|
|
642 | #ifdef _WIN32 |
|
|
643 | int arg = 1; |
|
|
644 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
|
|
645 | #else |
|
|
646 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
647 | fcntl (fd, F_SETFL, O_NONBLOCK); |
|
|
648 | #endif |
|
|
649 | } |
|
|
650 | |
630 | static void |
651 | static void |
631 | siginit (EV_P) |
652 | siginit (EV_P) |
632 | { |
653 | { |
633 | #ifndef WIN32 |
654 | fd_intern (sigpipe [0]); |
634 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
655 | fd_intern (sigpipe [1]); |
635 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
|
|
636 | |
|
|
637 | /* rather than sort out wether we really need nb, set it */ |
|
|
638 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
|
|
639 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
640 | #endif |
|
|
641 | |
656 | |
642 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
657 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
643 | ev_io_start (EV_A_ &sigev); |
658 | ev_io_start (EV_A_ &sigev); |
644 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
659 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
645 | } |
660 | } |
646 | |
661 | |
647 | /*****************************************************************************/ |
662 | /*****************************************************************************/ |
648 | |
663 | |
649 | static struct ev_child *childs [PID_HASHSIZE]; |
664 | static struct ev_child *childs [PID_HASHSIZE]; |
650 | |
665 | |
651 | #ifndef WIN32 |
666 | #ifndef _WIN32 |
652 | |
667 | |
653 | static struct ev_signal childev; |
668 | static struct ev_signal childev; |
654 | |
669 | |
655 | #ifndef WCONTINUED |
670 | #ifndef WCONTINUED |
656 | # define WCONTINUED 0 |
671 | # define WCONTINUED 0 |
… | |
… | |
717 | |
732 | |
718 | /* return true if we are running with elevated privileges and should ignore env variables */ |
733 | /* return true if we are running with elevated privileges and should ignore env variables */ |
719 | static int |
734 | static int |
720 | enable_secure (void) |
735 | enable_secure (void) |
721 | { |
736 | { |
722 | #ifdef WIN32 |
737 | #ifdef _WIN32 |
723 | return 0; |
738 | return 0; |
724 | #else |
739 | #else |
725 | return getuid () != geteuid () |
740 | return getuid () != geteuid () |
726 | || getgid () != getegid (); |
741 | || getgid () != getegid (); |
727 | #endif |
742 | #endif |
… | |
… | |
756 | methods = atoi (getenv ("LIBEV_METHODS")); |
771 | methods = atoi (getenv ("LIBEV_METHODS")); |
757 | else |
772 | else |
758 | methods = EVMETHOD_ANY; |
773 | methods = EVMETHOD_ANY; |
759 | |
774 | |
760 | method = 0; |
775 | method = 0; |
761 | #if EV_USE_WIN32 |
|
|
762 | if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); |
|
|
763 | #endif |
|
|
764 | #if EV_USE_KQUEUE |
776 | #if EV_USE_KQUEUE |
765 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
777 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
766 | #endif |
778 | #endif |
767 | #if EV_USE_EPOLL |
779 | #if EV_USE_EPOLL |
768 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
780 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
… | |
… | |
782 | void |
794 | void |
783 | loop_destroy (EV_P) |
795 | loop_destroy (EV_P) |
784 | { |
796 | { |
785 | int i; |
797 | int i; |
786 | |
798 | |
787 | #if EV_USE_WIN32 |
|
|
788 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
|
|
789 | #endif |
|
|
790 | #if EV_USE_KQUEUE |
799 | #if EV_USE_KQUEUE |
791 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
800 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
792 | #endif |
801 | #endif |
793 | #if EV_USE_EPOLL |
802 | #if EV_USE_EPOLL |
794 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
803 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
… | |
… | |
802 | |
811 | |
803 | for (i = NUMPRI; i--; ) |
812 | for (i = NUMPRI; i--; ) |
804 | array_free (pending, [i]); |
813 | array_free (pending, [i]); |
805 | |
814 | |
806 | /* have to use the microsoft-never-gets-it-right macro */ |
815 | /* have to use the microsoft-never-gets-it-right macro */ |
807 | array_free_microshit (fdchange); |
816 | array_free (fdchange, EMPTY); |
808 | array_free_microshit (timer); |
817 | array_free (timer, EMPTY); |
809 | array_free_microshit (periodic); |
818 | #if EV_PERIODICS |
810 | array_free_microshit (idle); |
819 | array_free (periodic, EMPTY); |
811 | array_free_microshit (prepare); |
820 | #endif |
812 | array_free_microshit (check); |
821 | array_free (idle, EMPTY); |
|
|
822 | array_free (prepare, EMPTY); |
|
|
823 | array_free (check, EMPTY); |
813 | |
824 | |
814 | method = 0; |
825 | method = 0; |
815 | } |
826 | } |
816 | |
827 | |
817 | static void |
828 | static void |
… | |
… | |
896 | |
907 | |
897 | if (ev_method (EV_A)) |
908 | if (ev_method (EV_A)) |
898 | { |
909 | { |
899 | siginit (EV_A); |
910 | siginit (EV_A); |
900 | |
911 | |
901 | #ifndef WIN32 |
912 | #ifndef _WIN32 |
902 | ev_signal_init (&childev, childcb, SIGCHLD); |
913 | ev_signal_init (&childev, childcb, SIGCHLD); |
903 | ev_set_priority (&childev, EV_MAXPRI); |
914 | ev_set_priority (&childev, EV_MAXPRI); |
904 | ev_signal_start (EV_A_ &childev); |
915 | ev_signal_start (EV_A_ &childev); |
905 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
916 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
906 | #endif |
917 | #endif |
… | |
… | |
917 | { |
928 | { |
918 | #if EV_MULTIPLICITY |
929 | #if EV_MULTIPLICITY |
919 | struct ev_loop *loop = default_loop; |
930 | struct ev_loop *loop = default_loop; |
920 | #endif |
931 | #endif |
921 | |
932 | |
922 | #ifndef WIN32 |
933 | #ifndef _WIN32 |
923 | ev_ref (EV_A); /* child watcher */ |
934 | ev_ref (EV_A); /* child watcher */ |
924 | ev_signal_stop (EV_A_ &childev); |
935 | ev_signal_stop (EV_A_ &childev); |
925 | #endif |
936 | #endif |
926 | |
937 | |
927 | ev_ref (EV_A); /* signal watcher */ |
938 | ev_ref (EV_A); /* signal watcher */ |
… | |
… | |
1001 | |
1012 | |
1002 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1013 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1003 | } |
1014 | } |
1004 | } |
1015 | } |
1005 | |
1016 | |
|
|
1017 | #if EV_PERIODICS |
1006 | static void |
1018 | static void |
1007 | periodics_reify (EV_P) |
1019 | periodics_reify (EV_P) |
1008 | { |
1020 | { |
1009 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1021 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1010 | { |
1022 | { |
… | |
… | |
1051 | |
1063 | |
1052 | /* now rebuild the heap */ |
1064 | /* now rebuild the heap */ |
1053 | for (i = periodiccnt >> 1; i--; ) |
1065 | for (i = periodiccnt >> 1; i--; ) |
1054 | downheap ((WT *)periodics, periodiccnt, i); |
1066 | downheap ((WT *)periodics, periodiccnt, i); |
1055 | } |
1067 | } |
|
|
1068 | #endif |
1056 | |
1069 | |
1057 | inline int |
1070 | inline int |
1058 | time_update_monotonic (EV_P) |
1071 | time_update_monotonic (EV_P) |
1059 | { |
1072 | { |
1060 | mn_now = get_clock (); |
1073 | mn_now = get_clock (); |
… | |
… | |
1094 | ev_rt_now = ev_time (); |
1107 | ev_rt_now = ev_time (); |
1095 | mn_now = get_clock (); |
1108 | mn_now = get_clock (); |
1096 | now_floor = mn_now; |
1109 | now_floor = mn_now; |
1097 | } |
1110 | } |
1098 | |
1111 | |
|
|
1112 | # if EV_PERIODICS |
1099 | periodics_reschedule (EV_A); |
1113 | periodics_reschedule (EV_A); |
|
|
1114 | # endif |
1100 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1115 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1101 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1116 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1102 | } |
1117 | } |
1103 | } |
1118 | } |
1104 | else |
1119 | else |
… | |
… | |
1106 | { |
1121 | { |
1107 | ev_rt_now = ev_time (); |
1122 | ev_rt_now = ev_time (); |
1108 | |
1123 | |
1109 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1124 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1110 | { |
1125 | { |
|
|
1126 | #if EV_PERIODICS |
1111 | periodics_reschedule (EV_A); |
1127 | periodics_reschedule (EV_A); |
|
|
1128 | #endif |
1112 | |
1129 | |
1113 | /* adjust timers. this is easy, as the offset is the same for all */ |
1130 | /* adjust timers. this is easy, as the offset is the same for all */ |
1114 | for (i = 0; i < timercnt; ++i) |
1131 | for (i = 0; i < timercnt; ++i) |
1115 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1132 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1116 | } |
1133 | } |
… | |
… | |
1179 | { |
1196 | { |
1180 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1197 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1181 | if (block > to) block = to; |
1198 | if (block > to) block = to; |
1182 | } |
1199 | } |
1183 | |
1200 | |
|
|
1201 | #if EV_PERIODICS |
1184 | if (periodiccnt) |
1202 | if (periodiccnt) |
1185 | { |
1203 | { |
1186 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1204 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1187 | if (block > to) block = to; |
1205 | if (block > to) block = to; |
1188 | } |
1206 | } |
|
|
1207 | #endif |
1189 | |
1208 | |
1190 | if (block < 0.) block = 0.; |
1209 | if (block < 0.) block = 0.; |
1191 | } |
1210 | } |
1192 | |
1211 | |
1193 | method_poll (EV_A_ block); |
1212 | method_poll (EV_A_ block); |
… | |
… | |
1195 | /* update ev_rt_now, do magic */ |
1214 | /* update ev_rt_now, do magic */ |
1196 | time_update (EV_A); |
1215 | time_update (EV_A); |
1197 | |
1216 | |
1198 | /* queue pending timers and reschedule them */ |
1217 | /* queue pending timers and reschedule them */ |
1199 | timers_reify (EV_A); /* relative timers called last */ |
1218 | timers_reify (EV_A); /* relative timers called last */ |
|
|
1219 | #if EV_PERIODICS |
1200 | periodics_reify (EV_A); /* absolute timers called first */ |
1220 | periodics_reify (EV_A); /* absolute timers called first */ |
|
|
1221 | #endif |
1201 | |
1222 | |
1202 | /* queue idle watchers unless io or timers are pending */ |
1223 | /* queue idle watchers unless io or timers are pending */ |
1203 | if (idlecnt && !any_pending (EV_A)) |
1224 | if (idlecnt && !any_pending (EV_A)) |
1204 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1225 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1205 | |
1226 | |
… | |
… | |
1334 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1355 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1335 | |
1356 | |
1336 | if (((W)w)->active < timercnt--) |
1357 | if (((W)w)->active < timercnt--) |
1337 | { |
1358 | { |
1338 | timers [((W)w)->active - 1] = timers [timercnt]; |
1359 | timers [((W)w)->active - 1] = timers [timercnt]; |
1339 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1360 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1340 | } |
1361 | } |
1341 | |
1362 | |
1342 | ((WT)w)->at -= mn_now; |
1363 | ((WT)w)->at -= mn_now; |
1343 | |
1364 | |
1344 | ev_stop (EV_A_ (W)w); |
1365 | ev_stop (EV_A_ (W)w); |
… | |
… | |
1348 | ev_timer_again (EV_P_ struct ev_timer *w) |
1369 | ev_timer_again (EV_P_ struct ev_timer *w) |
1349 | { |
1370 | { |
1350 | if (ev_is_active (w)) |
1371 | if (ev_is_active (w)) |
1351 | { |
1372 | { |
1352 | if (w->repeat) |
1373 | if (w->repeat) |
|
|
1374 | { |
|
|
1375 | ((WT)w)->at = mn_now + w->repeat; |
1353 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); |
1376 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
|
|
1377 | } |
1354 | else |
1378 | else |
1355 | ev_timer_stop (EV_A_ w); |
1379 | ev_timer_stop (EV_A_ w); |
1356 | } |
1380 | } |
1357 | else if (w->repeat) |
1381 | else if (w->repeat) |
1358 | ev_timer_start (EV_A_ w); |
1382 | ev_timer_start (EV_A_ w); |
1359 | } |
1383 | } |
1360 | |
1384 | |
|
|
1385 | #if EV_PERIODICS |
1361 | void |
1386 | void |
1362 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1387 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1363 | { |
1388 | { |
1364 | if (ev_is_active (w)) |
1389 | if (ev_is_active (w)) |
1365 | return; |
1390 | return; |
… | |
… | |
1391 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1416 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1392 | |
1417 | |
1393 | if (((W)w)->active < periodiccnt--) |
1418 | if (((W)w)->active < periodiccnt--) |
1394 | { |
1419 | { |
1395 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1420 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1396 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1421 | adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1397 | } |
1422 | } |
1398 | |
1423 | |
1399 | ev_stop (EV_A_ (W)w); |
1424 | ev_stop (EV_A_ (W)w); |
1400 | } |
1425 | } |
1401 | |
1426 | |
… | |
… | |
1404 | { |
1429 | { |
1405 | /* TODO: use adjustheap and recalculation */ |
1430 | /* TODO: use adjustheap and recalculation */ |
1406 | ev_periodic_stop (EV_A_ w); |
1431 | ev_periodic_stop (EV_A_ w); |
1407 | ev_periodic_start (EV_A_ w); |
1432 | ev_periodic_start (EV_A_ w); |
1408 | } |
1433 | } |
|
|
1434 | #endif |
1409 | |
1435 | |
1410 | void |
1436 | void |
1411 | ev_idle_start (EV_P_ struct ev_idle *w) |
1437 | ev_idle_start (EV_P_ struct ev_idle *w) |
1412 | { |
1438 | { |
1413 | if (ev_is_active (w)) |
1439 | if (ev_is_active (w)) |
… | |
… | |
1420 | |
1446 | |
1421 | void |
1447 | void |
1422 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1448 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1423 | { |
1449 | { |
1424 | ev_clear_pending (EV_A_ (W)w); |
1450 | ev_clear_pending (EV_A_ (W)w); |
1425 | if (ev_is_active (w)) |
1451 | if (!ev_is_active (w)) |
1426 | return; |
1452 | return; |
1427 | |
1453 | |
1428 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1454 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1429 | ev_stop (EV_A_ (W)w); |
1455 | ev_stop (EV_A_ (W)w); |
1430 | } |
1456 | } |
… | |
… | |
1442 | |
1468 | |
1443 | void |
1469 | void |
1444 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1470 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1445 | { |
1471 | { |
1446 | ev_clear_pending (EV_A_ (W)w); |
1472 | ev_clear_pending (EV_A_ (W)w); |
1447 | if (ev_is_active (w)) |
1473 | if (!ev_is_active (w)) |
1448 | return; |
1474 | return; |
1449 | |
1475 | |
1450 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1476 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1451 | ev_stop (EV_A_ (W)w); |
1477 | ev_stop (EV_A_ (W)w); |
1452 | } |
1478 | } |
… | |
… | |
1464 | |
1490 | |
1465 | void |
1491 | void |
1466 | ev_check_stop (EV_P_ struct ev_check *w) |
1492 | ev_check_stop (EV_P_ struct ev_check *w) |
1467 | { |
1493 | { |
1468 | ev_clear_pending (EV_A_ (W)w); |
1494 | ev_clear_pending (EV_A_ (W)w); |
1469 | if (ev_is_active (w)) |
1495 | if (!ev_is_active (w)) |
1470 | return; |
1496 | return; |
1471 | |
1497 | |
1472 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1498 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1473 | ev_stop (EV_A_ (W)w); |
1499 | ev_stop (EV_A_ (W)w); |
1474 | } |
1500 | } |
… | |
… | |
1492 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1518 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1493 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1519 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1494 | |
1520 | |
1495 | if (!((WL)w)->next) |
1521 | if (!((WL)w)->next) |
1496 | { |
1522 | { |
1497 | #if WIN32 |
1523 | #if _WIN32 |
1498 | signal (w->signum, sighandler); |
1524 | signal (w->signum, sighandler); |
1499 | #else |
1525 | #else |
1500 | struct sigaction sa; |
1526 | struct sigaction sa; |
1501 | sa.sa_handler = sighandler; |
1527 | sa.sa_handler = sighandler; |
1502 | sigfillset (&sa.sa_mask); |
1528 | sigfillset (&sa.sa_mask); |
… | |
… | |
1535 | |
1561 | |
1536 | void |
1562 | void |
1537 | ev_child_stop (EV_P_ struct ev_child *w) |
1563 | ev_child_stop (EV_P_ struct ev_child *w) |
1538 | { |
1564 | { |
1539 | ev_clear_pending (EV_A_ (W)w); |
1565 | ev_clear_pending (EV_A_ (W)w); |
1540 | if (ev_is_active (w)) |
1566 | if (!ev_is_active (w)) |
1541 | return; |
1567 | return; |
1542 | |
1568 | |
1543 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1569 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1544 | ev_stop (EV_A_ (W)w); |
1570 | ev_stop (EV_A_ (W)w); |
1545 | } |
1571 | } |