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

Comparing libev/ev.c (file contents):
Revision 1.303 by root, Sun Jul 19 01:36:34 2009 UTC vs.
Revision 1.317 by root, Sat Nov 14 00:15:21 2009 UTC

186# endif 186# endif
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 */
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
191#ifndef EV_USE_CLOCK_SYSCALL 218#ifndef EV_USE_CLOCK_SYSCALL
192# if __linux && __GLIBC__ >= 2 219# if __linux && __GLIBC__ >= 2
193# define EV_USE_CLOCK_SYSCALL 1 220# define EV_USE_CLOCK_SYSCALL 1
194# else 221# else
195# define EV_USE_CLOCK_SYSCALL 0 222# define EV_USE_CLOCK_SYSCALL 0
275# define EV_USE_EVENTFD 0 302# define EV_USE_EVENTFD 0
276# endif 303# endif
277#endif 304#endif
278 305
279#ifndef EV_USE_SIGNALFD 306#ifndef EV_USE_SIGNALFD
280# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9)) 307# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
281# define EV_USE_SIGNALFD 1 308# define EV_USE_SIGNALFD 1
282# else 309# else
283# define EV_USE_SIGNALFD 0 310# define EV_USE_SIGNALFD 0
284# endif 311# endif
285#endif 312#endif
359# include <stdint.h> 386# include <stdint.h>
360# ifndef EFD_NONBLOCK 387# ifndef EFD_NONBLOCK
361# define EFD_NONBLOCK O_NONBLOCK 388# define EFD_NONBLOCK O_NONBLOCK
362# endif 389# endif
363# ifndef EFD_CLOEXEC 390# ifndef EFD_CLOEXEC
391# ifdef O_CLOEXEC
364# define EFD_CLOEXEC O_CLOEXEC 392# define EFD_CLOEXEC O_CLOEXEC
393# else
394# define EFD_CLOEXEC 02000000
395# endif
365# endif 396# endif
366# ifdef __cplusplus 397# ifdef __cplusplus
367extern "C" { 398extern "C" {
368# endif 399# endif
369int eventfd (unsigned int initval, int flags); 400int eventfd (unsigned int initval, int flags);
371} 402}
372# endif 403# endif
373#endif 404#endif
374 405
375#if EV_USE_SIGNALFD 406#if EV_USE_SIGNALFD
376# 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
377#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
378 434
379/**/ 435/**/
380 436
381#if EV_VERIFY >= 3 437#if EV_VERIFY >= 3
382# define EV_FREQUENT_CHECK ev_loop_verify (EV_A) 438# define EV_FREQUENT_CHECK ev_loop_verify (EV_A)
394 */ 450 */
395#define TIME_EPSILON 0.0001220703125 /* 1/8192 */ 451#define TIME_EPSILON 0.0001220703125 /* 1/8192 */
396 452
397#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 453#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
398#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ 454#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
399/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds, TODO */
400 455
401#if __GNUC__ >= 4 456#if __GNUC__ >= 4
402# define expect(expr,value) __builtin_expect ((expr),(value)) 457# define expect(expr,value) __builtin_expect ((expr),(value))
403# define noinline __attribute__ ((noinline)) 458# define noinline __attribute__ ((noinline))
404#else 459#else
443static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */ 498static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */
444#endif 499#endif
445 500
446#if EV_USE_MONOTONIC 501#if EV_USE_MONOTONIC
447static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 502static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
503#endif
504
505#ifndef EV_FD_TO_WIN32_HANDLE
506# define EV_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd)
507#endif
508#ifndef EV_WIN32_HANDLE_TO_FD
509# define EV_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (fd, 0)
510#endif
511#ifndef EV_WIN32_CLOSE_FD
512# define EV_WIN32_CLOSE_FD(fd) close (fd)
448#endif 513#endif
449 514
450#ifdef _WIN32 515#ifdef _WIN32
451# include "ev_win32.c" 516# include "ev_win32.c"
452#endif 517#endif
845 910
846#if EV_SELECT_IS_WINSOCKET 911#if EV_SELECT_IS_WINSOCKET
847 if (events) 912 if (events)
848 { 913 {
849 unsigned long arg; 914 unsigned long arg;
850 #ifdef EV_FD_TO_WIN32_HANDLE
851 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); 915 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
852 #else
853 anfd->handle = _get_osfhandle (fd);
854 #endif
855 assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0)); 916 assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0));
856 } 917 }
857#endif 918#endif
858 919
859 { 920 {
930 991
931 for (fd = anfdmax; fd--; ) 992 for (fd = anfdmax; fd--; )
932 if (anfds [fd].events) 993 if (anfds [fd].events)
933 { 994 {
934 fd_kill (EV_A_ fd); 995 fd_kill (EV_A_ fd);
935 return; 996 break;
936 } 997 }
937} 998}
938 999
939/* usually called after fork if backend needs to re-arm all fds from scratch */ 1000/* usually called after fork if backend needs to re-arm all fds from scratch */
940static void noinline 1001static void noinline
1030 1091
1031 for (;;) 1092 for (;;)
1032 { 1093 {
1033 int c = k << 1; 1094 int c = k << 1;
1034 1095
1035 if (c > N + HEAP0 - 1) 1096 if (c >= N + HEAP0)
1036 break; 1097 break;
1037 1098
1038 c += c + 1 < N + HEAP0 && ANHE_at (heap [c]) > ANHE_at (heap [c + 1]) 1099 c += c + 1 < N + HEAP0 && ANHE_at (heap [c]) > ANHE_at (heap [c + 1])
1039 ? 1 : 0; 1100 ? 1 : 0;
1040 1101
1076 1137
1077/* move an element suitably so it is in a correct place */ 1138/* move an element suitably so it is in a correct place */
1078inline_size void 1139inline_size void
1079adjustheap (ANHE *heap, int N, int k) 1140adjustheap (ANHE *heap, int N, int k)
1080{ 1141{
1081 if (k > HEAP0 && ANHE_at (heap [HPARENT (k)]) >= ANHE_at (heap [k])) 1142 if (k > HEAP0 && ANHE_at (heap [k]) <= ANHE_at (heap [HPARENT (k)]))
1082 upheap (heap, k); 1143 upheap (heap, k);
1083 else 1144 else
1084 downheap (heap, N, k); 1145 downheap (heap, N, k);
1085} 1146}
1086 1147
1099/*****************************************************************************/ 1160/*****************************************************************************/
1100 1161
1101/* associate signal watchers to a signal signal */ 1162/* associate signal watchers to a signal signal */
1102typedef struct 1163typedef struct
1103{ 1164{
1165 EV_ATOMIC_T pending;
1166#if EV_MULTIPLICITY
1167 EV_P;
1168#endif
1104 WL head; 1169 WL head;
1105 EV_ATOMIC_T gotsig;
1106} ANSIG; 1170} ANSIG;
1107 1171
1108static ANSIG *signals; 1172static ANSIG signals [EV_NSIG - 1];
1109static int signalmax;
1110
1111static EV_ATOMIC_T gotsig;
1112 1173
1113/*****************************************************************************/ 1174/*****************************************************************************/
1114 1175
1115/* used to prepare libev internal fd's */ 1176/* used to prepare libev internal fd's */
1116/* this is not fork-safe */ 1177/* this is not fork-safe */
1184/* called whenever the libev signal pipe */ 1245/* called whenever the libev signal pipe */
1185/* got some events (signal, async) */ 1246/* got some events (signal, async) */
1186static void 1247static void
1187pipecb (EV_P_ ev_io *iow, int revents) 1248pipecb (EV_P_ ev_io *iow, int revents)
1188{ 1249{
1250 int i;
1251
1189#if EV_USE_EVENTFD 1252#if EV_USE_EVENTFD
1190 if (evfd >= 0) 1253 if (evfd >= 0)
1191 { 1254 {
1192 uint64_t counter; 1255 uint64_t counter;
1193 read (evfd, &counter, sizeof (uint64_t)); 1256 read (evfd, &counter, sizeof (uint64_t));
1197 { 1260 {
1198 char dummy; 1261 char dummy;
1199 read (evpipe [0], &dummy, 1); 1262 read (evpipe [0], &dummy, 1);
1200 } 1263 }
1201 1264
1202 if (gotsig && ev_is_default_loop (EV_A)) 1265 if (sig_pending)
1203 { 1266 {
1204 int signum; 1267 sig_pending = 0;
1205 gotsig = 0;
1206 1268
1207 for (signum = signalmax; signum--; ) 1269 for (i = EV_NSIG - 1; i--; )
1208 if (signals [signum].gotsig) 1270 if (expect_false (signals [i].pending))
1209 ev_feed_signal_event (EV_A_ signum + 1); 1271 ev_feed_signal_event (EV_A_ i + 1);
1210 } 1272 }
1211 1273
1212#if EV_ASYNC_ENABLE 1274#if EV_ASYNC_ENABLE
1213 if (gotasync) 1275 if (async_pending)
1214 { 1276 {
1215 int i; 1277 async_pending = 0;
1216 gotasync = 0;
1217 1278
1218 for (i = asynccnt; i--; ) 1279 for (i = asynccnt; i--; )
1219 if (asyncs [i]->sent) 1280 if (asyncs [i]->sent)
1220 { 1281 {
1221 asyncs [i]->sent = 0; 1282 asyncs [i]->sent = 0;
1229 1290
1230static void 1291static void
1231ev_sighandler (int signum) 1292ev_sighandler (int signum)
1232{ 1293{
1233#if EV_MULTIPLICITY 1294#if EV_MULTIPLICITY
1234 struct ev_loop *loop = &default_loop_struct; 1295 EV_P = signals [signum - 1].loop;
1235#endif 1296#endif
1236 1297
1237#if _WIN32 1298#if _WIN32
1238 signal (signum, ev_sighandler); 1299 signal (signum, ev_sighandler);
1239#endif 1300#endif
1240 1301
1241 signals [signum - 1].gotsig = 1; 1302 signals [signum - 1].pending = 1;
1242 evpipe_write (EV_A_ &gotsig); 1303 evpipe_write (EV_A_ &sig_pending);
1243} 1304}
1244 1305
1245void noinline 1306void noinline
1246ev_feed_signal_event (EV_P_ int signum) 1307ev_feed_signal_event (EV_P_ int signum)
1247{ 1308{
1248 WL w; 1309 WL w;
1249 1310
1311 if (expect_false (signum <= 0 || signum > EV_NSIG))
1312 return;
1313
1314 --signum;
1315
1250#if EV_MULTIPLICITY 1316#if EV_MULTIPLICITY
1251 assert (("libev: feeding signal events is only supported in the default loop", loop == ev_default_loop_ptr)); 1317 /* it is permissible to try to feed a signal to the wrong loop */
1252#endif 1318 /* or, likely more useful, feeding a signal nobody is waiting for */
1253 1319
1254 --signum; 1320 if (expect_false (signals [signum].loop != EV_A))
1255
1256 if (signum < 0 || signum >= signalmax)
1257 return; 1321 return;
1322#endif
1258 1323
1259 signals [signum].gotsig = 0; 1324 signals [signum].pending = 0;
1260 1325
1261 for (w = signals [signum].head; w; w = w->next) 1326 for (w = signals [signum].head; w; w = w->next)
1262 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 1327 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
1263} 1328}
1264 1329
1265#if EV_USE_SIGNALFD 1330#if EV_USE_SIGNALFD
1266static void 1331static void
1267sigfdcb (EV_P_ ev_io *iow, int revents) 1332sigfdcb (EV_P_ ev_io *iow, int revents)
1268{ 1333{
1269 struct signalfd_siginfo si[4], *sip; 1334 struct signalfd_siginfo si[2], *sip; /* these structs are big */
1270 1335
1271 for (;;) 1336 for (;;)
1272 { 1337 {
1273 ssize_t res = read (sigfd, si, sizeof (si)); 1338 ssize_t res = read (sigfd, si, sizeof (si));
1274 1339
1508 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 1573 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
1509 have_monotonic = 1; 1574 have_monotonic = 1;
1510 } 1575 }
1511#endif 1576#endif
1512 1577
1578 /* pid check not overridable via env */
1579#ifndef _WIN32
1580 if (flags & EVFLAG_FORKCHECK)
1581 curpid = getpid ();
1582#endif
1583
1584 if (!(flags & EVFLAG_NOENV)
1585 && !enable_secure ()
1586 && getenv ("LIBEV_FLAGS"))
1587 flags = atoi (getenv ("LIBEV_FLAGS"));
1588
1513 ev_rt_now = ev_time (); 1589 ev_rt_now = ev_time ();
1514 mn_now = get_clock (); 1590 mn_now = get_clock ();
1515 now_floor = mn_now; 1591 now_floor = mn_now;
1516 rtmn_diff = ev_rt_now - mn_now; 1592 rtmn_diff = ev_rt_now - mn_now;
1517#if EV_MINIMAL < 2 1593#if EV_MINIMAL < 2
1520 1596
1521 io_blocktime = 0.; 1597 io_blocktime = 0.;
1522 timeout_blocktime = 0.; 1598 timeout_blocktime = 0.;
1523 backend = 0; 1599 backend = 0;
1524 backend_fd = -1; 1600 backend_fd = -1;
1525 gotasync = 0; 1601 sig_pending = 0;
1602#if EV_ASYNC_ENABLE
1603 async_pending = 0;
1604#endif
1526#if EV_USE_INOTIFY 1605#if EV_USE_INOTIFY
1527 fs_fd = -2; 1606 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
1528#endif 1607#endif
1529#if EV_USE_SIGNALFD 1608#if EV_USE_SIGNALFD
1530 sigfd = -2; 1609 sigfd = flags & EVFLAG_NOSIGFD ? -1 : -2;
1531#endif 1610#endif
1532
1533 /* pid check not overridable via env */
1534#ifndef _WIN32
1535 if (flags & EVFLAG_FORKCHECK)
1536 curpid = getpid ();
1537#endif
1538
1539 if (!(flags & EVFLAG_NOENV)
1540 && !enable_secure ()
1541 && getenv ("LIBEV_FLAGS"))
1542 flags = atoi (getenv ("LIBEV_FLAGS"));
1543 1611
1544 if (!(flags & 0x0000ffffU)) 1612 if (!(flags & 0x0000ffffU))
1545 flags |= ev_recommended_backends (); 1613 flags |= ev_recommended_backends ();
1546 1614
1547#if EV_USE_PORT 1615#if EV_USE_PORT
1583 close (evfd); 1651 close (evfd);
1584#endif 1652#endif
1585 1653
1586 if (evpipe [0] >= 0) 1654 if (evpipe [0] >= 0)
1587 { 1655 {
1588 close (evpipe [0]); 1656 EV_WIN32_CLOSE_FD (evpipe [0]);
1589 close (evpipe [1]); 1657 EV_WIN32_CLOSE_FD (evpipe [1]);
1590 } 1658 }
1591 } 1659 }
1592 1660
1593#if EV_USE_SIGNALFD 1661#if EV_USE_SIGNALFD
1594 if (ev_is_active (&sigfd_w)) 1662 if (ev_is_active (&sigfd_w))
1595 {
1596 /*ev_ref (EV_A);*/
1597 /*ev_io_stop (EV_A_ &sigfd_w);*/
1598
1599 close (sigfd); 1663 close (sigfd);
1600 }
1601#endif 1664#endif
1602 1665
1603#if EV_USE_INOTIFY 1666#if EV_USE_INOTIFY
1604 if (fs_fd >= 0) 1667 if (fs_fd >= 0)
1605 close (fs_fd); 1668 close (fs_fd);
1630#if EV_IDLE_ENABLE 1693#if EV_IDLE_ENABLE
1631 array_free (idle, [i]); 1694 array_free (idle, [i]);
1632#endif 1695#endif
1633 } 1696 }
1634 1697
1635 ev_free (anfds); anfdmax = 0; 1698 ev_free (anfds); anfds = 0; anfdmax = 0;
1636 1699
1637 /* have to use the microsoft-never-gets-it-right macro */ 1700 /* have to use the microsoft-never-gets-it-right macro */
1638 array_free (rfeed, EMPTY); 1701 array_free (rfeed, EMPTY);
1639 array_free (fdchange, EMPTY); 1702 array_free (fdchange, EMPTY);
1640 array_free (timer, EMPTY); 1703 array_free (timer, EMPTY);
1675 1738
1676 if (ev_is_active (&pipe_w)) 1739 if (ev_is_active (&pipe_w))
1677 { 1740 {
1678 /* this "locks" the handlers against writing to the pipe */ 1741 /* this "locks" the handlers against writing to the pipe */
1679 /* while we modify the fd vars */ 1742 /* while we modify the fd vars */
1680 gotsig = 1; 1743 sig_pending = 1;
1681#if EV_ASYNC_ENABLE 1744#if EV_ASYNC_ENABLE
1682 gotasync = 1; 1745 async_pending = 1;
1683#endif 1746#endif
1684 1747
1685 ev_ref (EV_A); 1748 ev_ref (EV_A);
1686 ev_io_stop (EV_A_ &pipe_w); 1749 ev_io_stop (EV_A_ &pipe_w);
1687 1750
1690 close (evfd); 1753 close (evfd);
1691#endif 1754#endif
1692 1755
1693 if (evpipe [0] >= 0) 1756 if (evpipe [0] >= 0)
1694 { 1757 {
1695 close (evpipe [0]); 1758 EV_WIN32_CLOSE_FD (evpipe [0]);
1696 close (evpipe [1]); 1759 EV_WIN32_CLOSE_FD (evpipe [1]);
1697 } 1760 }
1698 1761
1699 evpipe_init (EV_A); 1762 evpipe_init (EV_A);
1700 /* now iterate over everything, in case we missed something */ 1763 /* now iterate over everything, in case we missed something */
1701 pipecb (EV_A_ &pipe_w, EV_READ); 1764 pipecb (EV_A_ &pipe_w, EV_READ);
1707#if EV_MULTIPLICITY 1770#if EV_MULTIPLICITY
1708 1771
1709struct ev_loop * 1772struct ev_loop *
1710ev_loop_new (unsigned int flags) 1773ev_loop_new (unsigned int flags)
1711{ 1774{
1712 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 1775 EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
1713 1776
1714 memset (loop, 0, sizeof (struct ev_loop)); 1777 memset (EV_A, 0, sizeof (struct ev_loop));
1715 loop_init (EV_A_ flags); 1778 loop_init (EV_A_ flags);
1716 1779
1717 if (ev_backend (EV_A)) 1780 if (ev_backend (EV_A))
1718 return loop; 1781 return EV_A;
1719 1782
1720 return 0; 1783 return 0;
1721} 1784}
1722 1785
1723void 1786void
1827 assert (checkmax >= checkcnt); 1890 assert (checkmax >= checkcnt);
1828 array_verify (EV_A_ (W *)checks, checkcnt); 1891 array_verify (EV_A_ (W *)checks, checkcnt);
1829 1892
1830# if 0 1893# if 0
1831 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 1894 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
1832 for (signum = signalmax; signum--; ) if (signals [signum].gotsig) 1895 for (signum = EV_NSIG; signum--; ) if (signals [signum].pending)
1833# endif 1896# endif
1834#endif 1897#endif
1835} 1898}
1836#endif 1899#endif
1837 1900
1844#endif 1907#endif
1845{ 1908{
1846 if (!ev_default_loop_ptr) 1909 if (!ev_default_loop_ptr)
1847 { 1910 {
1848#if EV_MULTIPLICITY 1911#if EV_MULTIPLICITY
1849 struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct; 1912 EV_P = ev_default_loop_ptr = &default_loop_struct;
1850#else 1913#else
1851 ev_default_loop_ptr = 1; 1914 ev_default_loop_ptr = 1;
1852#endif 1915#endif
1853 1916
1854 loop_init (EV_A_ flags); 1917 loop_init (EV_A_ flags);
1871 1934
1872void 1935void
1873ev_default_destroy (void) 1936ev_default_destroy (void)
1874{ 1937{
1875#if EV_MULTIPLICITY 1938#if EV_MULTIPLICITY
1876 struct ev_loop *loop = ev_default_loop_ptr; 1939 EV_P = ev_default_loop_ptr;
1877#endif 1940#endif
1878 1941
1879 ev_default_loop_ptr = 0; 1942 ev_default_loop_ptr = 0;
1880 1943
1881#ifndef _WIN32 1944#ifndef _WIN32
1888 1951
1889void 1952void
1890ev_default_fork (void) 1953ev_default_fork (void)
1891{ 1954{
1892#if EV_MULTIPLICITY 1955#if EV_MULTIPLICITY
1893 struct ev_loop *loop = ev_default_loop_ptr; 1956 EV_P = ev_default_loop_ptr;
1894#endif 1957#endif
1895 1958
1896 postfork = 1; /* must be in line with ev_loop_fork */ 1959 postfork = 1; /* must be in line with ev_loop_fork */
1897} 1960}
1898 1961
2364inline_size void 2427inline_size void
2365wlist_del (WL *head, WL elem) 2428wlist_del (WL *head, WL elem)
2366{ 2429{
2367 while (*head) 2430 while (*head)
2368 { 2431 {
2369 if (*head == elem) 2432 if (expect_true (*head == elem))
2370 { 2433 {
2371 *head = elem->next; 2434 *head = elem->next;
2372 return; 2435 break;
2373 } 2436 }
2374 2437
2375 head = &(*head)->next; 2438 head = &(*head)->next;
2376 } 2439 }
2377} 2440}
2631#endif 2694#endif
2632 2695
2633void noinline 2696void noinline
2634ev_signal_start (EV_P_ ev_signal *w) 2697ev_signal_start (EV_P_ ev_signal *w)
2635{ 2698{
2636#if EV_MULTIPLICITY
2637 assert (("libev: signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
2638#endif
2639 if (expect_false (ev_is_active (w))) 2699 if (expect_false (ev_is_active (w)))
2640 return; 2700 return;
2641 2701
2642 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0)); 2702 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG));
2703
2704#if EV_MULTIPLICITY
2705 assert (("libev: a signal must not be attached to two different loops",
2706 !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop));
2707
2708 signals [w->signum - 1].loop = EV_A;
2709#endif
2643 2710
2644 EV_FREQUENT_CHECK; 2711 EV_FREQUENT_CHECK;
2645 2712
2646#if EV_USE_SIGNALFD 2713#if EV_USE_SIGNALFD
2647 if (sigfd == -2) 2714 if (sigfd == -2)
2669 sigaddset (&sigfd_set, w->signum); 2736 sigaddset (&sigfd_set, w->signum);
2670 sigprocmask (SIG_BLOCK, &sigfd_set, 0); 2737 sigprocmask (SIG_BLOCK, &sigfd_set, 0);
2671 2738
2672 signalfd (sigfd, &sigfd_set, 0); 2739 signalfd (sigfd, &sigfd_set, 0);
2673 } 2740 }
2674 else
2675#endif 2741#endif
2676 evpipe_init (EV_A);
2677
2678 {
2679#ifndef _WIN32
2680 sigset_t full, prev;
2681 sigfillset (&full);
2682 sigprocmask (SIG_SETMASK, &full, &prev);
2683#endif
2684
2685 array_needsize (ANSIG, signals, signalmax, w->signum, array_init_zero);
2686
2687#ifndef _WIN32
2688 if (sigfd < 0)/*TODO*/
2689 sigdelset (&prev, w->signum);
2690 sigprocmask (SIG_SETMASK, &prev, 0);
2691#endif
2692 }
2693 2742
2694 ev_start (EV_A_ (W)w, 1); 2743 ev_start (EV_A_ (W)w, 1);
2695 wlist_add (&signals [w->signum - 1].head, (WL)w); 2744 wlist_add (&signals [w->signum - 1].head, (WL)w);
2696 2745
2697 if (!((WL)w)->next) 2746 if (!((WL)w)->next)
2747# if EV_USE_SIGNALFD
2748 if (sigfd < 0) /*TODO*/
2749# endif
2698 { 2750 {
2699#if _WIN32 2751# if _WIN32
2752 evpipe_init (EV_A);
2753
2700 signal (w->signum, ev_sighandler); 2754 signal (w->signum, ev_sighandler);
2701#else 2755# else
2702 if (sigfd < 0) /*TODO*/
2703 {
2704 struct sigaction sa = { }; 2756 struct sigaction sa;
2757
2758 evpipe_init (EV_A);
2759
2705 sa.sa_handler = ev_sighandler; 2760 sa.sa_handler = ev_sighandler;
2706 sigfillset (&sa.sa_mask); 2761 sigfillset (&sa.sa_mask);
2707 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 2762 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
2708 sigaction (w->signum, &sa, 0); 2763 sigaction (w->signum, &sa, 0);
2764
2765 sigemptyset (&sa.sa_mask);
2766 sigaddset (&sa.sa_mask, w->signum);
2767 sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0);
2768#endif
2709 } 2769 }
2710#endif
2711 }
2712 2770
2713 EV_FREQUENT_CHECK; 2771 EV_FREQUENT_CHECK;
2714} 2772}
2715 2773
2716void noinline 2774void noinline
2724 2782
2725 wlist_del (&signals [w->signum - 1].head, (WL)w); 2783 wlist_del (&signals [w->signum - 1].head, (WL)w);
2726 ev_stop (EV_A_ (W)w); 2784 ev_stop (EV_A_ (W)w);
2727 2785
2728 if (!signals [w->signum - 1].head) 2786 if (!signals [w->signum - 1].head)
2787 {
2788#if EV_MULTIPLICITY
2789 signals [w->signum - 1].loop = 0; /* unattach from signal */
2790#endif
2729#if EV_USE_SIGNALFD 2791#if EV_USE_SIGNALFD
2730 if (sigfd >= 0) 2792 if (sigfd >= 0)
2731 { 2793 {
2732 sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D 2794 sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D
2733 sigdelset (&sigfd_set, w->signum); 2795 sigdelset (&sigfd_set, w->signum);
2734 signalfd (sigfd, &sigfd_set, 0); 2796 signalfd (sigfd, &sigfd_set, 0);
2735 sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D 2797 sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D
2736 /*TODO: maybe unblock signal? */ 2798 /*TODO: maybe unblock signal? */
2737 } 2799 }
2738 else 2800 else
2739#endif 2801#endif
2740 signal (w->signum, SIG_DFL); 2802 signal (w->signum, SIG_DFL);
2803 }
2741 2804
2742 EV_FREQUENT_CHECK; 2805 EV_FREQUENT_CHECK;
2743} 2806}
2744 2807
2745void 2808void
2825 } 2888 }
2826 } 2889 }
2827 2890
2828 if (w->wd >= 0) 2891 if (w->wd >= 0)
2829 { 2892 {
2893 struct statfs sfs;
2894
2830 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); 2895 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w);
2831 2896
2832 /* now local changes will be tracked by inotify, but remote changes won't */ 2897 /* now local changes will be tracked by inotify, but remote changes won't */
2833 /* unless the filesystem it known to be local, we therefore still poll */ 2898 /* unless the filesystem is known to be local, we therefore still poll */
2834 /* also do poll on <2.6.25, but with normal frequency */ 2899 /* also do poll on <2.6.25, but with normal frequency */
2835 struct statfs sfs;
2836 2900
2837 if (fs_2625 && !statfs (w->path, &sfs)) 2901 if (fs_2625 && !statfs (w->path, &sfs))
2838 if (sfs.f_type == 0x1373 /* devfs */ 2902 if (sfs.f_type == 0x1373 /* devfs */
2839 || sfs.f_type == 0xEF53 /* ext2/3 */ 2903 || sfs.f_type == 0xEF53 /* ext2/3 */
2840 || sfs.f_type == 0x3153464a /* jfs */ 2904 || sfs.f_type == 0x3153464a /* jfs */
2929 return; 2993 return;
2930 2994
2931 fs_2625 = 1; 2995 fs_2625 = 1;
2932} 2996}
2933 2997
2998inline_size int
2999infy_newfd (void)
3000{
3001#if defined (IN_CLOEXEC) && defined (IN_NONBLOCK)
3002 int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK);
3003 if (fd >= 0)
3004 return fd;
3005#endif
3006 return inotify_init ();
3007}
3008
2934inline_size void 3009inline_size void
2935infy_init (EV_P) 3010infy_init (EV_P)
2936{ 3011{
2937 if (fs_fd != -2) 3012 if (fs_fd != -2)
2938 return; 3013 return;
2939 3014
2940 fs_fd = -1; 3015 fs_fd = -1;
2941 3016
2942 check_2625 (EV_A); 3017 check_2625 (EV_A);
2943 3018
2944 fs_fd = inotify_init (); 3019 fs_fd = infy_newfd ();
2945 3020
2946 if (fs_fd >= 0) 3021 if (fs_fd >= 0)
2947 { 3022 {
3023 fd_intern (fs_fd);
2948 ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ); 3024 ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ);
2949 ev_set_priority (&fs_w, EV_MAXPRI); 3025 ev_set_priority (&fs_w, EV_MAXPRI);
2950 ev_io_start (EV_A_ &fs_w); 3026 ev_io_start (EV_A_ &fs_w);
3027 ev_unref (EV_A);
2951 } 3028 }
2952} 3029}
2953 3030
2954inline_size void 3031inline_size void
2955infy_fork (EV_P) 3032infy_fork (EV_P)
2957 int slot; 3034 int slot;
2958 3035
2959 if (fs_fd < 0) 3036 if (fs_fd < 0)
2960 return; 3037 return;
2961 3038
3039 ev_ref (EV_A);
3040 ev_io_stop (EV_A_ &fs_w);
2962 close (fs_fd); 3041 close (fs_fd);
2963 fs_fd = inotify_init (); 3042 fs_fd = infy_newfd ();
3043
3044 if (fs_fd >= 0)
3045 {
3046 fd_intern (fs_fd);
3047 ev_io_set (&fs_w, fs_fd, EV_READ);
3048 ev_io_start (EV_A_ &fs_w);
3049 ev_unref (EV_A);
3050 }
2964 3051
2965 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot) 3052 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot)
2966 { 3053 {
2967 WL w_ = fs_hash [slot].head; 3054 WL w_ = fs_hash [slot].head;
2968 fs_hash [slot].head = 0; 3055 fs_hash [slot].head = 0;
3225embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents) 3312embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents)
3226{ 3313{
3227 ev_embed *w = (ev_embed *)(((char *)prepare) - offsetof (ev_embed, prepare)); 3314 ev_embed *w = (ev_embed *)(((char *)prepare) - offsetof (ev_embed, prepare));
3228 3315
3229 { 3316 {
3230 struct ev_loop *loop = w->other; 3317 EV_P = w->other;
3231 3318
3232 while (fdchangecnt) 3319 while (fdchangecnt)
3233 { 3320 {
3234 fd_reify (EV_A); 3321 fd_reify (EV_A);
3235 ev_loop (EV_A_ EVLOOP_NONBLOCK); 3322 ev_loop (EV_A_ EVLOOP_NONBLOCK);
3243 ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork)); 3330 ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork));
3244 3331
3245 ev_embed_stop (EV_A_ w); 3332 ev_embed_stop (EV_A_ w);
3246 3333
3247 { 3334 {
3248 struct ev_loop *loop = w->other; 3335 EV_P = w->other;
3249 3336
3250 ev_loop_fork (EV_A); 3337 ev_loop_fork (EV_A);
3251 ev_loop (EV_A_ EVLOOP_NONBLOCK); 3338 ev_loop (EV_A_ EVLOOP_NONBLOCK);
3252 } 3339 }
3253 3340
3267{ 3354{
3268 if (expect_false (ev_is_active (w))) 3355 if (expect_false (ev_is_active (w)))
3269 return; 3356 return;
3270 3357
3271 { 3358 {
3272 struct ev_loop *loop = w->other; 3359 EV_P = w->other;
3273 assert (("libev: loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); 3360 assert (("libev: loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
3274 ev_io_init (&w->io, embed_io_cb, backend_fd, EV_READ); 3361 ev_io_init (&w->io, embed_io_cb, backend_fd, EV_READ);
3275 } 3362 }
3276 3363
3277 EV_FREQUENT_CHECK; 3364 EV_FREQUENT_CHECK;
3389 3476
3390void 3477void
3391ev_async_send (EV_P_ ev_async *w) 3478ev_async_send (EV_P_ ev_async *w)
3392{ 3479{
3393 w->sent = 1; 3480 w->sent = 1;
3394 evpipe_write (EV_A_ &gotasync); 3481 evpipe_write (EV_A_ &async_pending);
3395} 3482}
3396#endif 3483#endif
3397 3484
3398/*****************************************************************************/ 3485/*****************************************************************************/
3399 3486
3548 if (types & EV_CHECK) 3635 if (types & EV_CHECK)
3549 for (i = checkcnt; i--; ) 3636 for (i = checkcnt; i--; )
3550 cb (EV_A_ EV_CHECK, checks [i]); 3637 cb (EV_A_ EV_CHECK, checks [i]);
3551 3638
3552 if (types & EV_SIGNAL) 3639 if (types & EV_SIGNAL)
3553 for (i = 0; i < signalmax; ++i) 3640 for (i = 0; i < EV_NSIG - 1; ++i)
3554 for (wl = signals [i].head; wl; ) 3641 for (wl = signals [i].head; wl; )
3555 { 3642 {
3556 wn = wl->next; 3643 wn = wl->next;
3557 cb (EV_A_ EV_SIGNAL, wl); 3644 cb (EV_A_ EV_SIGNAL, wl);
3558 wl = wn; 3645 wl = wn;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines