… | |
… | |
51 | |
51 | |
52 | # if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) |
52 | # if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) |
53 | # define EV_USE_POLL 1 |
53 | # define EV_USE_POLL 1 |
54 | # endif |
54 | # endif |
55 | |
55 | |
56 | # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL) |
56 | # if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL) |
57 | # define EV_USE_EPOLL 1 |
57 | # define EV_USE_EPOLL 1 |
58 | # endif |
58 | # endif |
59 | |
59 | |
60 | # if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE) |
60 | # if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE) |
61 | # define EV_USE_KQUEUE 1 |
61 | # define EV_USE_KQUEUE 1 |
… | |
… | |
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 |
|
|
106 | |
|
|
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 |
117 | #endif |
116 | |
118 | |
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 |
… | |
… | |
126 | #endif |
128 | #endif |
127 | |
129 | |
128 | #ifndef CLOCK_REALTIME |
130 | #ifndef CLOCK_REALTIME |
129 | # undef EV_USE_REALTIME |
131 | # undef EV_USE_REALTIME |
130 | # define EV_USE_REALTIME 0 |
132 | # define EV_USE_REALTIME 0 |
|
|
133 | #endif |
|
|
134 | |
|
|
135 | #if EV_SELECT_IS_WINSOCKET |
|
|
136 | # include <winsock.h> |
131 | #endif |
137 | #endif |
132 | |
138 | |
133 | /**/ |
139 | /**/ |
134 | |
140 | |
135 | #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) */ |
… | |
… | |
155 | #define expect_true(expr) expect ((expr) != 0, 1) |
161 | #define expect_true(expr) expect ((expr) != 0, 1) |
156 | |
162 | |
157 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
163 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
158 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
164 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
159 | |
165 | |
|
|
166 | #define EMPTY /* required for microsofts broken pseudo-c compiler */ |
|
|
167 | |
160 | typedef struct ev_watcher *W; |
168 | typedef struct ev_watcher *W; |
161 | typedef struct ev_watcher_list *WL; |
169 | typedef struct ev_watcher_list *WL; |
162 | typedef struct ev_watcher_time *WT; |
170 | typedef struct ev_watcher_time *WT; |
163 | |
171 | |
164 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
172 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
165 | |
173 | |
166 | #ifdef WIN32 |
174 | #ifdef _WIN32 |
167 | # include "ev_win32.c" |
175 | # include "ev_win32.c" |
168 | #endif |
176 | #endif |
169 | |
177 | |
170 | /*****************************************************************************/ |
178 | /*****************************************************************************/ |
171 | |
179 | |
… | |
… | |
220 | typedef struct |
228 | typedef struct |
221 | { |
229 | { |
222 | WL head; |
230 | WL head; |
223 | unsigned char events; |
231 | unsigned char events; |
224 | unsigned char reify; |
232 | unsigned char reify; |
|
|
233 | #if EV_SELECT_IS_WINSOCKET |
|
|
234 | SOCKET handle; |
|
|
235 | #endif |
225 | } ANFD; |
236 | } ANFD; |
226 | |
237 | |
227 | typedef struct |
238 | typedef struct |
228 | { |
239 | { |
229 | W w; |
240 | W w; |
… | |
… | |
318 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
329 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
319 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
330 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
320 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
331 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
321 | } |
332 | } |
322 | |
333 | |
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) \ |
334 | #define array_free(stem, idx) \ |
329 | 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; |
330 | |
336 | |
331 | /*****************************************************************************/ |
337 | /*****************************************************************************/ |
332 | |
338 | |
… | |
… | |
406 | int events = 0; |
412 | int events = 0; |
407 | |
413 | |
408 | 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) |
409 | events |= w->events; |
415 | events |= w->events; |
410 | |
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 | |
411 | anfd->reify = 0; |
426 | anfd->reify = 0; |
412 | |
427 | |
413 | method_modify (EV_A_ fd, anfd->events, events); |
428 | method_modify (EV_A_ fd, anfd->events, events); |
414 | anfd->events = events; |
429 | anfd->events = events; |
415 | } |
430 | } |
… | |
… | |
443 | } |
458 | } |
444 | |
459 | |
445 | static int |
460 | static int |
446 | fd_valid (int fd) |
461 | fd_valid (int fd) |
447 | { |
462 | { |
448 | #ifdef WIN32 |
463 | #ifdef _WIN32 |
449 | return !!win32_get_osfhandle (fd); |
464 | return _get_osfhandle (fd) != -1; |
450 | #else |
465 | #else |
451 | return fcntl (fd, F_GETFD) != -1; |
466 | return fcntl (fd, F_GETFD) != -1; |
452 | #endif |
467 | #endif |
453 | } |
468 | } |
454 | |
469 | |
… | |
… | |
571 | } |
586 | } |
572 | |
587 | |
573 | static void |
588 | static void |
574 | sighandler (int signum) |
589 | sighandler (int signum) |
575 | { |
590 | { |
576 | #if WIN32 |
591 | #if _WIN32 |
577 | signal (signum, sighandler); |
592 | signal (signum, sighandler); |
578 | #endif |
593 | #endif |
579 | |
594 | |
580 | signals [signum - 1].gotsig = 1; |
595 | signals [signum - 1].gotsig = 1; |
581 | |
596 | |
582 | if (!gotsig) |
597 | if (!gotsig) |
583 | { |
598 | { |
584 | int old_errno = errno; |
599 | int old_errno = errno; |
585 | gotsig = 1; |
600 | gotsig = 1; |
586 | #ifdef WIN32 |
|
|
587 | send (sigpipe [1], &signum, 1, MSG_DONTWAIT); |
|
|
588 | #else |
|
|
589 | write (sigpipe [1], &signum, 1); |
601 | write (sigpipe [1], &signum, 1); |
590 | #endif |
|
|
591 | errno = old_errno; |
602 | errno = old_errno; |
592 | } |
603 | } |
593 | } |
604 | } |
594 | |
605 | |
595 | void |
606 | void |
… | |
… | |
615 | static void |
626 | static void |
616 | sigcb (EV_P_ struct ev_io *iow, int revents) |
627 | sigcb (EV_P_ struct ev_io *iow, int revents) |
617 | { |
628 | { |
618 | int signum; |
629 | int signum; |
619 | |
630 | |
620 | #ifdef WIN32 |
|
|
621 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
|
|
622 | #else |
|
|
623 | read (sigpipe [0], &revents, 1); |
631 | read (sigpipe [0], &revents, 1); |
624 | #endif |
|
|
625 | gotsig = 0; |
632 | gotsig = 0; |
626 | |
633 | |
627 | for (signum = signalmax; signum--; ) |
634 | for (signum = signalmax; signum--; ) |
628 | if (signals [signum].gotsig) |
635 | if (signals [signum].gotsig) |
629 | ev_feed_signal_event (EV_A_ signum + 1); |
636 | ev_feed_signal_event (EV_A_ signum + 1); |
630 | } |
637 | } |
631 | |
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 | |
632 | static void |
651 | static void |
633 | siginit (EV_P) |
652 | siginit (EV_P) |
634 | { |
653 | { |
635 | #ifndef WIN32 |
654 | fd_intern (sigpipe [0]); |
636 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
655 | 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 | |
656 | |
644 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
657 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
645 | ev_io_start (EV_A_ &sigev); |
658 | ev_io_start (EV_A_ &sigev); |
646 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
659 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
647 | } |
660 | } |
648 | |
661 | |
649 | /*****************************************************************************/ |
662 | /*****************************************************************************/ |
650 | |
663 | |
651 | static struct ev_child *childs [PID_HASHSIZE]; |
664 | static struct ev_child *childs [PID_HASHSIZE]; |
652 | |
665 | |
653 | #ifndef WIN32 |
666 | #ifndef _WIN32 |
654 | |
667 | |
655 | static struct ev_signal childev; |
668 | static struct ev_signal childev; |
656 | |
669 | |
657 | #ifndef WCONTINUED |
670 | #ifndef WCONTINUED |
658 | # define WCONTINUED 0 |
671 | # define WCONTINUED 0 |
… | |
… | |
719 | |
732 | |
720 | /* 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 */ |
721 | static int |
734 | static int |
722 | enable_secure (void) |
735 | enable_secure (void) |
723 | { |
736 | { |
724 | #ifdef WIN32 |
737 | #ifdef _WIN32 |
725 | return 0; |
738 | return 0; |
726 | #else |
739 | #else |
727 | return getuid () != geteuid () |
740 | return getuid () != geteuid () |
728 | || getgid () != getegid (); |
741 | || getgid () != getegid (); |
729 | #endif |
742 | #endif |
… | |
… | |
758 | methods = atoi (getenv ("LIBEV_METHODS")); |
771 | methods = atoi (getenv ("LIBEV_METHODS")); |
759 | else |
772 | else |
760 | methods = EVMETHOD_ANY; |
773 | methods = EVMETHOD_ANY; |
761 | |
774 | |
762 | method = 0; |
775 | 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 |
776 | #if EV_USE_KQUEUE |
767 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
777 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
768 | #endif |
778 | #endif |
769 | #if EV_USE_EPOLL |
779 | #if EV_USE_EPOLL |
770 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
780 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
… | |
… | |
784 | void |
794 | void |
785 | loop_destroy (EV_P) |
795 | loop_destroy (EV_P) |
786 | { |
796 | { |
787 | int i; |
797 | int i; |
788 | |
798 | |
789 | #if EV_USE_WIN32 |
|
|
790 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
|
|
791 | #endif |
|
|
792 | #if EV_USE_KQUEUE |
799 | #if EV_USE_KQUEUE |
793 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
800 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
794 | #endif |
801 | #endif |
795 | #if EV_USE_EPOLL |
802 | #if EV_USE_EPOLL |
796 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
803 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
… | |
… | |
804 | |
811 | |
805 | for (i = NUMPRI; i--; ) |
812 | for (i = NUMPRI; i--; ) |
806 | array_free (pending, [i]); |
813 | array_free (pending, [i]); |
807 | |
814 | |
808 | /* have to use the microsoft-never-gets-it-right macro */ |
815 | /* have to use the microsoft-never-gets-it-right macro */ |
809 | array_free_microshit (fdchange); |
816 | array_free (fdchange, EMPTY); |
810 | array_free_microshit (timer); |
817 | array_free (timer, EMPTY); |
811 | #if EV_PERIODICS |
818 | #if EV_PERIODICS |
812 | array_free_microshit (periodic); |
819 | array_free (periodic, EMPTY); |
813 | #endif |
820 | #endif |
814 | array_free_microshit (idle); |
821 | array_free (idle, EMPTY); |
815 | array_free_microshit (prepare); |
822 | array_free (prepare, EMPTY); |
816 | array_free_microshit (check); |
823 | array_free (check, EMPTY); |
817 | |
824 | |
818 | method = 0; |
825 | method = 0; |
819 | } |
826 | } |
820 | |
827 | |
821 | static void |
828 | static void |
… | |
… | |
900 | |
907 | |
901 | if (ev_method (EV_A)) |
908 | if (ev_method (EV_A)) |
902 | { |
909 | { |
903 | siginit (EV_A); |
910 | siginit (EV_A); |
904 | |
911 | |
905 | #ifndef WIN32 |
912 | #ifndef _WIN32 |
906 | ev_signal_init (&childev, childcb, SIGCHLD); |
913 | ev_signal_init (&childev, childcb, SIGCHLD); |
907 | ev_set_priority (&childev, EV_MAXPRI); |
914 | ev_set_priority (&childev, EV_MAXPRI); |
908 | ev_signal_start (EV_A_ &childev); |
915 | ev_signal_start (EV_A_ &childev); |
909 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
916 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
910 | #endif |
917 | #endif |
… | |
… | |
921 | { |
928 | { |
922 | #if EV_MULTIPLICITY |
929 | #if EV_MULTIPLICITY |
923 | struct ev_loop *loop = default_loop; |
930 | struct ev_loop *loop = default_loop; |
924 | #endif |
931 | #endif |
925 | |
932 | |
926 | #ifndef WIN32 |
933 | #ifndef _WIN32 |
927 | ev_ref (EV_A); /* child watcher */ |
934 | ev_ref (EV_A); /* child watcher */ |
928 | ev_signal_stop (EV_A_ &childev); |
935 | ev_signal_stop (EV_A_ &childev); |
929 | #endif |
936 | #endif |
930 | |
937 | |
931 | ev_ref (EV_A); /* signal watcher */ |
938 | ev_ref (EV_A); /* signal watcher */ |
… | |
… | |
1439 | |
1446 | |
1440 | void |
1447 | void |
1441 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1448 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1442 | { |
1449 | { |
1443 | ev_clear_pending (EV_A_ (W)w); |
1450 | ev_clear_pending (EV_A_ (W)w); |
1444 | if (ev_is_active (w)) |
1451 | if (!ev_is_active (w)) |
1445 | return; |
1452 | return; |
1446 | |
1453 | |
1447 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1454 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1448 | ev_stop (EV_A_ (W)w); |
1455 | ev_stop (EV_A_ (W)w); |
1449 | } |
1456 | } |
… | |
… | |
1511 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1518 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1512 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1519 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1513 | |
1520 | |
1514 | if (!((WL)w)->next) |
1521 | if (!((WL)w)->next) |
1515 | { |
1522 | { |
1516 | #if WIN32 |
1523 | #if _WIN32 |
1517 | signal (w->signum, sighandler); |
1524 | signal (w->signum, sighandler); |
1518 | #else |
1525 | #else |
1519 | struct sigaction sa; |
1526 | struct sigaction sa; |
1520 | sa.sa_handler = sighandler; |
1527 | sa.sa_handler = sighandler; |
1521 | sigfillset (&sa.sa_mask); |
1528 | sigfillset (&sa.sa_mask); |