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

Comparing libev/ev.c (file contents):
Revision 1.50 by root, Sat Nov 3 19:41:55 2007 UTC vs.
Revision 1.53 by root, Sat Nov 3 22:31:11 2007 UTC

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_watcher_list *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 = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)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 {
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 while ((w = (struct ev_io *)anfds [fd].head)) 301 while ((w = (struct ev_io *)anfds [fd].head))
301 { 302 {
302 ev_io_stop (w); 303 ev_io_stop (EV_A_ w);
303 event ((W)w, EV_ERROR | EV_READ | EV_WRITE); 304 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
304 } 305 }
305} 306}
306 307
307/* called on EBADF to verify fds */ 308/* called on EBADF to verify fds */
308static void 309static void
309fd_ebadf (void) 310fd_ebadf (EV_P)
310{ 311{
311 int fd; 312 int fd;
312 313
313 for (fd = 0; fd < anfdmax; ++fd) 314 for (fd = 0; fd < anfdmax; ++fd)
314 if (anfds [fd].events) 315 if (anfds [fd].events)
315 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 316 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
316 fd_kill (fd); 317 fd_kill (EV_A_ fd);
317} 318}
318 319
319/* 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 */
320static void 321static void
321fd_enomem (void) 322fd_enomem (EV_P)
322{ 323{
323 int fd = anfdmax; 324 int fd = anfdmax;
324 325
325 while (fd--) 326 while (fd--)
326 if (anfds [fd].events) 327 if (anfds [fd].events)
327 { 328 {
328 close (fd); 329 close (fd);
329 fd_kill (fd); 330 fd_kill (EV_A_ fd);
330 return; 331 return;
331 } 332 }
332} 333}
333 334
334/*****************************************************************************/ 335/*****************************************************************************/
335
336static struct ev_timer **timers;
337static int timermax, timercnt;
338
339static struct ev_periodic **periodics;
340static int periodicmax, periodiccnt;
341 336
342static void 337static void
343upheap (WT *timers, int k) 338upheap (WT *timers, int k)
344{ 339{
345 WT w = timers [k]; 340 WT w = timers [k];
391static ANSIG *signals; 386static ANSIG *signals;
392static int signalmax; 387static int signalmax;
393 388
394static int sigpipe [2]; 389static int sigpipe [2];
395static sig_atomic_t volatile gotsig; 390static sig_atomic_t volatile gotsig;
396static struct ev_io sigev;
397 391
398static void 392static void
399signals_init (ANSIG *base, int count) 393signals_init (ANSIG *base, int count)
400{ 394{
401 while (count--) 395 while (count--)
420 errno = old_errno; 414 errno = old_errno;
421 } 415 }
422} 416}
423 417
424static void 418static void
425sigcb (struct ev_io *iow, int revents) 419sigcb (EV_P_ struct ev_io *iow, int revents)
426{ 420{
427 struct ev_watcher_list *w; 421 struct ev_watcher_list *w;
428 int signum; 422 int signum;
429 423
430 read (sigpipe [0], &revents, 1); 424 read (sigpipe [0], &revents, 1);
434 if (signals [signum].gotsig) 428 if (signals [signum].gotsig)
435 { 429 {
436 signals [signum].gotsig = 0; 430 signals [signum].gotsig = 0;
437 431
438 for (w = signals [signum].head; w; w = w->next) 432 for (w = signals [signum].head; w; w = w->next)
439 event ((W)w, EV_SIGNAL); 433 event (EV_A_ (W)w, EV_SIGNAL);
440 } 434 }
441} 435}
442 436
443static void 437static void
444siginit (void) 438siginit (EV_P)
445{ 439{
446#ifndef WIN32 440#ifndef WIN32
447 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 441 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
448 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); 442 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
449 443
452 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 446 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
453#endif 447#endif
454 448
455 ev_io_set (&sigev, sigpipe [0], EV_READ); 449 ev_io_set (&sigev, sigpipe [0], EV_READ);
456 ev_io_start (&sigev); 450 ev_io_start (&sigev);
451 ev_unref (EV_A); /* child watcher should not keep loop alive */
457} 452}
458 453
459/*****************************************************************************/ 454/*****************************************************************************/
460
461static struct ev_idle **idles;
462static int idlemax, idlecnt;
463
464static struct ev_prepare **prepares;
465static int preparemax, preparecnt;
466
467static struct ev_check **checks;
468static int checkmax, checkcnt;
469
470/*****************************************************************************/
471
472static struct ev_child *childs [PID_HASHSIZE];
473static struct ev_signal childev;
474 455
475#ifndef WIN32 456#ifndef WIN32
476 457
477#ifndef WCONTINUED 458#ifndef WCONTINUED
478# define WCONTINUED 0 459# define WCONTINUED 0
479#endif 460#endif
480 461
481static void 462static void
482child_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)
483{ 464{
484 struct ev_child *w; 465 struct ev_child *w;
485 466
486 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 467 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
487 if (w->pid == pid || !w->pid) 468 if (w->pid == pid || !w->pid)
488 { 469 {
489 w->priority = sw->priority; /* need to do it *now* */ 470 w->priority = sw->priority; /* need to do it *now* */
490 w->rpid = pid; 471 w->rpid = pid;
491 w->rstatus = status; 472 w->rstatus = status;
492 event ((W)w, EV_CHILD); 473 event (EV_A_ (W)w, EV_CHILD);
493 } 474 }
494} 475}
495 476
496static void 477static void
497childcb (struct ev_signal *sw, int revents) 478childcb (EV_P_ struct ev_signal *sw, int revents)
498{ 479{
499 int pid, status; 480 int pid, status;
500 481
501 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 482 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
502 { 483 {
503 /* 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 */
504 event ((W)sw, EV_SIGNAL); 485 event (EV_A_ (W)sw, EV_SIGNAL);
505 486
506 child_reap (sw, pid, pid, status); 487 child_reap (EV_A_ sw, pid, pid, status);
507 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 */
508 } 489 }
509} 490}
510 491
511#endif 492#endif
512 493
516# include "ev_kqueue.c" 497# include "ev_kqueue.c"
517#endif 498#endif
518#if EV_USE_EPOLL 499#if EV_USE_EPOLL
519# include "ev_epoll.c" 500# include "ev_epoll.c"
520#endif 501#endif
521#if EV_USE_POLL 502#if EV_USEV_POLL
522# include "ev_poll.c" 503# include "ev_poll.c"
523#endif 504#endif
524#if EV_USE_SELECT 505#if EV_USE_SELECT
525# include "ev_select.c" 506# include "ev_select.c"
526#endif 507#endif
537 return EV_VERSION_MINOR; 518 return EV_VERSION_MINOR;
538} 519}
539 520
540/* 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 */
541static int 522static int
542enable_secure () 523enable_secure (void)
543{ 524{
544#ifdef WIN32 525#ifdef WIN32
545 return 0; 526 return 0;
546#else 527#else
547 return getuid () != geteuid () 528 return getuid () != geteuid ()
548 || getgid () != getegid (); 529 || getgid () != getegid ();
549#endif 530#endif
550} 531}
551 532
533int
534ev_method (EV_P)
535{
536 return method;
537}
538
539int
552int ev_init (int methods) 540ev_init (EV_P_ int methods)
553{ 541{
542#ifdef EV_MULTIPLICITY
543 memset (loop, 0, sizeof (struct ev_loop));
544#endif
545
554 if (!ev_method) 546 if (!method)
555 { 547 {
556#if EV_USE_MONOTONIC 548#if EV_USE_MONOTONIC
557 { 549 {
558 struct timespec ts; 550 struct timespec ts;
559 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 551 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
560 have_monotonic = 1; 552 have_monotonic = 1;
561 } 553 }
562#endif 554#endif
563 555
564 ev_now = ev_time (); 556 rt_now = ev_time ();
565 now = get_clock (); 557 mn_now = get_clock ();
566 now_floor = now; 558 now_floor = mn_now;
567 diff = ev_now - now; 559 diff = rt_now - mn_now;
568 560
569 if (pipe (sigpipe)) 561 if (pipe (sigpipe))
570 return 0; 562 return 0;
571 563
572 if (methods == EVMETHOD_AUTO) 564 if (methods == EVMETHOD_AUTO)
573 if (!enable_secure () && getenv ("LIBEV_METHODS")) 565 if (!enable_secure () && getenv ("LIBmethodS"))
574 methods = atoi (getenv ("LIBEV_METHODS")); 566 methods = atoi (getenv ("LIBmethodS"));
575 else 567 else
576 methods = EVMETHOD_ANY; 568 methods = EVMETHOD_ANY;
577 569
578 ev_method = 0; 570 method = 0;
579#if EV_USE_KQUEUE 571#if EV_USE_KQUEUE
580 if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); 572 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
581#endif 573#endif
582#if EV_USE_EPOLL 574#if EV_USE_EPOLL
583 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); 575 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
584#endif 576#endif
585#if EV_USE_POLL 577#if EV_USEV_POLL
586 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); 578 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
587#endif 579#endif
588#if EV_USE_SELECT 580#if EV_USE_SELECT
589 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); 581 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
590#endif 582#endif
591 583
592 if (ev_method) 584 if (method)
593 { 585 {
594 ev_watcher_init (&sigev, sigcb); 586 ev_watcher_init (&sigev, sigcb);
595 ev_set_priority (&sigev, EV_MAXPRI); 587 ev_set_priority (&sigev, EV_MAXPRI);
596 siginit (); 588 siginit (EV_A);
597 589
598#ifndef WIN32 590#ifndef WIN32
599 ev_signal_init (&childev, childcb, SIGCHLD); 591 ev_signal_init (&childev, childcb, SIGCHLD);
600 ev_set_priority (&childev, EV_MAXPRI); 592 ev_set_priority (&childev, EV_MAXPRI);
601 ev_signal_start (&childev); 593 ev_signal_start (EV_A_ &childev);
594 ev_unref (EV_A); /* child watcher should not keep loop alive */
602#endif 595#endif
603 } 596 }
604 } 597 }
605 598
606 return ev_method; 599 return method;
607} 600}
608 601
609/*****************************************************************************/ 602/*****************************************************************************/
610 603
611void 604void
622 615
623void 616void
624ev_fork_child (void) 617ev_fork_child (void)
625{ 618{
626#if EV_USE_EPOLL 619#if EV_USE_EPOLL
627 if (ev_method == EVMETHOD_EPOLL) 620 if (method == EVMETHOD_EPOLL)
628 epoll_postfork_child (); 621 epoll_postfork_child ();
629#endif 622#endif
630 623
631 ev_io_stop (&sigev); 624 ev_io_stop (&sigev);
632 close (sigpipe [0]); 625 close (sigpipe [0]);
636} 629}
637 630
638/*****************************************************************************/ 631/*****************************************************************************/
639 632
640static void 633static void
641call_pending (void) 634call_pending (EV_P)
642{ 635{
643 int pri; 636 int pri;
644 637
645 for (pri = NUMPRI; pri--; ) 638 for (pri = NUMPRI; pri--; )
646 while (pendingcnt [pri]) 639 while (pendingcnt [pri])
648 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 641 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
649 642
650 if (p->w) 643 if (p->w)
651 { 644 {
652 p->w->pending = 0; 645 p->w->pending = 0;
653 p->w->cb (p->w, p->events); 646 p->w->cb (EV_A_ p->w, p->events);
654 } 647 }
655 } 648 }
656} 649}
657 650
658static void 651static void
659timers_reify (void) 652timers_reify (EV_P)
660{ 653{
661 while (timercnt && timers [0]->at <= now) 654 while (timercnt && timers [0]->at <= mn_now)
662 { 655 {
663 struct ev_timer *w = timers [0]; 656 struct ev_timer *w = timers [0];
664 657
665 /* first reschedule or stop timer */ 658 /* first reschedule or stop timer */
666 if (w->repeat) 659 if (w->repeat)
667 { 660 {
668 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.));
669 w->at = now + w->repeat; 662 w->at = mn_now + w->repeat;
670 downheap ((WT *)timers, timercnt, 0); 663 downheap ((WT *)timers, timercnt, 0);
671 } 664 }
672 else 665 else
673 ev_timer_stop (w); /* nonrepeating: stop timer */ 666 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
674 667
675 event ((W)w, EV_TIMEOUT); 668 event ((W)w, EV_TIMEOUT);
676 } 669 }
677} 670}
678 671
679static void 672static void
680periodics_reify (void) 673periodics_reify (EV_P)
681{ 674{
682 while (periodiccnt && periodics [0]->at <= ev_now) 675 while (periodiccnt && periodics [0]->at <= rt_now)
683 { 676 {
684 struct ev_periodic *w = periodics [0]; 677 struct ev_periodic *w = periodics [0];
685 678
686 /* first reschedule or stop timer */ 679 /* first reschedule or stop timer */
687 if (w->interval) 680 if (w->interval)
688 { 681 {
689 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;
690 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));
691 downheap ((WT *)periodics, periodiccnt, 0); 684 downheap ((WT *)periodics, periodiccnt, 0);
692 } 685 }
693 else 686 else
694 ev_periodic_stop (w); /* nonrepeating: stop timer */ 687 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
695 688
696 event ((W)w, EV_PERIODIC); 689 event (EV_A_ (W)w, EV_PERIODIC);
697 } 690 }
698} 691}
699 692
700static void 693static void
701periodics_reschedule (ev_tstamp diff) 694periodics_reschedule (EV_P_ ev_tstamp diff)
702{ 695{
703 int i; 696 int i;
704 697
705 /* adjust periodics after time jump */ 698 /* adjust periodics after time jump */
706 for (i = 0; i < periodiccnt; ++i) 699 for (i = 0; i < periodiccnt; ++i)
707 { 700 {
708 struct ev_periodic *w = periodics [i]; 701 struct ev_periodic *w = periodics [i];
709 702
710 if (w->interval) 703 if (w->interval)
711 { 704 {
712 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;
713 706
714 if (fabs (diff) >= 1e-4) 707 if (fabs (diff) >= 1e-4)
715 { 708 {
716 ev_periodic_stop (w); 709 ev_periodic_stop (EV_A_ w);
717 ev_periodic_start (w); 710 ev_periodic_start (EV_A_ w);
718 711
719 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 712 i = 0; /* restart loop, inefficient, but time jumps should be rare */
720 } 713 }
721 } 714 }
722 } 715 }
723} 716}
724 717
725static int 718inline int
726time_update_monotonic (void) 719time_update_monotonic (EV_P)
727{ 720{
728 now = get_clock (); 721 mn_now = get_clock ();
729 722
730 if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) 723 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
731 { 724 {
732 ev_now = now + diff; 725 rt_now = mn_now + diff;
733 return 0; 726 return 0;
734 } 727 }
735 else 728 else
736 { 729 {
737 now_floor = now; 730 now_floor = mn_now;
738 ev_now = ev_time (); 731 rt_now = ev_time ();
739 return 1; 732 return 1;
740 } 733 }
741} 734}
742 735
743static void 736static void
744time_update (void) 737time_update (EV_P)
745{ 738{
746 int i; 739 int i;
747 740
748#if EV_USE_MONOTONIC 741#if EV_USE_MONOTONIC
749 if (expect_true (have_monotonic)) 742 if (expect_true (have_monotonic))
750 { 743 {
751 if (time_update_monotonic ()) 744 if (time_update_monotonic (EV_A))
752 { 745 {
753 ev_tstamp odiff = diff; 746 ev_tstamp odiff = diff;
754 747
755 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 */
756 { 749 {
757 diff = ev_now - now; 750 diff = rt_now - mn_now;
758 751
759 if (fabs (odiff - diff) < MIN_TIMEJUMP) 752 if (fabs (odiff - diff) < MIN_TIMEJUMP)
760 return; /* all is well */ 753 return; /* all is well */
761 754
762 ev_now = ev_time (); 755 rt_now = ev_time ();
763 now = get_clock (); 756 mn_now = get_clock ();
764 now_floor = now; 757 now_floor = mn_now;
765 } 758 }
766 759
767 periodics_reschedule (diff - odiff); 760 periodics_reschedule (EV_A_ diff - odiff);
768 /* no timer adjustment, as the monotonic clock doesn't jump */ 761 /* no timer adjustment, as the monotonic clock doesn't jump */
769 } 762 }
770 } 763 }
771 else 764 else
772#endif 765#endif
773 { 766 {
774 ev_now = ev_time (); 767 rt_now = ev_time ();
775 768
776 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))
777 { 770 {
778 periodics_reschedule (ev_now - now); 771 periodics_reschedule (EV_A_ rt_now - mn_now);
779 772
780 /* 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 */
781 for (i = 0; i < timercnt; ++i) 774 for (i = 0; i < timercnt; ++i)
782 timers [i]->at += diff; 775 timers [i]->at += diff;
783 } 776 }
784 777
785 now = ev_now; 778 mn_now = rt_now;
786 } 779 }
787} 780}
788 781
789int ev_loop_done; 782void
783ev_ref (EV_P)
784{
785 ++activecnt;
786}
790 787
788void
789ev_unref (EV_P)
790{
791 --activecnt;
792}
793
794static int loop_done;
795
796void
791void ev_loop (int flags) 797ev_loop (EV_P_ int flags)
792{ 798{
793 double block; 799 double block;
794 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 800 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
795 801
796 do 802 do
797 { 803 {
798 /* queue check watchers (and execute them) */ 804 /* queue check watchers (and execute them) */
799 if (expect_false (preparecnt)) 805 if (expect_false (preparecnt))
800 { 806 {
801 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 807 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
802 call_pending (); 808 call_pending (EV_A);
803 } 809 }
804 810
805 /* update fd-related kernel structures */ 811 /* update fd-related kernel structures */
806 fd_reify (); 812 fd_reify (EV_A);
807 813
808 /* calculate blocking time */ 814 /* calculate blocking time */
809 815
810 /* 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
811 always have timers, we just calculate it always */ 817 always have timers, we just calculate it always */
812#if EV_USE_MONOTONIC 818#if EV_USE_MONOTONIC
813 if (expect_true (have_monotonic)) 819 if (expect_true (have_monotonic))
814 time_update_monotonic (); 820 time_update_monotonic (EV_A);
815 else 821 else
816#endif 822#endif
817 { 823 {
818 ev_now = ev_time (); 824 rt_now = ev_time ();
819 now = ev_now; 825 mn_now = rt_now;
820 } 826 }
821 827
822 if (flags & EVLOOP_NONBLOCK || idlecnt) 828 if (flags & EVLOOP_NONBLOCK || idlecnt)
823 block = 0.; 829 block = 0.;
824 else 830 else
825 { 831 {
826 block = MAX_BLOCKTIME; 832 block = MAX_BLOCKTIME;
827 833
828 if (timercnt) 834 if (timercnt)
829 { 835 {
830 ev_tstamp to = timers [0]->at - now + method_fudge; 836 ev_tstamp to = timers [0]->at - mn_now + method_fudge;
831 if (block > to) block = to; 837 if (block > to) block = to;
832 } 838 }
833 839
834 if (periodiccnt) 840 if (periodiccnt)
835 { 841 {
836 ev_tstamp to = periodics [0]->at - ev_now + method_fudge; 842 ev_tstamp to = periodics [0]->at - rt_now + method_fudge;
837 if (block > to) block = to; 843 if (block > to) block = to;
838 } 844 }
839 845
840 if (block < 0.) block = 0.; 846 if (block < 0.) block = 0.;
841 } 847 }
842 848
843 method_poll (block); 849 method_poll (EV_A_ block);
844 850
845 /* update ev_now, do magic */ 851 /* update rt_now, do magic */
846 time_update (); 852 time_update (EV_A);
847 853
848 /* queue pending timers and reschedule them */ 854 /* queue pending timers and reschedule them */
849 timers_reify (); /* relative timers called last */ 855 timers_reify (EV_A); /* relative timers called last */
850 periodics_reify (); /* absolute timers called first */ 856 periodics_reify (EV_A); /* absolute timers called first */
851 857
852 /* queue idle watchers unless io or timers are pending */ 858 /* queue idle watchers unless io or timers are pending */
853 if (!pendingcnt) 859 if (!pendingcnt)
854 queue_events ((W *)idles, idlecnt, EV_IDLE); 860 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
855 861
856 /* queue check watchers, to be executed first */ 862 /* queue check watchers, to be executed first */
857 if (checkcnt) 863 if (checkcnt)
858 queue_events ((W *)checks, checkcnt, EV_CHECK); 864 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
859 865
860 call_pending (); 866 call_pending (EV_A);
861 } 867 }
862 while (!ev_loop_done); 868 while (activecnt && !loop_done);
863 869
864 if (ev_loop_done != 2) 870 if (loop_done != 2)
865 ev_loop_done = 0; 871 loop_done = 0;
872}
873
874void
875ev_unloop (EV_P_ int how)
876{
877 loop_done = how;
866} 878}
867 879
868/*****************************************************************************/ 880/*****************************************************************************/
869 881
870static void 882inline void
871wlist_add (WL *head, WL elem) 883wlist_add (WL *head, WL elem)
872{ 884{
873 elem->next = *head; 885 elem->next = *head;
874 *head = elem; 886 *head = elem;
875} 887}
876 888
877static void 889inline void
878wlist_del (WL *head, WL elem) 890wlist_del (WL *head, WL elem)
879{ 891{
880 while (*head) 892 while (*head)
881 { 893 {
882 if (*head == elem) 894 if (*head == elem)
887 899
888 head = &(*head)->next; 900 head = &(*head)->next;
889 } 901 }
890} 902}
891 903
892static void 904inline void
893ev_clear_pending (W w) 905ev_clear_pending (EV_P_ W w)
894{ 906{
895 if (w->pending) 907 if (w->pending)
896 { 908 {
897 pendings [ABSPRI (w)][w->pending - 1].w = 0; 909 pendings [ABSPRI (w)][w->pending - 1].w = 0;
898 w->pending = 0; 910 w->pending = 0;
899 } 911 }
900} 912}
901 913
902static void 914inline void
903ev_start (W w, int active) 915ev_start (EV_P_ W w, int active)
904{ 916{
905 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 917 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
906 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; 918 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
907 919
908 w->active = active; 920 w->active = active;
921 ev_ref (EV_A);
909} 922}
910 923
911static void 924inline void
912ev_stop (W w) 925ev_stop (EV_P_ W w)
913{ 926{
927 ev_unref (EV_A);
914 w->active = 0; 928 w->active = 0;
915} 929}
916 930
917/*****************************************************************************/ 931/*****************************************************************************/
918 932
919void 933void
920ev_io_start (struct ev_io *w) 934ev_io_start (EV_P_ struct ev_io *w)
921{ 935{
922 int fd = w->fd; 936 int fd = w->fd;
923 937
924 if (ev_is_active (w)) 938 if (ev_is_active (w))
925 return; 939 return;
926 940
927 assert (("ev_io_start called with negative fd", fd >= 0)); 941 assert (("ev_io_start called with negative fd", fd >= 0));
928 942
929 ev_start ((W)w, 1); 943 ev_start (EV_A_ (W)w, 1);
930 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 944 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
931 wlist_add ((WL *)&anfds[fd].head, (WL)w); 945 wlist_add ((WL *)&anfds[fd].head, (WL)w);
932 946
933 fd_change (fd); 947 fd_change (EV_A_ fd);
934} 948}
935 949
936void 950void
937ev_io_stop (struct ev_io *w) 951ev_io_stop (EV_P_ struct ev_io *w)
938{ 952{
939 ev_clear_pending ((W)w); 953 ev_clear_pending (EV_A_ (W)w);
940 if (!ev_is_active (w)) 954 if (!ev_is_active (w))
941 return; 955 return;
942 956
943 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 957 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
944 ev_stop ((W)w); 958 ev_stop (EV_A_ (W)w);
945 959
946 fd_change (w->fd); 960 fd_change (EV_A_ w->fd);
947} 961}
948 962
949void 963void
950ev_timer_start (struct ev_timer *w) 964ev_timer_start (EV_P_ struct ev_timer *w)
951{ 965{
952 if (ev_is_active (w)) 966 if (ev_is_active (w))
953 return; 967 return;
954 968
955 w->at += now; 969 w->at += mn_now;
956 970
957 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.));
958 972
959 ev_start ((W)w, ++timercnt); 973 ev_start (EV_A_ (W)w, ++timercnt);
960 array_needsize (timers, timermax, timercnt, ); 974 array_needsize (timers, timermax, timercnt, );
961 timers [timercnt - 1] = w; 975 timers [timercnt - 1] = w;
962 upheap ((WT *)timers, timercnt - 1); 976 upheap ((WT *)timers, timercnt - 1);
963} 977}
964 978
965void 979void
966ev_timer_stop (struct ev_timer *w) 980ev_timer_stop (EV_P_ struct ev_timer *w)
967{ 981{
968 ev_clear_pending ((W)w); 982 ev_clear_pending (EV_A_ (W)w);
969 if (!ev_is_active (w)) 983 if (!ev_is_active (w))
970 return; 984 return;
971 985
972 if (w->active < timercnt--) 986 if (w->active < timercnt--)
973 { 987 {
975 downheap ((WT *)timers, timercnt, w->active - 1); 989 downheap ((WT *)timers, timercnt, w->active - 1);
976 } 990 }
977 991
978 w->at = w->repeat; 992 w->at = w->repeat;
979 993
980 ev_stop ((W)w); 994 ev_stop (EV_A_ (W)w);
981} 995}
982 996
983void 997void
984ev_timer_again (struct ev_timer *w) 998ev_timer_again (EV_P_ struct ev_timer *w)
985{ 999{
986 if (ev_is_active (w)) 1000 if (ev_is_active (w))
987 { 1001 {
988 if (w->repeat) 1002 if (w->repeat)
989 { 1003 {
990 w->at = now + w->repeat; 1004 w->at = mn_now + w->repeat;
991 downheap ((WT *)timers, timercnt, w->active - 1); 1005 downheap ((WT *)timers, timercnt, w->active - 1);
992 } 1006 }
993 else 1007 else
994 ev_timer_stop (w); 1008 ev_timer_stop (EV_A_ w);
995 } 1009 }
996 else if (w->repeat) 1010 else if (w->repeat)
997 ev_timer_start (w); 1011 ev_timer_start (EV_A_ w);
998} 1012}
999 1013
1000void 1014void
1001ev_periodic_start (struct ev_periodic *w) 1015ev_periodic_start (EV_P_ struct ev_periodic *w)
1002{ 1016{
1003 if (ev_is_active (w)) 1017 if (ev_is_active (w))
1004 return; 1018 return;
1005 1019
1006 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.));
1007 1021
1008 /* 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 */
1009 if (w->interval) 1023 if (w->interval)
1010 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 1024 w->at += ceil ((rt_now - w->at) / w->interval) * w->interval;
1011 1025
1012 ev_start ((W)w, ++periodiccnt); 1026 ev_start (EV_A_ (W)w, ++periodiccnt);
1013 array_needsize (periodics, periodicmax, periodiccnt, ); 1027 array_needsize (periodics, periodicmax, periodiccnt, );
1014 periodics [periodiccnt - 1] = w; 1028 periodics [periodiccnt - 1] = w;
1015 upheap ((WT *)periodics, periodiccnt - 1); 1029 upheap ((WT *)periodics, periodiccnt - 1);
1016} 1030}
1017 1031
1018void 1032void
1019ev_periodic_stop (struct ev_periodic *w) 1033ev_periodic_stop (EV_P_ struct ev_periodic *w)
1020{ 1034{
1021 ev_clear_pending ((W)w); 1035 ev_clear_pending (EV_A_ (W)w);
1022 if (!ev_is_active (w)) 1036 if (!ev_is_active (w))
1023 return; 1037 return;
1024 1038
1025 if (w->active < periodiccnt--) 1039 if (w->active < periodiccnt--)
1026 { 1040 {
1027 periodics [w->active - 1] = periodics [periodiccnt]; 1041 periodics [w->active - 1] = periodics [periodiccnt];
1028 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1042 downheap ((WT *)periodics, periodiccnt, w->active - 1);
1029 } 1043 }
1030 1044
1031 ev_stop ((W)w); 1045 ev_stop (EV_A_ (W)w);
1032} 1046}
1033 1047
1034#ifndef SA_RESTART 1048#ifndef SA_RESTART
1035# define SA_RESTART 0 1049# define SA_RESTART 0
1036#endif 1050#endif
1037 1051
1038void 1052void
1039ev_signal_start (struct ev_signal *w) 1053ev_signal_start (EV_P_ struct ev_signal *w)
1040{ 1054{
1041 if (ev_is_active (w)) 1055 if (ev_is_active (w))
1042 return; 1056 return;
1043 1057
1044 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));
1045 1059
1046 ev_start ((W)w, 1); 1060 ev_start (EV_A_ (W)w, 1);
1047 array_needsize (signals, signalmax, w->signum, signals_init); 1061 array_needsize (signals, signalmax, w->signum, signals_init);
1048 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1062 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1049 1063
1050 if (!w->next) 1064 if (!w->next)
1051 { 1065 {
1056 sigaction (w->signum, &sa, 0); 1070 sigaction (w->signum, &sa, 0);
1057 } 1071 }
1058} 1072}
1059 1073
1060void 1074void
1061ev_signal_stop (struct ev_signal *w) 1075ev_signal_stop (EV_P_ struct ev_signal *w)
1062{ 1076{
1063 ev_clear_pending ((W)w); 1077 ev_clear_pending (EV_A_ (W)w);
1064 if (!ev_is_active (w)) 1078 if (!ev_is_active (w))
1065 return; 1079 return;
1066 1080
1067 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1081 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1068 ev_stop ((W)w); 1082 ev_stop (EV_A_ (W)w);
1069 1083
1070 if (!signals [w->signum - 1].head) 1084 if (!signals [w->signum - 1].head)
1071 signal (w->signum, SIG_DFL); 1085 signal (w->signum, SIG_DFL);
1072} 1086}
1073 1087
1074void 1088void
1075ev_idle_start (struct ev_idle *w) 1089ev_idle_start (EV_P_ struct ev_idle *w)
1076{ 1090{
1077 if (ev_is_active (w)) 1091 if (ev_is_active (w))
1078 return; 1092 return;
1079 1093
1080 ev_start ((W)w, ++idlecnt); 1094 ev_start (EV_A_ (W)w, ++idlecnt);
1081 array_needsize (idles, idlemax, idlecnt, ); 1095 array_needsize (idles, idlemax, idlecnt, );
1082 idles [idlecnt - 1] = w; 1096 idles [idlecnt - 1] = w;
1083} 1097}
1084 1098
1085void 1099void
1086ev_idle_stop (struct ev_idle *w) 1100ev_idle_stop (EV_P_ struct ev_idle *w)
1087{ 1101{
1088 ev_clear_pending ((W)w); 1102 ev_clear_pending (EV_A_ (W)w);
1089 if (ev_is_active (w)) 1103 if (ev_is_active (w))
1090 return; 1104 return;
1091 1105
1092 idles [w->active - 1] = idles [--idlecnt]; 1106 idles [w->active - 1] = idles [--idlecnt];
1093 ev_stop ((W)w); 1107 ev_stop (EV_A_ (W)w);
1094} 1108}
1095 1109
1096void 1110void
1097ev_prepare_start (struct ev_prepare *w) 1111ev_prepare_start (EV_P_ struct ev_prepare *w)
1098{ 1112{
1099 if (ev_is_active (w)) 1113 if (ev_is_active (w))
1100 return; 1114 return;
1101 1115
1102 ev_start ((W)w, ++preparecnt); 1116 ev_start (EV_A_ (W)w, ++preparecnt);
1103 array_needsize (prepares, preparemax, preparecnt, ); 1117 array_needsize (prepares, preparemax, preparecnt, );
1104 prepares [preparecnt - 1] = w; 1118 prepares [preparecnt - 1] = w;
1105} 1119}
1106 1120
1107void 1121void
1108ev_prepare_stop (struct ev_prepare *w) 1122ev_prepare_stop (EV_P_ struct ev_prepare *w)
1109{ 1123{
1110 ev_clear_pending ((W)w); 1124 ev_clear_pending (EV_A_ (W)w);
1111 if (ev_is_active (w)) 1125 if (ev_is_active (w))
1112 return; 1126 return;
1113 1127
1114 prepares [w->active - 1] = prepares [--preparecnt]; 1128 prepares [w->active - 1] = prepares [--preparecnt];
1115 ev_stop ((W)w); 1129 ev_stop (EV_A_ (W)w);
1116} 1130}
1117 1131
1118void 1132void
1119ev_check_start (struct ev_check *w) 1133ev_check_start (EV_P_ struct ev_check *w)
1120{ 1134{
1121 if (ev_is_active (w)) 1135 if (ev_is_active (w))
1122 return; 1136 return;
1123 1137
1124 ev_start ((W)w, ++checkcnt); 1138 ev_start (EV_A_ (W)w, ++checkcnt);
1125 array_needsize (checks, checkmax, checkcnt, ); 1139 array_needsize (checks, checkmax, checkcnt, );
1126 checks [checkcnt - 1] = w; 1140 checks [checkcnt - 1] = w;
1127} 1141}
1128 1142
1129void 1143void
1130ev_check_stop (struct ev_check *w) 1144ev_check_stop (EV_P_ struct ev_check *w)
1131{ 1145{
1132 ev_clear_pending ((W)w); 1146 ev_clear_pending (EV_A_ (W)w);
1133 if (ev_is_active (w)) 1147 if (ev_is_active (w))
1134 return; 1148 return;
1135 1149
1136 checks [w->active - 1] = checks [--checkcnt]; 1150 checks [w->active - 1] = checks [--checkcnt];
1137 ev_stop ((W)w); 1151 ev_stop (EV_A_ (W)w);
1138} 1152}
1139 1153
1140void 1154void
1141ev_child_start (struct ev_child *w) 1155ev_child_start (EV_P_ struct ev_child *w)
1142{ 1156{
1143 if (ev_is_active (w)) 1157 if (ev_is_active (w))
1144 return; 1158 return;
1145 1159
1146 ev_start ((W)w, 1); 1160 ev_start (EV_A_ (W)w, 1);
1147 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1161 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1148} 1162}
1149 1163
1150void 1164void
1151ev_child_stop (struct ev_child *w) 1165ev_child_stop (EV_P_ struct ev_child *w)
1152{ 1166{
1153 ev_clear_pending ((W)w); 1167 ev_clear_pending (EV_A_ (W)w);
1154 if (ev_is_active (w)) 1168 if (ev_is_active (w))
1155 return; 1169 return;
1156 1170
1157 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1171 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1158 ev_stop ((W)w); 1172 ev_stop (EV_A_ (W)w);
1159} 1173}
1160 1174
1161/*****************************************************************************/ 1175/*****************************************************************************/
1162 1176
1163struct ev_once 1177struct ev_once
1167 void (*cb)(int revents, void *arg); 1181 void (*cb)(int revents, void *arg);
1168 void *arg; 1182 void *arg;
1169}; 1183};
1170 1184
1171static void 1185static void
1172once_cb (struct ev_once *once, int revents) 1186once_cb (EV_P_ struct ev_once *once, int revents)
1173{ 1187{
1174 void (*cb)(int revents, void *arg) = once->cb; 1188 void (*cb)(int revents, void *arg) = once->cb;
1175 void *arg = once->arg; 1189 void *arg = once->arg;
1176 1190
1177 ev_io_stop (&once->io); 1191 ev_io_stop (EV_A_ &once->io);
1178 ev_timer_stop (&once->to); 1192 ev_timer_stop (EV_A_ &once->to);
1179 free (once); 1193 free (once);
1180 1194
1181 cb (revents, arg); 1195 cb (revents, arg);
1182} 1196}
1183 1197
1184static void 1198static void
1185once_cb_io (struct ev_io *w, int revents) 1199once_cb_io (EV_P_ struct ev_io *w, int revents)
1186{ 1200{
1187 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);
1188} 1202}
1189 1203
1190static void 1204static void
1191once_cb_to (struct ev_timer *w, int revents) 1205once_cb_to (EV_P_ struct ev_timer *w, int revents)
1192{ 1206{
1193 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);
1194} 1208}
1195 1209
1196void 1210void
1197ev_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)
1198{ 1212{
1199 struct ev_once *once = malloc (sizeof (struct ev_once)); 1213 struct ev_once *once = malloc (sizeof (struct ev_once));
1200 1214
1201 if (!once) 1215 if (!once)
1202 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1216 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1207 1221
1208 ev_watcher_init (&once->io, once_cb_io); 1222 ev_watcher_init (&once->io, once_cb_io);
1209 if (fd >= 0) 1223 if (fd >= 0)
1210 { 1224 {
1211 ev_io_set (&once->io, fd, events); 1225 ev_io_set (&once->io, fd, events);
1212 ev_io_start (&once->io); 1226 ev_io_start (EV_A_ &once->io);
1213 } 1227 }
1214 1228
1215 ev_watcher_init (&once->to, once_cb_to); 1229 ev_watcher_init (&once->to, once_cb_to);
1216 if (timeout >= 0.) 1230 if (timeout >= 0.)
1217 { 1231 {
1218 ev_timer_set (&once->to, timeout, 0.); 1232 ev_timer_set (&once->to, timeout, 0.);
1219 ev_timer_start (&once->to); 1233 ev_timer_start (EV_A_ &once->to);
1220 } 1234 }
1221 } 1235 }
1222} 1236}
1223 1237
1224/*****************************************************************************/ 1238/*****************************************************************************/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines