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

Comparing libev/ev.c (file contents):
Revision 1.84 by root, Fri Nov 9 23:04:35 2007 UTC vs.
Revision 1.86 by root, Sat Nov 10 03:19:21 2007 UTC

221 221
222#if EV_MULTIPLICITY 222#if EV_MULTIPLICITY
223 223
224 struct ev_loop 224 struct ev_loop
225 { 225 {
226 ev_tstamp ev_rt_now;
226 #define VAR(name,decl) decl; 227 #define VAR(name,decl) decl;
227 #include "ev_vars.h" 228 #include "ev_vars.h"
228 #undef VAR 229 #undef VAR
229 }; 230 };
230 #include "ev_wrap.h" 231 #include "ev_wrap.h"
232 struct ev_loop default_loop_struct; 233 struct ev_loop default_loop_struct;
233 static struct ev_loop *default_loop; 234 static struct ev_loop *default_loop;
234 235
235#else 236#else
236 237
238 ev_tstamp ev_rt_now;
237 #define VAR(name,decl) static decl; 239 #define VAR(name,decl) static decl;
238 #include "ev_vars.h" 240 #include "ev_vars.h"
239 #undef VAR 241 #undef VAR
240 242
241 static int default_loop; 243 static int default_loop;
271#endif 273#endif
272 274
273 return ev_time (); 275 return ev_time ();
274} 276}
275 277
278#if EV_MULTIPLICITY
276ev_tstamp 279ev_tstamp
277ev_now (EV_P) 280ev_now (EV_P)
278{ 281{
279 return rt_now; 282 return ev_rt_now;
280} 283}
284#endif
281 285
282#define array_roundsize(type,n) ((n) | 4 & ~3) 286#define array_roundsize(type,n) ((n) | 4 & ~3)
283 287
284#define array_needsize(type,base,cur,cnt,init) \ 288#define array_needsize(type,base,cur,cnt,init) \
285 if (expect_false ((cnt) > cur)) \ 289 if (expect_false ((cnt) > cur)) \
735 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 739 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
736 have_monotonic = 1; 740 have_monotonic = 1;
737 } 741 }
738#endif 742#endif
739 743
740 rt_now = ev_time (); 744 ev_rt_now = ev_time ();
741 mn_now = get_clock (); 745 mn_now = get_clock ();
742 now_floor = mn_now; 746 now_floor = mn_now;
743 rtmn_diff = rt_now - mn_now; 747 rtmn_diff = ev_rt_now - mn_now;
744 748
745 if (methods == EVMETHOD_AUTO) 749 if (methods == EVMETHOD_AUTO)
746 if (!enable_secure () && getenv ("LIBEV_METHODS")) 750 if (!enable_secure () && getenv ("LIBEV_METHODS"))
747 methods = atoi (getenv ("LIBEV_METHODS")); 751 methods = atoi (getenv ("LIBEV_METHODS"));
748 else 752 else
991} 995}
992 996
993static void 997static void
994periodics_reify (EV_P) 998periodics_reify (EV_P)
995{ 999{
996 while (periodiccnt && ((WT)periodics [0])->at <= rt_now) 1000 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
997 { 1001 {
998 struct ev_periodic *w = periodics [0]; 1002 struct ev_periodic *w = periodics [0];
999 1003
1000 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1004 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1001 1005
1002 /* first reschedule or stop timer */ 1006 /* first reschedule or stop timer */
1003 if (w->reschedule_cb) 1007 if (w->reschedule_cb)
1004 { 1008 {
1005 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);
1006 1010
1007 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));
1008 downheap ((WT *)periodics, periodiccnt, 0); 1012 downheap ((WT *)periodics, periodiccnt, 0);
1009 } 1013 }
1010 else if (w->interval) 1014 else if (w->interval)
1011 { 1015 {
1012 ((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;
1013 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));
1014 downheap ((WT *)periodics, periodiccnt, 0); 1018 downheap ((WT *)periodics, periodiccnt, 0);
1015 } 1019 }
1016 else 1020 else
1017 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1021 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1018 1022
1029 for (i = 0; i < periodiccnt; ++i) 1033 for (i = 0; i < periodiccnt; ++i)
1030 { 1034 {
1031 struct ev_periodic *w = periodics [i]; 1035 struct ev_periodic *w = periodics [i];
1032 1036
1033 if (w->reschedule_cb) 1037 if (w->reschedule_cb)
1034 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1038 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1035 else if (w->interval) 1039 else if (w->interval)
1036 ((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;
1037 } 1041 }
1038 1042
1039 /* now rebuild the heap */ 1043 /* now rebuild the heap */
1040 for (i = periodiccnt >> 1; i--; ) 1044 for (i = periodiccnt >> 1; i--; )
1041 downheap ((WT *)periodics, periodiccnt, i); 1045 downheap ((WT *)periodics, periodiccnt, i);
1046{ 1050{
1047 mn_now = get_clock (); 1051 mn_now = get_clock ();
1048 1052
1049 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1053 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1050 { 1054 {
1051 rt_now = rtmn_diff + mn_now; 1055 ev_rt_now = rtmn_diff + mn_now;
1052 return 0; 1056 return 0;
1053 } 1057 }
1054 else 1058 else
1055 { 1059 {
1056 now_floor = mn_now; 1060 now_floor = mn_now;
1057 rt_now = ev_time (); 1061 ev_rt_now = ev_time ();
1058 return 1; 1062 return 1;
1059 } 1063 }
1060} 1064}
1061 1065
1062static void 1066static void
1071 { 1075 {
1072 ev_tstamp odiff = rtmn_diff; 1076 ev_tstamp odiff = rtmn_diff;
1073 1077
1074 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 */
1075 { 1079 {
1076 rtmn_diff = rt_now - mn_now; 1080 rtmn_diff = ev_rt_now - mn_now;
1077 1081
1078 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1082 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1079 return; /* all is well */ 1083 return; /* all is well */
1080 1084
1081 rt_now = ev_time (); 1085 ev_rt_now = ev_time ();
1082 mn_now = get_clock (); 1086 mn_now = get_clock ();
1083 now_floor = mn_now; 1087 now_floor = mn_now;
1084 } 1088 }
1085 1089
1086 periodics_reschedule (EV_A); 1090 periodics_reschedule (EV_A);
1089 } 1093 }
1090 } 1094 }
1091 else 1095 else
1092#endif 1096#endif
1093 { 1097 {
1094 rt_now = ev_time (); 1098 ev_rt_now = ev_time ();
1095 1099
1096 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))
1097 { 1101 {
1098 periodics_reschedule (EV_A); 1102 periodics_reschedule (EV_A);
1099 1103
1100 /* 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 */
1101 for (i = 0; i < timercnt; ++i) 1105 for (i = 0; i < timercnt; ++i)
1102 ((WT)timers [i])->at += rt_now - mn_now; 1106 ((WT)timers [i])->at += ev_rt_now - mn_now;
1103 } 1107 }
1104 1108
1105 mn_now = rt_now; 1109 mn_now = ev_rt_now;
1106 } 1110 }
1107} 1111}
1108 1112
1109void 1113void
1110ev_ref (EV_P) 1114ev_ref (EV_P)
1150 if (expect_true (have_monotonic)) 1154 if (expect_true (have_monotonic))
1151 time_update_monotonic (EV_A); 1155 time_update_monotonic (EV_A);
1152 else 1156 else
1153#endif 1157#endif
1154 { 1158 {
1155 rt_now = ev_time (); 1159 ev_rt_now = ev_time ();
1156 mn_now = rt_now; 1160 mn_now = ev_rt_now;
1157 } 1161 }
1158 1162
1159 if (flags & EVLOOP_NONBLOCK || idlecnt) 1163 if (flags & EVLOOP_NONBLOCK || idlecnt)
1160 block = 0.; 1164 block = 0.;
1161 else 1165 else
1168 if (block > to) block = to; 1172 if (block > to) block = to;
1169 } 1173 }
1170 1174
1171 if (periodiccnt) 1175 if (periodiccnt)
1172 { 1176 {
1173 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; 1177 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1174 if (block > to) block = to; 1178 if (block > to) block = to;
1175 } 1179 }
1176 1180
1177 if (block < 0.) block = 0.; 1181 if (block < 0.) block = 0.;
1178 } 1182 }
1179 1183
1180 method_poll (EV_A_ block); 1184 method_poll (EV_A_ block);
1181 1185
1182 /* update rt_now, do magic */ 1186 /* update ev_rt_now, do magic */
1183 time_update (EV_A); 1187 time_update (EV_A);
1184 1188
1185 /* queue pending timers and reschedule them */ 1189 /* queue pending timers and reschedule them */
1186 timers_reify (EV_A); /* relative timers called last */ 1190 timers_reify (EV_A); /* relative timers called last */
1187 periodics_reify (EV_A); /* absolute timers called first */ 1191 periodics_reify (EV_A); /* absolute timers called first */
1348{ 1352{
1349 if (ev_is_active (w)) 1353 if (ev_is_active (w))
1350 return; 1354 return;
1351 1355
1352 if (w->reschedule_cb) 1356 if (w->reschedule_cb)
1353 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1357 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1354 else if (w->interval) 1358 else if (w->interval)
1355 { 1359 {
1356 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.));
1357 /* 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 */
1358 ((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;
1359 } 1363 }
1360 1364
1361 ev_start (EV_A_ (W)w, ++periodiccnt); 1365 ev_start (EV_A_ (W)w, ++periodiccnt);
1362 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1366 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1363 periodics [periodiccnt - 1] = w; 1367 periodics [periodiccnt - 1] = w;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines