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.320 by root, Fri Dec 4 20:25:06 2009 UTC

153 153
154#endif 154#endif
155 155
156#include <math.h> 156#include <math.h>
157#include <stdlib.h> 157#include <stdlib.h>
158#include <string.h>
158#include <fcntl.h> 159#include <fcntl.h>
159#include <stddef.h> 160#include <stddef.h>
160 161
161#include <stdio.h> 162#include <stdio.h>
162 163
187#endif 188#endif
188 189
189/* this block tries to deduce configuration from header-defined symbols and defaults */ 190/* this block tries to deduce configuration from header-defined symbols and defaults */
190 191
191/* try to deduce the maximum number of signals on this platform */ 192/* 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) 193#if defined (EV_NSIG)
194/* use what's provided */ 194/* use what's provided */
195#elif defined (NSIG) 195#elif defined (NSIG)
196# define EV_NSIG (NSIG) 196# define EV_NSIG (NSIG)
197#elif defined(_NSIG) 197#elif defined(_NSIG)
303# define EV_USE_EVENTFD 0 303# define EV_USE_EVENTFD 0
304# endif 304# endif
305#endif 305#endif
306 306
307#ifndef EV_USE_SIGNALFD 307#ifndef EV_USE_SIGNALFD
308# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9)) 308# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
309# define EV_USE_SIGNALFD 1 309# define EV_USE_SIGNALFD 1
310# else 310# else
311# define EV_USE_SIGNALFD 0 311# define EV_USE_SIGNALFD 0
312# endif 312# endif
313#endif 313#endif
387# include <stdint.h> 387# include <stdint.h>
388# ifndef EFD_NONBLOCK 388# ifndef EFD_NONBLOCK
389# define EFD_NONBLOCK O_NONBLOCK 389# define EFD_NONBLOCK O_NONBLOCK
390# endif 390# endif
391# ifndef EFD_CLOEXEC 391# ifndef EFD_CLOEXEC
392# ifdef O_CLOEXEC
392# define EFD_CLOEXEC O_CLOEXEC 393# define EFD_CLOEXEC O_CLOEXEC
394# else
395# define EFD_CLOEXEC 02000000
396# endif
393# endif 397# endif
394# ifdef __cplusplus 398# ifdef __cplusplus
395extern "C" { 399extern "C" {
396# endif 400# endif
397int eventfd (unsigned int initval, int flags); 401int eventfd (unsigned int initval, int flags);
399} 403}
400# endif 404# endif
401#endif 405#endif
402 406
403#if EV_USE_SIGNALFD 407#if EV_USE_SIGNALFD
404# include <sys/signalfd.h> 408/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
409# include <stdint.h>
410# ifndef SFD_NONBLOCK
411# define SFD_NONBLOCK O_NONBLOCK
405#endif 412# endif
413# ifndef SFD_CLOEXEC
414# ifdef O_CLOEXEC
415# define SFD_CLOEXEC O_CLOEXEC
416# else
417# define SFD_CLOEXEC 02000000
418# endif
419# endif
420# ifdef __cplusplus
421extern "C" {
422# endif
423int signalfd (int fd, const sigset_t *mask, int flags);
424
425struct signalfd_siginfo
426{
427 uint32_t ssi_signo;
428 char pad[128 - sizeof (uint32_t)];
429};
430# ifdef __cplusplus
431}
432# endif
433#endif
434
406 435
407/**/ 436/**/
408 437
409#if EV_VERIFY >= 3 438#if EV_VERIFY >= 3
410# define EV_FREQUENT_CHECK ev_loop_verify (EV_A) 439# define EV_FREQUENT_CHECK ev_loop_verify (EV_A)
422 */ 451 */
423#define TIME_EPSILON 0.0001220703125 /* 1/8192 */ 452#define TIME_EPSILON 0.0001220703125 /* 1/8192 */
424 453
425#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 454#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
426#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ 455#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
427/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds, TODO */
428 456
429#if __GNUC__ >= 4 457#if __GNUC__ >= 4
430# define expect(expr,value) __builtin_expect ((expr),(value)) 458# define expect(expr,value) __builtin_expect ((expr),(value))
431# define noinline __attribute__ ((noinline)) 459# define noinline __attribute__ ((noinline))
432#else 460#else
471static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */ 499static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */
472#endif 500#endif
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? */
504#endif
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)
476#endif 514#endif
477 515
478#ifdef _WIN32 516#ifdef _WIN32
479# include "ev_win32.c" 517# include "ev_win32.c"
480#endif 518#endif
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))
1624 {
1625 /*ev_ref (EV_A);*/
1626 /*ev_io_stop (EV_A_ &sigfd_w);*/
1627
1628 close (sigfd); 1664 close (sigfd);
1629 }
1630#endif 1665#endif
1631 1666
1632#if EV_USE_INOTIFY 1667#if EV_USE_INOTIFY
1633 if (fs_fd >= 0) 1668 if (fs_fd >= 0)
1634 close (fs_fd); 1669 close (fs_fd);
1704 1739
1705 if (ev_is_active (&pipe_w)) 1740 if (ev_is_active (&pipe_w))
1706 { 1741 {
1707 /* this "locks" the handlers against writing to the pipe */ 1742 /* this "locks" the handlers against writing to the pipe */
1708 /* while we modify the fd vars */ 1743 /* while we modify the fd vars */
1709 gotsig = 1; 1744 sig_pending = 1;
1710#if EV_ASYNC_ENABLE 1745#if EV_ASYNC_ENABLE
1711 gotasync = 1; 1746 async_pending = 1;
1712#endif 1747#endif
1713 1748
1714 ev_ref (EV_A); 1749 ev_ref (EV_A);
1715 ev_io_stop (EV_A_ &pipe_w); 1750 ev_io_stop (EV_A_ &pipe_w);
1716 1751
1719 close (evfd); 1754 close (evfd);
1720#endif 1755#endif
1721 1756
1722 if (evpipe [0] >= 0) 1757 if (evpipe [0] >= 0)
1723 { 1758 {
1724 close (evpipe [0]); 1759 EV_WIN32_CLOSE_FD (evpipe [0]);
1725 close (evpipe [1]); 1760 EV_WIN32_CLOSE_FD (evpipe [1]);
1726 } 1761 }
1727 1762
1728 evpipe_init (EV_A); 1763 evpipe_init (EV_A);
1729 /* now iterate over everything, in case we missed something */ 1764 /* now iterate over everything, in case we missed something */
1730 pipecb (EV_A_ &pipe_w, EV_READ); 1765 pipecb (EV_A_ &pipe_w, EV_READ);
1856 assert (checkmax >= checkcnt); 1891 assert (checkmax >= checkcnt);
1857 array_verify (EV_A_ (W *)checks, checkcnt); 1892 array_verify (EV_A_ (W *)checks, checkcnt);
1858 1893
1859# if 0 1894# if 0
1860 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 1895 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) 1896 for (signum = EV_NSIG; signum--; ) if (signals [signum].pending)
1862# endif 1897# endif
1863#endif 1898#endif
1864} 1899}
1865#endif 1900#endif
1866 1901
2393inline_size void 2428inline_size void
2394wlist_del (WL *head, WL elem) 2429wlist_del (WL *head, WL elem)
2395{ 2430{
2396 while (*head) 2431 while (*head)
2397 { 2432 {
2398 if (*head == elem) 2433 if (expect_true (*head == elem))
2399 { 2434 {
2400 *head = elem->next; 2435 *head = elem->next;
2401 return; 2436 break;
2402 } 2437 }
2403 2438
2404 head = &(*head)->next; 2439 head = &(*head)->next;
2405 } 2440 }
2406} 2441}
2666 return; 2701 return;
2667 2702
2668 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG)); 2703 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG));
2669 2704
2670#if EV_MULTIPLICITY 2705#if EV_MULTIPLICITY
2671 assert (("libev: tried to attach to a signal from two different loops", 2706 assert (("libev: a signal must not be attached to two different loops",
2672 !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop)); 2707 !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop));
2673 2708
2674 signals [w->signum - 1].loop = EV_A; 2709 signals [w->signum - 1].loop = EV_A;
2675#endif 2710#endif
2676 2711
2713# if EV_USE_SIGNALFD 2748# if EV_USE_SIGNALFD
2714 if (sigfd < 0) /*TODO*/ 2749 if (sigfd < 0) /*TODO*/
2715# endif 2750# endif
2716 { 2751 {
2717# if _WIN32 2752# if _WIN32
2753 evpipe_init (EV_A);
2754
2718 signal (w->signum, ev_sighandler); 2755 signal (w->signum, ev_sighandler);
2719# else 2756# else
2720 struct sigaction sa; 2757 struct sigaction sa;
2721 2758
2722 evpipe_init (EV_A); 2759 evpipe_init (EV_A);
2747 wlist_del (&signals [w->signum - 1].head, (WL)w); 2784 wlist_del (&signals [w->signum - 1].head, (WL)w);
2748 ev_stop (EV_A_ (W)w); 2785 ev_stop (EV_A_ (W)w);
2749 2786
2750 if (!signals [w->signum - 1].head) 2787 if (!signals [w->signum - 1].head)
2751 { 2788 {
2752 #if EV_MULTIPLICITY 2789#if EV_MULTIPLICITY
2753 signals [w->signum - 1].loop = 0; /* unattach from signal */ 2790 signals [w->signum - 1].loop = 0; /* unattach from signal */
2754 #endif 2791#endif
2755 #if EV_USE_SIGNALFD 2792#if EV_USE_SIGNALFD
2756 if (sigfd >= 0) 2793 if (sigfd >= 0)
2757 { 2794 {
2758 sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D 2795 sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D
2759 sigdelset (&sigfd_set, w->signum); 2796 sigdelset (&sigfd_set, w->signum);
2760 signalfd (sigfd, &sigfd_set, 0); 2797 signalfd (sigfd, &sigfd_set, 0);
2761 sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D 2798 sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D
2762 /*TODO: maybe unblock signal? */ 2799 /*TODO: maybe unblock signal? */
2763 } 2800 }
2764 else 2801 else
2765 #endif 2802#endif
2766 signal (w->signum, SIG_DFL); 2803 signal (w->signum, SIG_DFL);
2767 } 2804 }
2768 2805
2769 EV_FREQUENT_CHECK; 2806 EV_FREQUENT_CHECK;
2770} 2807}
2820static void noinline 2857static void noinline
2821infy_add (EV_P_ ev_stat *w) 2858infy_add (EV_P_ ev_stat *w)
2822{ 2859{
2823 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); 2860 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);
2824 2861
2825 if (w->wd < 0) 2862 if (w->wd >= 0)
2863 {
2864 struct statfs sfs;
2865
2866 /* now local changes will be tracked by inotify, but remote changes won't */
2867 /* unless the filesystem is known to be local, we therefore still poll */
2868 /* also do poll on <2.6.25, but with normal frequency */
2869
2870 if (!fs_2625)
2871 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
2872 else if (!statfs (w->path, &sfs)
2873 && (sfs.f_type == 0x1373 /* devfs */
2874 || sfs.f_type == 0xEF53 /* ext2/3 */
2875 || sfs.f_type == 0x3153464a /* jfs */
2876 || sfs.f_type == 0x52654973 /* reiser3 */
2877 || sfs.f_type == 0x01021994 /* tempfs */
2878 || sfs.f_type == 0x58465342 /* xfs */))
2879 w->timer.repeat = 0.; /* filesystem is local, kernel new enough */
2880 else
2881 w->timer.repeat = w->interval ? w->interval : NFS_STAT_INTERVAL; /* remote, use reduced frequency */
2826 { 2882 }
2883 else
2884 {
2885 /* can't use inotify, continue to stat */
2827 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; 2886 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
2828 ev_timer_again (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */
2829 2887
2830 /* monitor some parent directory for speedup hints */ 2888 /* if path is not there, monitor some parent directory for speedup hints */
2831 /* note that exceeding the hardcoded path limit is not a correctness issue, */ 2889 /* note that exceeding the hardcoded path limit is not a correctness issue, */
2832 /* but an efficiency issue only */ 2890 /* but an efficiency issue only */
2833 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2891 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2834 { 2892 {
2835 char path [4096]; 2893 char path [4096];
2851 while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); 2909 while (w->wd < 0 && (errno == ENOENT || errno == EACCES));
2852 } 2910 }
2853 } 2911 }
2854 2912
2855 if (w->wd >= 0) 2913 if (w->wd >= 0)
2856 {
2857 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); 2914 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w);
2858 2915
2859 /* now local changes will be tracked by inotify, but remote changes won't */ 2916 /* now re-arm timer, if required */
2860 /* unless the filesystem it known to be local, we therefore still poll */ 2917 if (ev_is_active (&w->timer)) ev_ref (EV_A);
2861 /* also do poll on <2.6.25, but with normal frequency */
2862 struct statfs sfs;
2863
2864 if (fs_2625 && !statfs (w->path, &sfs))
2865 if (sfs.f_type == 0x1373 /* devfs */
2866 || sfs.f_type == 0xEF53 /* ext2/3 */
2867 || sfs.f_type == 0x3153464a /* jfs */
2868 || sfs.f_type == 0x52654973 /* reiser3 */
2869 || sfs.f_type == 0x01021994 /* tempfs */
2870 || sfs.f_type == 0x58465342 /* xfs */)
2871 return;
2872
2873 w->timer.repeat = w->interval ? w->interval : fs_2625 ? NFS_STAT_INTERVAL : DEF_STAT_INTERVAL;
2874 ev_timer_again (EV_A_ &w->timer); 2918 ev_timer_again (EV_A_ &w->timer);
2875 } 2919 if (ev_is_active (&w->timer)) ev_unref (EV_A);
2876} 2920}
2877 2921
2878static void noinline 2922static void noinline
2879infy_del (EV_P_ ev_stat *w) 2923infy_del (EV_P_ ev_stat *w)
2880{ 2924{
2956 return; 3000 return;
2957 3001
2958 fs_2625 = 1; 3002 fs_2625 = 1;
2959} 3003}
2960 3004
3005inline_size int
3006infy_newfd (void)
3007{
3008#if defined (IN_CLOEXEC) && defined (IN_NONBLOCK)
3009 int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK);
3010 if (fd >= 0)
3011 return fd;
3012#endif
3013 return inotify_init ();
3014}
3015
2961inline_size void 3016inline_size void
2962infy_init (EV_P) 3017infy_init (EV_P)
2963{ 3018{
2964 if (fs_fd != -2) 3019 if (fs_fd != -2)
2965 return; 3020 return;
2966 3021
2967 fs_fd = -1; 3022 fs_fd = -1;
2968 3023
2969 check_2625 (EV_A); 3024 check_2625 (EV_A);
2970 3025
2971 fs_fd = inotify_init (); 3026 fs_fd = infy_newfd ();
2972 3027
2973 if (fs_fd >= 0) 3028 if (fs_fd >= 0)
2974 { 3029 {
3030 fd_intern (fs_fd);
2975 ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ); 3031 ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ);
2976 ev_set_priority (&fs_w, EV_MAXPRI); 3032 ev_set_priority (&fs_w, EV_MAXPRI);
2977 ev_io_start (EV_A_ &fs_w); 3033 ev_io_start (EV_A_ &fs_w);
3034 ev_unref (EV_A);
2978 } 3035 }
2979} 3036}
2980 3037
2981inline_size void 3038inline_size void
2982infy_fork (EV_P) 3039infy_fork (EV_P)
2984 int slot; 3041 int slot;
2985 3042
2986 if (fs_fd < 0) 3043 if (fs_fd < 0)
2987 return; 3044 return;
2988 3045
3046 ev_ref (EV_A);
3047 ev_io_stop (EV_A_ &fs_w);
2989 close (fs_fd); 3048 close (fs_fd);
2990 fs_fd = inotify_init (); 3049 fs_fd = infy_newfd ();
3050
3051 if (fs_fd >= 0)
3052 {
3053 fd_intern (fs_fd);
3054 ev_io_set (&fs_w, fs_fd, EV_READ);
3055 ev_io_start (EV_A_ &fs_w);
3056 ev_unref (EV_A);
3057 }
2991 3058
2992 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot) 3059 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot)
2993 { 3060 {
2994 WL w_ = fs_hash [slot].head; 3061 WL w_ = fs_hash [slot].head;
2995 fs_hash [slot].head = 0; 3062 fs_hash [slot].head = 0;
3002 w->wd = -1; 3069 w->wd = -1;
3003 3070
3004 if (fs_fd >= 0) 3071 if (fs_fd >= 0)
3005 infy_add (EV_A_ w); /* re-add, no matter what */ 3072 infy_add (EV_A_ w); /* re-add, no matter what */
3006 else 3073 else
3074 {
3075 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
3076 if (ev_is_active (&w->timer)) ev_ref (EV_A);
3007 ev_timer_again (EV_A_ &w->timer); 3077 ev_timer_again (EV_A_ &w->timer);
3078 if (ev_is_active (&w->timer)) ev_unref (EV_A);
3079 }
3008 } 3080 }
3009 } 3081 }
3010} 3082}
3011 3083
3012#endif 3084#endif
3029static void noinline 3101static void noinline
3030stat_timer_cb (EV_P_ ev_timer *w_, int revents) 3102stat_timer_cb (EV_P_ ev_timer *w_, int revents)
3031{ 3103{
3032 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); 3104 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
3033 3105
3034 /* we copy this here each the time so that */ 3106 ev_statdata prev = w->attr;
3035 /* prev has the old value when the callback gets invoked */
3036 w->prev = w->attr;
3037 ev_stat_stat (EV_A_ w); 3107 ev_stat_stat (EV_A_ w);
3038 3108
3039 /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */ 3109 /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */
3040 if ( 3110 if (
3041 w->prev.st_dev != w->attr.st_dev 3111 prev.st_dev != w->attr.st_dev
3042 || w->prev.st_ino != w->attr.st_ino 3112 || prev.st_ino != w->attr.st_ino
3043 || w->prev.st_mode != w->attr.st_mode 3113 || prev.st_mode != w->attr.st_mode
3044 || w->prev.st_nlink != w->attr.st_nlink 3114 || prev.st_nlink != w->attr.st_nlink
3045 || w->prev.st_uid != w->attr.st_uid 3115 || prev.st_uid != w->attr.st_uid
3046 || w->prev.st_gid != w->attr.st_gid 3116 || prev.st_gid != w->attr.st_gid
3047 || w->prev.st_rdev != w->attr.st_rdev 3117 || prev.st_rdev != w->attr.st_rdev
3048 || w->prev.st_size != w->attr.st_size 3118 || prev.st_size != w->attr.st_size
3049 || w->prev.st_atime != w->attr.st_atime 3119 || prev.st_atime != w->attr.st_atime
3050 || w->prev.st_mtime != w->attr.st_mtime 3120 || prev.st_mtime != w->attr.st_mtime
3051 || w->prev.st_ctime != w->attr.st_ctime 3121 || prev.st_ctime != w->attr.st_ctime
3052 ) { 3122 ) {
3123 /* we only update w->prev on actual differences */
3124 /* in case we test more often than invoke the callback, */
3125 /* to ensure that prev is always different to attr */
3126 w->prev = prev;
3127
3053 #if EV_USE_INOTIFY 3128 #if EV_USE_INOTIFY
3054 if (fs_fd >= 0) 3129 if (fs_fd >= 0)
3055 { 3130 {
3056 infy_del (EV_A_ w); 3131 infy_del (EV_A_ w);
3057 infy_add (EV_A_ w); 3132 infy_add (EV_A_ w);
3082 3157
3083 if (fs_fd >= 0) 3158 if (fs_fd >= 0)
3084 infy_add (EV_A_ w); 3159 infy_add (EV_A_ w);
3085 else 3160 else
3086#endif 3161#endif
3162 {
3087 ev_timer_again (EV_A_ &w->timer); 3163 ev_timer_again (EV_A_ &w->timer);
3164 ev_unref (EV_A);
3165 }
3088 3166
3089 ev_start (EV_A_ (W)w, 1); 3167 ev_start (EV_A_ (W)w, 1);
3090 3168
3091 EV_FREQUENT_CHECK; 3169 EV_FREQUENT_CHECK;
3092} 3170}
3101 EV_FREQUENT_CHECK; 3179 EV_FREQUENT_CHECK;
3102 3180
3103#if EV_USE_INOTIFY 3181#if EV_USE_INOTIFY
3104 infy_del (EV_A_ w); 3182 infy_del (EV_A_ w);
3105#endif 3183#endif
3184
3185 if (ev_is_active (&w->timer))
3186 {
3187 ev_ref (EV_A);
3106 ev_timer_stop (EV_A_ &w->timer); 3188 ev_timer_stop (EV_A_ &w->timer);
3189 }
3107 3190
3108 ev_stop (EV_A_ (W)w); 3191 ev_stop (EV_A_ (W)w);
3109 3192
3110 EV_FREQUENT_CHECK; 3193 EV_FREQUENT_CHECK;
3111} 3194}
3416 3499
3417void 3500void
3418ev_async_send (EV_P_ ev_async *w) 3501ev_async_send (EV_P_ ev_async *w)
3419{ 3502{
3420 w->sent = 1; 3503 w->sent = 1;
3421 evpipe_write (EV_A_ &gotasync); 3504 evpipe_write (EV_A_ &async_pending);
3422} 3505}
3423#endif 3506#endif
3424 3507
3425/*****************************************************************************/ 3508/*****************************************************************************/
3426 3509

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines