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

Comparing libev/ev.c (file contents):
Revision 1.117 by ayin, Thu Nov 15 17:15:56 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"
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
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 *
896ev_default_loop_ (unsigned int flags) 992ev_default_loop_init (unsigned int flags)
897#else 993#else
898int 994int
899ev_default_loop (unsigned int flags) 995ev_default_loop (unsigned int flags)
900#endif 996#endif
901{ 997{
911 ev_default_loop_ptr = 1; 1007 ev_default_loop_ptr = 1;
912#endif 1008#endif
913 1009
914 loop_init (EV_A_ flags); 1010 loop_init (EV_A_ flags);
915 1011
916 if (ev_method (EV_A)) 1012 if (ev_backend (EV_A))
917 { 1013 {
918 siginit (EV_A); 1014 siginit (EV_A);
919 1015
920#ifndef _WIN32 1016#ifndef _WIN32
921 ev_signal_init (&childev, childcb, SIGCHLD); 1017 ev_signal_init (&childev, childcb, SIGCHLD);
957{ 1053{
958#if EV_MULTIPLICITY 1054#if EV_MULTIPLICITY
959 struct ev_loop *loop = ev_default_loop_ptr; 1055 struct ev_loop *loop = ev_default_loop_ptr;
960#endif 1056#endif
961 1057
962 if (method) 1058 if (backend)
963 postfork = 1; 1059 postfork = 1;
964} 1060}
965 1061
966/*****************************************************************************/ 1062/*****************************************************************************/
967 1063
968static int 1064int inline_size
969any_pending (EV_P) 1065any_pending (EV_P)
970{ 1066{
971 int pri; 1067 int pri;
972 1068
973 for (pri = NUMPRI; pri--; ) 1069 for (pri = NUMPRI; pri--; )
975 return 1; 1071 return 1;
976 1072
977 return 0; 1073 return 0;
978} 1074}
979 1075
980static void 1076void inline_speed
981call_pending (EV_P) 1077call_pending (EV_P)
982{ 1078{
983 int pri; 1079 int pri;
984 1080
985 for (pri = NUMPRI; pri--; ) 1081 for (pri = NUMPRI; pri--; )
986 while (pendingcnt [pri]) 1082 while (pendingcnt [pri])
987 { 1083 {
988 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1084 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
989 1085
990 if (p->w) 1086 if (expect_true (p->w))
991 { 1087 {
1088 assert (("non-pending watcher on pending list", p->w->pending));
1089
992 p->w->pending = 0; 1090 p->w->pending = 0;
993 EV_CB_INVOKE (p->w, p->events); 1091 EV_CB_INVOKE (p->w, p->events);
994 } 1092 }
995 } 1093 }
996} 1094}
997 1095
998static void 1096void inline_size
999timers_reify (EV_P) 1097timers_reify (EV_P)
1000{ 1098{
1001 while (timercnt && ((WT)timers [0])->at <= mn_now) 1099 while (timercnt && ((WT)timers [0])->at <= mn_now)
1002 { 1100 {
1003 struct ev_timer *w = timers [0]; 1101 ev_timer *w = timers [0];
1004 1102
1005 assert (("inactive timer on timer heap detected", ev_is_active (w))); 1103 assert (("inactive timer on timer heap detected", ev_is_active (w)));
1006 1104
1007 /* first reschedule or stop timer */ 1105 /* first reschedule or stop timer */
1008 if (w->repeat) 1106 if (w->repeat)
1020 1118
1021 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1119 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1022 } 1120 }
1023} 1121}
1024 1122
1025#if EV_PERIODICS 1123#if EV_PERIODIC_ENABLE
1026static void 1124void inline_size
1027periodics_reify (EV_P) 1125periodics_reify (EV_P)
1028{ 1126{
1029 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1127 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1030 { 1128 {
1031 struct ev_periodic *w = periodics [0]; 1129 ev_periodic *w = periodics [0];
1032 1130
1033 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1131 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1034 1132
1035 /* first reschedule or stop timer */ 1133 /* first reschedule or stop timer */
1036 if (w->reschedule_cb) 1134 if (w->reschedule_cb)
1050 1148
1051 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1149 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1052 } 1150 }
1053} 1151}
1054 1152
1055static void 1153static void noinline
1056periodics_reschedule (EV_P) 1154periodics_reschedule (EV_P)
1057{ 1155{
1058 int i; 1156 int i;
1059 1157
1060 /* adjust periodics after time jump */ 1158 /* adjust periodics after time jump */
1061 for (i = 0; i < periodiccnt; ++i) 1159 for (i = 0; i < periodiccnt; ++i)
1062 { 1160 {
1063 struct ev_periodic *w = periodics [i]; 1161 ev_periodic *w = periodics [i];
1064 1162
1065 if (w->reschedule_cb) 1163 if (w->reschedule_cb)
1066 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1164 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1067 else if (w->interval) 1165 else if (w->interval)
1068 ((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;
1072 for (i = periodiccnt >> 1; i--; ) 1170 for (i = periodiccnt >> 1; i--; )
1073 downheap ((WT *)periodics, periodiccnt, i); 1171 downheap ((WT *)periodics, periodiccnt, i);
1074} 1172}
1075#endif 1173#endif
1076 1174
1077inline int 1175int inline_size
1078time_update_monotonic (EV_P) 1176time_update_monotonic (EV_P)
1079{ 1177{
1080 mn_now = get_clock (); 1178 mn_now = get_clock ();
1081 1179
1082 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1180 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1090 ev_rt_now = ev_time (); 1188 ev_rt_now = ev_time ();
1091 return 1; 1189 return 1;
1092 } 1190 }
1093} 1191}
1094 1192
1095static void 1193void inline_size
1096time_update (EV_P) 1194time_update (EV_P)
1097{ 1195{
1098 int i; 1196 int i;
1099 1197
1100#if EV_USE_MONOTONIC 1198#if EV_USE_MONOTONIC
1102 { 1200 {
1103 if (time_update_monotonic (EV_A)) 1201 if (time_update_monotonic (EV_A))
1104 { 1202 {
1105 ev_tstamp odiff = rtmn_diff; 1203 ev_tstamp odiff = rtmn_diff;
1106 1204
1107 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; )
1108 { 1214 {
1109 rtmn_diff = ev_rt_now - mn_now; 1215 rtmn_diff = ev_rt_now - mn_now;
1110 1216
1111 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1217 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1112 return; /* all is well */ 1218 return; /* all is well */
1114 ev_rt_now = ev_time (); 1220 ev_rt_now = ev_time ();
1115 mn_now = get_clock (); 1221 mn_now = get_clock ();
1116 now_floor = mn_now; 1222 now_floor = mn_now;
1117 } 1223 }
1118 1224
1119# if EV_PERIODICS 1225# if EV_PERIODIC_ENABLE
1120 periodics_reschedule (EV_A); 1226 periodics_reschedule (EV_A);
1121# endif 1227# endif
1122 /* no timer adjustment, as the monotonic clock doesn't jump */ 1228 /* no timer adjustment, as the monotonic clock doesn't jump */
1123 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1229 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1124 } 1230 }
1128 { 1234 {
1129 ev_rt_now = ev_time (); 1235 ev_rt_now = ev_time ();
1130 1236
1131 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))
1132 { 1238 {
1133#if EV_PERIODICS 1239#if EV_PERIODIC_ENABLE
1134 periodics_reschedule (EV_A); 1240 periodics_reschedule (EV_A);
1135#endif 1241#endif
1136 1242
1137 /* 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 */
1138 for (i = 0; i < timercnt; ++i) 1244 for (i = 0; i < timercnt; ++i)
1158static int loop_done; 1264static int loop_done;
1159 1265
1160void 1266void
1161ev_loop (EV_P_ int flags) 1267ev_loop (EV_P_ int flags)
1162{ 1268{
1163 double block;
1164 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1269 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1270 ? EVUNLOOP_ONE
1271 : EVUNLOOP_CANCEL;
1165 1272
1166 while (activecnt) 1273 while (activecnt)
1167 { 1274 {
1168 /* queue check watchers (and execute them) */ 1275 /* queue check watchers (and execute them) */
1169 if (expect_false (preparecnt)) 1276 if (expect_false (preparecnt))
1178 1285
1179 /* update fd-related kernel structures */ 1286 /* update fd-related kernel structures */
1180 fd_reify (EV_A); 1287 fd_reify (EV_A);
1181 1288
1182 /* calculate blocking time */ 1289 /* calculate blocking time */
1290 {
1291 double block;
1183 1292
1184 /* we only need this for !monotonic clock or timers, but as we basically 1293 if (flags & EVLOOP_NONBLOCK || idlecnt)
1185 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 */
1186#if EV_USE_MONOTONIC 1298#if EV_USE_MONOTONIC
1187 if (expect_true (have_monotonic)) 1299 if (expect_true (have_monotonic))
1188 time_update_monotonic (EV_A); 1300 time_update_monotonic (EV_A);
1189 else 1301 else
1190#endif 1302#endif
1191 { 1303 {
1192 ev_rt_now = ev_time (); 1304 ev_rt_now = ev_time ();
1193 mn_now = ev_rt_now; 1305 mn_now = ev_rt_now;
1194 } 1306 }
1195 1307
1196 if (flags & EVLOOP_NONBLOCK || idlecnt)
1197 block = 0.;
1198 else
1199 {
1200 block = MAX_BLOCKTIME; 1308 block = MAX_BLOCKTIME;
1201 1309
1202 if (timercnt) 1310 if (timercnt)
1203 { 1311 {
1204 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1312 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
1205 if (block > to) block = to; 1313 if (block > to) block = to;
1206 } 1314 }
1207 1315
1208#if EV_PERIODICS 1316#if EV_PERIODIC_ENABLE
1209 if (periodiccnt) 1317 if (periodiccnt)
1210 { 1318 {
1211 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;
1212 if (block > to) block = to; 1320 if (block > to) block = to;
1213 } 1321 }
1214#endif 1322#endif
1215 1323
1216 if (block < 0.) block = 0.; 1324 if (expect_false (block < 0.)) block = 0.;
1217 } 1325 }
1218 1326
1219 method_poll (EV_A_ block); 1327 backend_poll (EV_A_ block);
1328 }
1220 1329
1221 /* update ev_rt_now, do magic */ 1330 /* update ev_rt_now, do magic */
1222 time_update (EV_A); 1331 time_update (EV_A);
1223 1332
1224 /* queue pending timers and reschedule them */ 1333 /* queue pending timers and reschedule them */
1225 timers_reify (EV_A); /* relative timers called last */ 1334 timers_reify (EV_A); /* relative timers called last */
1226#if EV_PERIODICS 1335#if EV_PERIODIC_ENABLE
1227 periodics_reify (EV_A); /* absolute timers called first */ 1336 periodics_reify (EV_A); /* absolute timers called first */
1228#endif 1337#endif
1229 1338
1230 /* queue idle watchers unless io or timers are pending */ 1339 /* queue idle watchers unless other events are pending */
1231 if (idlecnt && !any_pending (EV_A)) 1340 if (idlecnt && !any_pending (EV_A))
1232 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1341 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1233 1342
1234 /* queue check watchers, to be executed first */ 1343 /* queue check watchers, to be executed first */
1235 if (checkcnt) 1344 if (expect_false (checkcnt))
1236 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1345 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1237 1346
1238 call_pending (EV_A); 1347 call_pending (EV_A);
1239 1348
1240 if (loop_done) 1349 if (expect_false (loop_done))
1241 break; 1350 break;
1242 } 1351 }
1243 1352
1244 if (loop_done != 2) 1353 if (loop_done == EVUNLOOP_ONE)
1245 loop_done = 0; 1354 loop_done = EVUNLOOP_CANCEL;
1246} 1355}
1247 1356
1248void 1357void
1249ev_unloop (EV_P_ int how) 1358ev_unloop (EV_P_ int how)
1250{ 1359{
1251 loop_done = how; 1360 loop_done = how;
1252} 1361}
1253 1362
1254/*****************************************************************************/ 1363/*****************************************************************************/
1255 1364
1256inline void 1365void inline_size
1257wlist_add (WL *head, WL elem) 1366wlist_add (WL *head, WL elem)
1258{ 1367{
1259 elem->next = *head; 1368 elem->next = *head;
1260 *head = elem; 1369 *head = elem;
1261} 1370}
1262 1371
1263inline void 1372void inline_size
1264wlist_del (WL *head, WL elem) 1373wlist_del (WL *head, WL elem)
1265{ 1374{
1266 while (*head) 1375 while (*head)
1267 { 1376 {
1268 if (*head == elem) 1377 if (*head == elem)
1273 1382
1274 head = &(*head)->next; 1383 head = &(*head)->next;
1275 } 1384 }
1276} 1385}
1277 1386
1278inline void 1387void inline_speed
1279ev_clear_pending (EV_P_ W w) 1388ev_clear_pending (EV_P_ W w)
1280{ 1389{
1281 if (w->pending) 1390 if (w->pending)
1282 { 1391 {
1283 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1392 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1284 w->pending = 0; 1393 w->pending = 0;
1285 } 1394 }
1286} 1395}
1287 1396
1288inline void 1397void inline_speed
1289ev_start (EV_P_ W w, int active) 1398ev_start (EV_P_ W w, int active)
1290{ 1399{
1291 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1400 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
1292 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; 1401 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1293 1402
1294 w->active = active; 1403 w->active = active;
1295 ev_ref (EV_A); 1404 ev_ref (EV_A);
1296} 1405}
1297 1406
1298inline void 1407void inline_size
1299ev_stop (EV_P_ W w) 1408ev_stop (EV_P_ W w)
1300{ 1409{
1301 ev_unref (EV_A); 1410 ev_unref (EV_A);
1302 w->active = 0; 1411 w->active = 0;
1303} 1412}
1304 1413
1305/*****************************************************************************/ 1414/*****************************************************************************/
1306 1415
1307void 1416void
1308ev_io_start (EV_P_ struct ev_io *w) 1417ev_io_start (EV_P_ ev_io *w)
1309{ 1418{
1310 int fd = w->fd; 1419 int fd = w->fd;
1311 1420
1312 if (ev_is_active (w)) 1421 if (expect_false (ev_is_active (w)))
1313 return; 1422 return;
1314 1423
1315 assert (("ev_io_start called with negative fd", fd >= 0)); 1424 assert (("ev_io_start called with negative fd", fd >= 0));
1316 1425
1317 ev_start (EV_A_ (W)w, 1); 1426 ev_start (EV_A_ (W)w, 1);
1320 1429
1321 fd_change (EV_A_ fd); 1430 fd_change (EV_A_ fd);
1322} 1431}
1323 1432
1324void 1433void
1325ev_io_stop (EV_P_ struct ev_io *w) 1434ev_io_stop (EV_P_ ev_io *w)
1326{ 1435{
1327 ev_clear_pending (EV_A_ (W)w); 1436 ev_clear_pending (EV_A_ (W)w);
1328 if (!ev_is_active (w)) 1437 if (expect_false (!ev_is_active (w)))
1329 return; 1438 return;
1330 1439
1331 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));
1332 1441
1333 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1442 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1335 1444
1336 fd_change (EV_A_ w->fd); 1445 fd_change (EV_A_ w->fd);
1337} 1446}
1338 1447
1339void 1448void
1340ev_timer_start (EV_P_ struct ev_timer *w) 1449ev_timer_start (EV_P_ ev_timer *w)
1341{ 1450{
1342 if (ev_is_active (w)) 1451 if (expect_false (ev_is_active (w)))
1343 return; 1452 return;
1344 1453
1345 ((WT)w)->at += mn_now; 1454 ((WT)w)->at += mn_now;
1346 1455
1347 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.));
1348 1457
1349 ev_start (EV_A_ (W)w, ++timercnt); 1458 ev_start (EV_A_ (W)w, ++timercnt);
1350 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); 1459 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2);
1351 timers [timercnt - 1] = w; 1460 timers [timercnt - 1] = w;
1352 upheap ((WT *)timers, timercnt - 1); 1461 upheap ((WT *)timers, timercnt - 1);
1353 1462
1354 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1463 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1355} 1464}
1356 1465
1357void 1466void
1358ev_timer_stop (EV_P_ struct ev_timer *w) 1467ev_timer_stop (EV_P_ ev_timer *w)
1359{ 1468{
1360 ev_clear_pending (EV_A_ (W)w); 1469 ev_clear_pending (EV_A_ (W)w);
1361 if (!ev_is_active (w)) 1470 if (expect_false (!ev_is_active (w)))
1362 return; 1471 return;
1363 1472
1364 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1473 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1365 1474
1366 if (((W)w)->active < timercnt--) 1475 if (expect_true (((W)w)->active < timercnt--))
1367 { 1476 {
1368 timers [((W)w)->active - 1] = timers [timercnt]; 1477 timers [((W)w)->active - 1] = timers [timercnt];
1369 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1478 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1370 } 1479 }
1371 1480
1373 1482
1374 ev_stop (EV_A_ (W)w); 1483 ev_stop (EV_A_ (W)w);
1375} 1484}
1376 1485
1377void 1486void
1378ev_timer_again (EV_P_ struct ev_timer *w) 1487ev_timer_again (EV_P_ ev_timer *w)
1379{ 1488{
1380 if (ev_is_active (w)) 1489 if (ev_is_active (w))
1381 { 1490 {
1382 if (w->repeat) 1491 if (w->repeat)
1383 { 1492 {
1392 w->at = w->repeat; 1501 w->at = w->repeat;
1393 ev_timer_start (EV_A_ w); 1502 ev_timer_start (EV_A_ w);
1394 } 1503 }
1395} 1504}
1396 1505
1397#if EV_PERIODICS 1506#if EV_PERIODIC_ENABLE
1398void 1507void
1399ev_periodic_start (EV_P_ struct ev_periodic *w) 1508ev_periodic_start (EV_P_ ev_periodic *w)
1400{ 1509{
1401 if (ev_is_active (w)) 1510 if (expect_false (ev_is_active (w)))
1402 return; 1511 return;
1403 1512
1404 if (w->reschedule_cb) 1513 if (w->reschedule_cb)
1405 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1514 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1406 else if (w->interval) 1515 else if (w->interval)
1409 /* 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 */
1410 ((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;
1411 } 1520 }
1412 1521
1413 ev_start (EV_A_ (W)w, ++periodiccnt); 1522 ev_start (EV_A_ (W)w, ++periodiccnt);
1414 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1523 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1415 periodics [periodiccnt - 1] = w; 1524 periodics [periodiccnt - 1] = w;
1416 upheap ((WT *)periodics, periodiccnt - 1); 1525 upheap ((WT *)periodics, periodiccnt - 1);
1417 1526
1418 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1527 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1419} 1528}
1420 1529
1421void 1530void
1422ev_periodic_stop (EV_P_ struct ev_periodic *w) 1531ev_periodic_stop (EV_P_ ev_periodic *w)
1423{ 1532{
1424 ev_clear_pending (EV_A_ (W)w); 1533 ev_clear_pending (EV_A_ (W)w);
1425 if (!ev_is_active (w)) 1534 if (expect_false (!ev_is_active (w)))
1426 return; 1535 return;
1427 1536
1428 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1537 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1429 1538
1430 if (((W)w)->active < periodiccnt--) 1539 if (expect_true (((W)w)->active < periodiccnt--))
1431 { 1540 {
1432 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1541 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1433 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1542 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1434 } 1543 }
1435 1544
1436 ev_stop (EV_A_ (W)w); 1545 ev_stop (EV_A_ (W)w);
1437} 1546}
1438 1547
1439void 1548void
1440ev_periodic_again (EV_P_ struct ev_periodic *w) 1549ev_periodic_again (EV_P_ ev_periodic *w)
1441{ 1550{
1442 /* TODO: use adjustheap and recalculation */ 1551 /* TODO: use adjustheap and recalculation */
1443 ev_periodic_stop (EV_A_ w); 1552 ev_periodic_stop (EV_A_ w);
1444 ev_periodic_start (EV_A_ w); 1553 ev_periodic_start (EV_A_ w);
1445} 1554}
1446#endif 1555#endif
1447 1556
1448void 1557void
1449ev_idle_start (EV_P_ struct ev_idle *w) 1558ev_idle_start (EV_P_ ev_idle *w)
1450{ 1559{
1451 if (ev_is_active (w)) 1560 if (expect_false (ev_is_active (w)))
1452 return; 1561 return;
1453 1562
1454 ev_start (EV_A_ (W)w, ++idlecnt); 1563 ev_start (EV_A_ (W)w, ++idlecnt);
1455 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); 1564 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1456 idles [idlecnt - 1] = w; 1565 idles [idlecnt - 1] = w;
1457} 1566}
1458 1567
1459void 1568void
1460ev_idle_stop (EV_P_ struct ev_idle *w) 1569ev_idle_stop (EV_P_ ev_idle *w)
1461{ 1570{
1462 ev_clear_pending (EV_A_ (W)w); 1571 ev_clear_pending (EV_A_ (W)w);
1463 if (!ev_is_active (w)) 1572 if (expect_false (!ev_is_active (w)))
1464 return; 1573 return;
1465 1574
1575 {
1576 int active = ((W)w)->active;
1466 idles [((W)w)->active - 1] = idles [--idlecnt]; 1577 idles [active - 1] = idles [--idlecnt];
1578 ((W)idles [active - 1])->active = active;
1579 }
1580
1467 ev_stop (EV_A_ (W)w); 1581 ev_stop (EV_A_ (W)w);
1468} 1582}
1469 1583
1470void 1584void
1471ev_prepare_start (EV_P_ struct ev_prepare *w) 1585ev_prepare_start (EV_P_ ev_prepare *w)
1472{ 1586{
1473 if (ev_is_active (w)) 1587 if (expect_false (ev_is_active (w)))
1474 return; 1588 return;
1475 1589
1476 ev_start (EV_A_ (W)w, ++preparecnt); 1590 ev_start (EV_A_ (W)w, ++preparecnt);
1477 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); 1591 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1478 prepares [preparecnt - 1] = w; 1592 prepares [preparecnt - 1] = w;
1479} 1593}
1480 1594
1481void 1595void
1482ev_prepare_stop (EV_P_ struct ev_prepare *w) 1596ev_prepare_stop (EV_P_ ev_prepare *w)
1483{ 1597{
1484 ev_clear_pending (EV_A_ (W)w); 1598 ev_clear_pending (EV_A_ (W)w);
1485 if (!ev_is_active (w)) 1599 if (expect_false (!ev_is_active (w)))
1486 return; 1600 return;
1487 1601
1602 {
1603 int active = ((W)w)->active;
1488 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1604 prepares [active - 1] = prepares [--preparecnt];
1605 ((W)prepares [active - 1])->active = active;
1606 }
1607
1489 ev_stop (EV_A_ (W)w); 1608 ev_stop (EV_A_ (W)w);
1490} 1609}
1491 1610
1492void 1611void
1493ev_check_start (EV_P_ struct ev_check *w) 1612ev_check_start (EV_P_ ev_check *w)
1494{ 1613{
1495 if (ev_is_active (w)) 1614 if (expect_false (ev_is_active (w)))
1496 return; 1615 return;
1497 1616
1498 ev_start (EV_A_ (W)w, ++checkcnt); 1617 ev_start (EV_A_ (W)w, ++checkcnt);
1499 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); 1618 array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
1500 checks [checkcnt - 1] = w; 1619 checks [checkcnt - 1] = w;
1501} 1620}
1502 1621
1503void 1622void
1504ev_check_stop (EV_P_ struct ev_check *w) 1623ev_check_stop (EV_P_ ev_check *w)
1505{ 1624{
1506 ev_clear_pending (EV_A_ (W)w); 1625 ev_clear_pending (EV_A_ (W)w);
1507 if (!ev_is_active (w)) 1626 if (expect_false (!ev_is_active (w)))
1508 return; 1627 return;
1509 1628
1629 {
1630 int active = ((W)w)->active;
1510 checks [((W)w)->active - 1] = checks [--checkcnt]; 1631 checks [active - 1] = checks [--checkcnt];
1632 ((W)checks [active - 1])->active = active;
1633 }
1634
1511 ev_stop (EV_A_ (W)w); 1635 ev_stop (EV_A_ (W)w);
1512} 1636}
1513 1637
1514#ifndef SA_RESTART 1638#ifndef SA_RESTART
1515# define SA_RESTART 0 1639# define SA_RESTART 0
1516#endif 1640#endif
1517 1641
1518void 1642void
1519ev_signal_start (EV_P_ struct ev_signal *w) 1643ev_signal_start (EV_P_ ev_signal *w)
1520{ 1644{
1521#if EV_MULTIPLICITY 1645#if EV_MULTIPLICITY
1522 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1646 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1523#endif 1647#endif
1524 if (ev_is_active (w)) 1648 if (expect_false (ev_is_active (w)))
1525 return; 1649 return;
1526 1650
1527 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));
1528 1652
1529 ev_start (EV_A_ (W)w, 1); 1653 ev_start (EV_A_ (W)w, 1);
1543#endif 1667#endif
1544 } 1668 }
1545} 1669}
1546 1670
1547void 1671void
1548ev_signal_stop (EV_P_ struct ev_signal *w) 1672ev_signal_stop (EV_P_ ev_signal *w)
1549{ 1673{
1550 ev_clear_pending (EV_A_ (W)w); 1674 ev_clear_pending (EV_A_ (W)w);
1551 if (!ev_is_active (w)) 1675 if (expect_false (!ev_is_active (w)))
1552 return; 1676 return;
1553 1677
1554 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1678 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1555 ev_stop (EV_A_ (W)w); 1679 ev_stop (EV_A_ (W)w);
1556 1680
1557 if (!signals [w->signum - 1].head) 1681 if (!signals [w->signum - 1].head)
1558 signal (w->signum, SIG_DFL); 1682 signal (w->signum, SIG_DFL);
1559} 1683}
1560 1684
1561void 1685void
1562ev_child_start (EV_P_ struct ev_child *w) 1686ev_child_start (EV_P_ ev_child *w)
1563{ 1687{
1564#if EV_MULTIPLICITY 1688#if EV_MULTIPLICITY
1565 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1689 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1566#endif 1690#endif
1567 if (ev_is_active (w)) 1691 if (expect_false (ev_is_active (w)))
1568 return; 1692 return;
1569 1693
1570 ev_start (EV_A_ (W)w, 1); 1694 ev_start (EV_A_ (W)w, 1);
1571 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1695 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1572} 1696}
1573 1697
1574void 1698void
1575ev_child_stop (EV_P_ struct ev_child *w) 1699ev_child_stop (EV_P_ ev_child *w)
1576{ 1700{
1577 ev_clear_pending (EV_A_ (W)w); 1701 ev_clear_pending (EV_A_ (W)w);
1578 if (!ev_is_active (w)) 1702 if (expect_false (!ev_is_active (w)))
1579 return; 1703 return;
1580 1704
1581 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1705 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1582 ev_stop (EV_A_ (W)w); 1706 ev_stop (EV_A_ (W)w);
1583} 1707}
1584 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
1585/*****************************************************************************/ 1819/*****************************************************************************/
1586 1820
1587struct ev_once 1821struct ev_once
1588{ 1822{
1589 struct ev_io io; 1823 ev_io io;
1590 struct ev_timer to; 1824 ev_timer to;
1591 void (*cb)(int revents, void *arg); 1825 void (*cb)(int revents, void *arg);
1592 void *arg; 1826 void *arg;
1593}; 1827};
1594 1828
1595static void 1829static void
1604 1838
1605 cb (revents, arg); 1839 cb (revents, arg);
1606} 1840}
1607 1841
1608static void 1842static void
1609once_cb_io (EV_P_ struct ev_io *w, int revents) 1843once_cb_io (EV_P_ ev_io *w, int revents)
1610{ 1844{
1611 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);
1612} 1846}
1613 1847
1614static void 1848static void
1615once_cb_to (EV_P_ struct ev_timer *w, int revents) 1849once_cb_to (EV_P_ ev_timer *w, int revents)
1616{ 1850{
1617 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);
1618} 1852}
1619 1853
1620void 1854void
1621ev_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)
1622{ 1856{
1623 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));
1624 1858
1625 if (!once) 1859 if (expect_false (!once))
1860 {
1626 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1861 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1627 else 1862 return;
1628 { 1863 }
1864
1629 once->cb = cb; 1865 once->cb = cb;
1630 once->arg = arg; 1866 once->arg = arg;
1631 1867
1632 ev_init (&once->io, once_cb_io); 1868 ev_init (&once->io, once_cb_io);
1633 if (fd >= 0) 1869 if (fd >= 0)
1634 { 1870 {
1635 ev_io_set (&once->io, fd, events); 1871 ev_io_set (&once->io, fd, events);
1636 ev_io_start (EV_A_ &once->io); 1872 ev_io_start (EV_A_ &once->io);
1637 } 1873 }
1638 1874
1639 ev_init (&once->to, once_cb_to); 1875 ev_init (&once->to, once_cb_to);
1640 if (timeout >= 0.) 1876 if (timeout >= 0.)
1641 { 1877 {
1642 ev_timer_set (&once->to, timeout, 0.); 1878 ev_timer_set (&once->to, timeout, 0.);
1643 ev_timer_start (EV_A_ &once->to); 1879 ev_timer_start (EV_A_ &once->to);
1644 }
1645 } 1880 }
1646} 1881}
1647 1882
1648#ifdef __cplusplus 1883#ifdef __cplusplus
1649} 1884}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines