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

Comparing libev/ev.c (file contents):
Revision 1.80 by root, Fri Nov 9 15:30:59 2007 UTC vs.
Revision 1.90 by root, Sun Nov 11 00:05:59 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
46 51
47# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H 52# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
48# define EV_USE_EPOLL 1 53# define EV_USE_EPOLL 1
49# endif 54# endif
50 55
51# if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H 56# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
52# define EV_USE_KQUEUE 1 57# define EV_USE_KQUEUE 1
53# endif 58# endif
54 59
55#endif 60#endif
56 61
126#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 131#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) */ 132#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 */ 133#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 */ 134/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
130 135
136#ifdef EV_H
137# include EV_H
138#else
131#include "ev.h" 139# include "ev.h"
140#endif
132 141
133#if __GNUC__ >= 3 142#if __GNUC__ >= 3
134# define expect(expr,value) __builtin_expect ((expr),(value)) 143# define expect(expr,value) __builtin_expect ((expr),(value))
135# define inline inline 144# define inline inline
136#else 145#else
217 226
218#if EV_MULTIPLICITY 227#if EV_MULTIPLICITY
219 228
220 struct ev_loop 229 struct ev_loop
221 { 230 {
231 ev_tstamp ev_rt_now;
222 #define VAR(name,decl) decl; 232 #define VAR(name,decl) decl;
223 #include "ev_vars.h" 233 #include "ev_vars.h"
224 #undef VAR 234 #undef VAR
225 }; 235 };
226 #include "ev_wrap.h" 236 #include "ev_wrap.h"
228 struct ev_loop default_loop_struct; 238 struct ev_loop default_loop_struct;
229 static struct ev_loop *default_loop; 239 static struct ev_loop *default_loop;
230 240
231#else 241#else
232 242
243 ev_tstamp ev_rt_now;
233 #define VAR(name,decl) static decl; 244 #define VAR(name,decl) static decl;
234 #include "ev_vars.h" 245 #include "ev_vars.h"
235 #undef VAR 246 #undef VAR
236 247
237 static int default_loop; 248 static int default_loop;
267#endif 278#endif
268 279
269 return ev_time (); 280 return ev_time ();
270} 281}
271 282
283#if EV_MULTIPLICITY
272ev_tstamp 284ev_tstamp
273ev_now (EV_P) 285ev_now (EV_P)
274{ 286{
275 return rt_now; 287 return ev_rt_now;
276} 288}
289#endif
277 290
278#define array_roundsize(type,n) ((n) | 4 & ~3) 291#define array_roundsize(type,n) ((n) | 4 & ~3)
279 292
280#define array_needsize(type,base,cur,cnt,init) \ 293#define array_needsize(type,base,cur,cnt,init) \
281 if (expect_false ((cnt) > cur)) \ 294 if (expect_false ((cnt) > cur)) \
514 527
515 heap [k] = w; 528 heap [k] = w;
516 ((W)heap [k])->active = k + 1; 529 ((W)heap [k])->active = k + 1;
517} 530}
518 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
519/*****************************************************************************/ 544/*****************************************************************************/
520 545
521typedef struct 546typedef struct
522{ 547{
523 WL head; 548 WL head;
719 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 744 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
720 have_monotonic = 1; 745 have_monotonic = 1;
721 } 746 }
722#endif 747#endif
723 748
724 rt_now = ev_time (); 749 ev_rt_now = ev_time ();
725 mn_now = get_clock (); 750 mn_now = get_clock ();
726 now_floor = mn_now; 751 now_floor = mn_now;
727 rtmn_diff = rt_now - mn_now; 752 rtmn_diff = ev_rt_now - mn_now;
728 753
729 if (methods == EVMETHOD_AUTO) 754 if (methods == EVMETHOD_AUTO)
730 if (!enable_secure () && getenv ("LIBEV_METHODS")) 755 if (!enable_secure () && getenv ("LIBEV_METHODS"))
731 methods = atoi (getenv ("LIBEV_METHODS")); 756 methods = atoi (getenv ("LIBEV_METHODS"));
732 else 757 else
747#endif 772#endif
748#if EV_USE_SELECT 773#if EV_USE_SELECT
749 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 774 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
750#endif 775#endif
751 776
752 ev_watcher_init (&sigev, sigcb); 777 ev_init (&sigev, sigcb);
753 ev_set_priority (&sigev, EV_MAXPRI); 778 ev_set_priority (&sigev, EV_MAXPRI);
754 } 779 }
755} 780}
756 781
757void 782void
944 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 969 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
945 970
946 if (p->w) 971 if (p->w)
947 { 972 {
948 p->w->pending = 0; 973 p->w->pending = 0;
949 p->w->cb (EV_A_ p->w, p->events); 974 EV_CB_INVOKE (p->w, p->events);
950 } 975 }
951 } 976 }
952} 977}
953 978
954static void 979static void
962 987
963 /* first reschedule or stop timer */ 988 /* first reschedule or stop timer */
964 if (w->repeat) 989 if (w->repeat)
965 { 990 {
966 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 991 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
992
967 ((WT)w)->at = mn_now + w->repeat; 993 ((WT)w)->at += w->repeat;
994 if (((WT)w)->at < mn_now)
995 ((WT)w)->at = mn_now;
996
968 downheap ((WT *)timers, timercnt, 0); 997 downheap ((WT *)timers, timercnt, 0);
969 } 998 }
970 else 999 else
971 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1000 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
972 1001
975} 1004}
976 1005
977static void 1006static void
978periodics_reify (EV_P) 1007periodics_reify (EV_P)
979{ 1008{
980 while (periodiccnt && ((WT)periodics [0])->at <= rt_now) 1009 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
981 { 1010 {
982 struct ev_periodic *w = periodics [0]; 1011 struct ev_periodic *w = periodics [0];
983 1012
984 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1013 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
985 1014
986 /* first reschedule or stop timer */ 1015 /* first reschedule or stop timer */
987 if (w->reschedule_cb) 1016 if (w->reschedule_cb)
988 { 1017 {
989 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); 1018 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
990 1019
991 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); 1020 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
992 downheap ((WT *)periodics, periodiccnt, 0); 1021 downheap ((WT *)periodics, periodiccnt, 0);
993 } 1022 }
994 else if (w->interval) 1023 else if (w->interval)
995 { 1024 {
996 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 1025 ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
997 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); 1026 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
998 downheap ((WT *)periodics, periodiccnt, 0); 1027 downheap ((WT *)periodics, periodiccnt, 0);
999 } 1028 }
1000 else 1029 else
1001 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1030 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1002 1031
1013 for (i = 0; i < periodiccnt; ++i) 1042 for (i = 0; i < periodiccnt; ++i)
1014 { 1043 {
1015 struct ev_periodic *w = periodics [i]; 1044 struct ev_periodic *w = periodics [i];
1016 1045
1017 if (w->reschedule_cb) 1046 if (w->reschedule_cb)
1018 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1047 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1019 else if (w->interval) 1048 else if (w->interval)
1020 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1049 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1021 } 1050 }
1022 1051
1023 /* now rebuild the heap */ 1052 /* now rebuild the heap */
1024 for (i = periodiccnt >> 1; i--; ) 1053 for (i = periodiccnt >> 1; i--; )
1025 downheap ((WT *)periodics, periodiccnt, i); 1054 downheap ((WT *)periodics, periodiccnt, i);
1030{ 1059{
1031 mn_now = get_clock (); 1060 mn_now = get_clock ();
1032 1061
1033 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1062 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1034 { 1063 {
1035 rt_now = rtmn_diff + mn_now; 1064 ev_rt_now = rtmn_diff + mn_now;
1036 return 0; 1065 return 0;
1037 } 1066 }
1038 else 1067 else
1039 { 1068 {
1040 now_floor = mn_now; 1069 now_floor = mn_now;
1041 rt_now = ev_time (); 1070 ev_rt_now = ev_time ();
1042 return 1; 1071 return 1;
1043 } 1072 }
1044} 1073}
1045 1074
1046static void 1075static void
1055 { 1084 {
1056 ev_tstamp odiff = rtmn_diff; 1085 ev_tstamp odiff = rtmn_diff;
1057 1086
1058 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1087 for (i = 4; --i; ) /* loop a few times, before making important decisions */
1059 { 1088 {
1060 rtmn_diff = rt_now - mn_now; 1089 rtmn_diff = ev_rt_now - mn_now;
1061 1090
1062 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1091 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1063 return; /* all is well */ 1092 return; /* all is well */
1064 1093
1065 rt_now = ev_time (); 1094 ev_rt_now = ev_time ();
1066 mn_now = get_clock (); 1095 mn_now = get_clock ();
1067 now_floor = mn_now; 1096 now_floor = mn_now;
1068 } 1097 }
1069 1098
1070 periodics_reschedule (EV_A); 1099 periodics_reschedule (EV_A);
1073 } 1102 }
1074 } 1103 }
1075 else 1104 else
1076#endif 1105#endif
1077 { 1106 {
1078 rt_now = ev_time (); 1107 ev_rt_now = ev_time ();
1079 1108
1080 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1109 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1081 { 1110 {
1082 periodics_reschedule (EV_A); 1111 periodics_reschedule (EV_A);
1083 1112
1084 /* adjust timers. this is easy, as the offset is the same for all */ 1113 /* adjust timers. this is easy, as the offset is the same for all */
1085 for (i = 0; i < timercnt; ++i) 1114 for (i = 0; i < timercnt; ++i)
1086 ((WT)timers [i])->at += rt_now - mn_now; 1115 ((WT)timers [i])->at += ev_rt_now - mn_now;
1087 } 1116 }
1088 1117
1089 mn_now = rt_now; 1118 mn_now = ev_rt_now;
1090 } 1119 }
1091} 1120}
1092 1121
1093void 1122void
1094ev_ref (EV_P) 1123ev_ref (EV_P)
1134 if (expect_true (have_monotonic)) 1163 if (expect_true (have_monotonic))
1135 time_update_monotonic (EV_A); 1164 time_update_monotonic (EV_A);
1136 else 1165 else
1137#endif 1166#endif
1138 { 1167 {
1139 rt_now = ev_time (); 1168 ev_rt_now = ev_time ();
1140 mn_now = rt_now; 1169 mn_now = ev_rt_now;
1141 } 1170 }
1142 1171
1143 if (flags & EVLOOP_NONBLOCK || idlecnt) 1172 if (flags & EVLOOP_NONBLOCK || idlecnt)
1144 block = 0.; 1173 block = 0.;
1145 else 1174 else
1152 if (block > to) block = to; 1181 if (block > to) block = to;
1153 } 1182 }
1154 1183
1155 if (periodiccnt) 1184 if (periodiccnt)
1156 { 1185 {
1157 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; 1186 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1158 if (block > to) block = to; 1187 if (block > to) block = to;
1159 } 1188 }
1160 1189
1161 if (block < 0.) block = 0.; 1190 if (block < 0.) block = 0.;
1162 } 1191 }
1163 1192
1164 method_poll (EV_A_ block); 1193 method_poll (EV_A_ block);
1165 1194
1166 /* update rt_now, do magic */ 1195 /* update ev_rt_now, do magic */
1167 time_update (EV_A); 1196 time_update (EV_A);
1168 1197
1169 /* queue pending timers and reschedule them */ 1198 /* queue pending timers and reschedule them */
1170 timers_reify (EV_A); /* relative timers called last */ 1199 timers_reify (EV_A); /* relative timers called last */
1171 periodics_reify (EV_A); /* absolute timers called first */ 1200 periodics_reify (EV_A); /* absolute timers called first */
1267{ 1296{
1268 ev_clear_pending (EV_A_ (W)w); 1297 ev_clear_pending (EV_A_ (W)w);
1269 if (!ev_is_active (w)) 1298 if (!ev_is_active (w))
1270 return; 1299 return;
1271 1300
1301 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1302
1272 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1303 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1273 ev_stop (EV_A_ (W)w); 1304 ev_stop (EV_A_ (W)w);
1274 1305
1275 fd_change (EV_A_ w->fd); 1306 fd_change (EV_A_ w->fd);
1276} 1307}
1317ev_timer_again (EV_P_ struct ev_timer *w) 1348ev_timer_again (EV_P_ struct ev_timer *w)
1318{ 1349{
1319 if (ev_is_active (w)) 1350 if (ev_is_active (w))
1320 { 1351 {
1321 if (w->repeat) 1352 if (w->repeat)
1322 {
1323 ((WT)w)->at = mn_now + w->repeat;
1324 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1353 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat);
1325 }
1326 else 1354 else
1327 ev_timer_stop (EV_A_ w); 1355 ev_timer_stop (EV_A_ w);
1328 } 1356 }
1329 else if (w->repeat) 1357 else if (w->repeat)
1330 ev_timer_start (EV_A_ w); 1358 ev_timer_start (EV_A_ w);
1335{ 1363{
1336 if (ev_is_active (w)) 1364 if (ev_is_active (w))
1337 return; 1365 return;
1338 1366
1339 if (w->reschedule_cb) 1367 if (w->reschedule_cb)
1340 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1368 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1341 else if (w->interval) 1369 else if (w->interval)
1342 { 1370 {
1343 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1371 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1344 /* this formula differs from the one in periodic_reify because we do not always round up */ 1372 /* this formula differs from the one in periodic_reify because we do not always round up */
1345 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1373 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1346 } 1374 }
1347 1375
1348 ev_start (EV_A_ (W)w, ++periodiccnt); 1376 ev_start (EV_A_ (W)w, ++periodiccnt);
1349 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1377 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1350 periodics [periodiccnt - 1] = w; 1378 periodics [periodiccnt - 1] = w;
1372} 1400}
1373 1401
1374void 1402void
1375ev_periodic_again (EV_P_ struct ev_periodic *w) 1403ev_periodic_again (EV_P_ struct ev_periodic *w)
1376{ 1404{
1405 /* TODO: use adjustheap and recalculation */
1377 ev_periodic_stop (EV_A_ w); 1406 ev_periodic_stop (EV_A_ w);
1378 ev_periodic_start (EV_A_ w); 1407 ev_periodic_start (EV_A_ w);
1379} 1408}
1380 1409
1381void 1410void
1560 else 1589 else
1561 { 1590 {
1562 once->cb = cb; 1591 once->cb = cb;
1563 once->arg = arg; 1592 once->arg = arg;
1564 1593
1565 ev_watcher_init (&once->io, once_cb_io); 1594 ev_init (&once->io, once_cb_io);
1566 if (fd >= 0) 1595 if (fd >= 0)
1567 { 1596 {
1568 ev_io_set (&once->io, fd, events); 1597 ev_io_set (&once->io, fd, events);
1569 ev_io_start (EV_A_ &once->io); 1598 ev_io_start (EV_A_ &once->io);
1570 } 1599 }
1571 1600
1572 ev_watcher_init (&once->to, once_cb_to); 1601 ev_init (&once->to, once_cb_to);
1573 if (timeout >= 0.) 1602 if (timeout >= 0.)
1574 { 1603 {
1575 ev_timer_set (&once->to, timeout, 0.); 1604 ev_timer_set (&once->to, timeout, 0.);
1576 ev_timer_start (EV_A_ &once->to); 1605 ev_timer_start (EV_A_ &once->to);
1577 } 1606 }
1578 } 1607 }
1579} 1608}
1580 1609
1610#ifdef __cplusplus
1611}
1612#endif
1613

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines