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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines