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.347 by root, Fri Oct 15 22:44:41 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
437# ifdef __cplusplus 448# ifdef __cplusplus
438} 449}
439# endif 450# endif
440#endif 451#endif
441 452
442
443/**/ 453/**/
444 454
445#if EV_VERIFY >= 3 455#if EV_VERIFY >= 3
446# define EV_FREQUENT_CHECK ev_loop_verify (EV_A) 456# define EV_FREQUENT_CHECK ev_verify (EV_A)
447#else 457#else
448# define EV_FREQUENT_CHECK do { } while (0) 458# define EV_FREQUENT_CHECK do { } while (0)
449#endif 459#endif
450 460
451/* 461/*
459#define TIME_EPSILON 0.0001220703125 /* 1/8192 */ 469#define TIME_EPSILON 0.0001220703125 /* 1/8192 */
460 470
461#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 471#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
462#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ 472#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
463 473
474#define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0)
475#define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; tv.tv_nsec = (long)((t - tv.tv_sec) * 1e9); } while (0)
476
464#if __GNUC__ >= 4 477#if __GNUC__ >= 4
465# define expect(expr,value) __builtin_expect ((expr),(value)) 478# define expect(expr,value) __builtin_expect ((expr),(value))
466# define noinline __attribute__ ((noinline)) 479# define noinline __attribute__ ((noinline))
467#else 480#else
468# define expect(expr,value) (expr) 481# define expect(expr,value) (expr)
474 487
475#define expect_false(expr) expect ((expr) != 0, 0) 488#define expect_false(expr) expect ((expr) != 0, 0)
476#define expect_true(expr) expect ((expr) != 0, 1) 489#define expect_true(expr) expect ((expr) != 0, 1)
477#define inline_size static inline 490#define inline_size static inline
478 491
479#if EV_MINIMAL 492#if EV_FEATURE_CODE
493# define inline_speed static inline
494#else
480# define inline_speed static noinline 495# define inline_speed static noinline
481#else
482# define inline_speed static inline
483#endif 496#endif
484 497
485#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 498#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
486 499
487#if EV_MINPRI == EV_MAXPRI 500#if EV_MINPRI == EV_MAXPRI
500#define ev_active(w) ((W)(w))->active 513#define ev_active(w) ((W)(w))->active
501#define ev_at(w) ((WT)(w))->at 514#define ev_at(w) ((WT)(w))->at
502 515
503#if EV_USE_REALTIME 516#if EV_USE_REALTIME
504/* sig_atomic_t is used to avoid per-thread variables or locking but still */ 517/* 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 */ 518/* giving it a reasonably high chance of working on typical architectures */
506static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */ 519static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */
507#endif 520#endif
508 521
509#if EV_USE_MONOTONIC 522#if EV_USE_MONOTONIC
510static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 523static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
523#ifdef _WIN32 536#ifdef _WIN32
524# include "ev_win32.c" 537# include "ev_win32.c"
525#endif 538#endif
526 539
527/*****************************************************************************/ 540/*****************************************************************************/
541
542#if EV_AVOID_STDIO
543static void noinline
544ev_printerr (const char *msg)
545{
546 write (STDERR_FILENO, msg, strlen (msg));
547}
548#endif
528 549
529static void (*syserr_cb)(const char *msg); 550static void (*syserr_cb)(const char *msg);
530 551
531void 552void
532ev_set_syserr_cb (void (*cb)(const char *msg)) 553ev_set_syserr_cb (void (*cb)(const char *msg))
543 if (syserr_cb) 564 if (syserr_cb)
544 syserr_cb (msg); 565 syserr_cb (msg);
545 else 566 else
546 { 567 {
547#if EV_AVOID_STDIO 568#if EV_AVOID_STDIO
548 write (STDERR_FILENO, msg, strlen (msg));
549 write (STDERR_FILENO, ": ", 2);
550 msg = strerror (errno); 569 const char *err = strerror (errno);
551 write (STDERR_FILENO, msg, strlen (msg)); 570
552 write (STDERR_FILENO, "\n", 1); 571 ev_printerr (msg);
572 ev_printerr (": ");
573 ev_printerr (err);
574 ev_printerr ("\n");
553#else 575#else
554 perror (msg); 576 perror (msg);
555#endif 577#endif
556 abort (); 578 abort ();
557 } 579 }
558} 580}
559 581
560static void * 582static void *
561ev_realloc_emul (void *ptr, long size) 583ev_realloc_emul (void *ptr, long size)
562{ 584{
585#if __GLIBC__
586 return realloc (ptr, size);
587#else
563 /* some systems, notably openbsd and darwin, fail to properly 588 /* some systems, notably openbsd and darwin, fail to properly
564 * implement realloc (x, 0) (as required by both ansi c-98 and 589 * implement realloc (x, 0) (as required by both ansi c-89 and
565 * the single unix specification, so work around them here. 590 * the single unix specification, so work around them here.
566 */ 591 */
567 592
568 if (size) 593 if (size)
569 return realloc (ptr, size); 594 return realloc (ptr, size);
570 595
571 free (ptr); 596 free (ptr);
572 return 0; 597 return 0;
598#endif
573} 599}
574 600
575static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; 601static void *(*alloc)(void *ptr, long size) = ev_realloc_emul;
576 602
577void 603void
586 ptr = alloc (ptr, size); 612 ptr = alloc (ptr, size);
587 613
588 if (!ptr && size) 614 if (!ptr && size)
589 { 615 {
590#if EV_AVOID_STDIO 616#if EV_AVOID_STDIO
591 write (STDERR_FILENO, "libev: memory allocation failed, aborting.", 617 ev_printerr ("libev: memory allocation failed, aborting.\n");
592 sizeof ("libev: memory allocation failed, aborting.") - 1);
593#else 618#else
594 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); 619 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
595#endif 620#endif
596 abort (); 621 abort ();
597 } 622 }
682 707
683 static int ev_default_loop_ptr; 708 static int ev_default_loop_ptr;
684 709
685#endif 710#endif
686 711
687#if EV_MINIMAL < 2 712#if EV_FEATURE_API
688# define EV_RELEASE_CB if (expect_false (release_cb)) release_cb (EV_A) 713# 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) 714# define EV_ACQUIRE_CB if (expect_false (acquire_cb)) acquire_cb (EV_A)
690# define EV_INVOKE_PENDING invoke_cb (EV_A) 715# define EV_INVOKE_PENDING invoke_cb (EV_A)
691#else 716#else
692# define EV_RELEASE_CB (void)0 717# define EV_RELEASE_CB (void)0
746 if (delay > 0.) 771 if (delay > 0.)
747 { 772 {
748#if EV_USE_NANOSLEEP 773#if EV_USE_NANOSLEEP
749 struct timespec ts; 774 struct timespec ts;
750 775
751 ts.tv_sec = (time_t)delay; 776 EV_SET_TS (ts, delay);
752 ts.tv_nsec = (long)((delay - (ev_tstamp)(ts.tv_sec)) * 1e9);
753
754 nanosleep (&ts, 0); 777 nanosleep (&ts, 0);
755#elif defined(_WIN32) 778#elif defined(_WIN32)
756 Sleep ((unsigned long)(delay * 1e3)); 779 Sleep ((unsigned long)(delay * 1e3));
757#else 780#else
758 struct timeval tv; 781 struct timeval tv;
759 782
760 tv.tv_sec = (time_t)delay;
761 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
762
763 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ 783 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
764 /* something not guaranteed by newer posix versions, but guaranteed */ 784 /* something not guaranteed by newer posix versions, but guaranteed */
765 /* by older ones */ 785 /* by older ones */
786 EV_SET_TV (tv, delay);
766 select (0, 0, 0, 0, &tv); 787 select (0, 0, 0, 0, &tv);
767#endif 788#endif
768 } 789 }
769} 790}
770 791
771/*****************************************************************************/ 792/*****************************************************************************/
772 793
773#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ 794#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
774 795
775/* find a suitable new size for the given array, */ 796/* find a suitable new size for the given array, */
776/* hopefully by rounding to a ncie-to-malloc size */ 797/* hopefully by rounding to a nice-to-malloc size */
777inline_size int 798inline_size int
778array_nextsize (int elem, int cur, int cnt) 799array_nextsize (int elem, int cur, int cnt)
779{ 800{
780 int ncur = cur + 1; 801 int ncur = cur + 1;
781 802
877} 898}
878 899
879/*****************************************************************************/ 900/*****************************************************************************/
880 901
881inline_speed void 902inline_speed void
882fd_event_nc (EV_P_ int fd, int revents) 903fd_event_nocheck (EV_P_ int fd, int revents)
883{ 904{
884 ANFD *anfd = anfds + fd; 905 ANFD *anfd = anfds + fd;
885 ev_io *w; 906 ev_io *w;
886 907
887 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) 908 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
899fd_event (EV_P_ int fd, int revents) 920fd_event (EV_P_ int fd, int revents)
900{ 921{
901 ANFD *anfd = anfds + fd; 922 ANFD *anfd = anfds + fd;
902 923
903 if (expect_true (!anfd->reify)) 924 if (expect_true (!anfd->reify))
904 fd_event_nc (EV_A_ fd, revents); 925 fd_event_nocheck (EV_A_ fd, revents);
905} 926}
906 927
907void 928void
908ev_feed_fd_event (EV_P_ int fd, int revents) 929ev_feed_fd_event (EV_P_ int fd, int revents)
909{ 930{
910 if (fd >= 0 && fd < anfdmax) 931 if (fd >= 0 && fd < anfdmax)
911 fd_event_nc (EV_A_ fd, revents); 932 fd_event_nocheck (EV_A_ fd, revents);
912} 933}
913 934
914/* make sure the external fd watch events are in-sync */ 935/* make sure the external fd watch events are in-sync */
915/* with the kernel/libev internal state */ 936/* with the kernel/libev internal state */
916inline_size void 937inline_size void
979 ev_io_stop (EV_A_ w); 1000 ev_io_stop (EV_A_ w);
980 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 1001 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
981 } 1002 }
982} 1003}
983 1004
984/* check whether the given fd is atcually valid, for error recovery */ 1005/* check whether the given fd is actually valid, for error recovery */
985inline_size int 1006inline_size int
986fd_valid (int fd) 1007fd_valid (int fd)
987{ 1008{
988#ifdef _WIN32 1009#ifdef _WIN32
989 return EV_FD_TO_WIN32_HANDLE (fd) != -1; 1010 return EV_FD_TO_WIN32_HANDLE (fd) != -1;
1031 anfds [fd].emask = 0; 1052 anfds [fd].emask = 0;
1032 fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY); 1053 fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY);
1033 } 1054 }
1034} 1055}
1035 1056
1057/* used to prepare libev internal fd's */
1058/* this is not fork-safe */
1059inline_speed void
1060fd_intern (int fd)
1061{
1062#ifdef _WIN32
1063 unsigned long arg = 1;
1064 ioctlsocket (EV_FD_TO_WIN32_HANDLE (fd), FIONBIO, &arg);
1065#else
1066 fcntl (fd, F_SETFD, FD_CLOEXEC);
1067 fcntl (fd, F_SETFL, O_NONBLOCK);
1068#endif
1069}
1070
1036/*****************************************************************************/ 1071/*****************************************************************************/
1037 1072
1038/* 1073/*
1039 * the heap functions want a real array index. array index 0 uis guaranteed to not 1074 * 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 1075 * be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives
1041 * the branching factor of the d-tree. 1076 * the branching factor of the d-tree.
1042 */ 1077 */
1043 1078
1044/* 1079/*
1192 1227
1193static ANSIG signals [EV_NSIG - 1]; 1228static ANSIG signals [EV_NSIG - 1];
1194 1229
1195/*****************************************************************************/ 1230/*****************************************************************************/
1196 1231
1197/* used to prepare libev internal fd's */ 1232#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 1233
1211static void noinline 1234static void noinline
1212evpipe_init (EV_P) 1235evpipe_init (EV_P)
1213{ 1236{
1214 if (!ev_is_active (&pipe_w)) 1237 if (!ev_is_active (&pipe_w))
1215 { 1238 {
1216#if EV_USE_EVENTFD 1239# if EV_USE_EVENTFD
1217 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); 1240 evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
1218 if (evfd < 0 && errno == EINVAL) 1241 if (evfd < 0 && errno == EINVAL)
1219 evfd = eventfd (0, 0); 1242 evfd = eventfd (0, 0);
1220 1243
1221 if (evfd >= 0) 1244 if (evfd >= 0)
1223 evpipe [0] = -1; 1246 evpipe [0] = -1;
1224 fd_intern (evfd); /* doing it twice doesn't hurt */ 1247 fd_intern (evfd); /* doing it twice doesn't hurt */
1225 ev_io_set (&pipe_w, evfd, EV_READ); 1248 ev_io_set (&pipe_w, evfd, EV_READ);
1226 } 1249 }
1227 else 1250 else
1228#endif 1251# endif
1229 { 1252 {
1230 while (pipe (evpipe)) 1253 while (pipe (evpipe))
1231 ev_syserr ("(libev) error creating signal/async pipe"); 1254 ev_syserr ("(libev) error creating signal/async pipe");
1232 1255
1233 fd_intern (evpipe [0]); 1256 fd_intern (evpipe [0]);
1244evpipe_write (EV_P_ EV_ATOMIC_T *flag) 1267evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1245{ 1268{
1246 if (!*flag) 1269 if (!*flag)
1247 { 1270 {
1248 int old_errno = errno; /* save errno because write might clobber it */ 1271 int old_errno = errno; /* save errno because write might clobber it */
1272 char dummy;
1249 1273
1250 *flag = 1; 1274 *flag = 1;
1251 1275
1252#if EV_USE_EVENTFD 1276#if EV_USE_EVENTFD
1253 if (evfd >= 0) 1277 if (evfd >= 0)
1255 uint64_t counter = 1; 1279 uint64_t counter = 1;
1256 write (evfd, &counter, sizeof (uint64_t)); 1280 write (evfd, &counter, sizeof (uint64_t));
1257 } 1281 }
1258 else 1282 else
1259#endif 1283#endif
1284 /* win32 people keep sending patches that change this write() to send() */
1285 /* and then run away. but send() is wrong, it wants a socket handle on win32 */
1286 /* so when you think this write should be a send instead, please find out */
1287 /* where your send() is from - it's definitely not the microsoft send, and */
1288 /* tell me. thank you. */
1260 write (evpipe [1], &old_errno, 1); 1289 write (evpipe [1], &dummy, 1);
1261 1290
1262 errno = old_errno; 1291 errno = old_errno;
1263 } 1292 }
1264} 1293}
1265 1294
1278 } 1307 }
1279 else 1308 else
1280#endif 1309#endif
1281 { 1310 {
1282 char dummy; 1311 char dummy;
1312 /* see discussion in evpipe_write when you think this read should be recv in win32 */
1283 read (evpipe [0], &dummy, 1); 1313 read (evpipe [0], &dummy, 1);
1284 } 1314 }
1285 1315
1286 if (sig_pending) 1316 if (sig_pending)
1287 { 1317 {
1366 break; 1396 break;
1367 } 1397 }
1368} 1398}
1369#endif 1399#endif
1370 1400
1401#endif
1402
1371/*****************************************************************************/ 1403/*****************************************************************************/
1372 1404
1405#if EV_CHILD_ENABLE
1373static WL childs [EV_PID_HASHSIZE]; 1406static WL childs [EV_PID_HASHSIZE];
1374
1375#ifndef _WIN32
1376 1407
1377static ev_signal childev; 1408static ev_signal childev;
1378 1409
1379#ifndef WIFCONTINUED 1410#ifndef WIFCONTINUED
1380# define WIFCONTINUED(status) 0 1411# define WIFCONTINUED(status) 0
1385child_reap (EV_P_ int chain, int pid, int status) 1416child_reap (EV_P_ int chain, int pid, int status)
1386{ 1417{
1387 ev_child *w; 1418 ev_child *w;
1388 int traced = WIFSTOPPED (status) || WIFCONTINUED (status); 1419 int traced = WIFSTOPPED (status) || WIFCONTINUED (status);
1389 1420
1390 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 1421 for (w = (ev_child *)childs [chain & ((EV_PID_HASHSIZE) - 1)]; w; w = (ev_child *)((WL)w)->next)
1391 { 1422 {
1392 if ((w->pid == pid || !w->pid) 1423 if ((w->pid == pid || !w->pid)
1393 && (!traced || (w->flags & 1))) 1424 && (!traced || (w->flags & 1)))
1394 { 1425 {
1395 ev_set_priority (w, EV_MAXPRI); /* need to do it *now*, this *must* be the same prio as the signal watcher itself */ 1426 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 */ 1451 /* 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 */ 1452 /* 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); 1453 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
1423 1454
1424 child_reap (EV_A_ pid, pid, status); 1455 child_reap (EV_A_ pid, pid, status);
1425 if (EV_PID_HASHSIZE > 1) 1456 if ((EV_PID_HASHSIZE) > 1)
1426 child_reap (EV_A_ 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */ 1457 child_reap (EV_A_ 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
1427} 1458}
1428 1459
1429#endif 1460#endif
1430 1461
1497#ifdef __APPLE__ 1528#ifdef __APPLE__
1498 /* only select works correctly on that "unix-certified" platform */ 1529 /* only select works correctly on that "unix-certified" platform */
1499 flags &= ~EVBACKEND_KQUEUE; /* horribly broken, even for sockets */ 1530 flags &= ~EVBACKEND_KQUEUE; /* horribly broken, even for sockets */
1500 flags &= ~EVBACKEND_POLL; /* poll is based on kqueue from 10.5 onwards */ 1531 flags &= ~EVBACKEND_POLL; /* poll is based on kqueue from 10.5 onwards */
1501#endif 1532#endif
1533#ifdef __FreeBSD__
1534 flags &= ~EVBACKEND_POLL; /* poll return value is unusable (http://forums.freebsd.org/archive/index.php/t-10270.html) */
1535#endif
1502 1536
1503 return flags; 1537 return flags;
1504} 1538}
1505 1539
1506unsigned int 1540unsigned int
1519ev_backend (EV_P) 1553ev_backend (EV_P)
1520{ 1554{
1521 return backend; 1555 return backend;
1522} 1556}
1523 1557
1524#if EV_MINIMAL < 2 1558#if EV_FEATURE_API
1525unsigned int 1559unsigned int
1526ev_loop_count (EV_P) 1560ev_iteration (EV_P)
1527{ 1561{
1528 return loop_count; 1562 return loop_count;
1529} 1563}
1530 1564
1531unsigned int 1565unsigned int
1532ev_loop_depth (EV_P) 1566ev_depth (EV_P)
1533{ 1567{
1534 return loop_depth; 1568 return loop_depth;
1535} 1569}
1536 1570
1537void 1571void
1609 1643
1610 ev_rt_now = ev_time (); 1644 ev_rt_now = ev_time ();
1611 mn_now = get_clock (); 1645 mn_now = get_clock ();
1612 now_floor = mn_now; 1646 now_floor = mn_now;
1613 rtmn_diff = ev_rt_now - mn_now; 1647 rtmn_diff = ev_rt_now - mn_now;
1614#if EV_MINIMAL < 2 1648#if EV_FEATURE_API
1615 invoke_cb = ev_invoke_pending; 1649 invoke_cb = ev_invoke_pending;
1616#endif 1650#endif
1617 1651
1618 io_blocktime = 0.; 1652 io_blocktime = 0.;
1619 timeout_blocktime = 0.; 1653 timeout_blocktime = 0.;
1649 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); 1683 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
1650#endif 1684#endif
1651 1685
1652 ev_prepare_init (&pending_w, pendingcb); 1686 ev_prepare_init (&pending_w, pendingcb);
1653 1687
1688#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1654 ev_init (&pipe_w, pipecb); 1689 ev_init (&pipe_w, pipecb);
1655 ev_set_priority (&pipe_w, EV_MAXPRI); 1690 ev_set_priority (&pipe_w, EV_MAXPRI);
1691#endif
1656 } 1692 }
1657} 1693}
1658 1694
1659/* free up a loop structure */ 1695/* free up a loop structure */
1660static void noinline 1696static void noinline
1778 { 1814 {
1779 EV_WIN32_CLOSE_FD (evpipe [0]); 1815 EV_WIN32_CLOSE_FD (evpipe [0]);
1780 EV_WIN32_CLOSE_FD (evpipe [1]); 1816 EV_WIN32_CLOSE_FD (evpipe [1]);
1781 } 1817 }
1782 1818
1819#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1783 evpipe_init (EV_A); 1820 evpipe_init (EV_A);
1784 /* now iterate over everything, in case we missed something */ 1821 /* now iterate over everything, in case we missed something */
1785 pipecb (EV_A_ &pipe_w, EV_READ); 1822 pipecb (EV_A_ &pipe_w, EV_READ);
1823#endif
1786 } 1824 }
1787 1825
1788 postfork = 0; 1826 postfork = 0;
1789} 1827}
1790 1828
1852 verify_watcher (EV_A_ ws [cnt]); 1890 verify_watcher (EV_A_ ws [cnt]);
1853 } 1891 }
1854} 1892}
1855#endif 1893#endif
1856 1894
1857#if EV_MINIMAL < 2 1895#if EV_FEATURE_API
1858void 1896void
1859ev_loop_verify (EV_P) 1897ev_verify (EV_P)
1860{ 1898{
1861#if EV_VERIFY 1899#if EV_VERIFY
1862 int i; 1900 int i;
1863 WL w; 1901 WL w;
1864 1902
1903#if EV_ASYNC_ENABLE 1941#if EV_ASYNC_ENABLE
1904 assert (asyncmax >= asynccnt); 1942 assert (asyncmax >= asynccnt);
1905 array_verify (EV_A_ (W *)asyncs, asynccnt); 1943 array_verify (EV_A_ (W *)asyncs, asynccnt);
1906#endif 1944#endif
1907 1945
1946#if EV_PREPARE_ENABLE
1908 assert (preparemax >= preparecnt); 1947 assert (preparemax >= preparecnt);
1909 array_verify (EV_A_ (W *)prepares, preparecnt); 1948 array_verify (EV_A_ (W *)prepares, preparecnt);
1949#endif
1910 1950
1951#if EV_CHECK_ENABLE
1911 assert (checkmax >= checkcnt); 1952 assert (checkmax >= checkcnt);
1912 array_verify (EV_A_ (W *)checks, checkcnt); 1953 array_verify (EV_A_ (W *)checks, checkcnt);
1954#endif
1913 1955
1914# if 0 1956# if 0
1957#if EV_CHILD_ENABLE
1915 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 1958 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) 1959 for (signum = EV_NSIG; signum--; ) if (signals [signum].pending)
1960#endif
1917# endif 1961# endif
1918#endif 1962#endif
1919} 1963}
1920#endif 1964#endif
1921 1965
1937 1981
1938 loop_init (EV_A_ flags); 1982 loop_init (EV_A_ flags);
1939 1983
1940 if (ev_backend (EV_A)) 1984 if (ev_backend (EV_A))
1941 { 1985 {
1942#ifndef _WIN32 1986#if EV_CHILD_ENABLE
1943 ev_signal_init (&childev, childcb, SIGCHLD); 1987 ev_signal_init (&childev, childcb, SIGCHLD);
1944 ev_set_priority (&childev, EV_MAXPRI); 1988 ev_set_priority (&childev, EV_MAXPRI);
1945 ev_signal_start (EV_A_ &childev); 1989 ev_signal_start (EV_A_ &childev);
1946 ev_unref (EV_A); /* child watcher should not keep loop alive */ 1990 ev_unref (EV_A); /* child watcher should not keep loop alive */
1947#endif 1991#endif
1960 EV_P = ev_default_loop_ptr; 2004 EV_P = ev_default_loop_ptr;
1961#endif 2005#endif
1962 2006
1963 ev_default_loop_ptr = 0; 2007 ev_default_loop_ptr = 0;
1964 2008
1965#ifndef _WIN32 2009#if EV_CHILD_ENABLE
1966 ev_ref (EV_A); /* child watcher */ 2010 ev_ref (EV_A); /* child watcher */
1967 ev_signal_stop (EV_A_ &childev); 2011 ev_signal_stop (EV_A_ &childev);
1968#endif 2012#endif
1969 2013
1970 loop_destroy (EV_A); 2014 loop_destroy (EV_A);
2076 EV_FREQUENT_CHECK; 2120 EV_FREQUENT_CHECK;
2077 feed_reverse (EV_A_ (W)w); 2121 feed_reverse (EV_A_ (W)w);
2078 } 2122 }
2079 while (timercnt && ANHE_at (timers [HEAP0]) < mn_now); 2123 while (timercnt && ANHE_at (timers [HEAP0]) < mn_now);
2080 2124
2081 feed_reverse_done (EV_A_ EV_TIMEOUT); 2125 feed_reverse_done (EV_A_ EV_TIMER);
2082 } 2126 }
2083} 2127}
2084 2128
2085#if EV_PERIODIC_ENABLE 2129#if EV_PERIODIC_ENABLE
2086/* make periodics pending */ 2130/* make periodics pending */
2139 feed_reverse_done (EV_A_ EV_PERIODIC); 2183 feed_reverse_done (EV_A_ EV_PERIODIC);
2140 } 2184 }
2141} 2185}
2142 2186
2143/* simply recalculate all periodics */ 2187/* simply recalculate all periodics */
2144/* TODO: maybe ensure that at leats one event happens when jumping forward? */ 2188/* TODO: maybe ensure that at least one event happens when jumping forward? */
2145static void noinline 2189static void noinline
2146periodics_reschedule (EV_P) 2190periodics_reschedule (EV_P)
2147{ 2191{
2148 int i; 2192 int i;
2149 2193
2247} 2291}
2248 2292
2249void 2293void
2250ev_loop (EV_P_ int flags) 2294ev_loop (EV_P_ int flags)
2251{ 2295{
2252#if EV_MINIMAL < 2 2296#if EV_FEATURE_API
2253 ++loop_depth; 2297 ++loop_depth;
2254#endif 2298#endif
2255 2299
2256 assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE)); 2300 assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE));
2257 2301
2260 EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */ 2304 EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */
2261 2305
2262 do 2306 do
2263 { 2307 {
2264#if EV_VERIFY >= 2 2308#if EV_VERIFY >= 2
2265 ev_loop_verify (EV_A); 2309 ev_verify (EV_A);
2266#endif 2310#endif
2267 2311
2268#ifndef _WIN32 2312#ifndef _WIN32
2269 if (expect_false (curpid)) /* penalise the forking check even more */ 2313 if (expect_false (curpid)) /* penalise the forking check even more */
2270 if (expect_false (getpid () != curpid)) 2314 if (expect_false (getpid () != curpid))
2282 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 2326 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
2283 EV_INVOKE_PENDING; 2327 EV_INVOKE_PENDING;
2284 } 2328 }
2285#endif 2329#endif
2286 2330
2331#if EV_PREPARE_ENABLE
2287 /* queue prepare watchers (and execute them) */ 2332 /* queue prepare watchers (and execute them) */
2288 if (expect_false (preparecnt)) 2333 if (expect_false (preparecnt))
2289 { 2334 {
2290 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 2335 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
2291 EV_INVOKE_PENDING; 2336 EV_INVOKE_PENDING;
2292 } 2337 }
2338#endif
2293 2339
2294 if (expect_false (loop_done)) 2340 if (expect_false (loop_done))
2295 break; 2341 break;
2296 2342
2297 /* we might have forked, so reify kernel state if necessary */ 2343 /* we might have forked, so reify kernel state if necessary */
2348 waittime -= sleeptime; 2394 waittime -= sleeptime;
2349 } 2395 }
2350 } 2396 }
2351 } 2397 }
2352 2398
2353#if EV_MINIMAL < 2 2399#if EV_FEATURE_API
2354 ++loop_count; 2400 ++loop_count;
2355#endif 2401#endif
2356 assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */ 2402 assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */
2357 backend_poll (EV_A_ waittime); 2403 backend_poll (EV_A_ waittime);
2358 assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */ 2404 assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */
2370#if EV_IDLE_ENABLE 2416#if EV_IDLE_ENABLE
2371 /* queue idle watchers unless other events are pending */ 2417 /* queue idle watchers unless other events are pending */
2372 idle_reify (EV_A); 2418 idle_reify (EV_A);
2373#endif 2419#endif
2374 2420
2421#if EV_CHECK_ENABLE
2375 /* queue check watchers, to be executed first */ 2422 /* queue check watchers, to be executed first */
2376 if (expect_false (checkcnt)) 2423 if (expect_false (checkcnt))
2377 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 2424 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
2425#endif
2378 2426
2379 EV_INVOKE_PENDING; 2427 EV_INVOKE_PENDING;
2380 } 2428 }
2381 while (expect_true ( 2429 while (expect_true (
2382 activecnt 2430 activecnt
2385 )); 2433 ));
2386 2434
2387 if (loop_done == EVUNLOOP_ONE) 2435 if (loop_done == EVUNLOOP_ONE)
2388 loop_done = EVUNLOOP_CANCEL; 2436 loop_done = EVUNLOOP_CANCEL;
2389 2437
2390#if EV_MINIMAL < 2 2438#if EV_FEATURE_API
2391 --loop_depth; 2439 --loop_depth;
2392#endif 2440#endif
2393} 2441}
2394 2442
2395void 2443void
2712 2760
2713#ifndef SA_RESTART 2761#ifndef SA_RESTART
2714# define SA_RESTART 0 2762# define SA_RESTART 0
2715#endif 2763#endif
2716 2764
2765#if EV_SIGNAL_ENABLE
2766
2717void noinline 2767void noinline
2718ev_signal_start (EV_P_ ev_signal *w) 2768ev_signal_start (EV_P_ ev_signal *w)
2719{ 2769{
2720 if (expect_false (ev_is_active (w))) 2770 if (expect_false (ev_is_active (w)))
2721 return; 2771 return;
2827 } 2877 }
2828 2878
2829 EV_FREQUENT_CHECK; 2879 EV_FREQUENT_CHECK;
2830} 2880}
2831 2881
2882#endif
2883
2884#if EV_CHILD_ENABLE
2885
2832void 2886void
2833ev_child_start (EV_P_ ev_child *w) 2887ev_child_start (EV_P_ ev_child *w)
2834{ 2888{
2835#if EV_MULTIPLICITY 2889#if EV_MULTIPLICITY
2836 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 2890 assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
2839 return; 2893 return;
2840 2894
2841 EV_FREQUENT_CHECK; 2895 EV_FREQUENT_CHECK;
2842 2896
2843 ev_start (EV_A_ (W)w, 1); 2897 ev_start (EV_A_ (W)w, 1);
2844 wlist_add (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 2898 wlist_add (&childs [w->pid & ((EV_PID_HASHSIZE) - 1)], (WL)w);
2845 2899
2846 EV_FREQUENT_CHECK; 2900 EV_FREQUENT_CHECK;
2847} 2901}
2848 2902
2849void 2903void
2853 if (expect_false (!ev_is_active (w))) 2907 if (expect_false (!ev_is_active (w)))
2854 return; 2908 return;
2855 2909
2856 EV_FREQUENT_CHECK; 2910 EV_FREQUENT_CHECK;
2857 2911
2858 wlist_del (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 2912 wlist_del (&childs [w->pid & ((EV_PID_HASHSIZE) - 1)], (WL)w);
2859 ev_stop (EV_A_ (W)w); 2913 ev_stop (EV_A_ (W)w);
2860 2914
2861 EV_FREQUENT_CHECK; 2915 EV_FREQUENT_CHECK;
2862} 2916}
2917
2918#endif
2863 2919
2864#if EV_STAT_ENABLE 2920#if EV_STAT_ENABLE
2865 2921
2866# ifdef _WIN32 2922# ifdef _WIN32
2867# undef lstat 2923# undef lstat
2934 while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); 2990 while (w->wd < 0 && (errno == ENOENT || errno == EACCES));
2935 } 2991 }
2936 } 2992 }
2937 2993
2938 if (w->wd >= 0) 2994 if (w->wd >= 0)
2939 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); 2995 wlist_add (&fs_hash [w->wd & ((EV_INOTIFY_HASHSIZE) - 1)].head, (WL)w);
2940 2996
2941 /* now re-arm timer, if required */ 2997 /* now re-arm timer, if required */
2942 if (ev_is_active (&w->timer)) ev_ref (EV_A); 2998 if (ev_is_active (&w->timer)) ev_ref (EV_A);
2943 ev_timer_again (EV_A_ &w->timer); 2999 ev_timer_again (EV_A_ &w->timer);
2944 if (ev_is_active (&w->timer)) ev_unref (EV_A); 3000 if (ev_is_active (&w->timer)) ev_unref (EV_A);
2952 3008
2953 if (wd < 0) 3009 if (wd < 0)
2954 return; 3010 return;
2955 3011
2956 w->wd = -2; 3012 w->wd = -2;
2957 slot = wd & (EV_INOTIFY_HASHSIZE - 1); 3013 slot = wd & ((EV_INOTIFY_HASHSIZE) - 1);
2958 wlist_del (&fs_hash [slot].head, (WL)w); 3014 wlist_del (&fs_hash [slot].head, (WL)w);
2959 3015
2960 /* remove this watcher, if others are watching it, they will rearm */ 3016 /* remove this watcher, if others are watching it, they will rearm */
2961 inotify_rm_watch (fs_fd, wd); 3017 inotify_rm_watch (fs_fd, wd);
2962} 3018}
2964static void noinline 3020static void noinline
2965infy_wd (EV_P_ int slot, int wd, struct inotify_event *ev) 3021infy_wd (EV_P_ int slot, int wd, struct inotify_event *ev)
2966{ 3022{
2967 if (slot < 0) 3023 if (slot < 0)
2968 /* overflow, need to check for all hash slots */ 3024 /* overflow, need to check for all hash slots */
2969 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot) 3025 for (slot = 0; slot < (EV_INOTIFY_HASHSIZE); ++slot)
2970 infy_wd (EV_A_ slot, wd, ev); 3026 infy_wd (EV_A_ slot, wd, ev);
2971 else 3027 else
2972 { 3028 {
2973 WL w_; 3029 WL w_;
2974 3030
2975 for (w_ = fs_hash [slot & (EV_INOTIFY_HASHSIZE - 1)].head; w_; ) 3031 for (w_ = fs_hash [slot & ((EV_INOTIFY_HASHSIZE) - 1)].head; w_; )
2976 { 3032 {
2977 ev_stat *w = (ev_stat *)w_; 3033 ev_stat *w = (ev_stat *)w_;
2978 w_ = w_->next; /* lets us remove this watcher and all before it */ 3034 w_ = w_->next; /* lets us remove this watcher and all before it */
2979 3035
2980 if (w->wd == wd || wd == -1) 3036 if (w->wd == wd || wd == -1)
2981 { 3037 {
2982 if (ev->mask & (IN_IGNORED | IN_UNMOUNT | IN_DELETE_SELF)) 3038 if (ev->mask & (IN_IGNORED | IN_UNMOUNT | IN_DELETE_SELF))
2983 { 3039 {
2984 wlist_del (&fs_hash [slot & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); 3040 wlist_del (&fs_hash [slot & ((EV_INOTIFY_HASHSIZE) - 1)].head, (WL)w);
2985 w->wd = -1; 3041 w->wd = -1;
2986 infy_add (EV_A_ w); /* re-add, no matter what */ 3042 infy_add (EV_A_ w); /* re-add, no matter what */
2987 } 3043 }
2988 3044
2989 stat_timer_cb (EV_A_ &w->timer, 0); 3045 stat_timer_cb (EV_A_ &w->timer, 0);
3103 ev_io_set (&fs_w, fs_fd, EV_READ); 3159 ev_io_set (&fs_w, fs_fd, EV_READ);
3104 ev_io_start (EV_A_ &fs_w); 3160 ev_io_start (EV_A_ &fs_w);
3105 ev_unref (EV_A); 3161 ev_unref (EV_A);
3106 } 3162 }
3107 3163
3108 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot) 3164 for (slot = 0; slot < (EV_INOTIFY_HASHSIZE); ++slot)
3109 { 3165 {
3110 WL w_ = fs_hash [slot].head; 3166 WL w_ = fs_hash [slot].head;
3111 fs_hash [slot].head = 0; 3167 fs_hash [slot].head = 0;
3112 3168
3113 while (w_) 3169 while (w_)
3288 3344
3289 EV_FREQUENT_CHECK; 3345 EV_FREQUENT_CHECK;
3290} 3346}
3291#endif 3347#endif
3292 3348
3349#if EV_PREPARE_ENABLE
3293void 3350void
3294ev_prepare_start (EV_P_ ev_prepare *w) 3351ev_prepare_start (EV_P_ ev_prepare *w)
3295{ 3352{
3296 if (expect_false (ev_is_active (w))) 3353 if (expect_false (ev_is_active (w)))
3297 return; 3354 return;
3323 3380
3324 ev_stop (EV_A_ (W)w); 3381 ev_stop (EV_A_ (W)w);
3325 3382
3326 EV_FREQUENT_CHECK; 3383 EV_FREQUENT_CHECK;
3327} 3384}
3385#endif
3328 3386
3387#if EV_CHECK_ENABLE
3329void 3388void
3330ev_check_start (EV_P_ ev_check *w) 3389ev_check_start (EV_P_ ev_check *w)
3331{ 3390{
3332 if (expect_false (ev_is_active (w))) 3391 if (expect_false (ev_is_active (w)))
3333 return; 3392 return;
3359 3418
3360 ev_stop (EV_A_ (W)w); 3419 ev_stop (EV_A_ (W)w);
3361 3420
3362 EV_FREQUENT_CHECK; 3421 EV_FREQUENT_CHECK;
3363} 3422}
3423#endif
3364 3424
3365#if EV_EMBED_ENABLE 3425#if EV_EMBED_ENABLE
3366void noinline 3426void noinline
3367ev_embed_sweep (EV_P_ ev_embed *w) 3427ev_embed_sweep (EV_P_ ev_embed *w)
3368{ 3428{
3600{ 3660{
3601 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); 3661 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
3602 3662
3603 if (expect_false (!once)) 3663 if (expect_false (!once))
3604 { 3664 {
3605 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 3665 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMER, arg);
3606 return; 3666 return;
3607 } 3667 }
3608 3668
3609 once->cb = cb; 3669 once->cb = cb;
3610 once->arg = arg; 3670 once->arg = arg;
3697 if (types & EV_ASYNC) 3757 if (types & EV_ASYNC)
3698 for (i = asynccnt; i--; ) 3758 for (i = asynccnt; i--; )
3699 cb (EV_A_ EV_ASYNC, asyncs [i]); 3759 cb (EV_A_ EV_ASYNC, asyncs [i]);
3700#endif 3760#endif
3701 3761
3762#if EV_PREPARE_ENABLE
3702 if (types & EV_PREPARE) 3763 if (types & EV_PREPARE)
3703 for (i = preparecnt; i--; ) 3764 for (i = preparecnt; i--; )
3704#if EV_EMBED_ENABLE 3765# if EV_EMBED_ENABLE
3705 if (ev_cb (prepares [i]) != embed_prepare_cb) 3766 if (ev_cb (prepares [i]) != embed_prepare_cb)
3706#endif 3767# endif
3707 cb (EV_A_ EV_PREPARE, prepares [i]); 3768 cb (EV_A_ EV_PREPARE, prepares [i]);
3769#endif
3708 3770
3771#if EV_CHECK_ENABLE
3709 if (types & EV_CHECK) 3772 if (types & EV_CHECK)
3710 for (i = checkcnt; i--; ) 3773 for (i = checkcnt; i--; )
3711 cb (EV_A_ EV_CHECK, checks [i]); 3774 cb (EV_A_ EV_CHECK, checks [i]);
3775#endif
3712 3776
3777#if EV_SIGNAL_ENABLE
3713 if (types & EV_SIGNAL) 3778 if (types & EV_SIGNAL)
3714 for (i = 0; i < EV_NSIG - 1; ++i) 3779 for (i = 0; i < EV_NSIG - 1; ++i)
3715 for (wl = signals [i].head; wl; ) 3780 for (wl = signals [i].head; wl; )
3716 { 3781 {
3717 wn = wl->next; 3782 wn = wl->next;
3718 cb (EV_A_ EV_SIGNAL, wl); 3783 cb (EV_A_ EV_SIGNAL, wl);
3719 wl = wn; 3784 wl = wn;
3720 } 3785 }
3786#endif
3721 3787
3788#if EV_CHILD_ENABLE
3722 if (types & EV_CHILD) 3789 if (types & EV_CHILD)
3723 for (i = EV_PID_HASHSIZE; i--; ) 3790 for (i = (EV_PID_HASHSIZE); i--; )
3724 for (wl = childs [i]; wl; ) 3791 for (wl = childs [i]; wl; )
3725 { 3792 {
3726 wn = wl->next; 3793 wn = wl->next;
3727 cb (EV_A_ EV_CHILD, wl); 3794 cb (EV_A_ EV_CHILD, wl);
3728 wl = wn; 3795 wl = wn;
3729 } 3796 }
3797#endif
3730/* EV_STAT 0x00001000 /* stat data changed */ 3798/* EV_STAT 0x00001000 /* stat data changed */
3731/* EV_EMBED 0x00010000 /* embedded event loop needs sweep */ 3799/* EV_EMBED 0x00010000 /* embedded event loop needs sweep */
3732} 3800}
3733#endif 3801#endif
3734 3802

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines