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

Comparing libev/ev.c (file contents):
Revision 1.306 by root, Sun Jul 19 06:35:25 2009 UTC vs.
Revision 1.314 by root, Wed Aug 26 17:31:20 2009 UTC

187#endif 187#endif
188 188
189/* this block tries to deduce configuration from header-defined symbols and defaults */ 189/* this block tries to deduce configuration from header-defined symbols and defaults */
190 190
191/* try to deduce the maximum number of signals on this platform */ 191/* try to deduce the maximum number of signals on this platform */
192/* one some platforms, NSIG is one too large. we do not bother */
193#if defined (EV_NSIG) 192#if defined (EV_NSIG)
194/* use what's provided */ 193/* use what's provided */
195#elif defined (NSIG) 194#elif defined (NSIG)
196# define EV_NSIG (NSIG) 195# define EV_NSIG (NSIG)
197#elif defined(_NSIG) 196#elif defined(_NSIG)
303# define EV_USE_EVENTFD 0 302# define EV_USE_EVENTFD 0
304# endif 303# endif
305#endif 304#endif
306 305
307#ifndef EV_USE_SIGNALFD 306#ifndef EV_USE_SIGNALFD
308# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9)) 307# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
309# define EV_USE_SIGNALFD 1 308# define EV_USE_SIGNALFD 1
310# else 309# else
311# define EV_USE_SIGNALFD 0 310# define EV_USE_SIGNALFD 0
312# endif 311# endif
313#endif 312#endif
387# include <stdint.h> 386# include <stdint.h>
388# ifndef EFD_NONBLOCK 387# ifndef EFD_NONBLOCK
389# define EFD_NONBLOCK O_NONBLOCK 388# define EFD_NONBLOCK O_NONBLOCK
390# endif 389# endif
391# ifndef EFD_CLOEXEC 390# ifndef EFD_CLOEXEC
391# ifdef O_CLOEXEC
392# define EFD_CLOEXEC O_CLOEXEC 392# define EFD_CLOEXEC O_CLOEXEC
393# else
394# define EFD_CLOEXEC 02000000
395# endif
393# endif 396# endif
394# ifdef __cplusplus 397# ifdef __cplusplus
395extern "C" { 398extern "C" {
396# endif 399# endif
397int eventfd (unsigned int initval, int flags); 400int eventfd (unsigned int initval, int flags);
399} 402}
400# endif 403# endif
401#endif 404#endif
402 405
403#if EV_USE_SIGNALFD 406#if EV_USE_SIGNALFD
404# include <sys/signalfd.h> 407/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
408# include <stdint.h>
409# ifndef SFD_NONBLOCK
410# define SFD_NONBLOCK O_NONBLOCK
405#endif 411# endif
412# ifndef SFD_CLOEXEC
413# ifdef O_CLOEXEC
414# define SFD_CLOEXEC O_CLOEXEC
415# else
416# define SFD_CLOEXEC 02000000
417# endif
418# endif
419# ifdef __cplusplus
420extern "C" {
421# endif
422int signalfd (int fd, const sigset_t *mask, int flags);
423
424struct signalfd_siginfo
425{
426 uint32_t ssi_signo;
427 char pad[128 - sizeof (uint32_t)];
428};
429# ifdef __cplusplus
430}
431# endif
432#endif
433
406 434
407/**/ 435/**/
408 436
409#if EV_VERIFY >= 3 437#if EV_VERIFY >= 3
410# define EV_FREQUENT_CHECK ev_loop_verify (EV_A) 438# define EV_FREQUENT_CHECK ev_loop_verify (EV_A)
473 501
474#if EV_USE_MONOTONIC 502#if EV_USE_MONOTONIC
475static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 503static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
476#endif 504#endif
477 505
506#ifndef EV_FD_TO_WIN32_HANDLE
507# define EV_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd)
508#endif
509#ifndef EV_WIN32_HANDLE_TO_FD
510# define EV_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (fd, 0)
511#endif
512#ifndef EV_WIN32_CLOSE_FD
513# define EV_WIN32_CLOSE_FD(fd) close (fd)
514#endif
515
478#ifdef _WIN32 516#ifdef _WIN32
479# include "ev_win32.c" 517# include "ev_win32.c"
480#endif 518#endif
481 519
482/*****************************************************************************/ 520/*****************************************************************************/
873 911
874#if EV_SELECT_IS_WINSOCKET 912#if EV_SELECT_IS_WINSOCKET
875 if (events) 913 if (events)
876 { 914 {
877 unsigned long arg; 915 unsigned long arg;
878 #ifdef EV_FD_TO_WIN32_HANDLE
879 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); 916 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
880 #else
881 anfd->handle = _get_osfhandle (fd);
882 #endif
883 assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0)); 917 assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0));
884 } 918 }
885#endif 919#endif
886 920
887 { 921 {
958 992
959 for (fd = anfdmax; fd--; ) 993 for (fd = anfdmax; fd--; )
960 if (anfds [fd].events) 994 if (anfds [fd].events)
961 { 995 {
962 fd_kill (EV_A_ fd); 996 fd_kill (EV_A_ fd);
963 return; 997 break;
964 } 998 }
965} 999}
966 1000
967/* usually called after fork if backend needs to re-arm all fds from scratch */ 1001/* usually called after fork if backend needs to re-arm all fds from scratch */
968static void noinline 1002static void noinline
1058 1092
1059 for (;;) 1093 for (;;)
1060 { 1094 {
1061 int c = k << 1; 1095 int c = k << 1;
1062 1096
1063 if (c > N + HEAP0 - 1) 1097 if (c >= N + HEAP0)
1064 break; 1098 break;
1065 1099
1066 c += c + 1 < N + HEAP0 && ANHE_at (heap [c]) > ANHE_at (heap [c + 1]) 1100 c += c + 1 < N + HEAP0 && ANHE_at (heap [c]) > ANHE_at (heap [c + 1])
1067 ? 1 : 0; 1101 ? 1 : 0;
1068 1102
1104 1138
1105/* move an element suitably so it is in a correct place */ 1139/* move an element suitably so it is in a correct place */
1106inline_size void 1140inline_size void
1107adjustheap (ANHE *heap, int N, int k) 1141adjustheap (ANHE *heap, int N, int k)
1108{ 1142{
1109 if (k > HEAP0 && ANHE_at (heap [HPARENT (k)]) >= ANHE_at (heap [k])) 1143 if (k > HEAP0 && ANHE_at (heap [k]) <= ANHE_at (heap [HPARENT (k)]))
1110 upheap (heap, k); 1144 upheap (heap, k);
1111 else 1145 else
1112 downheap (heap, N, k); 1146 downheap (heap, N, k);
1113} 1147}
1114 1148
1127/*****************************************************************************/ 1161/*****************************************************************************/
1128 1162
1129/* associate signal watchers to a signal signal */ 1163/* associate signal watchers to a signal signal */
1130typedef struct 1164typedef struct
1131{ 1165{
1166 EV_ATOMIC_T pending;
1132#if EV_MULTIPLICITY 1167#if EV_MULTIPLICITY
1133 EV_P; 1168 EV_P;
1134#endif 1169#endif
1135 WL head; 1170 WL head;
1136 EV_ATOMIC_T gotsig;
1137} ANSIG; 1171} ANSIG;
1138 1172
1139static ANSIG signals [EV_NSIG - 1]; 1173static ANSIG signals [EV_NSIG - 1];
1140static EV_ATOMIC_T gotsig;
1141 1174
1142/*****************************************************************************/ 1175/*****************************************************************************/
1143 1176
1144/* used to prepare libev internal fd's */ 1177/* used to prepare libev internal fd's */
1145/* this is not fork-safe */ 1178/* this is not fork-safe */
1213/* called whenever the libev signal pipe */ 1246/* called whenever the libev signal pipe */
1214/* got some events (signal, async) */ 1247/* got some events (signal, async) */
1215static void 1248static void
1216pipecb (EV_P_ ev_io *iow, int revents) 1249pipecb (EV_P_ ev_io *iow, int revents)
1217{ 1250{
1251 int i;
1252
1218#if EV_USE_EVENTFD 1253#if EV_USE_EVENTFD
1219 if (evfd >= 0) 1254 if (evfd >= 0)
1220 { 1255 {
1221 uint64_t counter; 1256 uint64_t counter;
1222 read (evfd, &counter, sizeof (uint64_t)); 1257 read (evfd, &counter, sizeof (uint64_t));
1226 { 1261 {
1227 char dummy; 1262 char dummy;
1228 read (evpipe [0], &dummy, 1); 1263 read (evpipe [0], &dummy, 1);
1229 } 1264 }
1230 1265
1231 if (gotsig && ev_is_default_loop (EV_A)) 1266 if (sig_pending)
1232 { 1267 {
1233 int signum; 1268 sig_pending = 0;
1234 gotsig = 0;
1235 1269
1236 for (signum = EV_NSIG - 1; signum--; ) 1270 for (i = EV_NSIG - 1; i--; )
1237 if (signals [signum].gotsig) 1271 if (expect_false (signals [i].pending))
1238 ev_feed_signal_event (EV_A_ signum + 1); 1272 ev_feed_signal_event (EV_A_ i + 1);
1239 } 1273 }
1240 1274
1241#if EV_ASYNC_ENABLE 1275#if EV_ASYNC_ENABLE
1242 if (gotasync) 1276 if (async_pending)
1243 { 1277 {
1244 int i; 1278 async_pending = 0;
1245 gotasync = 0;
1246 1279
1247 for (i = asynccnt; i--; ) 1280 for (i = asynccnt; i--; )
1248 if (asyncs [i]->sent) 1281 if (asyncs [i]->sent)
1249 { 1282 {
1250 asyncs [i]->sent = 0; 1283 asyncs [i]->sent = 0;
1265 1298
1266#if _WIN32 1299#if _WIN32
1267 signal (signum, ev_sighandler); 1300 signal (signum, ev_sighandler);
1268#endif 1301#endif
1269 1302
1270 signals [signum - 1].gotsig = 1; 1303 signals [signum - 1].pending = 1;
1271 evpipe_write (EV_A_ &gotsig); 1304 evpipe_write (EV_A_ &sig_pending);
1272} 1305}
1273 1306
1274void noinline 1307void noinline
1275ev_feed_signal_event (EV_P_ int signum) 1308ev_feed_signal_event (EV_P_ int signum)
1276{ 1309{
1277 WL w; 1310 WL w;
1278 1311
1312 if (expect_false (signum <= 0 || signum > EV_NSIG))
1313 return;
1314
1315 --signum;
1316
1279#if EV_MULTIPLICITY 1317#if EV_MULTIPLICITY
1280 assert (("libev: feeding signal events is only supported in the default loop", loop == ev_default_loop_ptr)); 1318 /* it is permissible to try to feed a signal to the wrong loop */
1281#endif 1319 /* or, likely more useful, feeding a signal nobody is waiting for */
1282 1320
1283 if (signum <= 0 || signum > EV_NSIG) 1321 if (expect_false (signals [signum].loop != EV_A))
1284 return; 1322 return;
1323#endif
1285 1324
1286 --signum;
1287
1288 signals [signum].gotsig = 0; 1325 signals [signum].pending = 0;
1289 1326
1290 for (w = signals [signum].head; w; w = w->next) 1327 for (w = signals [signum].head; w; w = w->next)
1291 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 1328 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
1292} 1329}
1293 1330
1560 1597
1561 io_blocktime = 0.; 1598 io_blocktime = 0.;
1562 timeout_blocktime = 0.; 1599 timeout_blocktime = 0.;
1563 backend = 0; 1600 backend = 0;
1564 backend_fd = -1; 1601 backend_fd = -1;
1565 gotasync = 0; 1602 sig_pending = 0;
1603#if EV_ASYNC_ENABLE
1604 async_pending = 0;
1605#endif
1566#if EV_USE_INOTIFY 1606#if EV_USE_INOTIFY
1567 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; 1607 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
1568#endif 1608#endif
1569#if EV_USE_SIGNALFD 1609#if EV_USE_SIGNALFD
1570 sigfd = flags & EVFLAG_NOSIGFD ? -1 : -2; 1610 sigfd = flags & EVFLAG_NOSIGFD ? -1 : -2;
1612 close (evfd); 1652 close (evfd);
1613#endif 1653#endif
1614 1654
1615 if (evpipe [0] >= 0) 1655 if (evpipe [0] >= 0)
1616 { 1656 {
1617 close (evpipe [0]); 1657 EV_WIN32_CLOSE_FD (evpipe [0]);
1618 close (evpipe [1]); 1658 EV_WIN32_CLOSE_FD (evpipe [1]);
1619 } 1659 }
1620 } 1660 }
1621 1661
1622#if EV_USE_SIGNALFD 1662#if EV_USE_SIGNALFD
1623 if (ev_is_active (&sigfd_w)) 1663 if (ev_is_active (&sigfd_w))
1704 1744
1705 if (ev_is_active (&pipe_w)) 1745 if (ev_is_active (&pipe_w))
1706 { 1746 {
1707 /* this "locks" the handlers against writing to the pipe */ 1747 /* this "locks" the handlers against writing to the pipe */
1708 /* while we modify the fd vars */ 1748 /* while we modify the fd vars */
1709 gotsig = 1; 1749 sig_pending = 1;
1710#if EV_ASYNC_ENABLE 1750#if EV_ASYNC_ENABLE
1711 gotasync = 1; 1751 async_pending = 1;
1712#endif 1752#endif
1713 1753
1714 ev_ref (EV_A); 1754 ev_ref (EV_A);
1715 ev_io_stop (EV_A_ &pipe_w); 1755 ev_io_stop (EV_A_ &pipe_w);
1716 1756
1719 close (evfd); 1759 close (evfd);
1720#endif 1760#endif
1721 1761
1722 if (evpipe [0] >= 0) 1762 if (evpipe [0] >= 0)
1723 { 1763 {
1724 close (evpipe [0]); 1764 EV_WIN32_CLOSE_FD (evpipe [0]);
1725 close (evpipe [1]); 1765 EV_WIN32_CLOSE_FD (evpipe [1]);
1726 } 1766 }
1727 1767
1728 evpipe_init (EV_A); 1768 evpipe_init (EV_A);
1729 /* now iterate over everything, in case we missed something */ 1769 /* now iterate over everything, in case we missed something */
1730 pipecb (EV_A_ &pipe_w, EV_READ); 1770 pipecb (EV_A_ &pipe_w, EV_READ);
1856 assert (checkmax >= checkcnt); 1896 assert (checkmax >= checkcnt);
1857 array_verify (EV_A_ (W *)checks, checkcnt); 1897 array_verify (EV_A_ (W *)checks, checkcnt);
1858 1898
1859# if 0 1899# if 0
1860 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 1900 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
1861 for (signum = EV_NSIG; signum--; ) if (signals [signum].gotsig) 1901 for (signum = EV_NSIG; signum--; ) if (signals [signum].pending)
1862# endif 1902# endif
1863#endif 1903#endif
1864} 1904}
1865#endif 1905#endif
1866 1906
2393inline_size void 2433inline_size void
2394wlist_del (WL *head, WL elem) 2434wlist_del (WL *head, WL elem)
2395{ 2435{
2396 while (*head) 2436 while (*head)
2397 { 2437 {
2398 if (*head == elem) 2438 if (expect_true (*head == elem))
2399 { 2439 {
2400 *head = elem->next; 2440 *head = elem->next;
2401 return; 2441 break;
2402 } 2442 }
2403 2443
2404 head = &(*head)->next; 2444 head = &(*head)->next;
2405 } 2445 }
2406} 2446}
2666 return; 2706 return;
2667 2707
2668 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG)); 2708 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG));
2669 2709
2670#if EV_MULTIPLICITY 2710#if EV_MULTIPLICITY
2671 assert (("libev: tried to attach to a signal from two different loops", 2711 assert (("libev: a signal must not be attached to two different loops",
2672 !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop)); 2712 !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop));
2673 2713
2674 signals [w->signum - 1].loop = EV_A; 2714 signals [w->signum - 1].loop = EV_A;
2675#endif 2715#endif
2676 2716
2747 wlist_del (&signals [w->signum - 1].head, (WL)w); 2787 wlist_del (&signals [w->signum - 1].head, (WL)w);
2748 ev_stop (EV_A_ (W)w); 2788 ev_stop (EV_A_ (W)w);
2749 2789
2750 if (!signals [w->signum - 1].head) 2790 if (!signals [w->signum - 1].head)
2751 { 2791 {
2752 #if EV_MULTIPLICITY 2792#if EV_MULTIPLICITY
2753 signals [w->signum - 1].loop = 0; /* unattach from signal */ 2793 signals [w->signum - 1].loop = 0; /* unattach from signal */
2754 #endif 2794#endif
2755 #if EV_USE_SIGNALFD 2795#if EV_USE_SIGNALFD
2756 if (sigfd >= 0) 2796 if (sigfd >= 0)
2757 { 2797 {
2758 sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D 2798 sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D
2759 sigdelset (&sigfd_set, w->signum); 2799 sigdelset (&sigfd_set, w->signum);
2760 signalfd (sigfd, &sigfd_set, 0); 2800 signalfd (sigfd, &sigfd_set, 0);
2761 sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D 2801 sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D
2762 /*TODO: maybe unblock signal? */ 2802 /*TODO: maybe unblock signal? */
2763 } 2803 }
2764 else 2804 else
2765 #endif 2805#endif
2766 signal (w->signum, SIG_DFL); 2806 signal (w->signum, SIG_DFL);
2767 } 2807 }
2768 2808
2769 EV_FREQUENT_CHECK; 2809 EV_FREQUENT_CHECK;
2770} 2810}
2852 } 2892 }
2853 } 2893 }
2854 2894
2855 if (w->wd >= 0) 2895 if (w->wd >= 0)
2856 { 2896 {
2897 struct statfs sfs;
2898
2857 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); 2899 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w);
2858 2900
2859 /* now local changes will be tracked by inotify, but remote changes won't */ 2901 /* now local changes will be tracked by inotify, but remote changes won't */
2860 /* unless the filesystem it known to be local, we therefore still poll */ 2902 /* unless the filesystem it known to be local, we therefore still poll */
2861 /* also do poll on <2.6.25, but with normal frequency */ 2903 /* also do poll on <2.6.25, but with normal frequency */
2862 struct statfs sfs;
2863 2904
2864 if (fs_2625 && !statfs (w->path, &sfs)) 2905 if (fs_2625 && !statfs (w->path, &sfs))
2865 if (sfs.f_type == 0x1373 /* devfs */ 2906 if (sfs.f_type == 0x1373 /* devfs */
2866 || sfs.f_type == 0xEF53 /* ext2/3 */ 2907 || sfs.f_type == 0xEF53 /* ext2/3 */
2867 || sfs.f_type == 0x3153464a /* jfs */ 2908 || sfs.f_type == 0x3153464a /* jfs */
3416 3457
3417void 3458void
3418ev_async_send (EV_P_ ev_async *w) 3459ev_async_send (EV_P_ ev_async *w)
3419{ 3460{
3420 w->sent = 1; 3461 w->sent = 1;
3421 evpipe_write (EV_A_ &gotasync); 3462 evpipe_write (EV_A_ &async_pending);
3422} 3463}
3423#endif 3464#endif
3424 3465
3425/*****************************************************************************/ 3466/*****************************************************************************/
3426 3467

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines