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

Comparing libev/ev.c (file contents):
Revision 1.103 by root, Mon Nov 12 00:31:08 2007 UTC vs.
Revision 1.128 by root, Thu Nov 22 12:28:27 2007 UTC

41# define EV_USE_MONOTONIC 1 41# define EV_USE_MONOTONIC 1
42# endif 42# endif
43# ifndef EV_USE_REALTIME 43# ifndef EV_USE_REALTIME
44# define EV_USE_REALTIME 1 44# define EV_USE_REALTIME 1
45# endif 45# endif
46# else
47# ifndef EV_USE_MONOTONIC
48# define EV_USE_MONOTONIC 0
49# endif
50# ifndef EV_USE_REALTIME
51# define EV_USE_REALTIME 0
52# endif
46# endif 53# endif
47 54
48# if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT) 55# ifndef EV_USE_SELECT
56# if HAVE_SELECT && HAVE_SYS_SELECT_H
49# define EV_USE_SELECT 1 57# define EV_USE_SELECT 1
58# else
59# define EV_USE_SELECT 0
60# endif
50# endif 61# endif
51 62
52# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) 63# ifndef EV_USE_POLL
64# if HAVE_POLL && HAVE_POLL_H
53# define EV_USE_POLL 1 65# define EV_USE_POLL 1
66# else
67# define EV_USE_POLL 0
68# endif
54# endif 69# endif
55 70
56# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL) 71# ifndef EV_USE_EPOLL
72# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
57# define EV_USE_EPOLL 1 73# define EV_USE_EPOLL 1
74# else
75# define EV_USE_EPOLL 0
76# endif
58# endif 77# endif
59 78
79# ifndef EV_USE_KQUEUE
60# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE) 80# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
61# define EV_USE_KQUEUE 1 81# define EV_USE_KQUEUE 1
82# else
83# define EV_USE_KQUEUE 0
84# endif
85# endif
86
87# ifndef EV_USE_PORT
88# if HAVE_PORT_H && HAVE_PORT_CREATE
89# define EV_USE_PORT 1
90# else
91# define EV_USE_PORT 0
92# endif
62# endif 93# endif
63 94
64#endif 95#endif
65 96
66#include <math.h> 97#include <math.h>
90#endif 121#endif
91 122
92/**/ 123/**/
93 124
94#ifndef EV_USE_MONOTONIC 125#ifndef EV_USE_MONOTONIC
95# define EV_USE_MONOTONIC 1 126# define EV_USE_MONOTONIC 0
127#endif
128
129#ifndef EV_USE_REALTIME
130# define EV_USE_REALTIME 0
96#endif 131#endif
97 132
98#ifndef EV_USE_SELECT 133#ifndef EV_USE_SELECT
99# define EV_USE_SELECT 1 134# define EV_USE_SELECT 1
100#endif 135#endif
101 136
102#ifndef EV_USE_POLL 137#ifndef EV_USE_POLL
103# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ 138# ifdef _WIN32
139# define EV_USE_POLL 0
140# else
141# define EV_USE_POLL 1
142# endif
104#endif 143#endif
105 144
106#ifndef EV_USE_EPOLL 145#ifndef EV_USE_EPOLL
107# define EV_USE_EPOLL 0 146# define EV_USE_EPOLL 0
108#endif 147#endif
109 148
110#ifndef EV_USE_KQUEUE 149#ifndef EV_USE_KQUEUE
111# define EV_USE_KQUEUE 0 150# define EV_USE_KQUEUE 0
112#endif 151#endif
113 152
114#ifndef EV_USE_REALTIME 153#ifndef EV_USE_PORT
115# define EV_USE_REALTIME 1 154# define EV_USE_PORT 0
116#endif 155#endif
117 156
118/**/ 157/**/
158
159/* darwin simply cannot be helped */
160#ifdef __APPLE__
161# undef EV_USE_POLL
162# undef EV_USE_KQUEUE
163#endif
119 164
120#ifndef CLOCK_MONOTONIC 165#ifndef CLOCK_MONOTONIC
121# undef EV_USE_MONOTONIC 166# undef EV_USE_MONOTONIC
122# define EV_USE_MONOTONIC 0 167# define EV_USE_MONOTONIC 0
123#endif 168#endif
132#endif 177#endif
133 178
134/**/ 179/**/
135 180
136#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 181#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
137#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ 182#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
138#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ 183#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
139/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ 184/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
140 185
141#ifdef EV_H 186#ifdef EV_H
142# include EV_H 187# include EV_H
143#else 188#else
144# include "ev.h" 189# include "ev.h"
145#endif 190#endif
146 191
147#if __GNUC__ >= 3 192#if __GNUC__ >= 3
148# define expect(expr,value) __builtin_expect ((expr),(value)) 193# define expect(expr,value) __builtin_expect ((expr),(value))
149# define inline inline 194# define inline static inline
150#else 195#else
151# define expect(expr,value) (expr) 196# define expect(expr,value) (expr)
152# define inline static 197# define inline static
153#endif 198#endif
154 199
156#define expect_true(expr) expect ((expr) != 0, 1) 201#define expect_true(expr) expect ((expr) != 0, 1)
157 202
158#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 203#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
159#define ABSPRI(w) ((w)->priority - EV_MINPRI) 204#define ABSPRI(w) ((w)->priority - EV_MINPRI)
160 205
161#define EMPTY /* required for microsofts broken pseudo-c compiler */ 206#define EMPTY0 /* required for microsofts broken pseudo-c compiler */
207#define EMPTY2(a,b) /* used to suppress some warnings */
162 208
163typedef struct ev_watcher *W; 209typedef struct ev_watcher *W;
164typedef struct ev_watcher_list *WL; 210typedef struct ev_watcher_list *WL;
165typedef struct ev_watcher_time *WT; 211typedef struct ev_watcher_time *WT;
166 212
246 #include "ev_vars.h" 292 #include "ev_vars.h"
247 #undef VAR 293 #undef VAR
248 }; 294 };
249 #include "ev_wrap.h" 295 #include "ev_wrap.h"
250 296
251 struct ev_loop default_loop_struct; 297 static struct ev_loop default_loop_struct;
252 static struct ev_loop *default_loop; 298 struct ev_loop *ev_default_loop_ptr;
253 299
254#else 300#else
255 301
256 ev_tstamp ev_rt_now; 302 ev_tstamp ev_rt_now;
257 #define VAR(name,decl) static decl; 303 #define VAR(name,decl) static decl;
258 #include "ev_vars.h" 304 #include "ev_vars.h"
259 #undef VAR 305 #undef VAR
260 306
261 static int default_loop; 307 static int ev_default_loop_ptr;
262 308
263#endif 309#endif
264 310
265/*****************************************************************************/ 311/*****************************************************************************/
266 312
299{ 345{
300 return ev_rt_now; 346 return ev_rt_now;
301} 347}
302#endif 348#endif
303 349
304#define array_roundsize(type,n) ((n) | 4 & ~3) 350#define array_roundsize(type,n) (((n) | 4) & ~3)
305 351
306#define array_needsize(type,base,cur,cnt,init) \ 352#define array_needsize(type,base,cur,cnt,init) \
307 if (expect_false ((cnt) > cur)) \ 353 if (expect_false ((cnt) > cur)) \
308 { \ 354 { \
309 int newcnt = cur; \ 355 int newcnt = cur; \
347void 393void
348ev_feed_event (EV_P_ void *w, int revents) 394ev_feed_event (EV_P_ void *w, int revents)
349{ 395{
350 W w_ = (W)w; 396 W w_ = (W)w;
351 397
352 if (w_->pending) 398 if (expect_false (w_->pending))
353 { 399 {
354 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; 400 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
355 return; 401 return;
356 } 402 }
357 403
358 w_->pending = ++pendingcnt [ABSPRI (w_)]; 404 w_->pending = ++pendingcnt [ABSPRI (w_)];
359 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); 405 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2);
360 pendings [ABSPRI (w_)][w_->pending - 1].w = w_; 406 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
361 pendings [ABSPRI (w_)][w_->pending - 1].events = revents; 407 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
362} 408}
363 409
364static void 410static void
391 fd_event (EV_A_ fd, revents); 437 fd_event (EV_A_ fd, revents);
392} 438}
393 439
394/*****************************************************************************/ 440/*****************************************************************************/
395 441
396static void 442inline void
397fd_reify (EV_P) 443fd_reify (EV_P)
398{ 444{
399 int i; 445 int i;
400 446
401 for (i = 0; i < fdchangecnt; ++i) 447 for (i = 0; i < fdchangecnt; ++i)
428} 474}
429 475
430static void 476static void
431fd_change (EV_P_ int fd) 477fd_change (EV_P_ int fd)
432{ 478{
433 if (anfds [fd].reify) 479 if (expect_false (anfds [fd].reify))
434 return; 480 return;
435 481
436 anfds [fd].reify = 1; 482 anfds [fd].reify = 1;
437 483
438 ++fdchangecnt; 484 ++fdchangecnt;
439 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); 485 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
440 fdchanges [fdchangecnt - 1] = fd; 486 fdchanges [fdchangecnt - 1] = fd;
441} 487}
442 488
443static void 489static void
444fd_kill (EV_P_ int fd) 490fd_kill (EV_P_ int fd)
450 ev_io_stop (EV_A_ w); 496 ev_io_stop (EV_A_ w);
451 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 497 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
452 } 498 }
453} 499}
454 500
455static int 501inline int
456fd_valid (int fd) 502fd_valid (int fd)
457{ 503{
458#ifdef _WIN32 504#ifdef _WIN32
459 return _get_osfhandle (fd) != -1; 505 return _get_osfhandle (fd) != -1;
460#else 506#else
602ev_feed_signal_event (EV_P_ int signum) 648ev_feed_signal_event (EV_P_ int signum)
603{ 649{
604 WL w; 650 WL w;
605 651
606#if EV_MULTIPLICITY 652#if EV_MULTIPLICITY
607 assert (("feeding signal events is only supported in the default loop", loop == default_loop)); 653 assert (("feeding signal events is only supported in the default loop", loop == ev_default_loop_ptr));
608#endif 654#endif
609 655
610 --signum; 656 --signum;
611 657
612 if (signum < 0 || signum >= signalmax) 658 if (signum < 0 || signum >= signalmax)
629 for (signum = signalmax; signum--; ) 675 for (signum = signalmax; signum--; )
630 if (signals [signum].gotsig) 676 if (signals [signum].gotsig)
631 ev_feed_signal_event (EV_A_ signum + 1); 677 ev_feed_signal_event (EV_A_ signum + 1);
632} 678}
633 679
634inline void 680static void
635fd_intern (int fd) 681fd_intern (int fd)
636{ 682{
637#ifdef _WIN32 683#ifdef _WIN32
638 int arg = 1; 684 int arg = 1;
639 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 685 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
698 744
699#endif 745#endif
700 746
701/*****************************************************************************/ 747/*****************************************************************************/
702 748
749#if EV_USE_PORT
750# include "ev_port.c"
751#endif
703#if EV_USE_KQUEUE 752#if EV_USE_KQUEUE
704# include "ev_kqueue.c" 753# include "ev_kqueue.c"
705#endif 754#endif
706#if EV_USE_EPOLL 755#if EV_USE_EPOLL
707# include "ev_epoll.c" 756# include "ev_epoll.c"
735 return getuid () != geteuid () 784 return getuid () != geteuid ()
736 || getgid () != getegid (); 785 || getgid () != getegid ();
737#endif 786#endif
738} 787}
739 788
740int 789unsigned int
741ev_method (EV_P) 790ev_method (EV_P)
742{ 791{
743 return method; 792 return method;
744} 793}
745 794
746static void 795static void
747loop_init (EV_P_ int methods) 796loop_init (EV_P_ unsigned int flags)
748{ 797{
749 if (!method) 798 if (!method)
750 { 799 {
751#if EV_USE_MONOTONIC 800#if EV_USE_MONOTONIC
752 { 801 {
759 ev_rt_now = ev_time (); 808 ev_rt_now = ev_time ();
760 mn_now = get_clock (); 809 mn_now = get_clock ();
761 now_floor = mn_now; 810 now_floor = mn_now;
762 rtmn_diff = ev_rt_now - mn_now; 811 rtmn_diff = ev_rt_now - mn_now;
763 812
764 if (methods == EVMETHOD_AUTO) 813 if (!(flags & EVFLAG_NOENV)
765 if (!enable_secure () && getenv ("LIBEV_METHODS")) 814 && !enable_secure ()
815 && getenv ("LIBEV_FLAGS"))
766 methods = atoi (getenv ("LIBEV_METHODS")); 816 flags = atoi (getenv ("LIBEV_FLAGS"));
817
818 if (!(flags & EVMETHOD_ALL))
767 else 819 {
768 methods = EVMETHOD_ANY; 820 flags |= EVMETHOD_ALL;
821#if EV_USE_KQUEUE && !defined (__NetBSD__)
822 /* kqueue is borked on everything but netbsd apparently */
823 /* it usually doesn't work correctly on anything but sockets and pipes */
824 flags &= ~EVMETHOD_KQUEUE;
825#endif
826 }
769 827
770 method = 0; 828 method = 0;
829#if EV_USE_PORT
830 if (!method && (flags & EVMETHOD_PORT )) method = port_init (EV_A_ flags);
831#endif
771#if EV_USE_KQUEUE 832#if EV_USE_KQUEUE
772 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 833 if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags);
773#endif 834#endif
774#if EV_USE_EPOLL 835#if EV_USE_EPOLL
775 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 836 if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags);
776#endif 837#endif
777#if EV_USE_POLL 838#if EV_USE_POLL
778 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 839 if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags);
779#endif 840#endif
780#if EV_USE_SELECT 841#if EV_USE_SELECT
781 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 842 if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags);
782#endif 843#endif
783 844
784 ev_init (&sigev, sigcb); 845 ev_init (&sigev, sigcb);
785 ev_set_priority (&sigev, EV_MAXPRI); 846 ev_set_priority (&sigev, EV_MAXPRI);
786 } 847 }
787} 848}
788 849
789void 850static void
790loop_destroy (EV_P) 851loop_destroy (EV_P)
791{ 852{
792 int i; 853 int i;
793 854
855#if EV_USE_PORT
856 if (method == EVMETHOD_PORT ) port_destroy (EV_A);
857#endif
794#if EV_USE_KQUEUE 858#if EV_USE_KQUEUE
795 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 859 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
796#endif 860#endif
797#if EV_USE_EPOLL 861#if EV_USE_EPOLL
798 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 862 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
806 870
807 for (i = NUMPRI; i--; ) 871 for (i = NUMPRI; i--; )
808 array_free (pending, [i]); 872 array_free (pending, [i]);
809 873
810 /* have to use the microsoft-never-gets-it-right macro */ 874 /* have to use the microsoft-never-gets-it-right macro */
811 array_free (fdchange, EMPTY); 875 array_free (fdchange, EMPTY0);
812 array_free (timer, EMPTY); 876 array_free (timer, EMPTY0);
813#if EV_PERIODICS 877#if EV_PERIODICS
814 array_free (periodic, EMPTY); 878 array_free (periodic, EMPTY0);
815#endif 879#endif
816 array_free (idle, EMPTY); 880 array_free (idle, EMPTY0);
817 array_free (prepare, EMPTY); 881 array_free (prepare, EMPTY0);
818 array_free (check, EMPTY); 882 array_free (check, EMPTY0);
819 883
820 method = 0; 884 method = 0;
821} 885}
822 886
823static void 887static void
824loop_fork (EV_P) 888loop_fork (EV_P)
825{ 889{
890#if EV_USE_PORT
891 if (method == EVMETHOD_PORT ) port_fork (EV_A);
892#endif
893#if EV_USE_KQUEUE
894 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
895#endif
826#if EV_USE_EPOLL 896#if EV_USE_EPOLL
827 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 897 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
828#endif
829#if EV_USE_KQUEUE
830 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
831#endif 898#endif
832 899
833 if (ev_is_active (&sigev)) 900 if (ev_is_active (&sigev))
834 { 901 {
835 /* default loop */ 902 /* default loop */
848 postfork = 0; 915 postfork = 0;
849} 916}
850 917
851#if EV_MULTIPLICITY 918#if EV_MULTIPLICITY
852struct ev_loop * 919struct ev_loop *
853ev_loop_new (int methods) 920ev_loop_new (unsigned int flags)
854{ 921{
855 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 922 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
856 923
857 memset (loop, 0, sizeof (struct ev_loop)); 924 memset (loop, 0, sizeof (struct ev_loop));
858 925
859 loop_init (EV_A_ methods); 926 loop_init (EV_A_ flags);
860 927
861 if (ev_method (EV_A)) 928 if (ev_method (EV_A))
862 return loop; 929 return loop;
863 930
864 return 0; 931 return 0;
879 946
880#endif 947#endif
881 948
882#if EV_MULTIPLICITY 949#if EV_MULTIPLICITY
883struct ev_loop * 950struct ev_loop *
951ev_default_loop_init (unsigned int flags)
884#else 952#else
885int 953int
954ev_default_loop (unsigned int flags)
886#endif 955#endif
887ev_default_loop (int methods)
888{ 956{
889 if (sigpipe [0] == sigpipe [1]) 957 if (sigpipe [0] == sigpipe [1])
890 if (pipe (sigpipe)) 958 if (pipe (sigpipe))
891 return 0; 959 return 0;
892 960
893 if (!default_loop) 961 if (!ev_default_loop_ptr)
894 { 962 {
895#if EV_MULTIPLICITY 963#if EV_MULTIPLICITY
896 struct ev_loop *loop = default_loop = &default_loop_struct; 964 struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct;
897#else 965#else
898 default_loop = 1; 966 ev_default_loop_ptr = 1;
899#endif 967#endif
900 968
901 loop_init (EV_A_ methods); 969 loop_init (EV_A_ flags);
902 970
903 if (ev_method (EV_A)) 971 if (ev_method (EV_A))
904 { 972 {
905 siginit (EV_A); 973 siginit (EV_A);
906 974
910 ev_signal_start (EV_A_ &childev); 978 ev_signal_start (EV_A_ &childev);
911 ev_unref (EV_A); /* child watcher should not keep loop alive */ 979 ev_unref (EV_A); /* child watcher should not keep loop alive */
912#endif 980#endif
913 } 981 }
914 else 982 else
915 default_loop = 0; 983 ev_default_loop_ptr = 0;
916 } 984 }
917 985
918 return default_loop; 986 return ev_default_loop_ptr;
919} 987}
920 988
921void 989void
922ev_default_destroy (void) 990ev_default_destroy (void)
923{ 991{
924#if EV_MULTIPLICITY 992#if EV_MULTIPLICITY
925 struct ev_loop *loop = default_loop; 993 struct ev_loop *loop = ev_default_loop_ptr;
926#endif 994#endif
927 995
928#ifndef _WIN32 996#ifndef _WIN32
929 ev_ref (EV_A); /* child watcher */ 997 ev_ref (EV_A); /* child watcher */
930 ev_signal_stop (EV_A_ &childev); 998 ev_signal_stop (EV_A_ &childev);
941 1009
942void 1010void
943ev_default_fork (void) 1011ev_default_fork (void)
944{ 1012{
945#if EV_MULTIPLICITY 1013#if EV_MULTIPLICITY
946 struct ev_loop *loop = default_loop; 1014 struct ev_loop *loop = ev_default_loop_ptr;
947#endif 1015#endif
948 1016
949 if (method) 1017 if (method)
950 postfork = 1; 1018 postfork = 1;
951} 1019}
962 return 1; 1030 return 1;
963 1031
964 return 0; 1032 return 0;
965} 1033}
966 1034
967static void 1035inline void
968call_pending (EV_P) 1036call_pending (EV_P)
969{ 1037{
970 int pri; 1038 int pri;
971 1039
972 for (pri = NUMPRI; pri--; ) 1040 for (pri = NUMPRI; pri--; )
973 while (pendingcnt [pri]) 1041 while (pendingcnt [pri])
974 { 1042 {
975 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1043 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
976 1044
977 if (p->w) 1045 if (expect_true (p->w))
978 { 1046 {
979 p->w->pending = 0; 1047 p->w->pending = 0;
980 EV_CB_INVOKE (p->w, p->events); 1048 EV_CB_INVOKE (p->w, p->events);
981 } 1049 }
982 } 1050 }
983} 1051}
984 1052
985static void 1053inline void
986timers_reify (EV_P) 1054timers_reify (EV_P)
987{ 1055{
988 while (timercnt && ((WT)timers [0])->at <= mn_now) 1056 while (timercnt && ((WT)timers [0])->at <= mn_now)
989 { 1057 {
990 struct ev_timer *w = timers [0]; 1058 struct ev_timer *w = timers [0];
1008 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1076 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1009 } 1077 }
1010} 1078}
1011 1079
1012#if EV_PERIODICS 1080#if EV_PERIODICS
1013static void 1081inline void
1014periodics_reify (EV_P) 1082periodics_reify (EV_P)
1015{ 1083{
1016 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1084 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1017 { 1085 {
1018 struct ev_periodic *w = periodics [0]; 1086 struct ev_periodic *w = periodics [0];
1020 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1088 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1021 1089
1022 /* first reschedule or stop timer */ 1090 /* first reschedule or stop timer */
1023 if (w->reschedule_cb) 1091 if (w->reschedule_cb)
1024 { 1092 {
1025 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); 1093 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
1026
1027 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); 1094 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1028 downheap ((WT *)periodics, periodiccnt, 0); 1095 downheap ((WT *)periodics, periodiccnt, 0);
1029 } 1096 }
1030 else if (w->interval) 1097 else if (w->interval)
1031 { 1098 {
1078 ev_rt_now = ev_time (); 1145 ev_rt_now = ev_time ();
1079 return 1; 1146 return 1;
1080 } 1147 }
1081} 1148}
1082 1149
1083static void 1150inline void
1084time_update (EV_P) 1151time_update (EV_P)
1085{ 1152{
1086 int i; 1153 int i;
1087 1154
1088#if EV_USE_MONOTONIC 1155#if EV_USE_MONOTONIC
1149ev_loop (EV_P_ int flags) 1216ev_loop (EV_P_ int flags)
1150{ 1217{
1151 double block; 1218 double block;
1152 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1219 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
1153 1220
1154 do 1221 while (activecnt)
1155 { 1222 {
1156 /* queue check watchers (and execute them) */ 1223 /* queue check watchers (and execute them) */
1157 if (expect_false (preparecnt)) 1224 if (expect_false (preparecnt))
1158 { 1225 {
1159 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1226 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1199 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; 1266 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1200 if (block > to) block = to; 1267 if (block > to) block = to;
1201 } 1268 }
1202#endif 1269#endif
1203 1270
1204 if (block < 0.) block = 0.; 1271 if (expect_false (block < 0.)) block = 0.;
1205 } 1272 }
1206 1273
1207 method_poll (EV_A_ block); 1274 method_poll (EV_A_ block);
1208 1275
1209 /* update ev_rt_now, do magic */ 1276 /* update ev_rt_now, do magic */
1218 /* queue idle watchers unless io or timers are pending */ 1285 /* queue idle watchers unless io or timers are pending */
1219 if (idlecnt && !any_pending (EV_A)) 1286 if (idlecnt && !any_pending (EV_A))
1220 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1287 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1221 1288
1222 /* queue check watchers, to be executed first */ 1289 /* queue check watchers, to be executed first */
1223 if (checkcnt) 1290 if (expect_false (checkcnt))
1224 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1291 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1225 1292
1226 call_pending (EV_A); 1293 call_pending (EV_A);
1294
1295 if (expect_false (loop_done))
1296 break;
1227 } 1297 }
1228 while (activecnt && !loop_done);
1229 1298
1230 if (loop_done != 2) 1299 if (loop_done != 2)
1231 loop_done = 0; 1300 loop_done = 0;
1232} 1301}
1233 1302
1293void 1362void
1294ev_io_start (EV_P_ struct ev_io *w) 1363ev_io_start (EV_P_ struct ev_io *w)
1295{ 1364{
1296 int fd = w->fd; 1365 int fd = w->fd;
1297 1366
1298 if (ev_is_active (w)) 1367 if (expect_false (ev_is_active (w)))
1299 return; 1368 return;
1300 1369
1301 assert (("ev_io_start called with negative fd", fd >= 0)); 1370 assert (("ev_io_start called with negative fd", fd >= 0));
1302 1371
1303 ev_start (EV_A_ (W)w, 1); 1372 ev_start (EV_A_ (W)w, 1);
1309 1378
1310void 1379void
1311ev_io_stop (EV_P_ struct ev_io *w) 1380ev_io_stop (EV_P_ struct ev_io *w)
1312{ 1381{
1313 ev_clear_pending (EV_A_ (W)w); 1382 ev_clear_pending (EV_A_ (W)w);
1314 if (!ev_is_active (w)) 1383 if (expect_false (!ev_is_active (w)))
1315 return; 1384 return;
1316 1385
1317 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 1386 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1318 1387
1319 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1388 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1323} 1392}
1324 1393
1325void 1394void
1326ev_timer_start (EV_P_ struct ev_timer *w) 1395ev_timer_start (EV_P_ struct ev_timer *w)
1327{ 1396{
1328 if (ev_is_active (w)) 1397 if (expect_false (ev_is_active (w)))
1329 return; 1398 return;
1330 1399
1331 ((WT)w)->at += mn_now; 1400 ((WT)w)->at += mn_now;
1332 1401
1333 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1402 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1334 1403
1335 ev_start (EV_A_ (W)w, ++timercnt); 1404 ev_start (EV_A_ (W)w, ++timercnt);
1336 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); 1405 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2);
1337 timers [timercnt - 1] = w; 1406 timers [timercnt - 1] = w;
1338 upheap ((WT *)timers, timercnt - 1); 1407 upheap ((WT *)timers, timercnt - 1);
1339 1408
1340 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1409 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1341} 1410}
1342 1411
1343void 1412void
1344ev_timer_stop (EV_P_ struct ev_timer *w) 1413ev_timer_stop (EV_P_ struct ev_timer *w)
1345{ 1414{
1346 ev_clear_pending (EV_A_ (W)w); 1415 ev_clear_pending (EV_A_ (W)w);
1347 if (!ev_is_active (w)) 1416 if (expect_false (!ev_is_active (w)))
1348 return; 1417 return;
1349 1418
1350 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1419 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1351 1420
1352 if (((W)w)->active < timercnt--) 1421 if (expect_true (((W)w)->active < timercnt--))
1353 { 1422 {
1354 timers [((W)w)->active - 1] = timers [timercnt]; 1423 timers [((W)w)->active - 1] = timers [timercnt];
1355 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1424 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1356 } 1425 }
1357 1426
1372 } 1441 }
1373 else 1442 else
1374 ev_timer_stop (EV_A_ w); 1443 ev_timer_stop (EV_A_ w);
1375 } 1444 }
1376 else if (w->repeat) 1445 else if (w->repeat)
1446 {
1447 w->at = w->repeat;
1377 ev_timer_start (EV_A_ w); 1448 ev_timer_start (EV_A_ w);
1449 }
1378} 1450}
1379 1451
1380#if EV_PERIODICS 1452#if EV_PERIODICS
1381void 1453void
1382ev_periodic_start (EV_P_ struct ev_periodic *w) 1454ev_periodic_start (EV_P_ struct ev_periodic *w)
1383{ 1455{
1384 if (ev_is_active (w)) 1456 if (expect_false (ev_is_active (w)))
1385 return; 1457 return;
1386 1458
1387 if (w->reschedule_cb) 1459 if (w->reschedule_cb)
1388 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1460 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1389 else if (w->interval) 1461 else if (w->interval)
1392 /* this formula differs from the one in periodic_reify because we do not always round up */ 1464 /* this formula differs from the one in periodic_reify because we do not always round up */
1393 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1465 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1394 } 1466 }
1395 1467
1396 ev_start (EV_A_ (W)w, ++periodiccnt); 1468 ev_start (EV_A_ (W)w, ++periodiccnt);
1397 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1469 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1398 periodics [periodiccnt - 1] = w; 1470 periodics [periodiccnt - 1] = w;
1399 upheap ((WT *)periodics, periodiccnt - 1); 1471 upheap ((WT *)periodics, periodiccnt - 1);
1400 1472
1401 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1473 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1402} 1474}
1403 1475
1404void 1476void
1405ev_periodic_stop (EV_P_ struct ev_periodic *w) 1477ev_periodic_stop (EV_P_ struct ev_periodic *w)
1406{ 1478{
1407 ev_clear_pending (EV_A_ (W)w); 1479 ev_clear_pending (EV_A_ (W)w);
1408 if (!ev_is_active (w)) 1480 if (expect_false (!ev_is_active (w)))
1409 return; 1481 return;
1410 1482
1411 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1483 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1412 1484
1413 if (((W)w)->active < periodiccnt--) 1485 if (expect_true (((W)w)->active < periodiccnt--))
1414 { 1486 {
1415 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1487 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1416 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1488 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1417 } 1489 }
1418 1490
1429#endif 1501#endif
1430 1502
1431void 1503void
1432ev_idle_start (EV_P_ struct ev_idle *w) 1504ev_idle_start (EV_P_ struct ev_idle *w)
1433{ 1505{
1434 if (ev_is_active (w)) 1506 if (expect_false (ev_is_active (w)))
1435 return; 1507 return;
1436 1508
1437 ev_start (EV_A_ (W)w, ++idlecnt); 1509 ev_start (EV_A_ (W)w, ++idlecnt);
1438 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); 1510 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1439 idles [idlecnt - 1] = w; 1511 idles [idlecnt - 1] = w;
1440} 1512}
1441 1513
1442void 1514void
1443ev_idle_stop (EV_P_ struct ev_idle *w) 1515ev_idle_stop (EV_P_ struct ev_idle *w)
1444{ 1516{
1445 ev_clear_pending (EV_A_ (W)w); 1517 ev_clear_pending (EV_A_ (W)w);
1446 if (!ev_is_active (w)) 1518 if (expect_false (!ev_is_active (w)))
1447 return; 1519 return;
1448 1520
1449 idles [((W)w)->active - 1] = idles [--idlecnt]; 1521 idles [((W)w)->active - 1] = idles [--idlecnt];
1450 ev_stop (EV_A_ (W)w); 1522 ev_stop (EV_A_ (W)w);
1451} 1523}
1452 1524
1453void 1525void
1454ev_prepare_start (EV_P_ struct ev_prepare *w) 1526ev_prepare_start (EV_P_ struct ev_prepare *w)
1455{ 1527{
1456 if (ev_is_active (w)) 1528 if (expect_false (ev_is_active (w)))
1457 return; 1529 return;
1458 1530
1459 ev_start (EV_A_ (W)w, ++preparecnt); 1531 ev_start (EV_A_ (W)w, ++preparecnt);
1460 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); 1532 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1461 prepares [preparecnt - 1] = w; 1533 prepares [preparecnt - 1] = w;
1462} 1534}
1463 1535
1464void 1536void
1465ev_prepare_stop (EV_P_ struct ev_prepare *w) 1537ev_prepare_stop (EV_P_ struct ev_prepare *w)
1466{ 1538{
1467 ev_clear_pending (EV_A_ (W)w); 1539 ev_clear_pending (EV_A_ (W)w);
1468 if (!ev_is_active (w)) 1540 if (expect_false (!ev_is_active (w)))
1469 return; 1541 return;
1470 1542
1471 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1543 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1472 ev_stop (EV_A_ (W)w); 1544 ev_stop (EV_A_ (W)w);
1473} 1545}
1474 1546
1475void 1547void
1476ev_check_start (EV_P_ struct ev_check *w) 1548ev_check_start (EV_P_ struct ev_check *w)
1477{ 1549{
1478 if (ev_is_active (w)) 1550 if (expect_false (ev_is_active (w)))
1479 return; 1551 return;
1480 1552
1481 ev_start (EV_A_ (W)w, ++checkcnt); 1553 ev_start (EV_A_ (W)w, ++checkcnt);
1482 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); 1554 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2);
1483 checks [checkcnt - 1] = w; 1555 checks [checkcnt - 1] = w;
1484} 1556}
1485 1557
1486void 1558void
1487ev_check_stop (EV_P_ struct ev_check *w) 1559ev_check_stop (EV_P_ struct ev_check *w)
1488{ 1560{
1489 ev_clear_pending (EV_A_ (W)w); 1561 ev_clear_pending (EV_A_ (W)w);
1490 if (!ev_is_active (w)) 1562 if (expect_false (!ev_is_active (w)))
1491 return; 1563 return;
1492 1564
1493 checks [((W)w)->active - 1] = checks [--checkcnt]; 1565 checks [((W)w)->active - 1] = checks [--checkcnt];
1494 ev_stop (EV_A_ (W)w); 1566 ev_stop (EV_A_ (W)w);
1495} 1567}
1500 1572
1501void 1573void
1502ev_signal_start (EV_P_ struct ev_signal *w) 1574ev_signal_start (EV_P_ struct ev_signal *w)
1503{ 1575{
1504#if EV_MULTIPLICITY 1576#if EV_MULTIPLICITY
1505 assert (("signal watchers are only supported in the default loop", loop == default_loop)); 1577 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1506#endif 1578#endif
1507 if (ev_is_active (w)) 1579 if (expect_false (ev_is_active (w)))
1508 return; 1580 return;
1509 1581
1510 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1582 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1511 1583
1512 ev_start (EV_A_ (W)w, 1); 1584 ev_start (EV_A_ (W)w, 1);
1529 1601
1530void 1602void
1531ev_signal_stop (EV_P_ struct ev_signal *w) 1603ev_signal_stop (EV_P_ struct ev_signal *w)
1532{ 1604{
1533 ev_clear_pending (EV_A_ (W)w); 1605 ev_clear_pending (EV_A_ (W)w);
1534 if (!ev_is_active (w)) 1606 if (expect_false (!ev_is_active (w)))
1535 return; 1607 return;
1536 1608
1537 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1609 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1538 ev_stop (EV_A_ (W)w); 1610 ev_stop (EV_A_ (W)w);
1539 1611
1543 1615
1544void 1616void
1545ev_child_start (EV_P_ struct ev_child *w) 1617ev_child_start (EV_P_ struct ev_child *w)
1546{ 1618{
1547#if EV_MULTIPLICITY 1619#if EV_MULTIPLICITY
1548 assert (("child watchers are only supported in the default loop", loop == default_loop)); 1620 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1549#endif 1621#endif
1550 if (ev_is_active (w)) 1622 if (expect_false (ev_is_active (w)))
1551 return; 1623 return;
1552 1624
1553 ev_start (EV_A_ (W)w, 1); 1625 ev_start (EV_A_ (W)w, 1);
1554 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1626 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1555} 1627}
1556 1628
1557void 1629void
1558ev_child_stop (EV_P_ struct ev_child *w) 1630ev_child_stop (EV_P_ struct ev_child *w)
1559{ 1631{
1560 ev_clear_pending (EV_A_ (W)w); 1632 ev_clear_pending (EV_A_ (W)w);
1561 if (!ev_is_active (w)) 1633 if (expect_false (!ev_is_active (w)))
1562 return; 1634 return;
1563 1635
1564 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1636 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1565 ev_stop (EV_A_ (W)w); 1637 ev_stop (EV_A_ (W)w);
1566} 1638}
1603void 1675void
1604ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1676ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1605{ 1677{
1606 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); 1678 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
1607 1679
1608 if (!once) 1680 if (expect_false (!once))
1681 {
1609 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1682 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1610 else 1683 return;
1611 { 1684 }
1685
1612 once->cb = cb; 1686 once->cb = cb;
1613 once->arg = arg; 1687 once->arg = arg;
1614 1688
1615 ev_init (&once->io, once_cb_io); 1689 ev_init (&once->io, once_cb_io);
1616 if (fd >= 0) 1690 if (fd >= 0)
1617 { 1691 {
1618 ev_io_set (&once->io, fd, events); 1692 ev_io_set (&once->io, fd, events);
1619 ev_io_start (EV_A_ &once->io); 1693 ev_io_start (EV_A_ &once->io);
1620 } 1694 }
1621 1695
1622 ev_init (&once->to, once_cb_to); 1696 ev_init (&once->to, once_cb_to);
1623 if (timeout >= 0.) 1697 if (timeout >= 0.)
1624 { 1698 {
1625 ev_timer_set (&once->to, timeout, 0.); 1699 ev_timer_set (&once->to, timeout, 0.);
1626 ev_timer_start (EV_A_ &once->to); 1700 ev_timer_start (EV_A_ &once->to);
1627 }
1628 } 1701 }
1629} 1702}
1630 1703
1631#ifdef __cplusplus 1704#ifdef __cplusplus
1632} 1705}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines