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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines