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

Comparing libev/ev.c (file contents):
Revision 1.302 by root, Thu Jul 16 15:08:08 2009 UTC vs.
Revision 1.313 by root, Wed Aug 19 23:44:51 2009 UTC

133# else 133# else
134# define EV_USE_INOTIFY 0 134# define EV_USE_INOTIFY 0
135# endif 135# endif
136# endif 136# endif
137 137
138# ifndef EV_USE_SIGNALFD
139# if HAVE_SIGNALFD && HAVE_SYS_SIGNALFD_H
140# define EV_USE_SIGNALFD 1
141# else
142# define EV_USE_SIGNALFD 0
143# endif
144# endif
145
138# ifndef EV_USE_EVENTFD 146# ifndef EV_USE_EVENTFD
139# if HAVE_EVENTFD 147# if HAVE_EVENTFD
140# define EV_USE_EVENTFD 1 148# define EV_USE_EVENTFD 1
141# else 149# else
142# define EV_USE_EVENTFD 0 150# define EV_USE_EVENTFD 0
178# endif 186# endif
179#endif 187#endif
180 188
181/* 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 */
182 190
191/* try to deduce the maximum number of signals on this platform */
192#if defined (EV_NSIG)
193/* use what's provided */
194#elif defined (NSIG)
195# define EV_NSIG (NSIG)
196#elif defined(_NSIG)
197# define EV_NSIG (_NSIG)
198#elif defined (SIGMAX)
199# define EV_NSIG (SIGMAX+1)
200#elif defined (SIG_MAX)
201# define EV_NSIG (SIG_MAX+1)
202#elif defined (_SIG_MAX)
203# define EV_NSIG (_SIG_MAX+1)
204#elif defined (MAXSIG)
205# define EV_NSIG (MAXSIG+1)
206#elif defined (MAX_SIG)
207# define EV_NSIG (MAX_SIG+1)
208#elif defined (SIGARRAYSIZE)
209# define EV_NSIG SIGARRAYSIZE /* Assume ary[SIGARRAYSIZE] */
210#elif defined (_sys_nsig)
211# define EV_NSIG (_sys_nsig) /* Solaris 2.5 */
212#else
213# error "unable to find value for NSIG, please report"
214/* to make it compile regardless, just remove the above line */
215# define EV_NSIG 65
216#endif
217
183#ifndef EV_USE_CLOCK_SYSCALL 218#ifndef EV_USE_CLOCK_SYSCALL
184# if __linux && __GLIBC__ >= 2 219# if __linux && __GLIBC__ >= 2
185# define EV_USE_CLOCK_SYSCALL 1 220# define EV_USE_CLOCK_SYSCALL 1
186# else 221# else
187# define EV_USE_CLOCK_SYSCALL 0 222# define EV_USE_CLOCK_SYSCALL 0
263#ifndef EV_USE_EVENTFD 298#ifndef EV_USE_EVENTFD
264# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) 299# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
265# define EV_USE_EVENTFD 1 300# define EV_USE_EVENTFD 1
266# else 301# else
267# define EV_USE_EVENTFD 0 302# define EV_USE_EVENTFD 0
303# endif
304#endif
305
306#ifndef EV_USE_SIGNALFD
307# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9))
308# define EV_USE_SIGNALFD 1
309# else
310# define EV_USE_SIGNALFD 0
268# endif 311# endif
269#endif 312#endif
270 313
271#if 0 /* debugging */ 314#if 0 /* debugging */
272# define EV_VERIFY 3 315# define EV_VERIFY 3
339#endif 382#endif
340 383
341#if EV_USE_EVENTFD 384#if EV_USE_EVENTFD
342/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ 385/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
343# include <stdint.h> 386# include <stdint.h>
387# ifndef EFD_NONBLOCK
388# define EFD_NONBLOCK O_NONBLOCK
389# endif
390# ifndef EFD_CLOEXEC
391# ifdef O_CLOEXEC
392# define EFD_CLOEXEC O_CLOEXEC
393# else
394# define EFD_CLOEXEC 02000000
395# endif
396# endif
344# ifdef __cplusplus 397# ifdef __cplusplus
345extern "C" { 398extern "C" {
346# endif 399# endif
347int eventfd (unsigned int initval, int flags); 400int eventfd (unsigned int initval, int flags);
348# ifdef __cplusplus 401# ifdef __cplusplus
349} 402}
350# endif 403# endif
404#endif
405
406#if EV_USE_SIGNALFD
407# include <sys/signalfd.h>
351#endif 408#endif
352 409
353/**/ 410/**/
354 411
355#if EV_VERIFY >= 3 412#if EV_VERIFY >= 3
419 476
420#if EV_USE_MONOTONIC 477#if EV_USE_MONOTONIC
421static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 478static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
422#endif 479#endif
423 480
481#ifndef EV_FD_TO_WIN32_HANDLE
482# define EV_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd)
483#endif
484#ifndef EV_WIN32_HANDLE_TO_FD
485# define EV_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (fd, 0)
486#endif
487#ifndef EV_WIN32_CLOSE_FD
488# define EV_WIN32_CLOSE_FD(fd) close (fd)
489#endif
490
424#ifdef _WIN32 491#ifdef _WIN32
425# include "ev_win32.c" 492# include "ev_win32.c"
426#endif 493#endif
427 494
428/*****************************************************************************/ 495/*****************************************************************************/
819 886
820#if EV_SELECT_IS_WINSOCKET 887#if EV_SELECT_IS_WINSOCKET
821 if (events) 888 if (events)
822 { 889 {
823 unsigned long arg; 890 unsigned long arg;
824 #ifdef EV_FD_TO_WIN32_HANDLE
825 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); 891 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
826 #else
827 anfd->handle = _get_osfhandle (fd);
828 #endif
829 assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0)); 892 assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0));
830 } 893 }
831#endif 894#endif
832 895
833 { 896 {
904 967
905 for (fd = anfdmax; fd--; ) 968 for (fd = anfdmax; fd--; )
906 if (anfds [fd].events) 969 if (anfds [fd].events)
907 { 970 {
908 fd_kill (EV_A_ fd); 971 fd_kill (EV_A_ fd);
909 return; 972 break;
910 } 973 }
911} 974}
912 975
913/* usually called after fork if backend needs to re-arm all fds from scratch */ 976/* usually called after fork if backend needs to re-arm all fds from scratch */
914static void noinline 977static void noinline
1004 1067
1005 for (;;) 1068 for (;;)
1006 { 1069 {
1007 int c = k << 1; 1070 int c = k << 1;
1008 1071
1009 if (c > N + HEAP0 - 1) 1072 if (c >= N + HEAP0)
1010 break; 1073 break;
1011 1074
1012 c += c + 1 < N + HEAP0 && ANHE_at (heap [c]) > ANHE_at (heap [c + 1]) 1075 c += c + 1 < N + HEAP0 && ANHE_at (heap [c]) > ANHE_at (heap [c + 1])
1013 ? 1 : 0; 1076 ? 1 : 0;
1014 1077
1050 1113
1051/* move an element suitably so it is in a correct place */ 1114/* move an element suitably so it is in a correct place */
1052inline_size void 1115inline_size void
1053adjustheap (ANHE *heap, int N, int k) 1116adjustheap (ANHE *heap, int N, int k)
1054{ 1117{
1055 if (k > HEAP0 && ANHE_at (heap [HPARENT (k)]) >= ANHE_at (heap [k])) 1118 if (k > HEAP0 && ANHE_at (heap [k]) <= ANHE_at (heap [HPARENT (k)]))
1056 upheap (heap, k); 1119 upheap (heap, k);
1057 else 1120 else
1058 downheap (heap, N, k); 1121 downheap (heap, N, k);
1059} 1122}
1060 1123
1073/*****************************************************************************/ 1136/*****************************************************************************/
1074 1137
1075/* associate signal watchers to a signal signal */ 1138/* associate signal watchers to a signal signal */
1076typedef struct 1139typedef struct
1077{ 1140{
1141 EV_ATOMIC_T pending;
1142#if EV_MULTIPLICITY
1143 EV_P;
1144#endif
1078 WL head; 1145 WL head;
1079 EV_ATOMIC_T gotsig;
1080} ANSIG; 1146} ANSIG;
1081 1147
1082static ANSIG *signals; 1148static ANSIG signals [EV_NSIG - 1];
1083static int signalmax;
1084
1085static EV_ATOMIC_T gotsig;
1086 1149
1087/*****************************************************************************/ 1150/*****************************************************************************/
1088 1151
1089/* used to prepare libev internal fd's */ 1152/* used to prepare libev internal fd's */
1090/* this is not fork-safe */ 1153/* this is not fork-safe */
1104evpipe_init (EV_P) 1167evpipe_init (EV_P)
1105{ 1168{
1106 if (!ev_is_active (&pipe_w)) 1169 if (!ev_is_active (&pipe_w))
1107 { 1170 {
1108#if EV_USE_EVENTFD 1171#if EV_USE_EVENTFD
1172 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
1173 if (evfd < 0 && errno == EINVAL)
1109 if ((evfd = eventfd (0, 0)) >= 0) 1174 evfd = eventfd (0, 0);
1175
1176 if (evfd >= 0)
1110 { 1177 {
1111 evpipe [0] = -1; 1178 evpipe [0] = -1;
1112 fd_intern (evfd); 1179 fd_intern (evfd); /* doing it twice doesn't hurt */
1113 ev_io_set (&pipe_w, evfd, EV_READ); 1180 ev_io_set (&pipe_w, evfd, EV_READ);
1114 } 1181 }
1115 else 1182 else
1116#endif 1183#endif
1117 { 1184 {
1154/* called whenever the libev signal pipe */ 1221/* called whenever the libev signal pipe */
1155/* got some events (signal, async) */ 1222/* got some events (signal, async) */
1156static void 1223static void
1157pipecb (EV_P_ ev_io *iow, int revents) 1224pipecb (EV_P_ ev_io *iow, int revents)
1158{ 1225{
1226 int i;
1227
1159#if EV_USE_EVENTFD 1228#if EV_USE_EVENTFD
1160 if (evfd >= 0) 1229 if (evfd >= 0)
1161 { 1230 {
1162 uint64_t counter; 1231 uint64_t counter;
1163 read (evfd, &counter, sizeof (uint64_t)); 1232 read (evfd, &counter, sizeof (uint64_t));
1167 { 1236 {
1168 char dummy; 1237 char dummy;
1169 read (evpipe [0], &dummy, 1); 1238 read (evpipe [0], &dummy, 1);
1170 } 1239 }
1171 1240
1172 if (gotsig && ev_is_default_loop (EV_A)) 1241 if (sig_pending)
1173 { 1242 {
1174 int signum; 1243 sig_pending = 0;
1175 gotsig = 0;
1176 1244
1177 for (signum = signalmax; signum--; ) 1245 for (i = EV_NSIG - 1; i--; )
1178 if (signals [signum].gotsig) 1246 if (expect_false (signals [i].pending))
1179 ev_feed_signal_event (EV_A_ signum + 1); 1247 ev_feed_signal_event (EV_A_ i + 1);
1180 } 1248 }
1181 1249
1182#if EV_ASYNC_ENABLE 1250#if EV_ASYNC_ENABLE
1183 if (gotasync) 1251 if (async_pending)
1184 { 1252 {
1185 int i; 1253 async_pending = 0;
1186 gotasync = 0;
1187 1254
1188 for (i = asynccnt; i--; ) 1255 for (i = asynccnt; i--; )
1189 if (asyncs [i]->sent) 1256 if (asyncs [i]->sent)
1190 { 1257 {
1191 asyncs [i]->sent = 0; 1258 asyncs [i]->sent = 0;
1199 1266
1200static void 1267static void
1201ev_sighandler (int signum) 1268ev_sighandler (int signum)
1202{ 1269{
1203#if EV_MULTIPLICITY 1270#if EV_MULTIPLICITY
1204 struct ev_loop *loop = &default_loop_struct; 1271 EV_P = signals [signum - 1].loop;
1205#endif 1272#endif
1206 1273
1207#if _WIN32 1274#if _WIN32
1208 signal (signum, ev_sighandler); 1275 signal (signum, ev_sighandler);
1209#endif 1276#endif
1210 1277
1211 signals [signum - 1].gotsig = 1; 1278 signals [signum - 1].pending = 1;
1212 evpipe_write (EV_A_ &gotsig); 1279 evpipe_write (EV_A_ &sig_pending);
1213} 1280}
1214 1281
1215void noinline 1282void noinline
1216ev_feed_signal_event (EV_P_ int signum) 1283ev_feed_signal_event (EV_P_ int signum)
1217{ 1284{
1218 WL w; 1285 WL w;
1219 1286
1287 if (expect_false (signum <= 0 || signum > EV_NSIG))
1288 return;
1289
1290 --signum;
1291
1220#if EV_MULTIPLICITY 1292#if EV_MULTIPLICITY
1221 assert (("libev: feeding signal events is only supported in the default loop", loop == ev_default_loop_ptr)); 1293 /* it is permissible to try to feed a signal to the wrong loop */
1222#endif 1294 /* or, likely more useful, feeding a signal nobody is waiting for */
1223 1295
1224 --signum; 1296 if (expect_false (signals [signum].loop != EV_A))
1225
1226 if (signum < 0 || signum >= signalmax)
1227 return; 1297 return;
1298#endif
1228 1299
1229 signals [signum].gotsig = 0; 1300 signals [signum].pending = 0;
1230 1301
1231 for (w = signals [signum].head; w; w = w->next) 1302 for (w = signals [signum].head; w; w = w->next)
1232 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 1303 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
1233} 1304}
1305
1306#if EV_USE_SIGNALFD
1307static void
1308sigfdcb (EV_P_ ev_io *iow, int revents)
1309{
1310 struct signalfd_siginfo si[2], *sip; /* these structs are big */
1311
1312 for (;;)
1313 {
1314 ssize_t res = read (sigfd, si, sizeof (si));
1315
1316 /* not ISO-C, as res might be -1, but works with SuS */
1317 for (sip = si; (char *)sip < (char *)si + res; ++sip)
1318 ev_feed_signal_event (EV_A_ sip->ssi_signo);
1319
1320 if (res < (ssize_t)sizeof (si))
1321 break;
1322 }
1323}
1324#endif
1234 1325
1235/*****************************************************************************/ 1326/*****************************************************************************/
1236 1327
1237static WL childs [EV_PID_HASHSIZE]; 1328static WL childs [EV_PID_HASHSIZE];
1238 1329
1458 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 1549 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
1459 have_monotonic = 1; 1550 have_monotonic = 1;
1460 } 1551 }
1461#endif 1552#endif
1462 1553
1554 /* pid check not overridable via env */
1555#ifndef _WIN32
1556 if (flags & EVFLAG_FORKCHECK)
1557 curpid = getpid ();
1558#endif
1559
1560 if (!(flags & EVFLAG_NOENV)
1561 && !enable_secure ()
1562 && getenv ("LIBEV_FLAGS"))
1563 flags = atoi (getenv ("LIBEV_FLAGS"));
1564
1463 ev_rt_now = ev_time (); 1565 ev_rt_now = ev_time ();
1464 mn_now = get_clock (); 1566 mn_now = get_clock ();
1465 now_floor = mn_now; 1567 now_floor = mn_now;
1466 rtmn_diff = ev_rt_now - mn_now; 1568 rtmn_diff = ev_rt_now - mn_now;
1467#if EV_MINIMAL < 2 1569#if EV_MINIMAL < 2
1470 1572
1471 io_blocktime = 0.; 1573 io_blocktime = 0.;
1472 timeout_blocktime = 0.; 1574 timeout_blocktime = 0.;
1473 backend = 0; 1575 backend = 0;
1474 backend_fd = -1; 1576 backend_fd = -1;
1475 gotasync = 0; 1577 sig_pending = 0;
1578#if EV_ASYNC_ENABLE
1579 async_pending = 0;
1580#endif
1476#if EV_USE_INOTIFY 1581#if EV_USE_INOTIFY
1477 fs_fd = -2; 1582 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
1478#endif 1583#endif
1479 1584#if EV_USE_SIGNALFD
1480 /* pid check not overridable via env */ 1585 sigfd = flags & EVFLAG_NOSIGFD ? -1 : -2;
1481#ifndef _WIN32
1482 if (flags & EVFLAG_FORKCHECK)
1483 curpid = getpid ();
1484#endif 1586#endif
1485
1486 if (!(flags & EVFLAG_NOENV)
1487 && !enable_secure ()
1488 && getenv ("LIBEV_FLAGS"))
1489 flags = atoi (getenv ("LIBEV_FLAGS"));
1490 1587
1491 if (!(flags & 0x0000ffffU)) 1588 if (!(flags & 0x0000ffffU))
1492 flags |= ev_recommended_backends (); 1589 flags |= ev_recommended_backends ();
1493 1590
1494#if EV_USE_PORT 1591#if EV_USE_PORT
1520{ 1617{
1521 int i; 1618 int i;
1522 1619
1523 if (ev_is_active (&pipe_w)) 1620 if (ev_is_active (&pipe_w))
1524 { 1621 {
1525 ev_ref (EV_A); /* signal watcher */ 1622 /*ev_ref (EV_A);*/
1526 ev_io_stop (EV_A_ &pipe_w); 1623 /*ev_io_stop (EV_A_ &pipe_w);*/
1527 1624
1528#if EV_USE_EVENTFD 1625#if EV_USE_EVENTFD
1529 if (evfd >= 0) 1626 if (evfd >= 0)
1530 close (evfd); 1627 close (evfd);
1531#endif 1628#endif
1532 1629
1533 if (evpipe [0] >= 0) 1630 if (evpipe [0] >= 0)
1534 { 1631 {
1535 close (evpipe [0]); 1632 EV_WIN32_CLOSE_FD (evpipe [0]);
1536 close (evpipe [1]); 1633 EV_WIN32_CLOSE_FD (evpipe [1]);
1537 } 1634 }
1538 } 1635 }
1636
1637#if EV_USE_SIGNALFD
1638 if (ev_is_active (&sigfd_w))
1639 {
1640 /*ev_ref (EV_A);*/
1641 /*ev_io_stop (EV_A_ &sigfd_w);*/
1642
1643 close (sigfd);
1644 }
1645#endif
1539 1646
1540#if EV_USE_INOTIFY 1647#if EV_USE_INOTIFY
1541 if (fs_fd >= 0) 1648 if (fs_fd >= 0)
1542 close (fs_fd); 1649 close (fs_fd);
1543#endif 1650#endif
1567#if EV_IDLE_ENABLE 1674#if EV_IDLE_ENABLE
1568 array_free (idle, [i]); 1675 array_free (idle, [i]);
1569#endif 1676#endif
1570 } 1677 }
1571 1678
1572 ev_free (anfds); anfdmax = 0; 1679 ev_free (anfds); anfds = 0; anfdmax = 0;
1573 1680
1574 /* have to use the microsoft-never-gets-it-right macro */ 1681 /* have to use the microsoft-never-gets-it-right macro */
1575 array_free (rfeed, EMPTY); 1682 array_free (rfeed, EMPTY);
1576 array_free (fdchange, EMPTY); 1683 array_free (fdchange, EMPTY);
1577 array_free (timer, EMPTY); 1684 array_free (timer, EMPTY);
1612 1719
1613 if (ev_is_active (&pipe_w)) 1720 if (ev_is_active (&pipe_w))
1614 { 1721 {
1615 /* this "locks" the handlers against writing to the pipe */ 1722 /* this "locks" the handlers against writing to the pipe */
1616 /* while we modify the fd vars */ 1723 /* while we modify the fd vars */
1617 gotsig = 1; 1724 sig_pending = 1;
1618#if EV_ASYNC_ENABLE 1725#if EV_ASYNC_ENABLE
1619 gotasync = 1; 1726 async_pending = 1;
1620#endif 1727#endif
1621 1728
1622 ev_ref (EV_A); 1729 ev_ref (EV_A);
1623 ev_io_stop (EV_A_ &pipe_w); 1730 ev_io_stop (EV_A_ &pipe_w);
1624 1731
1627 close (evfd); 1734 close (evfd);
1628#endif 1735#endif
1629 1736
1630 if (evpipe [0] >= 0) 1737 if (evpipe [0] >= 0)
1631 { 1738 {
1632 close (evpipe [0]); 1739 EV_WIN32_CLOSE_FD (evpipe [0]);
1633 close (evpipe [1]); 1740 EV_WIN32_CLOSE_FD (evpipe [1]);
1634 } 1741 }
1635 1742
1636 evpipe_init (EV_A); 1743 evpipe_init (EV_A);
1637 /* now iterate over everything, in case we missed something */ 1744 /* now iterate over everything, in case we missed something */
1638 pipecb (EV_A_ &pipe_w, EV_READ); 1745 pipecb (EV_A_ &pipe_w, EV_READ);
1644#if EV_MULTIPLICITY 1751#if EV_MULTIPLICITY
1645 1752
1646struct ev_loop * 1753struct ev_loop *
1647ev_loop_new (unsigned int flags) 1754ev_loop_new (unsigned int flags)
1648{ 1755{
1649 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 1756 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
1650 1757
1651 memset (loop, 0, sizeof (struct ev_loop)); 1758 memset (EV_A, 0, sizeof (struct ev_loop));
1652
1653 loop_init (EV_A_ flags); 1759 loop_init (EV_A_ flags);
1654 1760
1655 if (ev_backend (EV_A)) 1761 if (ev_backend (EV_A))
1656 return loop; 1762 return EV_A;
1657 1763
1658 return 0; 1764 return 0;
1659} 1765}
1660 1766
1661void 1767void
1765 assert (checkmax >= checkcnt); 1871 assert (checkmax >= checkcnt);
1766 array_verify (EV_A_ (W *)checks, checkcnt); 1872 array_verify (EV_A_ (W *)checks, checkcnt);
1767 1873
1768# if 0 1874# if 0
1769 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 1875 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
1770 for (signum = signalmax; signum--; ) if (signals [signum].gotsig) 1876 for (signum = EV_NSIG; signum--; ) if (signals [signum].pending)
1771# endif 1877# endif
1772#endif 1878#endif
1773} 1879}
1774#endif 1880#endif
1775 1881
1782#endif 1888#endif
1783{ 1889{
1784 if (!ev_default_loop_ptr) 1890 if (!ev_default_loop_ptr)
1785 { 1891 {
1786#if EV_MULTIPLICITY 1892#if EV_MULTIPLICITY
1787 struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct; 1893 EV_P = ev_default_loop_ptr = &default_loop_struct;
1788#else 1894#else
1789 ev_default_loop_ptr = 1; 1895 ev_default_loop_ptr = 1;
1790#endif 1896#endif
1791 1897
1792 loop_init (EV_A_ flags); 1898 loop_init (EV_A_ flags);
1809 1915
1810void 1916void
1811ev_default_destroy (void) 1917ev_default_destroy (void)
1812{ 1918{
1813#if EV_MULTIPLICITY 1919#if EV_MULTIPLICITY
1814 struct ev_loop *loop = ev_default_loop_ptr; 1920 EV_P = ev_default_loop_ptr;
1815#endif 1921#endif
1816 1922
1817 ev_default_loop_ptr = 0; 1923 ev_default_loop_ptr = 0;
1818 1924
1819#ifndef _WIN32 1925#ifndef _WIN32
1826 1932
1827void 1933void
1828ev_default_fork (void) 1934ev_default_fork (void)
1829{ 1935{
1830#if EV_MULTIPLICITY 1936#if EV_MULTIPLICITY
1831 struct ev_loop *loop = ev_default_loop_ptr; 1937 EV_P = ev_default_loop_ptr;
1832#endif 1938#endif
1833 1939
1834 postfork = 1; /* must be in line with ev_loop_fork */ 1940 postfork = 1; /* must be in line with ev_loop_fork */
1835} 1941}
1836 1942
2302inline_size void 2408inline_size void
2303wlist_del (WL *head, WL elem) 2409wlist_del (WL *head, WL elem)
2304{ 2410{
2305 while (*head) 2411 while (*head)
2306 { 2412 {
2307 if (*head == elem) 2413 if (expect_true (*head == elem))
2308 { 2414 {
2309 *head = elem->next; 2415 *head = elem->next;
2310 return; 2416 break;
2311 } 2417 }
2312 2418
2313 head = &(*head)->next; 2419 head = &(*head)->next;
2314 } 2420 }
2315} 2421}
2569#endif 2675#endif
2570 2676
2571void noinline 2677void noinline
2572ev_signal_start (EV_P_ ev_signal *w) 2678ev_signal_start (EV_P_ ev_signal *w)
2573{ 2679{
2574#if EV_MULTIPLICITY
2575 assert (("libev: signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
2576#endif
2577 if (expect_false (ev_is_active (w))) 2680 if (expect_false (ev_is_active (w)))
2578 return; 2681 return;
2579 2682
2580 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0)); 2683 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG));
2581 2684
2582 evpipe_init (EV_A); 2685#if EV_MULTIPLICITY
2686 assert (("libev: a signal must not be attached to two different loops",
2687 !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop));
2583 2688
2584 EV_FREQUENT_CHECK; 2689 signals [w->signum - 1].loop = EV_A;
2690#endif
2585 2691
2692 EV_FREQUENT_CHECK;
2693
2694#if EV_USE_SIGNALFD
2695 if (sigfd == -2)
2586 { 2696 {
2587#ifndef _WIN32 2697 sigfd = signalfd (-1, &sigfd_set, SFD_NONBLOCK | SFD_CLOEXEC);
2588 sigset_t full, prev; 2698 if (sigfd < 0 && errno == EINVAL)
2589 sigfillset (&full); 2699 sigfd = signalfd (-1, &sigfd_set, 0); /* retry without flags */
2590 sigprocmask (SIG_SETMASK, &full, &prev);
2591#endif
2592 2700
2593 array_needsize (ANSIG, signals, signalmax, w->signum, array_init_zero); 2701 if (sigfd >= 0)
2702 {
2703 fd_intern (sigfd); /* doing it twice will not hurt */
2594 2704
2595#ifndef _WIN32 2705 sigemptyset (&sigfd_set);
2596 sigprocmask (SIG_SETMASK, &prev, 0); 2706
2597#endif 2707 ev_io_init (&sigfd_w, sigfdcb, sigfd, EV_READ);
2708 ev_set_priority (&sigfd_w, EV_MAXPRI);
2709 ev_io_start (EV_A_ &sigfd_w);
2710 ev_unref (EV_A); /* signalfd watcher should not keep loop alive */
2711 }
2598 } 2712 }
2713
2714 if (sigfd >= 0)
2715 {
2716 /* TODO: check .head */
2717 sigaddset (&sigfd_set, w->signum);
2718 sigprocmask (SIG_BLOCK, &sigfd_set, 0);
2719
2720 signalfd (sigfd, &sigfd_set, 0);
2721 }
2722#endif
2599 2723
2600 ev_start (EV_A_ (W)w, 1); 2724 ev_start (EV_A_ (W)w, 1);
2601 wlist_add (&signals [w->signum - 1].head, (WL)w); 2725 wlist_add (&signals [w->signum - 1].head, (WL)w);
2602 2726
2603 if (!((WL)w)->next) 2727 if (!((WL)w)->next)
2728# if EV_USE_SIGNALFD
2729 if (sigfd < 0) /*TODO*/
2730# endif
2604 { 2731 {
2605#if _WIN32 2732# if _WIN32
2606 signal (w->signum, ev_sighandler); 2733 signal (w->signum, ev_sighandler);
2607#else 2734# else
2608 struct sigaction sa = { }; 2735 struct sigaction sa;
2736
2737 evpipe_init (EV_A);
2738
2609 sa.sa_handler = ev_sighandler; 2739 sa.sa_handler = ev_sighandler;
2610 sigfillset (&sa.sa_mask); 2740 sigfillset (&sa.sa_mask);
2611 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 2741 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
2612 sigaction (w->signum, &sa, 0); 2742 sigaction (w->signum, &sa, 0);
2743
2744 sigemptyset (&sa.sa_mask);
2745 sigaddset (&sa.sa_mask, w->signum);
2746 sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0);
2613#endif 2747#endif
2614 } 2748 }
2615 2749
2616 EV_FREQUENT_CHECK; 2750 EV_FREQUENT_CHECK;
2617} 2751}
2618 2752
2619void noinline 2753void noinline
2627 2761
2628 wlist_del (&signals [w->signum - 1].head, (WL)w); 2762 wlist_del (&signals [w->signum - 1].head, (WL)w);
2629 ev_stop (EV_A_ (W)w); 2763 ev_stop (EV_A_ (W)w);
2630 2764
2631 if (!signals [w->signum - 1].head) 2765 if (!signals [w->signum - 1].head)
2766 {
2767#if EV_MULTIPLICITY
2768 signals [w->signum - 1].loop = 0; /* unattach from signal */
2769#endif
2770#if EV_USE_SIGNALFD
2771 if (sigfd >= 0)
2772 {
2773 sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D
2774 sigdelset (&sigfd_set, w->signum);
2775 signalfd (sigfd, &sigfd_set, 0);
2776 sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D
2777 /*TODO: maybe unblock signal? */
2778 }
2779 else
2780#endif
2632 signal (w->signum, SIG_DFL); 2781 signal (w->signum, SIG_DFL);
2782 }
2633 2783
2634 EV_FREQUENT_CHECK; 2784 EV_FREQUENT_CHECK;
2635} 2785}
2636 2786
2637void 2787void
2717 } 2867 }
2718 } 2868 }
2719 2869
2720 if (w->wd >= 0) 2870 if (w->wd >= 0)
2721 { 2871 {
2872 struct statfs sfs;
2873
2722 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); 2874 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w);
2723 2875
2724 /* now local changes will be tracked by inotify, but remote changes won't */ 2876 /* now local changes will be tracked by inotify, but remote changes won't */
2725 /* unless the filesystem it known to be local, we therefore still poll */ 2877 /* unless the filesystem it known to be local, we therefore still poll */
2726 /* also do poll on <2.6.25, but with normal frequency */ 2878 /* also do poll on <2.6.25, but with normal frequency */
2727 struct statfs sfs;
2728 2879
2729 if (fs_2625 && !statfs (w->path, &sfs)) 2880 if (fs_2625 && !statfs (w->path, &sfs))
2730 if (sfs.f_type == 0x1373 /* devfs */ 2881 if (sfs.f_type == 0x1373 /* devfs */
2731 || sfs.f_type == 0xEF53 /* ext2/3 */ 2882 || sfs.f_type == 0xEF53 /* ext2/3 */
2732 || sfs.f_type == 0x3153464a /* jfs */ 2883 || sfs.f_type == 0x3153464a /* jfs */
3117embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents) 3268embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents)
3118{ 3269{
3119 ev_embed *w = (ev_embed *)(((char *)prepare) - offsetof (ev_embed, prepare)); 3270 ev_embed *w = (ev_embed *)(((char *)prepare) - offsetof (ev_embed, prepare));
3120 3271
3121 { 3272 {
3122 struct ev_loop *loop = w->other; 3273 EV_P = w->other;
3123 3274
3124 while (fdchangecnt) 3275 while (fdchangecnt)
3125 { 3276 {
3126 fd_reify (EV_A); 3277 fd_reify (EV_A);
3127 ev_loop (EV_A_ EVLOOP_NONBLOCK); 3278 ev_loop (EV_A_ EVLOOP_NONBLOCK);
3135 ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork)); 3286 ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork));
3136 3287
3137 ev_embed_stop (EV_A_ w); 3288 ev_embed_stop (EV_A_ w);
3138 3289
3139 { 3290 {
3140 struct ev_loop *loop = w->other; 3291 EV_P = w->other;
3141 3292
3142 ev_loop_fork (EV_A); 3293 ev_loop_fork (EV_A);
3143 ev_loop (EV_A_ EVLOOP_NONBLOCK); 3294 ev_loop (EV_A_ EVLOOP_NONBLOCK);
3144 } 3295 }
3145 3296
3159{ 3310{
3160 if (expect_false (ev_is_active (w))) 3311 if (expect_false (ev_is_active (w)))
3161 return; 3312 return;
3162 3313
3163 { 3314 {
3164 struct ev_loop *loop = w->other; 3315 EV_P = w->other;
3165 assert (("libev: loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); 3316 assert (("libev: loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
3166 ev_io_init (&w->io, embed_io_cb, backend_fd, EV_READ); 3317 ev_io_init (&w->io, embed_io_cb, backend_fd, EV_READ);
3167 } 3318 }
3168 3319
3169 EV_FREQUENT_CHECK; 3320 EV_FREQUENT_CHECK;
3281 3432
3282void 3433void
3283ev_async_send (EV_P_ ev_async *w) 3434ev_async_send (EV_P_ ev_async *w)
3284{ 3435{
3285 w->sent = 1; 3436 w->sent = 1;
3286 evpipe_write (EV_A_ &gotasync); 3437 evpipe_write (EV_A_ &async_pending);
3287} 3438}
3288#endif 3439#endif
3289 3440
3290/*****************************************************************************/ 3441/*****************************************************************************/
3291 3442
3440 if (types & EV_CHECK) 3591 if (types & EV_CHECK)
3441 for (i = checkcnt; i--; ) 3592 for (i = checkcnt; i--; )
3442 cb (EV_A_ EV_CHECK, checks [i]); 3593 cb (EV_A_ EV_CHECK, checks [i]);
3443 3594
3444 if (types & EV_SIGNAL) 3595 if (types & EV_SIGNAL)
3445 for (i = 0; i < signalmax; ++i) 3596 for (i = 0; i < EV_NSIG - 1; ++i)
3446 for (wl = signals [i].head; wl; ) 3597 for (wl = signals [i].head; wl; )
3447 { 3598 {
3448 wn = wl->next; 3599 wn = wl->next;
3449 cb (EV_A_ EV_SIGNAL, wl); 3600 cb (EV_A_ EV_SIGNAL, wl);
3450 wl = wn; 3601 wl = wn;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines