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

Comparing libev/ev.c (file contents):
Revision 1.82 by root, Fri Nov 9 20:55:09 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)) \
518 522
519 heap [k] = w; 523 heap [k] = w;
520 ((W)heap [k])->active = k + 1; 524 ((W)heap [k])->active = k + 1;
521} 525}
522 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
523/*****************************************************************************/ 539/*****************************************************************************/
524 540
525typedef struct 541typedef struct
526{ 542{
527 WL head; 543 WL head;
723 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 739 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
724 have_monotonic = 1; 740 have_monotonic = 1;
725 } 741 }
726#endif 742#endif
727 743
728 rt_now = ev_time (); 744 ev_rt_now = ev_time ();
729 mn_now = get_clock (); 745 mn_now = get_clock ();
730 now_floor = mn_now; 746 now_floor = mn_now;
731 rtmn_diff = rt_now - mn_now; 747 rtmn_diff = ev_rt_now - mn_now;
732 748
733 if (methods == EVMETHOD_AUTO) 749 if (methods == EVMETHOD_AUTO)
734 if (!enable_secure () && getenv ("LIBEV_METHODS")) 750 if (!enable_secure () && getenv ("LIBEV_METHODS"))
735 methods = atoi (getenv ("LIBEV_METHODS")); 751 methods = atoi (getenv ("LIBEV_METHODS"));
736 else 752 else
751#endif 767#endif
752#if EV_USE_SELECT 768#if EV_USE_SELECT
753 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 769 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
754#endif 770#endif
755 771
756 ev_watcher_init (&sigev, sigcb); 772 ev_init (&sigev, sigcb);
757 ev_set_priority (&sigev, EV_MAXPRI); 773 ev_set_priority (&sigev, EV_MAXPRI);
758 } 774 }
759} 775}
760 776
761void 777void
979} 995}
980 996
981static void 997static void
982periodics_reify (EV_P) 998periodics_reify (EV_P)
983{ 999{
984 while (periodiccnt && ((WT)periodics [0])->at <= rt_now) 1000 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
985 { 1001 {
986 struct ev_periodic *w = periodics [0]; 1002 struct ev_periodic *w = periodics [0];
987 1003
988 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1004 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
989 1005
990 /* first reschedule or stop timer */ 1006 /* first reschedule or stop timer */
991 if (w->reschedule_cb) 1007 if (w->reschedule_cb)
992 { 1008 {
993 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);
994 1010
995 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));
996 downheap ((WT *)periodics, periodiccnt, 0); 1012 downheap ((WT *)periodics, periodiccnt, 0);
997 } 1013 }
998 else if (w->interval) 1014 else if (w->interval)
999 { 1015 {
1000 ((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;
1001 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));
1002 downheap ((WT *)periodics, periodiccnt, 0); 1018 downheap ((WT *)periodics, periodiccnt, 0);
1003 } 1019 }
1004 else 1020 else
1005 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1021 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1006 1022
1017 for (i = 0; i < periodiccnt; ++i) 1033 for (i = 0; i < periodiccnt; ++i)
1018 { 1034 {
1019 struct ev_periodic *w = periodics [i]; 1035 struct ev_periodic *w = periodics [i];
1020 1036
1021 if (w->reschedule_cb) 1037 if (w->reschedule_cb)
1022 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1038 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1023 else if (w->interval) 1039 else if (w->interval)
1024 ((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;
1025 } 1041 }
1026 1042
1027 /* now rebuild the heap */ 1043 /* now rebuild the heap */
1028 for (i = periodiccnt >> 1; i--; ) 1044 for (i = periodiccnt >> 1; i--; )
1029 downheap ((WT *)periodics, periodiccnt, i); 1045 downheap ((WT *)periodics, periodiccnt, i);
1034{ 1050{
1035 mn_now = get_clock (); 1051 mn_now = get_clock ();
1036 1052
1037 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1053 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1038 { 1054 {
1039 rt_now = rtmn_diff + mn_now; 1055 ev_rt_now = rtmn_diff + mn_now;
1040 return 0; 1056 return 0;
1041 } 1057 }
1042 else 1058 else
1043 { 1059 {
1044 now_floor = mn_now; 1060 now_floor = mn_now;
1045 rt_now = ev_time (); 1061 ev_rt_now = ev_time ();
1046 return 1; 1062 return 1;
1047 } 1063 }
1048} 1064}
1049 1065
1050static void 1066static void
1059 { 1075 {
1060 ev_tstamp odiff = rtmn_diff; 1076 ev_tstamp odiff = rtmn_diff;
1061 1077
1062 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 */
1063 { 1079 {
1064 rtmn_diff = rt_now - mn_now; 1080 rtmn_diff = ev_rt_now - mn_now;
1065 1081
1066 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1082 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1067 return; /* all is well */ 1083 return; /* all is well */
1068 1084
1069 rt_now = ev_time (); 1085 ev_rt_now = ev_time ();
1070 mn_now = get_clock (); 1086 mn_now = get_clock ();
1071 now_floor = mn_now; 1087 now_floor = mn_now;
1072 } 1088 }
1073 1089
1074 periodics_reschedule (EV_A); 1090 periodics_reschedule (EV_A);
1077 } 1093 }
1078 } 1094 }
1079 else 1095 else
1080#endif 1096#endif
1081 { 1097 {
1082 rt_now = ev_time (); 1098 ev_rt_now = ev_time ();
1083 1099
1084 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))
1085 { 1101 {
1086 periodics_reschedule (EV_A); 1102 periodics_reschedule (EV_A);
1087 1103
1088 /* 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 */
1089 for (i = 0; i < timercnt; ++i) 1105 for (i = 0; i < timercnt; ++i)
1090 ((WT)timers [i])->at += rt_now - mn_now; 1106 ((WT)timers [i])->at += ev_rt_now - mn_now;
1091 } 1107 }
1092 1108
1093 mn_now = rt_now; 1109 mn_now = ev_rt_now;
1094 } 1110 }
1095} 1111}
1096 1112
1097void 1113void
1098ev_ref (EV_P) 1114ev_ref (EV_P)
1138 if (expect_true (have_monotonic)) 1154 if (expect_true (have_monotonic))
1139 time_update_monotonic (EV_A); 1155 time_update_monotonic (EV_A);
1140 else 1156 else
1141#endif 1157#endif
1142 { 1158 {
1143 rt_now = ev_time (); 1159 ev_rt_now = ev_time ();
1144 mn_now = rt_now; 1160 mn_now = ev_rt_now;
1145 } 1161 }
1146 1162
1147 if (flags & EVLOOP_NONBLOCK || idlecnt) 1163 if (flags & EVLOOP_NONBLOCK || idlecnt)
1148 block = 0.; 1164 block = 0.;
1149 else 1165 else
1156 if (block > to) block = to; 1172 if (block > to) block = to;
1157 } 1173 }
1158 1174
1159 if (periodiccnt) 1175 if (periodiccnt)
1160 { 1176 {
1161 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; 1177 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1162 if (block > to) block = to; 1178 if (block > to) block = to;
1163 } 1179 }
1164 1180
1165 if (block < 0.) block = 0.; 1181 if (block < 0.) block = 0.;
1166 } 1182 }
1167 1183
1168 method_poll (EV_A_ block); 1184 method_poll (EV_A_ block);
1169 1185
1170 /* update rt_now, do magic */ 1186 /* update ev_rt_now, do magic */
1171 time_update (EV_A); 1187 time_update (EV_A);
1172 1188
1173 /* queue pending timers and reschedule them */ 1189 /* queue pending timers and reschedule them */
1174 timers_reify (EV_A); /* relative timers called last */ 1190 timers_reify (EV_A); /* relative timers called last */
1175 periodics_reify (EV_A); /* absolute timers called first */ 1191 periodics_reify (EV_A); /* absolute timers called first */
1321ev_timer_again (EV_P_ struct ev_timer *w) 1337ev_timer_again (EV_P_ struct ev_timer *w)
1322{ 1338{
1323 if (ev_is_active (w)) 1339 if (ev_is_active (w))
1324 { 1340 {
1325 if (w->repeat) 1341 if (w->repeat)
1326 {
1327 ((WT)w)->at = mn_now + w->repeat;
1328 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1342 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat);
1329 }
1330 else 1343 else
1331 ev_timer_stop (EV_A_ w); 1344 ev_timer_stop (EV_A_ w);
1332 } 1345 }
1333 else if (w->repeat) 1346 else if (w->repeat)
1334 ev_timer_start (EV_A_ w); 1347 ev_timer_start (EV_A_ w);
1339{ 1352{
1340 if (ev_is_active (w)) 1353 if (ev_is_active (w))
1341 return; 1354 return;
1342 1355
1343 if (w->reschedule_cb) 1356 if (w->reschedule_cb)
1344 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1357 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1345 else if (w->interval) 1358 else if (w->interval)
1346 { 1359 {
1347 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.));
1348 /* 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 */
1349 ((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;
1350 } 1363 }
1351 1364
1352 ev_start (EV_A_ (W)w, ++periodiccnt); 1365 ev_start (EV_A_ (W)w, ++periodiccnt);
1353 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1366 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1354 periodics [periodiccnt - 1] = w; 1367 periodics [periodiccnt - 1] = w;
1376} 1389}
1377 1390
1378void 1391void
1379ev_periodic_again (EV_P_ struct ev_periodic *w) 1392ev_periodic_again (EV_P_ struct ev_periodic *w)
1380{ 1393{
1394 /* TODO: use adjustheap and recalculation */
1381 ev_periodic_stop (EV_A_ w); 1395 ev_periodic_stop (EV_A_ w);
1382 ev_periodic_start (EV_A_ w); 1396 ev_periodic_start (EV_A_ w);
1383} 1397}
1384 1398
1385void 1399void
1564 else 1578 else
1565 { 1579 {
1566 once->cb = cb; 1580 once->cb = cb;
1567 once->arg = arg; 1581 once->arg = arg;
1568 1582
1569 ev_watcher_init (&once->io, once_cb_io); 1583 ev_init (&once->io, once_cb_io);
1570 if (fd >= 0) 1584 if (fd >= 0)
1571 { 1585 {
1572 ev_io_set (&once->io, fd, events); 1586 ev_io_set (&once->io, fd, events);
1573 ev_io_start (EV_A_ &once->io); 1587 ev_io_start (EV_A_ &once->io);
1574 } 1588 }
1575 1589
1576 ev_watcher_init (&once->to, once_cb_to); 1590 ev_init (&once->to, once_cb_to);
1577 if (timeout >= 0.) 1591 if (timeout >= 0.)
1578 { 1592 {
1579 ev_timer_set (&once->to, timeout, 0.); 1593 ev_timer_set (&once->to, timeout, 0.);
1580 ev_timer_start (EV_A_ &once->to); 1594 ev_timer_start (EV_A_ &once->to);
1581 } 1595 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines