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

Comparing libev/ev.c (file contents):
Revision 1.79 by root, Fri Nov 9 15:15:20 2007 UTC vs.
Revision 1.85 by root, Sat Nov 10 03:13:50 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
215 int events; 219 int events;
216} ANPENDING; 220} ANPENDING;
217 221
218#if EV_MULTIPLICITY 222#if EV_MULTIPLICITY
219 223
220struct ev_loop 224 struct ev_loop
221{ 225 {
222# define VAR(name,decl) decl; 226 #define VAR(name,decl) decl;
223# include "ev_vars.h" 227 #include "ev_vars.h"
224};
225# undef VAR 228 #undef VAR
229 };
226# include "ev_wrap.h" 230 #include "ev_wrap.h"
231
232 struct ev_loop default_loop_struct;
233 static struct ev_loop *default_loop;
227 234
228#else 235#else
229 236
230# define VAR(name,decl) static decl; 237 #define VAR(name,decl) static decl;
231# include "ev_vars.h" 238 #include "ev_vars.h"
232# undef VAR 239 #undef VAR
240
241 static int default_loop;
233 242
234#endif 243#endif
235 244
236/*****************************************************************************/ 245/*****************************************************************************/
237 246
262#endif 271#endif
263 272
264 return ev_time (); 273 return ev_time ();
265} 274}
266 275
276#if EV_MULTIPLICITY
267ev_tstamp 277ev_tstamp
268ev_now (EV_P) 278ev_now (EV_P)
269{ 279{
270 return rt_now; 280 return ev_rt_now;
271} 281}
282#endif
272 283
273#define array_roundsize(type,n) ((n) | 4 & ~3) 284#define array_roundsize(type,n) ((n) | 4 & ~3)
274 285
275#define array_needsize(type,base,cur,cnt,init) \ 286#define array_needsize(type,base,cur,cnt,init) \
276 if (expect_false ((cnt) > cur)) \ 287 if (expect_false ((cnt) > cur)) \
509 520
510 heap [k] = w; 521 heap [k] = w;
511 ((W)heap [k])->active = k + 1; 522 ((W)heap [k])->active = k + 1;
512} 523}
513 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
514/*****************************************************************************/ 537/*****************************************************************************/
515 538
516typedef struct 539typedef struct
517{ 540{
518 WL head; 541 WL head;
561} 584}
562 585
563void 586void
564ev_feed_signal_event (EV_P_ int signum) 587ev_feed_signal_event (EV_P_ int signum)
565{ 588{
589 WL w;
590
566#if EV_MULTIPLICITY 591#if EV_MULTIPLICITY
567 assert (("feeding signal events is only supported in the default loop", loop == default_loop)); 592 assert (("feeding signal events is only supported in the default loop", loop == default_loop));
568#endif 593#endif
569 594
570 --signum; 595 --signum;
579} 604}
580 605
581static void 606static void
582sigcb (EV_P_ struct ev_io *iow, int revents) 607sigcb (EV_P_ struct ev_io *iow, int revents)
583{ 608{
584 WL w;
585 int signum; 609 int signum;
586 610
587#ifdef WIN32 611#ifdef WIN32
588 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); 612 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
589#else 613#else
591#endif 615#endif
592 gotsig = 0; 616 gotsig = 0;
593 617
594 for (signum = signalmax; signum--; ) 618 for (signum = signalmax; signum--; )
595 if (signals [signum].gotsig) 619 if (signals [signum].gotsig)
596 sigevent (EV_A_ signum + 1); 620 ev_feed_signal_event (EV_A_ signum + 1);
597} 621}
598 622
599static void 623static void
600siginit (EV_P) 624siginit (EV_P)
601{ 625{
713 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 737 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
714 have_monotonic = 1; 738 have_monotonic = 1;
715 } 739 }
716#endif 740#endif
717 741
718 rt_now = ev_time (); 742 ev_rt_now = ev_time ();
719 mn_now = get_clock (); 743 mn_now = get_clock ();
720 now_floor = mn_now; 744 now_floor = mn_now;
721 rtmn_diff = rt_now - mn_now; 745 rtmn_diff = ev_rt_now - mn_now;
722 746
723 if (methods == EVMETHOD_AUTO) 747 if (methods == EVMETHOD_AUTO)
724 if (!enable_secure () && getenv ("LIBEV_METHODS")) 748 if (!enable_secure () && getenv ("LIBEV_METHODS"))
725 methods = atoi (getenv ("LIBEV_METHODS")); 749 methods = atoi (getenv ("LIBEV_METHODS"));
726 else 750 else
741#endif 765#endif
742#if EV_USE_SELECT 766#if EV_USE_SELECT
743 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 767 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
744#endif 768#endif
745 769
746 ev_watcher_init (&sigev, sigcb); 770 ev_init (&sigev, sigcb);
747 ev_set_priority (&sigev, EV_MAXPRI); 771 ev_set_priority (&sigev, EV_MAXPRI);
748 } 772 }
749} 773}
750 774
751void 775void
841} 865}
842 866
843#endif 867#endif
844 868
845#if EV_MULTIPLICITY 869#if EV_MULTIPLICITY
846struct ev_loop default_loop_struct;
847static struct ev_loop *default_loop;
848
849struct ev_loop * 870struct ev_loop *
850#else 871#else
851static int default_loop;
852
853int 872int
854#endif 873#endif
855ev_default_loop (int methods) 874ev_default_loop (int methods)
856{ 875{
857 if (sigpipe [0] == sigpipe [1]) 876 if (sigpipe [0] == sigpipe [1])
943 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 962 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
944 963
945 if (p->w) 964 if (p->w)
946 { 965 {
947 p->w->pending = 0; 966 p->w->pending = 0;
948 p->w->cb (EV_A_ p->w, p->events); 967 EV_CB_INVOKE (p->w, p->events);
949 } 968 }
950 } 969 }
951} 970}
952 971
953static void 972static void
974} 993}
975 994
976static void 995static void
977periodics_reify (EV_P) 996periodics_reify (EV_P)
978{ 997{
979 while (periodiccnt && ((WT)periodics [0])->at <= rt_now) 998 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
980 { 999 {
981 struct ev_periodic *w = periodics [0]; 1000 struct ev_periodic *w = periodics [0];
982 1001
983 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1002 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
984 1003
985 /* first reschedule or stop timer */ 1004 /* first reschedule or stop timer */
986 if (w->reschedule_cb) 1005 if (w->reschedule_cb)
987 { 1006 {
988 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);
989 1008
990 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));
991 downheap ((WT *)periodics, periodiccnt, 0); 1010 downheap ((WT *)periodics, periodiccnt, 0);
992 } 1011 }
993 else if (w->interval) 1012 else if (w->interval)
994 { 1013 {
995 ((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;
996 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));
997 downheap ((WT *)periodics, periodiccnt, 0); 1016 downheap ((WT *)periodics, periodiccnt, 0);
998 } 1017 }
999 else 1018 else
1000 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1019 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1001 1020
1012 for (i = 0; i < periodiccnt; ++i) 1031 for (i = 0; i < periodiccnt; ++i)
1013 { 1032 {
1014 struct ev_periodic *w = periodics [i]; 1033 struct ev_periodic *w = periodics [i];
1015 1034
1016 if (w->reschedule_cb) 1035 if (w->reschedule_cb)
1017 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1036 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1018 else if (w->interval) 1037 else if (w->interval)
1019 ((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;
1020 } 1039 }
1021 1040
1022 /* now rebuild the heap */ 1041 /* now rebuild the heap */
1023 for (i = periodiccnt >> 1; i--; ) 1042 for (i = periodiccnt >> 1; i--; )
1024 downheap ((WT *)periodics, periodiccnt, i); 1043 downheap ((WT *)periodics, periodiccnt, i);
1029{ 1048{
1030 mn_now = get_clock (); 1049 mn_now = get_clock ();
1031 1050
1032 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1051 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1033 { 1052 {
1034 rt_now = rtmn_diff + mn_now; 1053 ev_rt_now = rtmn_diff + mn_now;
1035 return 0; 1054 return 0;
1036 } 1055 }
1037 else 1056 else
1038 { 1057 {
1039 now_floor = mn_now; 1058 now_floor = mn_now;
1040 rt_now = ev_time (); 1059 ev_rt_now = ev_time ();
1041 return 1; 1060 return 1;
1042 } 1061 }
1043} 1062}
1044 1063
1045static void 1064static void
1054 { 1073 {
1055 ev_tstamp odiff = rtmn_diff; 1074 ev_tstamp odiff = rtmn_diff;
1056 1075
1057 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 */
1058 { 1077 {
1059 rtmn_diff = rt_now - mn_now; 1078 rtmn_diff = ev_rt_now - mn_now;
1060 1079
1061 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1080 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1062 return; /* all is well */ 1081 return; /* all is well */
1063 1082
1064 rt_now = ev_time (); 1083 ev_rt_now = ev_time ();
1065 mn_now = get_clock (); 1084 mn_now = get_clock ();
1066 now_floor = mn_now; 1085 now_floor = mn_now;
1067 } 1086 }
1068 1087
1069 periodics_reschedule (EV_A); 1088 periodics_reschedule (EV_A);
1072 } 1091 }
1073 } 1092 }
1074 else 1093 else
1075#endif 1094#endif
1076 { 1095 {
1077 rt_now = ev_time (); 1096 ev_rt_now = ev_time ();
1078 1097
1079 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))
1080 { 1099 {
1081 periodics_reschedule (EV_A); 1100 periodics_reschedule (EV_A);
1082 1101
1083 /* 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 */
1084 for (i = 0; i < timercnt; ++i) 1103 for (i = 0; i < timercnt; ++i)
1085 ((WT)timers [i])->at += rt_now - mn_now; 1104 ((WT)timers [i])->at += ev_rt_now - mn_now;
1086 } 1105 }
1087 1106
1088 mn_now = rt_now; 1107 mn_now = ev_rt_now;
1089 } 1108 }
1090} 1109}
1091 1110
1092void 1111void
1093ev_ref (EV_P) 1112ev_ref (EV_P)
1133 if (expect_true (have_monotonic)) 1152 if (expect_true (have_monotonic))
1134 time_update_monotonic (EV_A); 1153 time_update_monotonic (EV_A);
1135 else 1154 else
1136#endif 1155#endif
1137 { 1156 {
1138 rt_now = ev_time (); 1157 ev_rt_now = ev_time ();
1139 mn_now = rt_now; 1158 mn_now = ev_rt_now;
1140 } 1159 }
1141 1160
1142 if (flags & EVLOOP_NONBLOCK || idlecnt) 1161 if (flags & EVLOOP_NONBLOCK || idlecnt)
1143 block = 0.; 1162 block = 0.;
1144 else 1163 else
1151 if (block > to) block = to; 1170 if (block > to) block = to;
1152 } 1171 }
1153 1172
1154 if (periodiccnt) 1173 if (periodiccnt)
1155 { 1174 {
1156 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; 1175 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1157 if (block > to) block = to; 1176 if (block > to) block = to;
1158 } 1177 }
1159 1178
1160 if (block < 0.) block = 0.; 1179 if (block < 0.) block = 0.;
1161 } 1180 }
1162 1181
1163 method_poll (EV_A_ block); 1182 method_poll (EV_A_ block);
1164 1183
1165 /* update rt_now, do magic */ 1184 /* update ev_rt_now, do magic */
1166 time_update (EV_A); 1185 time_update (EV_A);
1167 1186
1168 /* queue pending timers and reschedule them */ 1187 /* queue pending timers and reschedule them */
1169 timers_reify (EV_A); /* relative timers called last */ 1188 timers_reify (EV_A); /* relative timers called last */
1170 periodics_reify (EV_A); /* absolute timers called first */ 1189 periodics_reify (EV_A); /* absolute timers called first */
1316ev_timer_again (EV_P_ struct ev_timer *w) 1335ev_timer_again (EV_P_ struct ev_timer *w)
1317{ 1336{
1318 if (ev_is_active (w)) 1337 if (ev_is_active (w))
1319 { 1338 {
1320 if (w->repeat) 1339 if (w->repeat)
1321 {
1322 ((WT)w)->at = mn_now + w->repeat;
1323 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1340 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat);
1324 }
1325 else 1341 else
1326 ev_timer_stop (EV_A_ w); 1342 ev_timer_stop (EV_A_ w);
1327 } 1343 }
1328 else if (w->repeat) 1344 else if (w->repeat)
1329 ev_timer_start (EV_A_ w); 1345 ev_timer_start (EV_A_ w);
1334{ 1350{
1335 if (ev_is_active (w)) 1351 if (ev_is_active (w))
1336 return; 1352 return;
1337 1353
1338 if (w->reschedule_cb) 1354 if (w->reschedule_cb)
1339 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1355 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1340 else if (w->interval) 1356 else if (w->interval)
1341 { 1357 {
1342 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.));
1343 /* 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 */
1344 ((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;
1345 } 1361 }
1346 1362
1347 ev_start (EV_A_ (W)w, ++periodiccnt); 1363 ev_start (EV_A_ (W)w, ++periodiccnt);
1348 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1364 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1349 periodics [periodiccnt - 1] = w; 1365 periodics [periodiccnt - 1] = w;
1371} 1387}
1372 1388
1373void 1389void
1374ev_periodic_again (EV_P_ struct ev_periodic *w) 1390ev_periodic_again (EV_P_ struct ev_periodic *w)
1375{ 1391{
1392 /* TODO: use adjustheap and recalculation */
1376 ev_periodic_stop (EV_A_ w); 1393 ev_periodic_stop (EV_A_ w);
1377 ev_periodic_start (EV_A_ w); 1394 ev_periodic_start (EV_A_ w);
1378} 1395}
1379 1396
1380void 1397void
1559 else 1576 else
1560 { 1577 {
1561 once->cb = cb; 1578 once->cb = cb;
1562 once->arg = arg; 1579 once->arg = arg;
1563 1580
1564 ev_watcher_init (&once->io, once_cb_io); 1581 ev_init (&once->io, once_cb_io);
1565 if (fd >= 0) 1582 if (fd >= 0)
1566 { 1583 {
1567 ev_io_set (&once->io, fd, events); 1584 ev_io_set (&once->io, fd, events);
1568 ev_io_start (EV_A_ &once->io); 1585 ev_io_start (EV_A_ &once->io);
1569 } 1586 }
1570 1587
1571 ev_watcher_init (&once->to, once_cb_to); 1588 ev_init (&once->to, once_cb_to);
1572 if (timeout >= 0.) 1589 if (timeout >= 0.)
1573 { 1590 {
1574 ev_timer_set (&once->to, timeout, 0.); 1591 ev_timer_set (&once->to, timeout, 0.);
1575 ev_timer_start (EV_A_ &once->to); 1592 ev_timer_start (EV_A_ &once->to);
1576 } 1593 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines