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

Comparing libev/ev.c (file contents):
Revision 1.424 by root, Tue May 1 22:01:40 2012 UTC vs.
Revision 1.436 by root, Tue May 29 20:44:39 2012 UTC

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
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
1110static void (*syserr_cb)(const char *msg) EV_THROW; 1107static void (*syserr_cb)(const char *msg) EV_THROW;
1111 1108
1112void ecb_cold 1109void ecb_cold
1113ev_set_syserr_cb (void (*cb)(const char *msg)) EV_THROW 1110ev_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
1118static void noinline ecb_cold 1115static void noinline ecb_cold
1136 abort (); 1133 abort ();
1137 } 1134 }
1138} 1135}
1139 1136
1140static void * 1137static void *
1141ev_realloc_emul (void *ptr, long size) 1138ev_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
1159static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul; 1156static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul;
1160 1157
1161void ecb_cold 1158void ecb_cold
1162ev_set_allocator (void *(*cb)(void *ptr, long size)) EV_THROW 1159ev_set_allocator (void *(*cb)(void *ptr, long size) EV_THROW) EV_THROW
1163{ 1160{
1164 alloc = cb; 1161 alloc = cb;
1165} 1162}
1166 1163
1167inline_speed void * 1164inline_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
1437inline_speed void 1436inline_speed void
1438feed_reverse (EV_P_ W w) 1437feed_reverse (EV_P_ W w)
1439{ 1438{
1850 1849
1851 if (expect_true (*flag)) 1850 if (expect_true (*flag))
1852 return; 1851 return;
1853 1852
1854 *flag = 1; 1853 *flag = 1;
1855
1856 ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */ 1854 ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */
1857 1855
1858 pipe_write_skipped = 1; 1856 pipe_write_skipped = 1;
1859 1857
1860 ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */ 1858 ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */
1861 1859
1862 if (pipe_write_wanted) 1860 if (pipe_write_wanted)
1863 { 1861 {
1864 int old_errno; 1862 int old_errno;
1865 1863
1866 pipe_write_skipped = 0; /* just an optimisation, no fence needed */ 1864 pipe_write_skipped = 0;
1865 ECB_MEMORY_FENCE_RELEASE;
1867 1866
1868 old_errno = errno; /* save errno because write will clobber it */ 1867 old_errno = errno; /* save errno because write will clobber it */
1869 1868
1870#if EV_USE_EVENTFD 1869#if EV_USE_EVENTFD
1871 if (evfd >= 0) 1870 if (evfd >= 0)
1874 write (evfd, &counter, sizeof (uint64_t)); 1873 write (evfd, &counter, sizeof (uint64_t));
1875 } 1874 }
1876 else 1875 else
1877#endif 1876#endif
1878 { 1877 {
1879 /* win32 people keep sending patches that change this write() to send() */ 1878#ifdef _WIN32
1880 /* and then run away. but send() is wrong, it wants a socket handle on win32 */ 1879 WSABUF buf;
1881 /* so when you think this write should be a send instead, please find out */ 1880 DWORD sent;
1882 /* where your send() is from - it's definitely not the microsoft send, and */ 1881 buf.buf = &buf;
1883 /* tell me. thank you. */ 1882 buf.len = 1;
1884 /* 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);
1885 /* check the ev documentation on how to use this flag */ 1884#else
1886 write (evpipe [1], &(evpipe [1]), 1); 1885 write (evpipe [1], &(evpipe [1]), 1);
1886#endif
1887 } 1887 }
1888 1888
1889 errno = old_errno; 1889 errno = old_errno;
1890 } 1890 }
1891} 1891}
1906 read (evfd, &counter, sizeof (uint64_t)); 1906 read (evfd, &counter, sizeof (uint64_t));
1907 } 1907 }
1908 else 1908 else
1909#endif 1909#endif
1910 { 1910 {
1911 char dummy; 1911 char dummy[4];
1912 /* 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
1913 read (evpipe [0], &dummy, 1); 1920 read (evpipe [0], &dummy, sizeof (dummy));
1921#endif
1914 } 1922 }
1915 } 1923 }
1916 1924
1917 pipe_write_skipped = 0; 1925 pipe_write_skipped = 0;
1918 1926
1921#if EV_SIGNAL_ENABLE 1929#if EV_SIGNAL_ENABLE
1922 if (sig_pending) 1930 if (sig_pending)
1923 { 1931 {
1924 sig_pending = 0; 1932 sig_pending = 0;
1925 1933
1926 ECB_MEMORY_FENCE_RELEASE; 1934 ECB_MEMORY_FENCE;
1927 1935
1928 for (i = EV_NSIG - 1; i--; ) 1936 for (i = EV_NSIG - 1; i--; )
1929 if (expect_false (signals [i].pending)) 1937 if (expect_false (signals [i].pending))
1930 ev_feed_signal_event (EV_A_ i + 1); 1938 ev_feed_signal_event (EV_A_ i + 1);
1931 } 1939 }
1934#if EV_ASYNC_ENABLE 1942#if EV_ASYNC_ENABLE
1935 if (async_pending) 1943 if (async_pending)
1936 { 1944 {
1937 async_pending = 0; 1945 async_pending = 0;
1938 1946
1939 ECB_MEMORY_FENCE_RELEASE; 1947 ECB_MEMORY_FENCE;
1940 1948
1941 for (i = asynccnt; i--; ) 1949 for (i = asynccnt; i--; )
1942 if (asyncs [i]->sent) 1950 if (asyncs [i]->sent)
1943 { 1951 {
1944 asyncs [i]->sent = 0; 1952 asyncs [i]->sent = 0;
1953 ECB_MEMORY_FENCE_RELEASE;
1945 ev_feed_event (EV_A_ asyncs [i], EV_ASYNC); 1954 ev_feed_event (EV_A_ asyncs [i], EV_ASYNC);
1946 } 1955 }
1947 } 1956 }
1948#endif 1957#endif
1949} 1958}
1994 if (expect_false (signals [signum].loop != EV_A)) 2003 if (expect_false (signals [signum].loop != EV_A))
1995 return; 2004 return;
1996#endif 2005#endif
1997 2006
1998 signals [signum].pending = 0; 2007 signals [signum].pending = 0;
2008 MEMORY_FENCE_RELEASE;
1999 2009
2000 for (w = signals [signum].head; w; w = w->next) 2010 for (w = signals [signum].head; w; w = w->next)
2001 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 2011 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
2002} 2012}
2003 2013
2347 EV_INVOKE_PENDING; 2357 EV_INVOKE_PENDING;
2348 } 2358 }
2349#endif 2359#endif
2350 2360
2351#if EV_CHILD_ENABLE 2361#if EV_CHILD_ENABLE
2352 if (ev_is_active (&childev)) 2362 if (ev_is_default_loop (EV_A) && ev_is_active (&childev))
2353 { 2363 {
2354 ev_ref (EV_A); /* child watcher */ 2364 ev_ref (EV_A); /* child watcher */
2355 ev_signal_stop (EV_A_ &childev); 2365 ev_signal_stop (EV_A_ &childev);
2356 } 2366 }
2357#endif 2367#endif
2553void ecb_cold 2563void ecb_cold
2554ev_verify (EV_P) EV_THROW 2564ev_verify (EV_P) EV_THROW
2555{ 2565{
2556#if EV_VERIFY 2566#if EV_VERIFY
2557 int i; 2567 int i;
2558 WL w; 2568 WL w, w2;
2559 2569
2560 assert (activecnt >= -1); 2570 assert (activecnt >= -1);
2561 2571
2562 assert (fdchangemax >= fdchangecnt); 2572 assert (fdchangemax >= fdchangecnt);
2563 for (i = 0; i < fdchangecnt; ++i) 2573 for (i = 0; i < fdchangecnt; ++i)
2564 assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0)); 2574 assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0));
2565 2575
2566 assert (anfdmax >= 0); 2576 assert (anfdmax >= 0);
2567 for (i = 0; i < anfdmax; ++i) 2577 for (i = 0; i < anfdmax; ++i)
2578 {
2579 int j = 0;
2580
2568 for (w = anfds [i].head; w; w = w->next) 2581 for (w = w2 = anfds [i].head; w; w = w->next)
2569 { 2582 {
2570 verify_watcher (EV_A_ (W)w); 2583 verify_watcher (EV_A_ (W)w);
2584
2585 if (j++ & 1)
2586 {
2587 assert (("libev: io watcher list contains a loop", w != w2));
2588 w2 = w2->next;
2589 }
2590
2571 assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1)); 2591 assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1));
2572 assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i)); 2592 assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i));
2573 } 2593 }
2594 }
2574 2595
2575 assert (timermax >= timercnt); 2596 assert (timermax >= timercnt);
2576 verify_heap (EV_A_ timers, timercnt); 2597 verify_heap (EV_A_ timers, timercnt);
2577 2598
2578#if EV_PERIODIC_ENABLE 2599#if EV_PERIODIC_ENABLE
2685} 2706}
2686 2707
2687void noinline 2708void noinline
2688ev_invoke_pending (EV_P) 2709ev_invoke_pending (EV_P)
2689{ 2710{
2690 int pri; 2711 for (pendingpri = NUMPRI; pendingpri--; ) /* pendingpri is modified during the loop */
2691
2692 for (pri = NUMPRI; pri--; )
2693 while (pendingcnt [pri]) 2712 while (pendingcnt [pendingpri])
2694 { 2713 {
2695 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 2714 ANPENDING *p = pendings [pendingpri] + --pendingcnt [pendingpri];
2696 2715
2697 p->w->pending = 0; 2716 p->w->pending = 0;
2698 EV_CB_INVOKE (p->w, p->events); 2717 EV_CB_INVOKE (p->w, p->events);
2699 EV_FREQUENT_CHECK; 2718 EV_FREQUENT_CHECK;
2700 } 2719 }
2795{ 2814{
2796 EV_FREQUENT_CHECK; 2815 EV_FREQUENT_CHECK;
2797 2816
2798 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) 2817 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now)
2799 { 2818 {
2800 int feed_count = 0;
2801
2802 do 2819 do
2803 { 2820 {
2804 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); 2821 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
2805 2822
2806 /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/ 2823 /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/
3249 EV_FREQUENT_CHECK; 3266 EV_FREQUENT_CHECK;
3250 3267
3251 ev_start (EV_A_ (W)w, 1); 3268 ev_start (EV_A_ (W)w, 1);
3252 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); 3269 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero);
3253 wlist_add (&anfds[fd].head, (WL)w); 3270 wlist_add (&anfds[fd].head, (WL)w);
3271
3272 /* common bug, apparently */
3273 assert (("libev: ev_io_start called with corrupted watcher", ((WL)w)->next != (WL)w));
3254 3274
3255 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY); 3275 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY);
3256 w->events &= ~EV__IOFDSET; 3276 w->events &= ~EV__IOFDSET;
3257 3277
3258 EV_FREQUENT_CHECK; 3278 EV_FREQUENT_CHECK;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines