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

Comparing libev/ev.c (file contents):
Revision 1.330 by root, Tue Mar 9 08:46:17 2010 UTC vs.
Revision 1.345 by sf-exg, Sat Jul 31 22:33:26 2010 UTC

77# ifndef EV_USE_REALTIME 77# ifndef EV_USE_REALTIME
78# define EV_USE_REALTIME 0 78# define EV_USE_REALTIME 0
79# endif 79# endif
80# endif 80# endif
81 81
82# if HAVE_NANOSLEEP
82# ifndef EV_USE_NANOSLEEP 83# ifndef EV_USE_NANOSLEEP
83# if HAVE_NANOSLEEP
84# define EV_USE_NANOSLEEP 1 84# define EV_USE_NANOSLEEP EV_FEATURE_OS
85# endif
85# else 86# else
87# undef EV_USE_NANOSLEEP
86# define EV_USE_NANOSLEEP 0 88# define EV_USE_NANOSLEEP 0
89# endif
90
91# if HAVE_SELECT && HAVE_SYS_SELECT_H
92# ifndef EV_USE_SELECT
93# define EV_USE_SELECT EV_FEATURE_BACKENDS
87# endif 94# endif
95# else
96# undef EV_USE_SELECT
97# define EV_USE_SELECT 0
88# endif 98# endif
89 99
100# if HAVE_POLL && HAVE_POLL_H
90# ifndef EV_USE_SELECT 101# ifndef EV_USE_POLL
91# if HAVE_SELECT && HAVE_SYS_SELECT_H 102# define EV_USE_POLL EV_FEATURE_BACKENDS
92# define EV_USE_SELECT 1
93# else
94# define EV_USE_SELECT 0
95# endif 103# endif
96# endif
97
98# ifndef EV_USE_POLL
99# if HAVE_POLL && HAVE_POLL_H
100# define EV_USE_POLL 1
101# else 104# else
105# undef EV_USE_POLL
102# define EV_USE_POLL 0 106# define EV_USE_POLL 0
103# endif
104# endif 107# endif
105 108
106# ifndef EV_USE_EPOLL
107# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H 109# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
108# define EV_USE_EPOLL 1 110# ifndef EV_USE_EPOLL
109# else 111# define EV_USE_EPOLL EV_FEATURE_BACKENDS
110# define EV_USE_EPOLL 0
111# endif 112# endif
113# else
114# undef EV_USE_EPOLL
115# define EV_USE_EPOLL 0
112# endif 116# endif
113 117
114# ifndef EV_USE_KQUEUE
115# if HAVE_KQUEUE && HAVE_SYS_EVENT_H 118# if HAVE_KQUEUE && HAVE_SYS_EVENT_H
116# define EV_USE_KQUEUE 1 119# ifndef EV_USE_KQUEUE
117# else 120# define EV_USE_KQUEUE EV_FEATURE_BACKENDS
118# define EV_USE_KQUEUE 0
119# endif 121# endif
122# else
123# undef EV_USE_KQUEUE
124# define EV_USE_KQUEUE 0
120# endif 125# endif
121 126
122# ifndef EV_USE_PORT
123# if HAVE_PORT_H && HAVE_PORT_CREATE 127# if HAVE_PORT_H && HAVE_PORT_CREATE
124# define EV_USE_PORT 1 128# ifndef EV_USE_PORT
125# else 129# define EV_USE_PORT EV_FEATURE_BACKENDS
126# define EV_USE_PORT 0
127# endif 130# endif
131# else
132# undef EV_USE_PORT
133# define EV_USE_PORT 0
128# endif 134# endif
129 135
130# ifndef EV_USE_INOTIFY
131# if HAVE_INOTIFY_INIT && HAVE_SYS_INOTIFY_H 136# if HAVE_INOTIFY_INIT && HAVE_SYS_INOTIFY_H
132# define EV_USE_INOTIFY 1 137# ifndef EV_USE_INOTIFY
133# else
134# define EV_USE_INOTIFY 0 138# define EV_USE_INOTIFY EV_FEATURE_OS
135# endif 139# endif
140# else
141# undef EV_USE_INOTIFY
142# define EV_USE_INOTIFY 0
136# endif 143# endif
137 144
138# ifndef EV_USE_SIGNALFD
139# if HAVE_SIGNALFD && HAVE_SYS_SIGNALFD_H 145# if HAVE_SIGNALFD && HAVE_SYS_SIGNALFD_H
140# define EV_USE_SIGNALFD 1 146# ifndef EV_USE_SIGNALFD
141# else
142# define EV_USE_SIGNALFD 0 147# define EV_USE_SIGNALFD EV_FEATURE_OS
143# endif 148# endif
149# else
150# undef EV_USE_SIGNALFD
151# define EV_USE_SIGNALFD 0
144# endif 152# endif
145 153
154# if HAVE_EVENTFD
146# ifndef EV_USE_EVENTFD 155# ifndef EV_USE_EVENTFD
147# if HAVE_EVENTFD
148# define EV_USE_EVENTFD 1 156# define EV_USE_EVENTFD EV_FEATURE_OS
149# else
150# define EV_USE_EVENTFD 0
151# endif 157# endif
158# else
159# undef EV_USE_EVENTFD
160# define EV_USE_EVENTFD 0
152# endif 161# endif
153 162
154#endif 163#endif
155 164
156#include <math.h> 165#include <math.h>
184# define WIN32_LEAN_AND_MEAN 193# define WIN32_LEAN_AND_MEAN
185# include <windows.h> 194# include <windows.h>
186# ifndef EV_SELECT_IS_WINSOCKET 195# ifndef EV_SELECT_IS_WINSOCKET
187# define EV_SELECT_IS_WINSOCKET 1 196# define EV_SELECT_IS_WINSOCKET 1
188# endif 197# endif
198# undef EV_AVOID_STDIO
189#endif 199#endif
200
201/* OS X, in its infinite idiocy, actually HARDCODES
202 * a limit of 1024 into their select. Where people have brains,
203 * OS X engineers apparently have a vacuum. Or maybe they were
204 * ordered to have a vacuum, or they do anything for money.
205 * This might help. Or not.
206 */
207#define _DARWIN_UNLIMITED_SELECT 1
190 208
191/* this block tries to deduce configuration from header-defined symbols and defaults */ 209/* this block tries to deduce configuration from header-defined symbols and defaults */
192 210
193/* try to deduce the maximum number of signals on this platform */ 211/* try to deduce the maximum number of signals on this platform */
194#if defined (EV_NSIG) 212#if defined (EV_NSIG)
206#elif defined (MAXSIG) 224#elif defined (MAXSIG)
207# define EV_NSIG (MAXSIG+1) 225# define EV_NSIG (MAXSIG+1)
208#elif defined (MAX_SIG) 226#elif defined (MAX_SIG)
209# define EV_NSIG (MAX_SIG+1) 227# define EV_NSIG (MAX_SIG+1)
210#elif defined (SIGARRAYSIZE) 228#elif defined (SIGARRAYSIZE)
211# define EV_NSIG SIGARRAYSIZE /* Assume ary[SIGARRAYSIZE] */ 229# define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */
212#elif defined (_sys_nsig) 230#elif defined (_sys_nsig)
213# define EV_NSIG (_sys_nsig) /* Solaris 2.5 */ 231# define EV_NSIG (_sys_nsig) /* Solaris 2.5 */
214#else 232#else
215# error "unable to find value for NSIG, please report" 233# error "unable to find value for NSIG, please report"
216/* to make it compile regardless, just remove the above line */ 234/* to make it compile regardless, just remove the above line, */
235/* but consider reporting it, too! :) */
217# define EV_NSIG 65 236# define EV_NSIG 65
218#endif 237#endif
219 238
220#ifndef EV_USE_CLOCK_SYSCALL 239#ifndef EV_USE_CLOCK_SYSCALL
221# if __linux && __GLIBC__ >= 2 240# if __linux && __GLIBC__ >= 2
222# define EV_USE_CLOCK_SYSCALL 1 241# define EV_USE_CLOCK_SYSCALL EV_FEATURE_OS
223# else 242# else
224# define EV_USE_CLOCK_SYSCALL 0 243# define EV_USE_CLOCK_SYSCALL 0
225# endif 244# endif
226#endif 245#endif
227 246
228#ifndef EV_USE_MONOTONIC 247#ifndef EV_USE_MONOTONIC
229# if defined (_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0 248# if defined (_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0
230# define EV_USE_MONOTONIC 1 249# define EV_USE_MONOTONIC EV_FEATURE_OS
231# else 250# else
232# define EV_USE_MONOTONIC 0 251# define EV_USE_MONOTONIC 0
233# endif 252# endif
234#endif 253#endif
235 254
237# define EV_USE_REALTIME !EV_USE_CLOCK_SYSCALL 256# define EV_USE_REALTIME !EV_USE_CLOCK_SYSCALL
238#endif 257#endif
239 258
240#ifndef EV_USE_NANOSLEEP 259#ifndef EV_USE_NANOSLEEP
241# if _POSIX_C_SOURCE >= 199309L 260# if _POSIX_C_SOURCE >= 199309L
242# define EV_USE_NANOSLEEP 1 261# define EV_USE_NANOSLEEP EV_FEATURE_OS
243# else 262# else
244# define EV_USE_NANOSLEEP 0 263# define EV_USE_NANOSLEEP 0
245# endif 264# endif
246#endif 265#endif
247 266
248#ifndef EV_USE_SELECT 267#ifndef EV_USE_SELECT
249# define EV_USE_SELECT 1 268# define EV_USE_SELECT EV_FEATURE_BACKENDS
250#endif 269#endif
251 270
252#ifndef EV_USE_POLL 271#ifndef EV_USE_POLL
253# ifdef _WIN32 272# ifdef _WIN32
254# define EV_USE_POLL 0 273# define EV_USE_POLL 0
255# else 274# else
256# define EV_USE_POLL 1 275# define EV_USE_POLL EV_FEATURE_BACKENDS
257# endif 276# endif
258#endif 277#endif
259 278
260#ifndef EV_USE_EPOLL 279#ifndef EV_USE_EPOLL
261# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4)) 280# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
262# define EV_USE_EPOLL 1 281# define EV_USE_EPOLL EV_FEATURE_BACKENDS
263# else 282# else
264# define EV_USE_EPOLL 0 283# define EV_USE_EPOLL 0
265# endif 284# endif
266#endif 285#endif
267 286
273# define EV_USE_PORT 0 292# define EV_USE_PORT 0
274#endif 293#endif
275 294
276#ifndef EV_USE_INOTIFY 295#ifndef EV_USE_INOTIFY
277# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4)) 296# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
278# define EV_USE_INOTIFY 1 297# define EV_USE_INOTIFY EV_FEATURE_OS
279# else 298# else
280# define EV_USE_INOTIFY 0 299# define EV_USE_INOTIFY 0
281# endif 300# endif
282#endif 301#endif
283 302
284#ifndef EV_PID_HASHSIZE 303#ifndef EV_PID_HASHSIZE
285# if EV_MINIMAL 304# define EV_PID_HASHSIZE EV_FEATURE_DATA ? 16 : 1
286# define EV_PID_HASHSIZE 1
287# else
288# define EV_PID_HASHSIZE 16
289# endif
290#endif 305#endif
291 306
292#ifndef EV_INOTIFY_HASHSIZE 307#ifndef EV_INOTIFY_HASHSIZE
293# if EV_MINIMAL 308# define EV_INOTIFY_HASHSIZE EV_FEATURE_DATA ? 16 : 1
294# define EV_INOTIFY_HASHSIZE 1
295# else
296# define EV_INOTIFY_HASHSIZE 16
297# endif
298#endif 309#endif
299 310
300#ifndef EV_USE_EVENTFD 311#ifndef EV_USE_EVENTFD
301# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) 312# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
302# define EV_USE_EVENTFD 1 313# define EV_USE_EVENTFD EV_FEATURE_OS
303# else 314# else
304# define EV_USE_EVENTFD 0 315# define EV_USE_EVENTFD 0
305# endif 316# endif
306#endif 317#endif
307 318
308#ifndef EV_USE_SIGNALFD 319#ifndef EV_USE_SIGNALFD
309# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) 320# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
310# define EV_USE_SIGNALFD 1 321# define EV_USE_SIGNALFD EV_FEATURE_OS
311# else 322# else
312# define EV_USE_SIGNALFD 0 323# define EV_USE_SIGNALFD 0
313# endif 324# endif
314#endif 325#endif
315 326
318# define EV_USE_4HEAP 1 329# define EV_USE_4HEAP 1
319# define EV_HEAP_CACHE_AT 1 330# define EV_HEAP_CACHE_AT 1
320#endif 331#endif
321 332
322#ifndef EV_VERIFY 333#ifndef EV_VERIFY
323# define EV_VERIFY !EV_MINIMAL 334# define EV_VERIFY (EV_FEATURE_API ? 1 : 0)
324#endif 335#endif
325 336
326#ifndef EV_USE_4HEAP 337#ifndef EV_USE_4HEAP
327# define EV_USE_4HEAP !EV_MINIMAL 338# define EV_USE_4HEAP EV_FEATURE_DATA
328#endif 339#endif
329 340
330#ifndef EV_HEAP_CACHE_AT 341#ifndef EV_HEAP_CACHE_AT
331# define EV_HEAP_CACHE_AT !EV_MINIMAL 342# define EV_HEAP_CACHE_AT EV_FEATURE_DATA
332#endif 343#endif
333 344
334/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */ 345/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
335/* which makes programs even slower. might work on other unices, too. */ 346/* which makes programs even slower. might work on other unices, too. */
336#if EV_USE_CLOCK_SYSCALL 347#if EV_USE_CLOCK_SYSCALL
441 452
442 453
443/**/ 454/**/
444 455
445#if EV_VERIFY >= 3 456#if EV_VERIFY >= 3
446# define EV_FREQUENT_CHECK ev_loop_verify (EV_A) 457# define EV_FREQUENT_CHECK ev_verify (EV_A)
447#else 458#else
448# define EV_FREQUENT_CHECK do { } while (0) 459# define EV_FREQUENT_CHECK do { } while (0)
449#endif 460#endif
450 461
451/* 462/*
474 485
475#define expect_false(expr) expect ((expr) != 0, 0) 486#define expect_false(expr) expect ((expr) != 0, 0)
476#define expect_true(expr) expect ((expr) != 0, 1) 487#define expect_true(expr) expect ((expr) != 0, 1)
477#define inline_size static inline 488#define inline_size static inline
478 489
479#if EV_MINIMAL 490#if EV_FEATURE_CODE
491# define inline_speed static inline
492#else
480# define inline_speed static noinline 493# define inline_speed static noinline
481#else
482# define inline_speed static inline
483#endif 494#endif
484 495
485#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 496#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
486 497
487#if EV_MINPRI == EV_MAXPRI 498#if EV_MINPRI == EV_MAXPRI
500#define ev_active(w) ((W)(w))->active 511#define ev_active(w) ((W)(w))->active
501#define ev_at(w) ((WT)(w))->at 512#define ev_at(w) ((WT)(w))->at
502 513
503#if EV_USE_REALTIME 514#if EV_USE_REALTIME
504/* sig_atomic_t is used to avoid per-thread variables or locking but still */ 515/* sig_atomic_t is used to avoid per-thread variables or locking but still */
505/* giving it a reasonably high chance of working on typical architetcures */ 516/* giving it a reasonably high chance of working on typical architectures */
506static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */ 517static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */
507#endif 518#endif
508 519
509#if EV_USE_MONOTONIC 520#if EV_USE_MONOTONIC
510static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 521static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
523#ifdef _WIN32 534#ifdef _WIN32
524# include "ev_win32.c" 535# include "ev_win32.c"
525#endif 536#endif
526 537
527/*****************************************************************************/ 538/*****************************************************************************/
539
540#if EV_AVOID_STDIO
541static void noinline
542ev_printerr (const char *msg)
543{
544 write (STDERR_FILENO, msg, strlen (msg));
545}
546#endif
528 547
529static void (*syserr_cb)(const char *msg); 548static void (*syserr_cb)(const char *msg);
530 549
531void 550void
532ev_set_syserr_cb (void (*cb)(const char *msg)) 551ev_set_syserr_cb (void (*cb)(const char *msg))
543 if (syserr_cb) 562 if (syserr_cb)
544 syserr_cb (msg); 563 syserr_cb (msg);
545 else 564 else
546 { 565 {
547#if EV_AVOID_STDIO 566#if EV_AVOID_STDIO
548 write (STDERR_FILENO, msg, strlen (msg));
549 write (STDERR_FILENO, ": ", 2);
550 msg = strerror (errno); 567 const char *err = strerror (errno);
551 write (STDERR_FILENO, msg, strlen (msg)); 568
552 write (STDERR_FILENO, "\n", 1); 569 ev_printerr (msg);
570 ev_printerr (": ");
571 ev_printerr (err);
572 ev_printerr ("\n");
553#else 573#else
554 perror (msg); 574 perror (msg);
555#endif 575#endif
556 abort (); 576 abort ();
557 } 577 }
558} 578}
559 579
560static void * 580static void *
561ev_realloc_emul (void *ptr, long size) 581ev_realloc_emul (void *ptr, long size)
562{ 582{
583#if __GLIBC__
584 return realloc (ptr, size);
585#else
563 /* some systems, notably openbsd and darwin, fail to properly 586 /* some systems, notably openbsd and darwin, fail to properly
564 * implement realloc (x, 0) (as required by both ansi c-98 and 587 * implement realloc (x, 0) (as required by both ansi c-89 and
565 * the single unix specification, so work around them here. 588 * the single unix specification, so work around them here.
566 */ 589 */
567 590
568 if (size) 591 if (size)
569 return realloc (ptr, size); 592 return realloc (ptr, size);
570 593
571 free (ptr); 594 free (ptr);
572 return 0; 595 return 0;
596#endif
573} 597}
574 598
575static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; 599static void *(*alloc)(void *ptr, long size) = ev_realloc_emul;
576 600
577void 601void
586 ptr = alloc (ptr, size); 610 ptr = alloc (ptr, size);
587 611
588 if (!ptr && size) 612 if (!ptr && size)
589 { 613 {
590#if EV_AVOID_STDIO 614#if EV_AVOID_STDIO
591 write (STDERR_FILENO, "libev: memory allocation failed, aborting.", 615 ev_printerr ("libev: memory allocation failed, aborting.\n");
592 sizeof ("libev: memory allocation failed, aborting.") - 1);
593#else 616#else
594 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); 617 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
595#endif 618#endif
596 abort (); 619 abort ();
597 } 620 }
682 705
683 static int ev_default_loop_ptr; 706 static int ev_default_loop_ptr;
684 707
685#endif 708#endif
686 709
687#if EV_MINIMAL < 2 710#if EV_FEATURE_API
688# define EV_RELEASE_CB if (expect_false (release_cb)) release_cb (EV_A) 711# define EV_RELEASE_CB if (expect_false (release_cb)) release_cb (EV_A)
689# define EV_ACQUIRE_CB if (expect_false (acquire_cb)) acquire_cb (EV_A) 712# define EV_ACQUIRE_CB if (expect_false (acquire_cb)) acquire_cb (EV_A)
690# define EV_INVOKE_PENDING invoke_cb (EV_A) 713# define EV_INVOKE_PENDING invoke_cb (EV_A)
691#else 714#else
692# define EV_RELEASE_CB (void)0 715# define EV_RELEASE_CB (void)0
771/*****************************************************************************/ 794/*****************************************************************************/
772 795
773#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ 796#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
774 797
775/* find a suitable new size for the given array, */ 798/* find a suitable new size for the given array, */
776/* hopefully by rounding to a ncie-to-malloc size */ 799/* hopefully by rounding to a nice-to-malloc size */
777inline_size int 800inline_size int
778array_nextsize (int elem, int cur, int cnt) 801array_nextsize (int elem, int cur, int cnt)
779{ 802{
780 int ncur = cur + 1; 803 int ncur = cur + 1;
781 804
877} 900}
878 901
879/*****************************************************************************/ 902/*****************************************************************************/
880 903
881inline_speed void 904inline_speed void
882fd_event_nc (EV_P_ int fd, int revents) 905fd_event_nocheck (EV_P_ int fd, int revents)
883{ 906{
884 ANFD *anfd = anfds + fd; 907 ANFD *anfd = anfds + fd;
885 ev_io *w; 908 ev_io *w;
886 909
887 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) 910 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
899fd_event (EV_P_ int fd, int revents) 922fd_event (EV_P_ int fd, int revents)
900{ 923{
901 ANFD *anfd = anfds + fd; 924 ANFD *anfd = anfds + fd;
902 925
903 if (expect_true (!anfd->reify)) 926 if (expect_true (!anfd->reify))
904 fd_event_nc (EV_A_ fd, revents); 927 fd_event_nocheck (EV_A_ fd, revents);
905} 928}
906 929
907void 930void
908ev_feed_fd_event (EV_P_ int fd, int revents) 931ev_feed_fd_event (EV_P_ int fd, int revents)
909{ 932{
910 if (fd >= 0 && fd < anfdmax) 933 if (fd >= 0 && fd < anfdmax)
911 fd_event_nc (EV_A_ fd, revents); 934 fd_event_nocheck (EV_A_ fd, revents);
912} 935}
913 936
914/* make sure the external fd watch events are in-sync */ 937/* make sure the external fd watch events are in-sync */
915/* with the kernel/libev internal state */ 938/* with the kernel/libev internal state */
916inline_size void 939inline_size void
979 ev_io_stop (EV_A_ w); 1002 ev_io_stop (EV_A_ w);
980 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 1003 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
981 } 1004 }
982} 1005}
983 1006
984/* check whether the given fd is atcually valid, for error recovery */ 1007/* check whether the given fd is actually valid, for error recovery */
985inline_size int 1008inline_size int
986fd_valid (int fd) 1009fd_valid (int fd)
987{ 1010{
988#ifdef _WIN32 1011#ifdef _WIN32
989 return EV_FD_TO_WIN32_HANDLE (fd) != -1; 1012 return EV_FD_TO_WIN32_HANDLE (fd) != -1;
1031 anfds [fd].emask = 0; 1054 anfds [fd].emask = 0;
1032 fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY); 1055 fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY);
1033 } 1056 }
1034} 1057}
1035 1058
1059/* used to prepare libev internal fd's */
1060/* this is not fork-safe */
1061inline_speed void
1062fd_intern (int fd)
1063{
1064#ifdef _WIN32
1065 unsigned long arg = 1;
1066 ioctlsocket (EV_FD_TO_WIN32_HANDLE (fd), FIONBIO, &arg);
1067#else
1068 fcntl (fd, F_SETFD, FD_CLOEXEC);
1069 fcntl (fd, F_SETFL, O_NONBLOCK);
1070#endif
1071}
1072
1036/*****************************************************************************/ 1073/*****************************************************************************/
1037 1074
1038/* 1075/*
1039 * the heap functions want a real array index. array index 0 uis guaranteed to not 1076 * the heap functions want a real array index. array index 0 is guaranteed to not
1040 * be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives 1077 * be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives
1041 * the branching factor of the d-tree. 1078 * the branching factor of the d-tree.
1042 */ 1079 */
1043 1080
1044/* 1081/*
1192 1229
1193static ANSIG signals [EV_NSIG - 1]; 1230static ANSIG signals [EV_NSIG - 1];
1194 1231
1195/*****************************************************************************/ 1232/*****************************************************************************/
1196 1233
1197/* used to prepare libev internal fd's */ 1234#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1198/* this is not fork-safe */
1199inline_speed void
1200fd_intern (int fd)
1201{
1202#ifdef _WIN32
1203 unsigned long arg = 1;
1204 ioctlsocket (EV_FD_TO_WIN32_HANDLE (fd), FIONBIO, &arg);
1205#else
1206 fcntl (fd, F_SETFD, FD_CLOEXEC);
1207 fcntl (fd, F_SETFL, O_NONBLOCK);
1208#endif
1209}
1210 1235
1211static void noinline 1236static void noinline
1212evpipe_init (EV_P) 1237evpipe_init (EV_P)
1213{ 1238{
1214 if (!ev_is_active (&pipe_w)) 1239 if (!ev_is_active (&pipe_w))
1215 { 1240 {
1216#if EV_USE_EVENTFD 1241# if EV_USE_EVENTFD
1217 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); 1242 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
1218 if (evfd < 0 && errno == EINVAL) 1243 if (evfd < 0 && errno == EINVAL)
1219 evfd = eventfd (0, 0); 1244 evfd = eventfd (0, 0);
1220 1245
1221 if (evfd >= 0) 1246 if (evfd >= 0)
1223 evpipe [0] = -1; 1248 evpipe [0] = -1;
1224 fd_intern (evfd); /* doing it twice doesn't hurt */ 1249 fd_intern (evfd); /* doing it twice doesn't hurt */
1225 ev_io_set (&pipe_w, evfd, EV_READ); 1250 ev_io_set (&pipe_w, evfd, EV_READ);
1226 } 1251 }
1227 else 1252 else
1228#endif 1253# endif
1229 { 1254 {
1230 while (pipe (evpipe)) 1255 while (pipe (evpipe))
1231 ev_syserr ("(libev) error creating signal/async pipe"); 1256 ev_syserr ("(libev) error creating signal/async pipe");
1232 1257
1233 fd_intern (evpipe [0]); 1258 fd_intern (evpipe [0]);
1244evpipe_write (EV_P_ EV_ATOMIC_T *flag) 1269evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1245{ 1270{
1246 if (!*flag) 1271 if (!*flag)
1247 { 1272 {
1248 int old_errno = errno; /* save errno because write might clobber it */ 1273 int old_errno = errno; /* save errno because write might clobber it */
1274 char dummy;
1249 1275
1250 *flag = 1; 1276 *flag = 1;
1251 1277
1252#if EV_USE_EVENTFD 1278#if EV_USE_EVENTFD
1253 if (evfd >= 0) 1279 if (evfd >= 0)
1255 uint64_t counter = 1; 1281 uint64_t counter = 1;
1256 write (evfd, &counter, sizeof (uint64_t)); 1282 write (evfd, &counter, sizeof (uint64_t));
1257 } 1283 }
1258 else 1284 else
1259#endif 1285#endif
1260 write (evpipe [1], &old_errno, 1); 1286 write (evpipe [1], &dummy, 1);
1261 1287
1262 errno = old_errno; 1288 errno = old_errno;
1263 } 1289 }
1264} 1290}
1265 1291
1366 break; 1392 break;
1367 } 1393 }
1368} 1394}
1369#endif 1395#endif
1370 1396
1397#endif
1398
1371/*****************************************************************************/ 1399/*****************************************************************************/
1372 1400
1401#if EV_CHILD_ENABLE
1373static WL childs [EV_PID_HASHSIZE]; 1402static WL childs [EV_PID_HASHSIZE];
1374
1375#ifndef _WIN32
1376 1403
1377static ev_signal childev; 1404static ev_signal childev;
1378 1405
1379#ifndef WIFCONTINUED 1406#ifndef WIFCONTINUED
1380# define WIFCONTINUED(status) 0 1407# define WIFCONTINUED(status) 0
1385child_reap (EV_P_ int chain, int pid, int status) 1412child_reap (EV_P_ int chain, int pid, int status)
1386{ 1413{
1387 ev_child *w; 1414 ev_child *w;
1388 int traced = WIFSTOPPED (status) || WIFCONTINUED (status); 1415 int traced = WIFSTOPPED (status) || WIFCONTINUED (status);
1389 1416
1390 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 1417 for (w = (ev_child *)childs [chain & ((EV_PID_HASHSIZE) - 1)]; w; w = (ev_child *)((WL)w)->next)
1391 { 1418 {
1392 if ((w->pid == pid || !w->pid) 1419 if ((w->pid == pid || !w->pid)
1393 && (!traced || (w->flags & 1))) 1420 && (!traced || (w->flags & 1)))
1394 { 1421 {
1395 ev_set_priority (w, EV_MAXPRI); /* need to do it *now*, this *must* be the same prio as the signal watcher itself */ 1422 ev_set_priority (w, EV_MAXPRI); /* need to do it *now*, this *must* be the same prio as the signal watcher itself */
1420 /* make sure we are called again until all children have been reaped */ 1447 /* make sure we are called again until all children have been reaped */
1421 /* we need to do it this way so that the callback gets called before we continue */ 1448 /* we need to do it this way so that the callback gets called before we continue */
1422 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); 1449 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
1423 1450
1424 child_reap (EV_A_ pid, pid, status); 1451 child_reap (EV_A_ pid, pid, status);
1425 if (EV_PID_HASHSIZE > 1) 1452 if ((EV_PID_HASHSIZE) > 1)
1426 child_reap (EV_A_ 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */ 1453 child_reap (EV_A_ 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
1427} 1454}
1428 1455
1429#endif 1456#endif
1430 1457
1497#ifdef __APPLE__ 1524#ifdef __APPLE__
1498 /* only select works correctly on that "unix-certified" platform */ 1525 /* only select works correctly on that "unix-certified" platform */
1499 flags &= ~EVBACKEND_KQUEUE; /* horribly broken, even for sockets */ 1526 flags &= ~EVBACKEND_KQUEUE; /* horribly broken, even for sockets */
1500 flags &= ~EVBACKEND_POLL; /* poll is based on kqueue from 10.5 onwards */ 1527 flags &= ~EVBACKEND_POLL; /* poll is based on kqueue from 10.5 onwards */
1501#endif 1528#endif
1529#ifdef __FreeBSD__
1530 flags &= ~EVBACKEND_POLL; /* poll return value is unusable (http://forums.freebsd.org/archive/index.php/t-10270.html) */
1531#endif
1502 1532
1503 return flags; 1533 return flags;
1504} 1534}
1505 1535
1506unsigned int 1536unsigned int
1519ev_backend (EV_P) 1549ev_backend (EV_P)
1520{ 1550{
1521 return backend; 1551 return backend;
1522} 1552}
1523 1553
1524#if EV_MINIMAL < 2 1554#if EV_FEATURE_API
1525unsigned int 1555unsigned int
1526ev_loop_count (EV_P) 1556ev_iteration (EV_P)
1527{ 1557{
1528 return loop_count; 1558 return loop_count;
1529} 1559}
1530 1560
1531unsigned int 1561unsigned int
1532ev_loop_depth (EV_P) 1562ev_depth (EV_P)
1533{ 1563{
1534 return loop_depth; 1564 return loop_depth;
1535} 1565}
1536 1566
1537void 1567void
1609 1639
1610 ev_rt_now = ev_time (); 1640 ev_rt_now = ev_time ();
1611 mn_now = get_clock (); 1641 mn_now = get_clock ();
1612 now_floor = mn_now; 1642 now_floor = mn_now;
1613 rtmn_diff = ev_rt_now - mn_now; 1643 rtmn_diff = ev_rt_now - mn_now;
1614#if EV_MINIMAL < 2 1644#if EV_FEATURE_API
1615 invoke_cb = ev_invoke_pending; 1645 invoke_cb = ev_invoke_pending;
1616#endif 1646#endif
1617 1647
1618 io_blocktime = 0.; 1648 io_blocktime = 0.;
1619 timeout_blocktime = 0.; 1649 timeout_blocktime = 0.;
1649 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); 1679 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
1650#endif 1680#endif
1651 1681
1652 ev_prepare_init (&pending_w, pendingcb); 1682 ev_prepare_init (&pending_w, pendingcb);
1653 1683
1684#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1654 ev_init (&pipe_w, pipecb); 1685 ev_init (&pipe_w, pipecb);
1655 ev_set_priority (&pipe_w, EV_MAXPRI); 1686 ev_set_priority (&pipe_w, EV_MAXPRI);
1687#endif
1656 } 1688 }
1657} 1689}
1658 1690
1659/* free up a loop structure */ 1691/* free up a loop structure */
1660static void noinline 1692static void noinline
1778 { 1810 {
1779 EV_WIN32_CLOSE_FD (evpipe [0]); 1811 EV_WIN32_CLOSE_FD (evpipe [0]);
1780 EV_WIN32_CLOSE_FD (evpipe [1]); 1812 EV_WIN32_CLOSE_FD (evpipe [1]);
1781 } 1813 }
1782 1814
1815#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1783 evpipe_init (EV_A); 1816 evpipe_init (EV_A);
1784 /* now iterate over everything, in case we missed something */ 1817 /* now iterate over everything, in case we missed something */
1785 pipecb (EV_A_ &pipe_w, EV_READ); 1818 pipecb (EV_A_ &pipe_w, EV_READ);
1819#endif
1786 } 1820 }
1787 1821
1788 postfork = 0; 1822 postfork = 0;
1789} 1823}
1790 1824
1852 verify_watcher (EV_A_ ws [cnt]); 1886 verify_watcher (EV_A_ ws [cnt]);
1853 } 1887 }
1854} 1888}
1855#endif 1889#endif
1856 1890
1857#if EV_MINIMAL < 2 1891#if EV_FEATURE_API
1858void 1892void
1859ev_loop_verify (EV_P) 1893ev_verify (EV_P)
1860{ 1894{
1861#if EV_VERIFY 1895#if EV_VERIFY
1862 int i; 1896 int i;
1863 WL w; 1897 WL w;
1864 1898
1903#if EV_ASYNC_ENABLE 1937#if EV_ASYNC_ENABLE
1904 assert (asyncmax >= asynccnt); 1938 assert (asyncmax >= asynccnt);
1905 array_verify (EV_A_ (W *)asyncs, asynccnt); 1939 array_verify (EV_A_ (W *)asyncs, asynccnt);
1906#endif 1940#endif
1907 1941
1942#if EV_PREPARE_ENABLE
1908 assert (preparemax >= preparecnt); 1943 assert (preparemax >= preparecnt);
1909 array_verify (EV_A_ (W *)prepares, preparecnt); 1944 array_verify (EV_A_ (W *)prepares, preparecnt);
1945#endif
1910 1946
1947#if EV_CHECK_ENABLE
1911 assert (checkmax >= checkcnt); 1948 assert (checkmax >= checkcnt);
1912 array_verify (EV_A_ (W *)checks, checkcnt); 1949 array_verify (EV_A_ (W *)checks, checkcnt);
1950#endif
1913 1951
1914# if 0 1952# if 0
1953#if EV_CHILD_ENABLE
1915 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 1954 for (w = (ev_child *)childs [chain & ((EV_PID_HASHSIZE) - 1)]; w; w = (ev_child *)((WL)w)->next)
1916 for (signum = EV_NSIG; signum--; ) if (signals [signum].pending) 1955 for (signum = EV_NSIG; signum--; ) if (signals [signum].pending)
1956#endif
1917# endif 1957# endif
1918#endif 1958#endif
1919} 1959}
1920#endif 1960#endif
1921 1961
1937 1977
1938 loop_init (EV_A_ flags); 1978 loop_init (EV_A_ flags);
1939 1979
1940 if (ev_backend (EV_A)) 1980 if (ev_backend (EV_A))
1941 { 1981 {
1942#ifndef _WIN32 1982#if EV_CHILD_ENABLE
1943 ev_signal_init (&childev, childcb, SIGCHLD); 1983 ev_signal_init (&childev, childcb, SIGCHLD);
1944 ev_set_priority (&childev, EV_MAXPRI); 1984 ev_set_priority (&childev, EV_MAXPRI);
1945 ev_signal_start (EV_A_ &childev); 1985 ev_signal_start (EV_A_ &childev);
1946 ev_unref (EV_A); /* child watcher should not keep loop alive */ 1986 ev_unref (EV_A); /* child watcher should not keep loop alive */
1947#endif 1987#endif
1960 EV_P = ev_default_loop_ptr; 2000 EV_P = ev_default_loop_ptr;
1961#endif 2001#endif
1962 2002
1963 ev_default_loop_ptr = 0; 2003 ev_default_loop_ptr = 0;
1964 2004
1965#ifndef _WIN32 2005#if EV_CHILD_ENABLE
1966 ev_ref (EV_A); /* child watcher */ 2006 ev_ref (EV_A); /* child watcher */
1967 ev_signal_stop (EV_A_ &childev); 2007 ev_signal_stop (EV_A_ &childev);
1968#endif 2008#endif
1969 2009
1970 loop_destroy (EV_A); 2010 loop_destroy (EV_A);
2076 EV_FREQUENT_CHECK; 2116 EV_FREQUENT_CHECK;
2077 feed_reverse (EV_A_ (W)w); 2117 feed_reverse (EV_A_ (W)w);
2078 } 2118 }
2079 while (timercnt && ANHE_at (timers [HEAP0]) < mn_now); 2119 while (timercnt && ANHE_at (timers [HEAP0]) < mn_now);
2080 2120
2081 feed_reverse_done (EV_A_ EV_TIMEOUT); 2121 feed_reverse_done (EV_A_ EV_TIMER);
2082 } 2122 }
2083} 2123}
2084 2124
2085#if EV_PERIODIC_ENABLE 2125#if EV_PERIODIC_ENABLE
2086/* make periodics pending */ 2126/* make periodics pending */
2139 feed_reverse_done (EV_A_ EV_PERIODIC); 2179 feed_reverse_done (EV_A_ EV_PERIODIC);
2140 } 2180 }
2141} 2181}
2142 2182
2143/* simply recalculate all periodics */ 2183/* simply recalculate all periodics */
2144/* TODO: maybe ensure that at leats one event happens when jumping forward? */ 2184/* TODO: maybe ensure that at least one event happens when jumping forward? */
2145static void noinline 2185static void noinline
2146periodics_reschedule (EV_P) 2186periodics_reschedule (EV_P)
2147{ 2187{
2148 int i; 2188 int i;
2149 2189
2247} 2287}
2248 2288
2249void 2289void
2250ev_loop (EV_P_ int flags) 2290ev_loop (EV_P_ int flags)
2251{ 2291{
2252#if EV_MINIMAL < 2 2292#if EV_FEATURE_API
2253 ++loop_depth; 2293 ++loop_depth;
2254#endif 2294#endif
2255 2295
2256 assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE)); 2296 assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE));
2257 2297
2260 EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */ 2300 EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */
2261 2301
2262 do 2302 do
2263 { 2303 {
2264#if EV_VERIFY >= 2 2304#if EV_VERIFY >= 2
2265 ev_loop_verify (EV_A); 2305 ev_verify (EV_A);
2266#endif 2306#endif
2267 2307
2268#ifndef _WIN32 2308#ifndef _WIN32
2269 if (expect_false (curpid)) /* penalise the forking check even more */ 2309 if (expect_false (curpid)) /* penalise the forking check even more */
2270 if (expect_false (getpid () != curpid)) 2310 if (expect_false (getpid () != curpid))
2282 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 2322 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
2283 EV_INVOKE_PENDING; 2323 EV_INVOKE_PENDING;
2284 } 2324 }
2285#endif 2325#endif
2286 2326
2327#if EV_PREPARE_ENABLE
2287 /* queue prepare watchers (and execute them) */ 2328 /* queue prepare watchers (and execute them) */
2288 if (expect_false (preparecnt)) 2329 if (expect_false (preparecnt))
2289 { 2330 {
2290 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 2331 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
2291 EV_INVOKE_PENDING; 2332 EV_INVOKE_PENDING;
2292 } 2333 }
2334#endif
2293 2335
2294 if (expect_false (loop_done)) 2336 if (expect_false (loop_done))
2295 break; 2337 break;
2296 2338
2297 /* we might have forked, so reify kernel state if necessary */ 2339 /* we might have forked, so reify kernel state if necessary */
2348 waittime -= sleeptime; 2390 waittime -= sleeptime;
2349 } 2391 }
2350 } 2392 }
2351 } 2393 }
2352 2394
2353#if EV_MINIMAL < 2 2395#if EV_FEATURE_API
2354 ++loop_count; 2396 ++loop_count;
2355#endif 2397#endif
2356 assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */ 2398 assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */
2357 backend_poll (EV_A_ waittime); 2399 backend_poll (EV_A_ waittime);
2358 assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */ 2400 assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */
2370#if EV_IDLE_ENABLE 2412#if EV_IDLE_ENABLE
2371 /* queue idle watchers unless other events are pending */ 2413 /* queue idle watchers unless other events are pending */
2372 idle_reify (EV_A); 2414 idle_reify (EV_A);
2373#endif 2415#endif
2374 2416
2417#if EV_CHECK_ENABLE
2375 /* queue check watchers, to be executed first */ 2418 /* queue check watchers, to be executed first */
2376 if (expect_false (checkcnt)) 2419 if (expect_false (checkcnt))
2377 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 2420 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
2421#endif
2378 2422
2379 EV_INVOKE_PENDING; 2423 EV_INVOKE_PENDING;
2380 } 2424 }
2381 while (expect_true ( 2425 while (expect_true (
2382 activecnt 2426 activecnt
2385 )); 2429 ));
2386 2430
2387 if (loop_done == EVUNLOOP_ONE) 2431 if (loop_done == EVUNLOOP_ONE)
2388 loop_done = EVUNLOOP_CANCEL; 2432 loop_done = EVUNLOOP_CANCEL;
2389 2433
2390#if EV_MINIMAL < 2 2434#if EV_FEATURE_API
2391 --loop_depth; 2435 --loop_depth;
2392#endif 2436#endif
2393} 2437}
2394 2438
2395void 2439void
2712 2756
2713#ifndef SA_RESTART 2757#ifndef SA_RESTART
2714# define SA_RESTART 0 2758# define SA_RESTART 0
2715#endif 2759#endif
2716 2760
2761#if EV_SIGNAL_ENABLE
2762
2717void noinline 2763void noinline
2718ev_signal_start (EV_P_ ev_signal *w) 2764ev_signal_start (EV_P_ ev_signal *w)
2719{ 2765{
2720 if (expect_false (ev_is_active (w))) 2766 if (expect_false (ev_is_active (w)))
2721 return; 2767 return;
2827 } 2873 }
2828 2874
2829 EV_FREQUENT_CHECK; 2875 EV_FREQUENT_CHECK;
2830} 2876}
2831 2877
2878#endif
2879
2880#if EV_CHILD_ENABLE
2881
2832void 2882void
2833ev_child_start (EV_P_ ev_child *w) 2883ev_child_start (EV_P_ ev_child *w)
2834{ 2884{
2835#if EV_MULTIPLICITY 2885#if EV_MULTIPLICITY
2836 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 2886 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
2839 return; 2889 return;
2840 2890
2841 EV_FREQUENT_CHECK; 2891 EV_FREQUENT_CHECK;
2842 2892
2843 ev_start (EV_A_ (W)w, 1); 2893 ev_start (EV_A_ (W)w, 1);
2844 wlist_add (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 2894 wlist_add (&childs [w->pid & ((EV_PID_HASHSIZE) - 1)], (WL)w);
2845 2895
2846 EV_FREQUENT_CHECK; 2896 EV_FREQUENT_CHECK;
2847} 2897}
2848 2898
2849void 2899void
2853 if (expect_false (!ev_is_active (w))) 2903 if (expect_false (!ev_is_active (w)))
2854 return; 2904 return;
2855 2905
2856 EV_FREQUENT_CHECK; 2906 EV_FREQUENT_CHECK;
2857 2907
2858 wlist_del (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 2908 wlist_del (&childs [w->pid & ((EV_PID_HASHSIZE) - 1)], (WL)w);
2859 ev_stop (EV_A_ (W)w); 2909 ev_stop (EV_A_ (W)w);
2860 2910
2861 EV_FREQUENT_CHECK; 2911 EV_FREQUENT_CHECK;
2862} 2912}
2913
2914#endif
2863 2915
2864#if EV_STAT_ENABLE 2916#if EV_STAT_ENABLE
2865 2917
2866# ifdef _WIN32 2918# ifdef _WIN32
2867# undef lstat 2919# undef lstat
2934 while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); 2986 while (w->wd < 0 && (errno == ENOENT || errno == EACCES));
2935 } 2987 }
2936 } 2988 }
2937 2989
2938 if (w->wd >= 0) 2990 if (w->wd >= 0)
2939 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); 2991 wlist_add (&fs_hash [w->wd & ((EV_INOTIFY_HASHSIZE) - 1)].head, (WL)w);
2940 2992
2941 /* now re-arm timer, if required */ 2993 /* now re-arm timer, if required */
2942 if (ev_is_active (&w->timer)) ev_ref (EV_A); 2994 if (ev_is_active (&w->timer)) ev_ref (EV_A);
2943 ev_timer_again (EV_A_ &w->timer); 2995 ev_timer_again (EV_A_ &w->timer);
2944 if (ev_is_active (&w->timer)) ev_unref (EV_A); 2996 if (ev_is_active (&w->timer)) ev_unref (EV_A);
2952 3004
2953 if (wd < 0) 3005 if (wd < 0)
2954 return; 3006 return;
2955 3007
2956 w->wd = -2; 3008 w->wd = -2;
2957 slot = wd & (EV_INOTIFY_HASHSIZE - 1); 3009 slot = wd & ((EV_INOTIFY_HASHSIZE) - 1);
2958 wlist_del (&fs_hash [slot].head, (WL)w); 3010 wlist_del (&fs_hash [slot].head, (WL)w);
2959 3011
2960 /* remove this watcher, if others are watching it, they will rearm */ 3012 /* remove this watcher, if others are watching it, they will rearm */
2961 inotify_rm_watch (fs_fd, wd); 3013 inotify_rm_watch (fs_fd, wd);
2962} 3014}
2964static void noinline 3016static void noinline
2965infy_wd (EV_P_ int slot, int wd, struct inotify_event *ev) 3017infy_wd (EV_P_ int slot, int wd, struct inotify_event *ev)
2966{ 3018{
2967 if (slot < 0) 3019 if (slot < 0)
2968 /* overflow, need to check for all hash slots */ 3020 /* overflow, need to check for all hash slots */
2969 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot) 3021 for (slot = 0; slot < (EV_INOTIFY_HASHSIZE); ++slot)
2970 infy_wd (EV_A_ slot, wd, ev); 3022 infy_wd (EV_A_ slot, wd, ev);
2971 else 3023 else
2972 { 3024 {
2973 WL w_; 3025 WL w_;
2974 3026
2975 for (w_ = fs_hash [slot & (EV_INOTIFY_HASHSIZE - 1)].head; w_; ) 3027 for (w_ = fs_hash [slot & ((EV_INOTIFY_HASHSIZE) - 1)].head; w_; )
2976 { 3028 {
2977 ev_stat *w = (ev_stat *)w_; 3029 ev_stat *w = (ev_stat *)w_;
2978 w_ = w_->next; /* lets us remove this watcher and all before it */ 3030 w_ = w_->next; /* lets us remove this watcher and all before it */
2979 3031
2980 if (w->wd == wd || wd == -1) 3032 if (w->wd == wd || wd == -1)
2981 { 3033 {
2982 if (ev->mask & (IN_IGNORED | IN_UNMOUNT | IN_DELETE_SELF)) 3034 if (ev->mask & (IN_IGNORED | IN_UNMOUNT | IN_DELETE_SELF))
2983 { 3035 {
2984 wlist_del (&fs_hash [slot & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); 3036 wlist_del (&fs_hash [slot & ((EV_INOTIFY_HASHSIZE) - 1)].head, (WL)w);
2985 w->wd = -1; 3037 w->wd = -1;
2986 infy_add (EV_A_ w); /* re-add, no matter what */ 3038 infy_add (EV_A_ w); /* re-add, no matter what */
2987 } 3039 }
2988 3040
2989 stat_timer_cb (EV_A_ &w->timer, 0); 3041 stat_timer_cb (EV_A_ &w->timer, 0);
3103 ev_io_set (&fs_w, fs_fd, EV_READ); 3155 ev_io_set (&fs_w, fs_fd, EV_READ);
3104 ev_io_start (EV_A_ &fs_w); 3156 ev_io_start (EV_A_ &fs_w);
3105 ev_unref (EV_A); 3157 ev_unref (EV_A);
3106 } 3158 }
3107 3159
3108 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot) 3160 for (slot = 0; slot < (EV_INOTIFY_HASHSIZE); ++slot)
3109 { 3161 {
3110 WL w_ = fs_hash [slot].head; 3162 WL w_ = fs_hash [slot].head;
3111 fs_hash [slot].head = 0; 3163 fs_hash [slot].head = 0;
3112 3164
3113 while (w_) 3165 while (w_)
3288 3340
3289 EV_FREQUENT_CHECK; 3341 EV_FREQUENT_CHECK;
3290} 3342}
3291#endif 3343#endif
3292 3344
3345#if EV_PREPARE_ENABLE
3293void 3346void
3294ev_prepare_start (EV_P_ ev_prepare *w) 3347ev_prepare_start (EV_P_ ev_prepare *w)
3295{ 3348{
3296 if (expect_false (ev_is_active (w))) 3349 if (expect_false (ev_is_active (w)))
3297 return; 3350 return;
3323 3376
3324 ev_stop (EV_A_ (W)w); 3377 ev_stop (EV_A_ (W)w);
3325 3378
3326 EV_FREQUENT_CHECK; 3379 EV_FREQUENT_CHECK;
3327} 3380}
3381#endif
3328 3382
3383#if EV_CHECK_ENABLE
3329void 3384void
3330ev_check_start (EV_P_ ev_check *w) 3385ev_check_start (EV_P_ ev_check *w)
3331{ 3386{
3332 if (expect_false (ev_is_active (w))) 3387 if (expect_false (ev_is_active (w)))
3333 return; 3388 return;
3359 3414
3360 ev_stop (EV_A_ (W)w); 3415 ev_stop (EV_A_ (W)w);
3361 3416
3362 EV_FREQUENT_CHECK; 3417 EV_FREQUENT_CHECK;
3363} 3418}
3419#endif
3364 3420
3365#if EV_EMBED_ENABLE 3421#if EV_EMBED_ENABLE
3366void noinline 3422void noinline
3367ev_embed_sweep (EV_P_ ev_embed *w) 3423ev_embed_sweep (EV_P_ ev_embed *w)
3368{ 3424{
3600{ 3656{
3601 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); 3657 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
3602 3658
3603 if (expect_false (!once)) 3659 if (expect_false (!once))
3604 { 3660 {
3605 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 3661 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMER, arg);
3606 return; 3662 return;
3607 } 3663 }
3608 3664
3609 once->cb = cb; 3665 once->cb = cb;
3610 once->arg = arg; 3666 once->arg = arg;
3697 if (types & EV_ASYNC) 3753 if (types & EV_ASYNC)
3698 for (i = asynccnt; i--; ) 3754 for (i = asynccnt; i--; )
3699 cb (EV_A_ EV_ASYNC, asyncs [i]); 3755 cb (EV_A_ EV_ASYNC, asyncs [i]);
3700#endif 3756#endif
3701 3757
3758#if EV_PREPARE_ENABLE
3702 if (types & EV_PREPARE) 3759 if (types & EV_PREPARE)
3703 for (i = preparecnt; i--; ) 3760 for (i = preparecnt; i--; )
3704#if EV_EMBED_ENABLE 3761# if EV_EMBED_ENABLE
3705 if (ev_cb (prepares [i]) != embed_prepare_cb) 3762 if (ev_cb (prepares [i]) != embed_prepare_cb)
3706#endif 3763# endif
3707 cb (EV_A_ EV_PREPARE, prepares [i]); 3764 cb (EV_A_ EV_PREPARE, prepares [i]);
3765#endif
3708 3766
3767#if EV_CHECK_ENABLE
3709 if (types & EV_CHECK) 3768 if (types & EV_CHECK)
3710 for (i = checkcnt; i--; ) 3769 for (i = checkcnt; i--; )
3711 cb (EV_A_ EV_CHECK, checks [i]); 3770 cb (EV_A_ EV_CHECK, checks [i]);
3771#endif
3712 3772
3773#if EV_SIGNAL_ENABLE
3713 if (types & EV_SIGNAL) 3774 if (types & EV_SIGNAL)
3714 for (i = 0; i < EV_NSIG - 1; ++i) 3775 for (i = 0; i < EV_NSIG - 1; ++i)
3715 for (wl = signals [i].head; wl; ) 3776 for (wl = signals [i].head; wl; )
3716 { 3777 {
3717 wn = wl->next; 3778 wn = wl->next;
3718 cb (EV_A_ EV_SIGNAL, wl); 3779 cb (EV_A_ EV_SIGNAL, wl);
3719 wl = wn; 3780 wl = wn;
3720 } 3781 }
3782#endif
3721 3783
3784#if EV_CHILD_ENABLE
3722 if (types & EV_CHILD) 3785 if (types & EV_CHILD)
3723 for (i = EV_PID_HASHSIZE; i--; ) 3786 for (i = (EV_PID_HASHSIZE); i--; )
3724 for (wl = childs [i]; wl; ) 3787 for (wl = childs [i]; wl; )
3725 { 3788 {
3726 wn = wl->next; 3789 wn = wl->next;
3727 cb (EV_A_ EV_CHILD, wl); 3790 cb (EV_A_ EV_CHILD, wl);
3728 wl = wn; 3791 wl = wn;
3729 } 3792 }
3793#endif
3730/* EV_STAT 0x00001000 /* stat data changed */ 3794/* EV_STAT 0x00001000 /* stat data changed */
3731/* EV_EMBED 0x00010000 /* embedded event loop needs sweep */ 3795/* EV_EMBED 0x00010000 /* embedded event loop needs sweep */
3732} 3796}
3733#endif 3797#endif
3734 3798

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines