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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines