… | |
… | |
201 | # include <sys/wait.h> |
201 | # include <sys/wait.h> |
202 | # include <unistd.h> |
202 | # include <unistd.h> |
203 | #else |
203 | #else |
204 | # include <io.h> |
204 | # include <io.h> |
205 | # define WIN32_LEAN_AND_MEAN |
205 | # define WIN32_LEAN_AND_MEAN |
|
|
206 | # include <winsock2.h> |
206 | # include <windows.h> |
207 | # include <windows.h> |
207 | # ifndef EV_SELECT_IS_WINSOCKET |
208 | # ifndef EV_SELECT_IS_WINSOCKET |
208 | # define EV_SELECT_IS_WINSOCKET 1 |
209 | # define EV_SELECT_IS_WINSOCKET 1 |
209 | # endif |
210 | # endif |
210 | # undef EV_AVOID_STDIO |
211 | # undef EV_AVOID_STDIO |
… | |
… | |
359 | #endif |
360 | #endif |
360 | |
361 | |
361 | /* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */ |
362 | /* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */ |
362 | /* which makes programs even slower. might work on other unices, too. */ |
363 | /* which makes programs even slower. might work on other unices, too. */ |
363 | #if EV_USE_CLOCK_SYSCALL |
364 | #if EV_USE_CLOCK_SYSCALL |
364 | # include <syscall.h> |
365 | # include <sys/syscall.h> |
365 | # ifdef SYS_clock_gettime |
366 | # ifdef SYS_clock_gettime |
366 | # define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
367 | # define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
367 | # undef EV_USE_MONOTONIC |
368 | # undef EV_USE_MONOTONIC |
368 | # define EV_USE_MONOTONIC 1 |
369 | # define EV_USE_MONOTONIC 1 |
369 | # else |
370 | # else |
… | |
… | |
408 | /* some very old inotify.h headers don't have IN_DONT_FOLLOW */ |
409 | /* some very old inotify.h headers don't have IN_DONT_FOLLOW */ |
409 | # ifndef IN_DONT_FOLLOW |
410 | # ifndef IN_DONT_FOLLOW |
410 | # undef EV_USE_INOTIFY |
411 | # undef EV_USE_INOTIFY |
411 | # define EV_USE_INOTIFY 0 |
412 | # define EV_USE_INOTIFY 0 |
412 | # endif |
413 | # endif |
413 | #endif |
|
|
414 | |
|
|
415 | #if EV_SELECT_IS_WINSOCKET |
|
|
416 | # include <winsock.h> |
|
|
417 | #endif |
414 | #endif |
418 | |
415 | |
419 | #if EV_USE_EVENTFD |
416 | #if EV_USE_EVENTFD |
420 | /* our minimum requirement is glibc 2.7 which has the stub, but not the header */ |
417 | /* our minimum requirement is glibc 2.7 which has the stub, but not the header */ |
421 | # include <stdint.h> |
418 | # include <stdint.h> |
… | |
… | |
1108 | #endif |
1105 | #endif |
1109 | |
1106 | |
1110 | static void (*syserr_cb)(const char *msg) EV_THROW; |
1107 | static void (*syserr_cb)(const char *msg) EV_THROW; |
1111 | |
1108 | |
1112 | void ecb_cold |
1109 | void ecb_cold |
1113 | ev_set_syserr_cb (void (*cb)(const char *msg)) EV_THROW |
1110 | ev_set_syserr_cb (void (*cb)(const char *msg) EV_THROW) EV_THROW |
1114 | { |
1111 | { |
1115 | syserr_cb = cb; |
1112 | syserr_cb = cb; |
1116 | } |
1113 | } |
1117 | |
1114 | |
1118 | static void noinline ecb_cold |
1115 | static void noinline ecb_cold |
… | |
… | |
1136 | abort (); |
1133 | abort (); |
1137 | } |
1134 | } |
1138 | } |
1135 | } |
1139 | |
1136 | |
1140 | static void * |
1137 | static void * |
1141 | ev_realloc_emul (void *ptr, long size) |
1138 | ev_realloc_emul (void *ptr, long size) EV_THROW |
1142 | { |
1139 | { |
1143 | #if __GLIBC__ |
1140 | #if __GLIBC__ |
1144 | return realloc (ptr, size); |
1141 | return realloc (ptr, size); |
1145 | #else |
1142 | #else |
1146 | /* some systems, notably openbsd and darwin, fail to properly |
1143 | /* some systems, notably openbsd and darwin, fail to properly |
… | |
… | |
1157 | } |
1154 | } |
1158 | |
1155 | |
1159 | static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul; |
1156 | static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul; |
1160 | |
1157 | |
1161 | void ecb_cold |
1158 | void ecb_cold |
1162 | ev_set_allocator (void *(*cb)(void *ptr, long size)) EV_THROW |
1159 | ev_set_allocator (void *(*cb)(void *ptr, long size) EV_THROW) EV_THROW |
1163 | { |
1160 | { |
1164 | alloc = cb; |
1161 | alloc = cb; |
1165 | } |
1162 | } |
1166 | |
1163 | |
1167 | inline_speed void * |
1164 | inline_speed void * |
… | |
… | |
1430 | w_->pending = ++pendingcnt [pri]; |
1427 | w_->pending = ++pendingcnt [pri]; |
1431 | array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2); |
1428 | array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2); |
1432 | pendings [pri][w_->pending - 1].w = w_; |
1429 | pendings [pri][w_->pending - 1].w = w_; |
1433 | pendings [pri][w_->pending - 1].events = revents; |
1430 | pendings [pri][w_->pending - 1].events = revents; |
1434 | } |
1431 | } |
|
|
1432 | |
|
|
1433 | pendingpri = NUMPRI - 1; |
1435 | } |
1434 | } |
1436 | |
1435 | |
1437 | inline_speed void |
1436 | inline_speed void |
1438 | feed_reverse (EV_P_ W w) |
1437 | feed_reverse (EV_P_ W w) |
1439 | { |
1438 | { |
… | |
… | |
1844 | } |
1843 | } |
1845 | |
1844 | |
1846 | inline_speed void |
1845 | inline_speed void |
1847 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1846 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1848 | { |
1847 | { |
|
|
1848 | ECB_MEMORY_FENCE; /* push out the write before this function was called, acquire flag */ |
|
|
1849 | |
1849 | if (expect_true (*flag)) |
1850 | if (expect_true (*flag)) |
1850 | return; |
1851 | return; |
1851 | |
1852 | |
1852 | *flag = 1; |
1853 | *flag = 1; |
1853 | |
1854 | |
… | |
… | |
1872 | write (evfd, &counter, sizeof (uint64_t)); |
1873 | write (evfd, &counter, sizeof (uint64_t)); |
1873 | } |
1874 | } |
1874 | else |
1875 | else |
1875 | #endif |
1876 | #endif |
1876 | { |
1877 | { |
1877 | /* win32 people keep sending patches that change this write() to send() */ |
1878 | #ifdef _WIN32 |
1878 | /* and then run away. but send() is wrong, it wants a socket handle on win32 */ |
1879 | WSABUF buf; |
1879 | /* so when you think this write should be a send instead, please find out */ |
1880 | DWORD sent; |
1880 | /* where your send() is from - it's definitely not the microsoft send, and */ |
1881 | buf.buf = &buf; |
1881 | /* tell me. thank you. */ |
1882 | buf.len = 1; |
1882 | /* it might be that your problem is that your environment needs EV_USE_WSASOCKET */ |
1883 | WSASend (EV_FD_TO_WIN32_HANDLE (evpipe [1]), &buf, 1, &sent, 0, 0, 0); |
1883 | /* check the ev documentation on how to use this flag */ |
1884 | #else |
1884 | write (evpipe [1], &(evpipe [1]), 1); |
1885 | write (evpipe [1], &(evpipe [1]), 1); |
|
|
1886 | #endif |
1885 | } |
1887 | } |
1886 | |
1888 | |
1887 | errno = old_errno; |
1889 | errno = old_errno; |
1888 | } |
1890 | } |
1889 | } |
1891 | } |
… | |
… | |
1904 | read (evfd, &counter, sizeof (uint64_t)); |
1906 | read (evfd, &counter, sizeof (uint64_t)); |
1905 | } |
1907 | } |
1906 | else |
1908 | else |
1907 | #endif |
1909 | #endif |
1908 | { |
1910 | { |
1909 | char dummy; |
1911 | char dummy[4]; |
1910 | /* see discussion in evpipe_write when you think this read should be recv in win32 */ |
1912 | #ifdef _WIN32 |
|
|
1913 | WSABUF buf; |
|
|
1914 | DWORD recvd; |
|
|
1915 | DWORD flags = 0; |
|
|
1916 | buf.buf = dummy; |
|
|
1917 | buf.len = sizeof (dummy); |
|
|
1918 | WSARecv (EV_FD_TO_WIN32_HANDLE (evpipe [0]), &buf, 1, &recvd, &flags, 0, 0); |
|
|
1919 | #else |
1911 | read (evpipe [0], &dummy, 1); |
1920 | read (evpipe [0], &dummy, sizeof (dummy)); |
|
|
1921 | #endif |
1912 | } |
1922 | } |
1913 | } |
1923 | } |
1914 | |
1924 | |
1915 | pipe_write_skipped = 0; |
1925 | pipe_write_skipped = 0; |
|
|
1926 | |
|
|
1927 | ECB_MEMORY_FENCE; /* push out skipped, acquire flags */ |
1916 | |
1928 | |
1917 | #if EV_SIGNAL_ENABLE |
1929 | #if EV_SIGNAL_ENABLE |
1918 | if (sig_pending) |
1930 | if (sig_pending) |
1919 | { |
1931 | { |
1920 | sig_pending = 0; |
1932 | sig_pending = 0; |
|
|
1933 | |
|
|
1934 | ECB_MEMORY_FENCE_RELEASE; |
1921 | |
1935 | |
1922 | for (i = EV_NSIG - 1; i--; ) |
1936 | for (i = EV_NSIG - 1; i--; ) |
1923 | if (expect_false (signals [i].pending)) |
1937 | if (expect_false (signals [i].pending)) |
1924 | ev_feed_signal_event (EV_A_ i + 1); |
1938 | ev_feed_signal_event (EV_A_ i + 1); |
1925 | } |
1939 | } |
… | |
… | |
1927 | |
1941 | |
1928 | #if EV_ASYNC_ENABLE |
1942 | #if EV_ASYNC_ENABLE |
1929 | if (async_pending) |
1943 | if (async_pending) |
1930 | { |
1944 | { |
1931 | async_pending = 0; |
1945 | async_pending = 0; |
|
|
1946 | |
|
|
1947 | ECB_MEMORY_FENCE_RELEASE; |
1932 | |
1948 | |
1933 | for (i = asynccnt; i--; ) |
1949 | for (i = asynccnt; i--; ) |
1934 | if (asyncs [i]->sent) |
1950 | if (asyncs [i]->sent) |
1935 | { |
1951 | { |
1936 | asyncs [i]->sent = 0; |
1952 | asyncs [i]->sent = 0; |
… | |
… | |
2319 | } |
2335 | } |
2320 | } |
2336 | } |
2321 | |
2337 | |
2322 | /* free up a loop structure */ |
2338 | /* free up a loop structure */ |
2323 | void ecb_cold |
2339 | void ecb_cold |
2324 | ev_loop_destroy (EV_P) EV_THROW |
2340 | ev_loop_destroy (EV_P) |
2325 | { |
2341 | { |
2326 | int i; |
2342 | int i; |
2327 | |
2343 | |
2328 | #if EV_MULTIPLICITY |
2344 | #if EV_MULTIPLICITY |
2329 | /* mimic free (0) */ |
2345 | /* mimic free (0) */ |
… | |
… | |
2339 | EV_INVOKE_PENDING; |
2355 | EV_INVOKE_PENDING; |
2340 | } |
2356 | } |
2341 | #endif |
2357 | #endif |
2342 | |
2358 | |
2343 | #if EV_CHILD_ENABLE |
2359 | #if EV_CHILD_ENABLE |
2344 | if (ev_is_active (&childev)) |
2360 | if (ev_is_default_loop (EV_A) && ev_is_active (&childev)) |
2345 | { |
2361 | { |
2346 | ev_ref (EV_A); /* child watcher */ |
2362 | ev_ref (EV_A); /* child watcher */ |
2347 | ev_signal_stop (EV_A_ &childev); |
2363 | ev_signal_stop (EV_A_ &childev); |
2348 | } |
2364 | } |
2349 | #endif |
2365 | #endif |
… | |
… | |
2545 | void ecb_cold |
2561 | void ecb_cold |
2546 | ev_verify (EV_P) EV_THROW |
2562 | ev_verify (EV_P) EV_THROW |
2547 | { |
2563 | { |
2548 | #if EV_VERIFY |
2564 | #if EV_VERIFY |
2549 | int i; |
2565 | int i; |
2550 | WL w; |
2566 | WL w, w2; |
2551 | |
2567 | |
2552 | assert (activecnt >= -1); |
2568 | assert (activecnt >= -1); |
2553 | |
2569 | |
2554 | assert (fdchangemax >= fdchangecnt); |
2570 | assert (fdchangemax >= fdchangecnt); |
2555 | for (i = 0; i < fdchangecnt; ++i) |
2571 | for (i = 0; i < fdchangecnt; ++i) |
2556 | assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0)); |
2572 | assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0)); |
2557 | |
2573 | |
2558 | assert (anfdmax >= 0); |
2574 | assert (anfdmax >= 0); |
2559 | for (i = 0; i < anfdmax; ++i) |
2575 | for (i = 0; i < anfdmax; ++i) |
|
|
2576 | { |
|
|
2577 | int j = 0; |
|
|
2578 | |
2560 | for (w = anfds [i].head; w; w = w->next) |
2579 | for (w = w2 = anfds [i].head; w; w = w->next) |
2561 | { |
2580 | { |
2562 | verify_watcher (EV_A_ (W)w); |
2581 | verify_watcher (EV_A_ (W)w); |
|
|
2582 | |
|
|
2583 | if (j++ & 1) |
|
|
2584 | { |
|
|
2585 | assert (("libev: io watcher list contains a loop", w != w2)); |
|
|
2586 | w2 = w2->next; |
|
|
2587 | } |
|
|
2588 | |
2563 | assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1)); |
2589 | assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1)); |
2564 | assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i)); |
2590 | assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i)); |
2565 | } |
2591 | } |
|
|
2592 | } |
2566 | |
2593 | |
2567 | assert (timermax >= timercnt); |
2594 | assert (timermax >= timercnt); |
2568 | verify_heap (EV_A_ timers, timercnt); |
2595 | verify_heap (EV_A_ timers, timercnt); |
2569 | |
2596 | |
2570 | #if EV_PERIODIC_ENABLE |
2597 | #if EV_PERIODIC_ENABLE |
… | |
… | |
2677 | } |
2704 | } |
2678 | |
2705 | |
2679 | void noinline |
2706 | void noinline |
2680 | ev_invoke_pending (EV_P) |
2707 | ev_invoke_pending (EV_P) |
2681 | { |
2708 | { |
2682 | int pri; |
2709 | for (pendingpri = NUMPRI; pendingpri--; ) /* pendingpri is modified during the loop */ |
2683 | |
|
|
2684 | for (pri = NUMPRI; pri--; ) |
|
|
2685 | while (pendingcnt [pri]) |
2710 | while (pendingcnt [pendingpri]) |
2686 | { |
2711 | { |
2687 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
2712 | ANPENDING *p = pendings [pendingpri] + --pendingcnt [pendingpri]; |
2688 | |
2713 | |
2689 | p->w->pending = 0; |
2714 | p->w->pending = 0; |
2690 | EV_CB_INVOKE (p->w, p->events); |
2715 | EV_CB_INVOKE (p->w, p->events); |
2691 | EV_FREQUENT_CHECK; |
2716 | EV_FREQUENT_CHECK; |
2692 | } |
2717 | } |
… | |
… | |
2787 | { |
2812 | { |
2788 | EV_FREQUENT_CHECK; |
2813 | EV_FREQUENT_CHECK; |
2789 | |
2814 | |
2790 | while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) |
2815 | while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) |
2791 | { |
2816 | { |
2792 | int feed_count = 0; |
|
|
2793 | |
|
|
2794 | do |
2817 | do |
2795 | { |
2818 | { |
2796 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); |
2819 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); |
2797 | |
2820 | |
2798 | /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/ |
2821 | /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/ |
… | |
… | |
3241 | EV_FREQUENT_CHECK; |
3264 | EV_FREQUENT_CHECK; |
3242 | |
3265 | |
3243 | ev_start (EV_A_ (W)w, 1); |
3266 | ev_start (EV_A_ (W)w, 1); |
3244 | array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); |
3267 | array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); |
3245 | wlist_add (&anfds[fd].head, (WL)w); |
3268 | wlist_add (&anfds[fd].head, (WL)w); |
|
|
3269 | |
|
|
3270 | /* common bug, apparently */ |
|
|
3271 | assert (("libev: ev_io_start called with corrupted watcher", ((WL)w)->next != (WL)w)); |
3246 | |
3272 | |
3247 | fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY); |
3273 | fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY); |
3248 | w->events &= ~EV__IOFDSET; |
3274 | w->events &= ~EV__IOFDSET; |
3249 | |
3275 | |
3250 | EV_FREQUENT_CHECK; |
3276 | EV_FREQUENT_CHECK; |