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

Comparing libev/ev.c (file contents):
Revision 1.43 by root, Fri Nov 2 20:21:33 2007 UTC vs.
Revision 1.52 by root, Sat Nov 3 22:10:39 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines