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

Comparing libev/ev.c (file contents):
Revision 1.114 by root, Mon Nov 12 20:03:39 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
258 #include "ev_vars.h" 282 #include "ev_vars.h"
259 #undef VAR 283 #undef VAR
260 }; 284 };
261 #include "ev_wrap.h" 285 #include "ev_wrap.h"
262 286
263 struct ev_loop default_loop_struct; 287 static struct ev_loop default_loop_struct;
264 static struct ev_loop *default_loop; 288 struct ev_loop *ev_default_loop_ptr;
265 289
266#else 290#else
267 291
268 ev_tstamp ev_rt_now; 292 ev_tstamp ev_rt_now;
269 #define VAR(name,decl) static decl; 293 #define VAR(name,decl) static decl;
270 #include "ev_vars.h" 294 #include "ev_vars.h"
271 #undef VAR 295 #undef VAR
272 296
273 static int default_loop; 297 static int ev_default_loop_ptr;
274 298
275#endif 299#endif
276 300
277/*****************************************************************************/ 301/*****************************************************************************/
278 302
359void 383void
360ev_feed_event (EV_P_ void *w, int revents) 384ev_feed_event (EV_P_ void *w, int revents)
361{ 385{
362 W w_ = (W)w; 386 W w_ = (W)w;
363 387
364 if (w_->pending) 388 if (expect_false (w_->pending))
365 { 389 {
366 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; 390 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
367 return; 391 return;
368 } 392 }
369 393
403 fd_event (EV_A_ fd, revents); 427 fd_event (EV_A_ fd, revents);
404} 428}
405 429
406/*****************************************************************************/ 430/*****************************************************************************/
407 431
408static void 432inline void
409fd_reify (EV_P) 433fd_reify (EV_P)
410{ 434{
411 int i; 435 int i;
412 436
413 for (i = 0; i < fdchangecnt; ++i) 437 for (i = 0; i < fdchangecnt; ++i)
440} 464}
441 465
442static void 466static void
443fd_change (EV_P_ int fd) 467fd_change (EV_P_ int fd)
444{ 468{
445 if (anfds [fd].reify) 469 if (expect_false (anfds [fd].reify))
446 return; 470 return;
447 471
448 anfds [fd].reify = 1; 472 anfds [fd].reify = 1;
449 473
450 ++fdchangecnt; 474 ++fdchangecnt;
462 ev_io_stop (EV_A_ w); 486 ev_io_stop (EV_A_ w);
463 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);
464 } 488 }
465} 489}
466 490
467static int 491inline int
468fd_valid (int fd) 492fd_valid (int fd)
469{ 493{
470#ifdef _WIN32 494#ifdef _WIN32
471 return _get_osfhandle (fd) != -1; 495 return _get_osfhandle (fd) != -1;
472#else 496#else
614ev_feed_signal_event (EV_P_ int signum) 638ev_feed_signal_event (EV_P_ int signum)
615{ 639{
616 WL w; 640 WL w;
617 641
618#if EV_MULTIPLICITY 642#if EV_MULTIPLICITY
619 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));
620#endif 644#endif
621 645
622 --signum; 646 --signum;
623 647
624 if (signum < 0 || signum >= signalmax) 648 if (signum < 0 || signum >= signalmax)
641 for (signum = signalmax; signum--; ) 665 for (signum = signalmax; signum--; )
642 if (signals [signum].gotsig) 666 if (signals [signum].gotsig)
643 ev_feed_signal_event (EV_A_ signum + 1); 667 ev_feed_signal_event (EV_A_ signum + 1);
644} 668}
645 669
646inline void 670static void
647fd_intern (int fd) 671fd_intern (int fd)
648{ 672{
649#ifdef _WIN32 673#ifdef _WIN32
650 int arg = 1; 674 int arg = 1;
651 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 675 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
710 734
711#endif 735#endif
712 736
713/*****************************************************************************/ 737/*****************************************************************************/
714 738
739#if EV_USE_PORT
740# include "ev_port.c"
741#endif
715#if EV_USE_KQUEUE 742#if EV_USE_KQUEUE
716# include "ev_kqueue.c" 743# include "ev_kqueue.c"
717#endif 744#endif
718#if EV_USE_EPOLL 745#if EV_USE_EPOLL
719# include "ev_epoll.c" 746# include "ev_epoll.c"
778 805
779 if (!(flags & 0x0000ffff)) 806 if (!(flags & 0x0000ffff))
780 flags |= 0x0000ffff; 807 flags |= 0x0000ffff;
781 808
782 method = 0; 809 method = 0;
810#if EV_USE_PORT
811 if (!method && (flags & EVMETHOD_PORT )) method = port_init (EV_A_ flags);
812#endif
783#if EV_USE_KQUEUE 813#if EV_USE_KQUEUE
784 if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags); 814 if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags);
785#endif 815#endif
786#if EV_USE_EPOLL 816#if EV_USE_EPOLL
787 if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags); 817 if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags);
796 ev_init (&sigev, sigcb); 826 ev_init (&sigev, sigcb);
797 ev_set_priority (&sigev, EV_MAXPRI); 827 ev_set_priority (&sigev, EV_MAXPRI);
798 } 828 }
799} 829}
800 830
801void 831static void
802loop_destroy (EV_P) 832loop_destroy (EV_P)
803{ 833{
804 int i; 834 int i;
805 835
836#if EV_USE_PORT
837 if (method == EVMETHOD_PORT ) port_destroy (EV_A);
838#endif
806#if EV_USE_KQUEUE 839#if EV_USE_KQUEUE
807 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 840 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
808#endif 841#endif
809#if EV_USE_EPOLL 842#if EV_USE_EPOLL
810 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 843 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
833} 866}
834 867
835static void 868static void
836loop_fork (EV_P) 869loop_fork (EV_P)
837{ 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
838#if EV_USE_EPOLL 877#if EV_USE_EPOLL
839 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 878 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
840#endif
841#if EV_USE_KQUEUE
842 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
843#endif 879#endif
844 880
845 if (ev_is_active (&sigev)) 881 if (ev_is_active (&sigev))
846 { 882 {
847 /* default loop */ 883 /* default loop */
891 927
892#endif 928#endif
893 929
894#if EV_MULTIPLICITY 930#if EV_MULTIPLICITY
895struct ev_loop * 931struct ev_loop *
932ev_default_loop_init (unsigned int flags)
896#else 933#else
897int 934int
898#endif
899ev_default_loop (unsigned int flags) 935ev_default_loop (unsigned int flags)
936#endif
900{ 937{
901 if (sigpipe [0] == sigpipe [1]) 938 if (sigpipe [0] == sigpipe [1])
902 if (pipe (sigpipe)) 939 if (pipe (sigpipe))
903 return 0; 940 return 0;
904 941
905 if (!default_loop) 942 if (!ev_default_loop_ptr)
906 { 943 {
907#if EV_MULTIPLICITY 944#if EV_MULTIPLICITY
908 struct ev_loop *loop = default_loop = &default_loop_struct; 945 struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct;
909#else 946#else
910 default_loop = 1; 947 ev_default_loop_ptr = 1;
911#endif 948#endif
912 949
913 loop_init (EV_A_ flags); 950 loop_init (EV_A_ flags);
914 951
915 if (ev_method (EV_A)) 952 if (ev_method (EV_A))
922 ev_signal_start (EV_A_ &childev); 959 ev_signal_start (EV_A_ &childev);
923 ev_unref (EV_A); /* child watcher should not keep loop alive */ 960 ev_unref (EV_A); /* child watcher should not keep loop alive */
924#endif 961#endif
925 } 962 }
926 else 963 else
927 default_loop = 0; 964 ev_default_loop_ptr = 0;
928 } 965 }
929 966
930 return default_loop; 967 return ev_default_loop_ptr;
931} 968}
932 969
933void 970void
934ev_default_destroy (void) 971ev_default_destroy (void)
935{ 972{
936#if EV_MULTIPLICITY 973#if EV_MULTIPLICITY
937 struct ev_loop *loop = default_loop; 974 struct ev_loop *loop = ev_default_loop_ptr;
938#endif 975#endif
939 976
940#ifndef _WIN32 977#ifndef _WIN32
941 ev_ref (EV_A); /* child watcher */ 978 ev_ref (EV_A); /* child watcher */
942 ev_signal_stop (EV_A_ &childev); 979 ev_signal_stop (EV_A_ &childev);
953 990
954void 991void
955ev_default_fork (void) 992ev_default_fork (void)
956{ 993{
957#if EV_MULTIPLICITY 994#if EV_MULTIPLICITY
958 struct ev_loop *loop = default_loop; 995 struct ev_loop *loop = ev_default_loop_ptr;
959#endif 996#endif
960 997
961 if (method) 998 if (method)
962 postfork = 1; 999 postfork = 1;
963} 1000}
974 return 1; 1011 return 1;
975 1012
976 return 0; 1013 return 0;
977} 1014}
978 1015
979static void 1016inline void
980call_pending (EV_P) 1017call_pending (EV_P)
981{ 1018{
982 int pri; 1019 int pri;
983 1020
984 for (pri = NUMPRI; pri--; ) 1021 for (pri = NUMPRI; pri--; )
985 while (pendingcnt [pri]) 1022 while (pendingcnt [pri])
986 { 1023 {
987 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1024 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
988 1025
989 if (p->w) 1026 if (expect_true (p->w))
990 { 1027 {
991 p->w->pending = 0; 1028 p->w->pending = 0;
992 EV_CB_INVOKE (p->w, p->events); 1029 EV_CB_INVOKE (p->w, p->events);
993 } 1030 }
994 } 1031 }
995} 1032}
996 1033
997static void 1034inline void
998timers_reify (EV_P) 1035timers_reify (EV_P)
999{ 1036{
1000 while (timercnt && ((WT)timers [0])->at <= mn_now) 1037 while (timercnt && ((WT)timers [0])->at <= mn_now)
1001 { 1038 {
1002 struct ev_timer *w = timers [0]; 1039 struct ev_timer *w = timers [0];
1020 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1057 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1021 } 1058 }
1022} 1059}
1023 1060
1024#if EV_PERIODICS 1061#if EV_PERIODICS
1025static void 1062inline void
1026periodics_reify (EV_P) 1063periodics_reify (EV_P)
1027{ 1064{
1028 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1065 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1029 { 1066 {
1030 struct ev_periodic *w = periodics [0]; 1067 struct ev_periodic *w = periodics [0];
1089 ev_rt_now = ev_time (); 1126 ev_rt_now = ev_time ();
1090 return 1; 1127 return 1;
1091 } 1128 }
1092} 1129}
1093 1130
1094static void 1131inline void
1095time_update (EV_P) 1132time_update (EV_P)
1096{ 1133{
1097 int i; 1134 int i;
1098 1135
1099#if EV_USE_MONOTONIC 1136#if EV_USE_MONOTONIC
1160ev_loop (EV_P_ int flags) 1197ev_loop (EV_P_ int flags)
1161{ 1198{
1162 double block; 1199 double block;
1163 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1200 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
1164 1201
1165 do 1202 while (activecnt)
1166 { 1203 {
1167 /* queue check watchers (and execute them) */ 1204 /* queue check watchers (and execute them) */
1168 if (expect_false (preparecnt)) 1205 if (expect_false (preparecnt))
1169 { 1206 {
1170 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1207 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1210 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;
1211 if (block > to) block = to; 1248 if (block > to) block = to;
1212 } 1249 }
1213#endif 1250#endif
1214 1251
1215 if (block < 0.) block = 0.; 1252 if (expect_false (block < 0.)) block = 0.;
1216 } 1253 }
1217 1254
1218 method_poll (EV_A_ block); 1255 method_poll (EV_A_ block);
1219 1256
1220 /* update ev_rt_now, do magic */ 1257 /* update ev_rt_now, do magic */
1229 /* queue idle watchers unless io or timers are pending */ 1266 /* queue idle watchers unless io or timers are pending */
1230 if (idlecnt && !any_pending (EV_A)) 1267 if (idlecnt && !any_pending (EV_A))
1231 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1268 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1232 1269
1233 /* queue check watchers, to be executed first */ 1270 /* queue check watchers, to be executed first */
1234 if (checkcnt) 1271 if (expect_false (checkcnt))
1235 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1272 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1236 1273
1237 call_pending (EV_A); 1274 call_pending (EV_A);
1275
1276 if (expect_false (loop_done))
1277 break;
1238 } 1278 }
1239 while (activecnt && !loop_done);
1240 1279
1241 if (loop_done != 2) 1280 if (loop_done != 2)
1242 loop_done = 0; 1281 loop_done = 0;
1243} 1282}
1244 1283
1304void 1343void
1305ev_io_start (EV_P_ struct ev_io *w) 1344ev_io_start (EV_P_ struct ev_io *w)
1306{ 1345{
1307 int fd = w->fd; 1346 int fd = w->fd;
1308 1347
1309 if (ev_is_active (w)) 1348 if (expect_false (ev_is_active (w)))
1310 return; 1349 return;
1311 1350
1312 assert (("ev_io_start called with negative fd", fd >= 0)); 1351 assert (("ev_io_start called with negative fd", fd >= 0));
1313 1352
1314 ev_start (EV_A_ (W)w, 1); 1353 ev_start (EV_A_ (W)w, 1);
1320 1359
1321void 1360void
1322ev_io_stop (EV_P_ struct ev_io *w) 1361ev_io_stop (EV_P_ struct ev_io *w)
1323{ 1362{
1324 ev_clear_pending (EV_A_ (W)w); 1363 ev_clear_pending (EV_A_ (W)w);
1325 if (!ev_is_active (w)) 1364 if (expect_false (!ev_is_active (w)))
1326 return; 1365 return;
1327 1366
1328 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));
1329 1368
1330 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1369 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1334} 1373}
1335 1374
1336void 1375void
1337ev_timer_start (EV_P_ struct ev_timer *w) 1376ev_timer_start (EV_P_ struct ev_timer *w)
1338{ 1377{
1339 if (ev_is_active (w)) 1378 if (expect_false (ev_is_active (w)))
1340 return; 1379 return;
1341 1380
1342 ((WT)w)->at += mn_now; 1381 ((WT)w)->at += mn_now;
1343 1382
1344 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.));
1353 1392
1354void 1393void
1355ev_timer_stop (EV_P_ struct ev_timer *w) 1394ev_timer_stop (EV_P_ struct ev_timer *w)
1356{ 1395{
1357 ev_clear_pending (EV_A_ (W)w); 1396 ev_clear_pending (EV_A_ (W)w);
1358 if (!ev_is_active (w)) 1397 if (expect_false (!ev_is_active (w)))
1359 return; 1398 return;
1360 1399
1361 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1400 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1362 1401
1363 if (((W)w)->active < timercnt--) 1402 if (expect_true (((W)w)->active < timercnt--))
1364 { 1403 {
1365 timers [((W)w)->active - 1] = timers [timercnt]; 1404 timers [((W)w)->active - 1] = timers [timercnt];
1366 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1405 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1367 } 1406 }
1368 1407
1393 1432
1394#if EV_PERIODICS 1433#if EV_PERIODICS
1395void 1434void
1396ev_periodic_start (EV_P_ struct ev_periodic *w) 1435ev_periodic_start (EV_P_ struct ev_periodic *w)
1397{ 1436{
1398 if (ev_is_active (w)) 1437 if (expect_false (ev_is_active (w)))
1399 return; 1438 return;
1400 1439
1401 if (w->reschedule_cb) 1440 if (w->reschedule_cb)
1402 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1441 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1403 else if (w->interval) 1442 else if (w->interval)
1417 1456
1418void 1457void
1419ev_periodic_stop (EV_P_ struct ev_periodic *w) 1458ev_periodic_stop (EV_P_ struct ev_periodic *w)
1420{ 1459{
1421 ev_clear_pending (EV_A_ (W)w); 1460 ev_clear_pending (EV_A_ (W)w);
1422 if (!ev_is_active (w)) 1461 if (expect_false (!ev_is_active (w)))
1423 return; 1462 return;
1424 1463
1425 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1464 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1426 1465
1427 if (((W)w)->active < periodiccnt--) 1466 if (expect_true (((W)w)->active < periodiccnt--))
1428 { 1467 {
1429 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1468 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1430 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1469 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1431 } 1470 }
1432 1471
1443#endif 1482#endif
1444 1483
1445void 1484void
1446ev_idle_start (EV_P_ struct ev_idle *w) 1485ev_idle_start (EV_P_ struct ev_idle *w)
1447{ 1486{
1448 if (ev_is_active (w)) 1487 if (expect_false (ev_is_active (w)))
1449 return; 1488 return;
1450 1489
1451 ev_start (EV_A_ (W)w, ++idlecnt); 1490 ev_start (EV_A_ (W)w, ++idlecnt);
1452 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); 1491 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1453 idles [idlecnt - 1] = w; 1492 idles [idlecnt - 1] = w;
1455 1494
1456void 1495void
1457ev_idle_stop (EV_P_ struct ev_idle *w) 1496ev_idle_stop (EV_P_ struct ev_idle *w)
1458{ 1497{
1459 ev_clear_pending (EV_A_ (W)w); 1498 ev_clear_pending (EV_A_ (W)w);
1460 if (!ev_is_active (w)) 1499 if (expect_false (!ev_is_active (w)))
1461 return; 1500 return;
1462 1501
1463 idles [((W)w)->active - 1] = idles [--idlecnt]; 1502 idles [((W)w)->active - 1] = idles [--idlecnt];
1464 ev_stop (EV_A_ (W)w); 1503 ev_stop (EV_A_ (W)w);
1465} 1504}
1466 1505
1467void 1506void
1468ev_prepare_start (EV_P_ struct ev_prepare *w) 1507ev_prepare_start (EV_P_ struct ev_prepare *w)
1469{ 1508{
1470 if (ev_is_active (w)) 1509 if (expect_false (ev_is_active (w)))
1471 return; 1510 return;
1472 1511
1473 ev_start (EV_A_ (W)w, ++preparecnt); 1512 ev_start (EV_A_ (W)w, ++preparecnt);
1474 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); 1513 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1475 prepares [preparecnt - 1] = w; 1514 prepares [preparecnt - 1] = w;
1477 1516
1478void 1517void
1479ev_prepare_stop (EV_P_ struct ev_prepare *w) 1518ev_prepare_stop (EV_P_ struct ev_prepare *w)
1480{ 1519{
1481 ev_clear_pending (EV_A_ (W)w); 1520 ev_clear_pending (EV_A_ (W)w);
1482 if (!ev_is_active (w)) 1521 if (expect_false (!ev_is_active (w)))
1483 return; 1522 return;
1484 1523
1485 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1524 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1486 ev_stop (EV_A_ (W)w); 1525 ev_stop (EV_A_ (W)w);
1487} 1526}
1488 1527
1489void 1528void
1490ev_check_start (EV_P_ struct ev_check *w) 1529ev_check_start (EV_P_ struct ev_check *w)
1491{ 1530{
1492 if (ev_is_active (w)) 1531 if (expect_false (ev_is_active (w)))
1493 return; 1532 return;
1494 1533
1495 ev_start (EV_A_ (W)w, ++checkcnt); 1534 ev_start (EV_A_ (W)w, ++checkcnt);
1496 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); 1535 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2);
1497 checks [checkcnt - 1] = w; 1536 checks [checkcnt - 1] = w;
1499 1538
1500void 1539void
1501ev_check_stop (EV_P_ struct ev_check *w) 1540ev_check_stop (EV_P_ struct ev_check *w)
1502{ 1541{
1503 ev_clear_pending (EV_A_ (W)w); 1542 ev_clear_pending (EV_A_ (W)w);
1504 if (!ev_is_active (w)) 1543 if (expect_false (!ev_is_active (w)))
1505 return; 1544 return;
1506 1545
1507 checks [((W)w)->active - 1] = checks [--checkcnt]; 1546 checks [((W)w)->active - 1] = checks [--checkcnt];
1508 ev_stop (EV_A_ (W)w); 1547 ev_stop (EV_A_ (W)w);
1509} 1548}
1514 1553
1515void 1554void
1516ev_signal_start (EV_P_ struct ev_signal *w) 1555ev_signal_start (EV_P_ struct ev_signal *w)
1517{ 1556{
1518#if EV_MULTIPLICITY 1557#if EV_MULTIPLICITY
1519 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));
1520#endif 1559#endif
1521 if (ev_is_active (w)) 1560 if (expect_false (ev_is_active (w)))
1522 return; 1561 return;
1523 1562
1524 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));
1525 1564
1526 ev_start (EV_A_ (W)w, 1); 1565 ev_start (EV_A_ (W)w, 1);
1543 1582
1544void 1583void
1545ev_signal_stop (EV_P_ struct ev_signal *w) 1584ev_signal_stop (EV_P_ struct ev_signal *w)
1546{ 1585{
1547 ev_clear_pending (EV_A_ (W)w); 1586 ev_clear_pending (EV_A_ (W)w);
1548 if (!ev_is_active (w)) 1587 if (expect_false (!ev_is_active (w)))
1549 return; 1588 return;
1550 1589
1551 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1590 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1552 ev_stop (EV_A_ (W)w); 1591 ev_stop (EV_A_ (W)w);
1553 1592
1557 1596
1558void 1597void
1559ev_child_start (EV_P_ struct ev_child *w) 1598ev_child_start (EV_P_ struct ev_child *w)
1560{ 1599{
1561#if EV_MULTIPLICITY 1600#if EV_MULTIPLICITY
1562 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));
1563#endif 1602#endif
1564 if (ev_is_active (w)) 1603 if (expect_false (ev_is_active (w)))
1565 return; 1604 return;
1566 1605
1567 ev_start (EV_A_ (W)w, 1); 1606 ev_start (EV_A_ (W)w, 1);
1568 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1607 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1569} 1608}
1570 1609
1571void 1610void
1572ev_child_stop (EV_P_ struct ev_child *w) 1611ev_child_stop (EV_P_ struct ev_child *w)
1573{ 1612{
1574 ev_clear_pending (EV_A_ (W)w); 1613 ev_clear_pending (EV_A_ (W)w);
1575 if (!ev_is_active (w)) 1614 if (expect_false (!ev_is_active (w)))
1576 return; 1615 return;
1577 1616
1578 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1617 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1579 ev_stop (EV_A_ (W)w); 1618 ev_stop (EV_A_ (W)w);
1580} 1619}
1617void 1656void
1618ev_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)
1619{ 1658{
1620 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));
1621 1660
1622 if (!once) 1661 if (expect_false (!once))
1662 {
1623 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1663 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1624 else 1664 return;
1625 { 1665 }
1666
1626 once->cb = cb; 1667 once->cb = cb;
1627 once->arg = arg; 1668 once->arg = arg;
1628 1669
1629 ev_init (&once->io, once_cb_io); 1670 ev_init (&once->io, once_cb_io);
1630 if (fd >= 0) 1671 if (fd >= 0)
1631 { 1672 {
1632 ev_io_set (&once->io, fd, events); 1673 ev_io_set (&once->io, fd, events);
1633 ev_io_start (EV_A_ &once->io); 1674 ev_io_start (EV_A_ &once->io);
1634 } 1675 }
1635 1676
1636 ev_init (&once->to, once_cb_to); 1677 ev_init (&once->to, once_cb_to);
1637 if (timeout >= 0.) 1678 if (timeout >= 0.)
1638 { 1679 {
1639 ev_timer_set (&once->to, timeout, 0.); 1680 ev_timer_set (&once->to, timeout, 0.);
1640 ev_timer_start (EV_A_ &once->to); 1681 ev_timer_start (EV_A_ &once->to);
1641 }
1642 } 1682 }
1643} 1683}
1644 1684
1645#ifdef __cplusplus 1685#ifdef __cplusplus
1646} 1686}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines