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

Comparing libev/ev.c (file contents):
Revision 1.332 by root, Tue Mar 9 08:58:17 2010 UTC vs.
Revision 1.339 by root, Tue Mar 16 00:43:22 2010 UTC

79# endif 79# endif
80# endif 80# endif
81 81
82# ifndef EV_USE_NANOSLEEP 82# ifndef EV_USE_NANOSLEEP
83# if HAVE_NANOSLEEP 83# if HAVE_NANOSLEEP
84# define EV_USE_NANOSLEEP 1 84# define EV_USE_NANOSLEEP EV_FEATURE_OS
85# else 85# else
86# define EV_USE_NANOSLEEP 0 86# define EV_USE_NANOSLEEP 0
87# endif 87# endif
88# endif 88# endif
89 89
90# ifndef EV_USE_SELECT 90# ifndef EV_USE_SELECT
91# if HAVE_SELECT && HAVE_SYS_SELECT_H 91# if HAVE_SELECT && HAVE_SYS_SELECT_H
92# define EV_USE_SELECT 1 92# define EV_USE_SELECT EV_FEATURE_BACKENDS
93# else 93# else
94# define EV_USE_SELECT 0 94# define EV_USE_SELECT 0
95# endif 95# endif
96# endif 96# endif
97 97
98# ifndef EV_USE_POLL 98# ifndef EV_USE_POLL
99# if HAVE_POLL && HAVE_POLL_H 99# if HAVE_POLL && HAVE_POLL_H
100# define EV_USE_POLL 1 100# define EV_USE_POLL EV_FEATURE_BACKENDS
101# else 101# else
102# define EV_USE_POLL 0 102# define EV_USE_POLL 0
103# endif 103# endif
104# endif 104# endif
105 105
106# ifndef EV_USE_EPOLL 106# ifndef EV_USE_EPOLL
107# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H 107# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
108# define EV_USE_EPOLL 1 108# define EV_USE_EPOLL EV_FEATURE_BACKENDS
109# else 109# else
110# define EV_USE_EPOLL 0 110# define EV_USE_EPOLL 0
111# endif 111# endif
112# endif 112# endif
113 113
114# ifndef EV_USE_KQUEUE 114# ifndef EV_USE_KQUEUE
115# if HAVE_KQUEUE && HAVE_SYS_EVENT_H 115# if HAVE_KQUEUE && HAVE_SYS_EVENT_H
116# define EV_USE_KQUEUE 1 116# define EV_USE_KQUEUE EV_FEATURE_BACKENDS
117# else 117# else
118# define EV_USE_KQUEUE 0 118# define EV_USE_KQUEUE 0
119# endif 119# endif
120# endif 120# endif
121 121
122# ifndef EV_USE_PORT 122# ifndef EV_USE_PORT
123# if HAVE_PORT_H && HAVE_PORT_CREATE 123# if HAVE_PORT_H && HAVE_PORT_CREATE
124# define EV_USE_PORT 1 124# define EV_USE_PORT EV_FEATURE_BACKENDS
125# else 125# else
126# define EV_USE_PORT 0 126# define EV_USE_PORT 0
127# endif 127# endif
128# endif 128# endif
129 129
130# ifndef EV_USE_INOTIFY 130# ifndef EV_USE_INOTIFY
131# if HAVE_INOTIFY_INIT && HAVE_SYS_INOTIFY_H 131# if HAVE_INOTIFY_INIT && HAVE_SYS_INOTIFY_H
132# define EV_USE_INOTIFY 1 132# define EV_USE_INOTIFY EV_FEATURE_OS
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 138# ifndef EV_USE_SIGNALFD
139# if HAVE_SIGNALFD && HAVE_SYS_SIGNALFD_H 139# if HAVE_SIGNALFD && HAVE_SYS_SIGNALFD_H
140# define EV_USE_SIGNALFD 1 140# define EV_USE_SIGNALFD EV_FEATURE_OS
141# else 141# else
142# define EV_USE_SIGNALFD 0 142# define EV_USE_SIGNALFD 0
143# endif 143# endif
144# endif 144# endif
145 145
146# ifndef EV_USE_EVENTFD 146# ifndef EV_USE_EVENTFD
147# if HAVE_EVENTFD 147# if HAVE_EVENTFD
148# define EV_USE_EVENTFD 1 148# define EV_USE_EVENTFD EV_FEATURE_OS
149# else 149# else
150# define EV_USE_EVENTFD 0 150# define EV_USE_EVENTFD 0
151# endif 151# endif
152# endif 152# endif
153 153
207#elif defined (MAXSIG) 207#elif defined (MAXSIG)
208# define EV_NSIG (MAXSIG+1) 208# define EV_NSIG (MAXSIG+1)
209#elif defined (MAX_SIG) 209#elif defined (MAX_SIG)
210# define EV_NSIG (MAX_SIG+1) 210# define EV_NSIG (MAX_SIG+1)
211#elif defined (SIGARRAYSIZE) 211#elif defined (SIGARRAYSIZE)
212# define EV_NSIG SIGARRAYSIZE /* Assume ary[SIGARRAYSIZE] */ 212# define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */
213#elif defined (_sys_nsig) 213#elif defined (_sys_nsig)
214# define EV_NSIG (_sys_nsig) /* Solaris 2.5 */ 214# define EV_NSIG (_sys_nsig) /* Solaris 2.5 */
215#else 215#else
216# error "unable to find value for NSIG, please report" 216# error "unable to find value for NSIG, please report"
217/* to make it compile regardless, just remove the above line */ 217/* to make it compile regardless, just remove the above line, */
218/* but consider reporting it, too! :) */
218# define EV_NSIG 65 219# define EV_NSIG 65
219#endif 220#endif
220 221
221#ifndef EV_USE_CLOCK_SYSCALL 222#ifndef EV_USE_CLOCK_SYSCALL
222# if __linux && __GLIBC__ >= 2 223# if __linux && __GLIBC__ >= 2
223# define EV_USE_CLOCK_SYSCALL 1 224# define EV_USE_CLOCK_SYSCALL EV_FEATURE_OS
224# else 225# else
225# define EV_USE_CLOCK_SYSCALL 0 226# define EV_USE_CLOCK_SYSCALL 0
226# endif 227# endif
227#endif 228#endif
228 229
229#ifndef EV_USE_MONOTONIC 230#ifndef EV_USE_MONOTONIC
230# if defined (_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0 231# if defined (_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0
231# define EV_USE_MONOTONIC 1 232# define EV_USE_MONOTONIC EV_FEATURE_OS
232# else 233# else
233# define EV_USE_MONOTONIC 0 234# define EV_USE_MONOTONIC 0
234# endif 235# endif
235#endif 236#endif
236 237
238# define EV_USE_REALTIME !EV_USE_CLOCK_SYSCALL 239# define EV_USE_REALTIME !EV_USE_CLOCK_SYSCALL
239#endif 240#endif
240 241
241#ifndef EV_USE_NANOSLEEP 242#ifndef EV_USE_NANOSLEEP
242# if _POSIX_C_SOURCE >= 199309L 243# if _POSIX_C_SOURCE >= 199309L
243# define EV_USE_NANOSLEEP 1 244# define EV_USE_NANOSLEEP EV_FEATURE_OS
244# else 245# else
245# define EV_USE_NANOSLEEP 0 246# define EV_USE_NANOSLEEP 0
246# endif 247# endif
247#endif 248#endif
248 249
249#ifndef EV_USE_SELECT 250#ifndef EV_USE_SELECT
250# define EV_USE_SELECT 1 251# define EV_USE_SELECT EV_FEATURE_BACKENDS
251#endif 252#endif
252 253
253#ifndef EV_USE_POLL 254#ifndef EV_USE_POLL
254# ifdef _WIN32 255# ifdef _WIN32
255# define EV_USE_POLL 0 256# define EV_USE_POLL 0
256# else 257# else
257# define EV_USE_POLL 1 258# define EV_USE_POLL EV_FEATURE_BACKENDS
258# endif 259# endif
259#endif 260#endif
260 261
261#ifndef EV_USE_EPOLL 262#ifndef EV_USE_EPOLL
262# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4)) 263# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
263# define EV_USE_EPOLL 1 264# define EV_USE_EPOLL EV_FEATURE_BACKENDS
264# else 265# else
265# define EV_USE_EPOLL 0 266# define EV_USE_EPOLL 0
266# endif 267# endif
267#endif 268#endif
268 269
274# define EV_USE_PORT 0 275# define EV_USE_PORT 0
275#endif 276#endif
276 277
277#ifndef EV_USE_INOTIFY 278#ifndef EV_USE_INOTIFY
278# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4)) 279# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
279# define EV_USE_INOTIFY 1 280# define EV_USE_INOTIFY EV_FEATURE_OS
280# else 281# else
281# define EV_USE_INOTIFY 0 282# define EV_USE_INOTIFY 0
282# endif 283# endif
283#endif 284#endif
284 285
285#ifndef EV_PID_HASHSIZE 286#ifndef EV_PID_HASHSIZE
286# if EV_MINIMAL 287# define EV_PID_HASHSIZE EV_FEATURE_DATA ? 16 : 1
287# define EV_PID_HASHSIZE 1
288# else
289# define EV_PID_HASHSIZE 16
290# endif
291#endif 288#endif
292 289
293#ifndef EV_INOTIFY_HASHSIZE 290#ifndef EV_INOTIFY_HASHSIZE
294# if EV_MINIMAL 291# define EV_INOTIFY_HASHSIZE EV_FEATURE_DATA ? 16 : 1
295# define EV_INOTIFY_HASHSIZE 1
296# else
297# define EV_INOTIFY_HASHSIZE 16
298# endif
299#endif 292#endif
300 293
301#ifndef EV_USE_EVENTFD 294#ifndef EV_USE_EVENTFD
302# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) 295# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
303# define EV_USE_EVENTFD 1 296# define EV_USE_EVENTFD EV_FEATURE_OS
304# else 297# else
305# define EV_USE_EVENTFD 0 298# define EV_USE_EVENTFD 0
306# endif 299# endif
307#endif 300#endif
308 301
309#ifndef EV_USE_SIGNALFD 302#ifndef EV_USE_SIGNALFD
310# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) 303# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
311# define EV_USE_SIGNALFD 1 304# define EV_USE_SIGNALFD EV_FEATURE_OS
312# else 305# else
313# define EV_USE_SIGNALFD 0 306# define EV_USE_SIGNALFD 0
314# endif 307# endif
315#endif 308#endif
316 309
319# define EV_USE_4HEAP 1 312# define EV_USE_4HEAP 1
320# define EV_HEAP_CACHE_AT 1 313# define EV_HEAP_CACHE_AT 1
321#endif 314#endif
322 315
323#ifndef EV_VERIFY 316#ifndef EV_VERIFY
324# define EV_VERIFY !EV_MINIMAL 317# define EV_VERIFY (EV_FEATURE_API ? 1 : 0)
325#endif 318#endif
326 319
327#ifndef EV_USE_4HEAP 320#ifndef EV_USE_4HEAP
328# define EV_USE_4HEAP !EV_MINIMAL 321# define EV_USE_4HEAP EV_FEATURE_DATA
329#endif 322#endif
330 323
331#ifndef EV_HEAP_CACHE_AT 324#ifndef EV_HEAP_CACHE_AT
332# define EV_HEAP_CACHE_AT !EV_MINIMAL 325# define EV_HEAP_CACHE_AT EV_FEATURE_DATA
333#endif 326#endif
334 327
335/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */ 328/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
336/* which makes programs even slower. might work on other unices, too. */ 329/* which makes programs even slower. might work on other unices, too. */
337#if EV_USE_CLOCK_SYSCALL 330#if EV_USE_CLOCK_SYSCALL
475 468
476#define expect_false(expr) expect ((expr) != 0, 0) 469#define expect_false(expr) expect ((expr) != 0, 0)
477#define expect_true(expr) expect ((expr) != 0, 1) 470#define expect_true(expr) expect ((expr) != 0, 1)
478#define inline_size static inline 471#define inline_size static inline
479 472
480#if EV_MINIMAL 473#if EV_FEATURE_CODE
474# define inline_speed static inline
475#else
481# define inline_speed static noinline 476# define inline_speed static noinline
482#else
483# define inline_speed static inline
484#endif 477#endif
485 478
486#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 479#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
487 480
488#if EV_MINPRI == EV_MAXPRI 481#if EV_MINPRI == EV_MAXPRI
568} 561}
569 562
570static void * 563static void *
571ev_realloc_emul (void *ptr, long size) 564ev_realloc_emul (void *ptr, long size)
572{ 565{
566#if __GLIBC__
567 return realloc (ptr, size);
568#else
573 /* some systems, notably openbsd and darwin, fail to properly 569 /* some systems, notably openbsd and darwin, fail to properly
574 * implement realloc (x, 0) (as required by both ansi c-98 and 570 * implement realloc (x, 0) (as required by both ansi c-89 and
575 * the single unix specification, so work around them here. 571 * the single unix specification, so work around them here.
576 */ 572 */
573
577 if (size) 574 if (size)
578 return realloc (ptr, size); 575 return realloc (ptr, size);
579 576
580 free (ptr); 577 free (ptr);
581 return 0; 578 return 0;
579#endif
582} 580}
583 581
584static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; 582static void *(*alloc)(void *ptr, long size) = ev_realloc_emul;
585 583
586void 584void
690 688
691 static int ev_default_loop_ptr; 689 static int ev_default_loop_ptr;
692 690
693#endif 691#endif
694 692
695#if EV_MINIMAL < 2 693#if EV_FEATURE_API
696# define EV_RELEASE_CB if (expect_false (release_cb)) release_cb (EV_A) 694# define EV_RELEASE_CB if (expect_false (release_cb)) release_cb (EV_A)
697# define EV_ACQUIRE_CB if (expect_false (acquire_cb)) acquire_cb (EV_A) 695# define EV_ACQUIRE_CB if (expect_false (acquire_cb)) acquire_cb (EV_A)
698# define EV_INVOKE_PENDING invoke_cb (EV_A) 696# define EV_INVOKE_PENDING invoke_cb (EV_A)
699#else 697#else
700# define EV_RELEASE_CB (void)0 698# define EV_RELEASE_CB (void)0
885} 883}
886 884
887/*****************************************************************************/ 885/*****************************************************************************/
888 886
889inline_speed void 887inline_speed void
890fd_event_nc (EV_P_ int fd, int revents) 888fd_event_nocheck (EV_P_ int fd, int revents)
891{ 889{
892 ANFD *anfd = anfds + fd; 890 ANFD *anfd = anfds + fd;
893 ev_io *w; 891 ev_io *w;
894 892
895 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) 893 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
907fd_event (EV_P_ int fd, int revents) 905fd_event (EV_P_ int fd, int revents)
908{ 906{
909 ANFD *anfd = anfds + fd; 907 ANFD *anfd = anfds + fd;
910 908
911 if (expect_true (!anfd->reify)) 909 if (expect_true (!anfd->reify))
912 fd_event_nc (EV_A_ fd, revents); 910 fd_event_nocheck (EV_A_ fd, revents);
913} 911}
914 912
915void 913void
916ev_feed_fd_event (EV_P_ int fd, int revents) 914ev_feed_fd_event (EV_P_ int fd, int revents)
917{ 915{
918 if (fd >= 0 && fd < anfdmax) 916 if (fd >= 0 && fd < anfdmax)
919 fd_event_nc (EV_A_ fd, revents); 917 fd_event_nocheck (EV_A_ fd, revents);
920} 918}
921 919
922/* make sure the external fd watch events are in-sync */ 920/* make sure the external fd watch events are in-sync */
923/* with the kernel/libev internal state */ 921/* with the kernel/libev internal state */
924inline_size void 922inline_size void
987 ev_io_stop (EV_A_ w); 985 ev_io_stop (EV_A_ w);
988 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 986 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
989 } 987 }
990} 988}
991 989
992/* check whether the given fd is atcually valid, for error recovery */ 990/* check whether the given fd is actually valid, for error recovery */
993inline_size int 991inline_size int
994fd_valid (int fd) 992fd_valid (int fd)
995{ 993{
996#ifdef _WIN32 994#ifdef _WIN32
997 return EV_FD_TO_WIN32_HANDLE (fd) != -1; 995 return EV_FD_TO_WIN32_HANDLE (fd) != -1;
1037 { 1035 {
1038 anfds [fd].events = 0; 1036 anfds [fd].events = 0;
1039 anfds [fd].emask = 0; 1037 anfds [fd].emask = 0;
1040 fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY); 1038 fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY);
1041 } 1039 }
1040}
1041
1042/* used to prepare libev internal fd's */
1043/* this is not fork-safe */
1044inline_speed void
1045fd_intern (int fd)
1046{
1047#ifdef _WIN32
1048 unsigned long arg = 1;
1049 ioctlsocket (EV_FD_TO_WIN32_HANDLE (fd), FIONBIO, &arg);
1050#else
1051 fcntl (fd, F_SETFD, FD_CLOEXEC);
1052 fcntl (fd, F_SETFL, O_NONBLOCK);
1053#endif
1042} 1054}
1043 1055
1044/*****************************************************************************/ 1056/*****************************************************************************/
1045 1057
1046/* 1058/*
1200 1212
1201static ANSIG signals [EV_NSIG - 1]; 1213static ANSIG signals [EV_NSIG - 1];
1202 1214
1203/*****************************************************************************/ 1215/*****************************************************************************/
1204 1216
1205/* used to prepare libev internal fd's */ 1217#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1206/* this is not fork-safe */
1207inline_speed void
1208fd_intern (int fd)
1209{
1210#ifdef _WIN32
1211 unsigned long arg = 1;
1212 ioctlsocket (EV_FD_TO_WIN32_HANDLE (fd), FIONBIO, &arg);
1213#else
1214 fcntl (fd, F_SETFD, FD_CLOEXEC);
1215 fcntl (fd, F_SETFL, O_NONBLOCK);
1216#endif
1217}
1218 1218
1219static void noinline 1219static void noinline
1220evpipe_init (EV_P) 1220evpipe_init (EV_P)
1221{ 1221{
1222 if (!ev_is_active (&pipe_w)) 1222 if (!ev_is_active (&pipe_w))
1223 { 1223 {
1224#if EV_USE_EVENTFD 1224# if EV_USE_EVENTFD
1225 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); 1225 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
1226 if (evfd < 0 && errno == EINVAL) 1226 if (evfd < 0 && errno == EINVAL)
1227 evfd = eventfd (0, 0); 1227 evfd = eventfd (0, 0);
1228 1228
1229 if (evfd >= 0) 1229 if (evfd >= 0)
1231 evpipe [0] = -1; 1231 evpipe [0] = -1;
1232 fd_intern (evfd); /* doing it twice doesn't hurt */ 1232 fd_intern (evfd); /* doing it twice doesn't hurt */
1233 ev_io_set (&pipe_w, evfd, EV_READ); 1233 ev_io_set (&pipe_w, evfd, EV_READ);
1234 } 1234 }
1235 else 1235 else
1236#endif 1236# endif
1237 { 1237 {
1238 while (pipe (evpipe)) 1238 while (pipe (evpipe))
1239 ev_syserr ("(libev) error creating signal/async pipe"); 1239 ev_syserr ("(libev) error creating signal/async pipe");
1240 1240
1241 fd_intern (evpipe [0]); 1241 fd_intern (evpipe [0]);
1252evpipe_write (EV_P_ EV_ATOMIC_T *flag) 1252evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1253{ 1253{
1254 if (!*flag) 1254 if (!*flag)
1255 { 1255 {
1256 int old_errno = errno; /* save errno because write might clobber it */ 1256 int old_errno = errno; /* save errno because write might clobber it */
1257 char dummy;
1257 1258
1258 *flag = 1; 1259 *flag = 1;
1259 1260
1260#if EV_USE_EVENTFD 1261#if EV_USE_EVENTFD
1261 if (evfd >= 0) 1262 if (evfd >= 0)
1263 uint64_t counter = 1; 1264 uint64_t counter = 1;
1264 write (evfd, &counter, sizeof (uint64_t)); 1265 write (evfd, &counter, sizeof (uint64_t));
1265 } 1266 }
1266 else 1267 else
1267#endif 1268#endif
1268 write (evpipe [1], &old_errno, 1); 1269 write (evpipe [1], &dummy, 1);
1269 1270
1270 errno = old_errno; 1271 errno = old_errno;
1271 } 1272 }
1272} 1273}
1273 1274
1374 break; 1375 break;
1375 } 1376 }
1376} 1377}
1377#endif 1378#endif
1378 1379
1380#endif
1381
1379/*****************************************************************************/ 1382/*****************************************************************************/
1380 1383
1384#if EV_CHILD_ENABLE
1381static WL childs [EV_PID_HASHSIZE]; 1385static WL childs [EV_PID_HASHSIZE];
1382
1383#ifndef _WIN32
1384 1386
1385static ev_signal childev; 1387static ev_signal childev;
1386 1388
1387#ifndef WIFCONTINUED 1389#ifndef WIFCONTINUED
1388# define WIFCONTINUED(status) 0 1390# define WIFCONTINUED(status) 0
1393child_reap (EV_P_ int chain, int pid, int status) 1395child_reap (EV_P_ int chain, int pid, int status)
1394{ 1396{
1395 ev_child *w; 1397 ev_child *w;
1396 int traced = WIFSTOPPED (status) || WIFCONTINUED (status); 1398 int traced = WIFSTOPPED (status) || WIFCONTINUED (status);
1397 1399
1398 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 1400 for (w = (ev_child *)childs [chain & ((EV_PID_HASHSIZE) - 1)]; w; w = (ev_child *)((WL)w)->next)
1399 { 1401 {
1400 if ((w->pid == pid || !w->pid) 1402 if ((w->pid == pid || !w->pid)
1401 && (!traced || (w->flags & 1))) 1403 && (!traced || (w->flags & 1)))
1402 { 1404 {
1403 ev_set_priority (w, EV_MAXPRI); /* need to do it *now*, this *must* be the same prio as the signal watcher itself */ 1405 ev_set_priority (w, EV_MAXPRI); /* need to do it *now*, this *must* be the same prio as the signal watcher itself */
1428 /* make sure we are called again until all children have been reaped */ 1430 /* make sure we are called again until all children have been reaped */
1429 /* we need to do it this way so that the callback gets called before we continue */ 1431 /* we need to do it this way so that the callback gets called before we continue */
1430 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); 1432 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
1431 1433
1432 child_reap (EV_A_ pid, pid, status); 1434 child_reap (EV_A_ pid, pid, status);
1433 if (EV_PID_HASHSIZE > 1) 1435 if ((EV_PID_HASHSIZE) > 1)
1434 child_reap (EV_A_ 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */ 1436 child_reap (EV_A_ 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
1435} 1437}
1436 1438
1437#endif 1439#endif
1438 1440
1527ev_backend (EV_P) 1529ev_backend (EV_P)
1528{ 1530{
1529 return backend; 1531 return backend;
1530} 1532}
1531 1533
1532#if EV_MINIMAL < 2 1534#if EV_FEATURE_API
1533unsigned int 1535unsigned int
1534ev_loop_count (EV_P) 1536ev_loop_count (EV_P)
1535{ 1537{
1536 return loop_count; 1538 return loop_count;
1537} 1539}
1617 1619
1618 ev_rt_now = ev_time (); 1620 ev_rt_now = ev_time ();
1619 mn_now = get_clock (); 1621 mn_now = get_clock ();
1620 now_floor = mn_now; 1622 now_floor = mn_now;
1621 rtmn_diff = ev_rt_now - mn_now; 1623 rtmn_diff = ev_rt_now - mn_now;
1622#if EV_MINIMAL < 2 1624#if EV_FEATURE_API
1623 invoke_cb = ev_invoke_pending; 1625 invoke_cb = ev_invoke_pending;
1624#endif 1626#endif
1625 1627
1626 io_blocktime = 0.; 1628 io_blocktime = 0.;
1627 timeout_blocktime = 0.; 1629 timeout_blocktime = 0.;
1657 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); 1659 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
1658#endif 1660#endif
1659 1661
1660 ev_prepare_init (&pending_w, pendingcb); 1662 ev_prepare_init (&pending_w, pendingcb);
1661 1663
1664#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1662 ev_init (&pipe_w, pipecb); 1665 ev_init (&pipe_w, pipecb);
1663 ev_set_priority (&pipe_w, EV_MAXPRI); 1666 ev_set_priority (&pipe_w, EV_MAXPRI);
1667#endif
1664 } 1668 }
1665} 1669}
1666 1670
1667/* free up a loop structure */ 1671/* free up a loop structure */
1668static void noinline 1672static void noinline
1786 { 1790 {
1787 EV_WIN32_CLOSE_FD (evpipe [0]); 1791 EV_WIN32_CLOSE_FD (evpipe [0]);
1788 EV_WIN32_CLOSE_FD (evpipe [1]); 1792 EV_WIN32_CLOSE_FD (evpipe [1]);
1789 } 1793 }
1790 1794
1795#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1791 evpipe_init (EV_A); 1796 evpipe_init (EV_A);
1792 /* now iterate over everything, in case we missed something */ 1797 /* now iterate over everything, in case we missed something */
1793 pipecb (EV_A_ &pipe_w, EV_READ); 1798 pipecb (EV_A_ &pipe_w, EV_READ);
1799#endif
1794 } 1800 }
1795 1801
1796 postfork = 0; 1802 postfork = 0;
1797} 1803}
1798 1804
1860 verify_watcher (EV_A_ ws [cnt]); 1866 verify_watcher (EV_A_ ws [cnt]);
1861 } 1867 }
1862} 1868}
1863#endif 1869#endif
1864 1870
1865#if EV_MINIMAL < 2 1871#if EV_FEATURE_API
1866void 1872void
1867ev_loop_verify (EV_P) 1873ev_loop_verify (EV_P)
1868{ 1874{
1869#if EV_VERIFY 1875#if EV_VERIFY
1870 int i; 1876 int i;
1911#if EV_ASYNC_ENABLE 1917#if EV_ASYNC_ENABLE
1912 assert (asyncmax >= asynccnt); 1918 assert (asyncmax >= asynccnt);
1913 array_verify (EV_A_ (W *)asyncs, asynccnt); 1919 array_verify (EV_A_ (W *)asyncs, asynccnt);
1914#endif 1920#endif
1915 1921
1922#if EV_PREPARE_ENABLE
1916 assert (preparemax >= preparecnt); 1923 assert (preparemax >= preparecnt);
1917 array_verify (EV_A_ (W *)prepares, preparecnt); 1924 array_verify (EV_A_ (W *)prepares, preparecnt);
1925#endif
1918 1926
1927#if EV_CHECK_ENABLE
1919 assert (checkmax >= checkcnt); 1928 assert (checkmax >= checkcnt);
1920 array_verify (EV_A_ (W *)checks, checkcnt); 1929 array_verify (EV_A_ (W *)checks, checkcnt);
1930#endif
1921 1931
1922# if 0 1932# if 0
1933#if EV_CHILD_ENABLE
1923 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 1934 for (w = (ev_child *)childs [chain & ((EV_PID_HASHSIZE) - 1)]; w; w = (ev_child *)((WL)w)->next)
1924 for (signum = EV_NSIG; signum--; ) if (signals [signum].pending) 1935 for (signum = EV_NSIG; signum--; ) if (signals [signum].pending)
1936#endif
1925# endif 1937# endif
1926#endif 1938#endif
1927} 1939}
1928#endif 1940#endif
1929 1941
1945 1957
1946 loop_init (EV_A_ flags); 1958 loop_init (EV_A_ flags);
1947 1959
1948 if (ev_backend (EV_A)) 1960 if (ev_backend (EV_A))
1949 { 1961 {
1950#ifndef _WIN32 1962#if EV_CHILD_ENABLE
1951 ev_signal_init (&childev, childcb, SIGCHLD); 1963 ev_signal_init (&childev, childcb, SIGCHLD);
1952 ev_set_priority (&childev, EV_MAXPRI); 1964 ev_set_priority (&childev, EV_MAXPRI);
1953 ev_signal_start (EV_A_ &childev); 1965 ev_signal_start (EV_A_ &childev);
1954 ev_unref (EV_A); /* child watcher should not keep loop alive */ 1966 ev_unref (EV_A); /* child watcher should not keep loop alive */
1955#endif 1967#endif
1968 EV_P = ev_default_loop_ptr; 1980 EV_P = ev_default_loop_ptr;
1969#endif 1981#endif
1970 1982
1971 ev_default_loop_ptr = 0; 1983 ev_default_loop_ptr = 0;
1972 1984
1973#ifndef _WIN32 1985#if EV_CHILD_ENABLE
1974 ev_ref (EV_A); /* child watcher */ 1986 ev_ref (EV_A); /* child watcher */
1975 ev_signal_stop (EV_A_ &childev); 1987 ev_signal_stop (EV_A_ &childev);
1976#endif 1988#endif
1977 1989
1978 loop_destroy (EV_A); 1990 loop_destroy (EV_A);
2255} 2267}
2256 2268
2257void 2269void
2258ev_loop (EV_P_ int flags) 2270ev_loop (EV_P_ int flags)
2259{ 2271{
2260#if EV_MINIMAL < 2 2272#if EV_FEATURE_API
2261 ++loop_depth; 2273 ++loop_depth;
2262#endif 2274#endif
2263 2275
2264 assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE)); 2276 assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE));
2265 2277
2290 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 2302 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
2291 EV_INVOKE_PENDING; 2303 EV_INVOKE_PENDING;
2292 } 2304 }
2293#endif 2305#endif
2294 2306
2307#if EV_PREPARE_ENABLE
2295 /* queue prepare watchers (and execute them) */ 2308 /* queue prepare watchers (and execute them) */
2296 if (expect_false (preparecnt)) 2309 if (expect_false (preparecnt))
2297 { 2310 {
2298 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 2311 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
2299 EV_INVOKE_PENDING; 2312 EV_INVOKE_PENDING;
2300 } 2313 }
2314#endif
2301 2315
2302 if (expect_false (loop_done)) 2316 if (expect_false (loop_done))
2303 break; 2317 break;
2304 2318
2305 /* we might have forked, so reify kernel state if necessary */ 2319 /* we might have forked, so reify kernel state if necessary */
2356 waittime -= sleeptime; 2370 waittime -= sleeptime;
2357 } 2371 }
2358 } 2372 }
2359 } 2373 }
2360 2374
2361#if EV_MINIMAL < 2 2375#if EV_FEATURE_API
2362 ++loop_count; 2376 ++loop_count;
2363#endif 2377#endif
2364 assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */ 2378 assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */
2365 backend_poll (EV_A_ waittime); 2379 backend_poll (EV_A_ waittime);
2366 assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */ 2380 assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */
2378#if EV_IDLE_ENABLE 2392#if EV_IDLE_ENABLE
2379 /* queue idle watchers unless other events are pending */ 2393 /* queue idle watchers unless other events are pending */
2380 idle_reify (EV_A); 2394 idle_reify (EV_A);
2381#endif 2395#endif
2382 2396
2397#if EV_CHECK_ENABLE
2383 /* queue check watchers, to be executed first */ 2398 /* queue check watchers, to be executed first */
2384 if (expect_false (checkcnt)) 2399 if (expect_false (checkcnt))
2385 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 2400 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
2401#endif
2386 2402
2387 EV_INVOKE_PENDING; 2403 EV_INVOKE_PENDING;
2388 } 2404 }
2389 while (expect_true ( 2405 while (expect_true (
2390 activecnt 2406 activecnt
2393 )); 2409 ));
2394 2410
2395 if (loop_done == EVUNLOOP_ONE) 2411 if (loop_done == EVUNLOOP_ONE)
2396 loop_done = EVUNLOOP_CANCEL; 2412 loop_done = EVUNLOOP_CANCEL;
2397 2413
2398#if EV_MINIMAL < 2 2414#if EV_FEATURE_API
2399 --loop_depth; 2415 --loop_depth;
2400#endif 2416#endif
2401} 2417}
2402 2418
2403void 2419void
2720 2736
2721#ifndef SA_RESTART 2737#ifndef SA_RESTART
2722# define SA_RESTART 0 2738# define SA_RESTART 0
2723#endif 2739#endif
2724 2740
2741#if EV_SIGNAL_ENABLE
2742
2725void noinline 2743void noinline
2726ev_signal_start (EV_P_ ev_signal *w) 2744ev_signal_start (EV_P_ ev_signal *w)
2727{ 2745{
2728 if (expect_false (ev_is_active (w))) 2746 if (expect_false (ev_is_active (w)))
2729 return; 2747 return;
2835 } 2853 }
2836 2854
2837 EV_FREQUENT_CHECK; 2855 EV_FREQUENT_CHECK;
2838} 2856}
2839 2857
2858#endif
2859
2860#if EV_CHILD_ENABLE
2861
2840void 2862void
2841ev_child_start (EV_P_ ev_child *w) 2863ev_child_start (EV_P_ ev_child *w)
2842{ 2864{
2843#if EV_MULTIPLICITY 2865#if EV_MULTIPLICITY
2844 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 2866 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
2847 return; 2869 return;
2848 2870
2849 EV_FREQUENT_CHECK; 2871 EV_FREQUENT_CHECK;
2850 2872
2851 ev_start (EV_A_ (W)w, 1); 2873 ev_start (EV_A_ (W)w, 1);
2852 wlist_add (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 2874 wlist_add (&childs [w->pid & ((EV_PID_HASHSIZE) - 1)], (WL)w);
2853 2875
2854 EV_FREQUENT_CHECK; 2876 EV_FREQUENT_CHECK;
2855} 2877}
2856 2878
2857void 2879void
2861 if (expect_false (!ev_is_active (w))) 2883 if (expect_false (!ev_is_active (w)))
2862 return; 2884 return;
2863 2885
2864 EV_FREQUENT_CHECK; 2886 EV_FREQUENT_CHECK;
2865 2887
2866 wlist_del (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 2888 wlist_del (&childs [w->pid & ((EV_PID_HASHSIZE) - 1)], (WL)w);
2867 ev_stop (EV_A_ (W)w); 2889 ev_stop (EV_A_ (W)w);
2868 2890
2869 EV_FREQUENT_CHECK; 2891 EV_FREQUENT_CHECK;
2870} 2892}
2893
2894#endif
2871 2895
2872#if EV_STAT_ENABLE 2896#if EV_STAT_ENABLE
2873 2897
2874# ifdef _WIN32 2898# ifdef _WIN32
2875# undef lstat 2899# undef lstat
2942 while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); 2966 while (w->wd < 0 && (errno == ENOENT || errno == EACCES));
2943 } 2967 }
2944 } 2968 }
2945 2969
2946 if (w->wd >= 0) 2970 if (w->wd >= 0)
2947 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); 2971 wlist_add (&fs_hash [w->wd & ((EV_INOTIFY_HASHSIZE) - 1)].head, (WL)w);
2948 2972
2949 /* now re-arm timer, if required */ 2973 /* now re-arm timer, if required */
2950 if (ev_is_active (&w->timer)) ev_ref (EV_A); 2974 if (ev_is_active (&w->timer)) ev_ref (EV_A);
2951 ev_timer_again (EV_A_ &w->timer); 2975 ev_timer_again (EV_A_ &w->timer);
2952 if (ev_is_active (&w->timer)) ev_unref (EV_A); 2976 if (ev_is_active (&w->timer)) ev_unref (EV_A);
2960 2984
2961 if (wd < 0) 2985 if (wd < 0)
2962 return; 2986 return;
2963 2987
2964 w->wd = -2; 2988 w->wd = -2;
2965 slot = wd & (EV_INOTIFY_HASHSIZE - 1); 2989 slot = wd & ((EV_INOTIFY_HASHSIZE) - 1);
2966 wlist_del (&fs_hash [slot].head, (WL)w); 2990 wlist_del (&fs_hash [slot].head, (WL)w);
2967 2991
2968 /* remove this watcher, if others are watching it, they will rearm */ 2992 /* remove this watcher, if others are watching it, they will rearm */
2969 inotify_rm_watch (fs_fd, wd); 2993 inotify_rm_watch (fs_fd, wd);
2970} 2994}
2972static void noinline 2996static void noinline
2973infy_wd (EV_P_ int slot, int wd, struct inotify_event *ev) 2997infy_wd (EV_P_ int slot, int wd, struct inotify_event *ev)
2974{ 2998{
2975 if (slot < 0) 2999 if (slot < 0)
2976 /* overflow, need to check for all hash slots */ 3000 /* overflow, need to check for all hash slots */
2977 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot) 3001 for (slot = 0; slot < (EV_INOTIFY_HASHSIZE); ++slot)
2978 infy_wd (EV_A_ slot, wd, ev); 3002 infy_wd (EV_A_ slot, wd, ev);
2979 else 3003 else
2980 { 3004 {
2981 WL w_; 3005 WL w_;
2982 3006
2983 for (w_ = fs_hash [slot & (EV_INOTIFY_HASHSIZE - 1)].head; w_; ) 3007 for (w_ = fs_hash [slot & ((EV_INOTIFY_HASHSIZE) - 1)].head; w_; )
2984 { 3008 {
2985 ev_stat *w = (ev_stat *)w_; 3009 ev_stat *w = (ev_stat *)w_;
2986 w_ = w_->next; /* lets us remove this watcher and all before it */ 3010 w_ = w_->next; /* lets us remove this watcher and all before it */
2987 3011
2988 if (w->wd == wd || wd == -1) 3012 if (w->wd == wd || wd == -1)
2989 { 3013 {
2990 if (ev->mask & (IN_IGNORED | IN_UNMOUNT | IN_DELETE_SELF)) 3014 if (ev->mask & (IN_IGNORED | IN_UNMOUNT | IN_DELETE_SELF))
2991 { 3015 {
2992 wlist_del (&fs_hash [slot & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); 3016 wlist_del (&fs_hash [slot & ((EV_INOTIFY_HASHSIZE) - 1)].head, (WL)w);
2993 w->wd = -1; 3017 w->wd = -1;
2994 infy_add (EV_A_ w); /* re-add, no matter what */ 3018 infy_add (EV_A_ w); /* re-add, no matter what */
2995 } 3019 }
2996 3020
2997 stat_timer_cb (EV_A_ &w->timer, 0); 3021 stat_timer_cb (EV_A_ &w->timer, 0);
3111 ev_io_set (&fs_w, fs_fd, EV_READ); 3135 ev_io_set (&fs_w, fs_fd, EV_READ);
3112 ev_io_start (EV_A_ &fs_w); 3136 ev_io_start (EV_A_ &fs_w);
3113 ev_unref (EV_A); 3137 ev_unref (EV_A);
3114 } 3138 }
3115 3139
3116 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot) 3140 for (slot = 0; slot < (EV_INOTIFY_HASHSIZE); ++slot)
3117 { 3141 {
3118 WL w_ = fs_hash [slot].head; 3142 WL w_ = fs_hash [slot].head;
3119 fs_hash [slot].head = 0; 3143 fs_hash [slot].head = 0;
3120 3144
3121 while (w_) 3145 while (w_)
3296 3320
3297 EV_FREQUENT_CHECK; 3321 EV_FREQUENT_CHECK;
3298} 3322}
3299#endif 3323#endif
3300 3324
3325#if EV_PREPARE_ENABLE
3301void 3326void
3302ev_prepare_start (EV_P_ ev_prepare *w) 3327ev_prepare_start (EV_P_ ev_prepare *w)
3303{ 3328{
3304 if (expect_false (ev_is_active (w))) 3329 if (expect_false (ev_is_active (w)))
3305 return; 3330 return;
3331 3356
3332 ev_stop (EV_A_ (W)w); 3357 ev_stop (EV_A_ (W)w);
3333 3358
3334 EV_FREQUENT_CHECK; 3359 EV_FREQUENT_CHECK;
3335} 3360}
3361#endif
3336 3362
3363#if EV_CHECK_ENABLE
3337void 3364void
3338ev_check_start (EV_P_ ev_check *w) 3365ev_check_start (EV_P_ ev_check *w)
3339{ 3366{
3340 if (expect_false (ev_is_active (w))) 3367 if (expect_false (ev_is_active (w)))
3341 return; 3368 return;
3367 3394
3368 ev_stop (EV_A_ (W)w); 3395 ev_stop (EV_A_ (W)w);
3369 3396
3370 EV_FREQUENT_CHECK; 3397 EV_FREQUENT_CHECK;
3371} 3398}
3399#endif
3372 3400
3373#if EV_EMBED_ENABLE 3401#if EV_EMBED_ENABLE
3374void noinline 3402void noinline
3375ev_embed_sweep (EV_P_ ev_embed *w) 3403ev_embed_sweep (EV_P_ ev_embed *w)
3376{ 3404{
3705 if (types & EV_ASYNC) 3733 if (types & EV_ASYNC)
3706 for (i = asynccnt; i--; ) 3734 for (i = asynccnt; i--; )
3707 cb (EV_A_ EV_ASYNC, asyncs [i]); 3735 cb (EV_A_ EV_ASYNC, asyncs [i]);
3708#endif 3736#endif
3709 3737
3738#if EV_PREPARE_ENABLE
3710 if (types & EV_PREPARE) 3739 if (types & EV_PREPARE)
3711 for (i = preparecnt; i--; ) 3740 for (i = preparecnt; i--; )
3712#if EV_EMBED_ENABLE 3741# if EV_EMBED_ENABLE
3713 if (ev_cb (prepares [i]) != embed_prepare_cb) 3742 if (ev_cb (prepares [i]) != embed_prepare_cb)
3714#endif 3743# endif
3715 cb (EV_A_ EV_PREPARE, prepares [i]); 3744 cb (EV_A_ EV_PREPARE, prepares [i]);
3745#endif
3716 3746
3747#if EV_CHECK_ENABLE
3717 if (types & EV_CHECK) 3748 if (types & EV_CHECK)
3718 for (i = checkcnt; i--; ) 3749 for (i = checkcnt; i--; )
3719 cb (EV_A_ EV_CHECK, checks [i]); 3750 cb (EV_A_ EV_CHECK, checks [i]);
3751#endif
3720 3752
3753#if EV_SIGNAL_ENABLE
3721 if (types & EV_SIGNAL) 3754 if (types & EV_SIGNAL)
3722 for (i = 0; i < EV_NSIG - 1; ++i) 3755 for (i = 0; i < EV_NSIG - 1; ++i)
3723 for (wl = signals [i].head; wl; ) 3756 for (wl = signals [i].head; wl; )
3724 { 3757 {
3725 wn = wl->next; 3758 wn = wl->next;
3726 cb (EV_A_ EV_SIGNAL, wl); 3759 cb (EV_A_ EV_SIGNAL, wl);
3727 wl = wn; 3760 wl = wn;
3728 } 3761 }
3762#endif
3729 3763
3764#if EV_CHILD_ENABLE
3730 if (types & EV_CHILD) 3765 if (types & EV_CHILD)
3731 for (i = EV_PID_HASHSIZE; i--; ) 3766 for (i = (EV_PID_HASHSIZE); i--; )
3732 for (wl = childs [i]; wl; ) 3767 for (wl = childs [i]; wl; )
3733 { 3768 {
3734 wn = wl->next; 3769 wn = wl->next;
3735 cb (EV_A_ EV_CHILD, wl); 3770 cb (EV_A_ EV_CHILD, wl);
3736 wl = wn; 3771 wl = wn;
3737 } 3772 }
3773#endif
3738/* EV_STAT 0x00001000 /* stat data changed */ 3774/* EV_STAT 0x00001000 /* stat data changed */
3739/* EV_EMBED 0x00010000 /* embedded event loop needs sweep */ 3775/* EV_EMBED 0x00010000 /* embedded event loop needs sweep */
3740} 3776}
3741#endif 3777#endif
3742 3778

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines