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

Comparing libev/ev.c (file contents):
Revision 1.35 by root, Thu Nov 1 11:55:54 2007 UTC vs.
Revision 1.53 by root, Sat Nov 3 22:31:11 2007 UTC

1/* 1/*
2 * libev event processing core, watcher management
3 *
2 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
3 * All rights reserved. 5 * All rights reserved.
4 * 6 *
5 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are 8 * modification, are permitted provided that the following conditions are
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (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
27 * 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.
28 */ 30 */
29#if EV_USE_CONFIG_H 31#ifndef EV_STANDALONE
30# include "config.h" 32# include "config.h"
31#endif 33#endif
32 34
33#include <math.h> 35#include <math.h>
34#include <stdlib.h> 36#include <stdlib.h>
40#include <stdio.h> 42#include <stdio.h>
41 43
42#include <assert.h> 44#include <assert.h>
43#include <errno.h> 45#include <errno.h>
44#include <sys/types.h> 46#include <sys/types.h>
47#ifndef WIN32
45#include <sys/wait.h> 48# include <sys/wait.h>
49#endif
46#include <sys/time.h> 50#include <sys/time.h>
47#include <time.h> 51#include <time.h>
48 52
53/**/
54
49#ifndef EV_USE_MONOTONIC 55#ifndef EV_USE_MONOTONIC
50# ifdef CLOCK_MONOTONIC
51# define EV_USE_MONOTONIC 1 56# define EV_USE_MONOTONIC 1
52# endif
53#endif 57#endif
54 58
55#ifndef EV_USE_SELECT 59#ifndef EV_USE_SELECT
56# define EV_USE_SELECT 1 60# define EV_USE_SELECT 1
57#endif 61#endif
58 62
63#ifndef EV_USEV_POLL
64# define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */
65#endif
66
59#ifndef EV_USE_EPOLL 67#ifndef EV_USE_EPOLL
60# define EV_USE_EPOLL 0 68# define EV_USE_EPOLL 0
61#endif 69#endif
62 70
71#ifndef EV_USE_KQUEUE
72# define EV_USE_KQUEUE 0
73#endif
74
75#ifndef EV_USE_REALTIME
76# define EV_USE_REALTIME 1
77#endif
78
79/**/
80
81#ifndef CLOCK_MONOTONIC
82# undef EV_USE_MONOTONIC
83# define EV_USE_MONOTONIC 0
84#endif
85
63#ifndef CLOCK_REALTIME 86#ifndef CLOCK_REALTIME
87# undef EV_USE_REALTIME
64# define EV_USE_REALTIME 0 88# define EV_USE_REALTIME 0
65#endif 89#endif
66#ifndef EV_USE_REALTIME 90
67# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ 91/**/
68#endif
69 92
70#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 93#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
71#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */ 94#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
72#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ 95#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
73#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ 96/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
74 97
75#include "ev.h" 98#include "ev.h"
99
100#if __GNUC__ >= 3
101# define expect(expr,value) __builtin_expect ((expr),(value))
102# define inline inline
103#else
104# define expect(expr,value) (expr)
105# define inline static
106#endif
107
108#define expect_false(expr) expect ((expr) != 0, 0)
109#define expect_true(expr) expect ((expr) != 0, 1)
110
111#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
112#define ABSPRI(w) ((w)->priority - EV_MINPRI)
76 113
77typedef struct ev_watcher *W; 114typedef struct ev_watcher *W;
78typedef struct ev_watcher_list *WL; 115typedef struct ev_watcher_list *WL;
79typedef struct ev_watcher_time *WT; 116typedef struct ev_watcher_time *WT;
80 117
81static ev_tstamp now, diff; /* monotonic clock */
82ev_tstamp ev_now;
83int ev_method;
84
85static int have_monotonic; /* runtime */
86
87static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
88static void (*method_modify)(int fd, int oev, int nev);
89static void (*method_poll)(ev_tstamp timeout);
90
91/*****************************************************************************/ 118/*****************************************************************************/
92 119
93ev_tstamp 120typedef struct
121{
122 struct ev_watcher_list *head;
123 unsigned char events;
124 unsigned char reify;
125} ANFD;
126
127typedef struct
128{
129 W w;
130 int events;
131} ANPENDING;
132
133#ifdef EV_MULTIPLICITY
134struct ev_loop
135{
136# define VAR(name,decl) decl
137# include "ev_vars.h"
138};
139#else
140# define VAR(name,decl) static decl
141# include "ev_vars.h"
142#endif
143#undef VAR
144
145/*****************************************************************************/
146
147inline ev_tstamp
94ev_time (void) 148ev_time (void)
95{ 149{
96#if EV_USE_REALTIME 150#if EV_USE_REALTIME
97 struct timespec ts; 151 struct timespec ts;
98 clock_gettime (CLOCK_REALTIME, &ts); 152 clock_gettime (CLOCK_REALTIME, &ts);
102 gettimeofday (&tv, 0); 156 gettimeofday (&tv, 0);
103 return tv.tv_sec + tv.tv_usec * 1e-6; 157 return tv.tv_sec + tv.tv_usec * 1e-6;
104#endif 158#endif
105} 159}
106 160
107static ev_tstamp 161inline ev_tstamp
108get_clock (void) 162get_clock (void)
109{ 163{
110#if EV_USE_MONOTONIC 164#if EV_USE_MONOTONIC
111 if (have_monotonic) 165 if (expect_true (have_monotonic))
112 { 166 {
113 struct timespec ts; 167 struct timespec ts;
114 clock_gettime (CLOCK_MONOTONIC, &ts); 168 clock_gettime (CLOCK_MONOTONIC, &ts);
115 return ts.tv_sec + ts.tv_nsec * 1e-9; 169 return ts.tv_sec + ts.tv_nsec * 1e-9;
116 } 170 }
117#endif 171#endif
118 172
119 return ev_time (); 173 return ev_time ();
120} 174}
121 175
176ev_tstamp
177ev_now (EV_P)
178{
179 return rt_now;
180}
181
122#define array_roundsize(base,n) ((n) | 4 & ~3) 182#define array_roundsize(base,n) ((n) | 4 & ~3)
123 183
124#define array_needsize(base,cur,cnt,init) \ 184#define array_needsize(base,cur,cnt,init) \
125 if ((cnt) > cur) \ 185 if (expect_false ((cnt) > cur)) \
126 { \ 186 { \
127 int newcnt = cur; \ 187 int newcnt = cur; \
128 do \ 188 do \
129 { \ 189 { \
130 newcnt = array_roundsize (base, newcnt << 1); \ 190 newcnt = array_roundsize (base, newcnt << 1); \
136 cur = newcnt; \ 196 cur = newcnt; \
137 } 197 }
138 198
139/*****************************************************************************/ 199/*****************************************************************************/
140 200
141typedef struct
142{
143 struct ev_io *head;
144 unsigned char events;
145 unsigned char reify;
146} ANFD;
147
148static ANFD *anfds;
149static int anfdmax;
150
151static void 201static void
152anfds_init (ANFD *base, int count) 202anfds_init (ANFD *base, int count)
153{ 203{
154 while (count--) 204 while (count--)
155 { 205 {
159 209
160 ++base; 210 ++base;
161 } 211 }
162} 212}
163 213
164typedef struct
165{
166 W w;
167 int events;
168} ANPENDING;
169
170static ANPENDING *pendings;
171static int pendingmax, pendingcnt;
172
173static void 214static void
174event (W w, int events) 215event (EV_P_ W w, int events)
175{ 216{
176 if (w->pending) 217 if (w->pending)
177 { 218 {
178 pendings [w->pending - 1].events |= events; 219 pendings [ABSPRI (w)][w->pending - 1].events |= events;
179 return; 220 return;
180 } 221 }
181 222
182 w->pending = ++pendingcnt; 223 w->pending = ++pendingcnt [ABSPRI (w)];
183 array_needsize (pendings, pendingmax, pendingcnt, ); 224 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
184 pendings [pendingcnt - 1].w = w; 225 pendings [ABSPRI (w)][w->pending - 1].w = w;
185 pendings [pendingcnt - 1].events = events; 226 pendings [ABSPRI (w)][w->pending - 1].events = events;
186} 227}
187 228
188static void 229static void
189queue_events (W *events, int eventcnt, int type) 230queue_events (EV_P_ W *events, int eventcnt, int type)
190{ 231{
191 int i; 232 int i;
192 233
193 for (i = 0; i < eventcnt; ++i) 234 for (i = 0; i < eventcnt; ++i)
194 event (events [i], type); 235 event (EV_A_ events [i], type);
195} 236}
196 237
197static void 238static void
198fd_event (int fd, int events) 239fd_event (EV_P_ int fd, int events)
199{ 240{
200 ANFD *anfd = anfds + fd; 241 ANFD *anfd = anfds + fd;
201 struct ev_io *w; 242 struct ev_io *w;
202 243
203 for (w = anfd->head; w; w = w->next) 244 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
204 { 245 {
205 int ev = w->events & events; 246 int ev = w->events & events;
206 247
207 if (ev) 248 if (ev)
208 event ((W)w, ev); 249 event (EV_A_ (W)w, ev);
209 } 250 }
210} 251}
211 252
212/*****************************************************************************/ 253/*****************************************************************************/
213 254
214static int *fdchanges;
215static int fdchangemax, fdchangecnt;
216
217static void 255static void
218fd_reify (void) 256fd_reify (EV_P)
219{ 257{
220 int i; 258 int i;
221 259
222 for (i = 0; i < fdchangecnt; ++i) 260 for (i = 0; i < fdchangecnt; ++i)
223 { 261 {
225 ANFD *anfd = anfds + fd; 263 ANFD *anfd = anfds + fd;
226 struct ev_io *w; 264 struct ev_io *w;
227 265
228 int events = 0; 266 int events = 0;
229 267
230 for (w = anfd->head; w; w = w->next) 268 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
231 events |= w->events; 269 events |= w->events;
232 270
233 anfd->reify = 0; 271 anfd->reify = 0;
234 272
235 if (anfd->events != events) 273 if (anfd->events != events)
236 { 274 {
237 method_modify (fd, anfd->events, events); 275 method_modify (EV_A_ fd, anfd->events, events);
238 anfd->events = events; 276 anfd->events = events;
239 } 277 }
240 } 278 }
241 279
242 fdchangecnt = 0; 280 fdchangecnt = 0;
243} 281}
244 282
245static void 283static void
246fd_change (int fd) 284fd_change (EV_P_ int fd)
247{ 285{
248 if (anfds [fd].reify || fdchangecnt < 0) 286 if (anfds [fd].reify || fdchangecnt < 0)
249 return; 287 return;
250 288
251 anfds [fd].reify = 1; 289 anfds [fd].reify = 1;
253 ++fdchangecnt; 291 ++fdchangecnt;
254 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 292 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
255 fdchanges [fdchangecnt - 1] = fd; 293 fdchanges [fdchangecnt - 1] = fd;
256} 294}
257 295
296static void
297fd_kill (EV_P_ int fd)
298{
299 struct ev_io *w;
300
301 while ((w = (struct ev_io *)anfds [fd].head))
302 {
303 ev_io_stop (EV_A_ w);
304 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
305 }
306}
307
258/* called on EBADF to verify fds */ 308/* called on EBADF to verify fds */
259static void 309static void
260fd_recheck (void) 310fd_ebadf (EV_P)
261{ 311{
262 int fd; 312 int fd;
263 313
264 for (fd = 0; fd < anfdmax; ++fd) 314 for (fd = 0; fd < anfdmax; ++fd)
265 if (anfds [fd].events) 315 if (anfds [fd].events)
266 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 316 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
267 while (anfds [fd].head) 317 fd_kill (EV_A_ fd);
318}
319
320/* called on ENOMEM in select/poll to kill some fds and retry */
321static void
322fd_enomem (EV_P)
323{
324 int fd = anfdmax;
325
326 while (fd--)
327 if (anfds [fd].events)
268 { 328 {
269 ev_io_stop (anfds [fd].head); 329 close (fd);
270 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); 330 fd_kill (EV_A_ fd);
331 return;
271 } 332 }
272} 333}
273 334
274/*****************************************************************************/ 335/*****************************************************************************/
275
276static struct ev_timer **timers;
277static int timermax, timercnt;
278
279static struct ev_periodic **periodics;
280static int periodicmax, periodiccnt;
281 336
282static void 337static void
283upheap (WT *timers, int k) 338upheap (WT *timers, int k)
284{ 339{
285 WT w = timers [k]; 340 WT w = timers [k];
322 377
323/*****************************************************************************/ 378/*****************************************************************************/
324 379
325typedef struct 380typedef struct
326{ 381{
327 struct ev_signal *head; 382 struct ev_watcher_list *head;
328 sig_atomic_t volatile gotsig; 383 sig_atomic_t volatile gotsig;
329} ANSIG; 384} ANSIG;
330 385
331static ANSIG *signals; 386static ANSIG *signals;
332static int signalmax; 387static int signalmax;
333 388
334static int sigpipe [2]; 389static int sigpipe [2];
335static sig_atomic_t volatile gotsig; 390static sig_atomic_t volatile gotsig;
336static struct ev_io sigev;
337 391
338static void 392static void
339signals_init (ANSIG *base, int count) 393signals_init (ANSIG *base, int count)
340{ 394{
341 while (count--) 395 while (count--)
352{ 406{
353 signals [signum - 1].gotsig = 1; 407 signals [signum - 1].gotsig = 1;
354 408
355 if (!gotsig) 409 if (!gotsig)
356 { 410 {
411 int old_errno = errno;
357 gotsig = 1; 412 gotsig = 1;
358 write (sigpipe [1], &signum, 1); 413 write (sigpipe [1], &signum, 1);
414 errno = old_errno;
359 } 415 }
360} 416}
361 417
362static void 418static void
363sigcb (struct ev_io *iow, int revents) 419sigcb (EV_P_ struct ev_io *iow, int revents)
364{ 420{
365 struct ev_signal *w; 421 struct ev_watcher_list *w;
366 int sig; 422 int signum;
367 423
368 read (sigpipe [0], &revents, 1); 424 read (sigpipe [0], &revents, 1);
369 gotsig = 0; 425 gotsig = 0;
370 426
371 for (sig = signalmax; sig--; ) 427 for (signum = signalmax; signum--; )
372 if (signals [sig].gotsig) 428 if (signals [signum].gotsig)
373 { 429 {
374 signals [sig].gotsig = 0; 430 signals [signum].gotsig = 0;
375 431
376 for (w = signals [sig].head; w; w = w->next) 432 for (w = signals [signum].head; w; w = w->next)
377 event ((W)w, EV_SIGNAL); 433 event (EV_A_ (W)w, EV_SIGNAL);
378 } 434 }
379} 435}
380 436
381static void 437static void
382siginit (void) 438siginit (EV_P)
383{ 439{
440#ifndef WIN32
384 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 441 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
385 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); 442 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
386 443
387 /* rather than sort out wether we really need nb, set it */ 444 /* rather than sort out wether we really need nb, set it */
388 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 445 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
389 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 446 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
447#endif
390 448
391 ev_io_set (&sigev, sigpipe [0], EV_READ); 449 ev_io_set (&sigev, sigpipe [0], EV_READ);
392 ev_io_start (&sigev); 450 ev_io_start (&sigev);
451 ev_unref (EV_A); /* child watcher should not keep loop alive */
393} 452}
394 453
395/*****************************************************************************/ 454/*****************************************************************************/
396 455
397static struct ev_idle **idles; 456#ifndef WIN32
398static int idlemax, idlecnt;
399
400static struct ev_prepare **prepares;
401static int preparemax, preparecnt;
402
403static struct ev_check **checks;
404static int checkmax, checkcnt;
405
406/*****************************************************************************/
407
408static struct ev_child *childs [PID_HASHSIZE];
409static struct ev_signal childev;
410 457
411#ifndef WCONTINUED 458#ifndef WCONTINUED
412# define WCONTINUED 0 459# define WCONTINUED 0
413#endif 460#endif
414 461
415static void 462static void
416childcb (struct ev_signal *sw, int revents) 463child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status)
417{ 464{
418 struct ev_child *w; 465 struct ev_child *w;
466
467 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
468 if (w->pid == pid || !w->pid)
469 {
470 w->priority = sw->priority; /* need to do it *now* */
471 w->rpid = pid;
472 w->rstatus = status;
473 event (EV_A_ (W)w, EV_CHILD);
474 }
475}
476
477static void
478childcb (EV_P_ struct ev_signal *sw, int revents)
479{
419 int pid, status; 480 int pid, status;
420 481
421 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 482 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
422 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 483 {
423 if (w->pid == pid || w->pid == -1) 484 /* make sure we are called again until all childs have been reaped */
424 { 485 event (EV_A_ (W)sw, EV_SIGNAL);
425 w->status = status; 486
426 event ((W)w, EV_CHILD); 487 child_reap (EV_A_ sw, pid, pid, status);
427 } 488 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
489 }
428} 490}
491
492#endif
429 493
430/*****************************************************************************/ 494/*****************************************************************************/
431 495
496#if EV_USE_KQUEUE
497# include "ev_kqueue.c"
498#endif
432#if EV_USE_EPOLL 499#if EV_USE_EPOLL
433# include "ev_epoll.c" 500# include "ev_epoll.c"
434#endif 501#endif
502#if EV_USEV_POLL
503# include "ev_poll.c"
504#endif
435#if EV_USE_SELECT 505#if EV_USE_SELECT
436# include "ev_select.c" 506# include "ev_select.c"
437#endif 507#endif
438 508
439int 509int
446ev_version_minor (void) 516ev_version_minor (void)
447{ 517{
448 return EV_VERSION_MINOR; 518 return EV_VERSION_MINOR;
449} 519}
450 520
451int ev_init (int flags) 521/* return true if we are running with elevated privileges and should ignore env variables */
522static int
523enable_secure (void)
452{ 524{
525#ifdef WIN32
526 return 0;
527#else
528 return getuid () != geteuid ()
529 || getgid () != getegid ();
530#endif
531}
532
533int
534ev_method (EV_P)
535{
536 return method;
537}
538
539int
540ev_init (EV_P_ int methods)
541{
542#ifdef EV_MULTIPLICITY
543 memset (loop, 0, sizeof (struct ev_loop));
544#endif
545
453 if (!ev_method) 546 if (!method)
454 { 547 {
455#if EV_USE_MONOTONIC 548#if EV_USE_MONOTONIC
456 { 549 {
457 struct timespec ts; 550 struct timespec ts;
458 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 551 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
459 have_monotonic = 1; 552 have_monotonic = 1;
460 } 553 }
461#endif 554#endif
462 555
463 ev_now = ev_time (); 556 rt_now = ev_time ();
464 now = get_clock (); 557 mn_now = get_clock ();
558 now_floor = mn_now;
465 diff = ev_now - now; 559 diff = rt_now - mn_now;
466 560
467 if (pipe (sigpipe)) 561 if (pipe (sigpipe))
468 return 0; 562 return 0;
469 563
564 if (methods == EVMETHOD_AUTO)
565 if (!enable_secure () && getenv ("LIBmethodS"))
566 methods = atoi (getenv ("LIBmethodS"));
567 else
470 ev_method = EVMETHOD_NONE; 568 methods = EVMETHOD_ANY;
569
570 method = 0;
571#if EV_USE_KQUEUE
572 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
573#endif
471#if EV_USE_EPOLL 574#if EV_USE_EPOLL
472 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 575 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
576#endif
577#if EV_USEV_POLL
578 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
473#endif 579#endif
474#if EV_USE_SELECT 580#if EV_USE_SELECT
475 if (ev_method == EVMETHOD_NONE) select_init (flags); 581 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
476#endif 582#endif
477 583
478 if (ev_method) 584 if (method)
479 { 585 {
480 ev_watcher_init (&sigev, sigcb); 586 ev_watcher_init (&sigev, sigcb);
587 ev_set_priority (&sigev, EV_MAXPRI);
481 siginit (); 588 siginit (EV_A);
482 589
590#ifndef WIN32
483 ev_signal_init (&childev, childcb, SIGCHLD); 591 ev_signal_init (&childev, childcb, SIGCHLD);
592 ev_set_priority (&childev, EV_MAXPRI);
484 ev_signal_start (&childev); 593 ev_signal_start (EV_A_ &childev);
594 ev_unref (EV_A); /* child watcher should not keep loop alive */
595#endif
485 } 596 }
486 } 597 }
487 598
488 return ev_method; 599 return method;
489} 600}
490 601
491/*****************************************************************************/ 602/*****************************************************************************/
492 603
493void 604void
504 615
505void 616void
506ev_fork_child (void) 617ev_fork_child (void)
507{ 618{
508#if EV_USE_EPOLL 619#if EV_USE_EPOLL
509 if (ev_method == EVMETHOD_EPOLL) 620 if (method == EVMETHOD_EPOLL)
510 epoll_postfork_child (); 621 epoll_postfork_child ();
511#endif 622#endif
512 623
513 ev_io_stop (&sigev); 624 ev_io_stop (&sigev);
514 close (sigpipe [0]); 625 close (sigpipe [0]);
518} 629}
519 630
520/*****************************************************************************/ 631/*****************************************************************************/
521 632
522static void 633static void
523call_pending (void) 634call_pending (EV_P)
524{ 635{
636 int pri;
637
638 for (pri = NUMPRI; pri--; )
525 while (pendingcnt) 639 while (pendingcnt [pri])
526 { 640 {
527 ANPENDING *p = pendings + --pendingcnt; 641 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
528 642
529 if (p->w) 643 if (p->w)
530 { 644 {
531 p->w->pending = 0; 645 p->w->pending = 0;
532 p->w->cb (p->w, p->events); 646 p->w->cb (EV_A_ p->w, p->events);
533 } 647 }
534 } 648 }
535} 649}
536 650
537static void 651static void
538timers_reify (void) 652timers_reify (EV_P)
539{ 653{
540 while (timercnt && timers [0]->at <= now) 654 while (timercnt && timers [0]->at <= mn_now)
541 { 655 {
542 struct ev_timer *w = timers [0]; 656 struct ev_timer *w = timers [0];
543 657
544 /* first reschedule or stop timer */ 658 /* first reschedule or stop timer */
545 if (w->repeat) 659 if (w->repeat)
546 { 660 {
547 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 661 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
548 w->at = now + w->repeat; 662 w->at = mn_now + w->repeat;
549 downheap ((WT *)timers, timercnt, 0); 663 downheap ((WT *)timers, timercnt, 0);
550 } 664 }
551 else 665 else
552 ev_timer_stop (w); /* nonrepeating: stop timer */ 666 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
553 667
554 event ((W)w, EV_TIMEOUT); 668 event ((W)w, EV_TIMEOUT);
555 } 669 }
556} 670}
557 671
558static void 672static void
559periodics_reify (void) 673periodics_reify (EV_P)
560{ 674{
561 while (periodiccnt && periodics [0]->at <= ev_now) 675 while (periodiccnt && periodics [0]->at <= rt_now)
562 { 676 {
563 struct ev_periodic *w = periodics [0]; 677 struct ev_periodic *w = periodics [0];
564 678
565 /* first reschedule or stop timer */ 679 /* first reschedule or stop timer */
566 if (w->interval) 680 if (w->interval)
567 { 681 {
568 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 682 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval;
569 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); 683 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now));
570 downheap ((WT *)periodics, periodiccnt, 0); 684 downheap ((WT *)periodics, periodiccnt, 0);
571 } 685 }
572 else 686 else
573 ev_periodic_stop (w); /* nonrepeating: stop timer */ 687 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
574 688
575 event ((W)w, EV_PERIODIC); 689 event (EV_A_ (W)w, EV_PERIODIC);
576 } 690 }
577} 691}
578 692
579static void 693static void
580periodics_reschedule (ev_tstamp diff) 694periodics_reschedule (EV_P_ ev_tstamp diff)
581{ 695{
582 int i; 696 int i;
583 697
584 /* adjust periodics after time jump */ 698 /* adjust periodics after time jump */
585 for (i = 0; i < periodiccnt; ++i) 699 for (i = 0; i < periodiccnt; ++i)
586 { 700 {
587 struct ev_periodic *w = periodics [i]; 701 struct ev_periodic *w = periodics [i];
588 702
589 if (w->interval) 703 if (w->interval)
590 { 704 {
591 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; 705 ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval;
592 706
593 if (fabs (diff) >= 1e-4) 707 if (fabs (diff) >= 1e-4)
594 { 708 {
595 ev_periodic_stop (w); 709 ev_periodic_stop (EV_A_ w);
596 ev_periodic_start (w); 710 ev_periodic_start (EV_A_ w);
597 711
598 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 712 i = 0; /* restart loop, inefficient, but time jumps should be rare */
599 } 713 }
600 } 714 }
601 } 715 }
602} 716}
603 717
718inline int
719time_update_monotonic (EV_P)
720{
721 mn_now = get_clock ();
722
723 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
724 {
725 rt_now = mn_now + diff;
726 return 0;
727 }
728 else
729 {
730 now_floor = mn_now;
731 rt_now = ev_time ();
732 return 1;
733 }
734}
735
604static void 736static void
605time_update (void) 737time_update (EV_P)
606{ 738{
607 int i; 739 int i;
608 740
609 ev_now = ev_time (); 741#if EV_USE_MONOTONIC
610
611 if (have_monotonic) 742 if (expect_true (have_monotonic))
612 { 743 {
613 ev_tstamp odiff = diff; 744 if (time_update_monotonic (EV_A))
614
615 for (i = 4; --i; ) /* loop a few times, before making important decisions */
616 { 745 {
617 now = get_clock (); 746 ev_tstamp odiff = diff;
747
748 for (i = 4; --i; ) /* loop a few times, before making important decisions */
749 {
618 diff = ev_now - now; 750 diff = rt_now - mn_now;
619 751
620 if (fabs (odiff - diff) < MIN_TIMEJUMP) 752 if (fabs (odiff - diff) < MIN_TIMEJUMP)
621 return; /* all is well */ 753 return; /* all is well */
622 754
623 ev_now = ev_time (); 755 rt_now = ev_time ();
756 mn_now = get_clock ();
757 now_floor = mn_now;
758 }
759
760 periodics_reschedule (EV_A_ diff - odiff);
761 /* no timer adjustment, as the monotonic clock doesn't jump */
624 } 762 }
625
626 periodics_reschedule (diff - odiff);
627 /* no timer adjustment, as the monotonic clock doesn't jump */
628 } 763 }
629 else 764 else
765#endif
630 { 766 {
631 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 767 rt_now = ev_time ();
768
769 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
632 { 770 {
633 periodics_reschedule (ev_now - now); 771 periodics_reschedule (EV_A_ rt_now - mn_now);
634 772
635 /* adjust timers. this is easy, as the offset is the same for all */ 773 /* adjust timers. this is easy, as the offset is the same for all */
636 for (i = 0; i < timercnt; ++i) 774 for (i = 0; i < timercnt; ++i)
637 timers [i]->at += diff; 775 timers [i]->at += diff;
638 } 776 }
639 777
640 now = ev_now; 778 mn_now = rt_now;
641 } 779 }
642} 780}
643 781
644int ev_loop_done; 782void
783ev_ref (EV_P)
784{
785 ++activecnt;
786}
645 787
788void
789ev_unref (EV_P)
790{
791 --activecnt;
792}
793
794static int loop_done;
795
796void
646void ev_loop (int flags) 797ev_loop (EV_P_ int flags)
647{ 798{
648 double block; 799 double block;
649 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 800 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
650 801
651 do 802 do
652 { 803 {
653 /* queue check watchers (and execute them) */ 804 /* queue check watchers (and execute them) */
654 if (preparecnt) 805 if (expect_false (preparecnt))
655 { 806 {
656 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 807 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
657 call_pending (); 808 call_pending (EV_A);
658 } 809 }
659 810
660 /* update fd-related kernel structures */ 811 /* update fd-related kernel structures */
661 fd_reify (); 812 fd_reify (EV_A);
662 813
663 /* calculate blocking time */ 814 /* calculate blocking time */
664 815
665 /* we only need this for !monotonic clockor timers, but as we basically 816 /* we only need this for !monotonic clockor timers, but as we basically
666 always have timers, we just calculate it always */ 817 always have timers, we just calculate it always */
818#if EV_USE_MONOTONIC
819 if (expect_true (have_monotonic))
820 time_update_monotonic (EV_A);
821 else
822#endif
823 {
667 ev_now = ev_time (); 824 rt_now = ev_time ();
825 mn_now = rt_now;
826 }
668 827
669 if (flags & EVLOOP_NONBLOCK || idlecnt) 828 if (flags & EVLOOP_NONBLOCK || idlecnt)
670 block = 0.; 829 block = 0.;
671 else 830 else
672 { 831 {
673 block = MAX_BLOCKTIME; 832 block = MAX_BLOCKTIME;
674 833
675 if (timercnt) 834 if (timercnt)
676 { 835 {
677 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 836 ev_tstamp to = timers [0]->at - mn_now + method_fudge;
678 if (block > to) block = to; 837 if (block > to) block = to;
679 } 838 }
680 839
681 if (periodiccnt) 840 if (periodiccnt)
682 { 841 {
683 ev_tstamp to = periodics [0]->at - ev_now + method_fudge; 842 ev_tstamp to = periodics [0]->at - rt_now + method_fudge;
684 if (block > to) block = to; 843 if (block > to) block = to;
685 } 844 }
686 845
687 if (block < 0.) block = 0.; 846 if (block < 0.) block = 0.;
688 } 847 }
689 848
690 method_poll (block); 849 method_poll (EV_A_ block);
691 850
692 /* update ev_now, do magic */ 851 /* update rt_now, do magic */
693 time_update (); 852 time_update (EV_A);
694 853
695 /* queue pending timers and reschedule them */ 854 /* queue pending timers and reschedule them */
696 timers_reify (); /* relative timers called last */ 855 timers_reify (EV_A); /* relative timers called last */
697 periodics_reify (); /* absolute timers called first */ 856 periodics_reify (EV_A); /* absolute timers called first */
698 857
699 /* queue idle watchers unless io or timers are pending */ 858 /* queue idle watchers unless io or timers are pending */
700 if (!pendingcnt) 859 if (!pendingcnt)
701 queue_events ((W *)idles, idlecnt, EV_IDLE); 860 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
702 861
703 /* queue check watchers, to be executed first */ 862 /* queue check watchers, to be executed first */
704 if (checkcnt) 863 if (checkcnt)
705 queue_events ((W *)checks, checkcnt, EV_CHECK); 864 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
706 865
707 call_pending (); 866 call_pending (EV_A);
708 } 867 }
709 while (!ev_loop_done); 868 while (activecnt && !loop_done);
710 869
711 if (ev_loop_done != 2) 870 if (loop_done != 2)
712 ev_loop_done = 0; 871 loop_done = 0;
872}
873
874void
875ev_unloop (EV_P_ int how)
876{
877 loop_done = how;
713} 878}
714 879
715/*****************************************************************************/ 880/*****************************************************************************/
716 881
717static void 882inline void
718wlist_add (WL *head, WL elem) 883wlist_add (WL *head, WL elem)
719{ 884{
720 elem->next = *head; 885 elem->next = *head;
721 *head = elem; 886 *head = elem;
722} 887}
723 888
724static void 889inline void
725wlist_del (WL *head, WL elem) 890wlist_del (WL *head, WL elem)
726{ 891{
727 while (*head) 892 while (*head)
728 { 893 {
729 if (*head == elem) 894 if (*head == elem)
734 899
735 head = &(*head)->next; 900 head = &(*head)->next;
736 } 901 }
737} 902}
738 903
739static void 904inline void
740ev_clear_pending (W w) 905ev_clear_pending (EV_P_ W w)
741{ 906{
742 if (w->pending) 907 if (w->pending)
743 { 908 {
744 pendings [w->pending - 1].w = 0; 909 pendings [ABSPRI (w)][w->pending - 1].w = 0;
745 w->pending = 0; 910 w->pending = 0;
746 } 911 }
747} 912}
748 913
749static void 914inline void
750ev_start (W w, int active) 915ev_start (EV_P_ W w, int active)
751{ 916{
917 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
918 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
919
752 w->active = active; 920 w->active = active;
921 ev_ref (EV_A);
753} 922}
754 923
755static void 924inline void
756ev_stop (W w) 925ev_stop (EV_P_ W w)
757{ 926{
927 ev_unref (EV_A);
758 w->active = 0; 928 w->active = 0;
759} 929}
760 930
761/*****************************************************************************/ 931/*****************************************************************************/
762 932
763void 933void
764ev_io_start (struct ev_io *w) 934ev_io_start (EV_P_ struct ev_io *w)
765{ 935{
936 int fd = w->fd;
937
766 if (ev_is_active (w)) 938 if (ev_is_active (w))
767 return; 939 return;
768 940
769 int fd = w->fd;
770
771 assert (("ev_io_start called with negative fd", fd >= 0)); 941 assert (("ev_io_start called with negative fd", fd >= 0));
772 942
773 ev_start ((W)w, 1); 943 ev_start (EV_A_ (W)w, 1);
774 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 944 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
775 wlist_add ((WL *)&anfds[fd].head, (WL)w); 945 wlist_add ((WL *)&anfds[fd].head, (WL)w);
776 946
777 fd_change (fd); 947 fd_change (EV_A_ fd);
778} 948}
779 949
780void 950void
781ev_io_stop (struct ev_io *w) 951ev_io_stop (EV_P_ struct ev_io *w)
782{ 952{
783 ev_clear_pending ((W)w); 953 ev_clear_pending (EV_A_ (W)w);
784 if (!ev_is_active (w)) 954 if (!ev_is_active (w))
785 return; 955 return;
786 956
787 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 957 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
788 ev_stop ((W)w); 958 ev_stop (EV_A_ (W)w);
789 959
790 fd_change (w->fd); 960 fd_change (EV_A_ w->fd);
791} 961}
792 962
793void 963void
794ev_timer_start (struct ev_timer *w) 964ev_timer_start (EV_P_ struct ev_timer *w)
795{ 965{
796 if (ev_is_active (w)) 966 if (ev_is_active (w))
797 return; 967 return;
798 968
799 w->at += now; 969 w->at += mn_now;
800 970
801 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 971 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
802 972
803 ev_start ((W)w, ++timercnt); 973 ev_start (EV_A_ (W)w, ++timercnt);
804 array_needsize (timers, timermax, timercnt, ); 974 array_needsize (timers, timermax, timercnt, );
805 timers [timercnt - 1] = w; 975 timers [timercnt - 1] = w;
806 upheap ((WT *)timers, timercnt - 1); 976 upheap ((WT *)timers, timercnt - 1);
807} 977}
808 978
809void 979void
810ev_timer_stop (struct ev_timer *w) 980ev_timer_stop (EV_P_ struct ev_timer *w)
811{ 981{
812 ev_clear_pending ((W)w); 982 ev_clear_pending (EV_A_ (W)w);
813 if (!ev_is_active (w)) 983 if (!ev_is_active (w))
814 return; 984 return;
815 985
816 if (w->active < timercnt--) 986 if (w->active < timercnt--)
817 { 987 {
819 downheap ((WT *)timers, timercnt, w->active - 1); 989 downheap ((WT *)timers, timercnt, w->active - 1);
820 } 990 }
821 991
822 w->at = w->repeat; 992 w->at = w->repeat;
823 993
824 ev_stop ((W)w); 994 ev_stop (EV_A_ (W)w);
825} 995}
826 996
827void 997void
828ev_timer_again (struct ev_timer *w) 998ev_timer_again (EV_P_ struct ev_timer *w)
829{ 999{
830 if (ev_is_active (w)) 1000 if (ev_is_active (w))
831 { 1001 {
832 if (w->repeat) 1002 if (w->repeat)
833 { 1003 {
834 w->at = now + w->repeat; 1004 w->at = mn_now + w->repeat;
835 downheap ((WT *)timers, timercnt, w->active - 1); 1005 downheap ((WT *)timers, timercnt, w->active - 1);
836 } 1006 }
837 else 1007 else
838 ev_timer_stop (w); 1008 ev_timer_stop (EV_A_ w);
839 } 1009 }
840 else if (w->repeat) 1010 else if (w->repeat)
841 ev_timer_start (w); 1011 ev_timer_start (EV_A_ w);
842} 1012}
843 1013
844void 1014void
845ev_periodic_start (struct ev_periodic *w) 1015ev_periodic_start (EV_P_ struct ev_periodic *w)
846{ 1016{
847 if (ev_is_active (w)) 1017 if (ev_is_active (w))
848 return; 1018 return;
849 1019
850 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1020 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
851 1021
852 /* this formula differs from the one in periodic_reify because we do not always round up */ 1022 /* this formula differs from the one in periodic_reify because we do not always round up */
853 if (w->interval) 1023 if (w->interval)
854 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 1024 w->at += ceil ((rt_now - w->at) / w->interval) * w->interval;
855 1025
856 ev_start ((W)w, ++periodiccnt); 1026 ev_start (EV_A_ (W)w, ++periodiccnt);
857 array_needsize (periodics, periodicmax, periodiccnt, ); 1027 array_needsize (periodics, periodicmax, periodiccnt, );
858 periodics [periodiccnt - 1] = w; 1028 periodics [periodiccnt - 1] = w;
859 upheap ((WT *)periodics, periodiccnt - 1); 1029 upheap ((WT *)periodics, periodiccnt - 1);
860} 1030}
861 1031
862void 1032void
863ev_periodic_stop (struct ev_periodic *w) 1033ev_periodic_stop (EV_P_ struct ev_periodic *w)
864{ 1034{
865 ev_clear_pending ((W)w); 1035 ev_clear_pending (EV_A_ (W)w);
866 if (!ev_is_active (w)) 1036 if (!ev_is_active (w))
867 return; 1037 return;
868 1038
869 if (w->active < periodiccnt--) 1039 if (w->active < periodiccnt--)
870 { 1040 {
871 periodics [w->active - 1] = periodics [periodiccnt]; 1041 periodics [w->active - 1] = periodics [periodiccnt];
872 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1042 downheap ((WT *)periodics, periodiccnt, w->active - 1);
873 } 1043 }
874 1044
875 ev_stop ((W)w); 1045 ev_stop (EV_A_ (W)w);
876} 1046}
877 1047
1048#ifndef SA_RESTART
1049# define SA_RESTART 0
1050#endif
1051
878void 1052void
879ev_signal_start (struct ev_signal *w) 1053ev_signal_start (EV_P_ struct ev_signal *w)
880{ 1054{
881 if (ev_is_active (w)) 1055 if (ev_is_active (w))
882 return; 1056 return;
883 1057
884 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1058 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
885 1059
886 ev_start ((W)w, 1); 1060 ev_start (EV_A_ (W)w, 1);
887 array_needsize (signals, signalmax, w->signum, signals_init); 1061 array_needsize (signals, signalmax, w->signum, signals_init);
888 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1062 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
889 1063
890 if (!w->next) 1064 if (!w->next)
891 { 1065 {
892 struct sigaction sa; 1066 struct sigaction sa;
893 sa.sa_handler = sighandler; 1067 sa.sa_handler = sighandler;
894 sigfillset (&sa.sa_mask); 1068 sigfillset (&sa.sa_mask);
895 sa.sa_flags = 0; 1069 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
896 sigaction (w->signum, &sa, 0); 1070 sigaction (w->signum, &sa, 0);
897 } 1071 }
898} 1072}
899 1073
900void 1074void
901ev_signal_stop (struct ev_signal *w) 1075ev_signal_stop (EV_P_ struct ev_signal *w)
902{ 1076{
903 ev_clear_pending ((W)w); 1077 ev_clear_pending (EV_A_ (W)w);
904 if (!ev_is_active (w)) 1078 if (!ev_is_active (w))
905 return; 1079 return;
906 1080
907 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1081 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
908 ev_stop ((W)w); 1082 ev_stop (EV_A_ (W)w);
909 1083
910 if (!signals [w->signum - 1].head) 1084 if (!signals [w->signum - 1].head)
911 signal (w->signum, SIG_DFL); 1085 signal (w->signum, SIG_DFL);
912} 1086}
913 1087
914void 1088void
915ev_idle_start (struct ev_idle *w) 1089ev_idle_start (EV_P_ struct ev_idle *w)
916{ 1090{
917 if (ev_is_active (w)) 1091 if (ev_is_active (w))
918 return; 1092 return;
919 1093
920 ev_start ((W)w, ++idlecnt); 1094 ev_start (EV_A_ (W)w, ++idlecnt);
921 array_needsize (idles, idlemax, idlecnt, ); 1095 array_needsize (idles, idlemax, idlecnt, );
922 idles [idlecnt - 1] = w; 1096 idles [idlecnt - 1] = w;
923} 1097}
924 1098
925void 1099void
926ev_idle_stop (struct ev_idle *w) 1100ev_idle_stop (EV_P_ struct ev_idle *w)
927{ 1101{
928 ev_clear_pending ((W)w); 1102 ev_clear_pending (EV_A_ (W)w);
929 if (ev_is_active (w)) 1103 if (ev_is_active (w))
930 return; 1104 return;
931 1105
932 idles [w->active - 1] = idles [--idlecnt]; 1106 idles [w->active - 1] = idles [--idlecnt];
933 ev_stop ((W)w); 1107 ev_stop (EV_A_ (W)w);
934} 1108}
935 1109
936void 1110void
937ev_prepare_start (struct ev_prepare *w) 1111ev_prepare_start (EV_P_ struct ev_prepare *w)
938{ 1112{
939 if (ev_is_active (w)) 1113 if (ev_is_active (w))
940 return; 1114 return;
941 1115
942 ev_start ((W)w, ++preparecnt); 1116 ev_start (EV_A_ (W)w, ++preparecnt);
943 array_needsize (prepares, preparemax, preparecnt, ); 1117 array_needsize (prepares, preparemax, preparecnt, );
944 prepares [preparecnt - 1] = w; 1118 prepares [preparecnt - 1] = w;
945} 1119}
946 1120
947void 1121void
948ev_prepare_stop (struct ev_prepare *w) 1122ev_prepare_stop (EV_P_ struct ev_prepare *w)
949{ 1123{
950 ev_clear_pending ((W)w); 1124 ev_clear_pending (EV_A_ (W)w);
951 if (ev_is_active (w)) 1125 if (ev_is_active (w))
952 return; 1126 return;
953 1127
954 prepares [w->active - 1] = prepares [--preparecnt]; 1128 prepares [w->active - 1] = prepares [--preparecnt];
955 ev_stop ((W)w); 1129 ev_stop (EV_A_ (W)w);
956} 1130}
957 1131
958void 1132void
959ev_check_start (struct ev_check *w) 1133ev_check_start (EV_P_ struct ev_check *w)
960{ 1134{
961 if (ev_is_active (w)) 1135 if (ev_is_active (w))
962 return; 1136 return;
963 1137
964 ev_start ((W)w, ++checkcnt); 1138 ev_start (EV_A_ (W)w, ++checkcnt);
965 array_needsize (checks, checkmax, checkcnt, ); 1139 array_needsize (checks, checkmax, checkcnt, );
966 checks [checkcnt - 1] = w; 1140 checks [checkcnt - 1] = w;
967} 1141}
968 1142
969void 1143void
970ev_check_stop (struct ev_check *w) 1144ev_check_stop (EV_P_ struct ev_check *w)
971{ 1145{
972 ev_clear_pending ((W)w); 1146 ev_clear_pending (EV_A_ (W)w);
973 if (ev_is_active (w)) 1147 if (ev_is_active (w))
974 return; 1148 return;
975 1149
976 checks [w->active - 1] = checks [--checkcnt]; 1150 checks [w->active - 1] = checks [--checkcnt];
977 ev_stop ((W)w); 1151 ev_stop (EV_A_ (W)w);
978} 1152}
979 1153
980void 1154void
981ev_child_start (struct ev_child *w) 1155ev_child_start (EV_P_ struct ev_child *w)
982{ 1156{
983 if (ev_is_active (w)) 1157 if (ev_is_active (w))
984 return; 1158 return;
985 1159
986 ev_start ((W)w, 1); 1160 ev_start (EV_A_ (W)w, 1);
987 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1161 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
988} 1162}
989 1163
990void 1164void
991ev_child_stop (struct ev_child *w) 1165ev_child_stop (EV_P_ struct ev_child *w)
992{ 1166{
993 ev_clear_pending ((W)w); 1167 ev_clear_pending (EV_A_ (W)w);
994 if (ev_is_active (w)) 1168 if (ev_is_active (w))
995 return; 1169 return;
996 1170
997 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1171 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
998 ev_stop ((W)w); 1172 ev_stop (EV_A_ (W)w);
999} 1173}
1000 1174
1001/*****************************************************************************/ 1175/*****************************************************************************/
1002 1176
1003struct ev_once 1177struct ev_once
1007 void (*cb)(int revents, void *arg); 1181 void (*cb)(int revents, void *arg);
1008 void *arg; 1182 void *arg;
1009}; 1183};
1010 1184
1011static void 1185static void
1012once_cb (struct ev_once *once, int revents) 1186once_cb (EV_P_ struct ev_once *once, int revents)
1013{ 1187{
1014 void (*cb)(int revents, void *arg) = once->cb; 1188 void (*cb)(int revents, void *arg) = once->cb;
1015 void *arg = once->arg; 1189 void *arg = once->arg;
1016 1190
1017 ev_io_stop (&once->io); 1191 ev_io_stop (EV_A_ &once->io);
1018 ev_timer_stop (&once->to); 1192 ev_timer_stop (EV_A_ &once->to);
1019 free (once); 1193 free (once);
1020 1194
1021 cb (revents, arg); 1195 cb (revents, arg);
1022} 1196}
1023 1197
1024static void 1198static void
1025once_cb_io (struct ev_io *w, int revents) 1199once_cb_io (EV_P_ struct ev_io *w, int revents)
1026{ 1200{
1027 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); 1201 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1028} 1202}
1029 1203
1030static void 1204static void
1031once_cb_to (struct ev_timer *w, int revents) 1205once_cb_to (EV_P_ struct ev_timer *w, int revents)
1032{ 1206{
1033 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); 1207 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1034} 1208}
1035 1209
1036void 1210void
1037ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1211ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1038{ 1212{
1039 struct ev_once *once = malloc (sizeof (struct ev_once)); 1213 struct ev_once *once = malloc (sizeof (struct ev_once));
1040 1214
1041 if (!once) 1215 if (!once)
1042 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1216 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1047 1221
1048 ev_watcher_init (&once->io, once_cb_io); 1222 ev_watcher_init (&once->io, once_cb_io);
1049 if (fd >= 0) 1223 if (fd >= 0)
1050 { 1224 {
1051 ev_io_set (&once->io, fd, events); 1225 ev_io_set (&once->io, fd, events);
1052 ev_io_start (&once->io); 1226 ev_io_start (EV_A_ &once->io);
1053 } 1227 }
1054 1228
1055 ev_watcher_init (&once->to, once_cb_to); 1229 ev_watcher_init (&once->to, once_cb_to);
1056 if (timeout >= 0.) 1230 if (timeout >= 0.)
1057 { 1231 {
1058 ev_timer_set (&once->to, timeout, 0.); 1232 ev_timer_set (&once->to, timeout, 0.);
1059 ev_timer_start (&once->to); 1233 ev_timer_start (EV_A_ &once->to);
1060 } 1234 }
1061 } 1235 }
1062} 1236}
1063 1237
1064/*****************************************************************************/ 1238/*****************************************************************************/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines