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.85 by root, Sat Nov 10 03:13:50 2007 UTC

271#endif 271#endif
272 272
273 return ev_time (); 273 return ev_time ();
274} 274}
275 275
276#if EV_MULTIPLICITY
276ev_tstamp 277ev_tstamp
277ev_now (EV_P) 278ev_now (EV_P)
278{ 279{
279 return rt_now; 280 return ev_rt_now;
280} 281}
282#endif
281 283
282#define array_roundsize(type,n) ((n) | 4 & ~3) 284#define array_roundsize(type,n) ((n) | 4 & ~3)
283 285
284#define array_needsize(type,base,cur,cnt,init) \ 286#define array_needsize(type,base,cur,cnt,init) \
285 if (expect_false ((cnt) > cur)) \ 287 if (expect_false ((cnt) > cur)) \
518 520
519 heap [k] = w; 521 heap [k] = w;
520 ((W)heap [k])->active = k + 1; 522 ((W)heap [k])->active = k + 1;
521} 523}
522 524
525inline void
526adjustheap (WT *heap, int N, int k, ev_tstamp at)
527{
528 ev_tstamp old_at = heap [k]->at;
529 heap [k]->at = at;
530
531 if (old_at < at)
532 downheap (heap, N, k);
533 else
534 upheap (heap, k);
535}
536
523/*****************************************************************************/ 537/*****************************************************************************/
524 538
525typedef struct 539typedef struct
526{ 540{
527 WL head; 541 WL head;
723 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 737 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
724 have_monotonic = 1; 738 have_monotonic = 1;
725 } 739 }
726#endif 740#endif
727 741
728 rt_now = ev_time (); 742 ev_rt_now = ev_time ();
729 mn_now = get_clock (); 743 mn_now = get_clock ();
730 now_floor = mn_now; 744 now_floor = mn_now;
731 rtmn_diff = rt_now - mn_now; 745 rtmn_diff = ev_rt_now - mn_now;
732 746
733 if (methods == EVMETHOD_AUTO) 747 if (methods == EVMETHOD_AUTO)
734 if (!enable_secure () && getenv ("LIBEV_METHODS")) 748 if (!enable_secure () && getenv ("LIBEV_METHODS"))
735 methods = atoi (getenv ("LIBEV_METHODS")); 749 methods = atoi (getenv ("LIBEV_METHODS"));
736 else 750 else
751#endif 765#endif
752#if EV_USE_SELECT 766#if EV_USE_SELECT
753 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 767 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
754#endif 768#endif
755 769
756 ev_watcher_init (&sigev, sigcb); 770 ev_init (&sigev, sigcb);
757 ev_set_priority (&sigev, EV_MAXPRI); 771 ev_set_priority (&sigev, EV_MAXPRI);
758 } 772 }
759} 773}
760 774
761void 775void
979} 993}
980 994
981static void 995static void
982periodics_reify (EV_P) 996periodics_reify (EV_P)
983{ 997{
984 while (periodiccnt && ((WT)periodics [0])->at <= rt_now) 998 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
985 { 999 {
986 struct ev_periodic *w = periodics [0]; 1000 struct ev_periodic *w = periodics [0];
987 1001
988 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1002 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
989 1003
990 /* first reschedule or stop timer */ 1004 /* first reschedule or stop timer */
991 if (w->reschedule_cb) 1005 if (w->reschedule_cb)
992 { 1006 {
993 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); 1007 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
994 1008
995 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); 1009 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
996 downheap ((WT *)periodics, periodiccnt, 0); 1010 downheap ((WT *)periodics, periodiccnt, 0);
997 } 1011 }
998 else if (w->interval) 1012 else if (w->interval)
999 { 1013 {
1000 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 1014 ((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)); 1015 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); 1016 downheap ((WT *)periodics, periodiccnt, 0);
1003 } 1017 }
1004 else 1018 else
1005 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1019 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1006 1020
1017 for (i = 0; i < periodiccnt; ++i) 1031 for (i = 0; i < periodiccnt; ++i)
1018 { 1032 {
1019 struct ev_periodic *w = periodics [i]; 1033 struct ev_periodic *w = periodics [i];
1020 1034
1021 if (w->reschedule_cb) 1035 if (w->reschedule_cb)
1022 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1036 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1023 else if (w->interval) 1037 else if (w->interval)
1024 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1038 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1025 } 1039 }
1026 1040
1027 /* now rebuild the heap */ 1041 /* now rebuild the heap */
1028 for (i = periodiccnt >> 1; i--; ) 1042 for (i = periodiccnt >> 1; i--; )
1029 downheap ((WT *)periodics, periodiccnt, i); 1043 downheap ((WT *)periodics, periodiccnt, i);
1034{ 1048{
1035 mn_now = get_clock (); 1049 mn_now = get_clock ();
1036 1050
1037 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1051 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1038 { 1052 {
1039 rt_now = rtmn_diff + mn_now; 1053 ev_rt_now = rtmn_diff + mn_now;
1040 return 0; 1054 return 0;
1041 } 1055 }
1042 else 1056 else
1043 { 1057 {
1044 now_floor = mn_now; 1058 now_floor = mn_now;
1045 rt_now = ev_time (); 1059 ev_rt_now = ev_time ();
1046 return 1; 1060 return 1;
1047 } 1061 }
1048} 1062}
1049 1063
1050static void 1064static void
1059 { 1073 {
1060 ev_tstamp odiff = rtmn_diff; 1074 ev_tstamp odiff = rtmn_diff;
1061 1075
1062 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1076 for (i = 4; --i; ) /* loop a few times, before making important decisions */
1063 { 1077 {
1064 rtmn_diff = rt_now - mn_now; 1078 rtmn_diff = ev_rt_now - mn_now;
1065 1079
1066 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1080 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1067 return; /* all is well */ 1081 return; /* all is well */
1068 1082
1069 rt_now = ev_time (); 1083 ev_rt_now = ev_time ();
1070 mn_now = get_clock (); 1084 mn_now = get_clock ();
1071 now_floor = mn_now; 1085 now_floor = mn_now;
1072 } 1086 }
1073 1087
1074 periodics_reschedule (EV_A); 1088 periodics_reschedule (EV_A);
1077 } 1091 }
1078 } 1092 }
1079 else 1093 else
1080#endif 1094#endif
1081 { 1095 {
1082 rt_now = ev_time (); 1096 ev_rt_now = ev_time ();
1083 1097
1084 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1098 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1085 { 1099 {
1086 periodics_reschedule (EV_A); 1100 periodics_reschedule (EV_A);
1087 1101
1088 /* adjust timers. this is easy, as the offset is the same for all */ 1102 /* adjust timers. this is easy, as the offset is the same for all */
1089 for (i = 0; i < timercnt; ++i) 1103 for (i = 0; i < timercnt; ++i)
1090 ((WT)timers [i])->at += rt_now - mn_now; 1104 ((WT)timers [i])->at += ev_rt_now - mn_now;
1091 } 1105 }
1092 1106
1093 mn_now = rt_now; 1107 mn_now = ev_rt_now;
1094 } 1108 }
1095} 1109}
1096 1110
1097void 1111void
1098ev_ref (EV_P) 1112ev_ref (EV_P)
1138 if (expect_true (have_monotonic)) 1152 if (expect_true (have_monotonic))
1139 time_update_monotonic (EV_A); 1153 time_update_monotonic (EV_A);
1140 else 1154 else
1141#endif 1155#endif
1142 { 1156 {
1143 rt_now = ev_time (); 1157 ev_rt_now = ev_time ();
1144 mn_now = rt_now; 1158 mn_now = ev_rt_now;
1145 } 1159 }
1146 1160
1147 if (flags & EVLOOP_NONBLOCK || idlecnt) 1161 if (flags & EVLOOP_NONBLOCK || idlecnt)
1148 block = 0.; 1162 block = 0.;
1149 else 1163 else
1156 if (block > to) block = to; 1170 if (block > to) block = to;
1157 } 1171 }
1158 1172
1159 if (periodiccnt) 1173 if (periodiccnt)
1160 { 1174 {
1161 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; 1175 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1162 if (block > to) block = to; 1176 if (block > to) block = to;
1163 } 1177 }
1164 1178
1165 if (block < 0.) block = 0.; 1179 if (block < 0.) block = 0.;
1166 } 1180 }
1167 1181
1168 method_poll (EV_A_ block); 1182 method_poll (EV_A_ block);
1169 1183
1170 /* update rt_now, do magic */ 1184 /* update ev_rt_now, do magic */
1171 time_update (EV_A); 1185 time_update (EV_A);
1172 1186
1173 /* queue pending timers and reschedule them */ 1187 /* queue pending timers and reschedule them */
1174 timers_reify (EV_A); /* relative timers called last */ 1188 timers_reify (EV_A); /* relative timers called last */
1175 periodics_reify (EV_A); /* absolute timers called first */ 1189 periodics_reify (EV_A); /* absolute timers called first */
1321ev_timer_again (EV_P_ struct ev_timer *w) 1335ev_timer_again (EV_P_ struct ev_timer *w)
1322{ 1336{
1323 if (ev_is_active (w)) 1337 if (ev_is_active (w))
1324 { 1338 {
1325 if (w->repeat) 1339 if (w->repeat)
1326 {
1327 ((WT)w)->at = mn_now + w->repeat;
1328 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1340 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat);
1329 }
1330 else 1341 else
1331 ev_timer_stop (EV_A_ w); 1342 ev_timer_stop (EV_A_ w);
1332 } 1343 }
1333 else if (w->repeat) 1344 else if (w->repeat)
1334 ev_timer_start (EV_A_ w); 1345 ev_timer_start (EV_A_ w);
1339{ 1350{
1340 if (ev_is_active (w)) 1351 if (ev_is_active (w))
1341 return; 1352 return;
1342 1353
1343 if (w->reschedule_cb) 1354 if (w->reschedule_cb)
1344 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1355 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1345 else if (w->interval) 1356 else if (w->interval)
1346 { 1357 {
1347 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1358 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 */ 1359 /* 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; 1360 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1350 } 1361 }
1351 1362
1352 ev_start (EV_A_ (W)w, ++periodiccnt); 1363 ev_start (EV_A_ (W)w, ++periodiccnt);
1353 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1364 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1354 periodics [periodiccnt - 1] = w; 1365 periodics [periodiccnt - 1] = w;
1376} 1387}
1377 1388
1378void 1389void
1379ev_periodic_again (EV_P_ struct ev_periodic *w) 1390ev_periodic_again (EV_P_ struct ev_periodic *w)
1380{ 1391{
1392 /* TODO: use adjustheap and recalculation */
1381 ev_periodic_stop (EV_A_ w); 1393 ev_periodic_stop (EV_A_ w);
1382 ev_periodic_start (EV_A_ w); 1394 ev_periodic_start (EV_A_ w);
1383} 1395}
1384 1396
1385void 1397void
1564 else 1576 else
1565 { 1577 {
1566 once->cb = cb; 1578 once->cb = cb;
1567 once->arg = arg; 1579 once->arg = arg;
1568 1580
1569 ev_watcher_init (&once->io, once_cb_io); 1581 ev_init (&once->io, once_cb_io);
1570 if (fd >= 0) 1582 if (fd >= 0)
1571 { 1583 {
1572 ev_io_set (&once->io, fd, events); 1584 ev_io_set (&once->io, fd, events);
1573 ev_io_start (EV_A_ &once->io); 1585 ev_io_start (EV_A_ &once->io);
1574 } 1586 }
1575 1587
1576 ev_watcher_init (&once->to, once_cb_to); 1588 ev_init (&once->to, once_cb_to);
1577 if (timeout >= 0.) 1589 if (timeout >= 0.)
1578 { 1590 {
1579 ev_timer_set (&once->to, timeout, 0.); 1591 ev_timer_set (&once->to, timeout, 0.);
1580 ev_timer_start (EV_A_ &once->to); 1592 ev_timer_start (EV_A_ &once->to);
1581 } 1593 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines