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.87 by root, Sat Nov 10 03:36:15 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
221 226
222#if EV_MULTIPLICITY 227#if EV_MULTIPLICITY
223 228
224 struct ev_loop 229 struct ev_loop
225 { 230 {
231 ev_tstamp ev_rt_now;
226 #define VAR(name,decl) decl; 232 #define VAR(name,decl) decl;
227 #include "ev_vars.h" 233 #include "ev_vars.h"
228 #undef VAR 234 #undef VAR
229 }; 235 };
230 #include "ev_wrap.h" 236 #include "ev_wrap.h"
232 struct ev_loop default_loop_struct; 238 struct ev_loop default_loop_struct;
233 static struct ev_loop *default_loop; 239 static struct ev_loop *default_loop;
234 240
235#else 241#else
236 242
243 ev_tstamp ev_rt_now;
237 #define VAR(name,decl) static decl; 244 #define VAR(name,decl) static decl;
238 #include "ev_vars.h" 245 #include "ev_vars.h"
239 #undef VAR 246 #undef VAR
240 247
241 static int default_loop; 248 static int default_loop;
271#endif 278#endif
272 279
273 return ev_time (); 280 return ev_time ();
274} 281}
275 282
283#if EV_MULTIPLICITY
276ev_tstamp 284ev_tstamp
277ev_now (EV_P) 285ev_now (EV_P)
278{ 286{
279 return rt_now; 287 return ev_rt_now;
280} 288}
289#endif
281 290
282#define array_roundsize(type,n) ((n) | 4 & ~3) 291#define array_roundsize(type,n) ((n) | 4 & ~3)
283 292
284#define array_needsize(type,base,cur,cnt,init) \ 293#define array_needsize(type,base,cur,cnt,init) \
285 if (expect_false ((cnt) > cur)) \ 294 if (expect_false ((cnt) > cur)) \
518 527
519 heap [k] = w; 528 heap [k] = w;
520 ((W)heap [k])->active = k + 1; 529 ((W)heap [k])->active = k + 1;
521} 530}
522 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
523/*****************************************************************************/ 544/*****************************************************************************/
524 545
525typedef struct 546typedef struct
526{ 547{
527 WL head; 548 WL head;
723 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 744 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
724 have_monotonic = 1; 745 have_monotonic = 1;
725 } 746 }
726#endif 747#endif
727 748
728 rt_now = ev_time (); 749 ev_rt_now = ev_time ();
729 mn_now = get_clock (); 750 mn_now = get_clock ();
730 now_floor = mn_now; 751 now_floor = mn_now;
731 rtmn_diff = rt_now - mn_now; 752 rtmn_diff = ev_rt_now - mn_now;
732 753
733 if (methods == EVMETHOD_AUTO) 754 if (methods == EVMETHOD_AUTO)
734 if (!enable_secure () && getenv ("LIBEV_METHODS")) 755 if (!enable_secure () && getenv ("LIBEV_METHODS"))
735 methods = atoi (getenv ("LIBEV_METHODS")); 756 methods = atoi (getenv ("LIBEV_METHODS"));
736 else 757 else
751#endif 772#endif
752#if EV_USE_SELECT 773#if EV_USE_SELECT
753 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 774 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
754#endif 775#endif
755 776
756 ev_watcher_init (&sigev, sigcb); 777 ev_init (&sigev, sigcb);
757 ev_set_priority (&sigev, EV_MAXPRI); 778 ev_set_priority (&sigev, EV_MAXPRI);
758 } 779 }
759} 780}
760 781
761void 782void
979} 1000}
980 1001
981static void 1002static void
982periodics_reify (EV_P) 1003periodics_reify (EV_P)
983{ 1004{
984 while (periodiccnt && ((WT)periodics [0])->at <= rt_now) 1005 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
985 { 1006 {
986 struct ev_periodic *w = periodics [0]; 1007 struct ev_periodic *w = periodics [0];
987 1008
988 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1009 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
989 1010
990 /* first reschedule or stop timer */ 1011 /* first reschedule or stop timer */
991 if (w->reschedule_cb) 1012 if (w->reschedule_cb)
992 { 1013 {
993 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); 1014 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
994 1015
995 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); 1016 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
996 downheap ((WT *)periodics, periodiccnt, 0); 1017 downheap ((WT *)periodics, periodiccnt, 0);
997 } 1018 }
998 else if (w->interval) 1019 else if (w->interval)
999 { 1020 {
1000 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 1021 ((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)); 1022 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); 1023 downheap ((WT *)periodics, periodiccnt, 0);
1003 } 1024 }
1004 else 1025 else
1005 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1026 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1006 1027
1017 for (i = 0; i < periodiccnt; ++i) 1038 for (i = 0; i < periodiccnt; ++i)
1018 { 1039 {
1019 struct ev_periodic *w = periodics [i]; 1040 struct ev_periodic *w = periodics [i];
1020 1041
1021 if (w->reschedule_cb) 1042 if (w->reschedule_cb)
1022 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1043 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1023 else if (w->interval) 1044 else if (w->interval)
1024 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1045 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1025 } 1046 }
1026 1047
1027 /* now rebuild the heap */ 1048 /* now rebuild the heap */
1028 for (i = periodiccnt >> 1; i--; ) 1049 for (i = periodiccnt >> 1; i--; )
1029 downheap ((WT *)periodics, periodiccnt, i); 1050 downheap ((WT *)periodics, periodiccnt, i);
1034{ 1055{
1035 mn_now = get_clock (); 1056 mn_now = get_clock ();
1036 1057
1037 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1058 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1038 { 1059 {
1039 rt_now = rtmn_diff + mn_now; 1060 ev_rt_now = rtmn_diff + mn_now;
1040 return 0; 1061 return 0;
1041 } 1062 }
1042 else 1063 else
1043 { 1064 {
1044 now_floor = mn_now; 1065 now_floor = mn_now;
1045 rt_now = ev_time (); 1066 ev_rt_now = ev_time ();
1046 return 1; 1067 return 1;
1047 } 1068 }
1048} 1069}
1049 1070
1050static void 1071static void
1059 { 1080 {
1060 ev_tstamp odiff = rtmn_diff; 1081 ev_tstamp odiff = rtmn_diff;
1061 1082
1062 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1083 for (i = 4; --i; ) /* loop a few times, before making important decisions */
1063 { 1084 {
1064 rtmn_diff = rt_now - mn_now; 1085 rtmn_diff = ev_rt_now - mn_now;
1065 1086
1066 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1087 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1067 return; /* all is well */ 1088 return; /* all is well */
1068 1089
1069 rt_now = ev_time (); 1090 ev_rt_now = ev_time ();
1070 mn_now = get_clock (); 1091 mn_now = get_clock ();
1071 now_floor = mn_now; 1092 now_floor = mn_now;
1072 } 1093 }
1073 1094
1074 periodics_reschedule (EV_A); 1095 periodics_reschedule (EV_A);
1077 } 1098 }
1078 } 1099 }
1079 else 1100 else
1080#endif 1101#endif
1081 { 1102 {
1082 rt_now = ev_time (); 1103 ev_rt_now = ev_time ();
1083 1104
1084 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1105 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1085 { 1106 {
1086 periodics_reschedule (EV_A); 1107 periodics_reschedule (EV_A);
1087 1108
1088 /* adjust timers. this is easy, as the offset is the same for all */ 1109 /* adjust timers. this is easy, as the offset is the same for all */
1089 for (i = 0; i < timercnt; ++i) 1110 for (i = 0; i < timercnt; ++i)
1090 ((WT)timers [i])->at += rt_now - mn_now; 1111 ((WT)timers [i])->at += ev_rt_now - mn_now;
1091 } 1112 }
1092 1113
1093 mn_now = rt_now; 1114 mn_now = ev_rt_now;
1094 } 1115 }
1095} 1116}
1096 1117
1097void 1118void
1098ev_ref (EV_P) 1119ev_ref (EV_P)
1138 if (expect_true (have_monotonic)) 1159 if (expect_true (have_monotonic))
1139 time_update_monotonic (EV_A); 1160 time_update_monotonic (EV_A);
1140 else 1161 else
1141#endif 1162#endif
1142 { 1163 {
1143 rt_now = ev_time (); 1164 ev_rt_now = ev_time ();
1144 mn_now = rt_now; 1165 mn_now = ev_rt_now;
1145 } 1166 }
1146 1167
1147 if (flags & EVLOOP_NONBLOCK || idlecnt) 1168 if (flags & EVLOOP_NONBLOCK || idlecnt)
1148 block = 0.; 1169 block = 0.;
1149 else 1170 else
1156 if (block > to) block = to; 1177 if (block > to) block = to;
1157 } 1178 }
1158 1179
1159 if (periodiccnt) 1180 if (periodiccnt)
1160 { 1181 {
1161 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; 1182 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1162 if (block > to) block = to; 1183 if (block > to) block = to;
1163 } 1184 }
1164 1185
1165 if (block < 0.) block = 0.; 1186 if (block < 0.) block = 0.;
1166 } 1187 }
1167 1188
1168 method_poll (EV_A_ block); 1189 method_poll (EV_A_ block);
1169 1190
1170 /* update rt_now, do magic */ 1191 /* update ev_rt_now, do magic */
1171 time_update (EV_A); 1192 time_update (EV_A);
1172 1193
1173 /* queue pending timers and reschedule them */ 1194 /* queue pending timers and reschedule them */
1174 timers_reify (EV_A); /* relative timers called last */ 1195 timers_reify (EV_A); /* relative timers called last */
1175 periodics_reify (EV_A); /* absolute timers called first */ 1196 periodics_reify (EV_A); /* absolute timers called first */
1321ev_timer_again (EV_P_ struct ev_timer *w) 1342ev_timer_again (EV_P_ struct ev_timer *w)
1322{ 1343{
1323 if (ev_is_active (w)) 1344 if (ev_is_active (w))
1324 { 1345 {
1325 if (w->repeat) 1346 if (w->repeat)
1326 {
1327 ((WT)w)->at = mn_now + w->repeat;
1328 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1347 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat);
1329 }
1330 else 1348 else
1331 ev_timer_stop (EV_A_ w); 1349 ev_timer_stop (EV_A_ w);
1332 } 1350 }
1333 else if (w->repeat) 1351 else if (w->repeat)
1334 ev_timer_start (EV_A_ w); 1352 ev_timer_start (EV_A_ w);
1339{ 1357{
1340 if (ev_is_active (w)) 1358 if (ev_is_active (w))
1341 return; 1359 return;
1342 1360
1343 if (w->reschedule_cb) 1361 if (w->reschedule_cb)
1344 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1362 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1345 else if (w->interval) 1363 else if (w->interval)
1346 { 1364 {
1347 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1365 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 */ 1366 /* 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; 1367 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1350 } 1368 }
1351 1369
1352 ev_start (EV_A_ (W)w, ++periodiccnt); 1370 ev_start (EV_A_ (W)w, ++periodiccnt);
1353 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1371 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1354 periodics [periodiccnt - 1] = w; 1372 periodics [periodiccnt - 1] = w;
1376} 1394}
1377 1395
1378void 1396void
1379ev_periodic_again (EV_P_ struct ev_periodic *w) 1397ev_periodic_again (EV_P_ struct ev_periodic *w)
1380{ 1398{
1399 /* TODO: use adjustheap and recalculation */
1381 ev_periodic_stop (EV_A_ w); 1400 ev_periodic_stop (EV_A_ w);
1382 ev_periodic_start (EV_A_ w); 1401 ev_periodic_start (EV_A_ w);
1383} 1402}
1384 1403
1385void 1404void
1564 else 1583 else
1565 { 1584 {
1566 once->cb = cb; 1585 once->cb = cb;
1567 once->arg = arg; 1586 once->arg = arg;
1568 1587
1569 ev_watcher_init (&once->io, once_cb_io); 1588 ev_init (&once->io, once_cb_io);
1570 if (fd >= 0) 1589 if (fd >= 0)
1571 { 1590 {
1572 ev_io_set (&once->io, fd, events); 1591 ev_io_set (&once->io, fd, events);
1573 ev_io_start (EV_A_ &once->io); 1592 ev_io_start (EV_A_ &once->io);
1574 } 1593 }
1575 1594
1576 ev_watcher_init (&once->to, once_cb_to); 1595 ev_init (&once->to, once_cb_to);
1577 if (timeout >= 0.) 1596 if (timeout >= 0.)
1578 { 1597 {
1579 ev_timer_set (&once->to, timeout, 0.); 1598 ev_timer_set (&once->to, timeout, 0.);
1580 ev_timer_start (EV_A_ &once->to); 1599 ev_timer_start (EV_A_ &once->to);
1581 } 1600 }
1582 } 1601 }
1583} 1602}
1584 1603
1604#ifdef __cplusplus
1605}
1606#endif
1607

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines