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.94 by root, Sun Nov 11 01:29:49 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
35# define EV_USE_MONOTONIC 1 40# define EV_USE_MONOTONIC 1
46 51
47# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H 52# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
48# define EV_USE_EPOLL 1 53# define EV_USE_EPOLL 1
49# endif 54# endif
50 55
51# if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H 56# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
52# define EV_USE_KQUEUE 1 57# define EV_USE_KQUEUE 1
53# endif 58# endif
54 59
55#endif 60#endif
56 61
126#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 131#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) */ 132#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 */ 133#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 */ 134/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
130 135
136#ifdef EV_H
137# include EV_H
138#else
131#include "ev.h" 139# include "ev.h"
140#endif
132 141
133#if __GNUC__ >= 3 142#if __GNUC__ >= 3
134# define expect(expr,value) __builtin_expect ((expr),(value)) 143# define expect(expr,value) __builtin_expect ((expr),(value))
135# define inline inline 144# define inline inline
136#else 145#else
215 int events; 224 int events;
216} ANPENDING; 225} ANPENDING;
217 226
218#if EV_MULTIPLICITY 227#if EV_MULTIPLICITY
219 228
220struct ev_loop 229 struct ev_loop
221{ 230 {
231 ev_tstamp ev_rt_now;
222# define VAR(name,decl) decl; 232 #define VAR(name,decl) decl;
223# include "ev_vars.h" 233 #include "ev_vars.h"
224};
225# undef VAR 234 #undef VAR
235 };
226# include "ev_wrap.h" 236 #include "ev_wrap.h"
237
238 struct ev_loop default_loop_struct;
239 static struct ev_loop *default_loop;
227 240
228#else 241#else
229 242
243 ev_tstamp ev_rt_now;
230# define VAR(name,decl) static decl; 244 #define VAR(name,decl) static decl;
231# include "ev_vars.h" 245 #include "ev_vars.h"
232# undef VAR 246 #undef VAR
247
248 static int default_loop;
233 249
234#endif 250#endif
235 251
236/*****************************************************************************/ 252/*****************************************************************************/
237 253
238inline ev_tstamp 254ev_tstamp
239ev_time (void) 255ev_time (void)
240{ 256{
241#if EV_USE_REALTIME 257#if EV_USE_REALTIME
242 struct timespec ts; 258 struct timespec ts;
243 clock_gettime (CLOCK_REALTIME, &ts); 259 clock_gettime (CLOCK_REALTIME, &ts);
262#endif 278#endif
263 279
264 return ev_time (); 280 return ev_time ();
265} 281}
266 282
283#if EV_MULTIPLICITY
267ev_tstamp 284ev_tstamp
268ev_now (EV_P) 285ev_now (EV_P)
269{ 286{
270 return rt_now; 287 return ev_rt_now;
271} 288}
289#endif
272 290
273#define array_roundsize(type,n) ((n) | 4 & ~3) 291#define array_roundsize(type,n) ((n) | 4 & ~3)
274 292
275#define array_needsize(type,base,cur,cnt,init) \ 293#define array_needsize(type,base,cur,cnt,init) \
276 if (expect_false ((cnt) > cur)) \ 294 if (expect_false ((cnt) > cur)) \
342 360
343 for (i = 0; i < eventcnt; ++i) 361 for (i = 0; i < eventcnt; ++i)
344 ev_feed_event (EV_A_ events [i], type); 362 ev_feed_event (EV_A_ events [i], type);
345} 363}
346 364
347static void 365inline void
348fd_event (EV_P_ int fd, int events) 366fd_event (EV_P_ int fd, int revents)
349{ 367{
350 ANFD *anfd = anfds + fd; 368 ANFD *anfd = anfds + fd;
351 struct ev_io *w; 369 struct ev_io *w;
352 370
353 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 371 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
354 { 372 {
355 int ev = w->events & events; 373 int ev = w->events & revents;
356 374
357 if (ev) 375 if (ev)
358 ev_feed_event (EV_A_ (W)w, ev); 376 ev_feed_event (EV_A_ (W)w, ev);
359 } 377 }
378}
379
380void
381ev_feed_fd_event (EV_P_ int fd, int revents)
382{
383 fd_event (EV_A_ fd, revents);
360} 384}
361 385
362/*****************************************************************************/ 386/*****************************************************************************/
363 387
364static void 388static void
503 527
504 heap [k] = w; 528 heap [k] = w;
505 ((W)heap [k])->active = k + 1; 529 ((W)heap [k])->active = k + 1;
506} 530}
507 531
532inline void
533adjustheap (WT *heap, int N, int k, ev_tstamp at)
534{
535 ev_tstamp old_at = heap [k]->at;
536 heap [k]->at = at;
537
538 if (old_at < at)
539 downheap (heap, N, k);
540 else
541 upheap (heap, k);
542}
543
508/*****************************************************************************/ 544/*****************************************************************************/
509 545
510typedef struct 546typedef struct
511{ 547{
512 WL head; 548 WL head;
552#endif 588#endif
553 errno = old_errno; 589 errno = old_errno;
554 } 590 }
555} 591}
556 592
593void
594ev_feed_signal_event (EV_P_ int signum)
595{
596 WL w;
597
598#if EV_MULTIPLICITY
599 assert (("feeding signal events is only supported in the default loop", loop == default_loop));
600#endif
601
602 --signum;
603
604 if (signum < 0 || signum >= signalmax)
605 return;
606
607 signals [signum].gotsig = 0;
608
609 for (w = signals [signum].head; w; w = w->next)
610 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
611}
612
557static void 613static void
558sigcb (EV_P_ struct ev_io *iow, int revents) 614sigcb (EV_P_ struct ev_io *iow, int revents)
559{ 615{
560 WL w;
561 int signum; 616 int signum;
562 617
563#ifdef WIN32 618#ifdef WIN32
564 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); 619 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
565#else 620#else
567#endif 622#endif
568 gotsig = 0; 623 gotsig = 0;
569 624
570 for (signum = signalmax; signum--; ) 625 for (signum = signalmax; signum--; )
571 if (signals [signum].gotsig) 626 if (signals [signum].gotsig)
572 { 627 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} 628}
579 629
580static void 630static void
581siginit (EV_P) 631siginit (EV_P)
582{ 632{
694 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 744 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
695 have_monotonic = 1; 745 have_monotonic = 1;
696 } 746 }
697#endif 747#endif
698 748
699 rt_now = ev_time (); 749 ev_rt_now = ev_time ();
700 mn_now = get_clock (); 750 mn_now = get_clock ();
701 now_floor = mn_now; 751 now_floor = mn_now;
702 rtmn_diff = rt_now - mn_now; 752 rtmn_diff = ev_rt_now - mn_now;
703 753
704 if (methods == EVMETHOD_AUTO) 754 if (methods == EVMETHOD_AUTO)
705 if (!enable_secure () && getenv ("LIBEV_METHODS")) 755 if (!enable_secure () && getenv ("LIBEV_METHODS"))
706 methods = atoi (getenv ("LIBEV_METHODS")); 756 methods = atoi (getenv ("LIBEV_METHODS"));
707 else 757 else
722#endif 772#endif
723#if EV_USE_SELECT 773#if EV_USE_SELECT
724 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 774 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
725#endif 775#endif
726 776
727 ev_watcher_init (&sigev, sigcb); 777 ev_init (&sigev, sigcb);
728 ev_set_priority (&sigev, EV_MAXPRI); 778 ev_set_priority (&sigev, EV_MAXPRI);
729 } 779 }
730} 780}
731 781
732void 782void
754 array_free (pending, [i]); 804 array_free (pending, [i]);
755 805
756 /* have to use the microsoft-never-gets-it-right macro */ 806 /* have to use the microsoft-never-gets-it-right macro */
757 array_free_microshit (fdchange); 807 array_free_microshit (fdchange);
758 array_free_microshit (timer); 808 array_free_microshit (timer);
809#if EV_PERIODICS
759 array_free_microshit (periodic); 810 array_free_microshit (periodic);
811#endif
760 array_free_microshit (idle); 812 array_free_microshit (idle);
761 array_free_microshit (prepare); 813 array_free_microshit (prepare);
762 array_free_microshit (check); 814 array_free_microshit (check);
763 815
764 method = 0; 816 method = 0;
822} 874}
823 875
824#endif 876#endif
825 877
826#if EV_MULTIPLICITY 878#if EV_MULTIPLICITY
827struct ev_loop default_loop_struct;
828static struct ev_loop *default_loop;
829
830struct ev_loop * 879struct ev_loop *
831#else 880#else
832static int default_loop;
833
834int 881int
835#endif 882#endif
836ev_default_loop (int methods) 883ev_default_loop (int methods)
837{ 884{
838 if (sigpipe [0] == sigpipe [1]) 885 if (sigpipe [0] == sigpipe [1])
924 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 971 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
925 972
926 if (p->w) 973 if (p->w)
927 { 974 {
928 p->w->pending = 0; 975 p->w->pending = 0;
929 p->w->cb (EV_A_ p->w, p->events); 976 EV_CB_INVOKE (p->w, p->events);
930 } 977 }
931 } 978 }
932} 979}
933 980
934static void 981static void
942 989
943 /* first reschedule or stop timer */ 990 /* first reschedule or stop timer */
944 if (w->repeat) 991 if (w->repeat)
945 { 992 {
946 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 993 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
994
947 ((WT)w)->at = mn_now + w->repeat; 995 ((WT)w)->at += w->repeat;
996 if (((WT)w)->at < mn_now)
997 ((WT)w)->at = mn_now;
998
948 downheap ((WT *)timers, timercnt, 0); 999 downheap ((WT *)timers, timercnt, 0);
949 } 1000 }
950 else 1001 else
951 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1002 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
952 1003
953 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1004 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
954 } 1005 }
955} 1006}
956 1007
1008#if EV_PERIODICS
957static void 1009static void
958periodics_reify (EV_P) 1010periodics_reify (EV_P)
959{ 1011{
960 while (periodiccnt && ((WT)periodics [0])->at <= rt_now) 1012 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
961 { 1013 {
962 struct ev_periodic *w = periodics [0]; 1014 struct ev_periodic *w = periodics [0];
963 1015
964 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1016 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
965 1017
966 /* first reschedule or stop timer */ 1018 /* first reschedule or stop timer */
967 if (w->reschedule_cb) 1019 if (w->reschedule_cb)
968 { 1020 {
969 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); 1021 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
970 1022
971 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); 1023 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
972 downheap ((WT *)periodics, periodiccnt, 0); 1024 downheap ((WT *)periodics, periodiccnt, 0);
973 } 1025 }
974 else if (w->interval) 1026 else if (w->interval)
975 { 1027 {
976 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 1028 ((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)); 1029 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); 1030 downheap ((WT *)periodics, periodiccnt, 0);
979 } 1031 }
980 else 1032 else
981 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1033 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
982 1034
993 for (i = 0; i < periodiccnt; ++i) 1045 for (i = 0; i < periodiccnt; ++i)
994 { 1046 {
995 struct ev_periodic *w = periodics [i]; 1047 struct ev_periodic *w = periodics [i];
996 1048
997 if (w->reschedule_cb) 1049 if (w->reschedule_cb)
998 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1050 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
999 else if (w->interval) 1051 else if (w->interval)
1000 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1052 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1001 } 1053 }
1002 1054
1003 /* now rebuild the heap */ 1055 /* now rebuild the heap */
1004 for (i = periodiccnt >> 1; i--; ) 1056 for (i = periodiccnt >> 1; i--; )
1005 downheap ((WT *)periodics, periodiccnt, i); 1057 downheap ((WT *)periodics, periodiccnt, i);
1006} 1058}
1059#endif
1007 1060
1008inline int 1061inline int
1009time_update_monotonic (EV_P) 1062time_update_monotonic (EV_P)
1010{ 1063{
1011 mn_now = get_clock (); 1064 mn_now = get_clock ();
1012 1065
1013 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1066 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1014 { 1067 {
1015 rt_now = rtmn_diff + mn_now; 1068 ev_rt_now = rtmn_diff + mn_now;
1016 return 0; 1069 return 0;
1017 } 1070 }
1018 else 1071 else
1019 { 1072 {
1020 now_floor = mn_now; 1073 now_floor = mn_now;
1021 rt_now = ev_time (); 1074 ev_rt_now = ev_time ();
1022 return 1; 1075 return 1;
1023 } 1076 }
1024} 1077}
1025 1078
1026static void 1079static void
1035 { 1088 {
1036 ev_tstamp odiff = rtmn_diff; 1089 ev_tstamp odiff = rtmn_diff;
1037 1090
1038 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1091 for (i = 4; --i; ) /* loop a few times, before making important decisions */
1039 { 1092 {
1040 rtmn_diff = rt_now - mn_now; 1093 rtmn_diff = ev_rt_now - mn_now;
1041 1094
1042 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1095 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1043 return; /* all is well */ 1096 return; /* all is well */
1044 1097
1045 rt_now = ev_time (); 1098 ev_rt_now = ev_time ();
1046 mn_now = get_clock (); 1099 mn_now = get_clock ();
1047 now_floor = mn_now; 1100 now_floor = mn_now;
1048 } 1101 }
1049 1102
1103# if EV_PERIODICS
1050 periodics_reschedule (EV_A); 1104 periodics_reschedule (EV_A);
1105# endif
1051 /* no timer adjustment, as the monotonic clock doesn't jump */ 1106 /* no timer adjustment, as the monotonic clock doesn't jump */
1052 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1107 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1053 } 1108 }
1054 } 1109 }
1055 else 1110 else
1056#endif 1111#endif
1057 { 1112 {
1058 rt_now = ev_time (); 1113 ev_rt_now = ev_time ();
1059 1114
1060 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1115 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1061 { 1116 {
1117#if EV_PERIODICS
1062 periodics_reschedule (EV_A); 1118 periodics_reschedule (EV_A);
1119#endif
1063 1120
1064 /* adjust timers. this is easy, as the offset is the same for all */ 1121 /* adjust timers. this is easy, as the offset is the same for all */
1065 for (i = 0; i < timercnt; ++i) 1122 for (i = 0; i < timercnt; ++i)
1066 ((WT)timers [i])->at += rt_now - mn_now; 1123 ((WT)timers [i])->at += ev_rt_now - mn_now;
1067 } 1124 }
1068 1125
1069 mn_now = rt_now; 1126 mn_now = ev_rt_now;
1070 } 1127 }
1071} 1128}
1072 1129
1073void 1130void
1074ev_ref (EV_P) 1131ev_ref (EV_P)
1114 if (expect_true (have_monotonic)) 1171 if (expect_true (have_monotonic))
1115 time_update_monotonic (EV_A); 1172 time_update_monotonic (EV_A);
1116 else 1173 else
1117#endif 1174#endif
1118 { 1175 {
1119 rt_now = ev_time (); 1176 ev_rt_now = ev_time ();
1120 mn_now = rt_now; 1177 mn_now = ev_rt_now;
1121 } 1178 }
1122 1179
1123 if (flags & EVLOOP_NONBLOCK || idlecnt) 1180 if (flags & EVLOOP_NONBLOCK || idlecnt)
1124 block = 0.; 1181 block = 0.;
1125 else 1182 else
1130 { 1187 {
1131 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1188 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
1132 if (block > to) block = to; 1189 if (block > to) block = to;
1133 } 1190 }
1134 1191
1192#if EV_PERIODICS
1135 if (periodiccnt) 1193 if (periodiccnt)
1136 { 1194 {
1137 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; 1195 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1138 if (block > to) block = to; 1196 if (block > to) block = to;
1139 } 1197 }
1198#endif
1140 1199
1141 if (block < 0.) block = 0.; 1200 if (block < 0.) block = 0.;
1142 } 1201 }
1143 1202
1144 method_poll (EV_A_ block); 1203 method_poll (EV_A_ block);
1145 1204
1146 /* update rt_now, do magic */ 1205 /* update ev_rt_now, do magic */
1147 time_update (EV_A); 1206 time_update (EV_A);
1148 1207
1149 /* queue pending timers and reschedule them */ 1208 /* queue pending timers and reschedule them */
1150 timers_reify (EV_A); /* relative timers called last */ 1209 timers_reify (EV_A); /* relative timers called last */
1210#if EV_PERIODICS
1151 periodics_reify (EV_A); /* absolute timers called first */ 1211 periodics_reify (EV_A); /* absolute timers called first */
1212#endif
1152 1213
1153 /* queue idle watchers unless io or timers are pending */ 1214 /* queue idle watchers unless io or timers are pending */
1154 if (idlecnt && !any_pending (EV_A)) 1215 if (idlecnt && !any_pending (EV_A))
1155 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1216 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1156 1217
1247{ 1308{
1248 ev_clear_pending (EV_A_ (W)w); 1309 ev_clear_pending (EV_A_ (W)w);
1249 if (!ev_is_active (w)) 1310 if (!ev_is_active (w))
1250 return; 1311 return;
1251 1312
1313 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1314
1252 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1315 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1253 ev_stop (EV_A_ (W)w); 1316 ev_stop (EV_A_ (W)w);
1254 1317
1255 fd_change (EV_A_ w->fd); 1318 fd_change (EV_A_ w->fd);
1256} 1319}
1286 { 1349 {
1287 timers [((W)w)->active - 1] = timers [timercnt]; 1350 timers [((W)w)->active - 1] = timers [timercnt];
1288 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1351 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1289 } 1352 }
1290 1353
1291 ((WT)w)->at = w->repeat; 1354 ((WT)w)->at -= mn_now;
1292 1355
1293 ev_stop (EV_A_ (W)w); 1356 ev_stop (EV_A_ (W)w);
1294} 1357}
1295 1358
1296void 1359void
1297ev_timer_again (EV_P_ struct ev_timer *w) 1360ev_timer_again (EV_P_ struct ev_timer *w)
1298{ 1361{
1299 if (ev_is_active (w)) 1362 if (ev_is_active (w))
1300 { 1363 {
1301 if (w->repeat) 1364 if (w->repeat)
1302 {
1303 ((WT)w)->at = mn_now + w->repeat;
1304 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1365 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat);
1305 }
1306 else 1366 else
1307 ev_timer_stop (EV_A_ w); 1367 ev_timer_stop (EV_A_ w);
1308 } 1368 }
1309 else if (w->repeat) 1369 else if (w->repeat)
1310 ev_timer_start (EV_A_ w); 1370 ev_timer_start (EV_A_ w);
1311} 1371}
1312 1372
1373#if EV_PERIODICS
1313void 1374void
1314ev_periodic_start (EV_P_ struct ev_periodic *w) 1375ev_periodic_start (EV_P_ struct ev_periodic *w)
1315{ 1376{
1316 if (ev_is_active (w)) 1377 if (ev_is_active (w))
1317 return; 1378 return;
1318 1379
1319 if (w->reschedule_cb) 1380 if (w->reschedule_cb)
1320 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1381 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1321 else if (w->interval) 1382 else if (w->interval)
1322 { 1383 {
1323 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1384 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 */ 1385 /* 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; 1386 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1326 } 1387 }
1327 1388
1328 ev_start (EV_A_ (W)w, ++periodiccnt); 1389 ev_start (EV_A_ (W)w, ++periodiccnt);
1329 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1390 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1330 periodics [periodiccnt - 1] = w; 1391 periodics [periodiccnt - 1] = w;
1352} 1413}
1353 1414
1354void 1415void
1355ev_periodic_again (EV_P_ struct ev_periodic *w) 1416ev_periodic_again (EV_P_ struct ev_periodic *w)
1356{ 1417{
1418 /* TODO: use adjustheap and recalculation */
1357 ev_periodic_stop (EV_A_ w); 1419 ev_periodic_stop (EV_A_ w);
1358 ev_periodic_start (EV_A_ w); 1420 ev_periodic_start (EV_A_ w);
1359} 1421}
1422#endif
1360 1423
1361void 1424void
1362ev_idle_start (EV_P_ struct ev_idle *w) 1425ev_idle_start (EV_P_ struct ev_idle *w)
1363{ 1426{
1364 if (ev_is_active (w)) 1427 if (ev_is_active (w))
1371 1434
1372void 1435void
1373ev_idle_stop (EV_P_ struct ev_idle *w) 1436ev_idle_stop (EV_P_ struct ev_idle *w)
1374{ 1437{
1375 ev_clear_pending (EV_A_ (W)w); 1438 ev_clear_pending (EV_A_ (W)w);
1376 if (ev_is_active (w)) 1439 if (!ev_is_active (w))
1377 return; 1440 return;
1378 1441
1379 idles [((W)w)->active - 1] = idles [--idlecnt]; 1442 idles [((W)w)->active - 1] = idles [--idlecnt];
1380 ev_stop (EV_A_ (W)w); 1443 ev_stop (EV_A_ (W)w);
1381} 1444}
1393 1456
1394void 1457void
1395ev_prepare_stop (EV_P_ struct ev_prepare *w) 1458ev_prepare_stop (EV_P_ struct ev_prepare *w)
1396{ 1459{
1397 ev_clear_pending (EV_A_ (W)w); 1460 ev_clear_pending (EV_A_ (W)w);
1398 if (ev_is_active (w)) 1461 if (!ev_is_active (w))
1399 return; 1462 return;
1400 1463
1401 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1464 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1402 ev_stop (EV_A_ (W)w); 1465 ev_stop (EV_A_ (W)w);
1403} 1466}
1415 1478
1416void 1479void
1417ev_check_stop (EV_P_ struct ev_check *w) 1480ev_check_stop (EV_P_ struct ev_check *w)
1418{ 1481{
1419 ev_clear_pending (EV_A_ (W)w); 1482 ev_clear_pending (EV_A_ (W)w);
1420 if (ev_is_active (w)) 1483 if (!ev_is_active (w))
1421 return; 1484 return;
1422 1485
1423 checks [((W)w)->active - 1] = checks [--checkcnt]; 1486 checks [((W)w)->active - 1] = checks [--checkcnt];
1424 ev_stop (EV_A_ (W)w); 1487 ev_stop (EV_A_ (W)w);
1425} 1488}
1540 else 1603 else
1541 { 1604 {
1542 once->cb = cb; 1605 once->cb = cb;
1543 once->arg = arg; 1606 once->arg = arg;
1544 1607
1545 ev_watcher_init (&once->io, once_cb_io); 1608 ev_init (&once->io, once_cb_io);
1546 if (fd >= 0) 1609 if (fd >= 0)
1547 { 1610 {
1548 ev_io_set (&once->io, fd, events); 1611 ev_io_set (&once->io, fd, events);
1549 ev_io_start (EV_A_ &once->io); 1612 ev_io_start (EV_A_ &once->io);
1550 } 1613 }
1551 1614
1552 ev_watcher_init (&once->to, once_cb_to); 1615 ev_init (&once->to, once_cb_to);
1553 if (timeout >= 0.) 1616 if (timeout >= 0.)
1554 { 1617 {
1555 ev_timer_set (&once->to, timeout, 0.); 1618 ev_timer_set (&once->to, timeout, 0.);
1556 ev_timer_start (EV_A_ &once->to); 1619 ev_timer_start (EV_A_ &once->to);
1557 } 1620 }
1558 } 1621 }
1559} 1622}
1560 1623
1624#ifdef __cplusplus
1625}
1626#endif
1627

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines