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

Comparing libev/ev.c (file contents):
Revision 1.305 by root, Sun Jul 19 03:49:04 2009 UTC vs.
Revision 1.318 by root, Tue Nov 17 00:22:28 2009 UTC

210#elif defined (_sys_nsig) 210#elif defined (_sys_nsig)
211# define EV_NSIG (_sys_nsig) /* Solaris 2.5 */ 211# define EV_NSIG (_sys_nsig) /* Solaris 2.5 */
212#else 212#else
213# error "unable to find value for NSIG, please report" 213# error "unable to find value for NSIG, please report"
214/* to make it compile regardless, just remove the above line */ 214/* to make it compile regardless, just remove the above line */
215# define EV_NSIG 64 215# define EV_NSIG 65
216#endif 216#endif
217 217
218/* Default to some arbitrary number that's big enough to get most
219 of the common signals.
220*/
221#ifndef NSIG
222# define NSIG 50
223#endif
224/* <-- NSIG logic from Configure */
225#ifndef EV_USE_CLOCK_SYSCALL 218#ifndef EV_USE_CLOCK_SYSCALL
226# if __linux && __GLIBC__ >= 2 219# if __linux && __GLIBC__ >= 2
227# define EV_USE_CLOCK_SYSCALL 1 220# define EV_USE_CLOCK_SYSCALL 1
228# else 221# else
229# define EV_USE_CLOCK_SYSCALL 0 222# define EV_USE_CLOCK_SYSCALL 0
309# define EV_USE_EVENTFD 0 302# define EV_USE_EVENTFD 0
310# endif 303# endif
311#endif 304#endif
312 305
313#ifndef EV_USE_SIGNALFD 306#ifndef EV_USE_SIGNALFD
314# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9)) 307# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
315# define EV_USE_SIGNALFD 1 308# define EV_USE_SIGNALFD 1
316# else 309# else
317# define EV_USE_SIGNALFD 0 310# define EV_USE_SIGNALFD 0
318# endif 311# endif
319#endif 312#endif
393# include <stdint.h> 386# include <stdint.h>
394# ifndef EFD_NONBLOCK 387# ifndef EFD_NONBLOCK
395# define EFD_NONBLOCK O_NONBLOCK 388# define EFD_NONBLOCK O_NONBLOCK
396# endif 389# endif
397# ifndef EFD_CLOEXEC 390# ifndef EFD_CLOEXEC
391# ifdef O_CLOEXEC
398# define EFD_CLOEXEC O_CLOEXEC 392# define EFD_CLOEXEC O_CLOEXEC
393# else
394# define EFD_CLOEXEC 02000000
395# endif
399# endif 396# endif
400# ifdef __cplusplus 397# ifdef __cplusplus
401extern "C" { 398extern "C" {
402# endif 399# endif
403int eventfd (unsigned int initval, int flags); 400int eventfd (unsigned int initval, int flags);
405} 402}
406# endif 403# endif
407#endif 404#endif
408 405
409#if EV_USE_SIGNALFD 406#if EV_USE_SIGNALFD
410# 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
411#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
412 434
413/**/ 435/**/
414 436
415#if EV_VERIFY >= 3 437#if EV_VERIFY >= 3
416# define EV_FREQUENT_CHECK ev_loop_verify (EV_A) 438# define EV_FREQUENT_CHECK ev_loop_verify (EV_A)
428 */ 450 */
429#define TIME_EPSILON 0.0001220703125 /* 1/8192 */ 451#define TIME_EPSILON 0.0001220703125 /* 1/8192 */
430 452
431#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) */
432#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) */
433/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds, TODO */
434 455
435#if __GNUC__ >= 4 456#if __GNUC__ >= 4
436# define expect(expr,value) __builtin_expect ((expr),(value)) 457# define expect(expr,value) __builtin_expect ((expr),(value))
437# define noinline __attribute__ ((noinline)) 458# define noinline __attribute__ ((noinline))
438#else 459#else
477static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */ 498static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */
478#endif 499#endif
479 500
480#if EV_USE_MONOTONIC 501#if EV_USE_MONOTONIC
481static 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)
482#endif 513#endif
483 514
484#ifdef _WIN32 515#ifdef _WIN32
485# include "ev_win32.c" 516# include "ev_win32.c"
486#endif 517#endif
879 910
880#if EV_SELECT_IS_WINSOCKET 911#if EV_SELECT_IS_WINSOCKET
881 if (events) 912 if (events)
882 { 913 {
883 unsigned long arg; 914 unsigned long arg;
884 #ifdef EV_FD_TO_WIN32_HANDLE
885 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); 915 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
886 #else
887 anfd->handle = _get_osfhandle (fd);
888 #endif
889 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));
890 } 917 }
891#endif 918#endif
892 919
893 { 920 {
964 991
965 for (fd = anfdmax; fd--; ) 992 for (fd = anfdmax; fd--; )
966 if (anfds [fd].events) 993 if (anfds [fd].events)
967 { 994 {
968 fd_kill (EV_A_ fd); 995 fd_kill (EV_A_ fd);
969 return; 996 break;
970 } 997 }
971} 998}
972 999
973/* 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 */
974static void noinline 1001static void noinline
1064 1091
1065 for (;;) 1092 for (;;)
1066 { 1093 {
1067 int c = k << 1; 1094 int c = k << 1;
1068 1095
1069 if (c > N + HEAP0 - 1) 1096 if (c >= N + HEAP0)
1070 break; 1097 break;
1071 1098
1072 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])
1073 ? 1 : 0; 1100 ? 1 : 0;
1074 1101
1110 1137
1111/* move an element suitably so it is in a correct place */ 1138/* move an element suitably so it is in a correct place */
1112inline_size void 1139inline_size void
1113adjustheap (ANHE *heap, int N, int k) 1140adjustheap (ANHE *heap, int N, int k)
1114{ 1141{
1115 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)]))
1116 upheap (heap, k); 1143 upheap (heap, k);
1117 else 1144 else
1118 downheap (heap, N, k); 1145 downheap (heap, N, k);
1119} 1146}
1120 1147
1133/*****************************************************************************/ 1160/*****************************************************************************/
1134 1161
1135/* associate signal watchers to a signal signal */ 1162/* associate signal watchers to a signal signal */
1136typedef struct 1163typedef struct
1137{ 1164{
1165 EV_ATOMIC_T pending;
1166#if EV_MULTIPLICITY
1167 EV_P;
1168#endif
1138 WL head; 1169 WL head;
1139 EV_ATOMIC_T gotsig;
1140} ANSIG; 1170} ANSIG;
1141 1171
1142static ANSIG *signals; 1172static ANSIG signals [EV_NSIG - 1];
1143static int signalmax;
1144
1145static EV_ATOMIC_T gotsig;
1146 1173
1147/*****************************************************************************/ 1174/*****************************************************************************/
1148 1175
1149/* used to prepare libev internal fd's */ 1176/* used to prepare libev internal fd's */
1150/* this is not fork-safe */ 1177/* this is not fork-safe */
1218/* called whenever the libev signal pipe */ 1245/* called whenever the libev signal pipe */
1219/* got some events (signal, async) */ 1246/* got some events (signal, async) */
1220static void 1247static void
1221pipecb (EV_P_ ev_io *iow, int revents) 1248pipecb (EV_P_ ev_io *iow, int revents)
1222{ 1249{
1250 int i;
1251
1223#if EV_USE_EVENTFD 1252#if EV_USE_EVENTFD
1224 if (evfd >= 0) 1253 if (evfd >= 0)
1225 { 1254 {
1226 uint64_t counter; 1255 uint64_t counter;
1227 read (evfd, &counter, sizeof (uint64_t)); 1256 read (evfd, &counter, sizeof (uint64_t));
1231 { 1260 {
1232 char dummy; 1261 char dummy;
1233 read (evpipe [0], &dummy, 1); 1262 read (evpipe [0], &dummy, 1);
1234 } 1263 }
1235 1264
1236 if (gotsig && ev_is_default_loop (EV_A)) 1265 if (sig_pending)
1237 { 1266 {
1238 int signum; 1267 sig_pending = 0;
1239 gotsig = 0;
1240 1268
1241 for (signum = signalmax; signum--; ) 1269 for (i = EV_NSIG - 1; i--; )
1242 if (signals [signum].gotsig) 1270 if (expect_false (signals [i].pending))
1243 ev_feed_signal_event (EV_A_ signum + 1); 1271 ev_feed_signal_event (EV_A_ i + 1);
1244 } 1272 }
1245 1273
1246#if EV_ASYNC_ENABLE 1274#if EV_ASYNC_ENABLE
1247 if (gotasync) 1275 if (async_pending)
1248 { 1276 {
1249 int i; 1277 async_pending = 0;
1250 gotasync = 0;
1251 1278
1252 for (i = asynccnt; i--; ) 1279 for (i = asynccnt; i--; )
1253 if (asyncs [i]->sent) 1280 if (asyncs [i]->sent)
1254 { 1281 {
1255 asyncs [i]->sent = 0; 1282 asyncs [i]->sent = 0;
1263 1290
1264static void 1291static void
1265ev_sighandler (int signum) 1292ev_sighandler (int signum)
1266{ 1293{
1267#if EV_MULTIPLICITY 1294#if EV_MULTIPLICITY
1268 struct ev_loop *loop = &default_loop_struct; 1295 EV_P = signals [signum - 1].loop;
1269#endif 1296#endif
1270 1297
1271#if _WIN32 1298#if _WIN32
1272 signal (signum, ev_sighandler); 1299 signal (signum, ev_sighandler);
1273#endif 1300#endif
1274 1301
1275 signals [signum - 1].gotsig = 1; 1302 signals [signum - 1].pending = 1;
1276 evpipe_write (EV_A_ &gotsig); 1303 evpipe_write (EV_A_ &sig_pending);
1277} 1304}
1278 1305
1279void noinline 1306void noinline
1280ev_feed_signal_event (EV_P_ int signum) 1307ev_feed_signal_event (EV_P_ int signum)
1281{ 1308{
1282 WL w; 1309 WL w;
1283 1310
1311 if (expect_false (signum <= 0 || signum > EV_NSIG))
1312 return;
1313
1314 --signum;
1315
1284#if EV_MULTIPLICITY 1316#if EV_MULTIPLICITY
1285 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 */
1286#endif 1318 /* or, likely more useful, feeding a signal nobody is waiting for */
1287 1319
1288 --signum; 1320 if (expect_false (signals [signum].loop != EV_A))
1289
1290 if (signum < 0 || signum >= signalmax)
1291 return; 1321 return;
1322#endif
1292 1323
1293 signals [signum].gotsig = 0; 1324 signals [signum].pending = 0;
1294 1325
1295 for (w = signals [signum].head; w; w = w->next) 1326 for (w = signals [signum].head; w; w = w->next)
1296 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 1327 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
1297} 1328}
1298 1329
1299#if EV_USE_SIGNALFD 1330#if EV_USE_SIGNALFD
1300static void 1331static void
1301sigfdcb (EV_P_ ev_io *iow, int revents) 1332sigfdcb (EV_P_ ev_io *iow, int revents)
1302{ 1333{
1303 struct signalfd_siginfo si[4], *sip; 1334 struct signalfd_siginfo si[2], *sip; /* these structs are big */
1304 1335
1305 for (;;) 1336 for (;;)
1306 { 1337 {
1307 ssize_t res = read (sigfd, si, sizeof (si)); 1338 ssize_t res = read (sigfd, si, sizeof (si));
1308 1339
1542 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 1573 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
1543 have_monotonic = 1; 1574 have_monotonic = 1;
1544 } 1575 }
1545#endif 1576#endif
1546 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
1547 ev_rt_now = ev_time (); 1589 ev_rt_now = ev_time ();
1548 mn_now = get_clock (); 1590 mn_now = get_clock ();
1549 now_floor = mn_now; 1591 now_floor = mn_now;
1550 rtmn_diff = ev_rt_now - mn_now; 1592 rtmn_diff = ev_rt_now - mn_now;
1551#if EV_MINIMAL < 2 1593#if EV_MINIMAL < 2
1554 1596
1555 io_blocktime = 0.; 1597 io_blocktime = 0.;
1556 timeout_blocktime = 0.; 1598 timeout_blocktime = 0.;
1557 backend = 0; 1599 backend = 0;
1558 backend_fd = -1; 1600 backend_fd = -1;
1559 gotasync = 0; 1601 sig_pending = 0;
1602#if EV_ASYNC_ENABLE
1603 async_pending = 0;
1604#endif
1560#if EV_USE_INOTIFY 1605#if EV_USE_INOTIFY
1561 fs_fd = -2; 1606 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
1562#endif 1607#endif
1563#if EV_USE_SIGNALFD 1608#if EV_USE_SIGNALFD
1564 sigfd = -2; 1609 sigfd = flags & EVFLAG_NOSIGFD ? -1 : -2;
1565#endif 1610#endif
1566
1567 /* pid check not overridable via env */
1568#ifndef _WIN32
1569 if (flags & EVFLAG_FORKCHECK)
1570 curpid = getpid ();
1571#endif
1572
1573 if (!(flags & EVFLAG_NOENV)
1574 && !enable_secure ()
1575 && getenv ("LIBEV_FLAGS"))
1576 flags = atoi (getenv ("LIBEV_FLAGS"));
1577 1611
1578 if (!(flags & 0x0000ffffU)) 1612 if (!(flags & 0x0000ffffU))
1579 flags |= ev_recommended_backends (); 1613 flags |= ev_recommended_backends ();
1580 1614
1581#if EV_USE_PORT 1615#if EV_USE_PORT
1617 close (evfd); 1651 close (evfd);
1618#endif 1652#endif
1619 1653
1620 if (evpipe [0] >= 0) 1654 if (evpipe [0] >= 0)
1621 { 1655 {
1622 close (evpipe [0]); 1656 EV_WIN32_CLOSE_FD (evpipe [0]);
1623 close (evpipe [1]); 1657 EV_WIN32_CLOSE_FD (evpipe [1]);
1624 } 1658 }
1625 } 1659 }
1626 1660
1627#if EV_USE_SIGNALFD 1661#if EV_USE_SIGNALFD
1628 if (ev_is_active (&sigfd_w)) 1662 if (ev_is_active (&sigfd_w))
1629 {
1630 /*ev_ref (EV_A);*/
1631 /*ev_io_stop (EV_A_ &sigfd_w);*/
1632
1633 close (sigfd); 1663 close (sigfd);
1634 }
1635#endif 1664#endif
1636 1665
1637#if EV_USE_INOTIFY 1666#if EV_USE_INOTIFY
1638 if (fs_fd >= 0) 1667 if (fs_fd >= 0)
1639 close (fs_fd); 1668 close (fs_fd);
1709 1738
1710 if (ev_is_active (&pipe_w)) 1739 if (ev_is_active (&pipe_w))
1711 { 1740 {
1712 /* this "locks" the handlers against writing to the pipe */ 1741 /* this "locks" the handlers against writing to the pipe */
1713 /* while we modify the fd vars */ 1742 /* while we modify the fd vars */
1714 gotsig = 1; 1743 sig_pending = 1;
1715#if EV_ASYNC_ENABLE 1744#if EV_ASYNC_ENABLE
1716 gotasync = 1; 1745 async_pending = 1;
1717#endif 1746#endif
1718 1747
1719 ev_ref (EV_A); 1748 ev_ref (EV_A);
1720 ev_io_stop (EV_A_ &pipe_w); 1749 ev_io_stop (EV_A_ &pipe_w);
1721 1750
1724 close (evfd); 1753 close (evfd);
1725#endif 1754#endif
1726 1755
1727 if (evpipe [0] >= 0) 1756 if (evpipe [0] >= 0)
1728 { 1757 {
1729 close (evpipe [0]); 1758 EV_WIN32_CLOSE_FD (evpipe [0]);
1730 close (evpipe [1]); 1759 EV_WIN32_CLOSE_FD (evpipe [1]);
1731 } 1760 }
1732 1761
1733 evpipe_init (EV_A); 1762 evpipe_init (EV_A);
1734 /* now iterate over everything, in case we missed something */ 1763 /* now iterate over everything, in case we missed something */
1735 pipecb (EV_A_ &pipe_w, EV_READ); 1764 pipecb (EV_A_ &pipe_w, EV_READ);
1741#if EV_MULTIPLICITY 1770#if EV_MULTIPLICITY
1742 1771
1743struct ev_loop * 1772struct ev_loop *
1744ev_loop_new (unsigned int flags) 1773ev_loop_new (unsigned int flags)
1745{ 1774{
1746 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));
1747 1776
1748 memset (loop, 0, sizeof (struct ev_loop)); 1777 memset (EV_A, 0, sizeof (struct ev_loop));
1749 loop_init (EV_A_ flags); 1778 loop_init (EV_A_ flags);
1750 1779
1751 if (ev_backend (EV_A)) 1780 if (ev_backend (EV_A))
1752 return loop; 1781 return EV_A;
1753 1782
1754 return 0; 1783 return 0;
1755} 1784}
1756 1785
1757void 1786void
1861 assert (checkmax >= checkcnt); 1890 assert (checkmax >= checkcnt);
1862 array_verify (EV_A_ (W *)checks, checkcnt); 1891 array_verify (EV_A_ (W *)checks, checkcnt);
1863 1892
1864# if 0 1893# if 0
1865 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)
1866 for (signum = signalmax; signum--; ) if (signals [signum].gotsig) 1895 for (signum = EV_NSIG; signum--; ) if (signals [signum].pending)
1867# endif 1896# endif
1868#endif 1897#endif
1869} 1898}
1870#endif 1899#endif
1871 1900
1878#endif 1907#endif
1879{ 1908{
1880 if (!ev_default_loop_ptr) 1909 if (!ev_default_loop_ptr)
1881 { 1910 {
1882#if EV_MULTIPLICITY 1911#if EV_MULTIPLICITY
1883 struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct; 1912 EV_P = ev_default_loop_ptr = &default_loop_struct;
1884#else 1913#else
1885 ev_default_loop_ptr = 1; 1914 ev_default_loop_ptr = 1;
1886#endif 1915#endif
1887 1916
1888 loop_init (EV_A_ flags); 1917 loop_init (EV_A_ flags);
1905 1934
1906void 1935void
1907ev_default_destroy (void) 1936ev_default_destroy (void)
1908{ 1937{
1909#if EV_MULTIPLICITY 1938#if EV_MULTIPLICITY
1910 struct ev_loop *loop = ev_default_loop_ptr; 1939 EV_P = ev_default_loop_ptr;
1911#endif 1940#endif
1912 1941
1913 ev_default_loop_ptr = 0; 1942 ev_default_loop_ptr = 0;
1914 1943
1915#ifndef _WIN32 1944#ifndef _WIN32
1922 1951
1923void 1952void
1924ev_default_fork (void) 1953ev_default_fork (void)
1925{ 1954{
1926#if EV_MULTIPLICITY 1955#if EV_MULTIPLICITY
1927 struct ev_loop *loop = ev_default_loop_ptr; 1956 EV_P = ev_default_loop_ptr;
1928#endif 1957#endif
1929 1958
1930 postfork = 1; /* must be in line with ev_loop_fork */ 1959 postfork = 1; /* must be in line with ev_loop_fork */
1931} 1960}
1932 1961
2398inline_size void 2427inline_size void
2399wlist_del (WL *head, WL elem) 2428wlist_del (WL *head, WL elem)
2400{ 2429{
2401 while (*head) 2430 while (*head)
2402 { 2431 {
2403 if (*head == elem) 2432 if (expect_true (*head == elem))
2404 { 2433 {
2405 *head = elem->next; 2434 *head = elem->next;
2406 return; 2435 break;
2407 } 2436 }
2408 2437
2409 head = &(*head)->next; 2438 head = &(*head)->next;
2410 } 2439 }
2411} 2440}
2665#endif 2694#endif
2666 2695
2667void noinline 2696void noinline
2668ev_signal_start (EV_P_ ev_signal *w) 2697ev_signal_start (EV_P_ ev_signal *w)
2669{ 2698{
2670#if EV_MULTIPLICITY
2671 assert (("libev: signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
2672#endif
2673 if (expect_false (ev_is_active (w))) 2699 if (expect_false (ev_is_active (w)))
2674 return; 2700 return;
2675 2701
2676 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
2677 2710
2678 EV_FREQUENT_CHECK; 2711 EV_FREQUENT_CHECK;
2679 2712
2680#if EV_USE_SIGNALFD 2713#if EV_USE_SIGNALFD
2681 if (sigfd == -2) 2714 if (sigfd == -2)
2703 sigaddset (&sigfd_set, w->signum); 2736 sigaddset (&sigfd_set, w->signum);
2704 sigprocmask (SIG_BLOCK, &sigfd_set, 0); 2737 sigprocmask (SIG_BLOCK, &sigfd_set, 0);
2705 2738
2706 signalfd (sigfd, &sigfd_set, 0); 2739 signalfd (sigfd, &sigfd_set, 0);
2707 } 2740 }
2708 else
2709#endif
2710 evpipe_init (EV_A);
2711
2712 {
2713#ifndef _WIN32
2714 sigset_t full, prev;
2715 sigfillset (&full);
2716 sigprocmask (SIG_SETMASK, &full, &prev);
2717#endif
2718
2719 array_needsize (ANSIG, signals, signalmax, w->signum, array_init_zero);
2720
2721#ifndef _WIN32
2722# if EV_USE_SIGNALFD
2723 if (sigfd < 0)/*TODO*/
2724# endif 2741#endif
2725 sigdelset (&prev, w->signum);
2726 sigprocmask (SIG_SETMASK, &prev, 0);
2727#endif
2728 }
2729 2742
2730 ev_start (EV_A_ (W)w, 1); 2743 ev_start (EV_A_ (W)w, 1);
2731 wlist_add (&signals [w->signum - 1].head, (WL)w); 2744 wlist_add (&signals [w->signum - 1].head, (WL)w);
2732 2745
2733 if (!((WL)w)->next) 2746 if (!((WL)w)->next)
2734 {
2735#if _WIN32
2736 signal (w->signum, ev_sighandler);
2737#else
2738# if EV_USE_SIGNALFD 2747# if EV_USE_SIGNALFD
2739 if (sigfd < 0) /*TODO*/ 2748 if (sigfd < 0) /*TODO*/
2740# endif 2749# endif
2741 { 2750 {
2751# if _WIN32
2752 evpipe_init (EV_A);
2753
2754 signal (w->signum, ev_sighandler);
2755# else
2742 struct sigaction sa = { }; 2756 struct sigaction sa;
2757
2758 evpipe_init (EV_A);
2759
2743 sa.sa_handler = ev_sighandler; 2760 sa.sa_handler = ev_sighandler;
2744 sigfillset (&sa.sa_mask); 2761 sigfillset (&sa.sa_mask);
2745 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 */
2746 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
2747 } 2769 }
2748#endif
2749 }
2750 2770
2751 EV_FREQUENT_CHECK; 2771 EV_FREQUENT_CHECK;
2752} 2772}
2753 2773
2754void noinline 2774void noinline
2762 2782
2763 wlist_del (&signals [w->signum - 1].head, (WL)w); 2783 wlist_del (&signals [w->signum - 1].head, (WL)w);
2764 ev_stop (EV_A_ (W)w); 2784 ev_stop (EV_A_ (W)w);
2765 2785
2766 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
2767#if EV_USE_SIGNALFD 2791#if EV_USE_SIGNALFD
2768 if (sigfd >= 0) 2792 if (sigfd >= 0)
2769 { 2793 {
2770 sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D 2794 sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D
2771 sigdelset (&sigfd_set, w->signum); 2795 sigdelset (&sigfd_set, w->signum);
2772 signalfd (sigfd, &sigfd_set, 0); 2796 signalfd (sigfd, &sigfd_set, 0);
2773 sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D 2797 sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D
2774 /*TODO: maybe unblock signal? */ 2798 /*TODO: maybe unblock signal? */
2775 } 2799 }
2776 else 2800 else
2777#endif 2801#endif
2778 signal (w->signum, SIG_DFL); 2802 signal (w->signum, SIG_DFL);
2803 }
2779 2804
2780 EV_FREQUENT_CHECK; 2805 EV_FREQUENT_CHECK;
2781} 2806}
2782 2807
2783void 2808void
2831static void noinline 2856static void noinline
2832infy_add (EV_P_ ev_stat *w) 2857infy_add (EV_P_ ev_stat *w)
2833{ 2858{
2834 w->wd = inotify_add_watch (fs_fd, w->path, IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY | IN_DONT_FOLLOW | IN_MASK_ADD); 2859 w->wd = inotify_add_watch (fs_fd, w->path, IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY | IN_DONT_FOLLOW | IN_MASK_ADD);
2835 2860
2836 if (w->wd < 0) 2861 if (w->wd >= 0)
2862 {
2863 struct statfs sfs;
2864
2865 /* now local changes will be tracked by inotify, but remote changes won't */
2866 /* unless the filesystem is known to be local, we therefore still poll */
2867 /* also do poll on <2.6.25, but with normal frequency */
2868
2869 if (!fs_2625)
2870 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
2871 else if (!statfs (w->path, &sfs)
2872 && (sfs.f_type == 0x1373 /* devfs */
2873 || sfs.f_type == 0xEF53 /* ext2/3 */
2874 || sfs.f_type == 0x3153464a /* jfs */
2875 || sfs.f_type == 0x52654973 /* reiser3 */
2876 || sfs.f_type == 0x01021994 /* tempfs */
2877 || sfs.f_type == 0x58465342 /* xfs */))
2878 w->timer.repeat = 0.; /* filesystem is local, kernel new enough */
2879 else
2880 w->timer.repeat = w->interval ? w->interval : NFS_STAT_INTERVAL; /* remote, use reduced frequency */
2837 { 2881 }
2882 else
2883 {
2884 /* can't use inotify, continue to stat */
2838 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; 2885 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
2839 ev_timer_again (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */
2840 2886
2841 /* monitor some parent directory for speedup hints */ 2887 /* if path is not there, monitor some parent directory for speedup hints */
2842 /* note that exceeding the hardcoded path limit is not a correctness issue, */ 2888 /* note that exceeding the hardcoded path limit is not a correctness issue, */
2843 /* but an efficiency issue only */ 2889 /* but an efficiency issue only */
2844 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2890 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2845 { 2891 {
2846 char path [4096]; 2892 char path [4096];
2862 while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); 2908 while (w->wd < 0 && (errno == ENOENT || errno == EACCES));
2863 } 2909 }
2864 } 2910 }
2865 2911
2866 if (w->wd >= 0) 2912 if (w->wd >= 0)
2867 {
2868 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); 2913 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w);
2869 2914
2870 /* now local changes will be tracked by inotify, but remote changes won't */ 2915 /* now re-arm timer, if required */
2871 /* unless the filesystem it known to be local, we therefore still poll */ 2916 if (ev_is_active (&w->timer)) ev_ref (EV_A);
2872 /* also do poll on <2.6.25, but with normal frequency */
2873 struct statfs sfs;
2874
2875 if (fs_2625 && !statfs (w->path, &sfs))
2876 if (sfs.f_type == 0x1373 /* devfs */
2877 || sfs.f_type == 0xEF53 /* ext2/3 */
2878 || sfs.f_type == 0x3153464a /* jfs */
2879 || sfs.f_type == 0x52654973 /* reiser3 */
2880 || sfs.f_type == 0x01021994 /* tempfs */
2881 || sfs.f_type == 0x58465342 /* xfs */)
2882 return;
2883
2884 w->timer.repeat = w->interval ? w->interval : fs_2625 ? NFS_STAT_INTERVAL : DEF_STAT_INTERVAL;
2885 ev_timer_again (EV_A_ &w->timer); 2917 ev_timer_again (EV_A_ &w->timer);
2886 } 2918 if (ev_is_active (&w->timer)) ev_unref (EV_A);
2887} 2919}
2888 2920
2889static void noinline 2921static void noinline
2890infy_del (EV_P_ ev_stat *w) 2922infy_del (EV_P_ ev_stat *w)
2891{ 2923{
2967 return; 2999 return;
2968 3000
2969 fs_2625 = 1; 3001 fs_2625 = 1;
2970} 3002}
2971 3003
3004inline_size int
3005infy_newfd (void)
3006{
3007#if defined (IN_CLOEXEC) && defined (IN_NONBLOCK)
3008 int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK);
3009 if (fd >= 0)
3010 return fd;
3011#endif
3012 return inotify_init ();
3013}
3014
2972inline_size void 3015inline_size void
2973infy_init (EV_P) 3016infy_init (EV_P)
2974{ 3017{
2975 if (fs_fd != -2) 3018 if (fs_fd != -2)
2976 return; 3019 return;
2977 3020
2978 fs_fd = -1; 3021 fs_fd = -1;
2979 3022
2980 check_2625 (EV_A); 3023 check_2625 (EV_A);
2981 3024
2982 fs_fd = inotify_init (); 3025 fs_fd = infy_newfd ();
2983 3026
2984 if (fs_fd >= 0) 3027 if (fs_fd >= 0)
2985 { 3028 {
3029 fd_intern (fs_fd);
2986 ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ); 3030 ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ);
2987 ev_set_priority (&fs_w, EV_MAXPRI); 3031 ev_set_priority (&fs_w, EV_MAXPRI);
2988 ev_io_start (EV_A_ &fs_w); 3032 ev_io_start (EV_A_ &fs_w);
3033 ev_unref (EV_A);
2989 } 3034 }
2990} 3035}
2991 3036
2992inline_size void 3037inline_size void
2993infy_fork (EV_P) 3038infy_fork (EV_P)
2995 int slot; 3040 int slot;
2996 3041
2997 if (fs_fd < 0) 3042 if (fs_fd < 0)
2998 return; 3043 return;
2999 3044
3045 ev_ref (EV_A);
3046 ev_io_stop (EV_A_ &fs_w);
3000 close (fs_fd); 3047 close (fs_fd);
3001 fs_fd = inotify_init (); 3048 fs_fd = infy_newfd ();
3049
3050 if (fs_fd >= 0)
3051 {
3052 fd_intern (fs_fd);
3053 ev_io_set (&fs_w, fs_fd, EV_READ);
3054 ev_io_start (EV_A_ &fs_w);
3055 ev_unref (EV_A);
3056 }
3002 3057
3003 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot) 3058 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot)
3004 { 3059 {
3005 WL w_ = fs_hash [slot].head; 3060 WL w_ = fs_hash [slot].head;
3006 fs_hash [slot].head = 0; 3061 fs_hash [slot].head = 0;
3013 w->wd = -1; 3068 w->wd = -1;
3014 3069
3015 if (fs_fd >= 0) 3070 if (fs_fd >= 0)
3016 infy_add (EV_A_ w); /* re-add, no matter what */ 3071 infy_add (EV_A_ w); /* re-add, no matter what */
3017 else 3072 else
3073 {
3074 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
3075 if (ev_is_active (&w->timer)) ev_ref (EV_A);
3018 ev_timer_again (EV_A_ &w->timer); 3076 ev_timer_again (EV_A_ &w->timer);
3077 if (ev_is_active (&w->timer)) ev_unref (EV_A);
3078 }
3019 } 3079 }
3020 } 3080 }
3021} 3081}
3022 3082
3023#endif 3083#endif
3093 3153
3094 if (fs_fd >= 0) 3154 if (fs_fd >= 0)
3095 infy_add (EV_A_ w); 3155 infy_add (EV_A_ w);
3096 else 3156 else
3097#endif 3157#endif
3158 {
3098 ev_timer_again (EV_A_ &w->timer); 3159 ev_timer_again (EV_A_ &w->timer);
3160 ev_unref (EV_A);
3161 }
3099 3162
3100 ev_start (EV_A_ (W)w, 1); 3163 ev_start (EV_A_ (W)w, 1);
3101 3164
3102 EV_FREQUENT_CHECK; 3165 EV_FREQUENT_CHECK;
3103} 3166}
3112 EV_FREQUENT_CHECK; 3175 EV_FREQUENT_CHECK;
3113 3176
3114#if EV_USE_INOTIFY 3177#if EV_USE_INOTIFY
3115 infy_del (EV_A_ w); 3178 infy_del (EV_A_ w);
3116#endif 3179#endif
3180
3181 if (ev_is_active (&w->timer))
3182 {
3183 ev_ref (EV_A);
3117 ev_timer_stop (EV_A_ &w->timer); 3184 ev_timer_stop (EV_A_ &w->timer);
3185 }
3118 3186
3119 ev_stop (EV_A_ (W)w); 3187 ev_stop (EV_A_ (W)w);
3120 3188
3121 EV_FREQUENT_CHECK; 3189 EV_FREQUENT_CHECK;
3122} 3190}
3263embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents) 3331embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents)
3264{ 3332{
3265 ev_embed *w = (ev_embed *)(((char *)prepare) - offsetof (ev_embed, prepare)); 3333 ev_embed *w = (ev_embed *)(((char *)prepare) - offsetof (ev_embed, prepare));
3266 3334
3267 { 3335 {
3268 struct ev_loop *loop = w->other; 3336 EV_P = w->other;
3269 3337
3270 while (fdchangecnt) 3338 while (fdchangecnt)
3271 { 3339 {
3272 fd_reify (EV_A); 3340 fd_reify (EV_A);
3273 ev_loop (EV_A_ EVLOOP_NONBLOCK); 3341 ev_loop (EV_A_ EVLOOP_NONBLOCK);
3281 ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork)); 3349 ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork));
3282 3350
3283 ev_embed_stop (EV_A_ w); 3351 ev_embed_stop (EV_A_ w);
3284 3352
3285 { 3353 {
3286 struct ev_loop *loop = w->other; 3354 EV_P = w->other;
3287 3355
3288 ev_loop_fork (EV_A); 3356 ev_loop_fork (EV_A);
3289 ev_loop (EV_A_ EVLOOP_NONBLOCK); 3357 ev_loop (EV_A_ EVLOOP_NONBLOCK);
3290 } 3358 }
3291 3359
3305{ 3373{
3306 if (expect_false (ev_is_active (w))) 3374 if (expect_false (ev_is_active (w)))
3307 return; 3375 return;
3308 3376
3309 { 3377 {
3310 struct ev_loop *loop = w->other; 3378 EV_P = w->other;
3311 assert (("libev: loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); 3379 assert (("libev: loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
3312 ev_io_init (&w->io, embed_io_cb, backend_fd, EV_READ); 3380 ev_io_init (&w->io, embed_io_cb, backend_fd, EV_READ);
3313 } 3381 }
3314 3382
3315 EV_FREQUENT_CHECK; 3383 EV_FREQUENT_CHECK;
3427 3495
3428void 3496void
3429ev_async_send (EV_P_ ev_async *w) 3497ev_async_send (EV_P_ ev_async *w)
3430{ 3498{
3431 w->sent = 1; 3499 w->sent = 1;
3432 evpipe_write (EV_A_ &gotasync); 3500 evpipe_write (EV_A_ &async_pending);
3433} 3501}
3434#endif 3502#endif
3435 3503
3436/*****************************************************************************/ 3504/*****************************************************************************/
3437 3505
3586 if (types & EV_CHECK) 3654 if (types & EV_CHECK)
3587 for (i = checkcnt; i--; ) 3655 for (i = checkcnt; i--; )
3588 cb (EV_A_ EV_CHECK, checks [i]); 3656 cb (EV_A_ EV_CHECK, checks [i]);
3589 3657
3590 if (types & EV_SIGNAL) 3658 if (types & EV_SIGNAL)
3591 for (i = 0; i < signalmax; ++i) 3659 for (i = 0; i < EV_NSIG - 1; ++i)
3592 for (wl = signals [i].head; wl; ) 3660 for (wl = signals [i].head; wl; )
3593 { 3661 {
3594 wn = wl->next; 3662 wn = wl->next;
3595 cb (EV_A_ EV_SIGNAL, wl); 3663 cb (EV_A_ EV_SIGNAL, wl);
3596 wl = wn; 3664 wl = wn;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines