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

Comparing libev/ev.c (file contents):
Revision 1.112 by root, Mon Nov 12 07:20:24 2007 UTC vs.
Revision 1.126 by root, Sun Nov 18 01:25:23 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# if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT)
49# define EV_USE_SELECT 1 56# define EV_USE_SELECT 1
57# else
58# define EV_USE_SELECT 0
50# endif 59# endif
51 60
52# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) 61# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL)
53# define EV_USE_POLL 1 62# define EV_USE_POLL 1
63# else
64# define EV_USE_POLL 0
54# endif 65# endif
55 66
56# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL) 67# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL)
57# define EV_USE_EPOLL 1 68# define EV_USE_EPOLL 1
69# else
70# define EV_USE_EPOLL 0
58# endif 71# endif
59 72
60# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE) 73# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE)
61# define EV_USE_KQUEUE 1 74# define EV_USE_KQUEUE 1
75# else
76# define EV_USE_KQUEUE 0
77# endif
78
79# if HAVE_PORT_H && HAVE_PORT_CREATE && !defined (EV_USE_PORT)
80# define EV_USE_PORT 1
81# else
82# define EV_USE_PORT 0
62# endif 83# endif
63 84
64#endif 85#endif
65 86
66#include <math.h> 87#include <math.h>
90#endif 111#endif
91 112
92/**/ 113/**/
93 114
94#ifndef EV_USE_MONOTONIC 115#ifndef EV_USE_MONOTONIC
95# define EV_USE_MONOTONIC 1 116# define EV_USE_MONOTONIC 0
117#endif
118
119#ifndef EV_USE_REALTIME
120# define EV_USE_REALTIME 0
96#endif 121#endif
97 122
98#ifndef EV_USE_SELECT 123#ifndef EV_USE_SELECT
99# define EV_USE_SELECT 1 124# define EV_USE_SELECT 1
100# define EV_SELECT_USE_FD_SET 1
101#endif 125#endif
102 126
103#ifndef EV_USE_POLL 127#ifndef EV_USE_POLL
104# ifdef _WIN32 128# ifdef _WIN32
105# define EV_USE_POLL 0 129# define EV_USE_POLL 0
114 138
115#ifndef EV_USE_KQUEUE 139#ifndef EV_USE_KQUEUE
116# define EV_USE_KQUEUE 0 140# define EV_USE_KQUEUE 0
117#endif 141#endif
118 142
119#ifndef EV_USE_REALTIME 143#ifndef EV_USE_PORT
120# define EV_USE_REALTIME 1 144# define EV_USE_PORT 0
121#endif 145#endif
122 146
123/**/ 147/**/
124 148
125/* darwin simply cannot be helped */ 149/* darwin simply cannot be helped */
143#endif 167#endif
144 168
145/**/ 169/**/
146 170
147#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 171#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) */ 172#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 */ 173#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 */ 174/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
151 175
152#ifdef EV_H 176#ifdef EV_H
153# include EV_H 177# include EV_H
154#else 178#else
155# include "ev.h" 179# include "ev.h"
156#endif 180#endif
157 181
158#if __GNUC__ >= 3 182#if __GNUC__ >= 3
159# define expect(expr,value) __builtin_expect ((expr),(value)) 183# define expect(expr,value) __builtin_expect ((expr),(value))
160# define inline inline 184# define inline static inline
161#else 185#else
162# define expect(expr,value) (expr) 186# define expect(expr,value) (expr)
163# define inline static 187# define inline static
164#endif 188#endif
165 189
167#define expect_true(expr) expect ((expr) != 0, 1) 191#define expect_true(expr) expect ((expr) != 0, 1)
168 192
169#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 193#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
170#define ABSPRI(w) ((w)->priority - EV_MINPRI) 194#define ABSPRI(w) ((w)->priority - EV_MINPRI)
171 195
172#define EMPTY /* required for microsofts broken pseudo-c compiler */ 196#define EMPTY0 /* required for microsofts broken pseudo-c compiler */
197#define EMPTY2(a,b) /* used to suppress some warnings */
173 198
174typedef struct ev_watcher *W; 199typedef struct ev_watcher *W;
175typedef struct ev_watcher_list *WL; 200typedef struct ev_watcher_list *WL;
176typedef struct ev_watcher_time *WT; 201typedef struct ev_watcher_time *WT;
177 202
257 #include "ev_vars.h" 282 #include "ev_vars.h"
258 #undef VAR 283 #undef VAR
259 }; 284 };
260 #include "ev_wrap.h" 285 #include "ev_wrap.h"
261 286
262 struct ev_loop default_loop_struct; 287 static struct ev_loop default_loop_struct;
263 static struct ev_loop *default_loop; 288 struct ev_loop *ev_default_loop_ptr;
264 289
265#else 290#else
266 291
267 ev_tstamp ev_rt_now; 292 ev_tstamp ev_rt_now;
268 #define VAR(name,decl) static decl; 293 #define VAR(name,decl) static decl;
269 #include "ev_vars.h" 294 #include "ev_vars.h"
270 #undef VAR 295 #undef VAR
271 296
272 static int default_loop; 297 static int ev_default_loop_ptr;
273 298
274#endif 299#endif
275 300
276/*****************************************************************************/ 301/*****************************************************************************/
277 302
358void 383void
359ev_feed_event (EV_P_ void *w, int revents) 384ev_feed_event (EV_P_ void *w, int revents)
360{ 385{
361 W w_ = (W)w; 386 W w_ = (W)w;
362 387
363 if (w_->pending) 388 if (expect_false (w_->pending))
364 { 389 {
365 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; 390 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
366 return; 391 return;
367 } 392 }
368 393
369 w_->pending = ++pendingcnt [ABSPRI (w_)]; 394 w_->pending = ++pendingcnt [ABSPRI (w_)];
370 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); 395 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2);
371 pendings [ABSPRI (w_)][w_->pending - 1].w = w_; 396 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
372 pendings [ABSPRI (w_)][w_->pending - 1].events = revents; 397 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
373} 398}
374 399
375static void 400static void
402 fd_event (EV_A_ fd, revents); 427 fd_event (EV_A_ fd, revents);
403} 428}
404 429
405/*****************************************************************************/ 430/*****************************************************************************/
406 431
407static void 432inline void
408fd_reify (EV_P) 433fd_reify (EV_P)
409{ 434{
410 int i; 435 int i;
411 436
412 for (i = 0; i < fdchangecnt; ++i) 437 for (i = 0; i < fdchangecnt; ++i)
439} 464}
440 465
441static void 466static void
442fd_change (EV_P_ int fd) 467fd_change (EV_P_ int fd)
443{ 468{
444 if (anfds [fd].reify) 469 if (expect_false (anfds [fd].reify))
445 return; 470 return;
446 471
447 anfds [fd].reify = 1; 472 anfds [fd].reify = 1;
448 473
449 ++fdchangecnt; 474 ++fdchangecnt;
450 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); 475 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
451 fdchanges [fdchangecnt - 1] = fd; 476 fdchanges [fdchangecnt - 1] = fd;
452} 477}
453 478
454static void 479static void
455fd_kill (EV_P_ int fd) 480fd_kill (EV_P_ int fd)
461 ev_io_stop (EV_A_ w); 486 ev_io_stop (EV_A_ w);
462 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 487 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
463 } 488 }
464} 489}
465 490
466static int 491inline int
467fd_valid (int fd) 492fd_valid (int fd)
468{ 493{
469#ifdef _WIN32 494#ifdef _WIN32
470 return _get_osfhandle (fd) != -1; 495 return _get_osfhandle (fd) != -1;
471#else 496#else
613ev_feed_signal_event (EV_P_ int signum) 638ev_feed_signal_event (EV_P_ int signum)
614{ 639{
615 WL w; 640 WL w;
616 641
617#if EV_MULTIPLICITY 642#if EV_MULTIPLICITY
618 assert (("feeding signal events is only supported in the default loop", loop == default_loop)); 643 assert (("feeding signal events is only supported in the default loop", loop == ev_default_loop_ptr));
619#endif 644#endif
620 645
621 --signum; 646 --signum;
622 647
623 if (signum < 0 || signum >= signalmax) 648 if (signum < 0 || signum >= signalmax)
640 for (signum = signalmax; signum--; ) 665 for (signum = signalmax; signum--; )
641 if (signals [signum].gotsig) 666 if (signals [signum].gotsig)
642 ev_feed_signal_event (EV_A_ signum + 1); 667 ev_feed_signal_event (EV_A_ signum + 1);
643} 668}
644 669
645inline void 670static void
646fd_intern (int fd) 671fd_intern (int fd)
647{ 672{
648#ifdef _WIN32 673#ifdef _WIN32
649 int arg = 1; 674 int arg = 1;
650 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 675 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
709 734
710#endif 735#endif
711 736
712/*****************************************************************************/ 737/*****************************************************************************/
713 738
739#if EV_USE_PORT
740# include "ev_port.c"
741#endif
714#if EV_USE_KQUEUE 742#if EV_USE_KQUEUE
715# include "ev_kqueue.c" 743# include "ev_kqueue.c"
716#endif 744#endif
717#if EV_USE_EPOLL 745#if EV_USE_EPOLL
718# include "ev_epoll.c" 746# include "ev_epoll.c"
770 ev_rt_now = ev_time (); 798 ev_rt_now = ev_time ();
771 mn_now = get_clock (); 799 mn_now = get_clock ();
772 now_floor = mn_now; 800 now_floor = mn_now;
773 rtmn_diff = ev_rt_now - mn_now; 801 rtmn_diff = ev_rt_now - mn_now;
774 802
775 if (!(flags & EVMETHOD_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS")) 803 if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS"))
776 flags = atoi (getenv ("LIBEV_FLAGS")); 804 flags = atoi (getenv ("LIBEV_FLAGS"));
777 805
778 if (!(flags & 0x0000ffff)) 806 if (!(flags & 0x0000ffff))
779 flags |= 0x0000ffff; 807 flags |= 0x0000ffff;
780 808
781 method = 0; 809 method = 0;
810#if EV_USE_PORT
811 if (!method && (flags & EVMETHOD_PORT )) method = port_init (EV_A_ flags);
812#endif
782#if EV_USE_KQUEUE 813#if EV_USE_KQUEUE
783 if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags); 814 if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags);
784#endif 815#endif
785#if EV_USE_EPOLL 816#if EV_USE_EPOLL
786 if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags); 817 if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags);
795 ev_init (&sigev, sigcb); 826 ev_init (&sigev, sigcb);
796 ev_set_priority (&sigev, EV_MAXPRI); 827 ev_set_priority (&sigev, EV_MAXPRI);
797 } 828 }
798} 829}
799 830
800void 831static void
801loop_destroy (EV_P) 832loop_destroy (EV_P)
802{ 833{
803 int i; 834 int i;
804 835
836#if EV_USE_PORT
837 if (method == EVMETHOD_PORT ) port_destroy (EV_A);
838#endif
805#if EV_USE_KQUEUE 839#if EV_USE_KQUEUE
806 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 840 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
807#endif 841#endif
808#if EV_USE_EPOLL 842#if EV_USE_EPOLL
809 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 843 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
817 851
818 for (i = NUMPRI; i--; ) 852 for (i = NUMPRI; i--; )
819 array_free (pending, [i]); 853 array_free (pending, [i]);
820 854
821 /* have to use the microsoft-never-gets-it-right macro */ 855 /* have to use the microsoft-never-gets-it-right macro */
822 array_free (fdchange, EMPTY); 856 array_free (fdchange, EMPTY0);
823 array_free (timer, EMPTY); 857 array_free (timer, EMPTY0);
824#if EV_PERIODICS 858#if EV_PERIODICS
825 array_free (periodic, EMPTY); 859 array_free (periodic, EMPTY0);
826#endif 860#endif
827 array_free (idle, EMPTY); 861 array_free (idle, EMPTY0);
828 array_free (prepare, EMPTY); 862 array_free (prepare, EMPTY0);
829 array_free (check, EMPTY); 863 array_free (check, EMPTY0);
830 864
831 method = 0; 865 method = 0;
832} 866}
833 867
834static void 868static void
835loop_fork (EV_P) 869loop_fork (EV_P)
836{ 870{
871#if EV_USE_PORT
872 if (method == EVMETHOD_PORT ) port_fork (EV_A);
873#endif
874#if EV_USE_KQUEUE
875 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
876#endif
837#if EV_USE_EPOLL 877#if EV_USE_EPOLL
838 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 878 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
839#endif
840#if EV_USE_KQUEUE
841 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
842#endif 879#endif
843 880
844 if (ev_is_active (&sigev)) 881 if (ev_is_active (&sigev))
845 { 882 {
846 /* default loop */ 883 /* default loop */
890 927
891#endif 928#endif
892 929
893#if EV_MULTIPLICITY 930#if EV_MULTIPLICITY
894struct ev_loop * 931struct ev_loop *
932ev_default_loop_init (unsigned int flags)
895#else 933#else
896int 934int
897#endif
898ev_default_loop (unsigned int flags) 935ev_default_loop (unsigned int flags)
936#endif
899{ 937{
900 if (sigpipe [0] == sigpipe [1]) 938 if (sigpipe [0] == sigpipe [1])
901 if (pipe (sigpipe)) 939 if (pipe (sigpipe))
902 return 0; 940 return 0;
903 941
904 if (!default_loop) 942 if (!ev_default_loop_ptr)
905 { 943 {
906#if EV_MULTIPLICITY 944#if EV_MULTIPLICITY
907 struct ev_loop *loop = default_loop = &default_loop_struct; 945 struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct;
908#else 946#else
909 default_loop = 1; 947 ev_default_loop_ptr = 1;
910#endif 948#endif
911 949
912 loop_init (EV_A_ flags); 950 loop_init (EV_A_ flags);
913 951
914 if (ev_method (EV_A)) 952 if (ev_method (EV_A))
921 ev_signal_start (EV_A_ &childev); 959 ev_signal_start (EV_A_ &childev);
922 ev_unref (EV_A); /* child watcher should not keep loop alive */ 960 ev_unref (EV_A); /* child watcher should not keep loop alive */
923#endif 961#endif
924 } 962 }
925 else 963 else
926 default_loop = 0; 964 ev_default_loop_ptr = 0;
927 } 965 }
928 966
929 return default_loop; 967 return ev_default_loop_ptr;
930} 968}
931 969
932void 970void
933ev_default_destroy (void) 971ev_default_destroy (void)
934{ 972{
935#if EV_MULTIPLICITY 973#if EV_MULTIPLICITY
936 struct ev_loop *loop = default_loop; 974 struct ev_loop *loop = ev_default_loop_ptr;
937#endif 975#endif
938 976
939#ifndef _WIN32 977#ifndef _WIN32
940 ev_ref (EV_A); /* child watcher */ 978 ev_ref (EV_A); /* child watcher */
941 ev_signal_stop (EV_A_ &childev); 979 ev_signal_stop (EV_A_ &childev);
952 990
953void 991void
954ev_default_fork (void) 992ev_default_fork (void)
955{ 993{
956#if EV_MULTIPLICITY 994#if EV_MULTIPLICITY
957 struct ev_loop *loop = default_loop; 995 struct ev_loop *loop = ev_default_loop_ptr;
958#endif 996#endif
959 997
960 if (method) 998 if (method)
961 postfork = 1; 999 postfork = 1;
962} 1000}
973 return 1; 1011 return 1;
974 1012
975 return 0; 1013 return 0;
976} 1014}
977 1015
978static void 1016inline void
979call_pending (EV_P) 1017call_pending (EV_P)
980{ 1018{
981 int pri; 1019 int pri;
982 1020
983 for (pri = NUMPRI; pri--; ) 1021 for (pri = NUMPRI; pri--; )
984 while (pendingcnt [pri]) 1022 while (pendingcnt [pri])
985 { 1023 {
986 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1024 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
987 1025
988 if (p->w) 1026 if (expect_true (p->w))
989 { 1027 {
990 p->w->pending = 0; 1028 p->w->pending = 0;
991 EV_CB_INVOKE (p->w, p->events); 1029 EV_CB_INVOKE (p->w, p->events);
992 } 1030 }
993 } 1031 }
994} 1032}
995 1033
996static void 1034inline void
997timers_reify (EV_P) 1035timers_reify (EV_P)
998{ 1036{
999 while (timercnt && ((WT)timers [0])->at <= mn_now) 1037 while (timercnt && ((WT)timers [0])->at <= mn_now)
1000 { 1038 {
1001 struct ev_timer *w = timers [0]; 1039 struct ev_timer *w = timers [0];
1019 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1057 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1020 } 1058 }
1021} 1059}
1022 1060
1023#if EV_PERIODICS 1061#if EV_PERIODICS
1024static void 1062inline void
1025periodics_reify (EV_P) 1063periodics_reify (EV_P)
1026{ 1064{
1027 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1065 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1028 { 1066 {
1029 struct ev_periodic *w = periodics [0]; 1067 struct ev_periodic *w = periodics [0];
1088 ev_rt_now = ev_time (); 1126 ev_rt_now = ev_time ();
1089 return 1; 1127 return 1;
1090 } 1128 }
1091} 1129}
1092 1130
1093static void 1131inline void
1094time_update (EV_P) 1132time_update (EV_P)
1095{ 1133{
1096 int i; 1134 int i;
1097 1135
1098#if EV_USE_MONOTONIC 1136#if EV_USE_MONOTONIC
1159ev_loop (EV_P_ int flags) 1197ev_loop (EV_P_ int flags)
1160{ 1198{
1161 double block; 1199 double block;
1162 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1200 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
1163 1201
1164 do 1202 while (activecnt)
1165 { 1203 {
1166 /* queue check watchers (and execute them) */ 1204 /* queue check watchers (and execute them) */
1167 if (expect_false (preparecnt)) 1205 if (expect_false (preparecnt))
1168 { 1206 {
1169 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1207 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1209 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; 1247 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1210 if (block > to) block = to; 1248 if (block > to) block = to;
1211 } 1249 }
1212#endif 1250#endif
1213 1251
1214 if (block < 0.) block = 0.; 1252 if (expect_false (block < 0.)) block = 0.;
1215 } 1253 }
1216 1254
1217 method_poll (EV_A_ block); 1255 method_poll (EV_A_ block);
1218 1256
1219 /* update ev_rt_now, do magic */ 1257 /* update ev_rt_now, do magic */
1228 /* queue idle watchers unless io or timers are pending */ 1266 /* queue idle watchers unless io or timers are pending */
1229 if (idlecnt && !any_pending (EV_A)) 1267 if (idlecnt && !any_pending (EV_A))
1230 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1268 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1231 1269
1232 /* queue check watchers, to be executed first */ 1270 /* queue check watchers, to be executed first */
1233 if (checkcnt) 1271 if (expect_false (checkcnt))
1234 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1272 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1235 1273
1236 call_pending (EV_A); 1274 call_pending (EV_A);
1275
1276 if (expect_false (loop_done))
1277 break;
1237 } 1278 }
1238 while (activecnt && !loop_done);
1239 1279
1240 if (loop_done != 2) 1280 if (loop_done != 2)
1241 loop_done = 0; 1281 loop_done = 0;
1242} 1282}
1243 1283
1303void 1343void
1304ev_io_start (EV_P_ struct ev_io *w) 1344ev_io_start (EV_P_ struct ev_io *w)
1305{ 1345{
1306 int fd = w->fd; 1346 int fd = w->fd;
1307 1347
1308 if (ev_is_active (w)) 1348 if (expect_false (ev_is_active (w)))
1309 return; 1349 return;
1310 1350
1311 assert (("ev_io_start called with negative fd", fd >= 0)); 1351 assert (("ev_io_start called with negative fd", fd >= 0));
1312 1352
1313 ev_start (EV_A_ (W)w, 1); 1353 ev_start (EV_A_ (W)w, 1);
1319 1359
1320void 1360void
1321ev_io_stop (EV_P_ struct ev_io *w) 1361ev_io_stop (EV_P_ struct ev_io *w)
1322{ 1362{
1323 ev_clear_pending (EV_A_ (W)w); 1363 ev_clear_pending (EV_A_ (W)w);
1324 if (!ev_is_active (w)) 1364 if (expect_false (!ev_is_active (w)))
1325 return; 1365 return;
1326 1366
1327 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 1367 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1328 1368
1329 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1369 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1333} 1373}
1334 1374
1335void 1375void
1336ev_timer_start (EV_P_ struct ev_timer *w) 1376ev_timer_start (EV_P_ struct ev_timer *w)
1337{ 1377{
1338 if (ev_is_active (w)) 1378 if (expect_false (ev_is_active (w)))
1339 return; 1379 return;
1340 1380
1341 ((WT)w)->at += mn_now; 1381 ((WT)w)->at += mn_now;
1342 1382
1343 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1383 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1344 1384
1345 ev_start (EV_A_ (W)w, ++timercnt); 1385 ev_start (EV_A_ (W)w, ++timercnt);
1346 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); 1386 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2);
1347 timers [timercnt - 1] = w; 1387 timers [timercnt - 1] = w;
1348 upheap ((WT *)timers, timercnt - 1); 1388 upheap ((WT *)timers, timercnt - 1);
1349 1389
1350 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1390 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1351} 1391}
1352 1392
1353void 1393void
1354ev_timer_stop (EV_P_ struct ev_timer *w) 1394ev_timer_stop (EV_P_ struct ev_timer *w)
1355{ 1395{
1356 ev_clear_pending (EV_A_ (W)w); 1396 ev_clear_pending (EV_A_ (W)w);
1357 if (!ev_is_active (w)) 1397 if (expect_false (!ev_is_active (w)))
1358 return; 1398 return;
1359 1399
1360 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1400 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1361 1401
1362 if (((W)w)->active < timercnt--) 1402 if (expect_true (((W)w)->active < timercnt--))
1363 { 1403 {
1364 timers [((W)w)->active - 1] = timers [timercnt]; 1404 timers [((W)w)->active - 1] = timers [timercnt];
1365 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1405 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1366 } 1406 }
1367 1407
1392 1432
1393#if EV_PERIODICS 1433#if EV_PERIODICS
1394void 1434void
1395ev_periodic_start (EV_P_ struct ev_periodic *w) 1435ev_periodic_start (EV_P_ struct ev_periodic *w)
1396{ 1436{
1397 if (ev_is_active (w)) 1437 if (expect_false (ev_is_active (w)))
1398 return; 1438 return;
1399 1439
1400 if (w->reschedule_cb) 1440 if (w->reschedule_cb)
1401 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1441 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1402 else if (w->interval) 1442 else if (w->interval)
1405 /* this formula differs from the one in periodic_reify because we do not always round up */ 1445 /* this formula differs from the one in periodic_reify because we do not always round up */
1406 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1446 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1407 } 1447 }
1408 1448
1409 ev_start (EV_A_ (W)w, ++periodiccnt); 1449 ev_start (EV_A_ (W)w, ++periodiccnt);
1410 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1450 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1411 periodics [periodiccnt - 1] = w; 1451 periodics [periodiccnt - 1] = w;
1412 upheap ((WT *)periodics, periodiccnt - 1); 1452 upheap ((WT *)periodics, periodiccnt - 1);
1413 1453
1414 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1454 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1415} 1455}
1416 1456
1417void 1457void
1418ev_periodic_stop (EV_P_ struct ev_periodic *w) 1458ev_periodic_stop (EV_P_ struct ev_periodic *w)
1419{ 1459{
1420 ev_clear_pending (EV_A_ (W)w); 1460 ev_clear_pending (EV_A_ (W)w);
1421 if (!ev_is_active (w)) 1461 if (expect_false (!ev_is_active (w)))
1422 return; 1462 return;
1423 1463
1424 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1464 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1425 1465
1426 if (((W)w)->active < periodiccnt--) 1466 if (expect_true (((W)w)->active < periodiccnt--))
1427 { 1467 {
1428 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1468 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1429 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1469 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1430 } 1470 }
1431 1471
1442#endif 1482#endif
1443 1483
1444void 1484void
1445ev_idle_start (EV_P_ struct ev_idle *w) 1485ev_idle_start (EV_P_ struct ev_idle *w)
1446{ 1486{
1447 if (ev_is_active (w)) 1487 if (expect_false (ev_is_active (w)))
1448 return; 1488 return;
1449 1489
1450 ev_start (EV_A_ (W)w, ++idlecnt); 1490 ev_start (EV_A_ (W)w, ++idlecnt);
1451 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); 1491 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1452 idles [idlecnt - 1] = w; 1492 idles [idlecnt - 1] = w;
1453} 1493}
1454 1494
1455void 1495void
1456ev_idle_stop (EV_P_ struct ev_idle *w) 1496ev_idle_stop (EV_P_ struct ev_idle *w)
1457{ 1497{
1458 ev_clear_pending (EV_A_ (W)w); 1498 ev_clear_pending (EV_A_ (W)w);
1459 if (!ev_is_active (w)) 1499 if (expect_false (!ev_is_active (w)))
1460 return; 1500 return;
1461 1501
1462 idles [((W)w)->active - 1] = idles [--idlecnt]; 1502 idles [((W)w)->active - 1] = idles [--idlecnt];
1463 ev_stop (EV_A_ (W)w); 1503 ev_stop (EV_A_ (W)w);
1464} 1504}
1465 1505
1466void 1506void
1467ev_prepare_start (EV_P_ struct ev_prepare *w) 1507ev_prepare_start (EV_P_ struct ev_prepare *w)
1468{ 1508{
1469 if (ev_is_active (w)) 1509 if (expect_false (ev_is_active (w)))
1470 return; 1510 return;
1471 1511
1472 ev_start (EV_A_ (W)w, ++preparecnt); 1512 ev_start (EV_A_ (W)w, ++preparecnt);
1473 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); 1513 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1474 prepares [preparecnt - 1] = w; 1514 prepares [preparecnt - 1] = w;
1475} 1515}
1476 1516
1477void 1517void
1478ev_prepare_stop (EV_P_ struct ev_prepare *w) 1518ev_prepare_stop (EV_P_ struct ev_prepare *w)
1479{ 1519{
1480 ev_clear_pending (EV_A_ (W)w); 1520 ev_clear_pending (EV_A_ (W)w);
1481 if (!ev_is_active (w)) 1521 if (expect_false (!ev_is_active (w)))
1482 return; 1522 return;
1483 1523
1484 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1524 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1485 ev_stop (EV_A_ (W)w); 1525 ev_stop (EV_A_ (W)w);
1486} 1526}
1487 1527
1488void 1528void
1489ev_check_start (EV_P_ struct ev_check *w) 1529ev_check_start (EV_P_ struct ev_check *w)
1490{ 1530{
1491 if (ev_is_active (w)) 1531 if (expect_false (ev_is_active (w)))
1492 return; 1532 return;
1493 1533
1494 ev_start (EV_A_ (W)w, ++checkcnt); 1534 ev_start (EV_A_ (W)w, ++checkcnt);
1495 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); 1535 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2);
1496 checks [checkcnt - 1] = w; 1536 checks [checkcnt - 1] = w;
1497} 1537}
1498 1538
1499void 1539void
1500ev_check_stop (EV_P_ struct ev_check *w) 1540ev_check_stop (EV_P_ struct ev_check *w)
1501{ 1541{
1502 ev_clear_pending (EV_A_ (W)w); 1542 ev_clear_pending (EV_A_ (W)w);
1503 if (!ev_is_active (w)) 1543 if (expect_false (!ev_is_active (w)))
1504 return; 1544 return;
1505 1545
1506 checks [((W)w)->active - 1] = checks [--checkcnt]; 1546 checks [((W)w)->active - 1] = checks [--checkcnt];
1507 ev_stop (EV_A_ (W)w); 1547 ev_stop (EV_A_ (W)w);
1508} 1548}
1513 1553
1514void 1554void
1515ev_signal_start (EV_P_ struct ev_signal *w) 1555ev_signal_start (EV_P_ struct ev_signal *w)
1516{ 1556{
1517#if EV_MULTIPLICITY 1557#if EV_MULTIPLICITY
1518 assert (("signal watchers are only supported in the default loop", loop == default_loop)); 1558 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1519#endif 1559#endif
1520 if (ev_is_active (w)) 1560 if (expect_false (ev_is_active (w)))
1521 return; 1561 return;
1522 1562
1523 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1563 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1524 1564
1525 ev_start (EV_A_ (W)w, 1); 1565 ev_start (EV_A_ (W)w, 1);
1542 1582
1543void 1583void
1544ev_signal_stop (EV_P_ struct ev_signal *w) 1584ev_signal_stop (EV_P_ struct ev_signal *w)
1545{ 1585{
1546 ev_clear_pending (EV_A_ (W)w); 1586 ev_clear_pending (EV_A_ (W)w);
1547 if (!ev_is_active (w)) 1587 if (expect_false (!ev_is_active (w)))
1548 return; 1588 return;
1549 1589
1550 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1590 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1551 ev_stop (EV_A_ (W)w); 1591 ev_stop (EV_A_ (W)w);
1552 1592
1556 1596
1557void 1597void
1558ev_child_start (EV_P_ struct ev_child *w) 1598ev_child_start (EV_P_ struct ev_child *w)
1559{ 1599{
1560#if EV_MULTIPLICITY 1600#if EV_MULTIPLICITY
1561 assert (("child watchers are only supported in the default loop", loop == default_loop)); 1601 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1562#endif 1602#endif
1563 if (ev_is_active (w)) 1603 if (expect_false (ev_is_active (w)))
1564 return; 1604 return;
1565 1605
1566 ev_start (EV_A_ (W)w, 1); 1606 ev_start (EV_A_ (W)w, 1);
1567 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1607 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1568} 1608}
1569 1609
1570void 1610void
1571ev_child_stop (EV_P_ struct ev_child *w) 1611ev_child_stop (EV_P_ struct ev_child *w)
1572{ 1612{
1573 ev_clear_pending (EV_A_ (W)w); 1613 ev_clear_pending (EV_A_ (W)w);
1574 if (!ev_is_active (w)) 1614 if (expect_false (!ev_is_active (w)))
1575 return; 1615 return;
1576 1616
1577 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1617 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1578 ev_stop (EV_A_ (W)w); 1618 ev_stop (EV_A_ (W)w);
1579} 1619}
1616void 1656void
1617ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1657ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1618{ 1658{
1619 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); 1659 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
1620 1660
1621 if (!once) 1661 if (expect_false (!once))
1662 {
1622 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1663 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1623 else 1664 return;
1624 { 1665 }
1666
1625 once->cb = cb; 1667 once->cb = cb;
1626 once->arg = arg; 1668 once->arg = arg;
1627 1669
1628 ev_init (&once->io, once_cb_io); 1670 ev_init (&once->io, once_cb_io);
1629 if (fd >= 0) 1671 if (fd >= 0)
1630 { 1672 {
1631 ev_io_set (&once->io, fd, events); 1673 ev_io_set (&once->io, fd, events);
1632 ev_io_start (EV_A_ &once->io); 1674 ev_io_start (EV_A_ &once->io);
1633 } 1675 }
1634 1676
1635 ev_init (&once->to, once_cb_to); 1677 ev_init (&once->to, once_cb_to);
1636 if (timeout >= 0.) 1678 if (timeout >= 0.)
1637 { 1679 {
1638 ev_timer_set (&once->to, timeout, 0.); 1680 ev_timer_set (&once->to, timeout, 0.);
1639 ev_timer_start (EV_A_ &once->to); 1681 ev_timer_start (EV_A_ &once->to);
1640 }
1641 } 1682 }
1642} 1683}
1643 1684
1644#ifdef __cplusplus 1685#ifdef __cplusplus
1645} 1686}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines