… | |
… | |
184 | # define WIN32_LEAN_AND_MEAN |
184 | # define WIN32_LEAN_AND_MEAN |
185 | # include <windows.h> |
185 | # include <windows.h> |
186 | # ifndef EV_SELECT_IS_WINSOCKET |
186 | # ifndef EV_SELECT_IS_WINSOCKET |
187 | # define EV_SELECT_IS_WINSOCKET 1 |
187 | # define EV_SELECT_IS_WINSOCKET 1 |
188 | # endif |
188 | # endif |
|
|
189 | # undef EV_AVOID_STDIO |
189 | #endif |
190 | #endif |
190 | |
191 | |
191 | /* this block tries to deduce configuration from header-defined symbols and defaults */ |
192 | /* this block tries to deduce configuration from header-defined symbols and defaults */ |
192 | |
193 | |
193 | /* try to deduce the maximum number of signals on this platform */ |
194 | /* try to deduce the maximum number of signals on this platform */ |
… | |
… | |
206 | #elif defined (MAXSIG) |
207 | #elif defined (MAXSIG) |
207 | # define EV_NSIG (MAXSIG+1) |
208 | # define EV_NSIG (MAXSIG+1) |
208 | #elif defined (MAX_SIG) |
209 | #elif defined (MAX_SIG) |
209 | # define EV_NSIG (MAX_SIG+1) |
210 | # define EV_NSIG (MAX_SIG+1) |
210 | #elif defined (SIGARRAYSIZE) |
211 | #elif defined (SIGARRAYSIZE) |
211 | # define EV_NSIG SIGARRAYSIZE /* Assume ary[SIGARRAYSIZE] */ |
212 | # define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */ |
212 | #elif defined (_sys_nsig) |
213 | #elif defined (_sys_nsig) |
213 | # define EV_NSIG (_sys_nsig) /* Solaris 2.5 */ |
214 | # define EV_NSIG (_sys_nsig) /* Solaris 2.5 */ |
214 | #else |
215 | #else |
215 | # error "unable to find value for NSIG, please report" |
216 | # error "unable to find value for NSIG, please report" |
216 | /* to make it compile regardless, just remove the above line */ |
217 | /* to make it compile regardless, just remove the above line, */ |
|
|
218 | /* but consider reporting it, too! :) */ |
217 | # define EV_NSIG 65 |
219 | # define EV_NSIG 65 |
218 | #endif |
220 | #endif |
219 | |
221 | |
220 | #ifndef EV_USE_CLOCK_SYSCALL |
222 | #ifndef EV_USE_CLOCK_SYSCALL |
221 | # if __linux && __GLIBC__ >= 2 |
223 | # if __linux && __GLIBC__ >= 2 |
… | |
… | |
524 | # include "ev_win32.c" |
526 | # include "ev_win32.c" |
525 | #endif |
527 | #endif |
526 | |
528 | |
527 | /*****************************************************************************/ |
529 | /*****************************************************************************/ |
528 | |
530 | |
|
|
531 | #if EV_AVOID_STDIO |
|
|
532 | static void noinline |
|
|
533 | ev_printerr (const char *msg) |
|
|
534 | { |
|
|
535 | write (STDERR_FILENO, msg, strlen (msg)); |
|
|
536 | } |
|
|
537 | #endif |
|
|
538 | |
529 | static void (*syserr_cb)(const char *msg); |
539 | static void (*syserr_cb)(const char *msg); |
530 | |
540 | |
531 | void |
541 | void |
532 | ev_set_syserr_cb (void (*cb)(const char *msg)) |
542 | ev_set_syserr_cb (void (*cb)(const char *msg)) |
533 | { |
543 | { |
… | |
… | |
542 | |
552 | |
543 | if (syserr_cb) |
553 | if (syserr_cb) |
544 | syserr_cb (msg); |
554 | syserr_cb (msg); |
545 | else |
555 | else |
546 | { |
556 | { |
|
|
557 | #if EV_AVOID_STDIO |
|
|
558 | const char *err = strerror (errno); |
|
|
559 | |
|
|
560 | ev_printerr (msg); |
|
|
561 | ev_printerr (": "); |
|
|
562 | ev_printerr (err); |
|
|
563 | ev_printerr ("\n"); |
|
|
564 | #else |
547 | perror (msg); |
565 | perror (msg); |
|
|
566 | #endif |
548 | abort (); |
567 | abort (); |
549 | } |
568 | } |
550 | } |
569 | } |
551 | |
570 | |
552 | static void * |
571 | static void * |
553 | ev_realloc_emul (void *ptr, long size) |
572 | ev_realloc_emul (void *ptr, long size) |
554 | { |
573 | { |
|
|
574 | #if __GLIBC__ |
|
|
575 | return realloc (ptr, size); |
|
|
576 | #else |
555 | /* some systems, notably openbsd and darwin, fail to properly |
577 | /* some systems, notably openbsd and darwin, fail to properly |
556 | * implement realloc (x, 0) (as required by both ansi c-98 and |
578 | * implement realloc (x, 0) (as required by both ansi c-89 and |
557 | * the single unix specification, so work around them here. |
579 | * the single unix specification, so work around them here. |
558 | */ |
580 | */ |
559 | |
581 | |
560 | if (size) |
582 | if (size) |
561 | return realloc (ptr, size); |
583 | return realloc (ptr, size); |
562 | |
584 | |
563 | free (ptr); |
585 | free (ptr); |
564 | return 0; |
586 | return 0; |
|
|
587 | #endif |
565 | } |
588 | } |
566 | |
589 | |
567 | static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; |
590 | static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; |
568 | |
591 | |
569 | void |
592 | void |
… | |
… | |
577 | { |
600 | { |
578 | ptr = alloc (ptr, size); |
601 | ptr = alloc (ptr, size); |
579 | |
602 | |
580 | if (!ptr && size) |
603 | if (!ptr && size) |
581 | { |
604 | { |
|
|
605 | #if EV_AVOID_STDIO |
|
|
606 | ev_printerr ("libev: memory allocation failed, aborting.\n"); |
|
|
607 | #else |
582 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
608 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
|
|
609 | #endif |
583 | abort (); |
610 | abort (); |
584 | } |
611 | } |
585 | |
612 | |
586 | return ptr; |
613 | return ptr; |
587 | } |
614 | } |
… | |
… | |
966 | ev_io_stop (EV_A_ w); |
993 | ev_io_stop (EV_A_ w); |
967 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
994 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
968 | } |
995 | } |
969 | } |
996 | } |
970 | |
997 | |
971 | /* check whether the given fd is atcually valid, for error recovery */ |
998 | /* check whether the given fd is actually valid, for error recovery */ |
972 | inline_size int |
999 | inline_size int |
973 | fd_valid (int fd) |
1000 | fd_valid (int fd) |
974 | { |
1001 | { |
975 | #ifdef _WIN32 |
1002 | #ifdef _WIN32 |
976 | return EV_FD_TO_WIN32_HANDLE (fd) != -1; |
1003 | return EV_FD_TO_WIN32_HANDLE (fd) != -1; |
… | |
… | |
1016 | { |
1043 | { |
1017 | anfds [fd].events = 0; |
1044 | anfds [fd].events = 0; |
1018 | anfds [fd].emask = 0; |
1045 | anfds [fd].emask = 0; |
1019 | fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY); |
1046 | fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY); |
1020 | } |
1047 | } |
|
|
1048 | } |
|
|
1049 | |
|
|
1050 | /* used to prepare libev internal fd's */ |
|
|
1051 | /* this is not fork-safe */ |
|
|
1052 | inline_speed void |
|
|
1053 | fd_intern (int fd) |
|
|
1054 | { |
|
|
1055 | #ifdef _WIN32 |
|
|
1056 | unsigned long arg = 1; |
|
|
1057 | ioctlsocket (EV_FD_TO_WIN32_HANDLE (fd), FIONBIO, &arg); |
|
|
1058 | #else |
|
|
1059 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
1060 | fcntl (fd, F_SETFL, O_NONBLOCK); |
|
|
1061 | #endif |
1021 | } |
1062 | } |
1022 | |
1063 | |
1023 | /*****************************************************************************/ |
1064 | /*****************************************************************************/ |
1024 | |
1065 | |
1025 | /* |
1066 | /* |
… | |
… | |
1179 | |
1220 | |
1180 | static ANSIG signals [EV_NSIG - 1]; |
1221 | static ANSIG signals [EV_NSIG - 1]; |
1181 | |
1222 | |
1182 | /*****************************************************************************/ |
1223 | /*****************************************************************************/ |
1183 | |
1224 | |
1184 | /* used to prepare libev internal fd's */ |
1225 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
1185 | /* this is not fork-safe */ |
|
|
1186 | inline_speed void |
|
|
1187 | fd_intern (int fd) |
|
|
1188 | { |
|
|
1189 | #ifdef _WIN32 |
|
|
1190 | unsigned long arg = 1; |
|
|
1191 | ioctlsocket (EV_FD_TO_WIN32_HANDLE (fd), FIONBIO, &arg); |
|
|
1192 | #else |
|
|
1193 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
1194 | fcntl (fd, F_SETFL, O_NONBLOCK); |
|
|
1195 | #endif |
|
|
1196 | } |
|
|
1197 | |
1226 | |
1198 | static void noinline |
1227 | static void noinline |
1199 | evpipe_init (EV_P) |
1228 | evpipe_init (EV_P) |
1200 | { |
1229 | { |
1201 | if (!ev_is_active (&pipe_w)) |
1230 | if (!ev_is_active (&pipe_w)) |
1202 | { |
1231 | { |
1203 | #if EV_USE_EVENTFD |
1232 | # if EV_USE_EVENTFD |
1204 | evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); |
1233 | evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); |
1205 | if (evfd < 0 && errno == EINVAL) |
1234 | if (evfd < 0 && errno == EINVAL) |
1206 | evfd = eventfd (0, 0); |
1235 | evfd = eventfd (0, 0); |
1207 | |
1236 | |
1208 | if (evfd >= 0) |
1237 | if (evfd >= 0) |
… | |
… | |
1210 | evpipe [0] = -1; |
1239 | evpipe [0] = -1; |
1211 | fd_intern (evfd); /* doing it twice doesn't hurt */ |
1240 | fd_intern (evfd); /* doing it twice doesn't hurt */ |
1212 | ev_io_set (&pipe_w, evfd, EV_READ); |
1241 | ev_io_set (&pipe_w, evfd, EV_READ); |
1213 | } |
1242 | } |
1214 | else |
1243 | else |
1215 | #endif |
1244 | # endif |
1216 | { |
1245 | { |
1217 | while (pipe (evpipe)) |
1246 | while (pipe (evpipe)) |
1218 | ev_syserr ("(libev) error creating signal/async pipe"); |
1247 | ev_syserr ("(libev) error creating signal/async pipe"); |
1219 | |
1248 | |
1220 | fd_intern (evpipe [0]); |
1249 | fd_intern (evpipe [0]); |
… | |
… | |
1231 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1260 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1232 | { |
1261 | { |
1233 | if (!*flag) |
1262 | if (!*flag) |
1234 | { |
1263 | { |
1235 | int old_errno = errno; /* save errno because write might clobber it */ |
1264 | int old_errno = errno; /* save errno because write might clobber it */ |
|
|
1265 | char dummy; |
1236 | |
1266 | |
1237 | *flag = 1; |
1267 | *flag = 1; |
1238 | |
1268 | |
1239 | #if EV_USE_EVENTFD |
1269 | #if EV_USE_EVENTFD |
1240 | if (evfd >= 0) |
1270 | if (evfd >= 0) |
… | |
… | |
1242 | uint64_t counter = 1; |
1272 | uint64_t counter = 1; |
1243 | write (evfd, &counter, sizeof (uint64_t)); |
1273 | write (evfd, &counter, sizeof (uint64_t)); |
1244 | } |
1274 | } |
1245 | else |
1275 | else |
1246 | #endif |
1276 | #endif |
1247 | write (evpipe [1], &old_errno, 1); |
1277 | write (evpipe [1], &dummy, 1); |
1248 | |
1278 | |
1249 | errno = old_errno; |
1279 | errno = old_errno; |
1250 | } |
1280 | } |
1251 | } |
1281 | } |
1252 | |
1282 | |
… | |
… | |
1353 | break; |
1383 | break; |
1354 | } |
1384 | } |
1355 | } |
1385 | } |
1356 | #endif |
1386 | #endif |
1357 | |
1387 | |
|
|
1388 | #endif |
|
|
1389 | |
1358 | /*****************************************************************************/ |
1390 | /*****************************************************************************/ |
1359 | |
1391 | |
|
|
1392 | #if EV_CHILD_ENABLE |
1360 | static WL childs [EV_PID_HASHSIZE]; |
1393 | static WL childs [EV_PID_HASHSIZE]; |
1361 | |
|
|
1362 | #ifndef _WIN32 |
|
|
1363 | |
1394 | |
1364 | static ev_signal childev; |
1395 | static ev_signal childev; |
1365 | |
1396 | |
1366 | #ifndef WIFCONTINUED |
1397 | #ifndef WIFCONTINUED |
1367 | # define WIFCONTINUED(status) 0 |
1398 | # define WIFCONTINUED(status) 0 |
… | |
… | |
1636 | if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); |
1667 | if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); |
1637 | #endif |
1668 | #endif |
1638 | |
1669 | |
1639 | ev_prepare_init (&pending_w, pendingcb); |
1670 | ev_prepare_init (&pending_w, pendingcb); |
1640 | |
1671 | |
|
|
1672 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
1641 | ev_init (&pipe_w, pipecb); |
1673 | ev_init (&pipe_w, pipecb); |
1642 | ev_set_priority (&pipe_w, EV_MAXPRI); |
1674 | ev_set_priority (&pipe_w, EV_MAXPRI); |
|
|
1675 | #endif |
1643 | } |
1676 | } |
1644 | } |
1677 | } |
1645 | |
1678 | |
1646 | /* free up a loop structure */ |
1679 | /* free up a loop structure */ |
1647 | static void noinline |
1680 | static void noinline |
… | |
… | |
1897 | |
1930 | |
1898 | assert (checkmax >= checkcnt); |
1931 | assert (checkmax >= checkcnt); |
1899 | array_verify (EV_A_ (W *)checks, checkcnt); |
1932 | array_verify (EV_A_ (W *)checks, checkcnt); |
1900 | |
1933 | |
1901 | # if 0 |
1934 | # if 0 |
|
|
1935 | #if EV_CHILD_ENABLE |
1902 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
1936 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
1903 | for (signum = EV_NSIG; signum--; ) if (signals [signum].pending) |
1937 | for (signum = EV_NSIG; signum--; ) if (signals [signum].pending) |
|
|
1938 | #endif |
1904 | # endif |
1939 | # endif |
1905 | #endif |
1940 | #endif |
1906 | } |
1941 | } |
1907 | #endif |
1942 | #endif |
1908 | |
1943 | |
… | |
… | |
1924 | |
1959 | |
1925 | loop_init (EV_A_ flags); |
1960 | loop_init (EV_A_ flags); |
1926 | |
1961 | |
1927 | if (ev_backend (EV_A)) |
1962 | if (ev_backend (EV_A)) |
1928 | { |
1963 | { |
1929 | #ifndef _WIN32 |
1964 | #if EV_CHILD_ENABLE |
1930 | ev_signal_init (&childev, childcb, SIGCHLD); |
1965 | ev_signal_init (&childev, childcb, SIGCHLD); |
1931 | ev_set_priority (&childev, EV_MAXPRI); |
1966 | ev_set_priority (&childev, EV_MAXPRI); |
1932 | ev_signal_start (EV_A_ &childev); |
1967 | ev_signal_start (EV_A_ &childev); |
1933 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
1968 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
1934 | #endif |
1969 | #endif |
… | |
… | |
1947 | EV_P = ev_default_loop_ptr; |
1982 | EV_P = ev_default_loop_ptr; |
1948 | #endif |
1983 | #endif |
1949 | |
1984 | |
1950 | ev_default_loop_ptr = 0; |
1985 | ev_default_loop_ptr = 0; |
1951 | |
1986 | |
1952 | #ifndef _WIN32 |
1987 | #if EV_CHILD_ENABLE |
1953 | ev_ref (EV_A); /* child watcher */ |
1988 | ev_ref (EV_A); /* child watcher */ |
1954 | ev_signal_stop (EV_A_ &childev); |
1989 | ev_signal_stop (EV_A_ &childev); |
1955 | #endif |
1990 | #endif |
1956 | |
1991 | |
1957 | loop_destroy (EV_A); |
1992 | loop_destroy (EV_A); |
… | |
… | |
2699 | |
2734 | |
2700 | #ifndef SA_RESTART |
2735 | #ifndef SA_RESTART |
2701 | # define SA_RESTART 0 |
2736 | # define SA_RESTART 0 |
2702 | #endif |
2737 | #endif |
2703 | |
2738 | |
|
|
2739 | #if EV_SIGNAL_ENABLE |
|
|
2740 | |
2704 | void noinline |
2741 | void noinline |
2705 | ev_signal_start (EV_P_ ev_signal *w) |
2742 | ev_signal_start (EV_P_ ev_signal *w) |
2706 | { |
2743 | { |
2707 | if (expect_false (ev_is_active (w))) |
2744 | if (expect_false (ev_is_active (w))) |
2708 | return; |
2745 | return; |
… | |
… | |
2814 | } |
2851 | } |
2815 | |
2852 | |
2816 | EV_FREQUENT_CHECK; |
2853 | EV_FREQUENT_CHECK; |
2817 | } |
2854 | } |
2818 | |
2855 | |
|
|
2856 | #endif |
|
|
2857 | |
|
|
2858 | #if EV_CHILD_ENABLE |
|
|
2859 | |
2819 | void |
2860 | void |
2820 | ev_child_start (EV_P_ ev_child *w) |
2861 | ev_child_start (EV_P_ ev_child *w) |
2821 | { |
2862 | { |
2822 | #if EV_MULTIPLICITY |
2863 | #if EV_MULTIPLICITY |
2823 | assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
2864 | assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
… | |
… | |
2845 | wlist_del (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); |
2886 | wlist_del (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); |
2846 | ev_stop (EV_A_ (W)w); |
2887 | ev_stop (EV_A_ (W)w); |
2847 | |
2888 | |
2848 | EV_FREQUENT_CHECK; |
2889 | EV_FREQUENT_CHECK; |
2849 | } |
2890 | } |
|
|
2891 | |
|
|
2892 | #endif |
2850 | |
2893 | |
2851 | #if EV_STAT_ENABLE |
2894 | #if EV_STAT_ENABLE |
2852 | |
2895 | |
2853 | # ifdef _WIN32 |
2896 | # ifdef _WIN32 |
2854 | # undef lstat |
2897 | # undef lstat |
… | |
… | |
2992 | infy_wd (EV_A_ ev->wd, ev->wd, ev); |
3035 | infy_wd (EV_A_ ev->wd, ev->wd, ev); |
2993 | ofs += sizeof (struct inotify_event) + ev->len; |
3036 | ofs += sizeof (struct inotify_event) + ev->len; |
2994 | } |
3037 | } |
2995 | } |
3038 | } |
2996 | |
3039 | |
|
|
3040 | inline_size unsigned int |
|
|
3041 | ev_linux_version (void) |
|
|
3042 | { |
|
|
3043 | struct utsname buf; |
|
|
3044 | unsigned int v; |
|
|
3045 | int i; |
|
|
3046 | char *p = buf.release; |
|
|
3047 | |
|
|
3048 | if (uname (&buf)) |
|
|
3049 | return 0; |
|
|
3050 | |
|
|
3051 | for (i = 3+1; --i; ) |
|
|
3052 | { |
|
|
3053 | unsigned int c = 0; |
|
|
3054 | |
|
|
3055 | for (;;) |
|
|
3056 | { |
|
|
3057 | if (*p >= '0' && *p <= '9') |
|
|
3058 | c = c * 10 + *p++ - '0'; |
|
|
3059 | else |
|
|
3060 | { |
|
|
3061 | p += *p == '.'; |
|
|
3062 | break; |
|
|
3063 | } |
|
|
3064 | } |
|
|
3065 | |
|
|
3066 | v = (v << 8) | c; |
|
|
3067 | } |
|
|
3068 | |
|
|
3069 | return v; |
|
|
3070 | } |
|
|
3071 | |
2997 | inline_size void |
3072 | inline_size void |
2998 | check_2625 (EV_P) |
3073 | ev_check_2625 (EV_P) |
2999 | { |
3074 | { |
3000 | /* kernels < 2.6.25 are borked |
3075 | /* kernels < 2.6.25 are borked |
3001 | * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html |
3076 | * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html |
3002 | */ |
3077 | */ |
3003 | struct utsname buf; |
3078 | if (ev_linux_version () < 0x020619) |
3004 | int major, minor, micro; |
|
|
3005 | |
|
|
3006 | if (uname (&buf)) |
|
|
3007 | return; |
|
|
3008 | |
|
|
3009 | if (sscanf (buf.release, "%d.%d.%d", &major, &minor, µ) != 3) |
|
|
3010 | return; |
|
|
3011 | |
|
|
3012 | if (major < 2 |
|
|
3013 | || (major == 2 && minor < 6) |
|
|
3014 | || (major == 2 && minor == 6 && micro < 25)) |
|
|
3015 | return; |
3079 | return; |
3016 | |
3080 | |
3017 | fs_2625 = 1; |
3081 | fs_2625 = 1; |
3018 | } |
3082 | } |
3019 | |
3083 | |
… | |
… | |
3034 | if (fs_fd != -2) |
3098 | if (fs_fd != -2) |
3035 | return; |
3099 | return; |
3036 | |
3100 | |
3037 | fs_fd = -1; |
3101 | fs_fd = -1; |
3038 | |
3102 | |
3039 | check_2625 (EV_A); |
3103 | ev_check_2625 (EV_A); |
3040 | |
3104 | |
3041 | fs_fd = infy_newfd (); |
3105 | fs_fd = infy_newfd (); |
3042 | |
3106 | |
3043 | if (fs_fd >= 0) |
3107 | if (fs_fd >= 0) |
3044 | { |
3108 | { |