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

Comparing libev/ev.c (file contents):
Revision 1.295 by root, Wed Jul 8 04:29:31 2009 UTC vs.
Revision 1.318 by root, Tue Nov 17 00:22:28 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__ >= 7))
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
351#endif 404#endif
405
406#if EV_USE_SIGNALFD
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
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
352 434
353/**/ 435/**/
354 436
355#if EV_VERIFY >= 3 437#if EV_VERIFY >= 3
356# define EV_FREQUENT_CHECK ev_loop_verify (EV_A) 438# define EV_FREQUENT_CHECK ev_loop_verify (EV_A)
368 */ 450 */
369#define TIME_EPSILON 0.0001220703125 /* 1/8192 */ 451#define TIME_EPSILON 0.0001220703125 /* 1/8192 */
370 452
371#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) */
372#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) */
373/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds, TODO */
374 455
375#if __GNUC__ >= 4 456#if __GNUC__ >= 4
376# define expect(expr,value) __builtin_expect ((expr),(value)) 457# define expect(expr,value) __builtin_expect ((expr),(value))
377# define noinline __attribute__ ((noinline)) 458# define noinline __attribute__ ((noinline))
378#else 459#else
419 500
420#if EV_USE_MONOTONIC 501#if EV_USE_MONOTONIC
421static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 502static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
422#endif 503#endif
423 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)
513#endif
514
424#ifdef _WIN32 515#ifdef _WIN32
425# include "ev_win32.c" 516# include "ev_win32.c"
426#endif 517#endif
427 518
428/*****************************************************************************/ 519/*****************************************************************************/
490#define ev_malloc(size) ev_realloc (0, (size)) 581#define ev_malloc(size) ev_realloc (0, (size))
491#define ev_free(ptr) ev_realloc ((ptr), 0) 582#define ev_free(ptr) ev_realloc ((ptr), 0)
492 583
493/*****************************************************************************/ 584/*****************************************************************************/
494 585
586/* set in reify when reification needed */
587#define EV_ANFD_REIFY 1
588
495/* file descriptor info structure */ 589/* file descriptor info structure */
496typedef struct 590typedef struct
497{ 591{
498 WL head; 592 WL head;
499 unsigned char events; /* the events watched for */ 593 unsigned char events; /* the events watched for */
500 unsigned char reify; /* flag set when this ANFD needs reification */ 594 unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */
501 unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ 595 unsigned char emask; /* the epoll backend stores the actual kernel mask in here */
502 unsigned char unused; 596 unsigned char unused;
503#if EV_USE_EPOLL 597#if EV_USE_EPOLL
504 unsigned int egen; /* generation counter to counter epoll bugs */ 598 unsigned int egen; /* generation counter to counter epoll bugs */
505#endif 599#endif
567 661
568 static int ev_default_loop_ptr; 662 static int ev_default_loop_ptr;
569 663
570#endif 664#endif
571 665
666#if EV_MINIMAL < 2
667# define EV_RELEASE_CB if (expect_false (release_cb)) release_cb (EV_A)
668# define EV_ACQUIRE_CB if (expect_false (acquire_cb)) acquire_cb (EV_A)
669# define EV_INVOKE_PENDING invoke_cb (EV_A)
670#else
671# define EV_RELEASE_CB (void)0
672# define EV_ACQUIRE_CB (void)0
673# define EV_INVOKE_PENDING ev_invoke_pending (EV_A)
674#endif
675
676#define EVUNLOOP_RECURSE 0x80
677
572/*****************************************************************************/ 678/*****************************************************************************/
573 679
574#ifndef EV_HAVE_EV_TIME 680#ifndef EV_HAVE_EV_TIME
575ev_tstamp 681ev_tstamp
576ev_time (void) 682ev_time (void)
632 738
633 tv.tv_sec = (time_t)delay; 739 tv.tv_sec = (time_t)delay;
634 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 740 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
635 741
636 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ 742 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
637 /* somehting not guaranteed by newer posix versions, but guaranteed */ 743 /* something not guaranteed by newer posix versions, but guaranteed */
638 /* by older ones */ 744 /* by older ones */
639 select (0, 0, 0, 0, &tv); 745 select (0, 0, 0, 0, &tv);
640#endif 746#endif
641 } 747 }
642} 748}
750} 856}
751 857
752/*****************************************************************************/ 858/*****************************************************************************/
753 859
754inline_speed void 860inline_speed void
755fd_event (EV_P_ int fd, int revents) 861fd_event_nc (EV_P_ int fd, int revents)
756{ 862{
757 ANFD *anfd = anfds + fd; 863 ANFD *anfd = anfds + fd;
758 ev_io *w; 864 ev_io *w;
759 865
760 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) 866 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
764 if (ev) 870 if (ev)
765 ev_feed_event (EV_A_ (W)w, ev); 871 ev_feed_event (EV_A_ (W)w, ev);
766 } 872 }
767} 873}
768 874
875/* do not submit kernel events for fds that have reify set */
876/* because that means they changed while we were polling for new events */
877inline_speed void
878fd_event (EV_P_ int fd, int revents)
879{
880 ANFD *anfd = anfds + fd;
881
882 if (expect_true (!anfd->reify))
883 fd_event_nc (EV_A_ fd, revents);
884}
885
769void 886void
770ev_feed_fd_event (EV_P_ int fd, int revents) 887ev_feed_fd_event (EV_P_ int fd, int revents)
771{ 888{
772 if (fd >= 0 && fd < anfdmax) 889 if (fd >= 0 && fd < anfdmax)
773 fd_event (EV_A_ fd, revents); 890 fd_event_nc (EV_A_ fd, revents);
774} 891}
775 892
776/* make sure the external fd watch events are in-sync */ 893/* make sure the external fd watch events are in-sync */
777/* with the kernel/libev internal state */ 894/* with the kernel/libev internal state */
778inline_size void 895inline_size void
793 910
794#if EV_SELECT_IS_WINSOCKET 911#if EV_SELECT_IS_WINSOCKET
795 if (events) 912 if (events)
796 { 913 {
797 unsigned long arg; 914 unsigned long arg;
798 #ifdef EV_FD_TO_WIN32_HANDLE
799 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); 915 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
800 #else
801 anfd->handle = _get_osfhandle (fd);
802 #endif
803 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));
804 } 917 }
805#endif 918#endif
806 919
807 { 920 {
878 991
879 for (fd = anfdmax; fd--; ) 992 for (fd = anfdmax; fd--; )
880 if (anfds [fd].events) 993 if (anfds [fd].events)
881 { 994 {
882 fd_kill (EV_A_ fd); 995 fd_kill (EV_A_ fd);
883 return; 996 break;
884 } 997 }
885} 998}
886 999
887/* 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 */
888static void noinline 1001static void noinline
893 for (fd = 0; fd < anfdmax; ++fd) 1006 for (fd = 0; fd < anfdmax; ++fd)
894 if (anfds [fd].events) 1007 if (anfds [fd].events)
895 { 1008 {
896 anfds [fd].events = 0; 1009 anfds [fd].events = 0;
897 anfds [fd].emask = 0; 1010 anfds [fd].emask = 0;
898 fd_change (EV_A_ fd, EV__IOFDSET | 1); 1011 fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY);
899 } 1012 }
900} 1013}
901 1014
902/*****************************************************************************/ 1015/*****************************************************************************/
903 1016
978 1091
979 for (;;) 1092 for (;;)
980 { 1093 {
981 int c = k << 1; 1094 int c = k << 1;
982 1095
983 if (c > N + HEAP0 - 1) 1096 if (c >= N + HEAP0)
984 break; 1097 break;
985 1098
986 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])
987 ? 1 : 0; 1100 ? 1 : 0;
988 1101
1024 1137
1025/* move an element suitably so it is in a correct place */ 1138/* move an element suitably so it is in a correct place */
1026inline_size void 1139inline_size void
1027adjustheap (ANHE *heap, int N, int k) 1140adjustheap (ANHE *heap, int N, int k)
1028{ 1141{
1029 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)]))
1030 upheap (heap, k); 1143 upheap (heap, k);
1031 else 1144 else
1032 downheap (heap, N, k); 1145 downheap (heap, N, k);
1033} 1146}
1034 1147
1047/*****************************************************************************/ 1160/*****************************************************************************/
1048 1161
1049/* associate signal watchers to a signal signal */ 1162/* associate signal watchers to a signal signal */
1050typedef struct 1163typedef struct
1051{ 1164{
1165 EV_ATOMIC_T pending;
1166#if EV_MULTIPLICITY
1167 EV_P;
1168#endif
1052 WL head; 1169 WL head;
1053 EV_ATOMIC_T gotsig;
1054} ANSIG; 1170} ANSIG;
1055 1171
1056static ANSIG *signals; 1172static ANSIG signals [EV_NSIG - 1];
1057static int signalmax;
1058
1059static EV_ATOMIC_T gotsig;
1060 1173
1061/*****************************************************************************/ 1174/*****************************************************************************/
1062 1175
1063/* used to prepare libev internal fd's */ 1176/* used to prepare libev internal fd's */
1064/* this is not fork-safe */ 1177/* this is not fork-safe */
1078evpipe_init (EV_P) 1191evpipe_init (EV_P)
1079{ 1192{
1080 if (!ev_is_active (&pipe_w)) 1193 if (!ev_is_active (&pipe_w))
1081 { 1194 {
1082#if EV_USE_EVENTFD 1195#if EV_USE_EVENTFD
1196 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
1197 if (evfd < 0 && errno == EINVAL)
1083 if ((evfd = eventfd (0, 0)) >= 0) 1198 evfd = eventfd (0, 0);
1199
1200 if (evfd >= 0)
1084 { 1201 {
1085 evpipe [0] = -1; 1202 evpipe [0] = -1;
1086 fd_intern (evfd); 1203 fd_intern (evfd); /* doing it twice doesn't hurt */
1087 ev_io_set (&pipe_w, evfd, EV_READ); 1204 ev_io_set (&pipe_w, evfd, EV_READ);
1088 } 1205 }
1089 else 1206 else
1090#endif 1207#endif
1091 { 1208 {
1128/* called whenever the libev signal pipe */ 1245/* called whenever the libev signal pipe */
1129/* got some events (signal, async) */ 1246/* got some events (signal, async) */
1130static void 1247static void
1131pipecb (EV_P_ ev_io *iow, int revents) 1248pipecb (EV_P_ ev_io *iow, int revents)
1132{ 1249{
1250 int i;
1251
1133#if EV_USE_EVENTFD 1252#if EV_USE_EVENTFD
1134 if (evfd >= 0) 1253 if (evfd >= 0)
1135 { 1254 {
1136 uint64_t counter; 1255 uint64_t counter;
1137 read (evfd, &counter, sizeof (uint64_t)); 1256 read (evfd, &counter, sizeof (uint64_t));
1141 { 1260 {
1142 char dummy; 1261 char dummy;
1143 read (evpipe [0], &dummy, 1); 1262 read (evpipe [0], &dummy, 1);
1144 } 1263 }
1145 1264
1146 if (gotsig && ev_is_default_loop (EV_A)) 1265 if (sig_pending)
1147 { 1266 {
1148 int signum; 1267 sig_pending = 0;
1149 gotsig = 0;
1150 1268
1151 for (signum = signalmax; signum--; ) 1269 for (i = EV_NSIG - 1; i--; )
1152 if (signals [signum].gotsig) 1270 if (expect_false (signals [i].pending))
1153 ev_feed_signal_event (EV_A_ signum + 1); 1271 ev_feed_signal_event (EV_A_ i + 1);
1154 } 1272 }
1155 1273
1156#if EV_ASYNC_ENABLE 1274#if EV_ASYNC_ENABLE
1157 if (gotasync) 1275 if (async_pending)
1158 { 1276 {
1159 int i; 1277 async_pending = 0;
1160 gotasync = 0;
1161 1278
1162 for (i = asynccnt; i--; ) 1279 for (i = asynccnt; i--; )
1163 if (asyncs [i]->sent) 1280 if (asyncs [i]->sent)
1164 { 1281 {
1165 asyncs [i]->sent = 0; 1282 asyncs [i]->sent = 0;
1173 1290
1174static void 1291static void
1175ev_sighandler (int signum) 1292ev_sighandler (int signum)
1176{ 1293{
1177#if EV_MULTIPLICITY 1294#if EV_MULTIPLICITY
1178 struct ev_loop *loop = &default_loop_struct; 1295 EV_P = signals [signum - 1].loop;
1179#endif 1296#endif
1180 1297
1181#if _WIN32 1298#if _WIN32
1182 signal (signum, ev_sighandler); 1299 signal (signum, ev_sighandler);
1183#endif 1300#endif
1184 1301
1185 signals [signum - 1].gotsig = 1; 1302 signals [signum - 1].pending = 1;
1186 evpipe_write (EV_A_ &gotsig); 1303 evpipe_write (EV_A_ &sig_pending);
1187} 1304}
1188 1305
1189void noinline 1306void noinline
1190ev_feed_signal_event (EV_P_ int signum) 1307ev_feed_signal_event (EV_P_ int signum)
1191{ 1308{
1192 WL w; 1309 WL w;
1193 1310
1311 if (expect_false (signum <= 0 || signum > EV_NSIG))
1312 return;
1313
1314 --signum;
1315
1194#if EV_MULTIPLICITY 1316#if EV_MULTIPLICITY
1195 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 */
1196#endif 1318 /* or, likely more useful, feeding a signal nobody is waiting for */
1197 1319
1198 --signum; 1320 if (expect_false (signals [signum].loop != EV_A))
1199
1200 if (signum < 0 || signum >= signalmax)
1201 return; 1321 return;
1322#endif
1202 1323
1203 signals [signum].gotsig = 0; 1324 signals [signum].pending = 0;
1204 1325
1205 for (w = signals [signum].head; w; w = w->next) 1326 for (w = signals [signum].head; w; w = w->next)
1206 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 1327 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
1207} 1328}
1329
1330#if EV_USE_SIGNALFD
1331static void
1332sigfdcb (EV_P_ ev_io *iow, int revents)
1333{
1334 struct signalfd_siginfo si[2], *sip; /* these structs are big */
1335
1336 for (;;)
1337 {
1338 ssize_t res = read (sigfd, si, sizeof (si));
1339
1340 /* not ISO-C, as res might be -1, but works with SuS */
1341 for (sip = si; (char *)sip < (char *)si + res; ++sip)
1342 ev_feed_signal_event (EV_A_ sip->ssi_signo);
1343
1344 if (res < (ssize_t)sizeof (si))
1345 break;
1346 }
1347}
1348#endif
1208 1349
1209/*****************************************************************************/ 1350/*****************************************************************************/
1210 1351
1211static WL childs [EV_PID_HASHSIZE]; 1352static WL childs [EV_PID_HASHSIZE];
1212 1353
1357ev_backend (EV_P) 1498ev_backend (EV_P)
1358{ 1499{
1359 return backend; 1500 return backend;
1360} 1501}
1361 1502
1503#if EV_MINIMAL < 2
1362unsigned int 1504unsigned int
1363ev_loop_count (EV_P) 1505ev_loop_count (EV_P)
1364{ 1506{
1365 return loop_count; 1507 return loop_count;
1366} 1508}
1380void 1522void
1381ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 1523ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval)
1382{ 1524{
1383 timeout_blocktime = interval; 1525 timeout_blocktime = interval;
1384} 1526}
1527
1528void
1529ev_set_userdata (EV_P_ void *data)
1530{
1531 userdata = data;
1532}
1533
1534void *
1535ev_userdata (EV_P)
1536{
1537 return userdata;
1538}
1539
1540void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P))
1541{
1542 invoke_cb = invoke_pending_cb;
1543}
1544
1545void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P))
1546{
1547 release_cb = release;
1548 acquire_cb = acquire;
1549}
1550#endif
1385 1551
1386/* initialise a loop structure, must be zero-initialised */ 1552/* initialise a loop structure, must be zero-initialised */
1387static void noinline 1553static void noinline
1388loop_init (EV_P_ unsigned int flags) 1554loop_init (EV_P_ unsigned int flags)
1389{ 1555{
1407 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 1573 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
1408 have_monotonic = 1; 1574 have_monotonic = 1;
1409 } 1575 }
1410#endif 1576#endif
1411 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
1412 ev_rt_now = ev_time (); 1589 ev_rt_now = ev_time ();
1413 mn_now = get_clock (); 1590 mn_now = get_clock ();
1414 now_floor = mn_now; 1591 now_floor = mn_now;
1415 rtmn_diff = ev_rt_now - mn_now; 1592 rtmn_diff = ev_rt_now - mn_now;
1593#if EV_MINIMAL < 2
1594 invoke_cb = ev_invoke_pending;
1595#endif
1416 1596
1417 io_blocktime = 0.; 1597 io_blocktime = 0.;
1418 timeout_blocktime = 0.; 1598 timeout_blocktime = 0.;
1419 backend = 0; 1599 backend = 0;
1420 backend_fd = -1; 1600 backend_fd = -1;
1421 gotasync = 0; 1601 sig_pending = 0;
1602#if EV_ASYNC_ENABLE
1603 async_pending = 0;
1604#endif
1422#if EV_USE_INOTIFY 1605#if EV_USE_INOTIFY
1423 fs_fd = -2; 1606 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
1424#endif 1607#endif
1425 1608#if EV_USE_SIGNALFD
1426 /* pid check not overridable via env */ 1609 sigfd = flags & EVFLAG_NOSIGFD ? -1 : -2;
1427#ifndef _WIN32
1428 if (flags & EVFLAG_FORKCHECK)
1429 curpid = getpid ();
1430#endif 1610#endif
1431
1432 if (!(flags & EVFLAG_NOENV)
1433 && !enable_secure ()
1434 && getenv ("LIBEV_FLAGS"))
1435 flags = atoi (getenv ("LIBEV_FLAGS"));
1436 1611
1437 if (!(flags & 0x0000ffffU)) 1612 if (!(flags & 0x0000ffffU))
1438 flags |= ev_recommended_backends (); 1613 flags |= ev_recommended_backends ();
1439 1614
1440#if EV_USE_PORT 1615#if EV_USE_PORT
1466{ 1641{
1467 int i; 1642 int i;
1468 1643
1469 if (ev_is_active (&pipe_w)) 1644 if (ev_is_active (&pipe_w))
1470 { 1645 {
1471 ev_ref (EV_A); /* signal watcher */ 1646 /*ev_ref (EV_A);*/
1472 ev_io_stop (EV_A_ &pipe_w); 1647 /*ev_io_stop (EV_A_ &pipe_w);*/
1473 1648
1474#if EV_USE_EVENTFD 1649#if EV_USE_EVENTFD
1475 if (evfd >= 0) 1650 if (evfd >= 0)
1476 close (evfd); 1651 close (evfd);
1477#endif 1652#endif
1478 1653
1479 if (evpipe [0] >= 0) 1654 if (evpipe [0] >= 0)
1480 { 1655 {
1481 close (evpipe [0]); 1656 EV_WIN32_CLOSE_FD (evpipe [0]);
1482 close (evpipe [1]); 1657 EV_WIN32_CLOSE_FD (evpipe [1]);
1483 } 1658 }
1484 } 1659 }
1660
1661#if EV_USE_SIGNALFD
1662 if (ev_is_active (&sigfd_w))
1663 close (sigfd);
1664#endif
1485 1665
1486#if EV_USE_INOTIFY 1666#if EV_USE_INOTIFY
1487 if (fs_fd >= 0) 1667 if (fs_fd >= 0)
1488 close (fs_fd); 1668 close (fs_fd);
1489#endif 1669#endif
1513#if EV_IDLE_ENABLE 1693#if EV_IDLE_ENABLE
1514 array_free (idle, [i]); 1694 array_free (idle, [i]);
1515#endif 1695#endif
1516 } 1696 }
1517 1697
1518 ev_free (anfds); anfdmax = 0; 1698 ev_free (anfds); anfds = 0; anfdmax = 0;
1519 1699
1520 /* have to use the microsoft-never-gets-it-right macro */ 1700 /* have to use the microsoft-never-gets-it-right macro */
1521 array_free (rfeed, EMPTY); 1701 array_free (rfeed, EMPTY);
1522 array_free (fdchange, EMPTY); 1702 array_free (fdchange, EMPTY);
1523 array_free (timer, EMPTY); 1703 array_free (timer, EMPTY);
1558 1738
1559 if (ev_is_active (&pipe_w)) 1739 if (ev_is_active (&pipe_w))
1560 { 1740 {
1561 /* this "locks" the handlers against writing to the pipe */ 1741 /* this "locks" the handlers against writing to the pipe */
1562 /* while we modify the fd vars */ 1742 /* while we modify the fd vars */
1563 gotsig = 1; 1743 sig_pending = 1;
1564#if EV_ASYNC_ENABLE 1744#if EV_ASYNC_ENABLE
1565 gotasync = 1; 1745 async_pending = 1;
1566#endif 1746#endif
1567 1747
1568 ev_ref (EV_A); 1748 ev_ref (EV_A);
1569 ev_io_stop (EV_A_ &pipe_w); 1749 ev_io_stop (EV_A_ &pipe_w);
1570 1750
1573 close (evfd); 1753 close (evfd);
1574#endif 1754#endif
1575 1755
1576 if (evpipe [0] >= 0) 1756 if (evpipe [0] >= 0)
1577 { 1757 {
1578 close (evpipe [0]); 1758 EV_WIN32_CLOSE_FD (evpipe [0]);
1579 close (evpipe [1]); 1759 EV_WIN32_CLOSE_FD (evpipe [1]);
1580 } 1760 }
1581 1761
1582 evpipe_init (EV_A); 1762 evpipe_init (EV_A);
1583 /* now iterate over everything, in case we missed something */ 1763 /* now iterate over everything, in case we missed something */
1584 pipecb (EV_A_ &pipe_w, EV_READ); 1764 pipecb (EV_A_ &pipe_w, EV_READ);
1590#if EV_MULTIPLICITY 1770#if EV_MULTIPLICITY
1591 1771
1592struct ev_loop * 1772struct ev_loop *
1593ev_loop_new (unsigned int flags) 1773ev_loop_new (unsigned int flags)
1594{ 1774{
1595 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));
1596 1776
1597 memset (loop, 0, sizeof (struct ev_loop)); 1777 memset (EV_A, 0, sizeof (struct ev_loop));
1598
1599 loop_init (EV_A_ flags); 1778 loop_init (EV_A_ flags);
1600 1779
1601 if (ev_backend (EV_A)) 1780 if (ev_backend (EV_A))
1602 return loop; 1781 return EV_A;
1603 1782
1604 return 0; 1783 return 0;
1605} 1784}
1606 1785
1607void 1786void
1614void 1793void
1615ev_loop_fork (EV_P) 1794ev_loop_fork (EV_P)
1616{ 1795{
1617 postfork = 1; /* must be in line with ev_default_fork */ 1796 postfork = 1; /* must be in line with ev_default_fork */
1618} 1797}
1798#endif /* multiplicity */
1619 1799
1620#if EV_VERIFY 1800#if EV_VERIFY
1621static void noinline 1801static void noinline
1622verify_watcher (EV_P_ W w) 1802verify_watcher (EV_P_ W w)
1623{ 1803{
1651 verify_watcher (EV_A_ ws [cnt]); 1831 verify_watcher (EV_A_ ws [cnt]);
1652 } 1832 }
1653} 1833}
1654#endif 1834#endif
1655 1835
1836#if EV_MINIMAL < 2
1656void 1837void
1657ev_loop_verify (EV_P) 1838ev_loop_verify (EV_P)
1658{ 1839{
1659#if EV_VERIFY 1840#if EV_VERIFY
1660 int i; 1841 int i;
1709 assert (checkmax >= checkcnt); 1890 assert (checkmax >= checkcnt);
1710 array_verify (EV_A_ (W *)checks, checkcnt); 1891 array_verify (EV_A_ (W *)checks, checkcnt);
1711 1892
1712# if 0 1893# if 0
1713 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)
1714 for (signum = signalmax; signum--; ) if (signals [signum].gotsig) 1895 for (signum = EV_NSIG; signum--; ) if (signals [signum].pending)
1715# endif
1716#endif 1896# endif
1897#endif
1717} 1898}
1718 1899#endif
1719#endif /* multiplicity */
1720 1900
1721#if EV_MULTIPLICITY 1901#if EV_MULTIPLICITY
1722struct ev_loop * 1902struct ev_loop *
1723ev_default_loop_init (unsigned int flags) 1903ev_default_loop_init (unsigned int flags)
1724#else 1904#else
1727#endif 1907#endif
1728{ 1908{
1729 if (!ev_default_loop_ptr) 1909 if (!ev_default_loop_ptr)
1730 { 1910 {
1731#if EV_MULTIPLICITY 1911#if EV_MULTIPLICITY
1732 struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct; 1912 EV_P = ev_default_loop_ptr = &default_loop_struct;
1733#else 1913#else
1734 ev_default_loop_ptr = 1; 1914 ev_default_loop_ptr = 1;
1735#endif 1915#endif
1736 1916
1737 loop_init (EV_A_ flags); 1917 loop_init (EV_A_ flags);
1754 1934
1755void 1935void
1756ev_default_destroy (void) 1936ev_default_destroy (void)
1757{ 1937{
1758#if EV_MULTIPLICITY 1938#if EV_MULTIPLICITY
1759 struct ev_loop *loop = ev_default_loop_ptr; 1939 EV_P = ev_default_loop_ptr;
1760#endif 1940#endif
1761 1941
1762 ev_default_loop_ptr = 0; 1942 ev_default_loop_ptr = 0;
1763 1943
1764#ifndef _WIN32 1944#ifndef _WIN32
1771 1951
1772void 1952void
1773ev_default_fork (void) 1953ev_default_fork (void)
1774{ 1954{
1775#if EV_MULTIPLICITY 1955#if EV_MULTIPLICITY
1776 struct ev_loop *loop = ev_default_loop_ptr; 1956 EV_P = ev_default_loop_ptr;
1777#endif 1957#endif
1778 1958
1779 postfork = 1; /* must be in line with ev_loop_fork */ 1959 postfork = 1; /* must be in line with ev_loop_fork */
1780} 1960}
1781 1961
1785ev_invoke (EV_P_ void *w, int revents) 1965ev_invoke (EV_P_ void *w, int revents)
1786{ 1966{
1787 EV_CB_INVOKE ((W)w, revents); 1967 EV_CB_INVOKE ((W)w, revents);
1788} 1968}
1789 1969
1790inline_speed void 1970unsigned int
1791call_pending (EV_P) 1971ev_pending_count (EV_P)
1972{
1973 int pri;
1974 unsigned int count = 0;
1975
1976 for (pri = NUMPRI; pri--; )
1977 count += pendingcnt [pri];
1978
1979 return count;
1980}
1981
1982void noinline
1983ev_invoke_pending (EV_P)
1792{ 1984{
1793 int pri; 1985 int pri;
1794 1986
1795 for (pri = NUMPRI; pri--; ) 1987 for (pri = NUMPRI; pri--; )
1796 while (pendingcnt [pri]) 1988 while (pendingcnt [pri])
2034} 2226}
2035 2227
2036void 2228void
2037ev_loop (EV_P_ int flags) 2229ev_loop (EV_P_ int flags)
2038{ 2230{
2231#if EV_MINIMAL < 2
2039 ++loop_depth; 2232 ++loop_depth;
2233#endif
2234
2235 assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE));
2040 2236
2041 loop_done = EVUNLOOP_CANCEL; 2237 loop_done = EVUNLOOP_CANCEL;
2042 2238
2043 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 2239 EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */
2044 2240
2045 do 2241 do
2046 { 2242 {
2047#if EV_VERIFY >= 2 2243#if EV_VERIFY >= 2
2048 ev_loop_verify (EV_A); 2244 ev_loop_verify (EV_A);
2061 /* we might have forked, so queue fork handlers */ 2257 /* we might have forked, so queue fork handlers */
2062 if (expect_false (postfork)) 2258 if (expect_false (postfork))
2063 if (forkcnt) 2259 if (forkcnt)
2064 { 2260 {
2065 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 2261 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
2066 call_pending (EV_A); 2262 EV_INVOKE_PENDING;
2067 } 2263 }
2068#endif 2264#endif
2069 2265
2070 /* queue prepare watchers (and execute them) */ 2266 /* queue prepare watchers (and execute them) */
2071 if (expect_false (preparecnt)) 2267 if (expect_false (preparecnt))
2072 { 2268 {
2073 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 2269 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
2074 call_pending (EV_A); 2270 EV_INVOKE_PENDING;
2075 } 2271 }
2272
2273 if (expect_false (loop_done))
2274 break;
2076 2275
2077 /* we might have forked, so reify kernel state if necessary */ 2276 /* we might have forked, so reify kernel state if necessary */
2078 if (expect_false (postfork)) 2277 if (expect_false (postfork))
2079 loop_fork (EV_A); 2278 loop_fork (EV_A);
2080 2279
2128 waittime -= sleeptime; 2327 waittime -= sleeptime;
2129 } 2328 }
2130 } 2329 }
2131 } 2330 }
2132 2331
2332#if EV_MINIMAL < 2
2133 ++loop_count; 2333 ++loop_count;
2334#endif
2335 assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */
2134 backend_poll (EV_A_ waittime); 2336 backend_poll (EV_A_ waittime);
2337 assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */
2135 2338
2136 /* update ev_rt_now, do magic */ 2339 /* update ev_rt_now, do magic */
2137 time_update (EV_A_ waittime + sleeptime); 2340 time_update (EV_A_ waittime + sleeptime);
2138 } 2341 }
2139 2342
2150 2353
2151 /* queue check watchers, to be executed first */ 2354 /* queue check watchers, to be executed first */
2152 if (expect_false (checkcnt)) 2355 if (expect_false (checkcnt))
2153 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 2356 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
2154 2357
2155 call_pending (EV_A); 2358 EV_INVOKE_PENDING;
2156 } 2359 }
2157 while (expect_true ( 2360 while (expect_true (
2158 activecnt 2361 activecnt
2159 && !loop_done 2362 && !loop_done
2160 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 2363 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK))
2161 )); 2364 ));
2162 2365
2163 if (loop_done == EVUNLOOP_ONE) 2366 if (loop_done == EVUNLOOP_ONE)
2164 loop_done = EVUNLOOP_CANCEL; 2367 loop_done = EVUNLOOP_CANCEL;
2165 2368
2369#if EV_MINIMAL < 2
2166 --loop_depth; 2370 --loop_depth;
2371#endif
2167} 2372}
2168 2373
2169void 2374void
2170ev_unloop (EV_P_ int how) 2375ev_unloop (EV_P_ int how)
2171{ 2376{
2222inline_size void 2427inline_size void
2223wlist_del (WL *head, WL elem) 2428wlist_del (WL *head, WL elem)
2224{ 2429{
2225 while (*head) 2430 while (*head)
2226 { 2431 {
2227 if (*head == elem) 2432 if (expect_true (*head == elem))
2228 { 2433 {
2229 *head = elem->next; 2434 *head = elem->next;
2230 return; 2435 break;
2231 } 2436 }
2232 2437
2233 head = &(*head)->next; 2438 head = &(*head)->next;
2234 } 2439 }
2235} 2440}
2303 2508
2304 ev_start (EV_A_ (W)w, 1); 2509 ev_start (EV_A_ (W)w, 1);
2305 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); 2510 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero);
2306 wlist_add (&anfds[fd].head, (WL)w); 2511 wlist_add (&anfds[fd].head, (WL)w);
2307 2512
2308 fd_change (EV_A_ fd, w->events & EV__IOFDSET | 1); 2513 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY);
2309 w->events &= ~EV__IOFDSET; 2514 w->events &= ~EV__IOFDSET;
2310 2515
2311 EV_FREQUENT_CHECK; 2516 EV_FREQUENT_CHECK;
2312} 2517}
2313 2518
2407 } 2612 }
2408 2613
2409 EV_FREQUENT_CHECK; 2614 EV_FREQUENT_CHECK;
2410} 2615}
2411 2616
2617ev_tstamp
2618ev_timer_remaining (EV_P_ ev_timer *w)
2619{
2620 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.);
2621}
2622
2412#if EV_PERIODIC_ENABLE 2623#if EV_PERIODIC_ENABLE
2413void noinline 2624void noinline
2414ev_periodic_start (EV_P_ ev_periodic *w) 2625ev_periodic_start (EV_P_ ev_periodic *w)
2415{ 2626{
2416 if (expect_false (ev_is_active (w))) 2627 if (expect_false (ev_is_active (w)))
2483#endif 2694#endif
2484 2695
2485void noinline 2696void noinline
2486ev_signal_start (EV_P_ ev_signal *w) 2697ev_signal_start (EV_P_ ev_signal *w)
2487{ 2698{
2488#if EV_MULTIPLICITY
2489 assert (("libev: signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
2490#endif
2491 if (expect_false (ev_is_active (w))) 2699 if (expect_false (ev_is_active (w)))
2492 return; 2700 return;
2493 2701
2494 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));
2495 2703
2496 evpipe_init (EV_A); 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));
2497 2707
2498 EV_FREQUENT_CHECK; 2708 signals [w->signum - 1].loop = EV_A;
2709#endif
2499 2710
2711 EV_FREQUENT_CHECK;
2712
2713#if EV_USE_SIGNALFD
2714 if (sigfd == -2)
2500 { 2715 {
2501#ifndef _WIN32 2716 sigfd = signalfd (-1, &sigfd_set, SFD_NONBLOCK | SFD_CLOEXEC);
2502 sigset_t full, prev; 2717 if (sigfd < 0 && errno == EINVAL)
2503 sigfillset (&full); 2718 sigfd = signalfd (-1, &sigfd_set, 0); /* retry without flags */
2504 sigprocmask (SIG_SETMASK, &full, &prev);
2505#endif
2506 2719
2507 array_needsize (ANSIG, signals, signalmax, w->signum, array_init_zero); 2720 if (sigfd >= 0)
2721 {
2722 fd_intern (sigfd); /* doing it twice will not hurt */
2508 2723
2509#ifndef _WIN32 2724 sigemptyset (&sigfd_set);
2510 sigprocmask (SIG_SETMASK, &prev, 0); 2725
2511#endif 2726 ev_io_init (&sigfd_w, sigfdcb, sigfd, EV_READ);
2727 ev_set_priority (&sigfd_w, EV_MAXPRI);
2728 ev_io_start (EV_A_ &sigfd_w);
2729 ev_unref (EV_A); /* signalfd watcher should not keep loop alive */
2730 }
2512 } 2731 }
2732
2733 if (sigfd >= 0)
2734 {
2735 /* TODO: check .head */
2736 sigaddset (&sigfd_set, w->signum);
2737 sigprocmask (SIG_BLOCK, &sigfd_set, 0);
2738
2739 signalfd (sigfd, &sigfd_set, 0);
2740 }
2741#endif
2513 2742
2514 ev_start (EV_A_ (W)w, 1); 2743 ev_start (EV_A_ (W)w, 1);
2515 wlist_add (&signals [w->signum - 1].head, (WL)w); 2744 wlist_add (&signals [w->signum - 1].head, (WL)w);
2516 2745
2517 if (!((WL)w)->next) 2746 if (!((WL)w)->next)
2747# if EV_USE_SIGNALFD
2748 if (sigfd < 0) /*TODO*/
2749# endif
2518 { 2750 {
2519#if _WIN32 2751# if _WIN32
2752 evpipe_init (EV_A);
2753
2520 signal (w->signum, ev_sighandler); 2754 signal (w->signum, ev_sighandler);
2521#else 2755# else
2522 struct sigaction sa; 2756 struct sigaction sa;
2757
2758 evpipe_init (EV_A);
2759
2523 sa.sa_handler = ev_sighandler; 2760 sa.sa_handler = ev_sighandler;
2524 sigfillset (&sa.sa_mask); 2761 sigfillset (&sa.sa_mask);
2525 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 */
2526 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);
2527#endif 2768#endif
2528 } 2769 }
2529 2770
2530 EV_FREQUENT_CHECK; 2771 EV_FREQUENT_CHECK;
2531} 2772}
2532 2773
2533void noinline 2774void noinline
2541 2782
2542 wlist_del (&signals [w->signum - 1].head, (WL)w); 2783 wlist_del (&signals [w->signum - 1].head, (WL)w);
2543 ev_stop (EV_A_ (W)w); 2784 ev_stop (EV_A_ (W)w);
2544 2785
2545 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
2791#if EV_USE_SIGNALFD
2792 if (sigfd >= 0)
2793 {
2794 sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D
2795 sigdelset (&sigfd_set, w->signum);
2796 signalfd (sigfd, &sigfd_set, 0);
2797 sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D
2798 /*TODO: maybe unblock signal? */
2799 }
2800 else
2801#endif
2546 signal (w->signum, SIG_DFL); 2802 signal (w->signum, SIG_DFL);
2803 }
2547 2804
2548 EV_FREQUENT_CHECK; 2805 EV_FREQUENT_CHECK;
2549} 2806}
2550 2807
2551void 2808void
2599static void noinline 2856static void noinline
2600infy_add (EV_P_ ev_stat *w) 2857infy_add (EV_P_ ev_stat *w)
2601{ 2858{
2602 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);
2603 2860
2604 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 */
2605 { 2881 }
2882 else
2883 {
2884 /* can't use inotify, continue to stat */
2606 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL; 2885 w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
2607 ev_timer_again (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */
2608 2886
2609 /* monitor some parent directory for speedup hints */ 2887 /* if path is not there, monitor some parent directory for speedup hints */
2610 /* 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, */
2611 /* but an efficiency issue only */ 2889 /* but an efficiency issue only */
2612 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2890 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2613 { 2891 {
2614 char path [4096]; 2892 char path [4096];
2630 while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); 2908 while (w->wd < 0 && (errno == ENOENT || errno == EACCES));
2631 } 2909 }
2632 } 2910 }
2633 2911
2634 if (w->wd >= 0) 2912 if (w->wd >= 0)
2635 {
2636 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);
2637 2914
2638 /* now local changes will be tracked by inotify, but remote changes won't */ 2915 /* now re-arm timer, if required */
2639 /* unless the filesystem it known to be local, we therefore still poll */ 2916 if (ev_is_active (&w->timer)) ev_ref (EV_A);
2640 /* also do poll on <2.6.25, but with normal frequency */
2641 struct statfs sfs;
2642
2643 if (fs_2625 && !statfs (w->path, &sfs))
2644 if (sfs.f_type == 0x1373 /* devfs */
2645 || sfs.f_type == 0xEF53 /* ext2/3 */
2646 || sfs.f_type == 0x3153464a /* jfs */
2647 || sfs.f_type == 0x52654973 /* reiser3 */
2648 || sfs.f_type == 0x01021994 /* tempfs */
2649 || sfs.f_type == 0x58465342 /* xfs */)
2650 return;
2651
2652 w->timer.repeat = w->interval ? w->interval : fs_2625 ? NFS_STAT_INTERVAL : DEF_STAT_INTERVAL;
2653 ev_timer_again (EV_A_ &w->timer); 2917 ev_timer_again (EV_A_ &w->timer);
2654 } 2918 if (ev_is_active (&w->timer)) ev_unref (EV_A);
2655} 2919}
2656 2920
2657static void noinline 2921static void noinline
2658infy_del (EV_P_ ev_stat *w) 2922infy_del (EV_P_ ev_stat *w)
2659{ 2923{
2735 return; 2999 return;
2736 3000
2737 fs_2625 = 1; 3001 fs_2625 = 1;
2738} 3002}
2739 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
2740inline_size void 3015inline_size void
2741infy_init (EV_P) 3016infy_init (EV_P)
2742{ 3017{
2743 if (fs_fd != -2) 3018 if (fs_fd != -2)
2744 return; 3019 return;
2745 3020
2746 fs_fd = -1; 3021 fs_fd = -1;
2747 3022
2748 check_2625 (EV_A); 3023 check_2625 (EV_A);
2749 3024
2750 fs_fd = inotify_init (); 3025 fs_fd = infy_newfd ();
2751 3026
2752 if (fs_fd >= 0) 3027 if (fs_fd >= 0)
2753 { 3028 {
3029 fd_intern (fs_fd);
2754 ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ); 3030 ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ);
2755 ev_set_priority (&fs_w, EV_MAXPRI); 3031 ev_set_priority (&fs_w, EV_MAXPRI);
2756 ev_io_start (EV_A_ &fs_w); 3032 ev_io_start (EV_A_ &fs_w);
3033 ev_unref (EV_A);
2757 } 3034 }
2758} 3035}
2759 3036
2760inline_size void 3037inline_size void
2761infy_fork (EV_P) 3038infy_fork (EV_P)
2763 int slot; 3040 int slot;
2764 3041
2765 if (fs_fd < 0) 3042 if (fs_fd < 0)
2766 return; 3043 return;
2767 3044
3045 ev_ref (EV_A);
3046 ev_io_stop (EV_A_ &fs_w);
2768 close (fs_fd); 3047 close (fs_fd);
2769 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 }
2770 3057
2771 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot) 3058 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot)
2772 { 3059 {
2773 WL w_ = fs_hash [slot].head; 3060 WL w_ = fs_hash [slot].head;
2774 fs_hash [slot].head = 0; 3061 fs_hash [slot].head = 0;
2781 w->wd = -1; 3068 w->wd = -1;
2782 3069
2783 if (fs_fd >= 0) 3070 if (fs_fd >= 0)
2784 infy_add (EV_A_ w); /* re-add, no matter what */ 3071 infy_add (EV_A_ w); /* re-add, no matter what */
2785 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);
2786 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 }
2787 } 3079 }
2788 } 3080 }
2789} 3081}
2790 3082
2791#endif 3083#endif
2861 3153
2862 if (fs_fd >= 0) 3154 if (fs_fd >= 0)
2863 infy_add (EV_A_ w); 3155 infy_add (EV_A_ w);
2864 else 3156 else
2865#endif 3157#endif
3158 {
2866 ev_timer_again (EV_A_ &w->timer); 3159 ev_timer_again (EV_A_ &w->timer);
3160 ev_unref (EV_A);
3161 }
2867 3162
2868 ev_start (EV_A_ (W)w, 1); 3163 ev_start (EV_A_ (W)w, 1);
2869 3164
2870 EV_FREQUENT_CHECK; 3165 EV_FREQUENT_CHECK;
2871} 3166}
2880 EV_FREQUENT_CHECK; 3175 EV_FREQUENT_CHECK;
2881 3176
2882#if EV_USE_INOTIFY 3177#if EV_USE_INOTIFY
2883 infy_del (EV_A_ w); 3178 infy_del (EV_A_ w);
2884#endif 3179#endif
3180
3181 if (ev_is_active (&w->timer))
3182 {
3183 ev_ref (EV_A);
2885 ev_timer_stop (EV_A_ &w->timer); 3184 ev_timer_stop (EV_A_ &w->timer);
3185 }
2886 3186
2887 ev_stop (EV_A_ (W)w); 3187 ev_stop (EV_A_ (W)w);
2888 3188
2889 EV_FREQUENT_CHECK; 3189 EV_FREQUENT_CHECK;
2890} 3190}
3031embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents) 3331embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents)
3032{ 3332{
3033 ev_embed *w = (ev_embed *)(((char *)prepare) - offsetof (ev_embed, prepare)); 3333 ev_embed *w = (ev_embed *)(((char *)prepare) - offsetof (ev_embed, prepare));
3034 3334
3035 { 3335 {
3036 struct ev_loop *loop = w->other; 3336 EV_P = w->other;
3037 3337
3038 while (fdchangecnt) 3338 while (fdchangecnt)
3039 { 3339 {
3040 fd_reify (EV_A); 3340 fd_reify (EV_A);
3041 ev_loop (EV_A_ EVLOOP_NONBLOCK); 3341 ev_loop (EV_A_ EVLOOP_NONBLOCK);
3049 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));
3050 3350
3051 ev_embed_stop (EV_A_ w); 3351 ev_embed_stop (EV_A_ w);
3052 3352
3053 { 3353 {
3054 struct ev_loop *loop = w->other; 3354 EV_P = w->other;
3055 3355
3056 ev_loop_fork (EV_A); 3356 ev_loop_fork (EV_A);
3057 ev_loop (EV_A_ EVLOOP_NONBLOCK); 3357 ev_loop (EV_A_ EVLOOP_NONBLOCK);
3058 } 3358 }
3059 3359
3073{ 3373{
3074 if (expect_false (ev_is_active (w))) 3374 if (expect_false (ev_is_active (w)))
3075 return; 3375 return;
3076 3376
3077 { 3377 {
3078 struct ev_loop *loop = w->other; 3378 EV_P = w->other;
3079 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 ()));
3080 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);
3081 } 3381 }
3082 3382
3083 EV_FREQUENT_CHECK; 3383 EV_FREQUENT_CHECK;
3195 3495
3196void 3496void
3197ev_async_send (EV_P_ ev_async *w) 3497ev_async_send (EV_P_ ev_async *w)
3198{ 3498{
3199 w->sent = 1; 3499 w->sent = 1;
3200 evpipe_write (EV_A_ &gotasync); 3500 evpipe_write (EV_A_ &async_pending);
3201} 3501}
3202#endif 3502#endif
3203 3503
3204/*****************************************************************************/ 3504/*****************************************************************************/
3205 3505
3354 if (types & EV_CHECK) 3654 if (types & EV_CHECK)
3355 for (i = checkcnt; i--; ) 3655 for (i = checkcnt; i--; )
3356 cb (EV_A_ EV_CHECK, checks [i]); 3656 cb (EV_A_ EV_CHECK, checks [i]);
3357 3657
3358 if (types & EV_SIGNAL) 3658 if (types & EV_SIGNAL)
3359 for (i = 0; i < signalmax; ++i) 3659 for (i = 0; i < EV_NSIG - 1; ++i)
3360 for (wl = signals [i].head; wl; ) 3660 for (wl = signals [i].head; wl; )
3361 { 3661 {
3362 wn = wl->next; 3662 wn = wl->next;
3363 cb (EV_A_ EV_SIGNAL, wl); 3663 cb (EV_A_ EV_SIGNAL, wl);
3364 wl = wn; 3664 wl = wn;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines