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

Comparing libev/ev.c (file contents):
Revision 1.80 by root, Fri Nov 9 15:30:59 2007 UTC vs.
Revision 1.86 by root, Sat Nov 10 03:19:21 2007 UTC

126#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 126#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) */ 127#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 */ 128#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 */ 129/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
130 130
131#ifdef EV_H
132# include EV_H
133#else
131#include "ev.h" 134# include "ev.h"
135#endif
132 136
133#if __GNUC__ >= 3 137#if __GNUC__ >= 3
134# define expect(expr,value) __builtin_expect ((expr),(value)) 138# define expect(expr,value) __builtin_expect ((expr),(value))
135# define inline inline 139# define inline inline
136#else 140#else
217 221
218#if EV_MULTIPLICITY 222#if EV_MULTIPLICITY
219 223
220 struct ev_loop 224 struct ev_loop
221 { 225 {
226 ev_tstamp ev_rt_now;
222 #define VAR(name,decl) decl; 227 #define VAR(name,decl) decl;
223 #include "ev_vars.h" 228 #include "ev_vars.h"
224 #undef VAR 229 #undef VAR
225 }; 230 };
226 #include "ev_wrap.h" 231 #include "ev_wrap.h"
228 struct ev_loop default_loop_struct; 233 struct ev_loop default_loop_struct;
229 static struct ev_loop *default_loop; 234 static struct ev_loop *default_loop;
230 235
231#else 236#else
232 237
238 ev_tstamp ev_rt_now;
233 #define VAR(name,decl) static decl; 239 #define VAR(name,decl) static decl;
234 #include "ev_vars.h" 240 #include "ev_vars.h"
235 #undef VAR 241 #undef VAR
236 242
237 static int default_loop; 243 static int default_loop;
267#endif 273#endif
268 274
269 return ev_time (); 275 return ev_time ();
270} 276}
271 277
278#if EV_MULTIPLICITY
272ev_tstamp 279ev_tstamp
273ev_now (EV_P) 280ev_now (EV_P)
274{ 281{
275 return rt_now; 282 return ev_rt_now;
276} 283}
284#endif
277 285
278#define array_roundsize(type,n) ((n) | 4 & ~3) 286#define array_roundsize(type,n) ((n) | 4 & ~3)
279 287
280#define array_needsize(type,base,cur,cnt,init) \ 288#define array_needsize(type,base,cur,cnt,init) \
281 if (expect_false ((cnt) > cur)) \ 289 if (expect_false ((cnt) > cur)) \
514 522
515 heap [k] = w; 523 heap [k] = w;
516 ((W)heap [k])->active = k + 1; 524 ((W)heap [k])->active = k + 1;
517} 525}
518 526
527inline void
528adjustheap (WT *heap, int N, int k, ev_tstamp at)
529{
530 ev_tstamp old_at = heap [k]->at;
531 heap [k]->at = at;
532
533 if (old_at < at)
534 downheap (heap, N, k);
535 else
536 upheap (heap, k);
537}
538
519/*****************************************************************************/ 539/*****************************************************************************/
520 540
521typedef struct 541typedef struct
522{ 542{
523 WL head; 543 WL head;
719 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 739 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
720 have_monotonic = 1; 740 have_monotonic = 1;
721 } 741 }
722#endif 742#endif
723 743
724 rt_now = ev_time (); 744 ev_rt_now = ev_time ();
725 mn_now = get_clock (); 745 mn_now = get_clock ();
726 now_floor = mn_now; 746 now_floor = mn_now;
727 rtmn_diff = rt_now - mn_now; 747 rtmn_diff = ev_rt_now - mn_now;
728 748
729 if (methods == EVMETHOD_AUTO) 749 if (methods == EVMETHOD_AUTO)
730 if (!enable_secure () && getenv ("LIBEV_METHODS")) 750 if (!enable_secure () && getenv ("LIBEV_METHODS"))
731 methods = atoi (getenv ("LIBEV_METHODS")); 751 methods = atoi (getenv ("LIBEV_METHODS"));
732 else 752 else
747#endif 767#endif
748#if EV_USE_SELECT 768#if EV_USE_SELECT
749 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 769 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
750#endif 770#endif
751 771
752 ev_watcher_init (&sigev, sigcb); 772 ev_init (&sigev, sigcb);
753 ev_set_priority (&sigev, EV_MAXPRI); 773 ev_set_priority (&sigev, EV_MAXPRI);
754 } 774 }
755} 775}
756 776
757void 777void
944 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 964 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
945 965
946 if (p->w) 966 if (p->w)
947 { 967 {
948 p->w->pending = 0; 968 p->w->pending = 0;
949 p->w->cb (EV_A_ p->w, p->events); 969 EV_CB_INVOKE (p->w, p->events);
950 } 970 }
951 } 971 }
952} 972}
953 973
954static void 974static void
975} 995}
976 996
977static void 997static void
978periodics_reify (EV_P) 998periodics_reify (EV_P)
979{ 999{
980 while (periodiccnt && ((WT)periodics [0])->at <= rt_now) 1000 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
981 { 1001 {
982 struct ev_periodic *w = periodics [0]; 1002 struct ev_periodic *w = periodics [0];
983 1003
984 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1004 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
985 1005
986 /* first reschedule or stop timer */ 1006 /* first reschedule or stop timer */
987 if (w->reschedule_cb) 1007 if (w->reschedule_cb)
988 { 1008 {
989 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); 1009 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
990 1010
991 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); 1011 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
992 downheap ((WT *)periodics, periodiccnt, 0); 1012 downheap ((WT *)periodics, periodiccnt, 0);
993 } 1013 }
994 else if (w->interval) 1014 else if (w->interval)
995 { 1015 {
996 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 1016 ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
997 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); 1017 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
998 downheap ((WT *)periodics, periodiccnt, 0); 1018 downheap ((WT *)periodics, periodiccnt, 0);
999 } 1019 }
1000 else 1020 else
1001 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1021 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1002 1022
1013 for (i = 0; i < periodiccnt; ++i) 1033 for (i = 0; i < periodiccnt; ++i)
1014 { 1034 {
1015 struct ev_periodic *w = periodics [i]; 1035 struct ev_periodic *w = periodics [i];
1016 1036
1017 if (w->reschedule_cb) 1037 if (w->reschedule_cb)
1018 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1038 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1019 else if (w->interval) 1039 else if (w->interval)
1020 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1040 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1021 } 1041 }
1022 1042
1023 /* now rebuild the heap */ 1043 /* now rebuild the heap */
1024 for (i = periodiccnt >> 1; i--; ) 1044 for (i = periodiccnt >> 1; i--; )
1025 downheap ((WT *)periodics, periodiccnt, i); 1045 downheap ((WT *)periodics, periodiccnt, i);
1030{ 1050{
1031 mn_now = get_clock (); 1051 mn_now = get_clock ();
1032 1052
1033 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1053 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1034 { 1054 {
1035 rt_now = rtmn_diff + mn_now; 1055 ev_rt_now = rtmn_diff + mn_now;
1036 return 0; 1056 return 0;
1037 } 1057 }
1038 else 1058 else
1039 { 1059 {
1040 now_floor = mn_now; 1060 now_floor = mn_now;
1041 rt_now = ev_time (); 1061 ev_rt_now = ev_time ();
1042 return 1; 1062 return 1;
1043 } 1063 }
1044} 1064}
1045 1065
1046static void 1066static void
1055 { 1075 {
1056 ev_tstamp odiff = rtmn_diff; 1076 ev_tstamp odiff = rtmn_diff;
1057 1077
1058 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1078 for (i = 4; --i; ) /* loop a few times, before making important decisions */
1059 { 1079 {
1060 rtmn_diff = rt_now - mn_now; 1080 rtmn_diff = ev_rt_now - mn_now;
1061 1081
1062 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1082 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1063 return; /* all is well */ 1083 return; /* all is well */
1064 1084
1065 rt_now = ev_time (); 1085 ev_rt_now = ev_time ();
1066 mn_now = get_clock (); 1086 mn_now = get_clock ();
1067 now_floor = mn_now; 1087 now_floor = mn_now;
1068 } 1088 }
1069 1089
1070 periodics_reschedule (EV_A); 1090 periodics_reschedule (EV_A);
1073 } 1093 }
1074 } 1094 }
1075 else 1095 else
1076#endif 1096#endif
1077 { 1097 {
1078 rt_now = ev_time (); 1098 ev_rt_now = ev_time ();
1079 1099
1080 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1100 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1081 { 1101 {
1082 periodics_reschedule (EV_A); 1102 periodics_reschedule (EV_A);
1083 1103
1084 /* adjust timers. this is easy, as the offset is the same for all */ 1104 /* adjust timers. this is easy, as the offset is the same for all */
1085 for (i = 0; i < timercnt; ++i) 1105 for (i = 0; i < timercnt; ++i)
1086 ((WT)timers [i])->at += rt_now - mn_now; 1106 ((WT)timers [i])->at += ev_rt_now - mn_now;
1087 } 1107 }
1088 1108
1089 mn_now = rt_now; 1109 mn_now = ev_rt_now;
1090 } 1110 }
1091} 1111}
1092 1112
1093void 1113void
1094ev_ref (EV_P) 1114ev_ref (EV_P)
1134 if (expect_true (have_monotonic)) 1154 if (expect_true (have_monotonic))
1135 time_update_monotonic (EV_A); 1155 time_update_monotonic (EV_A);
1136 else 1156 else
1137#endif 1157#endif
1138 { 1158 {
1139 rt_now = ev_time (); 1159 ev_rt_now = ev_time ();
1140 mn_now = rt_now; 1160 mn_now = ev_rt_now;
1141 } 1161 }
1142 1162
1143 if (flags & EVLOOP_NONBLOCK || idlecnt) 1163 if (flags & EVLOOP_NONBLOCK || idlecnt)
1144 block = 0.; 1164 block = 0.;
1145 else 1165 else
1152 if (block > to) block = to; 1172 if (block > to) block = to;
1153 } 1173 }
1154 1174
1155 if (periodiccnt) 1175 if (periodiccnt)
1156 { 1176 {
1157 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; 1177 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1158 if (block > to) block = to; 1178 if (block > to) block = to;
1159 } 1179 }
1160 1180
1161 if (block < 0.) block = 0.; 1181 if (block < 0.) block = 0.;
1162 } 1182 }
1163 1183
1164 method_poll (EV_A_ block); 1184 method_poll (EV_A_ block);
1165 1185
1166 /* update rt_now, do magic */ 1186 /* update ev_rt_now, do magic */
1167 time_update (EV_A); 1187 time_update (EV_A);
1168 1188
1169 /* queue pending timers and reschedule them */ 1189 /* queue pending timers and reschedule them */
1170 timers_reify (EV_A); /* relative timers called last */ 1190 timers_reify (EV_A); /* relative timers called last */
1171 periodics_reify (EV_A); /* absolute timers called first */ 1191 periodics_reify (EV_A); /* absolute timers called first */
1317ev_timer_again (EV_P_ struct ev_timer *w) 1337ev_timer_again (EV_P_ struct ev_timer *w)
1318{ 1338{
1319 if (ev_is_active (w)) 1339 if (ev_is_active (w))
1320 { 1340 {
1321 if (w->repeat) 1341 if (w->repeat)
1322 {
1323 ((WT)w)->at = mn_now + w->repeat;
1324 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1342 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat);
1325 }
1326 else 1343 else
1327 ev_timer_stop (EV_A_ w); 1344 ev_timer_stop (EV_A_ w);
1328 } 1345 }
1329 else if (w->repeat) 1346 else if (w->repeat)
1330 ev_timer_start (EV_A_ w); 1347 ev_timer_start (EV_A_ w);
1335{ 1352{
1336 if (ev_is_active (w)) 1353 if (ev_is_active (w))
1337 return; 1354 return;
1338 1355
1339 if (w->reschedule_cb) 1356 if (w->reschedule_cb)
1340 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1357 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1341 else if (w->interval) 1358 else if (w->interval)
1342 { 1359 {
1343 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1360 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1344 /* this formula differs from the one in periodic_reify because we do not always round up */ 1361 /* this formula differs from the one in periodic_reify because we do not always round up */
1345 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1362 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1346 } 1363 }
1347 1364
1348 ev_start (EV_A_ (W)w, ++periodiccnt); 1365 ev_start (EV_A_ (W)w, ++periodiccnt);
1349 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1366 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1350 periodics [periodiccnt - 1] = w; 1367 periodics [periodiccnt - 1] = w;
1372} 1389}
1373 1390
1374void 1391void
1375ev_periodic_again (EV_P_ struct ev_periodic *w) 1392ev_periodic_again (EV_P_ struct ev_periodic *w)
1376{ 1393{
1394 /* TODO: use adjustheap and recalculation */
1377 ev_periodic_stop (EV_A_ w); 1395 ev_periodic_stop (EV_A_ w);
1378 ev_periodic_start (EV_A_ w); 1396 ev_periodic_start (EV_A_ w);
1379} 1397}
1380 1398
1381void 1399void
1560 else 1578 else
1561 { 1579 {
1562 once->cb = cb; 1580 once->cb = cb;
1563 once->arg = arg; 1581 once->arg = arg;
1564 1582
1565 ev_watcher_init (&once->io, once_cb_io); 1583 ev_init (&once->io, once_cb_io);
1566 if (fd >= 0) 1584 if (fd >= 0)
1567 { 1585 {
1568 ev_io_set (&once->io, fd, events); 1586 ev_io_set (&once->io, fd, events);
1569 ev_io_start (EV_A_ &once->io); 1587 ev_io_start (EV_A_ &once->io);
1570 } 1588 }
1571 1589
1572 ev_watcher_init (&once->to, once_cb_to); 1590 ev_init (&once->to, once_cb_to);
1573 if (timeout >= 0.) 1591 if (timeout >= 0.)
1574 { 1592 {
1575 ev_timer_set (&once->to, timeout, 0.); 1593 ev_timer_set (&once->to, timeout, 0.);
1576 ev_timer_start (EV_A_ &once->to); 1594 ev_timer_start (EV_A_ &once->to);
1577 } 1595 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines