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

Comparing libev/ev.c (file contents):
Revision 1.78 by root, Thu Nov 8 21:08:56 2007 UTC vs.
Revision 1.98 by root, Sun Nov 11 02:05:20 2007 UTC

26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
31#ifndef EV_STANDALONE 36#ifndef EV_STANDALONE
32# include "config.h" 37# include "config.h"
33 38
34# if HAVE_CLOCK_GETTIME 39# if HAVE_CLOCK_GETTIME
40# ifndef EV_USE_MONOTONIC
35# define EV_USE_MONOTONIC 1 41# define EV_USE_MONOTONIC 1
42# endif
43# ifndef EV_USE_REALTIME
36# define EV_USE_REALTIME 1 44# define EV_USE_REALTIME 1
45# endif
37# endif 46# endif
38 47
39# if HAVE_SELECT && HAVE_SYS_SELECT_H 48# if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT)
40# define EV_USE_SELECT 1 49# define EV_USE_SELECT 1
41# endif 50# endif
42 51
43# if HAVE_POLL && HAVE_POLL_H 52# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL)
44# define EV_USE_POLL 1 53# define EV_USE_POLL 1
45# endif 54# endif
46 55
47# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H 56# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL)
48# define EV_USE_EPOLL 1 57# define EV_USE_EPOLL 1
49# endif 58# endif
50 59
51# if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H 60# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE)
52# define EV_USE_KQUEUE 1 61# define EV_USE_KQUEUE 1
53# endif 62# endif
54 63
55#endif 64#endif
56 65
126#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 135#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
127#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ 136#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
128#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ 137#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
129/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ 138/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
130 139
140#ifdef EV_H
141# include EV_H
142#else
131#include "ev.h" 143# include "ev.h"
144#endif
132 145
133#if __GNUC__ >= 3 146#if __GNUC__ >= 3
134# define expect(expr,value) __builtin_expect ((expr),(value)) 147# define expect(expr,value) __builtin_expect ((expr),(value))
135# define inline inline 148# define inline inline
136#else 149#else
148typedef struct ev_watcher_list *WL; 161typedef struct ev_watcher_list *WL;
149typedef struct ev_watcher_time *WT; 162typedef struct ev_watcher_time *WT;
150 163
151static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 164static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
152 165
166#ifdef WIN32
153#include "ev_win32.c" 167# include "ev_win32.c"
168#endif
154 169
155/*****************************************************************************/ 170/*****************************************************************************/
156 171
157static void (*syserr_cb)(const char *msg); 172static void (*syserr_cb)(const char *msg);
158 173
215 int events; 230 int events;
216} ANPENDING; 231} ANPENDING;
217 232
218#if EV_MULTIPLICITY 233#if EV_MULTIPLICITY
219 234
220struct ev_loop 235 struct ev_loop
221{ 236 {
237 ev_tstamp ev_rt_now;
222# define VAR(name,decl) decl; 238 #define VAR(name,decl) decl;
223# include "ev_vars.h" 239 #include "ev_vars.h"
224};
225# undef VAR 240 #undef VAR
241 };
226# include "ev_wrap.h" 242 #include "ev_wrap.h"
243
244 struct ev_loop default_loop_struct;
245 static struct ev_loop *default_loop;
227 246
228#else 247#else
229 248
249 ev_tstamp ev_rt_now;
230# define VAR(name,decl) static decl; 250 #define VAR(name,decl) static decl;
231# include "ev_vars.h" 251 #include "ev_vars.h"
232# undef VAR 252 #undef VAR
253
254 static int default_loop;
233 255
234#endif 256#endif
235 257
236/*****************************************************************************/ 258/*****************************************************************************/
237 259
238inline ev_tstamp 260ev_tstamp
239ev_time (void) 261ev_time (void)
240{ 262{
241#if EV_USE_REALTIME 263#if EV_USE_REALTIME
242 struct timespec ts; 264 struct timespec ts;
243 clock_gettime (CLOCK_REALTIME, &ts); 265 clock_gettime (CLOCK_REALTIME, &ts);
262#endif 284#endif
263 285
264 return ev_time (); 286 return ev_time ();
265} 287}
266 288
289#if EV_MULTIPLICITY
267ev_tstamp 290ev_tstamp
268ev_now (EV_P) 291ev_now (EV_P)
269{ 292{
270 return rt_now; 293 return ev_rt_now;
271} 294}
295#endif
272 296
273#define array_roundsize(type,n) ((n) | 4 & ~3) 297#define array_roundsize(type,n) ((n) | 4 & ~3)
274 298
275#define array_needsize(type,base,cur,cnt,init) \ 299#define array_needsize(type,base,cur,cnt,init) \
276 if (expect_false ((cnt) > cur)) \ 300 if (expect_false ((cnt) > cur)) \
342 366
343 for (i = 0; i < eventcnt; ++i) 367 for (i = 0; i < eventcnt; ++i)
344 ev_feed_event (EV_A_ events [i], type); 368 ev_feed_event (EV_A_ events [i], type);
345} 369}
346 370
347static void 371inline void
348fd_event (EV_P_ int fd, int events) 372fd_event (EV_P_ int fd, int revents)
349{ 373{
350 ANFD *anfd = anfds + fd; 374 ANFD *anfd = anfds + fd;
351 struct ev_io *w; 375 struct ev_io *w;
352 376
353 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 377 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
354 { 378 {
355 int ev = w->events & events; 379 int ev = w->events & revents;
356 380
357 if (ev) 381 if (ev)
358 ev_feed_event (EV_A_ (W)w, ev); 382 ev_feed_event (EV_A_ (W)w, ev);
359 } 383 }
384}
385
386void
387ev_feed_fd_event (EV_P_ int fd, int revents)
388{
389 fd_event (EV_A_ fd, revents);
360} 390}
361 391
362/*****************************************************************************/ 392/*****************************************************************************/
363 393
364static void 394static void
503 533
504 heap [k] = w; 534 heap [k] = w;
505 ((W)heap [k])->active = k + 1; 535 ((W)heap [k])->active = k + 1;
506} 536}
507 537
538inline void
539adjustheap (WT *heap, int N, int k, ev_tstamp at)
540{
541 ev_tstamp old_at = heap [k]->at;
542 heap [k]->at = at;
543
544 if (old_at < at)
545 downheap (heap, N, k);
546 else
547 upheap (heap, k);
548}
549
508/*****************************************************************************/ 550/*****************************************************************************/
509 551
510typedef struct 552typedef struct
511{ 553{
512 WL head; 554 WL head;
552#endif 594#endif
553 errno = old_errno; 595 errno = old_errno;
554 } 596 }
555} 597}
556 598
599void
600ev_feed_signal_event (EV_P_ int signum)
601{
602 WL w;
603
604#if EV_MULTIPLICITY
605 assert (("feeding signal events is only supported in the default loop", loop == default_loop));
606#endif
607
608 --signum;
609
610 if (signum < 0 || signum >= signalmax)
611 return;
612
613 signals [signum].gotsig = 0;
614
615 for (w = signals [signum].head; w; w = w->next)
616 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
617}
618
557static void 619static void
558sigcb (EV_P_ struct ev_io *iow, int revents) 620sigcb (EV_P_ struct ev_io *iow, int revents)
559{ 621{
560 WL w;
561 int signum; 622 int signum;
562 623
563#ifdef WIN32 624#ifdef WIN32
564 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); 625 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
565#else 626#else
567#endif 628#endif
568 gotsig = 0; 629 gotsig = 0;
569 630
570 for (signum = signalmax; signum--; ) 631 for (signum = signalmax; signum--; )
571 if (signals [signum].gotsig) 632 if (signals [signum].gotsig)
572 { 633 ev_feed_signal_event (EV_A_ signum + 1);
573 signals [signum].gotsig = 0;
574
575 for (w = signals [signum].head; w; w = w->next)
576 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
577 }
578} 634}
579 635
580static void 636static void
581siginit (EV_P) 637siginit (EV_P)
582{ 638{
694 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 750 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
695 have_monotonic = 1; 751 have_monotonic = 1;
696 } 752 }
697#endif 753#endif
698 754
699 rt_now = ev_time (); 755 ev_rt_now = ev_time ();
700 mn_now = get_clock (); 756 mn_now = get_clock ();
701 now_floor = mn_now; 757 now_floor = mn_now;
702 rtmn_diff = rt_now - mn_now; 758 rtmn_diff = ev_rt_now - mn_now;
703 759
704 if (methods == EVMETHOD_AUTO) 760 if (methods == EVMETHOD_AUTO)
705 if (!enable_secure () && getenv ("LIBEV_METHODS")) 761 if (!enable_secure () && getenv ("LIBEV_METHODS"))
706 methods = atoi (getenv ("LIBEV_METHODS")); 762 methods = atoi (getenv ("LIBEV_METHODS"));
707 else 763 else
722#endif 778#endif
723#if EV_USE_SELECT 779#if EV_USE_SELECT
724 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 780 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
725#endif 781#endif
726 782
727 ev_watcher_init (&sigev, sigcb); 783 ev_init (&sigev, sigcb);
728 ev_set_priority (&sigev, EV_MAXPRI); 784 ev_set_priority (&sigev, EV_MAXPRI);
729 } 785 }
730} 786}
731 787
732void 788void
754 array_free (pending, [i]); 810 array_free (pending, [i]);
755 811
756 /* have to use the microsoft-never-gets-it-right macro */ 812 /* have to use the microsoft-never-gets-it-right macro */
757 array_free_microshit (fdchange); 813 array_free_microshit (fdchange);
758 array_free_microshit (timer); 814 array_free_microshit (timer);
815#if EV_PERIODICS
759 array_free_microshit (periodic); 816 array_free_microshit (periodic);
817#endif
760 array_free_microshit (idle); 818 array_free_microshit (idle);
761 array_free_microshit (prepare); 819 array_free_microshit (prepare);
762 array_free_microshit (check); 820 array_free_microshit (check);
763 821
764 method = 0; 822 method = 0;
822} 880}
823 881
824#endif 882#endif
825 883
826#if EV_MULTIPLICITY 884#if EV_MULTIPLICITY
827struct ev_loop default_loop_struct;
828static struct ev_loop *default_loop;
829
830struct ev_loop * 885struct ev_loop *
831#else 886#else
832static int default_loop;
833
834int 887int
835#endif 888#endif
836ev_default_loop (int methods) 889ev_default_loop (int methods)
837{ 890{
838 if (sigpipe [0] == sigpipe [1]) 891 if (sigpipe [0] == sigpipe [1])
924 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 977 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
925 978
926 if (p->w) 979 if (p->w)
927 { 980 {
928 p->w->pending = 0; 981 p->w->pending = 0;
929 p->w->cb (EV_A_ p->w, p->events); 982 EV_CB_INVOKE (p->w, p->events);
930 } 983 }
931 } 984 }
932} 985}
933 986
934static void 987static void
942 995
943 /* first reschedule or stop timer */ 996 /* first reschedule or stop timer */
944 if (w->repeat) 997 if (w->repeat)
945 { 998 {
946 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 999 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1000
947 ((WT)w)->at = mn_now + w->repeat; 1001 ((WT)w)->at += w->repeat;
1002 if (((WT)w)->at < mn_now)
1003 ((WT)w)->at = mn_now;
1004
948 downheap ((WT *)timers, timercnt, 0); 1005 downheap ((WT *)timers, timercnt, 0);
949 } 1006 }
950 else 1007 else
951 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1008 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
952 1009
953 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1010 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
954 } 1011 }
955} 1012}
956 1013
1014#if EV_PERIODICS
957static void 1015static void
958periodics_reify (EV_P) 1016periodics_reify (EV_P)
959{ 1017{
960 while (periodiccnt && ((WT)periodics [0])->at <= rt_now) 1018 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
961 { 1019 {
962 struct ev_periodic *w = periodics [0]; 1020 struct ev_periodic *w = periodics [0];
963 1021
964 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1022 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
965 1023
966 /* first reschedule or stop timer */ 1024 /* first reschedule or stop timer */
967 if (w->reschedule_cb) 1025 if (w->reschedule_cb)
968 { 1026 {
969 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); 1027 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
970 1028
971 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); 1029 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
972 downheap ((WT *)periodics, periodiccnt, 0); 1030 downheap ((WT *)periodics, periodiccnt, 0);
973 } 1031 }
974 else if (w->interval) 1032 else if (w->interval)
975 { 1033 {
976 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 1034 ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
977 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); 1035 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
978 downheap ((WT *)periodics, periodiccnt, 0); 1036 downheap ((WT *)periodics, periodiccnt, 0);
979 } 1037 }
980 else 1038 else
981 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1039 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
982 1040
993 for (i = 0; i < periodiccnt; ++i) 1051 for (i = 0; i < periodiccnt; ++i)
994 { 1052 {
995 struct ev_periodic *w = periodics [i]; 1053 struct ev_periodic *w = periodics [i];
996 1054
997 if (w->reschedule_cb) 1055 if (w->reschedule_cb)
998 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1056 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
999 else if (w->interval) 1057 else if (w->interval)
1000 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1058 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1001 } 1059 }
1002 1060
1003 /* now rebuild the heap */ 1061 /* now rebuild the heap */
1004 for (i = periodiccnt >> 1; i--; ) 1062 for (i = periodiccnt >> 1; i--; )
1005 downheap ((WT *)periodics, periodiccnt, i); 1063 downheap ((WT *)periodics, periodiccnt, i);
1006} 1064}
1065#endif
1007 1066
1008inline int 1067inline int
1009time_update_monotonic (EV_P) 1068time_update_monotonic (EV_P)
1010{ 1069{
1011 mn_now = get_clock (); 1070 mn_now = get_clock ();
1012 1071
1013 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1072 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1014 { 1073 {
1015 rt_now = rtmn_diff + mn_now; 1074 ev_rt_now = rtmn_diff + mn_now;
1016 return 0; 1075 return 0;
1017 } 1076 }
1018 else 1077 else
1019 { 1078 {
1020 now_floor = mn_now; 1079 now_floor = mn_now;
1021 rt_now = ev_time (); 1080 ev_rt_now = ev_time ();
1022 return 1; 1081 return 1;
1023 } 1082 }
1024} 1083}
1025 1084
1026static void 1085static void
1035 { 1094 {
1036 ev_tstamp odiff = rtmn_diff; 1095 ev_tstamp odiff = rtmn_diff;
1037 1096
1038 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1097 for (i = 4; --i; ) /* loop a few times, before making important decisions */
1039 { 1098 {
1040 rtmn_diff = rt_now - mn_now; 1099 rtmn_diff = ev_rt_now - mn_now;
1041 1100
1042 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1101 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1043 return; /* all is well */ 1102 return; /* all is well */
1044 1103
1045 rt_now = ev_time (); 1104 ev_rt_now = ev_time ();
1046 mn_now = get_clock (); 1105 mn_now = get_clock ();
1047 now_floor = mn_now; 1106 now_floor = mn_now;
1048 } 1107 }
1049 1108
1109# if EV_PERIODICS
1050 periodics_reschedule (EV_A); 1110 periodics_reschedule (EV_A);
1111# endif
1051 /* no timer adjustment, as the monotonic clock doesn't jump */ 1112 /* no timer adjustment, as the monotonic clock doesn't jump */
1052 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1113 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1053 } 1114 }
1054 } 1115 }
1055 else 1116 else
1056#endif 1117#endif
1057 { 1118 {
1058 rt_now = ev_time (); 1119 ev_rt_now = ev_time ();
1059 1120
1060 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1121 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1061 { 1122 {
1123#if EV_PERIODICS
1062 periodics_reschedule (EV_A); 1124 periodics_reschedule (EV_A);
1125#endif
1063 1126
1064 /* adjust timers. this is easy, as the offset is the same for all */ 1127 /* adjust timers. this is easy, as the offset is the same for all */
1065 for (i = 0; i < timercnt; ++i) 1128 for (i = 0; i < timercnt; ++i)
1066 ((WT)timers [i])->at += rt_now - mn_now; 1129 ((WT)timers [i])->at += ev_rt_now - mn_now;
1067 } 1130 }
1068 1131
1069 mn_now = rt_now; 1132 mn_now = ev_rt_now;
1070 } 1133 }
1071} 1134}
1072 1135
1073void 1136void
1074ev_ref (EV_P) 1137ev_ref (EV_P)
1114 if (expect_true (have_monotonic)) 1177 if (expect_true (have_monotonic))
1115 time_update_monotonic (EV_A); 1178 time_update_monotonic (EV_A);
1116 else 1179 else
1117#endif 1180#endif
1118 { 1181 {
1119 rt_now = ev_time (); 1182 ev_rt_now = ev_time ();
1120 mn_now = rt_now; 1183 mn_now = ev_rt_now;
1121 } 1184 }
1122 1185
1123 if (flags & EVLOOP_NONBLOCK || idlecnt) 1186 if (flags & EVLOOP_NONBLOCK || idlecnt)
1124 block = 0.; 1187 block = 0.;
1125 else 1188 else
1130 { 1193 {
1131 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1194 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
1132 if (block > to) block = to; 1195 if (block > to) block = to;
1133 } 1196 }
1134 1197
1198#if EV_PERIODICS
1135 if (periodiccnt) 1199 if (periodiccnt)
1136 { 1200 {
1137 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; 1201 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1138 if (block > to) block = to; 1202 if (block > to) block = to;
1139 } 1203 }
1204#endif
1140 1205
1141 if (block < 0.) block = 0.; 1206 if (block < 0.) block = 0.;
1142 } 1207 }
1143 1208
1144 method_poll (EV_A_ block); 1209 method_poll (EV_A_ block);
1145 1210
1146 /* update rt_now, do magic */ 1211 /* update ev_rt_now, do magic */
1147 time_update (EV_A); 1212 time_update (EV_A);
1148 1213
1149 /* queue pending timers and reschedule them */ 1214 /* queue pending timers and reschedule them */
1150 timers_reify (EV_A); /* relative timers called last */ 1215 timers_reify (EV_A); /* relative timers called last */
1216#if EV_PERIODICS
1151 periodics_reify (EV_A); /* absolute timers called first */ 1217 periodics_reify (EV_A); /* absolute timers called first */
1218#endif
1152 1219
1153 /* queue idle watchers unless io or timers are pending */ 1220 /* queue idle watchers unless io or timers are pending */
1154 if (idlecnt && !any_pending (EV_A)) 1221 if (idlecnt && !any_pending (EV_A))
1155 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1222 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1156 1223
1247{ 1314{
1248 ev_clear_pending (EV_A_ (W)w); 1315 ev_clear_pending (EV_A_ (W)w);
1249 if (!ev_is_active (w)) 1316 if (!ev_is_active (w))
1250 return; 1317 return;
1251 1318
1319 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1320
1252 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1321 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1253 ev_stop (EV_A_ (W)w); 1322 ev_stop (EV_A_ (W)w);
1254 1323
1255 fd_change (EV_A_ w->fd); 1324 fd_change (EV_A_ w->fd);
1256} 1325}
1286 { 1355 {
1287 timers [((W)w)->active - 1] = timers [timercnt]; 1356 timers [((W)w)->active - 1] = timers [timercnt];
1288 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1357 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1289 } 1358 }
1290 1359
1291 ((WT)w)->at = w->repeat; 1360 ((WT)w)->at -= mn_now;
1292 1361
1293 ev_stop (EV_A_ (W)w); 1362 ev_stop (EV_A_ (W)w);
1294} 1363}
1295 1364
1296void 1365void
1297ev_timer_again (EV_P_ struct ev_timer *w) 1366ev_timer_again (EV_P_ struct ev_timer *w)
1298{ 1367{
1299 if (ev_is_active (w)) 1368 if (ev_is_active (w))
1300 { 1369 {
1301 if (w->repeat) 1370 if (w->repeat)
1302 {
1303 ((WT)w)->at = mn_now + w->repeat;
1304 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1371 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat);
1305 }
1306 else 1372 else
1307 ev_timer_stop (EV_A_ w); 1373 ev_timer_stop (EV_A_ w);
1308 } 1374 }
1309 else if (w->repeat) 1375 else if (w->repeat)
1310 ev_timer_start (EV_A_ w); 1376 ev_timer_start (EV_A_ w);
1311} 1377}
1312 1378
1379#if EV_PERIODICS
1313void 1380void
1314ev_periodic_start (EV_P_ struct ev_periodic *w) 1381ev_periodic_start (EV_P_ struct ev_periodic *w)
1315{ 1382{
1316 if (ev_is_active (w)) 1383 if (ev_is_active (w))
1317 return; 1384 return;
1318 1385
1319 if (w->reschedule_cb) 1386 if (w->reschedule_cb)
1320 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1387 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1321 else if (w->interval) 1388 else if (w->interval)
1322 { 1389 {
1323 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1390 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1324 /* this formula differs from the one in periodic_reify because we do not always round up */ 1391 /* this formula differs from the one in periodic_reify because we do not always round up */
1325 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1392 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1326 } 1393 }
1327 1394
1328 ev_start (EV_A_ (W)w, ++periodiccnt); 1395 ev_start (EV_A_ (W)w, ++periodiccnt);
1329 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1396 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1330 periodics [periodiccnt - 1] = w; 1397 periodics [periodiccnt - 1] = w;
1352} 1419}
1353 1420
1354void 1421void
1355ev_periodic_again (EV_P_ struct ev_periodic *w) 1422ev_periodic_again (EV_P_ struct ev_periodic *w)
1356{ 1423{
1424 /* TODO: use adjustheap and recalculation */
1357 ev_periodic_stop (EV_A_ w); 1425 ev_periodic_stop (EV_A_ w);
1358 ev_periodic_start (EV_A_ w); 1426 ev_periodic_start (EV_A_ w);
1359} 1427}
1428#endif
1360 1429
1361void 1430void
1362ev_idle_start (EV_P_ struct ev_idle *w) 1431ev_idle_start (EV_P_ struct ev_idle *w)
1363{ 1432{
1364 if (ev_is_active (w)) 1433 if (ev_is_active (w))
1415 1484
1416void 1485void
1417ev_check_stop (EV_P_ struct ev_check *w) 1486ev_check_stop (EV_P_ struct ev_check *w)
1418{ 1487{
1419 ev_clear_pending (EV_A_ (W)w); 1488 ev_clear_pending (EV_A_ (W)w);
1420 if (ev_is_active (w)) 1489 if (!ev_is_active (w))
1421 return; 1490 return;
1422 1491
1423 checks [((W)w)->active - 1] = checks [--checkcnt]; 1492 checks [((W)w)->active - 1] = checks [--checkcnt];
1424 ev_stop (EV_A_ (W)w); 1493 ev_stop (EV_A_ (W)w);
1425} 1494}
1486 1555
1487void 1556void
1488ev_child_stop (EV_P_ struct ev_child *w) 1557ev_child_stop (EV_P_ struct ev_child *w)
1489{ 1558{
1490 ev_clear_pending (EV_A_ (W)w); 1559 ev_clear_pending (EV_A_ (W)w);
1491 if (ev_is_active (w)) 1560 if (!ev_is_active (w))
1492 return; 1561 return;
1493 1562
1494 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1563 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1495 ev_stop (EV_A_ (W)w); 1564 ev_stop (EV_A_ (W)w);
1496} 1565}
1540 else 1609 else
1541 { 1610 {
1542 once->cb = cb; 1611 once->cb = cb;
1543 once->arg = arg; 1612 once->arg = arg;
1544 1613
1545 ev_watcher_init (&once->io, once_cb_io); 1614 ev_init (&once->io, once_cb_io);
1546 if (fd >= 0) 1615 if (fd >= 0)
1547 { 1616 {
1548 ev_io_set (&once->io, fd, events); 1617 ev_io_set (&once->io, fd, events);
1549 ev_io_start (EV_A_ &once->io); 1618 ev_io_start (EV_A_ &once->io);
1550 } 1619 }
1551 1620
1552 ev_watcher_init (&once->to, once_cb_to); 1621 ev_init (&once->to, once_cb_to);
1553 if (timeout >= 0.) 1622 if (timeout >= 0.)
1554 { 1623 {
1555 ev_timer_set (&once->to, timeout, 0.); 1624 ev_timer_set (&once->to, timeout, 0.);
1556 ev_timer_start (EV_A_ &once->to); 1625 ev_timer_start (EV_A_ &once->to);
1557 } 1626 }
1558 } 1627 }
1559} 1628}
1560 1629
1630#ifdef __cplusplus
1631}
1632#endif
1633

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines