ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
(Generate patch)

Comparing libev/ev.c (file contents):
Revision 1.418 by root, Mon Apr 2 23:14:41 2012 UTC vs.
Revision 1.427 by root, Sun May 6 19:29:59 2012 UTC

359#endif 359#endif
360 360
361/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */ 361/* 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. */ 362/* which makes programs even slower. might work on other unices, too. */
363#if EV_USE_CLOCK_SYSCALL 363#if EV_USE_CLOCK_SYSCALL
364# include <syscall.h> 364# include <sys/syscall.h>
365# ifdef SYS_clock_gettime 365# ifdef SYS_clock_gettime
366# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) 366# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
367# undef EV_USE_MONOTONIC 367# undef EV_USE_MONOTONIC
368# define EV_USE_MONOTONIC 1 368# define EV_USE_MONOTONIC 1
369# else 369# else
579 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore") 579 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore")
580 #elif defined __s390__ || defined __s390x__ 580 #elif defined __s390__ || defined __s390x__
581 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory") 581 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory")
582 #elif defined __mips__ 582 #elif defined __mips__
583 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") 583 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
584 #elif defined __alpha__
585 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mb" : : : "memory")
584 #endif 586 #endif
585 #endif 587 #endif
586#endif 588#endif
587 589
588#ifndef ECB_MEMORY_FENCE 590#ifndef ECB_MEMORY_FENCE
1103{ 1105{
1104 write (STDERR_FILENO, msg, strlen (msg)); 1106 write (STDERR_FILENO, msg, strlen (msg));
1105} 1107}
1106#endif 1108#endif
1107 1109
1108static void (*syserr_cb)(const char *msg); 1110static void (*syserr_cb)(const char *msg) EV_THROW;
1109 1111
1110void ecb_cold 1112void ecb_cold
1111ev_set_syserr_cb (void (*cb)(const char *msg)) 1113ev_set_syserr_cb (void (*cb)(const char *msg)) EV_THROW
1112{ 1114{
1113 syserr_cb = cb; 1115 syserr_cb = cb;
1114} 1116}
1115 1117
1116static void noinline ecb_cold 1118static void noinline ecb_cold
1152 free (ptr); 1154 free (ptr);
1153 return 0; 1155 return 0;
1154#endif 1156#endif
1155} 1157}
1156 1158
1157static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; 1159static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul;
1158 1160
1159void ecb_cold 1161void ecb_cold
1160ev_set_allocator (void *(*cb)(void *ptr, long size)) 1162ev_set_allocator (void *(*cb)(void *ptr, long size)) EV_THROW
1161{ 1163{
1162 alloc = cb; 1164 alloc = cb;
1163} 1165}
1164 1166
1165inline_speed void * 1167inline_speed void *
1282 1284
1283/*****************************************************************************/ 1285/*****************************************************************************/
1284 1286
1285#ifndef EV_HAVE_EV_TIME 1287#ifndef EV_HAVE_EV_TIME
1286ev_tstamp 1288ev_tstamp
1287ev_time (void) 1289ev_time (void) EV_THROW
1288{ 1290{
1289#if EV_USE_REALTIME 1291#if EV_USE_REALTIME
1290 if (expect_true (have_realtime)) 1292 if (expect_true (have_realtime))
1291 { 1293 {
1292 struct timespec ts; 1294 struct timespec ts;
1316 return ev_time (); 1318 return ev_time ();
1317} 1319}
1318 1320
1319#if EV_MULTIPLICITY 1321#if EV_MULTIPLICITY
1320ev_tstamp 1322ev_tstamp
1321ev_now (EV_P) 1323ev_now (EV_P) EV_THROW
1322{ 1324{
1323 return ev_rt_now; 1325 return ev_rt_now;
1324} 1326}
1325#endif 1327#endif
1326 1328
1327void 1329void
1328ev_sleep (ev_tstamp delay) 1330ev_sleep (ev_tstamp delay) EV_THROW
1329{ 1331{
1330 if (delay > 0.) 1332 if (delay > 0.)
1331 { 1333 {
1332#if EV_USE_NANOSLEEP 1334#if EV_USE_NANOSLEEP
1333 struct timespec ts; 1335 struct timespec ts;
1414pendingcb (EV_P_ ev_prepare *w, int revents) 1416pendingcb (EV_P_ ev_prepare *w, int revents)
1415{ 1417{
1416} 1418}
1417 1419
1418void noinline 1420void noinline
1419ev_feed_event (EV_P_ void *w, int revents) 1421ev_feed_event (EV_P_ void *w, int revents) EV_THROW
1420{ 1422{
1421 W w_ = (W)w; 1423 W w_ = (W)w;
1422 int pri = ABSPRI (w_); 1424 int pri = ABSPRI (w_);
1423 1425
1424 if (expect_false (w_->pending)) 1426 if (expect_false (w_->pending))
1428 w_->pending = ++pendingcnt [pri]; 1430 w_->pending = ++pendingcnt [pri];
1429 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2); 1431 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2);
1430 pendings [pri][w_->pending - 1].w = w_; 1432 pendings [pri][w_->pending - 1].w = w_;
1431 pendings [pri][w_->pending - 1].events = revents; 1433 pendings [pri][w_->pending - 1].events = revents;
1432 } 1434 }
1435
1436 pendingpri = NUMPRI - 1;
1433} 1437}
1434 1438
1435inline_speed void 1439inline_speed void
1436feed_reverse (EV_P_ W w) 1440feed_reverse (EV_P_ W w)
1437{ 1441{
1483 if (expect_true (!anfd->reify)) 1487 if (expect_true (!anfd->reify))
1484 fd_event_nocheck (EV_A_ fd, revents); 1488 fd_event_nocheck (EV_A_ fd, revents);
1485} 1489}
1486 1490
1487void 1491void
1488ev_feed_fd_event (EV_P_ int fd, int revents) 1492ev_feed_fd_event (EV_P_ int fd, int revents) EV_THROW
1489{ 1493{
1490 if (fd >= 0 && fd < anfdmax) 1494 if (fd >= 0 && fd < anfdmax)
1491 fd_event_nocheck (EV_A_ fd, revents); 1495 fd_event_nocheck (EV_A_ fd, revents);
1492} 1496}
1493 1497
1842} 1846}
1843 1847
1844inline_speed void 1848inline_speed void
1845evpipe_write (EV_P_ EV_ATOMIC_T *flag) 1849evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1846{ 1850{
1851 ECB_MEMORY_FENCE; /* push out the write before this function was called, acquire flag */
1852
1847 if (expect_true (*flag)) 1853 if (expect_true (*flag))
1848 return; 1854 return;
1849 1855
1850 *flag = 1; 1856 *flag = 1;
1851 1857
1870 write (evfd, &counter, sizeof (uint64_t)); 1876 write (evfd, &counter, sizeof (uint64_t));
1871 } 1877 }
1872 else 1878 else
1873#endif 1879#endif
1874 { 1880 {
1875 /* win32 people keep sending patches that change this write() to send() */ 1881#ifdef _WIN32
1876 /* and then run away. but send() is wrong, it wants a socket handle on win32 */ 1882 WSABUF buf;
1877 /* so when you think this write should be a send instead, please find out */ 1883 DWORD sent;
1878 /* where your send() is from - it's definitely not the microsoft send, and */ 1884 buf.buf = &buf;
1879 /* tell me. thank you. */ 1885 buf.len = 1;
1880 /* it might be that your problem is that your environment needs EV_USE_WSASOCKET */ 1886 WSASend (EV_FD_TO_WIN32_HANDLE (evpipe [1]), &buf, 1, &sent, 0, 0, 0);
1881 /* check the ev documentation on how to use this flag */ 1887#else
1882 write (evpipe [1], &(evpipe [1]), 1); 1888 write (evpipe [1], &(evpipe [1]), 1);
1889#endif
1883 } 1890 }
1884 1891
1885 errno = old_errno; 1892 errno = old_errno;
1886 } 1893 }
1887} 1894}
1902 read (evfd, &counter, sizeof (uint64_t)); 1909 read (evfd, &counter, sizeof (uint64_t));
1903 } 1910 }
1904 else 1911 else
1905#endif 1912#endif
1906 { 1913 {
1907 char dummy; 1914 char dummy[4];
1908 /* see discussion in evpipe_write when you think this read should be recv in win32 */ 1915#ifdef _WIN32
1916 WSABUF buf;
1917 DWORD recvd;
1918 buf.buf = dummy;
1919 buf.len = sizeof (dummy);
1920 WSARecv (EV_FD_TO_WIN32_HANDLE (evpipe [0]), &buf, 1, &recvd, 0, 0, 0);
1921#else
1909 read (evpipe [0], &dummy, 1); 1922 read (evpipe [0], &dummy, sizeof (dummy));
1923#endif
1910 } 1924 }
1911 } 1925 }
1912 1926
1913 pipe_write_skipped = 0; 1927 pipe_write_skipped = 0;
1928
1929 ECB_MEMORY_FENCE; /* push out skipped, acquire flags */
1914 1930
1915#if EV_SIGNAL_ENABLE 1931#if EV_SIGNAL_ENABLE
1916 if (sig_pending) 1932 if (sig_pending)
1917 { 1933 {
1918 sig_pending = 0; 1934 sig_pending = 0;
1935
1936 ECB_MEMORY_FENCE_RELEASE;
1919 1937
1920 for (i = EV_NSIG - 1; i--; ) 1938 for (i = EV_NSIG - 1; i--; )
1921 if (expect_false (signals [i].pending)) 1939 if (expect_false (signals [i].pending))
1922 ev_feed_signal_event (EV_A_ i + 1); 1940 ev_feed_signal_event (EV_A_ i + 1);
1923 } 1941 }
1925 1943
1926#if EV_ASYNC_ENABLE 1944#if EV_ASYNC_ENABLE
1927 if (async_pending) 1945 if (async_pending)
1928 { 1946 {
1929 async_pending = 0; 1947 async_pending = 0;
1948
1949 ECB_MEMORY_FENCE_RELEASE;
1930 1950
1931 for (i = asynccnt; i--; ) 1951 for (i = asynccnt; i--; )
1932 if (asyncs [i]->sent) 1952 if (asyncs [i]->sent)
1933 { 1953 {
1934 asyncs [i]->sent = 0; 1954 asyncs [i]->sent = 0;
1939} 1959}
1940 1960
1941/*****************************************************************************/ 1961/*****************************************************************************/
1942 1962
1943void 1963void
1944ev_feed_signal (int signum) 1964ev_feed_signal (int signum) EV_THROW
1945{ 1965{
1946#if EV_MULTIPLICITY 1966#if EV_MULTIPLICITY
1947 EV_P = signals [signum - 1].loop; 1967 EV_P = signals [signum - 1].loop;
1948 1968
1949 if (!EV_A) 1969 if (!EV_A)
1966 1986
1967 ev_feed_signal (signum); 1987 ev_feed_signal (signum);
1968} 1988}
1969 1989
1970void noinline 1990void noinline
1971ev_feed_signal_event (EV_P_ int signum) 1991ev_feed_signal_event (EV_P_ int signum) EV_THROW
1972{ 1992{
1973 WL w; 1993 WL w;
1974 1994
1975 if (expect_false (signum <= 0 || signum > EV_NSIG)) 1995 if (expect_false (signum <= 0 || signum > EV_NSIG))
1976 return; 1996 return;
2092#if EV_USE_SELECT 2112#if EV_USE_SELECT
2093# include "ev_select.c" 2113# include "ev_select.c"
2094#endif 2114#endif
2095 2115
2096int ecb_cold 2116int ecb_cold
2097ev_version_major (void) 2117ev_version_major (void) EV_THROW
2098{ 2118{
2099 return EV_VERSION_MAJOR; 2119 return EV_VERSION_MAJOR;
2100} 2120}
2101 2121
2102int ecb_cold 2122int ecb_cold
2103ev_version_minor (void) 2123ev_version_minor (void) EV_THROW
2104{ 2124{
2105 return EV_VERSION_MINOR; 2125 return EV_VERSION_MINOR;
2106} 2126}
2107 2127
2108/* return true if we are running with elevated privileges and should ignore env variables */ 2128/* return true if we are running with elevated privileges and should ignore env variables */
2116 || getgid () != getegid (); 2136 || getgid () != getegid ();
2117#endif 2137#endif
2118} 2138}
2119 2139
2120unsigned int ecb_cold 2140unsigned int ecb_cold
2121ev_supported_backends (void) 2141ev_supported_backends (void) EV_THROW
2122{ 2142{
2123 unsigned int flags = 0; 2143 unsigned int flags = 0;
2124 2144
2125 if (EV_USE_PORT ) flags |= EVBACKEND_PORT; 2145 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
2126 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; 2146 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
2130 2150
2131 return flags; 2151 return flags;
2132} 2152}
2133 2153
2134unsigned int ecb_cold 2154unsigned int ecb_cold
2135ev_recommended_backends (void) 2155ev_recommended_backends (void) EV_THROW
2136{ 2156{
2137 unsigned int flags = ev_supported_backends (); 2157 unsigned int flags = ev_supported_backends ();
2138 2158
2139#ifndef __NetBSD__ 2159#ifndef __NetBSD__
2140 /* kqueue is borked on everything but netbsd apparently */ 2160 /* kqueue is borked on everything but netbsd apparently */
2152 2172
2153 return flags; 2173 return flags;
2154} 2174}
2155 2175
2156unsigned int ecb_cold 2176unsigned int ecb_cold
2157ev_embeddable_backends (void) 2177ev_embeddable_backends (void) EV_THROW
2158{ 2178{
2159 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; 2179 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT;
2160 2180
2161 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ 2181 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */
2162 if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */ 2182 if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */
2164 2184
2165 return flags; 2185 return flags;
2166} 2186}
2167 2187
2168unsigned int 2188unsigned int
2169ev_backend (EV_P) 2189ev_backend (EV_P) EV_THROW
2170{ 2190{
2171 return backend; 2191 return backend;
2172} 2192}
2173 2193
2174#if EV_FEATURE_API 2194#if EV_FEATURE_API
2175unsigned int 2195unsigned int
2176ev_iteration (EV_P) 2196ev_iteration (EV_P) EV_THROW
2177{ 2197{
2178 return loop_count; 2198 return loop_count;
2179} 2199}
2180 2200
2181unsigned int 2201unsigned int
2182ev_depth (EV_P) 2202ev_depth (EV_P) EV_THROW
2183{ 2203{
2184 return loop_depth; 2204 return loop_depth;
2185} 2205}
2186 2206
2187void 2207void
2188ev_set_io_collect_interval (EV_P_ ev_tstamp interval) 2208ev_set_io_collect_interval (EV_P_ ev_tstamp interval) EV_THROW
2189{ 2209{
2190 io_blocktime = interval; 2210 io_blocktime = interval;
2191} 2211}
2192 2212
2193void 2213void
2194ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 2214ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) EV_THROW
2195{ 2215{
2196 timeout_blocktime = interval; 2216 timeout_blocktime = interval;
2197} 2217}
2198 2218
2199void 2219void
2200ev_set_userdata (EV_P_ void *data) 2220ev_set_userdata (EV_P_ void *data) EV_THROW
2201{ 2221{
2202 userdata = data; 2222 userdata = data;
2203} 2223}
2204 2224
2205void * 2225void *
2206ev_userdata (EV_P) 2226ev_userdata (EV_P) EV_THROW
2207{ 2227{
2208 return userdata; 2228 return userdata;
2209} 2229}
2210 2230
2211void 2231void
2212ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) 2232ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) EV_THROW
2213{ 2233{
2214 invoke_cb = invoke_pending_cb; 2234 invoke_cb = invoke_pending_cb;
2215} 2235}
2216 2236
2217void 2237void
2218ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) 2238ev_set_loop_release_cb (EV_P_ void (*release)(EV_P) EV_THROW, void (*acquire)(EV_P) EV_THROW) EV_THROW
2219{ 2239{
2220 release_cb = release; 2240 release_cb = release;
2221 acquire_cb = acquire; 2241 acquire_cb = acquire;
2222} 2242}
2223#endif 2243#endif
2224 2244
2225/* initialise a loop structure, must be zero-initialised */ 2245/* initialise a loop structure, must be zero-initialised */
2226static void noinline ecb_cold 2246static void noinline ecb_cold
2227loop_init (EV_P_ unsigned int flags) 2247loop_init (EV_P_ unsigned int flags) EV_THROW
2228{ 2248{
2229 if (!backend) 2249 if (!backend)
2230 { 2250 {
2231 origflags = flags; 2251 origflags = flags;
2232 2252
2485} 2505}
2486 2506
2487#if EV_MULTIPLICITY 2507#if EV_MULTIPLICITY
2488 2508
2489struct ev_loop * ecb_cold 2509struct ev_loop * ecb_cold
2490ev_loop_new (unsigned int flags) 2510ev_loop_new (unsigned int flags) EV_THROW
2491{ 2511{
2492 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 2512 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
2493 2513
2494 memset (EV_A, 0, sizeof (struct ev_loop)); 2514 memset (EV_A, 0, sizeof (struct ev_loop));
2495 loop_init (EV_A_ flags); 2515 loop_init (EV_A_ flags);
2539} 2559}
2540#endif 2560#endif
2541 2561
2542#if EV_FEATURE_API 2562#if EV_FEATURE_API
2543void ecb_cold 2563void ecb_cold
2544ev_verify (EV_P) 2564ev_verify (EV_P) EV_THROW
2545{ 2565{
2546#if EV_VERIFY 2566#if EV_VERIFY
2547 int i; 2567 int i, j;
2548 WL w; 2568 WL w, w2;
2549 2569
2550 assert (activecnt >= -1); 2570 assert (activecnt >= -1);
2551 2571
2552 assert (fdchangemax >= fdchangecnt); 2572 assert (fdchangemax >= fdchangecnt);
2553 for (i = 0; i < fdchangecnt; ++i) 2573 for (i = 0; i < fdchangecnt; ++i)
2554 assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0)); 2574 assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0));
2555 2575
2556 assert (anfdmax >= 0); 2576 assert (anfdmax >= 0);
2557 for (i = 0; i < anfdmax; ++i) 2577 for (i = j = 0; i < anfdmax; ++i)
2558 for (w = anfds [i].head; w; w = w->next) 2578 for (w = w2 = anfds [i].head; w; w = w->next)
2559 { 2579 {
2560 verify_watcher (EV_A_ (W)w); 2580 verify_watcher (EV_A_ (W)w);
2581
2582 if (++j & 1)
2583 w2 = w2->next;
2584
2585 assert (("libev: io watcher list contains a loop", w != w2));
2561 assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1)); 2586 assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1));
2562 assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i)); 2587 assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i));
2563 } 2588 }
2564 2589
2565 assert (timermax >= timercnt); 2590 assert (timermax >= timercnt);
2618#if EV_MULTIPLICITY 2643#if EV_MULTIPLICITY
2619struct ev_loop * ecb_cold 2644struct ev_loop * ecb_cold
2620#else 2645#else
2621int 2646int
2622#endif 2647#endif
2623ev_default_loop (unsigned int flags) 2648ev_default_loop (unsigned int flags) EV_THROW
2624{ 2649{
2625 if (!ev_default_loop_ptr) 2650 if (!ev_default_loop_ptr)
2626 { 2651 {
2627#if EV_MULTIPLICITY 2652#if EV_MULTIPLICITY
2628 EV_P = ev_default_loop_ptr = &default_loop_struct; 2653 EV_P = ev_default_loop_ptr = &default_loop_struct;
2647 2672
2648 return ev_default_loop_ptr; 2673 return ev_default_loop_ptr;
2649} 2674}
2650 2675
2651void 2676void
2652ev_loop_fork (EV_P) 2677ev_loop_fork (EV_P) EV_THROW
2653{ 2678{
2654 postfork = 1; /* must be in line with ev_default_fork */ 2679 postfork = 1; /* must be in line with ev_default_fork */
2655} 2680}
2656 2681
2657/*****************************************************************************/ 2682/*****************************************************************************/
2661{ 2686{
2662 EV_CB_INVOKE ((W)w, revents); 2687 EV_CB_INVOKE ((W)w, revents);
2663} 2688}
2664 2689
2665unsigned int 2690unsigned int
2666ev_pending_count (EV_P) 2691ev_pending_count (EV_P) EV_THROW
2667{ 2692{
2668 int pri; 2693 int pri;
2669 unsigned int count = 0; 2694 unsigned int count = 0;
2670 2695
2671 for (pri = NUMPRI; pri--; ) 2696 for (pri = NUMPRI; pri--; )
2675} 2700}
2676 2701
2677void noinline 2702void noinline
2678ev_invoke_pending (EV_P) 2703ev_invoke_pending (EV_P)
2679{ 2704{
2680 int pri; 2705 for (pendingpri = NUMPRI; pendingpri--; ) /* pendingpri is modified during the loop */
2681
2682 for (pri = NUMPRI; pri--; )
2683 while (pendingcnt [pri]) 2706 while (pendingcnt [pendingpri])
2684 { 2707 {
2685 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 2708 ANPENDING *p = pendings [pendingpri] + --pendingcnt [pendingpri];
2686 2709
2687 p->w->pending = 0; 2710 p->w->pending = 0;
2688 EV_CB_INVOKE (p->w, p->events); 2711 EV_CB_INVOKE (p->w, p->events);
2689 EV_FREQUENT_CHECK; 2712 EV_FREQUENT_CHECK;
2690 } 2713 }
3102 3125
3103 return activecnt; 3126 return activecnt;
3104} 3127}
3105 3128
3106void 3129void
3107ev_break (EV_P_ int how) 3130ev_break (EV_P_ int how) EV_THROW
3108{ 3131{
3109 loop_done = how; 3132 loop_done = how;
3110} 3133}
3111 3134
3112void 3135void
3113ev_ref (EV_P) 3136ev_ref (EV_P) EV_THROW
3114{ 3137{
3115 ++activecnt; 3138 ++activecnt;
3116} 3139}
3117 3140
3118void 3141void
3119ev_unref (EV_P) 3142ev_unref (EV_P) EV_THROW
3120{ 3143{
3121 --activecnt; 3144 --activecnt;
3122} 3145}
3123 3146
3124void 3147void
3125ev_now_update (EV_P) 3148ev_now_update (EV_P) EV_THROW
3126{ 3149{
3127 time_update (EV_A_ 1e100); 3150 time_update (EV_A_ 1e100);
3128} 3151}
3129 3152
3130void 3153void
3131ev_suspend (EV_P) 3154ev_suspend (EV_P) EV_THROW
3132{ 3155{
3133 ev_now_update (EV_A); 3156 ev_now_update (EV_A);
3134} 3157}
3135 3158
3136void 3159void
3137ev_resume (EV_P) 3160ev_resume (EV_P) EV_THROW
3138{ 3161{
3139 ev_tstamp mn_prev = mn_now; 3162 ev_tstamp mn_prev = mn_now;
3140 3163
3141 ev_now_update (EV_A); 3164 ev_now_update (EV_A);
3142 timers_reschedule (EV_A_ mn_now - mn_prev); 3165 timers_reschedule (EV_A_ mn_now - mn_prev);
3181 w->pending = 0; 3204 w->pending = 0;
3182 } 3205 }
3183} 3206}
3184 3207
3185int 3208int
3186ev_clear_pending (EV_P_ void *w) 3209ev_clear_pending (EV_P_ void *w) EV_THROW
3187{ 3210{
3188 W w_ = (W)w; 3211 W w_ = (W)w;
3189 int pending = w_->pending; 3212 int pending = w_->pending;
3190 3213
3191 if (expect_true (pending)) 3214 if (expect_true (pending))
3224} 3247}
3225 3248
3226/*****************************************************************************/ 3249/*****************************************************************************/
3227 3250
3228void noinline 3251void noinline
3229ev_io_start (EV_P_ ev_io *w) 3252ev_io_start (EV_P_ ev_io *w) EV_THROW
3230{ 3253{
3231 int fd = w->fd; 3254 int fd = w->fd;
3232 3255
3233 if (expect_false (ev_is_active (w))) 3256 if (expect_false (ev_is_active (w)))
3234 return; 3257 return;
3240 3263
3241 ev_start (EV_A_ (W)w, 1); 3264 ev_start (EV_A_ (W)w, 1);
3242 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); 3265 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero);
3243 wlist_add (&anfds[fd].head, (WL)w); 3266 wlist_add (&anfds[fd].head, (WL)w);
3244 3267
3268 /* common bug, apparently */
3269 assert (("libev: ev_io_start called with corrupted watcher", ((WL)w)->next != (WL)w));
3270
3245 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY); 3271 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY);
3246 w->events &= ~EV__IOFDSET; 3272 w->events &= ~EV__IOFDSET;
3247 3273
3248 EV_FREQUENT_CHECK; 3274 EV_FREQUENT_CHECK;
3249} 3275}
3250 3276
3251void noinline 3277void noinline
3252ev_io_stop (EV_P_ ev_io *w) 3278ev_io_stop (EV_P_ ev_io *w) EV_THROW
3253{ 3279{
3254 clear_pending (EV_A_ (W)w); 3280 clear_pending (EV_A_ (W)w);
3255 if (expect_false (!ev_is_active (w))) 3281 if (expect_false (!ev_is_active (w)))
3256 return; 3282 return;
3257 3283
3266 3292
3267 EV_FREQUENT_CHECK; 3293 EV_FREQUENT_CHECK;
3268} 3294}
3269 3295
3270void noinline 3296void noinline
3271ev_timer_start (EV_P_ ev_timer *w) 3297ev_timer_start (EV_P_ ev_timer *w) EV_THROW
3272{ 3298{
3273 if (expect_false (ev_is_active (w))) 3299 if (expect_false (ev_is_active (w)))
3274 return; 3300 return;
3275 3301
3276 ev_at (w) += mn_now; 3302 ev_at (w) += mn_now;
3290 3316
3291 /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/ 3317 /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/
3292} 3318}
3293 3319
3294void noinline 3320void noinline
3295ev_timer_stop (EV_P_ ev_timer *w) 3321ev_timer_stop (EV_P_ ev_timer *w) EV_THROW
3296{ 3322{
3297 clear_pending (EV_A_ (W)w); 3323 clear_pending (EV_A_ (W)w);
3298 if (expect_false (!ev_is_active (w))) 3324 if (expect_false (!ev_is_active (w)))
3299 return; 3325 return;
3300 3326
3320 3346
3321 EV_FREQUENT_CHECK; 3347 EV_FREQUENT_CHECK;
3322} 3348}
3323 3349
3324void noinline 3350void noinline
3325ev_timer_again (EV_P_ ev_timer *w) 3351ev_timer_again (EV_P_ ev_timer *w) EV_THROW
3326{ 3352{
3327 EV_FREQUENT_CHECK; 3353 EV_FREQUENT_CHECK;
3328 3354
3329 clear_pending (EV_A_ (W)w); 3355 clear_pending (EV_A_ (W)w);
3330 3356
3347 3373
3348 EV_FREQUENT_CHECK; 3374 EV_FREQUENT_CHECK;
3349} 3375}
3350 3376
3351ev_tstamp 3377ev_tstamp
3352ev_timer_remaining (EV_P_ ev_timer *w) 3378ev_timer_remaining (EV_P_ ev_timer *w) EV_THROW
3353{ 3379{
3354 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); 3380 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.);
3355} 3381}
3356 3382
3357#if EV_PERIODIC_ENABLE 3383#if EV_PERIODIC_ENABLE
3358void noinline 3384void noinline
3359ev_periodic_start (EV_P_ ev_periodic *w) 3385ev_periodic_start (EV_P_ ev_periodic *w) EV_THROW
3360{ 3386{
3361 if (expect_false (ev_is_active (w))) 3387 if (expect_false (ev_is_active (w)))
3362 return; 3388 return;
3363 3389
3364 if (w->reschedule_cb) 3390 if (w->reschedule_cb)
3384 3410
3385 /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/ 3411 /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/
3386} 3412}
3387 3413
3388void noinline 3414void noinline
3389ev_periodic_stop (EV_P_ ev_periodic *w) 3415ev_periodic_stop (EV_P_ ev_periodic *w) EV_THROW
3390{ 3416{
3391 clear_pending (EV_A_ (W)w); 3417 clear_pending (EV_A_ (W)w);
3392 if (expect_false (!ev_is_active (w))) 3418 if (expect_false (!ev_is_active (w)))
3393 return; 3419 return;
3394 3420
3412 3438
3413 EV_FREQUENT_CHECK; 3439 EV_FREQUENT_CHECK;
3414} 3440}
3415 3441
3416void noinline 3442void noinline
3417ev_periodic_again (EV_P_ ev_periodic *w) 3443ev_periodic_again (EV_P_ ev_periodic *w) EV_THROW
3418{ 3444{
3419 /* TODO: use adjustheap and recalculation */ 3445 /* TODO: use adjustheap and recalculation */
3420 ev_periodic_stop (EV_A_ w); 3446 ev_periodic_stop (EV_A_ w);
3421 ev_periodic_start (EV_A_ w); 3447 ev_periodic_start (EV_A_ w);
3422} 3448}
3427#endif 3453#endif
3428 3454
3429#if EV_SIGNAL_ENABLE 3455#if EV_SIGNAL_ENABLE
3430 3456
3431void noinline 3457void noinline
3432ev_signal_start (EV_P_ ev_signal *w) 3458ev_signal_start (EV_P_ ev_signal *w) EV_THROW
3433{ 3459{
3434 if (expect_false (ev_is_active (w))) 3460 if (expect_false (ev_is_active (w)))
3435 return; 3461 return;
3436 3462
3437 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG)); 3463 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG));
3508 3534
3509 EV_FREQUENT_CHECK; 3535 EV_FREQUENT_CHECK;
3510} 3536}
3511 3537
3512void noinline 3538void noinline
3513ev_signal_stop (EV_P_ ev_signal *w) 3539ev_signal_stop (EV_P_ ev_signal *w) EV_THROW
3514{ 3540{
3515 clear_pending (EV_A_ (W)w); 3541 clear_pending (EV_A_ (W)w);
3516 if (expect_false (!ev_is_active (w))) 3542 if (expect_false (!ev_is_active (w)))
3517 return; 3543 return;
3518 3544
3549#endif 3575#endif
3550 3576
3551#if EV_CHILD_ENABLE 3577#if EV_CHILD_ENABLE
3552 3578
3553void 3579void
3554ev_child_start (EV_P_ ev_child *w) 3580ev_child_start (EV_P_ ev_child *w) EV_THROW
3555{ 3581{
3556#if EV_MULTIPLICITY 3582#if EV_MULTIPLICITY
3557 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 3583 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
3558#endif 3584#endif
3559 if (expect_false (ev_is_active (w))) 3585 if (expect_false (ev_is_active (w)))
3566 3592
3567 EV_FREQUENT_CHECK; 3593 EV_FREQUENT_CHECK;
3568} 3594}
3569 3595
3570void 3596void
3571ev_child_stop (EV_P_ ev_child *w) 3597ev_child_stop (EV_P_ ev_child *w) EV_THROW
3572{ 3598{
3573 clear_pending (EV_A_ (W)w); 3599 clear_pending (EV_A_ (W)w);
3574 if (expect_false (!ev_is_active (w))) 3600 if (expect_false (!ev_is_active (w)))
3575 return; 3601 return;
3576 3602
3828#else 3854#else
3829# define EV_LSTAT(p,b) lstat (p, b) 3855# define EV_LSTAT(p,b) lstat (p, b)
3830#endif 3856#endif
3831 3857
3832void 3858void
3833ev_stat_stat (EV_P_ ev_stat *w) 3859ev_stat_stat (EV_P_ ev_stat *w) EV_THROW
3834{ 3860{
3835 if (lstat (w->path, &w->attr) < 0) 3861 if (lstat (w->path, &w->attr) < 0)
3836 w->attr.st_nlink = 0; 3862 w->attr.st_nlink = 0;
3837 else if (!w->attr.st_nlink) 3863 else if (!w->attr.st_nlink)
3838 w->attr.st_nlink = 1; 3864 w->attr.st_nlink = 1;
3877 ev_feed_event (EV_A_ w, EV_STAT); 3903 ev_feed_event (EV_A_ w, EV_STAT);
3878 } 3904 }
3879} 3905}
3880 3906
3881void 3907void
3882ev_stat_start (EV_P_ ev_stat *w) 3908ev_stat_start (EV_P_ ev_stat *w) EV_THROW
3883{ 3909{
3884 if (expect_false (ev_is_active (w))) 3910 if (expect_false (ev_is_active (w)))
3885 return; 3911 return;
3886 3912
3887 ev_stat_stat (EV_A_ w); 3913 ev_stat_stat (EV_A_ w);
3908 3934
3909 EV_FREQUENT_CHECK; 3935 EV_FREQUENT_CHECK;
3910} 3936}
3911 3937
3912void 3938void
3913ev_stat_stop (EV_P_ ev_stat *w) 3939ev_stat_stop (EV_P_ ev_stat *w) EV_THROW
3914{ 3940{
3915 clear_pending (EV_A_ (W)w); 3941 clear_pending (EV_A_ (W)w);
3916 if (expect_false (!ev_is_active (w))) 3942 if (expect_false (!ev_is_active (w)))
3917 return; 3943 return;
3918 3944
3934} 3960}
3935#endif 3961#endif
3936 3962
3937#if EV_IDLE_ENABLE 3963#if EV_IDLE_ENABLE
3938void 3964void
3939ev_idle_start (EV_P_ ev_idle *w) 3965ev_idle_start (EV_P_ ev_idle *w) EV_THROW
3940{ 3966{
3941 if (expect_false (ev_is_active (w))) 3967 if (expect_false (ev_is_active (w)))
3942 return; 3968 return;
3943 3969
3944 pri_adjust (EV_A_ (W)w); 3970 pri_adjust (EV_A_ (W)w);
3957 3983
3958 EV_FREQUENT_CHECK; 3984 EV_FREQUENT_CHECK;
3959} 3985}
3960 3986
3961void 3987void
3962ev_idle_stop (EV_P_ ev_idle *w) 3988ev_idle_stop (EV_P_ ev_idle *w) EV_THROW
3963{ 3989{
3964 clear_pending (EV_A_ (W)w); 3990 clear_pending (EV_A_ (W)w);
3965 if (expect_false (!ev_is_active (w))) 3991 if (expect_false (!ev_is_active (w)))
3966 return; 3992 return;
3967 3993
3981} 4007}
3982#endif 4008#endif
3983 4009
3984#if EV_PREPARE_ENABLE 4010#if EV_PREPARE_ENABLE
3985void 4011void
3986ev_prepare_start (EV_P_ ev_prepare *w) 4012ev_prepare_start (EV_P_ ev_prepare *w) EV_THROW
3987{ 4013{
3988 if (expect_false (ev_is_active (w))) 4014 if (expect_false (ev_is_active (w)))
3989 return; 4015 return;
3990 4016
3991 EV_FREQUENT_CHECK; 4017 EV_FREQUENT_CHECK;
3996 4022
3997 EV_FREQUENT_CHECK; 4023 EV_FREQUENT_CHECK;
3998} 4024}
3999 4025
4000void 4026void
4001ev_prepare_stop (EV_P_ ev_prepare *w) 4027ev_prepare_stop (EV_P_ ev_prepare *w) EV_THROW
4002{ 4028{
4003 clear_pending (EV_A_ (W)w); 4029 clear_pending (EV_A_ (W)w);
4004 if (expect_false (!ev_is_active (w))) 4030 if (expect_false (!ev_is_active (w)))
4005 return; 4031 return;
4006 4032
4019} 4045}
4020#endif 4046#endif
4021 4047
4022#if EV_CHECK_ENABLE 4048#if EV_CHECK_ENABLE
4023void 4049void
4024ev_check_start (EV_P_ ev_check *w) 4050ev_check_start (EV_P_ ev_check *w) EV_THROW
4025{ 4051{
4026 if (expect_false (ev_is_active (w))) 4052 if (expect_false (ev_is_active (w)))
4027 return; 4053 return;
4028 4054
4029 EV_FREQUENT_CHECK; 4055 EV_FREQUENT_CHECK;
4034 4060
4035 EV_FREQUENT_CHECK; 4061 EV_FREQUENT_CHECK;
4036} 4062}
4037 4063
4038void 4064void
4039ev_check_stop (EV_P_ ev_check *w) 4065ev_check_stop (EV_P_ ev_check *w) EV_THROW
4040{ 4066{
4041 clear_pending (EV_A_ (W)w); 4067 clear_pending (EV_A_ (W)w);
4042 if (expect_false (!ev_is_active (w))) 4068 if (expect_false (!ev_is_active (w)))
4043 return; 4069 return;
4044 4070
4057} 4083}
4058#endif 4084#endif
4059 4085
4060#if EV_EMBED_ENABLE 4086#if EV_EMBED_ENABLE
4061void noinline 4087void noinline
4062ev_embed_sweep (EV_P_ ev_embed *w) 4088ev_embed_sweep (EV_P_ ev_embed *w) EV_THROW
4063{ 4089{
4064 ev_run (w->other, EVRUN_NOWAIT); 4090 ev_run (w->other, EVRUN_NOWAIT);
4065} 4091}
4066 4092
4067static void 4093static void
4115 ev_idle_stop (EV_A_ idle); 4141 ev_idle_stop (EV_A_ idle);
4116} 4142}
4117#endif 4143#endif
4118 4144
4119void 4145void
4120ev_embed_start (EV_P_ ev_embed *w) 4146ev_embed_start (EV_P_ ev_embed *w) EV_THROW
4121{ 4147{
4122 if (expect_false (ev_is_active (w))) 4148 if (expect_false (ev_is_active (w)))
4123 return; 4149 return;
4124 4150
4125 { 4151 {
4146 4172
4147 EV_FREQUENT_CHECK; 4173 EV_FREQUENT_CHECK;
4148} 4174}
4149 4175
4150void 4176void
4151ev_embed_stop (EV_P_ ev_embed *w) 4177ev_embed_stop (EV_P_ ev_embed *w) EV_THROW
4152{ 4178{
4153 clear_pending (EV_A_ (W)w); 4179 clear_pending (EV_A_ (W)w);
4154 if (expect_false (!ev_is_active (w))) 4180 if (expect_false (!ev_is_active (w)))
4155 return; 4181 return;
4156 4182
4166} 4192}
4167#endif 4193#endif
4168 4194
4169#if EV_FORK_ENABLE 4195#if EV_FORK_ENABLE
4170void 4196void
4171ev_fork_start (EV_P_ ev_fork *w) 4197ev_fork_start (EV_P_ ev_fork *w) EV_THROW
4172{ 4198{
4173 if (expect_false (ev_is_active (w))) 4199 if (expect_false (ev_is_active (w)))
4174 return; 4200 return;
4175 4201
4176 EV_FREQUENT_CHECK; 4202 EV_FREQUENT_CHECK;
4181 4207
4182 EV_FREQUENT_CHECK; 4208 EV_FREQUENT_CHECK;
4183} 4209}
4184 4210
4185void 4211void
4186ev_fork_stop (EV_P_ ev_fork *w) 4212ev_fork_stop (EV_P_ ev_fork *w) EV_THROW
4187{ 4213{
4188 clear_pending (EV_A_ (W)w); 4214 clear_pending (EV_A_ (W)w);
4189 if (expect_false (!ev_is_active (w))) 4215 if (expect_false (!ev_is_active (w)))
4190 return; 4216 return;
4191 4217
4204} 4230}
4205#endif 4231#endif
4206 4232
4207#if EV_CLEANUP_ENABLE 4233#if EV_CLEANUP_ENABLE
4208void 4234void
4209ev_cleanup_start (EV_P_ ev_cleanup *w) 4235ev_cleanup_start (EV_P_ ev_cleanup *w) EV_THROW
4210{ 4236{
4211 if (expect_false (ev_is_active (w))) 4237 if (expect_false (ev_is_active (w)))
4212 return; 4238 return;
4213 4239
4214 EV_FREQUENT_CHECK; 4240 EV_FREQUENT_CHECK;
4221 ev_unref (EV_A); 4247 ev_unref (EV_A);
4222 EV_FREQUENT_CHECK; 4248 EV_FREQUENT_CHECK;
4223} 4249}
4224 4250
4225void 4251void
4226ev_cleanup_stop (EV_P_ ev_cleanup *w) 4252ev_cleanup_stop (EV_P_ ev_cleanup *w) EV_THROW
4227{ 4253{
4228 clear_pending (EV_A_ (W)w); 4254 clear_pending (EV_A_ (W)w);
4229 if (expect_false (!ev_is_active (w))) 4255 if (expect_false (!ev_is_active (w)))
4230 return; 4256 return;
4231 4257
4245} 4271}
4246#endif 4272#endif
4247 4273
4248#if EV_ASYNC_ENABLE 4274#if EV_ASYNC_ENABLE
4249void 4275void
4250ev_async_start (EV_P_ ev_async *w) 4276ev_async_start (EV_P_ ev_async *w) EV_THROW
4251{ 4277{
4252 if (expect_false (ev_is_active (w))) 4278 if (expect_false (ev_is_active (w)))
4253 return; 4279 return;
4254 4280
4255 w->sent = 0; 4281 w->sent = 0;
4264 4290
4265 EV_FREQUENT_CHECK; 4291 EV_FREQUENT_CHECK;
4266} 4292}
4267 4293
4268void 4294void
4269ev_async_stop (EV_P_ ev_async *w) 4295ev_async_stop (EV_P_ ev_async *w) EV_THROW
4270{ 4296{
4271 clear_pending (EV_A_ (W)w); 4297 clear_pending (EV_A_ (W)w);
4272 if (expect_false (!ev_is_active (w))) 4298 if (expect_false (!ev_is_active (w)))
4273 return; 4299 return;
4274 4300
4285 4311
4286 EV_FREQUENT_CHECK; 4312 EV_FREQUENT_CHECK;
4287} 4313}
4288 4314
4289void 4315void
4290ev_async_send (EV_P_ ev_async *w) 4316ev_async_send (EV_P_ ev_async *w) EV_THROW
4291{ 4317{
4292 w->sent = 1; 4318 w->sent = 1;
4293 evpipe_write (EV_A_ &async_pending); 4319 evpipe_write (EV_A_ &async_pending);
4294} 4320}
4295#endif 4321#endif
4332 4358
4333 once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io)); 4359 once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io));
4334} 4360}
4335 4361
4336void 4362void
4337ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 4363ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) EV_THROW
4338{ 4364{
4339 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); 4365 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
4340 4366
4341 if (expect_false (!once)) 4367 if (expect_false (!once))
4342 { 4368 {
4364 4390
4365/*****************************************************************************/ 4391/*****************************************************************************/
4366 4392
4367#if EV_WALK_ENABLE 4393#if EV_WALK_ENABLE
4368void ecb_cold 4394void ecb_cold
4369ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) 4395ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) EV_THROW
4370{ 4396{
4371 int i, j; 4397 int i, j;
4372 ev_watcher_list *wl, *wn; 4398 ev_watcher_list *wl, *wn;
4373 4399
4374 if (types & (EV_IO | EV_EMBED)) 4400 if (types & (EV_IO | EV_EMBED))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines