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

Comparing libev/ev.c (file contents):
Revision 1.115 by root, Wed Nov 14 04:53:21 2007 UTC vs.
Revision 1.140 by root, Mon Nov 26 19:49:36 2007 UTC

32#ifdef __cplusplus 32#ifdef __cplusplus
33extern "C" { 33extern "C" {
34#endif 34#endif
35 35
36#ifndef EV_STANDALONE 36#ifndef EV_STANDALONE
37# ifdef EV_CONFIG_H
38# include EV_CONFIG_H
39# else
37# include "config.h" 40# include "config.h"
41# endif
38 42
39# if HAVE_CLOCK_GETTIME 43# if HAVE_CLOCK_GETTIME
40# ifndef EV_USE_MONOTONIC 44# ifndef EV_USE_MONOTONIC
41# define EV_USE_MONOTONIC 1 45# define EV_USE_MONOTONIC 1
42# endif 46# endif
43# ifndef EV_USE_REALTIME 47# ifndef EV_USE_REALTIME
44# define EV_USE_REALTIME 1 48# define EV_USE_REALTIME 1
45# endif 49# endif
50# else
51# ifndef EV_USE_MONOTONIC
52# define EV_USE_MONOTONIC 0
53# endif
54# ifndef EV_USE_REALTIME
55# define EV_USE_REALTIME 0
56# endif
46# endif 57# endif
47 58
48# if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT) 59# ifndef EV_USE_SELECT
60# if HAVE_SELECT && HAVE_SYS_SELECT_H
49# define EV_USE_SELECT 1 61# define EV_USE_SELECT 1
62# else
63# define EV_USE_SELECT 0
64# endif
50# endif 65# endif
51 66
52# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) 67# ifndef EV_USE_POLL
68# if HAVE_POLL && HAVE_POLL_H
53# define EV_USE_POLL 1 69# define EV_USE_POLL 1
70# else
71# define EV_USE_POLL 0
72# endif
54# endif 73# endif
55 74
56# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL) 75# ifndef EV_USE_EPOLL
76# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
57# define EV_USE_EPOLL 1 77# define EV_USE_EPOLL 1
78# else
79# define EV_USE_EPOLL 0
80# endif
58# endif 81# endif
59 82
83# ifndef EV_USE_KQUEUE
60# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE) 84# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
61# define EV_USE_KQUEUE 1 85# define EV_USE_KQUEUE 1
86# else
87# define EV_USE_KQUEUE 0
88# endif
89# endif
90
91# ifndef EV_USE_PORT
92# if HAVE_PORT_H && HAVE_PORT_CREATE
93# define EV_USE_PORT 1
94# else
95# define EV_USE_PORT 0
96# endif
62# endif 97# endif
63 98
64#endif 99#endif
65 100
66#include <math.h> 101#include <math.h>
76#include <time.h> 111#include <time.h>
77 112
78#include <signal.h> 113#include <signal.h>
79 114
80#ifndef _WIN32 115#ifndef _WIN32
81# include <unistd.h>
82# include <sys/time.h> 116# include <sys/time.h>
83# include <sys/wait.h> 117# include <sys/wait.h>
118# include <unistd.h>
84#else 119#else
85# define WIN32_LEAN_AND_MEAN 120# define WIN32_LEAN_AND_MEAN
86# include <windows.h> 121# include <windows.h>
87# ifndef EV_SELECT_IS_WINSOCKET 122# ifndef EV_SELECT_IS_WINSOCKET
88# define EV_SELECT_IS_WINSOCKET 1 123# define EV_SELECT_IS_WINSOCKET 1
90#endif 125#endif
91 126
92/**/ 127/**/
93 128
94#ifndef EV_USE_MONOTONIC 129#ifndef EV_USE_MONOTONIC
95# define EV_USE_MONOTONIC 1 130# define EV_USE_MONOTONIC 0
131#endif
132
133#ifndef EV_USE_REALTIME
134# define EV_USE_REALTIME 0
96#endif 135#endif
97 136
98#ifndef EV_USE_SELECT 137#ifndef EV_USE_SELECT
99# define EV_USE_SELECT 1 138# define EV_USE_SELECT 1
100# define EV_SELECT_USE_FD_SET 1
101#endif 139#endif
102 140
103#ifndef EV_USE_POLL 141#ifndef EV_USE_POLL
104# ifdef _WIN32 142# ifdef _WIN32
105# define EV_USE_POLL 0 143# define EV_USE_POLL 0
114 152
115#ifndef EV_USE_KQUEUE 153#ifndef EV_USE_KQUEUE
116# define EV_USE_KQUEUE 0 154# define EV_USE_KQUEUE 0
117#endif 155#endif
118 156
119#ifndef EV_USE_REALTIME 157#ifndef EV_USE_PORT
120# define EV_USE_REALTIME 1 158# define EV_USE_PORT 0
121#endif 159#endif
122 160
123/**/ 161/**/
124
125/* darwin simply cannot be helped */
126#ifdef __APPLE__
127# undef EV_USE_POLL
128# undef EV_USE_KQUEUE
129#endif
130 162
131#ifndef CLOCK_MONOTONIC 163#ifndef CLOCK_MONOTONIC
132# undef EV_USE_MONOTONIC 164# undef EV_USE_MONOTONIC
133# define EV_USE_MONOTONIC 0 165# define EV_USE_MONOTONIC 0
134#endif 166#endif
143#endif 175#endif
144 176
145/**/ 177/**/
146 178
147#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 179#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
148#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ 180#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
149#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ 181#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
150/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ 182/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
151 183
152#ifdef EV_H 184#ifdef EV_H
153# include EV_H 185# include EV_H
154#else 186#else
155# include "ev.h" 187# include "ev.h"
156#endif 188#endif
157 189
158#if __GNUC__ >= 3 190#if __GNUC__ >= 3
159# define expect(expr,value) __builtin_expect ((expr),(value)) 191# define expect(expr,value) __builtin_expect ((expr),(value))
192# define inline_size static inline /* inline for codesize */
193# if EV_MINIMAL
160# define inline inline 194# define noinline __attribute__ ((noinline))
195# define inline_speed static noinline
196# else
197# define noinline
198# define inline_speed static inline
199# endif
161#else 200#else
162# define expect(expr,value) (expr) 201# define expect(expr,value) (expr)
163# define inline static 202# define inline_speed static
203# define inline_minimal static
204# define noinline
164#endif 205#endif
165 206
166#define expect_false(expr) expect ((expr) != 0, 0) 207#define expect_false(expr) expect ((expr) != 0, 0)
167#define expect_true(expr) expect ((expr) != 0, 1) 208#define expect_true(expr) expect ((expr) != 0, 1)
168 209
170#define ABSPRI(w) ((w)->priority - EV_MINPRI) 211#define ABSPRI(w) ((w)->priority - EV_MINPRI)
171 212
172#define EMPTY0 /* required for microsofts broken pseudo-c compiler */ 213#define EMPTY0 /* required for microsofts broken pseudo-c compiler */
173#define EMPTY2(a,b) /* used to suppress some warnings */ 214#define EMPTY2(a,b) /* used to suppress some warnings */
174 215
175typedef struct ev_watcher *W; 216typedef ev_watcher *W;
176typedef struct ev_watcher_list *WL; 217typedef ev_watcher_list *WL;
177typedef struct ev_watcher_time *WT; 218typedef ev_watcher_time *WT;
178 219
179static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 220static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
180 221
181#ifdef _WIN32 222#ifdef _WIN32
182# include "ev_win32.c" 223# include "ev_win32.c"
258 #include "ev_vars.h" 299 #include "ev_vars.h"
259 #undef VAR 300 #undef VAR
260 }; 301 };
261 #include "ev_wrap.h" 302 #include "ev_wrap.h"
262 303
263 struct ev_loop default_loop_struct; 304 static struct ev_loop default_loop_struct;
264 static struct ev_loop *default_loop; 305 struct ev_loop *ev_default_loop_ptr;
265 306
266#else 307#else
267 308
268 ev_tstamp ev_rt_now; 309 ev_tstamp ev_rt_now;
269 #define VAR(name,decl) static decl; 310 #define VAR(name,decl) static decl;
270 #include "ev_vars.h" 311 #include "ev_vars.h"
271 #undef VAR 312 #undef VAR
272 313
273 static int default_loop; 314 static int ev_default_loop_ptr;
274 315
275#endif 316#endif
276 317
277/*****************************************************************************/ 318/*****************************************************************************/
278 319
279ev_tstamp 320ev_tstamp noinline
280ev_time (void) 321ev_time (void)
281{ 322{
282#if EV_USE_REALTIME 323#if EV_USE_REALTIME
283 struct timespec ts; 324 struct timespec ts;
284 clock_gettime (CLOCK_REALTIME, &ts); 325 clock_gettime (CLOCK_REALTIME, &ts);
288 gettimeofday (&tv, 0); 329 gettimeofday (&tv, 0);
289 return tv.tv_sec + tv.tv_usec * 1e-6; 330 return tv.tv_sec + tv.tv_usec * 1e-6;
290#endif 331#endif
291} 332}
292 333
293inline ev_tstamp 334ev_tstamp inline_size
294get_clock (void) 335get_clock (void)
295{ 336{
296#if EV_USE_MONOTONIC 337#if EV_USE_MONOTONIC
297 if (expect_true (have_monotonic)) 338 if (expect_true (have_monotonic))
298 { 339 {
341#define array_free(stem, idx) \ 382#define array_free(stem, idx) \
342 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 383 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
343 384
344/*****************************************************************************/ 385/*****************************************************************************/
345 386
346static void 387void inline_size
347anfds_init (ANFD *base, int count) 388anfds_init (ANFD *base, int count)
348{ 389{
349 while (count--) 390 while (count--)
350 { 391 {
351 base->head = 0; 392 base->head = 0;
354 395
355 ++base; 396 ++base;
356 } 397 }
357} 398}
358 399
359void 400void noinline
360ev_feed_event (EV_P_ void *w, int revents) 401ev_feed_event (EV_P_ void *w, int revents)
361{ 402{
362 W w_ = (W)w; 403 W w_ = (W)w;
363 404
364 if (w_->pending) 405 if (expect_false (w_->pending))
365 { 406 {
366 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; 407 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
367 return; 408 return;
368 } 409 }
369 410
380 421
381 for (i = 0; i < eventcnt; ++i) 422 for (i = 0; i < eventcnt; ++i)
382 ev_feed_event (EV_A_ events [i], type); 423 ev_feed_event (EV_A_ events [i], type);
383} 424}
384 425
385inline void 426void inline_speed
386fd_event (EV_P_ int fd, int revents) 427fd_event (EV_P_ int fd, int revents)
387{ 428{
388 ANFD *anfd = anfds + fd; 429 ANFD *anfd = anfds + fd;
389 struct ev_io *w; 430 ev_io *w;
390 431
391 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 432 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
392 { 433 {
393 int ev = w->events & revents; 434 int ev = w->events & revents;
394 435
395 if (ev) 436 if (ev)
396 ev_feed_event (EV_A_ (W)w, ev); 437 ev_feed_event (EV_A_ (W)w, ev);
403 fd_event (EV_A_ fd, revents); 444 fd_event (EV_A_ fd, revents);
404} 445}
405 446
406/*****************************************************************************/ 447/*****************************************************************************/
407 448
408static void 449void inline_size
409fd_reify (EV_P) 450fd_reify (EV_P)
410{ 451{
411 int i; 452 int i;
412 453
413 for (i = 0; i < fdchangecnt; ++i) 454 for (i = 0; i < fdchangecnt; ++i)
414 { 455 {
415 int fd = fdchanges [i]; 456 int fd = fdchanges [i];
416 ANFD *anfd = anfds + fd; 457 ANFD *anfd = anfds + fd;
417 struct ev_io *w; 458 ev_io *w;
418 459
419 int events = 0; 460 int events = 0;
420 461
421 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 462 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
422 events |= w->events; 463 events |= w->events;
423 464
424#if EV_SELECT_IS_WINSOCKET 465#if EV_SELECT_IS_WINSOCKET
425 if (events) 466 if (events)
426 { 467 {
430 } 471 }
431#endif 472#endif
432 473
433 anfd->reify = 0; 474 anfd->reify = 0;
434 475
435 method_modify (EV_A_ fd, anfd->events, events); 476 backend_modify (EV_A_ fd, anfd->events, events);
436 anfd->events = events; 477 anfd->events = events;
437 } 478 }
438 479
439 fdchangecnt = 0; 480 fdchangecnt = 0;
440} 481}
441 482
442static void 483void inline_size
443fd_change (EV_P_ int fd) 484fd_change (EV_P_ int fd)
444{ 485{
445 if (anfds [fd].reify) 486 if (expect_false (anfds [fd].reify))
446 return; 487 return;
447 488
448 anfds [fd].reify = 1; 489 anfds [fd].reify = 1;
449 490
450 ++fdchangecnt; 491 ++fdchangecnt;
451 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); 492 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
452 fdchanges [fdchangecnt - 1] = fd; 493 fdchanges [fdchangecnt - 1] = fd;
453} 494}
454 495
455static void 496void inline_speed
456fd_kill (EV_P_ int fd) 497fd_kill (EV_P_ int fd)
457{ 498{
458 struct ev_io *w; 499 ev_io *w;
459 500
460 while ((w = (struct ev_io *)anfds [fd].head)) 501 while ((w = (ev_io *)anfds [fd].head))
461 { 502 {
462 ev_io_stop (EV_A_ w); 503 ev_io_stop (EV_A_ w);
463 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 504 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
464 } 505 }
465} 506}
466 507
467static int 508int inline_size
468fd_valid (int fd) 509fd_valid (int fd)
469{ 510{
470#ifdef _WIN32 511#ifdef _WIN32
471 return _get_osfhandle (fd) != -1; 512 return _get_osfhandle (fd) != -1;
472#else 513#else
473 return fcntl (fd, F_GETFD) != -1; 514 return fcntl (fd, F_GETFD) != -1;
474#endif 515#endif
475} 516}
476 517
477/* called on EBADF to verify fds */ 518/* called on EBADF to verify fds */
478static void 519static void noinline
479fd_ebadf (EV_P) 520fd_ebadf (EV_P)
480{ 521{
481 int fd; 522 int fd;
482 523
483 for (fd = 0; fd < anfdmax; ++fd) 524 for (fd = 0; fd < anfdmax; ++fd)
485 if (!fd_valid (fd) == -1 && errno == EBADF) 526 if (!fd_valid (fd) == -1 && errno == EBADF)
486 fd_kill (EV_A_ fd); 527 fd_kill (EV_A_ fd);
487} 528}
488 529
489/* called on ENOMEM in select/poll to kill some fds and retry */ 530/* called on ENOMEM in select/poll to kill some fds and retry */
490static void 531static void noinline
491fd_enomem (EV_P) 532fd_enomem (EV_P)
492{ 533{
493 int fd; 534 int fd;
494 535
495 for (fd = anfdmax; fd--; ) 536 for (fd = anfdmax; fd--; )
498 fd_kill (EV_A_ fd); 539 fd_kill (EV_A_ fd);
499 return; 540 return;
500 } 541 }
501} 542}
502 543
503/* usually called after fork if method needs to re-arm all fds from scratch */ 544/* usually called after fork if backend needs to re-arm all fds from scratch */
504static void 545static void noinline
505fd_rearm_all (EV_P) 546fd_rearm_all (EV_P)
506{ 547{
507 int fd; 548 int fd;
508 549
509 /* this should be highly optimised to not do anything but set a flag */ 550 /* this should be highly optimised to not do anything but set a flag */
515 } 556 }
516} 557}
517 558
518/*****************************************************************************/ 559/*****************************************************************************/
519 560
520static void 561void inline_speed
521upheap (WT *heap, int k) 562upheap (WT *heap, int k)
522{ 563{
523 WT w = heap [k]; 564 WT w = heap [k];
524 565
525 while (k && heap [k >> 1]->at > w->at) 566 while (k && heap [k >> 1]->at > w->at)
532 heap [k] = w; 573 heap [k] = w;
533 ((W)heap [k])->active = k + 1; 574 ((W)heap [k])->active = k + 1;
534 575
535} 576}
536 577
537static void 578void inline_speed
538downheap (WT *heap, int N, int k) 579downheap (WT *heap, int N, int k)
539{ 580{
540 WT w = heap [k]; 581 WT w = heap [k];
541 582
542 while (k < (N >> 1)) 583 while (k < (N >> 1))
556 597
557 heap [k] = w; 598 heap [k] = w;
558 ((W)heap [k])->active = k + 1; 599 ((W)heap [k])->active = k + 1;
559} 600}
560 601
561inline void 602void inline_size
562adjustheap (WT *heap, int N, int k) 603adjustheap (WT *heap, int N, int k)
563{ 604{
564 upheap (heap, k); 605 upheap (heap, k);
565 downheap (heap, N, k); 606 downheap (heap, N, k);
566} 607}
576static ANSIG *signals; 617static ANSIG *signals;
577static int signalmax; 618static int signalmax;
578 619
579static int sigpipe [2]; 620static int sigpipe [2];
580static sig_atomic_t volatile gotsig; 621static sig_atomic_t volatile gotsig;
581static struct ev_io sigev; 622static ev_io sigev;
582 623
583static void 624void inline_size
584signals_init (ANSIG *base, int count) 625signals_init (ANSIG *base, int count)
585{ 626{
586 while (count--) 627 while (count--)
587 { 628 {
588 base->head = 0; 629 base->head = 0;
608 write (sigpipe [1], &signum, 1); 649 write (sigpipe [1], &signum, 1);
609 errno = old_errno; 650 errno = old_errno;
610 } 651 }
611} 652}
612 653
613void 654void noinline
614ev_feed_signal_event (EV_P_ int signum) 655ev_feed_signal_event (EV_P_ int signum)
615{ 656{
616 WL w; 657 WL w;
617 658
618#if EV_MULTIPLICITY 659#if EV_MULTIPLICITY
619 assert (("feeding signal events is only supported in the default loop", loop == default_loop)); 660 assert (("feeding signal events is only supported in the default loop", loop == ev_default_loop_ptr));
620#endif 661#endif
621 662
622 --signum; 663 --signum;
623 664
624 if (signum < 0 || signum >= signalmax) 665 if (signum < 0 || signum >= signalmax)
629 for (w = signals [signum].head; w; w = w->next) 670 for (w = signals [signum].head; w; w = w->next)
630 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 671 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
631} 672}
632 673
633static void 674static void
634sigcb (EV_P_ struct ev_io *iow, int revents) 675sigcb (EV_P_ ev_io *iow, int revents)
635{ 676{
636 int signum; 677 int signum;
637 678
638 read (sigpipe [0], &revents, 1); 679 read (sigpipe [0], &revents, 1);
639 gotsig = 0; 680 gotsig = 0;
641 for (signum = signalmax; signum--; ) 682 for (signum = signalmax; signum--; )
642 if (signals [signum].gotsig) 683 if (signals [signum].gotsig)
643 ev_feed_signal_event (EV_A_ signum + 1); 684 ev_feed_signal_event (EV_A_ signum + 1);
644} 685}
645 686
646inline void 687void inline_size
647fd_intern (int fd) 688fd_intern (int fd)
648{ 689{
649#ifdef _WIN32 690#ifdef _WIN32
650 int arg = 1; 691 int arg = 1;
651 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 692 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
653 fcntl (fd, F_SETFD, FD_CLOEXEC); 694 fcntl (fd, F_SETFD, FD_CLOEXEC);
654 fcntl (fd, F_SETFL, O_NONBLOCK); 695 fcntl (fd, F_SETFL, O_NONBLOCK);
655#endif 696#endif
656} 697}
657 698
658static void 699static void noinline
659siginit (EV_P) 700siginit (EV_P)
660{ 701{
661 fd_intern (sigpipe [0]); 702 fd_intern (sigpipe [0]);
662 fd_intern (sigpipe [1]); 703 fd_intern (sigpipe [1]);
663 704
666 ev_unref (EV_A); /* child watcher should not keep loop alive */ 707 ev_unref (EV_A); /* child watcher should not keep loop alive */
667} 708}
668 709
669/*****************************************************************************/ 710/*****************************************************************************/
670 711
671static struct ev_child *childs [PID_HASHSIZE]; 712static ev_child *childs [PID_HASHSIZE];
672 713
673#ifndef _WIN32 714#ifndef _WIN32
674 715
675static struct ev_signal childev; 716static ev_signal childev;
676 717
677#ifndef WCONTINUED 718#ifndef WCONTINUED
678# define WCONTINUED 0 719# define WCONTINUED 0
679#endif 720#endif
680 721
681static void 722void inline_speed
682child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) 723child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status)
683{ 724{
684 struct ev_child *w; 725 ev_child *w;
685 726
686 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 727 for (w = (ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
687 if (w->pid == pid || !w->pid) 728 if (w->pid == pid || !w->pid)
688 { 729 {
689 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 730 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
690 w->rpid = pid; 731 w->rpid = pid;
691 w->rstatus = status; 732 w->rstatus = status;
692 ev_feed_event (EV_A_ (W)w, EV_CHILD); 733 ev_feed_event (EV_A_ (W)w, EV_CHILD);
693 } 734 }
694} 735}
695 736
696static void 737static void
697childcb (EV_P_ struct ev_signal *sw, int revents) 738childcb (EV_P_ ev_signal *sw, int revents)
698{ 739{
699 int pid, status; 740 int pid, status;
700 741
701 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 742 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
702 { 743 {
703 /* make sure we are called again until all childs have been reaped */ 744 /* make sure we are called again until all childs have been reaped */
745 /* we need to do it this way so that the callback gets called before we continue */
704 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); 746 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
705 747
706 child_reap (EV_A_ sw, pid, pid, status); 748 child_reap (EV_A_ sw, pid, pid, status);
707 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ 749 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
708 } 750 }
709} 751}
710 752
711#endif 753#endif
712 754
713/*****************************************************************************/ 755/*****************************************************************************/
714 756
757#if EV_USE_PORT
758# include "ev_port.c"
759#endif
715#if EV_USE_KQUEUE 760#if EV_USE_KQUEUE
716# include "ev_kqueue.c" 761# include "ev_kqueue.c"
717#endif 762#endif
718#if EV_USE_EPOLL 763#if EV_USE_EPOLL
719# include "ev_epoll.c" 764# include "ev_epoll.c"
736{ 781{
737 return EV_VERSION_MINOR; 782 return EV_VERSION_MINOR;
738} 783}
739 784
740/* return true if we are running with elevated privileges and should ignore env variables */ 785/* return true if we are running with elevated privileges and should ignore env variables */
741static int 786int inline_size
742enable_secure (void) 787enable_secure (void)
743{ 788{
744#ifdef _WIN32 789#ifdef _WIN32
745 return 0; 790 return 0;
746#else 791#else
748 || getgid () != getegid (); 793 || getgid () != getegid ();
749#endif 794#endif
750} 795}
751 796
752unsigned int 797unsigned int
753ev_method (EV_P) 798ev_supported_backends (void)
754{ 799{
755 return method; 800 unsigned int flags = 0;
801
802 if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
803 if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
804 if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL;
805 if (EV_USE_POLL ) flags |= EVBACKEND_POLL;
806 if (EV_USE_SELECT) flags |= EVBACKEND_SELECT;
807
808 return flags;
809}
810
811unsigned int
812ev_recommended_backends (void)
813{
814 unsigned int flags = ev_supported_backends ();
815
816#ifndef __NetBSD__
817 /* kqueue is borked on everything but netbsd apparently */
818 /* it usually doesn't work correctly on anything but sockets and pipes */
819 flags &= ~EVBACKEND_KQUEUE;
820#endif
821#ifdef __APPLE__
822 // flags &= ~EVBACKEND_KQUEUE; for documentation
823 flags &= ~EVBACKEND_POLL;
824#endif
825
826 return flags;
827}
828
829unsigned int
830ev_embeddable_backends (void)
831{
832 return EVBACKEND_EPOLL
833 | EVBACKEND_KQUEUE
834 | EVBACKEND_PORT;
835}
836
837unsigned int
838ev_backend (EV_P)
839{
840 return backend;
756} 841}
757 842
758static void 843static void
759loop_init (EV_P_ unsigned int flags) 844loop_init (EV_P_ unsigned int flags)
760{ 845{
761 if (!method) 846 if (!backend)
762 { 847 {
763#if EV_USE_MONOTONIC 848#if EV_USE_MONOTONIC
764 { 849 {
765 struct timespec ts; 850 struct timespec ts;
766 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 851 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
771 ev_rt_now = ev_time (); 856 ev_rt_now = ev_time ();
772 mn_now = get_clock (); 857 mn_now = get_clock ();
773 now_floor = mn_now; 858 now_floor = mn_now;
774 rtmn_diff = ev_rt_now - mn_now; 859 rtmn_diff = ev_rt_now - mn_now;
775 860
776 if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS")) 861 if (!(flags & EVFLAG_NOENV)
862 && !enable_secure ()
863 && getenv ("LIBEV_FLAGS"))
777 flags = atoi (getenv ("LIBEV_FLAGS")); 864 flags = atoi (getenv ("LIBEV_FLAGS"));
778 865
779 if (!(flags & 0x0000ffff)) 866 if (!(flags & 0x0000ffffUL))
780 flags |= 0x0000ffff; 867 flags |= ev_recommended_backends ();
781 868
782 method = 0; 869 backend = 0;
870#if EV_USE_PORT
871 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
872#endif
783#if EV_USE_KQUEUE 873#if EV_USE_KQUEUE
784 if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags); 874 if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags);
785#endif 875#endif
786#if EV_USE_EPOLL 876#if EV_USE_EPOLL
787 if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags); 877 if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags);
788#endif 878#endif
789#if EV_USE_POLL 879#if EV_USE_POLL
790 if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags); 880 if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags);
791#endif 881#endif
792#if EV_USE_SELECT 882#if EV_USE_SELECT
793 if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags); 883 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
794#endif 884#endif
795 885
796 ev_init (&sigev, sigcb); 886 ev_init (&sigev, sigcb);
797 ev_set_priority (&sigev, EV_MAXPRI); 887 ev_set_priority (&sigev, EV_MAXPRI);
798 } 888 }
799} 889}
800 890
801void 891static void
802loop_destroy (EV_P) 892loop_destroy (EV_P)
803{ 893{
804 int i; 894 int i;
805 895
896#if EV_USE_PORT
897 if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
898#endif
806#if EV_USE_KQUEUE 899#if EV_USE_KQUEUE
807 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 900 if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A);
808#endif 901#endif
809#if EV_USE_EPOLL 902#if EV_USE_EPOLL
810 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 903 if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A);
811#endif 904#endif
812#if EV_USE_POLL 905#if EV_USE_POLL
813 if (method == EVMETHOD_POLL ) poll_destroy (EV_A); 906 if (backend == EVBACKEND_POLL ) poll_destroy (EV_A);
814#endif 907#endif
815#if EV_USE_SELECT 908#if EV_USE_SELECT
816 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 909 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
817#endif 910#endif
818 911
819 for (i = NUMPRI; i--; ) 912 for (i = NUMPRI; i--; )
820 array_free (pending, [i]); 913 array_free (pending, [i]);
821 914
822 /* have to use the microsoft-never-gets-it-right macro */ 915 /* have to use the microsoft-never-gets-it-right macro */
823 array_free (fdchange, EMPTY0); 916 array_free (fdchange, EMPTY0);
824 array_free (timer, EMPTY0); 917 array_free (timer, EMPTY0);
825#if EV_PERIODICS 918#if EV_PERIODIC_ENABLE
826 array_free (periodic, EMPTY0); 919 array_free (periodic, EMPTY0);
827#endif 920#endif
828 array_free (idle, EMPTY0); 921 array_free (idle, EMPTY0);
829 array_free (prepare, EMPTY0); 922 array_free (prepare, EMPTY0);
830 array_free (check, EMPTY0); 923 array_free (check, EMPTY0);
831 924
832 method = 0; 925 backend = 0;
833} 926}
834 927
835static void 928static void
836loop_fork (EV_P) 929loop_fork (EV_P)
837{ 930{
931#if EV_USE_PORT
932 if (backend == EVBACKEND_PORT ) port_fork (EV_A);
933#endif
934#if EV_USE_KQUEUE
935 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A);
936#endif
838#if EV_USE_EPOLL 937#if EV_USE_EPOLL
839 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 938 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
840#endif
841#if EV_USE_KQUEUE
842 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
843#endif 939#endif
844 940
845 if (ev_is_active (&sigev)) 941 if (ev_is_active (&sigev))
846 { 942 {
847 /* default loop */ 943 /* default loop */
868 964
869 memset (loop, 0, sizeof (struct ev_loop)); 965 memset (loop, 0, sizeof (struct ev_loop));
870 966
871 loop_init (EV_A_ flags); 967 loop_init (EV_A_ flags);
872 968
873 if (ev_method (EV_A)) 969 if (ev_backend (EV_A))
874 return loop; 970 return loop;
875 971
876 return 0; 972 return 0;
877} 973}
878 974
891 987
892#endif 988#endif
893 989
894#if EV_MULTIPLICITY 990#if EV_MULTIPLICITY
895struct ev_loop * 991struct ev_loop *
992ev_default_loop_init (unsigned int flags)
896#else 993#else
897int 994int
898#endif
899ev_default_loop (unsigned int flags) 995ev_default_loop (unsigned int flags)
996#endif
900{ 997{
901 if (sigpipe [0] == sigpipe [1]) 998 if (sigpipe [0] == sigpipe [1])
902 if (pipe (sigpipe)) 999 if (pipe (sigpipe))
903 return 0; 1000 return 0;
904 1001
905 if (!default_loop) 1002 if (!ev_default_loop_ptr)
906 { 1003 {
907#if EV_MULTIPLICITY 1004#if EV_MULTIPLICITY
908 struct ev_loop *loop = default_loop = &default_loop_struct; 1005 struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct;
909#else 1006#else
910 default_loop = 1; 1007 ev_default_loop_ptr = 1;
911#endif 1008#endif
912 1009
913 loop_init (EV_A_ flags); 1010 loop_init (EV_A_ flags);
914 1011
915 if (ev_method (EV_A)) 1012 if (ev_backend (EV_A))
916 { 1013 {
917 siginit (EV_A); 1014 siginit (EV_A);
918 1015
919#ifndef _WIN32 1016#ifndef _WIN32
920 ev_signal_init (&childev, childcb, SIGCHLD); 1017 ev_signal_init (&childev, childcb, SIGCHLD);
922 ev_signal_start (EV_A_ &childev); 1019 ev_signal_start (EV_A_ &childev);
923 ev_unref (EV_A); /* child watcher should not keep loop alive */ 1020 ev_unref (EV_A); /* child watcher should not keep loop alive */
924#endif 1021#endif
925 } 1022 }
926 else 1023 else
927 default_loop = 0; 1024 ev_default_loop_ptr = 0;
928 } 1025 }
929 1026
930 return default_loop; 1027 return ev_default_loop_ptr;
931} 1028}
932 1029
933void 1030void
934ev_default_destroy (void) 1031ev_default_destroy (void)
935{ 1032{
936#if EV_MULTIPLICITY 1033#if EV_MULTIPLICITY
937 struct ev_loop *loop = default_loop; 1034 struct ev_loop *loop = ev_default_loop_ptr;
938#endif 1035#endif
939 1036
940#ifndef _WIN32 1037#ifndef _WIN32
941 ev_ref (EV_A); /* child watcher */ 1038 ev_ref (EV_A); /* child watcher */
942 ev_signal_stop (EV_A_ &childev); 1039 ev_signal_stop (EV_A_ &childev);
953 1050
954void 1051void
955ev_default_fork (void) 1052ev_default_fork (void)
956{ 1053{
957#if EV_MULTIPLICITY 1054#if EV_MULTIPLICITY
958 struct ev_loop *loop = default_loop; 1055 struct ev_loop *loop = ev_default_loop_ptr;
959#endif 1056#endif
960 1057
961 if (method) 1058 if (backend)
962 postfork = 1; 1059 postfork = 1;
963} 1060}
964 1061
965/*****************************************************************************/ 1062/*****************************************************************************/
966 1063
967static int 1064int inline_size
968any_pending (EV_P) 1065any_pending (EV_P)
969{ 1066{
970 int pri; 1067 int pri;
971 1068
972 for (pri = NUMPRI; pri--; ) 1069 for (pri = NUMPRI; pri--; )
974 return 1; 1071 return 1;
975 1072
976 return 0; 1073 return 0;
977} 1074}
978 1075
979static void 1076void inline_speed
980call_pending (EV_P) 1077call_pending (EV_P)
981{ 1078{
982 int pri; 1079 int pri;
983 1080
984 for (pri = NUMPRI; pri--; ) 1081 for (pri = NUMPRI; pri--; )
985 while (pendingcnt [pri]) 1082 while (pendingcnt [pri])
986 { 1083 {
987 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1084 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
988 1085
989 if (p->w) 1086 if (expect_true (p->w))
990 { 1087 {
1088 assert (("non-pending watcher on pending list", p->w->pending));
1089
991 p->w->pending = 0; 1090 p->w->pending = 0;
992 EV_CB_INVOKE (p->w, p->events); 1091 EV_CB_INVOKE (p->w, p->events);
993 } 1092 }
994 } 1093 }
995} 1094}
996 1095
997static void 1096void inline_size
998timers_reify (EV_P) 1097timers_reify (EV_P)
999{ 1098{
1000 while (timercnt && ((WT)timers [0])->at <= mn_now) 1099 while (timercnt && ((WT)timers [0])->at <= mn_now)
1001 { 1100 {
1002 struct ev_timer *w = timers [0]; 1101 ev_timer *w = timers [0];
1003 1102
1004 assert (("inactive timer on timer heap detected", ev_is_active (w))); 1103 assert (("inactive timer on timer heap detected", ev_is_active (w)));
1005 1104
1006 /* first reschedule or stop timer */ 1105 /* first reschedule or stop timer */
1007 if (w->repeat) 1106 if (w->repeat)
1019 1118
1020 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1119 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1021 } 1120 }
1022} 1121}
1023 1122
1024#if EV_PERIODICS 1123#if EV_PERIODIC_ENABLE
1025static void 1124void inline_size
1026periodics_reify (EV_P) 1125periodics_reify (EV_P)
1027{ 1126{
1028 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1127 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1029 { 1128 {
1030 struct ev_periodic *w = periodics [0]; 1129 ev_periodic *w = periodics [0];
1031 1130
1032 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1131 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1033 1132
1034 /* first reschedule or stop timer */ 1133 /* first reschedule or stop timer */
1035 if (w->reschedule_cb) 1134 if (w->reschedule_cb)
1049 1148
1050 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1149 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1051 } 1150 }
1052} 1151}
1053 1152
1054static void 1153static void noinline
1055periodics_reschedule (EV_P) 1154periodics_reschedule (EV_P)
1056{ 1155{
1057 int i; 1156 int i;
1058 1157
1059 /* adjust periodics after time jump */ 1158 /* adjust periodics after time jump */
1060 for (i = 0; i < periodiccnt; ++i) 1159 for (i = 0; i < periodiccnt; ++i)
1061 { 1160 {
1062 struct ev_periodic *w = periodics [i]; 1161 ev_periodic *w = periodics [i];
1063 1162
1064 if (w->reschedule_cb) 1163 if (w->reschedule_cb)
1065 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1164 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1066 else if (w->interval) 1165 else if (w->interval)
1067 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1166 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1071 for (i = periodiccnt >> 1; i--; ) 1170 for (i = periodiccnt >> 1; i--; )
1072 downheap ((WT *)periodics, periodiccnt, i); 1171 downheap ((WT *)periodics, periodiccnt, i);
1073} 1172}
1074#endif 1173#endif
1075 1174
1076inline int 1175int inline_size
1077time_update_monotonic (EV_P) 1176time_update_monotonic (EV_P)
1078{ 1177{
1079 mn_now = get_clock (); 1178 mn_now = get_clock ();
1080 1179
1081 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1180 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1089 ev_rt_now = ev_time (); 1188 ev_rt_now = ev_time ();
1090 return 1; 1189 return 1;
1091 } 1190 }
1092} 1191}
1093 1192
1094static void 1193void inline_size
1095time_update (EV_P) 1194time_update (EV_P)
1096{ 1195{
1097 int i; 1196 int i;
1098 1197
1099#if EV_USE_MONOTONIC 1198#if EV_USE_MONOTONIC
1101 { 1200 {
1102 if (time_update_monotonic (EV_A)) 1201 if (time_update_monotonic (EV_A))
1103 { 1202 {
1104 ev_tstamp odiff = rtmn_diff; 1203 ev_tstamp odiff = rtmn_diff;
1105 1204
1106 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1205 /* loop a few times, before making important decisions.
1206 * on the choice of "4": one iteration isn't enough,
1207 * in case we get preempted during the calls to
1208 * ev_time and get_clock. a second call is almost guarenteed
1209 * to succeed in that case, though. and looping a few more times
1210 * doesn't hurt either as we only do this on time-jumps or
1211 * in the unlikely event of getting preempted here.
1212 */
1213 for (i = 4; --i; )
1107 { 1214 {
1108 rtmn_diff = ev_rt_now - mn_now; 1215 rtmn_diff = ev_rt_now - mn_now;
1109 1216
1110 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1217 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1111 return; /* all is well */ 1218 return; /* all is well */
1113 ev_rt_now = ev_time (); 1220 ev_rt_now = ev_time ();
1114 mn_now = get_clock (); 1221 mn_now = get_clock ();
1115 now_floor = mn_now; 1222 now_floor = mn_now;
1116 } 1223 }
1117 1224
1118# if EV_PERIODICS 1225# if EV_PERIODIC_ENABLE
1119 periodics_reschedule (EV_A); 1226 periodics_reschedule (EV_A);
1120# endif 1227# endif
1121 /* no timer adjustment, as the monotonic clock doesn't jump */ 1228 /* no timer adjustment, as the monotonic clock doesn't jump */
1122 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1229 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1123 } 1230 }
1127 { 1234 {
1128 ev_rt_now = ev_time (); 1235 ev_rt_now = ev_time ();
1129 1236
1130 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1237 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1131 { 1238 {
1132#if EV_PERIODICS 1239#if EV_PERIODIC_ENABLE
1133 periodics_reschedule (EV_A); 1240 periodics_reschedule (EV_A);
1134#endif 1241#endif
1135 1242
1136 /* adjust timers. this is easy, as the offset is the same for all */ 1243 /* adjust timers. this is easy, as the offset is the same for all */
1137 for (i = 0; i < timercnt; ++i) 1244 for (i = 0; i < timercnt; ++i)
1157static int loop_done; 1264static int loop_done;
1158 1265
1159void 1266void
1160ev_loop (EV_P_ int flags) 1267ev_loop (EV_P_ int flags)
1161{ 1268{
1162 double block;
1163 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1269 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1270 ? EVUNLOOP_ONE
1271 : EVUNLOOP_CANCEL;
1164 1272
1165 while (activecnt) 1273 while (activecnt)
1166 { 1274 {
1167 /* queue check watchers (and execute them) */ 1275 /* queue check watchers (and execute them) */
1168 if (expect_false (preparecnt)) 1276 if (expect_false (preparecnt))
1177 1285
1178 /* update fd-related kernel structures */ 1286 /* update fd-related kernel structures */
1179 fd_reify (EV_A); 1287 fd_reify (EV_A);
1180 1288
1181 /* calculate blocking time */ 1289 /* calculate blocking time */
1290 {
1291 double block;
1182 1292
1183 /* we only need this for !monotonic clock or timers, but as we basically 1293 if (flags & EVLOOP_NONBLOCK || idlecnt)
1184 always have timers, we just calculate it always */ 1294 block = 0.; /* do not block at all */
1295 else
1296 {
1297 /* update time to cancel out callback processing overhead */
1185#if EV_USE_MONOTONIC 1298#if EV_USE_MONOTONIC
1186 if (expect_true (have_monotonic)) 1299 if (expect_true (have_monotonic))
1187 time_update_monotonic (EV_A); 1300 time_update_monotonic (EV_A);
1188 else 1301 else
1189#endif 1302#endif
1190 { 1303 {
1191 ev_rt_now = ev_time (); 1304 ev_rt_now = ev_time ();
1192 mn_now = ev_rt_now; 1305 mn_now = ev_rt_now;
1193 } 1306 }
1194 1307
1195 if (flags & EVLOOP_NONBLOCK || idlecnt)
1196 block = 0.;
1197 else
1198 {
1199 block = MAX_BLOCKTIME; 1308 block = MAX_BLOCKTIME;
1200 1309
1201 if (timercnt) 1310 if (timercnt)
1202 { 1311 {
1203 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1312 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
1204 if (block > to) block = to; 1313 if (block > to) block = to;
1205 } 1314 }
1206 1315
1207#if EV_PERIODICS 1316#if EV_PERIODIC_ENABLE
1208 if (periodiccnt) 1317 if (periodiccnt)
1209 { 1318 {
1210 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; 1319 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge;
1211 if (block > to) block = to; 1320 if (block > to) block = to;
1212 } 1321 }
1213#endif 1322#endif
1214 1323
1215 if (block < 0.) block = 0.; 1324 if (expect_false (block < 0.)) block = 0.;
1216 } 1325 }
1217 1326
1218 method_poll (EV_A_ block); 1327 backend_poll (EV_A_ block);
1328 }
1219 1329
1220 /* update ev_rt_now, do magic */ 1330 /* update ev_rt_now, do magic */
1221 time_update (EV_A); 1331 time_update (EV_A);
1222 1332
1223 /* queue pending timers and reschedule them */ 1333 /* queue pending timers and reschedule them */
1224 timers_reify (EV_A); /* relative timers called last */ 1334 timers_reify (EV_A); /* relative timers called last */
1225#if EV_PERIODICS 1335#if EV_PERIODIC_ENABLE
1226 periodics_reify (EV_A); /* absolute timers called first */ 1336 periodics_reify (EV_A); /* absolute timers called first */
1227#endif 1337#endif
1228 1338
1229 /* queue idle watchers unless io or timers are pending */ 1339 /* queue idle watchers unless other events are pending */
1230 if (idlecnt && !any_pending (EV_A)) 1340 if (idlecnt && !any_pending (EV_A))
1231 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1341 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1232 1342
1233 /* queue check watchers, to be executed first */ 1343 /* queue check watchers, to be executed first */
1234 if (checkcnt) 1344 if (expect_false (checkcnt))
1235 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1345 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1236 1346
1237 call_pending (EV_A); 1347 call_pending (EV_A);
1238 1348
1239 if (loop_done) 1349 if (expect_false (loop_done))
1240 break; 1350 break;
1241 } 1351 }
1242 1352
1243 if (loop_done != 2) 1353 if (loop_done == EVUNLOOP_ONE)
1244 loop_done = 0; 1354 loop_done = EVUNLOOP_CANCEL;
1245} 1355}
1246 1356
1247void 1357void
1248ev_unloop (EV_P_ int how) 1358ev_unloop (EV_P_ int how)
1249{ 1359{
1250 loop_done = how; 1360 loop_done = how;
1251} 1361}
1252 1362
1253/*****************************************************************************/ 1363/*****************************************************************************/
1254 1364
1255inline void 1365void inline_size
1256wlist_add (WL *head, WL elem) 1366wlist_add (WL *head, WL elem)
1257{ 1367{
1258 elem->next = *head; 1368 elem->next = *head;
1259 *head = elem; 1369 *head = elem;
1260} 1370}
1261 1371
1262inline void 1372void inline_size
1263wlist_del (WL *head, WL elem) 1373wlist_del (WL *head, WL elem)
1264{ 1374{
1265 while (*head) 1375 while (*head)
1266 { 1376 {
1267 if (*head == elem) 1377 if (*head == elem)
1272 1382
1273 head = &(*head)->next; 1383 head = &(*head)->next;
1274 } 1384 }
1275} 1385}
1276 1386
1277inline void 1387void inline_speed
1278ev_clear_pending (EV_P_ W w) 1388ev_clear_pending (EV_P_ W w)
1279{ 1389{
1280 if (w->pending) 1390 if (w->pending)
1281 { 1391 {
1282 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1392 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1283 w->pending = 0; 1393 w->pending = 0;
1284 } 1394 }
1285} 1395}
1286 1396
1287inline void 1397void inline_speed
1288ev_start (EV_P_ W w, int active) 1398ev_start (EV_P_ W w, int active)
1289{ 1399{
1290 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1400 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
1291 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; 1401 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1292 1402
1293 w->active = active; 1403 w->active = active;
1294 ev_ref (EV_A); 1404 ev_ref (EV_A);
1295} 1405}
1296 1406
1297inline void 1407void inline_size
1298ev_stop (EV_P_ W w) 1408ev_stop (EV_P_ W w)
1299{ 1409{
1300 ev_unref (EV_A); 1410 ev_unref (EV_A);
1301 w->active = 0; 1411 w->active = 0;
1302} 1412}
1303 1413
1304/*****************************************************************************/ 1414/*****************************************************************************/
1305 1415
1306void 1416void
1307ev_io_start (EV_P_ struct ev_io *w) 1417ev_io_start (EV_P_ ev_io *w)
1308{ 1418{
1309 int fd = w->fd; 1419 int fd = w->fd;
1310 1420
1311 if (ev_is_active (w)) 1421 if (expect_false (ev_is_active (w)))
1312 return; 1422 return;
1313 1423
1314 assert (("ev_io_start called with negative fd", fd >= 0)); 1424 assert (("ev_io_start called with negative fd", fd >= 0));
1315 1425
1316 ev_start (EV_A_ (W)w, 1); 1426 ev_start (EV_A_ (W)w, 1);
1319 1429
1320 fd_change (EV_A_ fd); 1430 fd_change (EV_A_ fd);
1321} 1431}
1322 1432
1323void 1433void
1324ev_io_stop (EV_P_ struct ev_io *w) 1434ev_io_stop (EV_P_ ev_io *w)
1325{ 1435{
1326 ev_clear_pending (EV_A_ (W)w); 1436 ev_clear_pending (EV_A_ (W)w);
1327 if (!ev_is_active (w)) 1437 if (expect_false (!ev_is_active (w)))
1328 return; 1438 return;
1329 1439
1330 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 1440 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1331 1441
1332 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1442 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1334 1444
1335 fd_change (EV_A_ w->fd); 1445 fd_change (EV_A_ w->fd);
1336} 1446}
1337 1447
1338void 1448void
1339ev_timer_start (EV_P_ struct ev_timer *w) 1449ev_timer_start (EV_P_ ev_timer *w)
1340{ 1450{
1341 if (ev_is_active (w)) 1451 if (expect_false (ev_is_active (w)))
1342 return; 1452 return;
1343 1453
1344 ((WT)w)->at += mn_now; 1454 ((WT)w)->at += mn_now;
1345 1455
1346 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1456 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1347 1457
1348 ev_start (EV_A_ (W)w, ++timercnt); 1458 ev_start (EV_A_ (W)w, ++timercnt);
1349 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); 1459 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2);
1350 timers [timercnt - 1] = w; 1460 timers [timercnt - 1] = w;
1351 upheap ((WT *)timers, timercnt - 1); 1461 upheap ((WT *)timers, timercnt - 1);
1352 1462
1353 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1463 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1354} 1464}
1355 1465
1356void 1466void
1357ev_timer_stop (EV_P_ struct ev_timer *w) 1467ev_timer_stop (EV_P_ ev_timer *w)
1358{ 1468{
1359 ev_clear_pending (EV_A_ (W)w); 1469 ev_clear_pending (EV_A_ (W)w);
1360 if (!ev_is_active (w)) 1470 if (expect_false (!ev_is_active (w)))
1361 return; 1471 return;
1362 1472
1363 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1473 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1364 1474
1365 if (((W)w)->active < timercnt--) 1475 if (expect_true (((W)w)->active < timercnt--))
1366 { 1476 {
1367 timers [((W)w)->active - 1] = timers [timercnt]; 1477 timers [((W)w)->active - 1] = timers [timercnt];
1368 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1478 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1369 } 1479 }
1370 1480
1372 1482
1373 ev_stop (EV_A_ (W)w); 1483 ev_stop (EV_A_ (W)w);
1374} 1484}
1375 1485
1376void 1486void
1377ev_timer_again (EV_P_ struct ev_timer *w) 1487ev_timer_again (EV_P_ ev_timer *w)
1378{ 1488{
1379 if (ev_is_active (w)) 1489 if (ev_is_active (w))
1380 { 1490 {
1381 if (w->repeat) 1491 if (w->repeat)
1382 { 1492 {
1391 w->at = w->repeat; 1501 w->at = w->repeat;
1392 ev_timer_start (EV_A_ w); 1502 ev_timer_start (EV_A_ w);
1393 } 1503 }
1394} 1504}
1395 1505
1396#if EV_PERIODICS 1506#if EV_PERIODIC_ENABLE
1397void 1507void
1398ev_periodic_start (EV_P_ struct ev_periodic *w) 1508ev_periodic_start (EV_P_ ev_periodic *w)
1399{ 1509{
1400 if (ev_is_active (w)) 1510 if (expect_false (ev_is_active (w)))
1401 return; 1511 return;
1402 1512
1403 if (w->reschedule_cb) 1513 if (w->reschedule_cb)
1404 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1514 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1405 else if (w->interval) 1515 else if (w->interval)
1408 /* this formula differs from the one in periodic_reify because we do not always round up */ 1518 /* this formula differs from the one in periodic_reify because we do not always round up */
1409 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1519 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1410 } 1520 }
1411 1521
1412 ev_start (EV_A_ (W)w, ++periodiccnt); 1522 ev_start (EV_A_ (W)w, ++periodiccnt);
1413 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1523 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1414 periodics [periodiccnt - 1] = w; 1524 periodics [periodiccnt - 1] = w;
1415 upheap ((WT *)periodics, periodiccnt - 1); 1525 upheap ((WT *)periodics, periodiccnt - 1);
1416 1526
1417 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1527 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1418} 1528}
1419 1529
1420void 1530void
1421ev_periodic_stop (EV_P_ struct ev_periodic *w) 1531ev_periodic_stop (EV_P_ ev_periodic *w)
1422{ 1532{
1423 ev_clear_pending (EV_A_ (W)w); 1533 ev_clear_pending (EV_A_ (W)w);
1424 if (!ev_is_active (w)) 1534 if (expect_false (!ev_is_active (w)))
1425 return; 1535 return;
1426 1536
1427 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1537 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1428 1538
1429 if (((W)w)->active < periodiccnt--) 1539 if (expect_true (((W)w)->active < periodiccnt--))
1430 { 1540 {
1431 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1541 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1432 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1542 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1433 } 1543 }
1434 1544
1435 ev_stop (EV_A_ (W)w); 1545 ev_stop (EV_A_ (W)w);
1436} 1546}
1437 1547
1438void 1548void
1439ev_periodic_again (EV_P_ struct ev_periodic *w) 1549ev_periodic_again (EV_P_ ev_periodic *w)
1440{ 1550{
1441 /* TODO: use adjustheap and recalculation */ 1551 /* TODO: use adjustheap and recalculation */
1442 ev_periodic_stop (EV_A_ w); 1552 ev_periodic_stop (EV_A_ w);
1443 ev_periodic_start (EV_A_ w); 1553 ev_periodic_start (EV_A_ w);
1444} 1554}
1445#endif 1555#endif
1446 1556
1447void 1557void
1448ev_idle_start (EV_P_ struct ev_idle *w) 1558ev_idle_start (EV_P_ ev_idle *w)
1449{ 1559{
1450 if (ev_is_active (w)) 1560 if (expect_false (ev_is_active (w)))
1451 return; 1561 return;
1452 1562
1453 ev_start (EV_A_ (W)w, ++idlecnt); 1563 ev_start (EV_A_ (W)w, ++idlecnt);
1454 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); 1564 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1455 idles [idlecnt - 1] = w; 1565 idles [idlecnt - 1] = w;
1456} 1566}
1457 1567
1458void 1568void
1459ev_idle_stop (EV_P_ struct ev_idle *w) 1569ev_idle_stop (EV_P_ ev_idle *w)
1460{ 1570{
1461 ev_clear_pending (EV_A_ (W)w); 1571 ev_clear_pending (EV_A_ (W)w);
1462 if (!ev_is_active (w)) 1572 if (expect_false (!ev_is_active (w)))
1463 return; 1573 return;
1464 1574
1575 {
1576 int active = ((W)w)->active;
1465 idles [((W)w)->active - 1] = idles [--idlecnt]; 1577 idles [active - 1] = idles [--idlecnt];
1578 ((W)idles [active - 1])->active = active;
1579 }
1580
1466 ev_stop (EV_A_ (W)w); 1581 ev_stop (EV_A_ (W)w);
1467} 1582}
1468 1583
1469void 1584void
1470ev_prepare_start (EV_P_ struct ev_prepare *w) 1585ev_prepare_start (EV_P_ ev_prepare *w)
1471{ 1586{
1472 if (ev_is_active (w)) 1587 if (expect_false (ev_is_active (w)))
1473 return; 1588 return;
1474 1589
1475 ev_start (EV_A_ (W)w, ++preparecnt); 1590 ev_start (EV_A_ (W)w, ++preparecnt);
1476 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); 1591 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1477 prepares [preparecnt - 1] = w; 1592 prepares [preparecnt - 1] = w;
1478} 1593}
1479 1594
1480void 1595void
1481ev_prepare_stop (EV_P_ struct ev_prepare *w) 1596ev_prepare_stop (EV_P_ ev_prepare *w)
1482{ 1597{
1483 ev_clear_pending (EV_A_ (W)w); 1598 ev_clear_pending (EV_A_ (W)w);
1484 if (!ev_is_active (w)) 1599 if (expect_false (!ev_is_active (w)))
1485 return; 1600 return;
1486 1601
1602 {
1603 int active = ((W)w)->active;
1487 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1604 prepares [active - 1] = prepares [--preparecnt];
1605 ((W)prepares [active - 1])->active = active;
1606 }
1607
1488 ev_stop (EV_A_ (W)w); 1608 ev_stop (EV_A_ (W)w);
1489} 1609}
1490 1610
1491void 1611void
1492ev_check_start (EV_P_ struct ev_check *w) 1612ev_check_start (EV_P_ ev_check *w)
1493{ 1613{
1494 if (ev_is_active (w)) 1614 if (expect_false (ev_is_active (w)))
1495 return; 1615 return;
1496 1616
1497 ev_start (EV_A_ (W)w, ++checkcnt); 1617 ev_start (EV_A_ (W)w, ++checkcnt);
1498 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); 1618 array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
1499 checks [checkcnt - 1] = w; 1619 checks [checkcnt - 1] = w;
1500} 1620}
1501 1621
1502void 1622void
1503ev_check_stop (EV_P_ struct ev_check *w) 1623ev_check_stop (EV_P_ ev_check *w)
1504{ 1624{
1505 ev_clear_pending (EV_A_ (W)w); 1625 ev_clear_pending (EV_A_ (W)w);
1506 if (!ev_is_active (w)) 1626 if (expect_false (!ev_is_active (w)))
1507 return; 1627 return;
1508 1628
1629 {
1630 int active = ((W)w)->active;
1509 checks [((W)w)->active - 1] = checks [--checkcnt]; 1631 checks [active - 1] = checks [--checkcnt];
1632 ((W)checks [active - 1])->active = active;
1633 }
1634
1510 ev_stop (EV_A_ (W)w); 1635 ev_stop (EV_A_ (W)w);
1511} 1636}
1512 1637
1513#ifndef SA_RESTART 1638#ifndef SA_RESTART
1514# define SA_RESTART 0 1639# define SA_RESTART 0
1515#endif 1640#endif
1516 1641
1517void 1642void
1518ev_signal_start (EV_P_ struct ev_signal *w) 1643ev_signal_start (EV_P_ ev_signal *w)
1519{ 1644{
1520#if EV_MULTIPLICITY 1645#if EV_MULTIPLICITY
1521 assert (("signal watchers are only supported in the default loop", loop == default_loop)); 1646 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1522#endif 1647#endif
1523 if (ev_is_active (w)) 1648 if (expect_false (ev_is_active (w)))
1524 return; 1649 return;
1525 1650
1526 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1651 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1527 1652
1528 ev_start (EV_A_ (W)w, 1); 1653 ev_start (EV_A_ (W)w, 1);
1542#endif 1667#endif
1543 } 1668 }
1544} 1669}
1545 1670
1546void 1671void
1547ev_signal_stop (EV_P_ struct ev_signal *w) 1672ev_signal_stop (EV_P_ ev_signal *w)
1548{ 1673{
1549 ev_clear_pending (EV_A_ (W)w); 1674 ev_clear_pending (EV_A_ (W)w);
1550 if (!ev_is_active (w)) 1675 if (expect_false (!ev_is_active (w)))
1551 return; 1676 return;
1552 1677
1553 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1678 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1554 ev_stop (EV_A_ (W)w); 1679 ev_stop (EV_A_ (W)w);
1555 1680
1556 if (!signals [w->signum - 1].head) 1681 if (!signals [w->signum - 1].head)
1557 signal (w->signum, SIG_DFL); 1682 signal (w->signum, SIG_DFL);
1558} 1683}
1559 1684
1560void 1685void
1561ev_child_start (EV_P_ struct ev_child *w) 1686ev_child_start (EV_P_ ev_child *w)
1562{ 1687{
1563#if EV_MULTIPLICITY 1688#if EV_MULTIPLICITY
1564 assert (("child watchers are only supported in the default loop", loop == default_loop)); 1689 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1565#endif 1690#endif
1566 if (ev_is_active (w)) 1691 if (expect_false (ev_is_active (w)))
1567 return; 1692 return;
1568 1693
1569 ev_start (EV_A_ (W)w, 1); 1694 ev_start (EV_A_ (W)w, 1);
1570 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1695 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1571} 1696}
1572 1697
1573void 1698void
1574ev_child_stop (EV_P_ struct ev_child *w) 1699ev_child_stop (EV_P_ ev_child *w)
1575{ 1700{
1576 ev_clear_pending (EV_A_ (W)w); 1701 ev_clear_pending (EV_A_ (W)w);
1577 if (!ev_is_active (w)) 1702 if (expect_false (!ev_is_active (w)))
1578 return; 1703 return;
1579 1704
1580 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1705 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1581 ev_stop (EV_A_ (W)w); 1706 ev_stop (EV_A_ (W)w);
1582} 1707}
1583 1708
1709#if EV_EMBED_ENABLE
1710void noinline
1711ev_embed_sweep (EV_P_ ev_embed *w)
1712{
1713 ev_loop (w->loop, EVLOOP_NONBLOCK);
1714}
1715
1716static void
1717embed_cb (EV_P_ ev_io *io, int revents)
1718{
1719 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
1720
1721 if (ev_cb (w))
1722 ev_feed_event (EV_A_ (W)w, EV_EMBED);
1723 else
1724 ev_embed_sweep (loop, w);
1725}
1726
1727void
1728ev_embed_start (EV_P_ ev_embed *w)
1729{
1730 if (expect_false (ev_is_active (w)))
1731 return;
1732
1733 {
1734 struct ev_loop *loop = w->loop;
1735 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
1736 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ);
1737 }
1738
1739 ev_set_priority (&w->io, ev_priority (w));
1740 ev_io_start (EV_A_ &w->io);
1741
1742 ev_start (EV_A_ (W)w, 1);
1743}
1744
1745void
1746ev_embed_stop (EV_P_ ev_embed *w)
1747{
1748 ev_clear_pending (EV_A_ (W)w);
1749 if (expect_false (!ev_is_active (w)))
1750 return;
1751
1752 ev_io_stop (EV_A_ &w->io);
1753
1754 ev_stop (EV_A_ (W)w);
1755}
1756#endif
1757
1758#if EV_STAT_ENABLE
1759
1760# ifdef _WIN32
1761# define lstat(a,b) stat(a,b)
1762# endif
1763
1764void
1765ev_stat_stat (EV_P_ ev_stat *w)
1766{
1767 if (lstat (w->path, &w->attr) < 0)
1768 w->attr.st_nlink = 0;
1769 else if (!w->attr.st_nlink)
1770 w->attr.st_nlink = 1;
1771}
1772
1773static void
1774stat_timer_cb (EV_P_ ev_timer *w_, int revents)
1775{
1776 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
1777
1778 /* we copy this here each the time so that */
1779 /* prev has the old value when the callback gets invoked */
1780 w->prev = w->attr;
1781 ev_stat_stat (EV_A_ w);
1782
1783 if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata)))
1784 ev_feed_event (EV_A_ w, EV_STAT);
1785}
1786
1787void
1788ev_stat_start (EV_P_ ev_stat *w)
1789{
1790 if (expect_false (ev_is_active (w)))
1791 return;
1792
1793 /* since we use memcmp, we need to clear any padding data etc. */
1794 memset (&w->prev, 0, sizeof (ev_statdata));
1795 memset (&w->attr, 0, sizeof (ev_statdata));
1796
1797 ev_stat_stat (EV_A_ w);
1798
1799 ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval);
1800 ev_set_priority (&w->timer, ev_priority (w));
1801 ev_timer_start (EV_A_ &w->timer);
1802
1803 ev_start (EV_A_ (W)w, 1);
1804}
1805
1806void
1807ev_stat_stop (EV_P_ ev_stat *w)
1808{
1809 ev_clear_pending (EV_A_ (W)w);
1810 if (expect_false (!ev_is_active (w)))
1811 return;
1812
1813 ev_timer_stop (EV_A_ &w->timer);
1814
1815 ev_stop (EV_A_ (W)w);
1816}
1817#endif
1818
1584/*****************************************************************************/ 1819/*****************************************************************************/
1585 1820
1586struct ev_once 1821struct ev_once
1587{ 1822{
1588 struct ev_io io; 1823 ev_io io;
1589 struct ev_timer to; 1824 ev_timer to;
1590 void (*cb)(int revents, void *arg); 1825 void (*cb)(int revents, void *arg);
1591 void *arg; 1826 void *arg;
1592}; 1827};
1593 1828
1594static void 1829static void
1603 1838
1604 cb (revents, arg); 1839 cb (revents, arg);
1605} 1840}
1606 1841
1607static void 1842static void
1608once_cb_io (EV_P_ struct ev_io *w, int revents) 1843once_cb_io (EV_P_ ev_io *w, int revents)
1609{ 1844{
1610 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); 1845 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1611} 1846}
1612 1847
1613static void 1848static void
1614once_cb_to (EV_P_ struct ev_timer *w, int revents) 1849once_cb_to (EV_P_ ev_timer *w, int revents)
1615{ 1850{
1616 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); 1851 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1617} 1852}
1618 1853
1619void 1854void
1620ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1855ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1621{ 1856{
1622 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); 1857 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
1623 1858
1624 if (!once) 1859 if (expect_false (!once))
1860 {
1625 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1861 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1626 else 1862 return;
1627 { 1863 }
1864
1628 once->cb = cb; 1865 once->cb = cb;
1629 once->arg = arg; 1866 once->arg = arg;
1630 1867
1631 ev_init (&once->io, once_cb_io); 1868 ev_init (&once->io, once_cb_io);
1632 if (fd >= 0) 1869 if (fd >= 0)
1633 { 1870 {
1634 ev_io_set (&once->io, fd, events); 1871 ev_io_set (&once->io, fd, events);
1635 ev_io_start (EV_A_ &once->io); 1872 ev_io_start (EV_A_ &once->io);
1636 } 1873 }
1637 1874
1638 ev_init (&once->to, once_cb_to); 1875 ev_init (&once->to, once_cb_to);
1639 if (timeout >= 0.) 1876 if (timeout >= 0.)
1640 { 1877 {
1641 ev_timer_set (&once->to, timeout, 0.); 1878 ev_timer_set (&once->to, timeout, 0.);
1642 ev_timer_start (EV_A_ &once->to); 1879 ev_timer_start (EV_A_ &once->to);
1643 }
1644 } 1880 }
1645} 1881}
1646 1882
1647#ifdef __cplusplus 1883#ifdef __cplusplus
1648} 1884}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines