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

Comparing libev/ev.c (file contents):
Revision 1.79 by root, Fri Nov 9 15:15:20 2007 UTC vs.
Revision 1.114 by root, Mon Nov 12 20:03:39 2007 UTC

26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
31#ifndef EV_STANDALONE 36#ifndef EV_STANDALONE
32# include "config.h" 37# include "config.h"
33 38
34# if HAVE_CLOCK_GETTIME 39# if HAVE_CLOCK_GETTIME
40# ifndef EV_USE_MONOTONIC
35# define EV_USE_MONOTONIC 1 41# define EV_USE_MONOTONIC 1
42# endif
43# ifndef EV_USE_REALTIME
36# define EV_USE_REALTIME 1 44# define EV_USE_REALTIME 1
45# endif
37# endif 46# endif
38 47
39# if HAVE_SELECT && HAVE_SYS_SELECT_H 48# if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT)
40# define EV_USE_SELECT 1 49# define EV_USE_SELECT 1
41# endif 50# endif
42 51
43# if HAVE_POLL && HAVE_POLL_H 52# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL)
44# define EV_USE_POLL 1 53# define EV_USE_POLL 1
45# endif 54# endif
46 55
47# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H 56# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL)
48# define EV_USE_EPOLL 1 57# define EV_USE_EPOLL 1
49# endif 58# endif
50 59
51# if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H 60# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE)
52# define EV_USE_KQUEUE 1 61# define EV_USE_KQUEUE 1
53# endif 62# endif
54 63
55#endif 64#endif
56 65
66#include <sys/types.h> 75#include <sys/types.h>
67#include <time.h> 76#include <time.h>
68 77
69#include <signal.h> 78#include <signal.h>
70 79
71#ifndef WIN32 80#ifndef _WIN32
72# include <unistd.h> 81# include <unistd.h>
73# include <sys/time.h> 82# include <sys/time.h>
74# include <sys/wait.h> 83# include <sys/wait.h>
84#else
85# define WIN32_LEAN_AND_MEAN
86# include <windows.h>
87# ifndef EV_SELECT_IS_WINSOCKET
88# define EV_SELECT_IS_WINSOCKET 1
75#endif 89# endif
90#endif
91
76/**/ 92/**/
77 93
78#ifndef EV_USE_MONOTONIC 94#ifndef EV_USE_MONOTONIC
79# define EV_USE_MONOTONIC 1 95# define EV_USE_MONOTONIC 1
80#endif 96#endif
81 97
82#ifndef EV_USE_SELECT 98#ifndef EV_USE_SELECT
83# define EV_USE_SELECT 1 99# define EV_USE_SELECT 1
100# define EV_SELECT_USE_FD_SET 1
84#endif 101#endif
85 102
86#ifndef EV_USE_POLL 103#ifndef EV_USE_POLL
87# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ 104# ifdef _WIN32
105# define EV_USE_POLL 0
106# else
107# define EV_USE_POLL 1
108# endif
88#endif 109#endif
89 110
90#ifndef EV_USE_EPOLL 111#ifndef EV_USE_EPOLL
91# define EV_USE_EPOLL 0 112# define EV_USE_EPOLL 0
92#endif 113#endif
93 114
94#ifndef EV_USE_KQUEUE 115#ifndef EV_USE_KQUEUE
95# define EV_USE_KQUEUE 0 116# define EV_USE_KQUEUE 0
96#endif 117#endif
97 118
98#ifndef EV_USE_WIN32
99# ifdef WIN32
100# define EV_USE_WIN32 0 /* it does not exist, use select */
101# undef EV_USE_SELECT
102# define EV_USE_SELECT 1
103# else
104# define EV_USE_WIN32 0
105# endif
106#endif
107
108#ifndef EV_USE_REALTIME 119#ifndef EV_USE_REALTIME
109# define EV_USE_REALTIME 1 120# define EV_USE_REALTIME 1
110#endif 121#endif
111 122
112/**/ 123/**/
124
125/* darwin simply cannot be helped */
126#ifdef __APPLE__
127# undef EV_USE_POLL
128# undef EV_USE_KQUEUE
129#endif
113 130
114#ifndef CLOCK_MONOTONIC 131#ifndef CLOCK_MONOTONIC
115# undef EV_USE_MONOTONIC 132# undef EV_USE_MONOTONIC
116# define EV_USE_MONOTONIC 0 133# define EV_USE_MONOTONIC 0
117#endif 134#endif
118 135
119#ifndef CLOCK_REALTIME 136#ifndef CLOCK_REALTIME
120# undef EV_USE_REALTIME 137# undef EV_USE_REALTIME
121# define EV_USE_REALTIME 0 138# define EV_USE_REALTIME 0
139#endif
140
141#if EV_SELECT_IS_WINSOCKET
142# include <winsock.h>
122#endif 143#endif
123 144
124/**/ 145/**/
125 146
126#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 147#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
127#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ 148#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
128#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ 149#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
129/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ 150/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
130 151
152#ifdef EV_H
153# include EV_H
154#else
131#include "ev.h" 155# include "ev.h"
156#endif
132 157
133#if __GNUC__ >= 3 158#if __GNUC__ >= 3
134# define expect(expr,value) __builtin_expect ((expr),(value)) 159# define expect(expr,value) __builtin_expect ((expr),(value))
135# define inline inline 160# define inline inline
136#else 161#else
142#define expect_true(expr) expect ((expr) != 0, 1) 167#define expect_true(expr) expect ((expr) != 0, 1)
143 168
144#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 169#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
145#define ABSPRI(w) ((w)->priority - EV_MINPRI) 170#define ABSPRI(w) ((w)->priority - EV_MINPRI)
146 171
172#define EMPTY0 /* required for microsofts broken pseudo-c compiler */
173#define EMPTY2(a,b) /* used to suppress some warnings */
174
147typedef struct ev_watcher *W; 175typedef struct ev_watcher *W;
148typedef struct ev_watcher_list *WL; 176typedef struct ev_watcher_list *WL;
149typedef struct ev_watcher_time *WT; 177typedef struct ev_watcher_time *WT;
150 178
151static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 179static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
152 180
181#ifdef _WIN32
153#include "ev_win32.c" 182# include "ev_win32.c"
183#endif
154 184
155/*****************************************************************************/ 185/*****************************************************************************/
156 186
157static void (*syserr_cb)(const char *msg); 187static void (*syserr_cb)(const char *msg);
158 188
205typedef struct 235typedef struct
206{ 236{
207 WL head; 237 WL head;
208 unsigned char events; 238 unsigned char events;
209 unsigned char reify; 239 unsigned char reify;
240#if EV_SELECT_IS_WINSOCKET
241 SOCKET handle;
242#endif
210} ANFD; 243} ANFD;
211 244
212typedef struct 245typedef struct
213{ 246{
214 W w; 247 W w;
215 int events; 248 int events;
216} ANPENDING; 249} ANPENDING;
217 250
218#if EV_MULTIPLICITY 251#if EV_MULTIPLICITY
219 252
220struct ev_loop 253 struct ev_loop
221{ 254 {
255 ev_tstamp ev_rt_now;
256 #define ev_rt_now ((loop)->ev_rt_now)
222# define VAR(name,decl) decl; 257 #define VAR(name,decl) decl;
223# include "ev_vars.h" 258 #include "ev_vars.h"
224};
225# undef VAR 259 #undef VAR
260 };
226# include "ev_wrap.h" 261 #include "ev_wrap.h"
262
263 struct ev_loop default_loop_struct;
264 static struct ev_loop *default_loop;
227 265
228#else 266#else
229 267
268 ev_tstamp ev_rt_now;
230# define VAR(name,decl) static decl; 269 #define VAR(name,decl) static decl;
231# include "ev_vars.h" 270 #include "ev_vars.h"
232# undef VAR 271 #undef VAR
272
273 static int default_loop;
233 274
234#endif 275#endif
235 276
236/*****************************************************************************/ 277/*****************************************************************************/
237 278
238inline ev_tstamp 279ev_tstamp
239ev_time (void) 280ev_time (void)
240{ 281{
241#if EV_USE_REALTIME 282#if EV_USE_REALTIME
242 struct timespec ts; 283 struct timespec ts;
243 clock_gettime (CLOCK_REALTIME, &ts); 284 clock_gettime (CLOCK_REALTIME, &ts);
262#endif 303#endif
263 304
264 return ev_time (); 305 return ev_time ();
265} 306}
266 307
308#if EV_MULTIPLICITY
267ev_tstamp 309ev_tstamp
268ev_now (EV_P) 310ev_now (EV_P)
269{ 311{
270 return rt_now; 312 return ev_rt_now;
271} 313}
314#endif
272 315
273#define array_roundsize(type,n) ((n) | 4 & ~3) 316#define array_roundsize(type,n) (((n) | 4) & ~3)
274 317
275#define array_needsize(type,base,cur,cnt,init) \ 318#define array_needsize(type,base,cur,cnt,init) \
276 if (expect_false ((cnt) > cur)) \ 319 if (expect_false ((cnt) > cur)) \
277 { \ 320 { \
278 int newcnt = cur; \ 321 int newcnt = cur; \
293 stem ## max = array_roundsize (stem ## cnt >> 1); \ 336 stem ## max = array_roundsize (stem ## cnt >> 1); \
294 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 337 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
295 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 338 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
296 } 339 }
297 340
298/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */
299/* bringing us everlasting joy in form of stupid extra macros that are not required in C */
300#define array_free_microshit(stem) \
301 ev_free (stem ## s); stem ## cnt = stem ## max = 0;
302
303#define array_free(stem, idx) \ 341#define array_free(stem, idx) \
304 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 342 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
305 343
306/*****************************************************************************/ 344/*****************************************************************************/
307 345
328 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; 366 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
329 return; 367 return;
330 } 368 }
331 369
332 w_->pending = ++pendingcnt [ABSPRI (w_)]; 370 w_->pending = ++pendingcnt [ABSPRI (w_)];
333 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); 371 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2);
334 pendings [ABSPRI (w_)][w_->pending - 1].w = w_; 372 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
335 pendings [ABSPRI (w_)][w_->pending - 1].events = revents; 373 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
336} 374}
337 375
338static void 376static void
381 int events = 0; 419 int events = 0;
382 420
383 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 421 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
384 events |= w->events; 422 events |= w->events;
385 423
424#if EV_SELECT_IS_WINSOCKET
425 if (events)
426 {
427 unsigned long argp;
428 anfd->handle = _get_osfhandle (fd);
429 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0));
430 }
431#endif
432
386 anfd->reify = 0; 433 anfd->reify = 0;
387 434
388 method_modify (EV_A_ fd, anfd->events, events); 435 method_modify (EV_A_ fd, anfd->events, events);
389 anfd->events = events; 436 anfd->events = events;
390 } 437 }
399 return; 446 return;
400 447
401 anfds [fd].reify = 1; 448 anfds [fd].reify = 1;
402 449
403 ++fdchangecnt; 450 ++fdchangecnt;
404 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); 451 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
405 fdchanges [fdchangecnt - 1] = fd; 452 fdchanges [fdchangecnt - 1] = fd;
406} 453}
407 454
408static void 455static void
409fd_kill (EV_P_ int fd) 456fd_kill (EV_P_ int fd)
418} 465}
419 466
420static int 467static int
421fd_valid (int fd) 468fd_valid (int fd)
422{ 469{
423#ifdef WIN32 470#ifdef _WIN32
424 return !!win32_get_osfhandle (fd); 471 return _get_osfhandle (fd) != -1;
425#else 472#else
426 return fcntl (fd, F_GETFD) != -1; 473 return fcntl (fd, F_GETFD) != -1;
427#endif 474#endif
428} 475}
429 476
509 556
510 heap [k] = w; 557 heap [k] = w;
511 ((W)heap [k])->active = k + 1; 558 ((W)heap [k])->active = k + 1;
512} 559}
513 560
561inline void
562adjustheap (WT *heap, int N, int k)
563{
564 upheap (heap, k);
565 downheap (heap, N, k);
566}
567
514/*****************************************************************************/ 568/*****************************************************************************/
515 569
516typedef struct 570typedef struct
517{ 571{
518 WL head; 572 WL head;
539} 593}
540 594
541static void 595static void
542sighandler (int signum) 596sighandler (int signum)
543{ 597{
544#if WIN32 598#if _WIN32
545 signal (signum, sighandler); 599 signal (signum, sighandler);
546#endif 600#endif
547 601
548 signals [signum - 1].gotsig = 1; 602 signals [signum - 1].gotsig = 1;
549 603
550 if (!gotsig) 604 if (!gotsig)
551 { 605 {
552 int old_errno = errno; 606 int old_errno = errno;
553 gotsig = 1; 607 gotsig = 1;
554#ifdef WIN32
555 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
556#else
557 write (sigpipe [1], &signum, 1); 608 write (sigpipe [1], &signum, 1);
558#endif
559 errno = old_errno; 609 errno = old_errno;
560 } 610 }
561} 611}
562 612
563void 613void
564ev_feed_signal_event (EV_P_ int signum) 614ev_feed_signal_event (EV_P_ int signum)
565{ 615{
616 WL w;
617
566#if EV_MULTIPLICITY 618#if EV_MULTIPLICITY
567 assert (("feeding signal events is only supported in the default loop", loop == default_loop)); 619 assert (("feeding signal events is only supported in the default loop", loop == default_loop));
568#endif 620#endif
569 621
570 --signum; 622 --signum;
579} 631}
580 632
581static void 633static void
582sigcb (EV_P_ struct ev_io *iow, int revents) 634sigcb (EV_P_ struct ev_io *iow, int revents)
583{ 635{
584 WL w;
585 int signum; 636 int signum;
586 637
587#ifdef WIN32
588 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
589#else
590 read (sigpipe [0], &revents, 1); 638 read (sigpipe [0], &revents, 1);
591#endif
592 gotsig = 0; 639 gotsig = 0;
593 640
594 for (signum = signalmax; signum--; ) 641 for (signum = signalmax; signum--; )
595 if (signals [signum].gotsig) 642 if (signals [signum].gotsig)
596 sigevent (EV_A_ signum + 1); 643 ev_feed_signal_event (EV_A_ signum + 1);
644}
645
646inline void
647fd_intern (int fd)
648{
649#ifdef _WIN32
650 int arg = 1;
651 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
652#else
653 fcntl (fd, F_SETFD, FD_CLOEXEC);
654 fcntl (fd, F_SETFL, O_NONBLOCK);
655#endif
597} 656}
598 657
599static void 658static void
600siginit (EV_P) 659siginit (EV_P)
601{ 660{
602#ifndef WIN32 661 fd_intern (sigpipe [0]);
603 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 662 fd_intern (sigpipe [1]);
604 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
605
606 /* rather than sort out wether we really need nb, set it */
607 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
608 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
609#endif
610 663
611 ev_io_set (&sigev, sigpipe [0], EV_READ); 664 ev_io_set (&sigev, sigpipe [0], EV_READ);
612 ev_io_start (EV_A_ &sigev); 665 ev_io_start (EV_A_ &sigev);
613 ev_unref (EV_A); /* child watcher should not keep loop alive */ 666 ev_unref (EV_A); /* child watcher should not keep loop alive */
614} 667}
615 668
616/*****************************************************************************/ 669/*****************************************************************************/
617 670
618static struct ev_child *childs [PID_HASHSIZE]; 671static struct ev_child *childs [PID_HASHSIZE];
619 672
620#ifndef WIN32 673#ifndef _WIN32
621 674
622static struct ev_signal childev; 675static struct ev_signal childev;
623 676
624#ifndef WCONTINUED 677#ifndef WCONTINUED
625# define WCONTINUED 0 678# define WCONTINUED 0
686 739
687/* return true if we are running with elevated privileges and should ignore env variables */ 740/* return true if we are running with elevated privileges and should ignore env variables */
688static int 741static int
689enable_secure (void) 742enable_secure (void)
690{ 743{
691#ifdef WIN32 744#ifdef _WIN32
692 return 0; 745 return 0;
693#else 746#else
694 return getuid () != geteuid () 747 return getuid () != geteuid ()
695 || getgid () != getegid (); 748 || getgid () != getegid ();
696#endif 749#endif
697} 750}
698 751
699int 752unsigned int
700ev_method (EV_P) 753ev_method (EV_P)
701{ 754{
702 return method; 755 return method;
703} 756}
704 757
705static void 758static void
706loop_init (EV_P_ int methods) 759loop_init (EV_P_ unsigned int flags)
707{ 760{
708 if (!method) 761 if (!method)
709 { 762 {
710#if EV_USE_MONOTONIC 763#if EV_USE_MONOTONIC
711 { 764 {
713 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 766 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
714 have_monotonic = 1; 767 have_monotonic = 1;
715 } 768 }
716#endif 769#endif
717 770
718 rt_now = ev_time (); 771 ev_rt_now = ev_time ();
719 mn_now = get_clock (); 772 mn_now = get_clock ();
720 now_floor = mn_now; 773 now_floor = mn_now;
721 rtmn_diff = rt_now - mn_now; 774 rtmn_diff = ev_rt_now - mn_now;
722 775
723 if (methods == EVMETHOD_AUTO) 776 if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS"))
724 if (!enable_secure () && getenv ("LIBEV_METHODS"))
725 methods = atoi (getenv ("LIBEV_METHODS")); 777 flags = atoi (getenv ("LIBEV_FLAGS"));
726 else 778
727 methods = EVMETHOD_ANY; 779 if (!(flags & 0x0000ffff))
780 flags |= 0x0000ffff;
728 781
729 method = 0; 782 method = 0;
730#if EV_USE_WIN32
731 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
732#endif
733#if EV_USE_KQUEUE 783#if EV_USE_KQUEUE
734 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 784 if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags);
735#endif 785#endif
736#if EV_USE_EPOLL 786#if EV_USE_EPOLL
737 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 787 if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags);
738#endif 788#endif
739#if EV_USE_POLL 789#if EV_USE_POLL
740 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 790 if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags);
741#endif 791#endif
742#if EV_USE_SELECT 792#if EV_USE_SELECT
743 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 793 if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags);
744#endif 794#endif
745 795
746 ev_watcher_init (&sigev, sigcb); 796 ev_init (&sigev, sigcb);
747 ev_set_priority (&sigev, EV_MAXPRI); 797 ev_set_priority (&sigev, EV_MAXPRI);
748 } 798 }
749} 799}
750 800
751void 801void
752loop_destroy (EV_P) 802loop_destroy (EV_P)
753{ 803{
754 int i; 804 int i;
755 805
756#if EV_USE_WIN32
757 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
758#endif
759#if EV_USE_KQUEUE 806#if EV_USE_KQUEUE
760 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 807 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
761#endif 808#endif
762#if EV_USE_EPOLL 809#if EV_USE_EPOLL
763 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 810 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
771 818
772 for (i = NUMPRI; i--; ) 819 for (i = NUMPRI; i--; )
773 array_free (pending, [i]); 820 array_free (pending, [i]);
774 821
775 /* have to use the microsoft-never-gets-it-right macro */ 822 /* have to use the microsoft-never-gets-it-right macro */
776 array_free_microshit (fdchange); 823 array_free (fdchange, EMPTY0);
777 array_free_microshit (timer); 824 array_free (timer, EMPTY0);
778 array_free_microshit (periodic); 825#if EV_PERIODICS
779 array_free_microshit (idle); 826 array_free (periodic, EMPTY0);
780 array_free_microshit (prepare); 827#endif
781 array_free_microshit (check); 828 array_free (idle, EMPTY0);
829 array_free (prepare, EMPTY0);
830 array_free (check, EMPTY0);
782 831
783 method = 0; 832 method = 0;
784} 833}
785 834
786static void 835static void
811 postfork = 0; 860 postfork = 0;
812} 861}
813 862
814#if EV_MULTIPLICITY 863#if EV_MULTIPLICITY
815struct ev_loop * 864struct ev_loop *
816ev_loop_new (int methods) 865ev_loop_new (unsigned int flags)
817{ 866{
818 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 867 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
819 868
820 memset (loop, 0, sizeof (struct ev_loop)); 869 memset (loop, 0, sizeof (struct ev_loop));
821 870
822 loop_init (EV_A_ methods); 871 loop_init (EV_A_ flags);
823 872
824 if (ev_method (EV_A)) 873 if (ev_method (EV_A))
825 return loop; 874 return loop;
826 875
827 return 0; 876 return 0;
841} 890}
842 891
843#endif 892#endif
844 893
845#if EV_MULTIPLICITY 894#if EV_MULTIPLICITY
846struct ev_loop default_loop_struct;
847static struct ev_loop *default_loop;
848
849struct ev_loop * 895struct ev_loop *
850#else 896#else
851static int default_loop;
852
853int 897int
854#endif 898#endif
855ev_default_loop (int methods) 899ev_default_loop (unsigned int flags)
856{ 900{
857 if (sigpipe [0] == sigpipe [1]) 901 if (sigpipe [0] == sigpipe [1])
858 if (pipe (sigpipe)) 902 if (pipe (sigpipe))
859 return 0; 903 return 0;
860 904
864 struct ev_loop *loop = default_loop = &default_loop_struct; 908 struct ev_loop *loop = default_loop = &default_loop_struct;
865#else 909#else
866 default_loop = 1; 910 default_loop = 1;
867#endif 911#endif
868 912
869 loop_init (EV_A_ methods); 913 loop_init (EV_A_ flags);
870 914
871 if (ev_method (EV_A)) 915 if (ev_method (EV_A))
872 { 916 {
873 siginit (EV_A); 917 siginit (EV_A);
874 918
875#ifndef WIN32 919#ifndef _WIN32
876 ev_signal_init (&childev, childcb, SIGCHLD); 920 ev_signal_init (&childev, childcb, SIGCHLD);
877 ev_set_priority (&childev, EV_MAXPRI); 921 ev_set_priority (&childev, EV_MAXPRI);
878 ev_signal_start (EV_A_ &childev); 922 ev_signal_start (EV_A_ &childev);
879 ev_unref (EV_A); /* child watcher should not keep loop alive */ 923 ev_unref (EV_A); /* child watcher should not keep loop alive */
880#endif 924#endif
891{ 935{
892#if EV_MULTIPLICITY 936#if EV_MULTIPLICITY
893 struct ev_loop *loop = default_loop; 937 struct ev_loop *loop = default_loop;
894#endif 938#endif
895 939
896#ifndef WIN32 940#ifndef _WIN32
897 ev_ref (EV_A); /* child watcher */ 941 ev_ref (EV_A); /* child watcher */
898 ev_signal_stop (EV_A_ &childev); 942 ev_signal_stop (EV_A_ &childev);
899#endif 943#endif
900 944
901 ev_ref (EV_A); /* signal watcher */ 945 ev_ref (EV_A); /* signal watcher */
943 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 987 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
944 988
945 if (p->w) 989 if (p->w)
946 { 990 {
947 p->w->pending = 0; 991 p->w->pending = 0;
948 p->w->cb (EV_A_ p->w, p->events); 992 EV_CB_INVOKE (p->w, p->events);
949 } 993 }
950 } 994 }
951} 995}
952 996
953static void 997static void
961 1005
962 /* first reschedule or stop timer */ 1006 /* first reschedule or stop timer */
963 if (w->repeat) 1007 if (w->repeat)
964 { 1008 {
965 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 1009 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1010
966 ((WT)w)->at = mn_now + w->repeat; 1011 ((WT)w)->at += w->repeat;
1012 if (((WT)w)->at < mn_now)
1013 ((WT)w)->at = mn_now;
1014
967 downheap ((WT *)timers, timercnt, 0); 1015 downheap ((WT *)timers, timercnt, 0);
968 } 1016 }
969 else 1017 else
970 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1018 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
971 1019
972 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1020 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
973 } 1021 }
974} 1022}
975 1023
1024#if EV_PERIODICS
976static void 1025static void
977periodics_reify (EV_P) 1026periodics_reify (EV_P)
978{ 1027{
979 while (periodiccnt && ((WT)periodics [0])->at <= rt_now) 1028 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
980 { 1029 {
981 struct ev_periodic *w = periodics [0]; 1030 struct ev_periodic *w = periodics [0];
982 1031
983 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1032 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
984 1033
985 /* first reschedule or stop timer */ 1034 /* first reschedule or stop timer */
986 if (w->reschedule_cb) 1035 if (w->reschedule_cb)
987 { 1036 {
988 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); 1037 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
989
990 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); 1038 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
991 downheap ((WT *)periodics, periodiccnt, 0); 1039 downheap ((WT *)periodics, periodiccnt, 0);
992 } 1040 }
993 else if (w->interval) 1041 else if (w->interval)
994 { 1042 {
995 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 1043 ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
996 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); 1044 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
997 downheap ((WT *)periodics, periodiccnt, 0); 1045 downheap ((WT *)periodics, periodiccnt, 0);
998 } 1046 }
999 else 1047 else
1000 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1048 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1001 1049
1012 for (i = 0; i < periodiccnt; ++i) 1060 for (i = 0; i < periodiccnt; ++i)
1013 { 1061 {
1014 struct ev_periodic *w = periodics [i]; 1062 struct ev_periodic *w = periodics [i];
1015 1063
1016 if (w->reschedule_cb) 1064 if (w->reschedule_cb)
1017 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1065 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1018 else if (w->interval) 1066 else if (w->interval)
1019 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1067 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1020 } 1068 }
1021 1069
1022 /* now rebuild the heap */ 1070 /* now rebuild the heap */
1023 for (i = periodiccnt >> 1; i--; ) 1071 for (i = periodiccnt >> 1; i--; )
1024 downheap ((WT *)periodics, periodiccnt, i); 1072 downheap ((WT *)periodics, periodiccnt, i);
1025} 1073}
1074#endif
1026 1075
1027inline int 1076inline int
1028time_update_monotonic (EV_P) 1077time_update_monotonic (EV_P)
1029{ 1078{
1030 mn_now = get_clock (); 1079 mn_now = get_clock ();
1031 1080
1032 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1081 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1033 { 1082 {
1034 rt_now = rtmn_diff + mn_now; 1083 ev_rt_now = rtmn_diff + mn_now;
1035 return 0; 1084 return 0;
1036 } 1085 }
1037 else 1086 else
1038 { 1087 {
1039 now_floor = mn_now; 1088 now_floor = mn_now;
1040 rt_now = ev_time (); 1089 ev_rt_now = ev_time ();
1041 return 1; 1090 return 1;
1042 } 1091 }
1043} 1092}
1044 1093
1045static void 1094static void
1054 { 1103 {
1055 ev_tstamp odiff = rtmn_diff; 1104 ev_tstamp odiff = rtmn_diff;
1056 1105
1057 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1106 for (i = 4; --i; ) /* loop a few times, before making important decisions */
1058 { 1107 {
1059 rtmn_diff = rt_now - mn_now; 1108 rtmn_diff = ev_rt_now - mn_now;
1060 1109
1061 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1110 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1062 return; /* all is well */ 1111 return; /* all is well */
1063 1112
1064 rt_now = ev_time (); 1113 ev_rt_now = ev_time ();
1065 mn_now = get_clock (); 1114 mn_now = get_clock ();
1066 now_floor = mn_now; 1115 now_floor = mn_now;
1067 } 1116 }
1068 1117
1118# if EV_PERIODICS
1069 periodics_reschedule (EV_A); 1119 periodics_reschedule (EV_A);
1120# endif
1070 /* no timer adjustment, as the monotonic clock doesn't jump */ 1121 /* no timer adjustment, as the monotonic clock doesn't jump */
1071 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1122 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1072 } 1123 }
1073 } 1124 }
1074 else 1125 else
1075#endif 1126#endif
1076 { 1127 {
1077 rt_now = ev_time (); 1128 ev_rt_now = ev_time ();
1078 1129
1079 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1130 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1080 { 1131 {
1132#if EV_PERIODICS
1081 periodics_reschedule (EV_A); 1133 periodics_reschedule (EV_A);
1134#endif
1082 1135
1083 /* adjust timers. this is easy, as the offset is the same for all */ 1136 /* adjust timers. this is easy, as the offset is the same for all */
1084 for (i = 0; i < timercnt; ++i) 1137 for (i = 0; i < timercnt; ++i)
1085 ((WT)timers [i])->at += rt_now - mn_now; 1138 ((WT)timers [i])->at += ev_rt_now - mn_now;
1086 } 1139 }
1087 1140
1088 mn_now = rt_now; 1141 mn_now = ev_rt_now;
1089 } 1142 }
1090} 1143}
1091 1144
1092void 1145void
1093ev_ref (EV_P) 1146ev_ref (EV_P)
1133 if (expect_true (have_monotonic)) 1186 if (expect_true (have_monotonic))
1134 time_update_monotonic (EV_A); 1187 time_update_monotonic (EV_A);
1135 else 1188 else
1136#endif 1189#endif
1137 { 1190 {
1138 rt_now = ev_time (); 1191 ev_rt_now = ev_time ();
1139 mn_now = rt_now; 1192 mn_now = ev_rt_now;
1140 } 1193 }
1141 1194
1142 if (flags & EVLOOP_NONBLOCK || idlecnt) 1195 if (flags & EVLOOP_NONBLOCK || idlecnt)
1143 block = 0.; 1196 block = 0.;
1144 else 1197 else
1149 { 1202 {
1150 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1203 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
1151 if (block > to) block = to; 1204 if (block > to) block = to;
1152 } 1205 }
1153 1206
1207#if EV_PERIODICS
1154 if (periodiccnt) 1208 if (periodiccnt)
1155 { 1209 {
1156 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; 1210 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1157 if (block > to) block = to; 1211 if (block > to) block = to;
1158 } 1212 }
1213#endif
1159 1214
1160 if (block < 0.) block = 0.; 1215 if (block < 0.) block = 0.;
1161 } 1216 }
1162 1217
1163 method_poll (EV_A_ block); 1218 method_poll (EV_A_ block);
1164 1219
1165 /* update rt_now, do magic */ 1220 /* update ev_rt_now, do magic */
1166 time_update (EV_A); 1221 time_update (EV_A);
1167 1222
1168 /* queue pending timers and reschedule them */ 1223 /* queue pending timers and reschedule them */
1169 timers_reify (EV_A); /* relative timers called last */ 1224 timers_reify (EV_A); /* relative timers called last */
1225#if EV_PERIODICS
1170 periodics_reify (EV_A); /* absolute timers called first */ 1226 periodics_reify (EV_A); /* absolute timers called first */
1227#endif
1171 1228
1172 /* queue idle watchers unless io or timers are pending */ 1229 /* queue idle watchers unless io or timers are pending */
1173 if (idlecnt && !any_pending (EV_A)) 1230 if (idlecnt && !any_pending (EV_A))
1174 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1231 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1175 1232
1266{ 1323{
1267 ev_clear_pending (EV_A_ (W)w); 1324 ev_clear_pending (EV_A_ (W)w);
1268 if (!ev_is_active (w)) 1325 if (!ev_is_active (w))
1269 return; 1326 return;
1270 1327
1328 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1329
1271 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1330 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1272 ev_stop (EV_A_ (W)w); 1331 ev_stop (EV_A_ (W)w);
1273 1332
1274 fd_change (EV_A_ w->fd); 1333 fd_change (EV_A_ w->fd);
1275} 1334}
1283 ((WT)w)->at += mn_now; 1342 ((WT)w)->at += mn_now;
1284 1343
1285 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1344 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1286 1345
1287 ev_start (EV_A_ (W)w, ++timercnt); 1346 ev_start (EV_A_ (W)w, ++timercnt);
1288 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); 1347 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2);
1289 timers [timercnt - 1] = w; 1348 timers [timercnt - 1] = w;
1290 upheap ((WT *)timers, timercnt - 1); 1349 upheap ((WT *)timers, timercnt - 1);
1291 1350
1292 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1351 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1293} 1352}
1302 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1361 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1303 1362
1304 if (((W)w)->active < timercnt--) 1363 if (((W)w)->active < timercnt--)
1305 { 1364 {
1306 timers [((W)w)->active - 1] = timers [timercnt]; 1365 timers [((W)w)->active - 1] = timers [timercnt];
1307 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1366 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1308 } 1367 }
1309 1368
1310 ((WT)w)->at = w->repeat; 1369 ((WT)w)->at -= mn_now;
1311 1370
1312 ev_stop (EV_A_ (W)w); 1371 ev_stop (EV_A_ (W)w);
1313} 1372}
1314 1373
1315void 1374void
1318 if (ev_is_active (w)) 1377 if (ev_is_active (w))
1319 { 1378 {
1320 if (w->repeat) 1379 if (w->repeat)
1321 { 1380 {
1322 ((WT)w)->at = mn_now + w->repeat; 1381 ((WT)w)->at = mn_now + w->repeat;
1323 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1382 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1324 } 1383 }
1325 else 1384 else
1326 ev_timer_stop (EV_A_ w); 1385 ev_timer_stop (EV_A_ w);
1327 } 1386 }
1328 else if (w->repeat) 1387 else if (w->repeat)
1388 {
1389 w->at = w->repeat;
1329 ev_timer_start (EV_A_ w); 1390 ev_timer_start (EV_A_ w);
1391 }
1330} 1392}
1331 1393
1394#if EV_PERIODICS
1332void 1395void
1333ev_periodic_start (EV_P_ struct ev_periodic *w) 1396ev_periodic_start (EV_P_ struct ev_periodic *w)
1334{ 1397{
1335 if (ev_is_active (w)) 1398 if (ev_is_active (w))
1336 return; 1399 return;
1337 1400
1338 if (w->reschedule_cb) 1401 if (w->reschedule_cb)
1339 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1402 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1340 else if (w->interval) 1403 else if (w->interval)
1341 { 1404 {
1342 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1405 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1343 /* this formula differs from the one in periodic_reify because we do not always round up */ 1406 /* this formula differs from the one in periodic_reify because we do not always round up */
1344 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1407 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1345 } 1408 }
1346 1409
1347 ev_start (EV_A_ (W)w, ++periodiccnt); 1410 ev_start (EV_A_ (W)w, ++periodiccnt);
1348 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1411 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1349 periodics [periodiccnt - 1] = w; 1412 periodics [periodiccnt - 1] = w;
1350 upheap ((WT *)periodics, periodiccnt - 1); 1413 upheap ((WT *)periodics, periodiccnt - 1);
1351 1414
1352 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1415 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1353} 1416}
1362 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1425 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1363 1426
1364 if (((W)w)->active < periodiccnt--) 1427 if (((W)w)->active < periodiccnt--)
1365 { 1428 {
1366 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1429 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1367 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1430 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1368 } 1431 }
1369 1432
1370 ev_stop (EV_A_ (W)w); 1433 ev_stop (EV_A_ (W)w);
1371} 1434}
1372 1435
1373void 1436void
1374ev_periodic_again (EV_P_ struct ev_periodic *w) 1437ev_periodic_again (EV_P_ struct ev_periodic *w)
1375{ 1438{
1439 /* TODO: use adjustheap and recalculation */
1376 ev_periodic_stop (EV_A_ w); 1440 ev_periodic_stop (EV_A_ w);
1377 ev_periodic_start (EV_A_ w); 1441 ev_periodic_start (EV_A_ w);
1378} 1442}
1443#endif
1379 1444
1380void 1445void
1381ev_idle_start (EV_P_ struct ev_idle *w) 1446ev_idle_start (EV_P_ struct ev_idle *w)
1382{ 1447{
1383 if (ev_is_active (w)) 1448 if (ev_is_active (w))
1384 return; 1449 return;
1385 1450
1386 ev_start (EV_A_ (W)w, ++idlecnt); 1451 ev_start (EV_A_ (W)w, ++idlecnt);
1387 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); 1452 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1388 idles [idlecnt - 1] = w; 1453 idles [idlecnt - 1] = w;
1389} 1454}
1390 1455
1391void 1456void
1392ev_idle_stop (EV_P_ struct ev_idle *w) 1457ev_idle_stop (EV_P_ struct ev_idle *w)
1393{ 1458{
1394 ev_clear_pending (EV_A_ (W)w); 1459 ev_clear_pending (EV_A_ (W)w);
1395 if (ev_is_active (w)) 1460 if (!ev_is_active (w))
1396 return; 1461 return;
1397 1462
1398 idles [((W)w)->active - 1] = idles [--idlecnt]; 1463 idles [((W)w)->active - 1] = idles [--idlecnt];
1399 ev_stop (EV_A_ (W)w); 1464 ev_stop (EV_A_ (W)w);
1400} 1465}
1404{ 1469{
1405 if (ev_is_active (w)) 1470 if (ev_is_active (w))
1406 return; 1471 return;
1407 1472
1408 ev_start (EV_A_ (W)w, ++preparecnt); 1473 ev_start (EV_A_ (W)w, ++preparecnt);
1409 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); 1474 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1410 prepares [preparecnt - 1] = w; 1475 prepares [preparecnt - 1] = w;
1411} 1476}
1412 1477
1413void 1478void
1414ev_prepare_stop (EV_P_ struct ev_prepare *w) 1479ev_prepare_stop (EV_P_ struct ev_prepare *w)
1415{ 1480{
1416 ev_clear_pending (EV_A_ (W)w); 1481 ev_clear_pending (EV_A_ (W)w);
1417 if (ev_is_active (w)) 1482 if (!ev_is_active (w))
1418 return; 1483 return;
1419 1484
1420 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1485 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1421 ev_stop (EV_A_ (W)w); 1486 ev_stop (EV_A_ (W)w);
1422} 1487}
1426{ 1491{
1427 if (ev_is_active (w)) 1492 if (ev_is_active (w))
1428 return; 1493 return;
1429 1494
1430 ev_start (EV_A_ (W)w, ++checkcnt); 1495 ev_start (EV_A_ (W)w, ++checkcnt);
1431 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); 1496 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2);
1432 checks [checkcnt - 1] = w; 1497 checks [checkcnt - 1] = w;
1433} 1498}
1434 1499
1435void 1500void
1436ev_check_stop (EV_P_ struct ev_check *w) 1501ev_check_stop (EV_P_ struct ev_check *w)
1437{ 1502{
1438 ev_clear_pending (EV_A_ (W)w); 1503 ev_clear_pending (EV_A_ (W)w);
1439 if (ev_is_active (w)) 1504 if (!ev_is_active (w))
1440 return; 1505 return;
1441 1506
1442 checks [((W)w)->active - 1] = checks [--checkcnt]; 1507 checks [((W)w)->active - 1] = checks [--checkcnt];
1443 ev_stop (EV_A_ (W)w); 1508 ev_stop (EV_A_ (W)w);
1444} 1509}
1462 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); 1527 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1463 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1528 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1464 1529
1465 if (!((WL)w)->next) 1530 if (!((WL)w)->next)
1466 { 1531 {
1467#if WIN32 1532#if _WIN32
1468 signal (w->signum, sighandler); 1533 signal (w->signum, sighandler);
1469#else 1534#else
1470 struct sigaction sa; 1535 struct sigaction sa;
1471 sa.sa_handler = sighandler; 1536 sa.sa_handler = sighandler;
1472 sigfillset (&sa.sa_mask); 1537 sigfillset (&sa.sa_mask);
1505 1570
1506void 1571void
1507ev_child_stop (EV_P_ struct ev_child *w) 1572ev_child_stop (EV_P_ struct ev_child *w)
1508{ 1573{
1509 ev_clear_pending (EV_A_ (W)w); 1574 ev_clear_pending (EV_A_ (W)w);
1510 if (ev_is_active (w)) 1575 if (!ev_is_active (w))
1511 return; 1576 return;
1512 1577
1513 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1578 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1514 ev_stop (EV_A_ (W)w); 1579 ev_stop (EV_A_ (W)w);
1515} 1580}
1559 else 1624 else
1560 { 1625 {
1561 once->cb = cb; 1626 once->cb = cb;
1562 once->arg = arg; 1627 once->arg = arg;
1563 1628
1564 ev_watcher_init (&once->io, once_cb_io); 1629 ev_init (&once->io, once_cb_io);
1565 if (fd >= 0) 1630 if (fd >= 0)
1566 { 1631 {
1567 ev_io_set (&once->io, fd, events); 1632 ev_io_set (&once->io, fd, events);
1568 ev_io_start (EV_A_ &once->io); 1633 ev_io_start (EV_A_ &once->io);
1569 } 1634 }
1570 1635
1571 ev_watcher_init (&once->to, once_cb_to); 1636 ev_init (&once->to, once_cb_to);
1572 if (timeout >= 0.) 1637 if (timeout >= 0.)
1573 { 1638 {
1574 ev_timer_set (&once->to, timeout, 0.); 1639 ev_timer_set (&once->to, timeout, 0.);
1575 ev_timer_start (EV_A_ &once->to); 1640 ev_timer_start (EV_A_ &once->to);
1576 } 1641 }
1577 } 1642 }
1578} 1643}
1579 1644
1645#ifdef __cplusplus
1646}
1647#endif
1648

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines