… | |
… | |
559 | |
559 | |
560 | #define ECB_C (__STDC__+0) /* this assumes that __STDC__ is either empty or a number */ |
560 | #define ECB_C (__STDC__+0) /* this assumes that __STDC__ is either empty or a number */ |
561 | #define ECB_C99 (__STDC_VERSION__ >= 199901L) |
561 | #define ECB_C99 (__STDC_VERSION__ >= 199901L) |
562 | #define ECB_C11 (__STDC_VERSION__ >= 201112L) |
562 | #define ECB_C11 (__STDC_VERSION__ >= 201112L) |
563 | #define ECB_CPP (__cplusplus+0) |
563 | #define ECB_CPP (__cplusplus+0) |
564 | #define ECB_CPP98 (__cplusplus >= 199711L) |
|
|
565 | #define ECB_CPP11 (__cplusplus >= 201103L) |
564 | #define ECB_CPP11 (__cplusplus >= 201103L) |
566 | |
565 | |
567 | /*****************************************************************************/ |
566 | /*****************************************************************************/ |
568 | |
567 | |
569 | /* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ |
568 | /* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ |
… | |
… | |
614 | #endif |
613 | #endif |
615 | #endif |
614 | #endif |
616 | |
615 | |
617 | #ifndef ECB_MEMORY_FENCE |
616 | #ifndef ECB_MEMORY_FENCE |
618 | #if ECB_GCC_VERSION(4,7) |
617 | #if ECB_GCC_VERSION(4,7) |
619 | /* see comment below about the C11 memory model. in short - avoid */ |
618 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
620 | #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST) |
619 | #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST) |
621 | #elif defined __clang && __has_feature (cxx_atomic) |
620 | #elif defined __clang && __has_feature (cxx_atomic) |
622 | /* see above */ |
621 | /* see comment below (stdatomic.h) about the C11 memory model. */ |
623 | #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST) |
622 | #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST) |
624 | #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ |
623 | #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ |
625 | #define ECB_MEMORY_FENCE __sync_synchronize () |
624 | #define ECB_MEMORY_FENCE __sync_synchronize () |
626 | #elif _MSC_VER >= 1400 /* VC++ 2005 */ |
625 | #elif _MSC_VER >= 1400 /* VC++ 2005 */ |
627 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
626 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
… | |
… | |
644 | #ifndef ECB_MEMORY_FENCE |
643 | #ifndef ECB_MEMORY_FENCE |
645 | #if ECB_C11 && !defined __STDC_NO_ATOMICS__ |
644 | #if ECB_C11 && !defined __STDC_NO_ATOMICS__ |
646 | /* we assume that these memory fences work on all variables/all memory accesses, */ |
645 | /* we assume that these memory fences work on all variables/all memory accesses, */ |
647 | /* not just C11 atomics and atomic accesses */ |
646 | /* not just C11 atomics and atomic accesses */ |
648 | #include <stdatomic.h> |
647 | #include <stdatomic.h> |
649 | /* unfortunately, the C11 memory model seems to be very limited, and unable to express */ |
648 | /* Unfortunately, neither gcc 4.7 nor clang 3.1 generate any instructions for */ |
650 | /* simple barrier semantics. That means we need to take out thor's hammer. */ |
649 | /* any fence other than seq_cst, which isn't very efficient for us. */ |
|
|
650 | /* Why that is, we don't know - either the C11 memory model is quite useless */ |
|
|
651 | /* for most usages, or gcc and clang have a bug */ |
|
|
652 | /* I *currently* lean towards the latter, and inefficiently implement */ |
|
|
653 | /* all three of ecb's fences as a seq_cst fence */ |
651 | #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst) |
654 | #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst) |
652 | #endif |
655 | #endif |
653 | #endif |
656 | #endif |
654 | |
657 | |
655 | #ifndef ECB_MEMORY_FENCE |
658 | #ifndef ECB_MEMORY_FENCE |
… | |
… | |
1188 | } |
1191 | } |
1189 | |
1192 | |
1190 | static void * |
1193 | static void * |
1191 | ev_realloc_emul (void *ptr, long size) EV_THROW |
1194 | ev_realloc_emul (void *ptr, long size) EV_THROW |
1192 | { |
1195 | { |
1193 | #if __GLIBC__ |
|
|
1194 | return realloc (ptr, size); |
|
|
1195 | #else |
|
|
1196 | /* some systems, notably openbsd and darwin, fail to properly |
1196 | /* some systems, notably openbsd and darwin, fail to properly |
1197 | * implement realloc (x, 0) (as required by both ansi c-89 and |
1197 | * implement realloc (x, 0) (as required by both ansi c-89 and |
1198 | * the single unix specification, so work around them here. |
1198 | * the single unix specification, so work around them here. |
|
|
1199 | * recently, also (at least) fedora and debian started breaking it, |
|
|
1200 | * despite documenting it otherwise. |
1199 | */ |
1201 | */ |
1200 | |
1202 | |
1201 | if (size) |
1203 | if (size) |
1202 | return realloc (ptr, size); |
1204 | return realloc (ptr, size); |
1203 | |
1205 | |
1204 | free (ptr); |
1206 | free (ptr); |
1205 | return 0; |
1207 | return 0; |
1206 | #endif |
|
|
1207 | } |
1208 | } |
1208 | |
1209 | |
1209 | static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul; |
1210 | static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul; |
1210 | |
1211 | |
1211 | void ecb_cold |
1212 | void ecb_cold |
… | |
… | |
1866 | static void noinline ecb_cold |
1867 | static void noinline ecb_cold |
1867 | evpipe_init (EV_P) |
1868 | evpipe_init (EV_P) |
1868 | { |
1869 | { |
1869 | if (!ev_is_active (&pipe_w)) |
1870 | if (!ev_is_active (&pipe_w)) |
1870 | { |
1871 | { |
|
|
1872 | int fds [2]; |
|
|
1873 | |
1871 | # if EV_USE_EVENTFD |
1874 | # if EV_USE_EVENTFD |
|
|
1875 | fds [0] = -1; |
1872 | evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); |
1876 | fds [1] = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); |
1873 | if (evfd < 0 && errno == EINVAL) |
1877 | if (fds [1] < 0 && errno == EINVAL) |
1874 | evfd = eventfd (0, 0); |
1878 | fds [1] = eventfd (0, 0); |
1875 | |
1879 | |
1876 | if (evfd >= 0) |
1880 | if (fds [1] < 0) |
1877 | { |
|
|
1878 | evpipe [0] = -1; |
|
|
1879 | fd_intern (evfd); /* doing it twice doesn't hurt */ |
|
|
1880 | ev_io_set (&pipe_w, evfd, EV_READ); |
|
|
1881 | } |
|
|
1882 | else |
|
|
1883 | # endif |
1881 | # endif |
1884 | { |
1882 | { |
1885 | while (pipe (evpipe)) |
1883 | while (pipe (fds)) |
1886 | ev_syserr ("(libev) error creating signal/async pipe"); |
1884 | ev_syserr ("(libev) error creating signal/async pipe"); |
1887 | |
1885 | |
1888 | fd_intern (evpipe [0]); |
1886 | fd_intern (fds [0]); |
1889 | fd_intern (evpipe [1]); |
|
|
1890 | ev_io_set (&pipe_w, evpipe [0], EV_READ); |
|
|
1891 | } |
1887 | } |
1892 | |
1888 | |
|
|
1889 | fd_intern (fds [1]); |
|
|
1890 | |
|
|
1891 | evpipe [0] = fds [0]; |
|
|
1892 | |
|
|
1893 | if (evpipe [1] < 0) |
|
|
1894 | evpipe [1] = fds [1]; /* first call, set write fd */ |
|
|
1895 | else |
|
|
1896 | { |
|
|
1897 | /* on subsequent calls, do not change evpipe [1] */ |
|
|
1898 | /* so that evpipe_write can always rely on its value. */ |
|
|
1899 | /* this branch does not do anything sensible on windows, */ |
|
|
1900 | /* so must not be executed on windows */ |
|
|
1901 | |
|
|
1902 | dup2 (fds [1], evpipe [1]); |
|
|
1903 | close (fds [1]); |
|
|
1904 | } |
|
|
1905 | |
|
|
1906 | ev_io_set (&pipe_w, evpipe [0] < 0 ? evpipe [1] : evpipe [0], EV_READ); |
1893 | ev_io_start (EV_A_ &pipe_w); |
1907 | ev_io_start (EV_A_ &pipe_w); |
1894 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
1908 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
1895 | } |
1909 | } |
1896 | } |
1910 | } |
1897 | |
1911 | |
… | |
… | |
1918 | ECB_MEMORY_FENCE_RELEASE; |
1932 | ECB_MEMORY_FENCE_RELEASE; |
1919 | |
1933 | |
1920 | old_errno = errno; /* save errno because write will clobber it */ |
1934 | old_errno = errno; /* save errno because write will clobber it */ |
1921 | |
1935 | |
1922 | #if EV_USE_EVENTFD |
1936 | #if EV_USE_EVENTFD |
1923 | if (evfd >= 0) |
1937 | if (evpipe [0] < 0) |
1924 | { |
1938 | { |
1925 | uint64_t counter = 1; |
1939 | uint64_t counter = 1; |
1926 | write (evfd, &counter, sizeof (uint64_t)); |
1940 | write (evpipe [1], &counter, sizeof (uint64_t)); |
1927 | } |
1941 | } |
1928 | else |
1942 | else |
1929 | #endif |
1943 | #endif |
1930 | { |
1944 | { |
1931 | #ifdef _WIN32 |
1945 | #ifdef _WIN32 |
… | |
… | |
1951 | int i; |
1965 | int i; |
1952 | |
1966 | |
1953 | if (revents & EV_READ) |
1967 | if (revents & EV_READ) |
1954 | { |
1968 | { |
1955 | #if EV_USE_EVENTFD |
1969 | #if EV_USE_EVENTFD |
1956 | if (evfd >= 0) |
1970 | if (evpipe [0] < 0) |
1957 | { |
1971 | { |
1958 | uint64_t counter; |
1972 | uint64_t counter; |
1959 | read (evfd, &counter, sizeof (uint64_t)); |
1973 | read (evpipe [1], &counter, sizeof (uint64_t)); |
1960 | } |
1974 | } |
1961 | else |
1975 | else |
1962 | #endif |
1976 | #endif |
1963 | { |
1977 | { |
1964 | char dummy[4]; |
1978 | char dummy[4]; |
… | |
… | |
2020 | |
2034 | |
2021 | if (!EV_A) |
2035 | if (!EV_A) |
2022 | return; |
2036 | return; |
2023 | #endif |
2037 | #endif |
2024 | |
2038 | |
2025 | if (!ev_active (&pipe_w)) |
|
|
2026 | return; |
|
|
2027 | |
|
|
2028 | signals [signum - 1].pending = 1; |
2039 | signals [signum - 1].pending = 1; |
2029 | evpipe_write (EV_A_ &sig_pending); |
2040 | evpipe_write (EV_A_ &sig_pending); |
2030 | } |
2041 | } |
2031 | |
2042 | |
2032 | static void |
2043 | static void |
… | |
… | |
2042 | void noinline |
2053 | void noinline |
2043 | ev_feed_signal_event (EV_P_ int signum) EV_THROW |
2054 | ev_feed_signal_event (EV_P_ int signum) EV_THROW |
2044 | { |
2055 | { |
2045 | WL w; |
2056 | WL w; |
2046 | |
2057 | |
2047 | if (expect_false (signum <= 0 || signum > EV_NSIG)) |
2058 | if (expect_false (signum <= 0 || signum >= EV_NSIG)) |
2048 | return; |
2059 | return; |
2049 | |
2060 | |
2050 | --signum; |
2061 | --signum; |
2051 | |
2062 | |
2052 | #if EV_MULTIPLICITY |
2063 | #if EV_MULTIPLICITY |
… | |
… | |
2350 | #if EV_ASYNC_ENABLE |
2361 | #if EV_ASYNC_ENABLE |
2351 | async_pending = 0; |
2362 | async_pending = 0; |
2352 | #endif |
2363 | #endif |
2353 | pipe_write_skipped = 0; |
2364 | pipe_write_skipped = 0; |
2354 | pipe_write_wanted = 0; |
2365 | pipe_write_wanted = 0; |
|
|
2366 | evpipe [0] = -1; |
|
|
2367 | evpipe [1] = -1; |
2355 | #if EV_USE_INOTIFY |
2368 | #if EV_USE_INOTIFY |
2356 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
2369 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
2357 | #endif |
2370 | #endif |
2358 | #if EV_USE_SIGNALFD |
2371 | #if EV_USE_SIGNALFD |
2359 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
2372 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
… | |
… | |
2422 | if (ev_is_active (&pipe_w)) |
2435 | if (ev_is_active (&pipe_w)) |
2423 | { |
2436 | { |
2424 | /*ev_ref (EV_A);*/ |
2437 | /*ev_ref (EV_A);*/ |
2425 | /*ev_io_stop (EV_A_ &pipe_w);*/ |
2438 | /*ev_io_stop (EV_A_ &pipe_w);*/ |
2426 | |
2439 | |
2427 | #if EV_USE_EVENTFD |
|
|
2428 | if (evfd >= 0) |
|
|
2429 | close (evfd); |
|
|
2430 | #endif |
|
|
2431 | |
|
|
2432 | if (evpipe [0] >= 0) |
|
|
2433 | { |
|
|
2434 | EV_WIN32_CLOSE_FD (evpipe [0]); |
2440 | if (evpipe [0] >= 0) EV_WIN32_CLOSE_FD (evpipe [0]); |
2435 | EV_WIN32_CLOSE_FD (evpipe [1]); |
2441 | if (evpipe [1] >= 0) EV_WIN32_CLOSE_FD (evpipe [1]); |
2436 | } |
|
|
2437 | } |
2442 | } |
2438 | |
2443 | |
2439 | #if EV_USE_SIGNALFD |
2444 | #if EV_USE_SIGNALFD |
2440 | if (ev_is_active (&sigfd_w)) |
2445 | if (ev_is_active (&sigfd_w)) |
2441 | close (sigfd); |
2446 | close (sigfd); |
… | |
… | |
2527 | #endif |
2532 | #endif |
2528 | #if EV_USE_INOTIFY |
2533 | #if EV_USE_INOTIFY |
2529 | infy_fork (EV_A); |
2534 | infy_fork (EV_A); |
2530 | #endif |
2535 | #endif |
2531 | |
2536 | |
|
|
2537 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
2532 | if (ev_is_active (&pipe_w)) |
2538 | if (ev_is_active (&pipe_w)) |
2533 | { |
2539 | { |
2534 | /* pipe_write_wanted must be false now, so modifying fd vars should be safe */ |
2540 | /* pipe_write_wanted must be false now, so modifying fd vars should be safe */ |
2535 | |
2541 | |
2536 | ev_ref (EV_A); |
2542 | ev_ref (EV_A); |
2537 | ev_io_stop (EV_A_ &pipe_w); |
2543 | ev_io_stop (EV_A_ &pipe_w); |
2538 | |
2544 | |
2539 | #if EV_USE_EVENTFD |
|
|
2540 | if (evfd >= 0) |
|
|
2541 | close (evfd); |
|
|
2542 | #endif |
|
|
2543 | |
|
|
2544 | if (evpipe [0] >= 0) |
2545 | if (evpipe [0] >= 0) |
2545 | { |
|
|
2546 | EV_WIN32_CLOSE_FD (evpipe [0]); |
2546 | EV_WIN32_CLOSE_FD (evpipe [0]); |
2547 | EV_WIN32_CLOSE_FD (evpipe [1]); |
|
|
2548 | } |
|
|
2549 | |
2547 | |
2550 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
|
|
2551 | evpipe_init (EV_A); |
2548 | evpipe_init (EV_A); |
2552 | /* now iterate over everything, in case we missed something */ |
2549 | /* iterate over everything, in case we missed something before */ |
2553 | pipecb (EV_A_ &pipe_w, EV_READ); |
2550 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
2554 | #endif |
|
|
2555 | } |
2551 | } |
|
|
2552 | #endif |
2556 | |
2553 | |
2557 | postfork = 0; |
2554 | postfork = 0; |
2558 | } |
2555 | } |
2559 | |
2556 | |
2560 | #if EV_MULTIPLICITY |
2557 | #if EV_MULTIPLICITY |
… | |
… | |
2759 | } |
2756 | } |
2760 | |
2757 | |
2761 | void noinline |
2758 | void noinline |
2762 | ev_invoke_pending (EV_P) |
2759 | ev_invoke_pending (EV_P) |
2763 | { |
2760 | { |
2764 | for (pendingpri = NUMPRI; pendingpri--; ) /* pendingpri is modified during the loop */ |
2761 | pendingpri = NUMPRI; |
|
|
2762 | |
|
|
2763 | while (pendingpri) /* pendingpri possibly gets modified in the inner loop */ |
|
|
2764 | { |
|
|
2765 | --pendingpri; |
|
|
2766 | |
2765 | while (pendingcnt [pendingpri]) |
2767 | while (pendingcnt [pendingpri]) |
2766 | { |
2768 | { |
2767 | ANPENDING *p = pendings [pendingpri] + --pendingcnt [pendingpri]; |
2769 | ANPENDING *p = pendings [pendingpri] + --pendingcnt [pendingpri]; |
2768 | |
2770 | |
2769 | p->w->pending = 0; |
2771 | p->w->pending = 0; |
2770 | EV_CB_INVOKE (p->w, p->events); |
2772 | EV_CB_INVOKE (p->w, p->events); |
2771 | EV_FREQUENT_CHECK; |
2773 | EV_FREQUENT_CHECK; |
2772 | } |
2774 | } |
|
|
2775 | } |
2773 | } |
2776 | } |
2774 | |
2777 | |
2775 | #if EV_IDLE_ENABLE |
2778 | #if EV_IDLE_ENABLE |
2776 | /* make idle watchers pending. this handles the "call-idle */ |
2779 | /* make idle watchers pending. this handles the "call-idle */ |
2777 | /* only when higher priorities are idle" logic */ |
2780 | /* only when higher priorities are idle" logic */ |
… | |
… | |
3135 | backend_poll (EV_A_ waittime); |
3138 | backend_poll (EV_A_ waittime); |
3136 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
3139 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
3137 | |
3140 | |
3138 | pipe_write_wanted = 0; /* just an optimisation, no fence needed */ |
3141 | pipe_write_wanted = 0; /* just an optimisation, no fence needed */ |
3139 | |
3142 | |
3140 | MEMORY_FENCE_ACQUIRE; |
3143 | ECB_MEMORY_FENCE_ACQUIRE; |
3141 | if (pipe_write_skipped) |
3144 | if (pipe_write_skipped) |
3142 | { |
3145 | { |
3143 | assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); |
3146 | assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); |
3144 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
3147 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
3145 | } |
3148 | } |