… | |
… | |
75 | #include <sys/types.h> |
75 | #include <sys/types.h> |
76 | #include <time.h> |
76 | #include <time.h> |
77 | |
77 | |
78 | #include <signal.h> |
78 | #include <signal.h> |
79 | |
79 | |
80 | #ifndef WIN32 |
80 | #ifndef _WIN32 |
81 | # include <unistd.h> |
81 | # include <unistd.h> |
82 | # include <sys/time.h> |
82 | # include <sys/time.h> |
83 | # 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 |
84 | #endif |
89 | # endif |
|
|
90 | #endif |
|
|
91 | |
85 | /**/ |
92 | /**/ |
86 | |
93 | |
87 | #ifndef EV_USE_MONOTONIC |
94 | #ifndef EV_USE_MONOTONIC |
88 | # define EV_USE_MONOTONIC 1 |
95 | # define EV_USE_MONOTONIC 1 |
89 | #endif |
96 | #endif |
90 | |
97 | |
91 | #ifndef EV_USE_SELECT |
98 | #ifndef EV_USE_SELECT |
92 | # define EV_USE_SELECT 1 |
99 | # define EV_USE_SELECT 1 |
|
|
100 | # define EV_SELECT_USE_FD_SET 1 |
93 | #endif |
101 | #endif |
94 | |
102 | |
95 | #ifndef EV_USE_POLL |
103 | #ifndef EV_USE_POLL |
96 | # 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 |
97 | #endif |
109 | #endif |
98 | |
110 | |
99 | #ifndef EV_USE_EPOLL |
111 | #ifndef EV_USE_EPOLL |
100 | # define EV_USE_EPOLL 0 |
112 | # define EV_USE_EPOLL 0 |
101 | #endif |
113 | #endif |
102 | |
114 | |
103 | #ifndef EV_USE_KQUEUE |
115 | #ifndef EV_USE_KQUEUE |
104 | # define EV_USE_KQUEUE 0 |
116 | # define EV_USE_KQUEUE 0 |
105 | #endif |
117 | #endif |
106 | |
118 | |
107 | #ifndef EV_USE_WIN32 |
|
|
108 | # ifdef WIN32 |
|
|
109 | # define EV_USE_WIN32 0 /* it does not exist, use select */ |
|
|
110 | # undef EV_USE_SELECT |
|
|
111 | # define EV_USE_SELECT 1 |
|
|
112 | # else |
|
|
113 | # define EV_USE_WIN32 0 |
|
|
114 | # endif |
|
|
115 | #endif |
|
|
116 | |
|
|
117 | #ifndef EV_USE_REALTIME |
119 | #ifndef EV_USE_REALTIME |
118 | # define EV_USE_REALTIME 1 |
120 | # define EV_USE_REALTIME 1 |
119 | #endif |
121 | #endif |
120 | |
122 | |
121 | /**/ |
123 | /**/ |
|
|
124 | |
|
|
125 | /* darwin simply cannot be helped */ |
|
|
126 | #ifdef __APPLE__ |
|
|
127 | # undef EV_USE_POLL |
|
|
128 | # undef EV_USE_KQUEUE |
|
|
129 | #endif |
122 | |
130 | |
123 | #ifndef CLOCK_MONOTONIC |
131 | #ifndef CLOCK_MONOTONIC |
124 | # undef EV_USE_MONOTONIC |
132 | # undef EV_USE_MONOTONIC |
125 | # define EV_USE_MONOTONIC 0 |
133 | # define EV_USE_MONOTONIC 0 |
126 | #endif |
134 | #endif |
127 | |
135 | |
128 | #ifndef CLOCK_REALTIME |
136 | #ifndef CLOCK_REALTIME |
129 | # undef EV_USE_REALTIME |
137 | # undef EV_USE_REALTIME |
130 | # define EV_USE_REALTIME 0 |
138 | # define EV_USE_REALTIME 0 |
|
|
139 | #endif |
|
|
140 | |
|
|
141 | #if EV_SELECT_IS_WINSOCKET |
|
|
142 | # include <winsock.h> |
131 | #endif |
143 | #endif |
132 | |
144 | |
133 | /**/ |
145 | /**/ |
134 | |
146 | |
135 | #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) */ |
… | |
… | |
155 | #define expect_true(expr) expect ((expr) != 0, 1) |
167 | #define expect_true(expr) expect ((expr) != 0, 1) |
156 | |
168 | |
157 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
169 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
158 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
170 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
159 | |
171 | |
|
|
172 | #define EMPTY0 /* required for microsofts broken pseudo-c compiler */ |
|
|
173 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
|
|
174 | |
160 | typedef struct ev_watcher *W; |
175 | typedef struct ev_watcher *W; |
161 | typedef struct ev_watcher_list *WL; |
176 | typedef struct ev_watcher_list *WL; |
162 | typedef struct ev_watcher_time *WT; |
177 | typedef struct ev_watcher_time *WT; |
163 | |
178 | |
164 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
179 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
165 | |
180 | |
166 | #ifdef WIN32 |
181 | #ifdef _WIN32 |
167 | # include "ev_win32.c" |
182 | # include "ev_win32.c" |
168 | #endif |
183 | #endif |
169 | |
184 | |
170 | /*****************************************************************************/ |
185 | /*****************************************************************************/ |
171 | |
186 | |
… | |
… | |
220 | typedef struct |
235 | typedef struct |
221 | { |
236 | { |
222 | WL head; |
237 | WL head; |
223 | unsigned char events; |
238 | unsigned char events; |
224 | unsigned char reify; |
239 | unsigned char reify; |
|
|
240 | #if EV_SELECT_IS_WINSOCKET |
|
|
241 | SOCKET handle; |
|
|
242 | #endif |
225 | } ANFD; |
243 | } ANFD; |
226 | |
244 | |
227 | typedef struct |
245 | typedef struct |
228 | { |
246 | { |
229 | W w; |
247 | W w; |
… | |
… | |
293 | { |
311 | { |
294 | return ev_rt_now; |
312 | return ev_rt_now; |
295 | } |
313 | } |
296 | #endif |
314 | #endif |
297 | |
315 | |
298 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
316 | #define array_roundsize(type,n) (((n) | 4) & ~3) |
299 | |
317 | |
300 | #define array_needsize(type,base,cur,cnt,init) \ |
318 | #define array_needsize(type,base,cur,cnt,init) \ |
301 | if (expect_false ((cnt) > cur)) \ |
319 | if (expect_false ((cnt) > cur)) \ |
302 | { \ |
320 | { \ |
303 | int newcnt = cur; \ |
321 | int newcnt = cur; \ |
… | |
… | |
318 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
336 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
319 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
337 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
320 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
338 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
321 | } |
339 | } |
322 | |
340 | |
323 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
|
|
324 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
|
|
325 | #define array_free_microshit(stem) \ |
|
|
326 | ev_free (stem ## s); stem ## cnt = stem ## max = 0; |
|
|
327 | |
|
|
328 | #define array_free(stem, idx) \ |
341 | #define array_free(stem, idx) \ |
329 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
342 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
330 | |
343 | |
331 | /*****************************************************************************/ |
344 | /*****************************************************************************/ |
332 | |
345 | |
… | |
… | |
353 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
366 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
354 | return; |
367 | return; |
355 | } |
368 | } |
356 | |
369 | |
357 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
370 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
358 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); |
371 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); |
359 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
372 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
360 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
373 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
361 | } |
374 | } |
362 | |
375 | |
363 | static void |
376 | static void |
… | |
… | |
406 | int events = 0; |
419 | int events = 0; |
407 | |
420 | |
408 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
421 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
409 | events |= w->events; |
422 | events |= w->events; |
410 | |
423 | |
|
|
424 | #if EV_SELECT_IS_WINSOCKET |
|
|
425 | if (events) |
|
|
426 | { |
|
|
427 | unsigned long argp; |
|
|
428 | anfd->handle = _get_osfhandle (fd); |
|
|
429 | assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0)); |
|
|
430 | } |
|
|
431 | #endif |
|
|
432 | |
411 | anfd->reify = 0; |
433 | anfd->reify = 0; |
412 | |
434 | |
413 | method_modify (EV_A_ fd, anfd->events, events); |
435 | method_modify (EV_A_ fd, anfd->events, events); |
414 | anfd->events = events; |
436 | anfd->events = events; |
415 | } |
437 | } |
… | |
… | |
424 | return; |
446 | return; |
425 | |
447 | |
426 | anfds [fd].reify = 1; |
448 | anfds [fd].reify = 1; |
427 | |
449 | |
428 | ++fdchangecnt; |
450 | ++fdchangecnt; |
429 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); |
451 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
430 | fdchanges [fdchangecnt - 1] = fd; |
452 | fdchanges [fdchangecnt - 1] = fd; |
431 | } |
453 | } |
432 | |
454 | |
433 | static void |
455 | static void |
434 | fd_kill (EV_P_ int fd) |
456 | fd_kill (EV_P_ int fd) |
… | |
… | |
443 | } |
465 | } |
444 | |
466 | |
445 | static int |
467 | static int |
446 | fd_valid (int fd) |
468 | fd_valid (int fd) |
447 | { |
469 | { |
448 | #ifdef WIN32 |
470 | #ifdef _WIN32 |
449 | return !!win32_get_osfhandle (fd); |
471 | return _get_osfhandle (fd) != -1; |
450 | #else |
472 | #else |
451 | return fcntl (fd, F_GETFD) != -1; |
473 | return fcntl (fd, F_GETFD) != -1; |
452 | #endif |
474 | #endif |
453 | } |
475 | } |
454 | |
476 | |
… | |
… | |
571 | } |
593 | } |
572 | |
594 | |
573 | static void |
595 | static void |
574 | sighandler (int signum) |
596 | sighandler (int signum) |
575 | { |
597 | { |
576 | #if WIN32 |
598 | #if _WIN32 |
577 | signal (signum, sighandler); |
599 | signal (signum, sighandler); |
578 | #endif |
600 | #endif |
579 | |
601 | |
580 | signals [signum - 1].gotsig = 1; |
602 | signals [signum - 1].gotsig = 1; |
581 | |
603 | |
582 | if (!gotsig) |
604 | if (!gotsig) |
583 | { |
605 | { |
584 | int old_errno = errno; |
606 | int old_errno = errno; |
585 | gotsig = 1; |
607 | gotsig = 1; |
586 | #ifdef WIN32 |
|
|
587 | send (sigpipe [1], &signum, 1, MSG_DONTWAIT); |
|
|
588 | #else |
|
|
589 | write (sigpipe [1], &signum, 1); |
608 | write (sigpipe [1], &signum, 1); |
590 | #endif |
|
|
591 | errno = old_errno; |
609 | errno = old_errno; |
592 | } |
610 | } |
593 | } |
611 | } |
594 | |
612 | |
595 | void |
613 | void |
… | |
… | |
615 | static void |
633 | static void |
616 | sigcb (EV_P_ struct ev_io *iow, int revents) |
634 | sigcb (EV_P_ struct ev_io *iow, int revents) |
617 | { |
635 | { |
618 | int signum; |
636 | int signum; |
619 | |
637 | |
620 | #ifdef WIN32 |
|
|
621 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
|
|
622 | #else |
|
|
623 | read (sigpipe [0], &revents, 1); |
638 | read (sigpipe [0], &revents, 1); |
624 | #endif |
|
|
625 | gotsig = 0; |
639 | gotsig = 0; |
626 | |
640 | |
627 | for (signum = signalmax; signum--; ) |
641 | for (signum = signalmax; signum--; ) |
628 | if (signals [signum].gotsig) |
642 | if (signals [signum].gotsig) |
629 | ev_feed_signal_event (EV_A_ signum + 1); |
643 | ev_feed_signal_event (EV_A_ signum + 1); |
630 | } |
644 | } |
631 | |
645 | |
|
|
646 | inline void |
|
|
647 | fd_intern (int fd) |
|
|
648 | { |
|
|
649 | #ifdef _WIN32 |
|
|
650 | int arg = 1; |
|
|
651 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
|
|
652 | #else |
|
|
653 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
654 | fcntl (fd, F_SETFL, O_NONBLOCK); |
|
|
655 | #endif |
|
|
656 | } |
|
|
657 | |
632 | static void |
658 | static void |
633 | siginit (EV_P) |
659 | siginit (EV_P) |
634 | { |
660 | { |
635 | #ifndef WIN32 |
661 | fd_intern (sigpipe [0]); |
636 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
662 | fd_intern (sigpipe [1]); |
637 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
|
|
638 | |
|
|
639 | /* rather than sort out wether we really need nb, set it */ |
|
|
640 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
|
|
641 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
642 | #endif |
|
|
643 | |
663 | |
644 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
664 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
645 | ev_io_start (EV_A_ &sigev); |
665 | ev_io_start (EV_A_ &sigev); |
646 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
666 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
647 | } |
667 | } |
648 | |
668 | |
649 | /*****************************************************************************/ |
669 | /*****************************************************************************/ |
650 | |
670 | |
651 | static struct ev_child *childs [PID_HASHSIZE]; |
671 | static struct ev_child *childs [PID_HASHSIZE]; |
652 | |
672 | |
653 | #ifndef WIN32 |
673 | #ifndef _WIN32 |
654 | |
674 | |
655 | static struct ev_signal childev; |
675 | static struct ev_signal childev; |
656 | |
676 | |
657 | #ifndef WCONTINUED |
677 | #ifndef WCONTINUED |
658 | # define WCONTINUED 0 |
678 | # define WCONTINUED 0 |
… | |
… | |
719 | |
739 | |
720 | /* return true if we are running with elevated privileges and should ignore env variables */ |
740 | /* return true if we are running with elevated privileges and should ignore env variables */ |
721 | static int |
741 | static int |
722 | enable_secure (void) |
742 | enable_secure (void) |
723 | { |
743 | { |
724 | #ifdef WIN32 |
744 | #ifdef _WIN32 |
725 | return 0; |
745 | return 0; |
726 | #else |
746 | #else |
727 | return getuid () != geteuid () |
747 | return getuid () != geteuid () |
728 | || getgid () != getegid (); |
748 | || getgid () != getegid (); |
729 | #endif |
749 | #endif |
730 | } |
750 | } |
731 | |
751 | |
732 | int |
752 | unsigned int |
733 | ev_method (EV_P) |
753 | ev_method (EV_P) |
734 | { |
754 | { |
735 | return method; |
755 | return method; |
736 | } |
756 | } |
737 | |
757 | |
738 | static void |
758 | static void |
739 | loop_init (EV_P_ int methods) |
759 | loop_init (EV_P_ unsigned int flags) |
740 | { |
760 | { |
741 | if (!method) |
761 | if (!method) |
742 | { |
762 | { |
743 | #if EV_USE_MONOTONIC |
763 | #if EV_USE_MONOTONIC |
744 | { |
764 | { |
… | |
… | |
751 | ev_rt_now = ev_time (); |
771 | ev_rt_now = ev_time (); |
752 | mn_now = get_clock (); |
772 | mn_now = get_clock (); |
753 | now_floor = mn_now; |
773 | now_floor = mn_now; |
754 | rtmn_diff = ev_rt_now - mn_now; |
774 | rtmn_diff = ev_rt_now - mn_now; |
755 | |
775 | |
756 | if (methods == EVMETHOD_AUTO) |
776 | if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS")) |
757 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
758 | methods = atoi (getenv ("LIBEV_METHODS")); |
777 | flags = atoi (getenv ("LIBEV_FLAGS")); |
759 | else |
778 | |
760 | methods = EVMETHOD_ANY; |
779 | if (!(flags & 0x0000ffff)) |
|
|
780 | flags |= 0x0000ffff; |
761 | |
781 | |
762 | method = 0; |
782 | method = 0; |
763 | #if EV_USE_WIN32 |
|
|
764 | if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); |
|
|
765 | #endif |
|
|
766 | #if EV_USE_KQUEUE |
783 | #if EV_USE_KQUEUE |
767 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
784 | if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags); |
768 | #endif |
785 | #endif |
769 | #if EV_USE_EPOLL |
786 | #if EV_USE_EPOLL |
770 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
787 | if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags); |
771 | #endif |
788 | #endif |
772 | #if EV_USE_POLL |
789 | #if EV_USE_POLL |
773 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
790 | if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags); |
774 | #endif |
791 | #endif |
775 | #if EV_USE_SELECT |
792 | #if EV_USE_SELECT |
776 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
793 | if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags); |
777 | #endif |
794 | #endif |
778 | |
795 | |
779 | ev_init (&sigev, sigcb); |
796 | ev_init (&sigev, sigcb); |
780 | ev_set_priority (&sigev, EV_MAXPRI); |
797 | ev_set_priority (&sigev, EV_MAXPRI); |
781 | } |
798 | } |
… | |
… | |
784 | void |
801 | void |
785 | loop_destroy (EV_P) |
802 | loop_destroy (EV_P) |
786 | { |
803 | { |
787 | int i; |
804 | int i; |
788 | |
805 | |
789 | #if EV_USE_WIN32 |
|
|
790 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
|
|
791 | #endif |
|
|
792 | #if EV_USE_KQUEUE |
806 | #if EV_USE_KQUEUE |
793 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
807 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
794 | #endif |
808 | #endif |
795 | #if EV_USE_EPOLL |
809 | #if EV_USE_EPOLL |
796 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
810 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
… | |
… | |
804 | |
818 | |
805 | for (i = NUMPRI; i--; ) |
819 | for (i = NUMPRI; i--; ) |
806 | array_free (pending, [i]); |
820 | array_free (pending, [i]); |
807 | |
821 | |
808 | /* have to use the microsoft-never-gets-it-right macro */ |
822 | /* have to use the microsoft-never-gets-it-right macro */ |
809 | array_free_microshit (fdchange); |
823 | array_free (fdchange, EMPTY0); |
810 | array_free_microshit (timer); |
824 | array_free (timer, EMPTY0); |
811 | #if EV_PERIODICS |
825 | #if EV_PERIODICS |
812 | array_free_microshit (periodic); |
826 | array_free (periodic, EMPTY0); |
813 | #endif |
827 | #endif |
814 | array_free_microshit (idle); |
828 | array_free (idle, EMPTY0); |
815 | array_free_microshit (prepare); |
829 | array_free (prepare, EMPTY0); |
816 | array_free_microshit (check); |
830 | array_free (check, EMPTY0); |
817 | |
831 | |
818 | method = 0; |
832 | method = 0; |
819 | } |
833 | } |
820 | |
834 | |
821 | static void |
835 | static void |
… | |
… | |
846 | postfork = 0; |
860 | postfork = 0; |
847 | } |
861 | } |
848 | |
862 | |
849 | #if EV_MULTIPLICITY |
863 | #if EV_MULTIPLICITY |
850 | struct ev_loop * |
864 | struct ev_loop * |
851 | ev_loop_new (int methods) |
865 | ev_loop_new (unsigned int flags) |
852 | { |
866 | { |
853 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
867 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
854 | |
868 | |
855 | memset (loop, 0, sizeof (struct ev_loop)); |
869 | memset (loop, 0, sizeof (struct ev_loop)); |
856 | |
870 | |
857 | loop_init (EV_A_ methods); |
871 | loop_init (EV_A_ flags); |
858 | |
872 | |
859 | if (ev_method (EV_A)) |
873 | if (ev_method (EV_A)) |
860 | return loop; |
874 | return loop; |
861 | |
875 | |
862 | return 0; |
876 | return 0; |
… | |
… | |
880 | #if EV_MULTIPLICITY |
894 | #if EV_MULTIPLICITY |
881 | struct ev_loop * |
895 | struct ev_loop * |
882 | #else |
896 | #else |
883 | int |
897 | int |
884 | #endif |
898 | #endif |
885 | ev_default_loop (int methods) |
899 | ev_default_loop (unsigned int flags) |
886 | { |
900 | { |
887 | if (sigpipe [0] == sigpipe [1]) |
901 | if (sigpipe [0] == sigpipe [1]) |
888 | if (pipe (sigpipe)) |
902 | if (pipe (sigpipe)) |
889 | return 0; |
903 | return 0; |
890 | |
904 | |
… | |
… | |
894 | struct ev_loop *loop = default_loop = &default_loop_struct; |
908 | struct ev_loop *loop = default_loop = &default_loop_struct; |
895 | #else |
909 | #else |
896 | default_loop = 1; |
910 | default_loop = 1; |
897 | #endif |
911 | #endif |
898 | |
912 | |
899 | loop_init (EV_A_ methods); |
913 | loop_init (EV_A_ flags); |
900 | |
914 | |
901 | if (ev_method (EV_A)) |
915 | if (ev_method (EV_A)) |
902 | { |
916 | { |
903 | siginit (EV_A); |
917 | siginit (EV_A); |
904 | |
918 | |
905 | #ifndef WIN32 |
919 | #ifndef _WIN32 |
906 | ev_signal_init (&childev, childcb, SIGCHLD); |
920 | ev_signal_init (&childev, childcb, SIGCHLD); |
907 | ev_set_priority (&childev, EV_MAXPRI); |
921 | ev_set_priority (&childev, EV_MAXPRI); |
908 | ev_signal_start (EV_A_ &childev); |
922 | ev_signal_start (EV_A_ &childev); |
909 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
923 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
910 | #endif |
924 | #endif |
… | |
… | |
921 | { |
935 | { |
922 | #if EV_MULTIPLICITY |
936 | #if EV_MULTIPLICITY |
923 | struct ev_loop *loop = default_loop; |
937 | struct ev_loop *loop = default_loop; |
924 | #endif |
938 | #endif |
925 | |
939 | |
926 | #ifndef WIN32 |
940 | #ifndef _WIN32 |
927 | ev_ref (EV_A); /* child watcher */ |
941 | ev_ref (EV_A); /* child watcher */ |
928 | ev_signal_stop (EV_A_ &childev); |
942 | ev_signal_stop (EV_A_ &childev); |
929 | #endif |
943 | #endif |
930 | |
944 | |
931 | ev_ref (EV_A); /* signal watcher */ |
945 | ev_ref (EV_A); /* signal watcher */ |
… | |
… | |
1018 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1032 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1019 | |
1033 | |
1020 | /* first reschedule or stop timer */ |
1034 | /* first reschedule or stop timer */ |
1021 | if (w->reschedule_cb) |
1035 | if (w->reschedule_cb) |
1022 | { |
1036 | { |
1023 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
1037 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
1024 | |
|
|
1025 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
1038 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
1026 | downheap ((WT *)periodics, periodiccnt, 0); |
1039 | downheap ((WT *)periodics, periodiccnt, 0); |
1027 | } |
1040 | } |
1028 | else if (w->interval) |
1041 | else if (w->interval) |
1029 | { |
1042 | { |
… | |
… | |
1147 | ev_loop (EV_P_ int flags) |
1160 | ev_loop (EV_P_ int flags) |
1148 | { |
1161 | { |
1149 | double block; |
1162 | double block; |
1150 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
1163 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
1151 | |
1164 | |
1152 | do |
1165 | while (activecnt) |
1153 | { |
1166 | { |
1154 | /* queue check watchers (and execute them) */ |
1167 | /* queue check watchers (and execute them) */ |
1155 | if (expect_false (preparecnt)) |
1168 | if (expect_false (preparecnt)) |
1156 | { |
1169 | { |
1157 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1170 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
… | |
… | |
1220 | /* queue check watchers, to be executed first */ |
1233 | /* queue check watchers, to be executed first */ |
1221 | if (checkcnt) |
1234 | if (checkcnt) |
1222 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1235 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1223 | |
1236 | |
1224 | call_pending (EV_A); |
1237 | call_pending (EV_A); |
|
|
1238 | |
|
|
1239 | if (loop_done) |
|
|
1240 | break; |
1225 | } |
1241 | } |
1226 | while (activecnt && !loop_done); |
|
|
1227 | |
1242 | |
1228 | if (loop_done != 2) |
1243 | if (loop_done != 2) |
1229 | loop_done = 0; |
1244 | loop_done = 0; |
1230 | } |
1245 | } |
1231 | |
1246 | |
… | |
… | |
1329 | ((WT)w)->at += mn_now; |
1344 | ((WT)w)->at += mn_now; |
1330 | |
1345 | |
1331 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1346 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1332 | |
1347 | |
1333 | ev_start (EV_A_ (W)w, ++timercnt); |
1348 | ev_start (EV_A_ (W)w, ++timercnt); |
1334 | array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); |
1349 | array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); |
1335 | timers [timercnt - 1] = w; |
1350 | timers [timercnt - 1] = w; |
1336 | upheap ((WT *)timers, timercnt - 1); |
1351 | upheap ((WT *)timers, timercnt - 1); |
1337 | |
1352 | |
1338 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1353 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1339 | } |
1354 | } |
… | |
… | |
1370 | } |
1385 | } |
1371 | else |
1386 | else |
1372 | ev_timer_stop (EV_A_ w); |
1387 | ev_timer_stop (EV_A_ w); |
1373 | } |
1388 | } |
1374 | else if (w->repeat) |
1389 | else if (w->repeat) |
|
|
1390 | { |
|
|
1391 | w->at = w->repeat; |
1375 | ev_timer_start (EV_A_ w); |
1392 | ev_timer_start (EV_A_ w); |
|
|
1393 | } |
1376 | } |
1394 | } |
1377 | |
1395 | |
1378 | #if EV_PERIODICS |
1396 | #if EV_PERIODICS |
1379 | void |
1397 | void |
1380 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1398 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
… | |
… | |
1390 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1408 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1391 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1409 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1392 | } |
1410 | } |
1393 | |
1411 | |
1394 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1412 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1395 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1413 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1396 | periodics [periodiccnt - 1] = w; |
1414 | periodics [periodiccnt - 1] = w; |
1397 | upheap ((WT *)periodics, periodiccnt - 1); |
1415 | upheap ((WT *)periodics, periodiccnt - 1); |
1398 | |
1416 | |
1399 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1417 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1400 | } |
1418 | } |
… | |
… | |
1431 | { |
1449 | { |
1432 | if (ev_is_active (w)) |
1450 | if (ev_is_active (w)) |
1433 | return; |
1451 | return; |
1434 | |
1452 | |
1435 | ev_start (EV_A_ (W)w, ++idlecnt); |
1453 | ev_start (EV_A_ (W)w, ++idlecnt); |
1436 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); |
1454 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
1437 | idles [idlecnt - 1] = w; |
1455 | idles [idlecnt - 1] = w; |
1438 | } |
1456 | } |
1439 | |
1457 | |
1440 | void |
1458 | void |
1441 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1459 | ev_idle_stop (EV_P_ struct ev_idle *w) |
… | |
… | |
1453 | { |
1471 | { |
1454 | if (ev_is_active (w)) |
1472 | if (ev_is_active (w)) |
1455 | return; |
1473 | return; |
1456 | |
1474 | |
1457 | ev_start (EV_A_ (W)w, ++preparecnt); |
1475 | ev_start (EV_A_ (W)w, ++preparecnt); |
1458 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); |
1476 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); |
1459 | prepares [preparecnt - 1] = w; |
1477 | prepares [preparecnt - 1] = w; |
1460 | } |
1478 | } |
1461 | |
1479 | |
1462 | void |
1480 | void |
1463 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1481 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
… | |
… | |
1475 | { |
1493 | { |
1476 | if (ev_is_active (w)) |
1494 | if (ev_is_active (w)) |
1477 | return; |
1495 | return; |
1478 | |
1496 | |
1479 | ev_start (EV_A_ (W)w, ++checkcnt); |
1497 | ev_start (EV_A_ (W)w, ++checkcnt); |
1480 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); |
1498 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); |
1481 | checks [checkcnt - 1] = w; |
1499 | checks [checkcnt - 1] = w; |
1482 | } |
1500 | } |
1483 | |
1501 | |
1484 | void |
1502 | void |
1485 | ev_check_stop (EV_P_ struct ev_check *w) |
1503 | ev_check_stop (EV_P_ struct ev_check *w) |
… | |
… | |
1511 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1529 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1512 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1530 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1513 | |
1531 | |
1514 | if (!((WL)w)->next) |
1532 | if (!((WL)w)->next) |
1515 | { |
1533 | { |
1516 | #if WIN32 |
1534 | #if _WIN32 |
1517 | signal (w->signum, sighandler); |
1535 | signal (w->signum, sighandler); |
1518 | #else |
1536 | #else |
1519 | struct sigaction sa; |
1537 | struct sigaction sa; |
1520 | sa.sa_handler = sighandler; |
1538 | sa.sa_handler = sighandler; |
1521 | sigfillset (&sa.sa_mask); |
1539 | sigfillset (&sa.sa_mask); |