… | |
… | |
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 |
117 | #endif |
102 | |
118 | |
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 |
|
|
112 | |
|
|
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 |
116 | |
122 | |
117 | /**/ |
123 | /**/ |
|
|
124 | |
|
|
125 | /* darwin simply cannot be helped */ |
|
|
126 | #ifdef __APPLE__ |
|
|
127 | # undef EV_USE_POLL |
|
|
128 | # undef EV_USE_KQUEUE |
|
|
129 | #endif |
118 | |
130 | |
119 | #ifndef CLOCK_MONOTONIC |
131 | #ifndef CLOCK_MONOTONIC |
120 | # undef EV_USE_MONOTONIC |
132 | # undef EV_USE_MONOTONIC |
121 | # define EV_USE_MONOTONIC 0 |
133 | # define EV_USE_MONOTONIC 0 |
122 | #endif |
134 | #endif |
123 | |
135 | |
124 | #ifndef CLOCK_REALTIME |
136 | #ifndef CLOCK_REALTIME |
125 | # undef EV_USE_REALTIME |
137 | # undef EV_USE_REALTIME |
126 | # define EV_USE_REALTIME 0 |
138 | # define EV_USE_REALTIME 0 |
|
|
139 | #endif |
|
|
140 | |
|
|
141 | #if EV_SELECT_IS_WINSOCKET |
|
|
142 | # include <winsock.h> |
127 | #endif |
143 | #endif |
128 | |
144 | |
129 | /**/ |
145 | /**/ |
130 | |
146 | |
131 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
147 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
… | |
… | |
151 | #define expect_true(expr) expect ((expr) != 0, 1) |
167 | #define expect_true(expr) expect ((expr) != 0, 1) |
152 | |
168 | |
153 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
169 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
154 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
170 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
155 | |
171 | |
|
|
172 | #define EMPTY /* required for microsofts broken pseudo-c compiler */ |
|
|
173 | |
156 | typedef struct ev_watcher *W; |
174 | typedef struct ev_watcher *W; |
157 | typedef struct ev_watcher_list *WL; |
175 | typedef struct ev_watcher_list *WL; |
158 | typedef struct ev_watcher_time *WT; |
176 | typedef struct ev_watcher_time *WT; |
159 | |
177 | |
160 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
178 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
161 | |
179 | |
|
|
180 | #ifdef _WIN32 |
162 | #include "ev_win32.c" |
181 | # include "ev_win32.c" |
|
|
182 | #endif |
163 | |
183 | |
164 | /*****************************************************************************/ |
184 | /*****************************************************************************/ |
165 | |
185 | |
166 | static void (*syserr_cb)(const char *msg); |
186 | static void (*syserr_cb)(const char *msg); |
167 | |
187 | |
… | |
… | |
214 | typedef struct |
234 | typedef struct |
215 | { |
235 | { |
216 | WL head; |
236 | WL head; |
217 | unsigned char events; |
237 | unsigned char events; |
218 | unsigned char reify; |
238 | unsigned char reify; |
|
|
239 | #if EV_SELECT_IS_WINSOCKET |
|
|
240 | SOCKET handle; |
|
|
241 | #endif |
219 | } ANFD; |
242 | } ANFD; |
220 | |
243 | |
221 | typedef struct |
244 | typedef struct |
222 | { |
245 | { |
223 | W w; |
246 | W w; |
… | |
… | |
227 | #if EV_MULTIPLICITY |
250 | #if EV_MULTIPLICITY |
228 | |
251 | |
229 | struct ev_loop |
252 | struct ev_loop |
230 | { |
253 | { |
231 | ev_tstamp ev_rt_now; |
254 | ev_tstamp ev_rt_now; |
|
|
255 | #define ev_rt_now ((loop)->ev_rt_now) |
232 | #define VAR(name,decl) decl; |
256 | #define VAR(name,decl) decl; |
233 | #include "ev_vars.h" |
257 | #include "ev_vars.h" |
234 | #undef VAR |
258 | #undef VAR |
235 | }; |
259 | }; |
236 | #include "ev_wrap.h" |
260 | #include "ev_wrap.h" |
… | |
… | |
286 | { |
310 | { |
287 | return ev_rt_now; |
311 | return ev_rt_now; |
288 | } |
312 | } |
289 | #endif |
313 | #endif |
290 | |
314 | |
291 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
315 | #define array_roundsize(type,n) (((n) | 4) & ~3) |
292 | |
316 | |
293 | #define array_needsize(type,base,cur,cnt,init) \ |
317 | #define array_needsize(type,base,cur,cnt,init) \ |
294 | if (expect_false ((cnt) > cur)) \ |
318 | if (expect_false ((cnt) > cur)) \ |
295 | { \ |
319 | { \ |
296 | int newcnt = cur; \ |
320 | int newcnt = cur; \ |
… | |
… | |
311 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
335 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
312 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
336 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
313 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
337 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
314 | } |
338 | } |
315 | |
339 | |
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) \ |
340 | #define array_free(stem, idx) \ |
322 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
341 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
323 | |
342 | |
324 | /*****************************************************************************/ |
343 | /*****************************************************************************/ |
325 | |
344 | |
… | |
… | |
399 | int events = 0; |
418 | int events = 0; |
400 | |
419 | |
401 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
420 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
402 | events |= w->events; |
421 | events |= w->events; |
403 | |
422 | |
|
|
423 | #if EV_SELECT_IS_WINSOCKET |
|
|
424 | if (events) |
|
|
425 | { |
|
|
426 | unsigned long argp; |
|
|
427 | anfd->handle = _get_osfhandle (fd); |
|
|
428 | assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0)); |
|
|
429 | } |
|
|
430 | #endif |
|
|
431 | |
404 | anfd->reify = 0; |
432 | anfd->reify = 0; |
405 | |
433 | |
406 | method_modify (EV_A_ fd, anfd->events, events); |
434 | method_modify (EV_A_ fd, anfd->events, events); |
407 | anfd->events = events; |
435 | anfd->events = events; |
408 | } |
436 | } |
… | |
… | |
436 | } |
464 | } |
437 | |
465 | |
438 | static int |
466 | static int |
439 | fd_valid (int fd) |
467 | fd_valid (int fd) |
440 | { |
468 | { |
441 | #ifdef WIN32 |
469 | #ifdef _WIN32 |
442 | return !!win32_get_osfhandle (fd); |
470 | return _get_osfhandle (fd) != -1; |
443 | #else |
471 | #else |
444 | return fcntl (fd, F_GETFD) != -1; |
472 | return fcntl (fd, F_GETFD) != -1; |
445 | #endif |
473 | #endif |
446 | } |
474 | } |
447 | |
475 | |
… | |
… | |
528 | heap [k] = w; |
556 | heap [k] = w; |
529 | ((W)heap [k])->active = k + 1; |
557 | ((W)heap [k])->active = k + 1; |
530 | } |
558 | } |
531 | |
559 | |
532 | inline void |
560 | inline void |
533 | adjustheap (WT *heap, int N, int k, ev_tstamp at) |
561 | adjustheap (WT *heap, int N, int k) |
534 | { |
562 | { |
535 | ev_tstamp old_at = heap [k]->at; |
563 | upheap (heap, k); |
536 | heap [k]->at = at; |
|
|
537 | |
|
|
538 | if (old_at < at) |
|
|
539 | downheap (heap, N, k); |
564 | downheap (heap, N, k); |
540 | else |
|
|
541 | upheap (heap, k); |
|
|
542 | } |
565 | } |
543 | |
566 | |
544 | /*****************************************************************************/ |
567 | /*****************************************************************************/ |
545 | |
568 | |
546 | typedef struct |
569 | typedef struct |
… | |
… | |
569 | } |
592 | } |
570 | |
593 | |
571 | static void |
594 | static void |
572 | sighandler (int signum) |
595 | sighandler (int signum) |
573 | { |
596 | { |
574 | #if WIN32 |
597 | #if _WIN32 |
575 | signal (signum, sighandler); |
598 | signal (signum, sighandler); |
576 | #endif |
599 | #endif |
577 | |
600 | |
578 | signals [signum - 1].gotsig = 1; |
601 | signals [signum - 1].gotsig = 1; |
579 | |
602 | |
580 | if (!gotsig) |
603 | if (!gotsig) |
581 | { |
604 | { |
582 | int old_errno = errno; |
605 | int old_errno = errno; |
583 | gotsig = 1; |
606 | gotsig = 1; |
584 | #ifdef WIN32 |
|
|
585 | send (sigpipe [1], &signum, 1, MSG_DONTWAIT); |
|
|
586 | #else |
|
|
587 | write (sigpipe [1], &signum, 1); |
607 | write (sigpipe [1], &signum, 1); |
588 | #endif |
|
|
589 | errno = old_errno; |
608 | errno = old_errno; |
590 | } |
609 | } |
591 | } |
610 | } |
592 | |
611 | |
593 | void |
612 | void |
… | |
… | |
613 | static void |
632 | static void |
614 | sigcb (EV_P_ struct ev_io *iow, int revents) |
633 | sigcb (EV_P_ struct ev_io *iow, int revents) |
615 | { |
634 | { |
616 | int signum; |
635 | int signum; |
617 | |
636 | |
618 | #ifdef WIN32 |
|
|
619 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
|
|
620 | #else |
|
|
621 | read (sigpipe [0], &revents, 1); |
637 | read (sigpipe [0], &revents, 1); |
622 | #endif |
|
|
623 | gotsig = 0; |
638 | gotsig = 0; |
624 | |
639 | |
625 | for (signum = signalmax; signum--; ) |
640 | for (signum = signalmax; signum--; ) |
626 | if (signals [signum].gotsig) |
641 | if (signals [signum].gotsig) |
627 | ev_feed_signal_event (EV_A_ signum + 1); |
642 | ev_feed_signal_event (EV_A_ signum + 1); |
628 | } |
643 | } |
629 | |
644 | |
|
|
645 | inline void |
|
|
646 | fd_intern (int fd) |
|
|
647 | { |
|
|
648 | #ifdef _WIN32 |
|
|
649 | int arg = 1; |
|
|
650 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
|
|
651 | #else |
|
|
652 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
653 | fcntl (fd, F_SETFL, O_NONBLOCK); |
|
|
654 | #endif |
|
|
655 | } |
|
|
656 | |
630 | static void |
657 | static void |
631 | siginit (EV_P) |
658 | siginit (EV_P) |
632 | { |
659 | { |
633 | #ifndef WIN32 |
660 | fd_intern (sigpipe [0]); |
634 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
661 | 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 | |
662 | |
642 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
663 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
643 | ev_io_start (EV_A_ &sigev); |
664 | ev_io_start (EV_A_ &sigev); |
644 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
665 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
645 | } |
666 | } |
646 | |
667 | |
647 | /*****************************************************************************/ |
668 | /*****************************************************************************/ |
648 | |
669 | |
649 | static struct ev_child *childs [PID_HASHSIZE]; |
670 | static struct ev_child *childs [PID_HASHSIZE]; |
650 | |
671 | |
651 | #ifndef WIN32 |
672 | #ifndef _WIN32 |
652 | |
673 | |
653 | static struct ev_signal childev; |
674 | static struct ev_signal childev; |
654 | |
675 | |
655 | #ifndef WCONTINUED |
676 | #ifndef WCONTINUED |
656 | # define WCONTINUED 0 |
677 | # define WCONTINUED 0 |
… | |
… | |
717 | |
738 | |
718 | /* return true if we are running with elevated privileges and should ignore env variables */ |
739 | /* return true if we are running with elevated privileges and should ignore env variables */ |
719 | static int |
740 | static int |
720 | enable_secure (void) |
741 | enable_secure (void) |
721 | { |
742 | { |
722 | #ifdef WIN32 |
743 | #ifdef _WIN32 |
723 | return 0; |
744 | return 0; |
724 | #else |
745 | #else |
725 | return getuid () != geteuid () |
746 | return getuid () != geteuid () |
726 | || getgid () != getegid (); |
747 | || getgid () != getegid (); |
727 | #endif |
748 | #endif |
… | |
… | |
732 | { |
753 | { |
733 | return method; |
754 | return method; |
734 | } |
755 | } |
735 | |
756 | |
736 | static void |
757 | static void |
737 | loop_init (EV_P_ int methods) |
758 | loop_init (EV_P_ unsigned int flags) |
738 | { |
759 | { |
739 | if (!method) |
760 | if (!method) |
740 | { |
761 | { |
741 | #if EV_USE_MONOTONIC |
762 | #if EV_USE_MONOTONIC |
742 | { |
763 | { |
… | |
… | |
749 | ev_rt_now = ev_time (); |
770 | ev_rt_now = ev_time (); |
750 | mn_now = get_clock (); |
771 | mn_now = get_clock (); |
751 | now_floor = mn_now; |
772 | now_floor = mn_now; |
752 | rtmn_diff = ev_rt_now - mn_now; |
773 | rtmn_diff = ev_rt_now - mn_now; |
753 | |
774 | |
754 | if (methods == EVMETHOD_AUTO) |
775 | if (!(flags & EVMETHOD_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS")) |
755 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
756 | methods = atoi (getenv ("LIBEV_METHODS")); |
776 | flags = atoi (getenv ("LIBEV_FLAGS")); |
757 | else |
777 | |
758 | methods = EVMETHOD_ANY; |
778 | if (!(flags & 0x0000ffff)) |
|
|
779 | flags |= 0x0000ffff; |
759 | |
780 | |
760 | method = 0; |
781 | 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 |
782 | #if EV_USE_KQUEUE |
765 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
783 | if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags); |
766 | #endif |
784 | #endif |
767 | #if EV_USE_EPOLL |
785 | #if EV_USE_EPOLL |
768 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
786 | if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags); |
769 | #endif |
787 | #endif |
770 | #if EV_USE_POLL |
788 | #if EV_USE_POLL |
771 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
789 | if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags); |
772 | #endif |
790 | #endif |
773 | #if EV_USE_SELECT |
791 | #if EV_USE_SELECT |
774 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
792 | if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags); |
775 | #endif |
793 | #endif |
776 | |
794 | |
777 | ev_init (&sigev, sigcb); |
795 | ev_init (&sigev, sigcb); |
778 | ev_set_priority (&sigev, EV_MAXPRI); |
796 | ev_set_priority (&sigev, EV_MAXPRI); |
779 | } |
797 | } |
… | |
… | |
782 | void |
800 | void |
783 | loop_destroy (EV_P) |
801 | loop_destroy (EV_P) |
784 | { |
802 | { |
785 | int i; |
803 | int i; |
786 | |
804 | |
787 | #if EV_USE_WIN32 |
|
|
788 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
|
|
789 | #endif |
|
|
790 | #if EV_USE_KQUEUE |
805 | #if EV_USE_KQUEUE |
791 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
806 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
792 | #endif |
807 | #endif |
793 | #if EV_USE_EPOLL |
808 | #if EV_USE_EPOLL |
794 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
809 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
… | |
… | |
802 | |
817 | |
803 | for (i = NUMPRI; i--; ) |
818 | for (i = NUMPRI; i--; ) |
804 | array_free (pending, [i]); |
819 | array_free (pending, [i]); |
805 | |
820 | |
806 | /* have to use the microsoft-never-gets-it-right macro */ |
821 | /* have to use the microsoft-never-gets-it-right macro */ |
807 | array_free_microshit (fdchange); |
822 | array_free (fdchange, EMPTY); |
808 | array_free_microshit (timer); |
823 | array_free (timer, EMPTY); |
809 | array_free_microshit (periodic); |
824 | #if EV_PERIODICS |
810 | array_free_microshit (idle); |
825 | array_free (periodic, EMPTY); |
811 | array_free_microshit (prepare); |
826 | #endif |
812 | array_free_microshit (check); |
827 | array_free (idle, EMPTY); |
|
|
828 | array_free (prepare, EMPTY); |
|
|
829 | array_free (check, EMPTY); |
813 | |
830 | |
814 | method = 0; |
831 | method = 0; |
815 | } |
832 | } |
816 | |
833 | |
817 | static void |
834 | static void |
… | |
… | |
842 | postfork = 0; |
859 | postfork = 0; |
843 | } |
860 | } |
844 | |
861 | |
845 | #if EV_MULTIPLICITY |
862 | #if EV_MULTIPLICITY |
846 | struct ev_loop * |
863 | struct ev_loop * |
847 | ev_loop_new (int methods) |
864 | ev_loop_new (unsigned int flags) |
848 | { |
865 | { |
849 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
866 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
850 | |
867 | |
851 | memset (loop, 0, sizeof (struct ev_loop)); |
868 | memset (loop, 0, sizeof (struct ev_loop)); |
852 | |
869 | |
853 | loop_init (EV_A_ methods); |
870 | loop_init (EV_A_ flags); |
854 | |
871 | |
855 | if (ev_method (EV_A)) |
872 | if (ev_method (EV_A)) |
856 | return loop; |
873 | return loop; |
857 | |
874 | |
858 | return 0; |
875 | return 0; |
… | |
… | |
876 | #if EV_MULTIPLICITY |
893 | #if EV_MULTIPLICITY |
877 | struct ev_loop * |
894 | struct ev_loop * |
878 | #else |
895 | #else |
879 | int |
896 | int |
880 | #endif |
897 | #endif |
881 | ev_default_loop (int methods) |
898 | ev_default_loop (unsigned int methods) |
882 | { |
899 | { |
883 | if (sigpipe [0] == sigpipe [1]) |
900 | if (sigpipe [0] == sigpipe [1]) |
884 | if (pipe (sigpipe)) |
901 | if (pipe (sigpipe)) |
885 | return 0; |
902 | return 0; |
886 | |
903 | |
… | |
… | |
896 | |
913 | |
897 | if (ev_method (EV_A)) |
914 | if (ev_method (EV_A)) |
898 | { |
915 | { |
899 | siginit (EV_A); |
916 | siginit (EV_A); |
900 | |
917 | |
901 | #ifndef WIN32 |
918 | #ifndef _WIN32 |
902 | ev_signal_init (&childev, childcb, SIGCHLD); |
919 | ev_signal_init (&childev, childcb, SIGCHLD); |
903 | ev_set_priority (&childev, EV_MAXPRI); |
920 | ev_set_priority (&childev, EV_MAXPRI); |
904 | ev_signal_start (EV_A_ &childev); |
921 | ev_signal_start (EV_A_ &childev); |
905 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
922 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
906 | #endif |
923 | #endif |
… | |
… | |
917 | { |
934 | { |
918 | #if EV_MULTIPLICITY |
935 | #if EV_MULTIPLICITY |
919 | struct ev_loop *loop = default_loop; |
936 | struct ev_loop *loop = default_loop; |
920 | #endif |
937 | #endif |
921 | |
938 | |
922 | #ifndef WIN32 |
939 | #ifndef _WIN32 |
923 | ev_ref (EV_A); /* child watcher */ |
940 | ev_ref (EV_A); /* child watcher */ |
924 | ev_signal_stop (EV_A_ &childev); |
941 | ev_signal_stop (EV_A_ &childev); |
925 | #endif |
942 | #endif |
926 | |
943 | |
927 | ev_ref (EV_A); /* signal watcher */ |
944 | ev_ref (EV_A); /* signal watcher */ |
… | |
… | |
1001 | |
1018 | |
1002 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1019 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1003 | } |
1020 | } |
1004 | } |
1021 | } |
1005 | |
1022 | |
|
|
1023 | #if EV_PERIODICS |
1006 | static void |
1024 | static void |
1007 | periodics_reify (EV_P) |
1025 | periodics_reify (EV_P) |
1008 | { |
1026 | { |
1009 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1027 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1010 | { |
1028 | { |
… | |
… | |
1013 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1031 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1014 | |
1032 | |
1015 | /* first reschedule or stop timer */ |
1033 | /* first reschedule or stop timer */ |
1016 | if (w->reschedule_cb) |
1034 | if (w->reschedule_cb) |
1017 | { |
1035 | { |
1018 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
1036 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
1019 | |
|
|
1020 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
1037 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
1021 | downheap ((WT *)periodics, periodiccnt, 0); |
1038 | downheap ((WT *)periodics, periodiccnt, 0); |
1022 | } |
1039 | } |
1023 | else if (w->interval) |
1040 | else if (w->interval) |
1024 | { |
1041 | { |
… | |
… | |
1051 | |
1068 | |
1052 | /* now rebuild the heap */ |
1069 | /* now rebuild the heap */ |
1053 | for (i = periodiccnt >> 1; i--; ) |
1070 | for (i = periodiccnt >> 1; i--; ) |
1054 | downheap ((WT *)periodics, periodiccnt, i); |
1071 | downheap ((WT *)periodics, periodiccnt, i); |
1055 | } |
1072 | } |
|
|
1073 | #endif |
1056 | |
1074 | |
1057 | inline int |
1075 | inline int |
1058 | time_update_monotonic (EV_P) |
1076 | time_update_monotonic (EV_P) |
1059 | { |
1077 | { |
1060 | mn_now = get_clock (); |
1078 | mn_now = get_clock (); |
… | |
… | |
1094 | ev_rt_now = ev_time (); |
1112 | ev_rt_now = ev_time (); |
1095 | mn_now = get_clock (); |
1113 | mn_now = get_clock (); |
1096 | now_floor = mn_now; |
1114 | now_floor = mn_now; |
1097 | } |
1115 | } |
1098 | |
1116 | |
|
|
1117 | # if EV_PERIODICS |
1099 | periodics_reschedule (EV_A); |
1118 | periodics_reschedule (EV_A); |
|
|
1119 | # endif |
1100 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1120 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1101 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1121 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1102 | } |
1122 | } |
1103 | } |
1123 | } |
1104 | else |
1124 | else |
… | |
… | |
1106 | { |
1126 | { |
1107 | ev_rt_now = ev_time (); |
1127 | ev_rt_now = ev_time (); |
1108 | |
1128 | |
1109 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1129 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1110 | { |
1130 | { |
|
|
1131 | #if EV_PERIODICS |
1111 | periodics_reschedule (EV_A); |
1132 | periodics_reschedule (EV_A); |
|
|
1133 | #endif |
1112 | |
1134 | |
1113 | /* adjust timers. this is easy, as the offset is the same for all */ |
1135 | /* adjust timers. this is easy, as the offset is the same for all */ |
1114 | for (i = 0; i < timercnt; ++i) |
1136 | for (i = 0; i < timercnt; ++i) |
1115 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1137 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1116 | } |
1138 | } |
… | |
… | |
1179 | { |
1201 | { |
1180 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1202 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1181 | if (block > to) block = to; |
1203 | if (block > to) block = to; |
1182 | } |
1204 | } |
1183 | |
1205 | |
|
|
1206 | #if EV_PERIODICS |
1184 | if (periodiccnt) |
1207 | if (periodiccnt) |
1185 | { |
1208 | { |
1186 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1209 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1187 | if (block > to) block = to; |
1210 | if (block > to) block = to; |
1188 | } |
1211 | } |
|
|
1212 | #endif |
1189 | |
1213 | |
1190 | if (block < 0.) block = 0.; |
1214 | if (block < 0.) block = 0.; |
1191 | } |
1215 | } |
1192 | |
1216 | |
1193 | method_poll (EV_A_ block); |
1217 | method_poll (EV_A_ block); |
… | |
… | |
1195 | /* update ev_rt_now, do magic */ |
1219 | /* update ev_rt_now, do magic */ |
1196 | time_update (EV_A); |
1220 | time_update (EV_A); |
1197 | |
1221 | |
1198 | /* queue pending timers and reschedule them */ |
1222 | /* queue pending timers and reschedule them */ |
1199 | timers_reify (EV_A); /* relative timers called last */ |
1223 | timers_reify (EV_A); /* relative timers called last */ |
|
|
1224 | #if EV_PERIODICS |
1200 | periodics_reify (EV_A); /* absolute timers called first */ |
1225 | periodics_reify (EV_A); /* absolute timers called first */ |
|
|
1226 | #endif |
1201 | |
1227 | |
1202 | /* queue idle watchers unless io or timers are pending */ |
1228 | /* queue idle watchers unless io or timers are pending */ |
1203 | if (idlecnt && !any_pending (EV_A)) |
1229 | if (idlecnt && !any_pending (EV_A)) |
1204 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1230 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1205 | |
1231 | |
… | |
… | |
1334 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1360 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1335 | |
1361 | |
1336 | if (((W)w)->active < timercnt--) |
1362 | if (((W)w)->active < timercnt--) |
1337 | { |
1363 | { |
1338 | timers [((W)w)->active - 1] = timers [timercnt]; |
1364 | timers [((W)w)->active - 1] = timers [timercnt]; |
1339 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1365 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1340 | } |
1366 | } |
1341 | |
1367 | |
1342 | ((WT)w)->at -= mn_now; |
1368 | ((WT)w)->at -= mn_now; |
1343 | |
1369 | |
1344 | ev_stop (EV_A_ (W)w); |
1370 | ev_stop (EV_A_ (W)w); |
… | |
… | |
1348 | ev_timer_again (EV_P_ struct ev_timer *w) |
1374 | ev_timer_again (EV_P_ struct ev_timer *w) |
1349 | { |
1375 | { |
1350 | if (ev_is_active (w)) |
1376 | if (ev_is_active (w)) |
1351 | { |
1377 | { |
1352 | if (w->repeat) |
1378 | if (w->repeat) |
|
|
1379 | { |
|
|
1380 | ((WT)w)->at = mn_now + w->repeat; |
1353 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); |
1381 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
|
|
1382 | } |
1354 | else |
1383 | else |
1355 | ev_timer_stop (EV_A_ w); |
1384 | ev_timer_stop (EV_A_ w); |
1356 | } |
1385 | } |
1357 | else if (w->repeat) |
1386 | else if (w->repeat) |
1358 | ev_timer_start (EV_A_ w); |
1387 | ev_timer_start (EV_A_ w); |
1359 | } |
1388 | } |
1360 | |
1389 | |
|
|
1390 | #if EV_PERIODICS |
1361 | void |
1391 | void |
1362 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1392 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1363 | { |
1393 | { |
1364 | if (ev_is_active (w)) |
1394 | if (ev_is_active (w)) |
1365 | return; |
1395 | return; |
… | |
… | |
1391 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1421 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1392 | |
1422 | |
1393 | if (((W)w)->active < periodiccnt--) |
1423 | if (((W)w)->active < periodiccnt--) |
1394 | { |
1424 | { |
1395 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1425 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1396 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1426 | adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1397 | } |
1427 | } |
1398 | |
1428 | |
1399 | ev_stop (EV_A_ (W)w); |
1429 | ev_stop (EV_A_ (W)w); |
1400 | } |
1430 | } |
1401 | |
1431 | |
… | |
… | |
1404 | { |
1434 | { |
1405 | /* TODO: use adjustheap and recalculation */ |
1435 | /* TODO: use adjustheap and recalculation */ |
1406 | ev_periodic_stop (EV_A_ w); |
1436 | ev_periodic_stop (EV_A_ w); |
1407 | ev_periodic_start (EV_A_ w); |
1437 | ev_periodic_start (EV_A_ w); |
1408 | } |
1438 | } |
|
|
1439 | #endif |
1409 | |
1440 | |
1410 | void |
1441 | void |
1411 | ev_idle_start (EV_P_ struct ev_idle *w) |
1442 | ev_idle_start (EV_P_ struct ev_idle *w) |
1412 | { |
1443 | { |
1413 | if (ev_is_active (w)) |
1444 | if (ev_is_active (w)) |
… | |
… | |
1420 | |
1451 | |
1421 | void |
1452 | void |
1422 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1453 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1423 | { |
1454 | { |
1424 | ev_clear_pending (EV_A_ (W)w); |
1455 | ev_clear_pending (EV_A_ (W)w); |
1425 | if (ev_is_active (w)) |
1456 | if (!ev_is_active (w)) |
1426 | return; |
1457 | return; |
1427 | |
1458 | |
1428 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1459 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1429 | ev_stop (EV_A_ (W)w); |
1460 | ev_stop (EV_A_ (W)w); |
1430 | } |
1461 | } |
… | |
… | |
1442 | |
1473 | |
1443 | void |
1474 | void |
1444 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1475 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1445 | { |
1476 | { |
1446 | ev_clear_pending (EV_A_ (W)w); |
1477 | ev_clear_pending (EV_A_ (W)w); |
1447 | if (ev_is_active (w)) |
1478 | if (!ev_is_active (w)) |
1448 | return; |
1479 | return; |
1449 | |
1480 | |
1450 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1481 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1451 | ev_stop (EV_A_ (W)w); |
1482 | ev_stop (EV_A_ (W)w); |
1452 | } |
1483 | } |
… | |
… | |
1464 | |
1495 | |
1465 | void |
1496 | void |
1466 | ev_check_stop (EV_P_ struct ev_check *w) |
1497 | ev_check_stop (EV_P_ struct ev_check *w) |
1467 | { |
1498 | { |
1468 | ev_clear_pending (EV_A_ (W)w); |
1499 | ev_clear_pending (EV_A_ (W)w); |
1469 | if (ev_is_active (w)) |
1500 | if (!ev_is_active (w)) |
1470 | return; |
1501 | return; |
1471 | |
1502 | |
1472 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1503 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1473 | ev_stop (EV_A_ (W)w); |
1504 | ev_stop (EV_A_ (W)w); |
1474 | } |
1505 | } |
… | |
… | |
1492 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1523 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1493 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1524 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1494 | |
1525 | |
1495 | if (!((WL)w)->next) |
1526 | if (!((WL)w)->next) |
1496 | { |
1527 | { |
1497 | #if WIN32 |
1528 | #if _WIN32 |
1498 | signal (w->signum, sighandler); |
1529 | signal (w->signum, sighandler); |
1499 | #else |
1530 | #else |
1500 | struct sigaction sa; |
1531 | struct sigaction sa; |
1501 | sa.sa_handler = sighandler; |
1532 | sa.sa_handler = sighandler; |
1502 | sigfillset (&sa.sa_mask); |
1533 | sigfillset (&sa.sa_mask); |
… | |
… | |
1535 | |
1566 | |
1536 | void |
1567 | void |
1537 | ev_child_stop (EV_P_ struct ev_child *w) |
1568 | ev_child_stop (EV_P_ struct ev_child *w) |
1538 | { |
1569 | { |
1539 | ev_clear_pending (EV_A_ (W)w); |
1570 | ev_clear_pending (EV_A_ (W)w); |
1540 | if (ev_is_active (w)) |
1571 | if (!ev_is_active (w)) |
1541 | return; |
1572 | return; |
1542 | |
1573 | |
1543 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1574 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1544 | ev_stop (EV_A_ (W)w); |
1575 | ev_stop (EV_A_ (W)w); |
1545 | } |
1576 | } |