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

Comparing libev/ev.c (file contents):
Revision 1.84 by root, Fri Nov 9 23:04:35 2007 UTC vs.
Revision 1.97 by root, Sun Nov 11 01:53:07 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
40# ifndef EV_USE_MONOTONIC
35# define EV_USE_MONOTONIC 1 41# define EV_USE_MONOTONIC 1
42# endif
43# ifndef EV_USE_REALTIME
36# define EV_USE_REALTIME 1 44# define EV_USE_REALTIME 1
45# endif
37# endif 46# endif
38 47
39# if HAVE_SELECT && HAVE_SYS_SELECT_H 48# if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT)
40# define EV_USE_SELECT 1 49# define EV_USE_SELECT 1
41# endif 50# endif
42 51
43# if HAVE_POLL && HAVE_POLL_H 52# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL)
44# define EV_USE_POLL 1 53# define EV_USE_POLL 1
45# endif 54# endif
46 55
47# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H 56# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL)
48# define EV_USE_EPOLL 1 57# define EV_USE_EPOLL 1
49# endif 58# endif
50 59
51# if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H 60# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE)
52# define EV_USE_KQUEUE 1 61# define EV_USE_KQUEUE 1
53# endif 62# endif
54 63
55#endif 64#endif
56 65
221 230
222#if EV_MULTIPLICITY 231#if EV_MULTIPLICITY
223 232
224 struct ev_loop 233 struct ev_loop
225 { 234 {
235 ev_tstamp ev_rt_now;
226 #define VAR(name,decl) decl; 236 #define VAR(name,decl) decl;
227 #include "ev_vars.h" 237 #include "ev_vars.h"
228 #undef VAR 238 #undef VAR
229 }; 239 };
230 #include "ev_wrap.h" 240 #include "ev_wrap.h"
232 struct ev_loop default_loop_struct; 242 struct ev_loop default_loop_struct;
233 static struct ev_loop *default_loop; 243 static struct ev_loop *default_loop;
234 244
235#else 245#else
236 246
247 ev_tstamp ev_rt_now;
237 #define VAR(name,decl) static decl; 248 #define VAR(name,decl) static decl;
238 #include "ev_vars.h" 249 #include "ev_vars.h"
239 #undef VAR 250 #undef VAR
240 251
241 static int default_loop; 252 static int default_loop;
242 253
243#endif 254#endif
244 255
245/*****************************************************************************/ 256/*****************************************************************************/
246 257
247inline ev_tstamp 258ev_tstamp
248ev_time (void) 259ev_time (void)
249{ 260{
250#if EV_USE_REALTIME 261#if EV_USE_REALTIME
251 struct timespec ts; 262 struct timespec ts;
252 clock_gettime (CLOCK_REALTIME, &ts); 263 clock_gettime (CLOCK_REALTIME, &ts);
271#endif 282#endif
272 283
273 return ev_time (); 284 return ev_time ();
274} 285}
275 286
287#if EV_MULTIPLICITY
276ev_tstamp 288ev_tstamp
277ev_now (EV_P) 289ev_now (EV_P)
278{ 290{
279 return rt_now; 291 return ev_rt_now;
280} 292}
293#endif
281 294
282#define array_roundsize(type,n) ((n) | 4 & ~3) 295#define array_roundsize(type,n) ((n) | 4 & ~3)
283 296
284#define array_needsize(type,base,cur,cnt,init) \ 297#define array_needsize(type,base,cur,cnt,init) \
285 if (expect_false ((cnt) > cur)) \ 298 if (expect_false ((cnt) > cur)) \
735 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 748 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
736 have_monotonic = 1; 749 have_monotonic = 1;
737 } 750 }
738#endif 751#endif
739 752
740 rt_now = ev_time (); 753 ev_rt_now = ev_time ();
741 mn_now = get_clock (); 754 mn_now = get_clock ();
742 now_floor = mn_now; 755 now_floor = mn_now;
743 rtmn_diff = rt_now - mn_now; 756 rtmn_diff = ev_rt_now - mn_now;
744 757
745 if (methods == EVMETHOD_AUTO) 758 if (methods == EVMETHOD_AUTO)
746 if (!enable_secure () && getenv ("LIBEV_METHODS")) 759 if (!enable_secure () && getenv ("LIBEV_METHODS"))
747 methods = atoi (getenv ("LIBEV_METHODS")); 760 methods = atoi (getenv ("LIBEV_METHODS"));
748 else 761 else
795 array_free (pending, [i]); 808 array_free (pending, [i]);
796 809
797 /* have to use the microsoft-never-gets-it-right macro */ 810 /* have to use the microsoft-never-gets-it-right macro */
798 array_free_microshit (fdchange); 811 array_free_microshit (fdchange);
799 array_free_microshit (timer); 812 array_free_microshit (timer);
813#if EV_PERIODICS
800 array_free_microshit (periodic); 814 array_free_microshit (periodic);
815#endif
801 array_free_microshit (idle); 816 array_free_microshit (idle);
802 array_free_microshit (prepare); 817 array_free_microshit (prepare);
803 array_free_microshit (check); 818 array_free_microshit (check);
804 819
805 method = 0; 820 method = 0;
978 993
979 /* first reschedule or stop timer */ 994 /* first reschedule or stop timer */
980 if (w->repeat) 995 if (w->repeat)
981 { 996 {
982 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 997 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
998
983 ((WT)w)->at = mn_now + w->repeat; 999 ((WT)w)->at += w->repeat;
1000 if (((WT)w)->at < mn_now)
1001 ((WT)w)->at = mn_now;
1002
984 downheap ((WT *)timers, timercnt, 0); 1003 downheap ((WT *)timers, timercnt, 0);
985 } 1004 }
986 else 1005 else
987 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1006 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
988 1007
989 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1008 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
990 } 1009 }
991} 1010}
992 1011
1012#if EV_PERIODICS
993static void 1013static void
994periodics_reify (EV_P) 1014periodics_reify (EV_P)
995{ 1015{
996 while (periodiccnt && ((WT)periodics [0])->at <= rt_now) 1016 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
997 { 1017 {
998 struct ev_periodic *w = periodics [0]; 1018 struct ev_periodic *w = periodics [0];
999 1019
1000 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1020 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1001 1021
1002 /* first reschedule or stop timer */ 1022 /* first reschedule or stop timer */
1003 if (w->reschedule_cb) 1023 if (w->reschedule_cb)
1004 { 1024 {
1005 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); 1025 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
1006 1026
1007 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); 1027 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1008 downheap ((WT *)periodics, periodiccnt, 0); 1028 downheap ((WT *)periodics, periodiccnt, 0);
1009 } 1029 }
1010 else if (w->interval) 1030 else if (w->interval)
1011 { 1031 {
1012 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 1032 ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
1013 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); 1033 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
1014 downheap ((WT *)periodics, periodiccnt, 0); 1034 downheap ((WT *)periodics, periodiccnt, 0);
1015 } 1035 }
1016 else 1036 else
1017 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1037 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1018 1038
1029 for (i = 0; i < periodiccnt; ++i) 1049 for (i = 0; i < periodiccnt; ++i)
1030 { 1050 {
1031 struct ev_periodic *w = periodics [i]; 1051 struct ev_periodic *w = periodics [i];
1032 1052
1033 if (w->reschedule_cb) 1053 if (w->reschedule_cb)
1034 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1054 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1035 else if (w->interval) 1055 else if (w->interval)
1036 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1056 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1037 } 1057 }
1038 1058
1039 /* now rebuild the heap */ 1059 /* now rebuild the heap */
1040 for (i = periodiccnt >> 1; i--; ) 1060 for (i = periodiccnt >> 1; i--; )
1041 downheap ((WT *)periodics, periodiccnt, i); 1061 downheap ((WT *)periodics, periodiccnt, i);
1042} 1062}
1063#endif
1043 1064
1044inline int 1065inline int
1045time_update_monotonic (EV_P) 1066time_update_monotonic (EV_P)
1046{ 1067{
1047 mn_now = get_clock (); 1068 mn_now = get_clock ();
1048 1069
1049 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1070 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1050 { 1071 {
1051 rt_now = rtmn_diff + mn_now; 1072 ev_rt_now = rtmn_diff + mn_now;
1052 return 0; 1073 return 0;
1053 } 1074 }
1054 else 1075 else
1055 { 1076 {
1056 now_floor = mn_now; 1077 now_floor = mn_now;
1057 rt_now = ev_time (); 1078 ev_rt_now = ev_time ();
1058 return 1; 1079 return 1;
1059 } 1080 }
1060} 1081}
1061 1082
1062static void 1083static void
1071 { 1092 {
1072 ev_tstamp odiff = rtmn_diff; 1093 ev_tstamp odiff = rtmn_diff;
1073 1094
1074 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1095 for (i = 4; --i; ) /* loop a few times, before making important decisions */
1075 { 1096 {
1076 rtmn_diff = rt_now - mn_now; 1097 rtmn_diff = ev_rt_now - mn_now;
1077 1098
1078 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1099 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1079 return; /* all is well */ 1100 return; /* all is well */
1080 1101
1081 rt_now = ev_time (); 1102 ev_rt_now = ev_time ();
1082 mn_now = get_clock (); 1103 mn_now = get_clock ();
1083 now_floor = mn_now; 1104 now_floor = mn_now;
1084 } 1105 }
1085 1106
1107# if EV_PERIODICS
1086 periodics_reschedule (EV_A); 1108 periodics_reschedule (EV_A);
1109# endif
1087 /* no timer adjustment, as the monotonic clock doesn't jump */ 1110 /* no timer adjustment, as the monotonic clock doesn't jump */
1088 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1111 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1089 } 1112 }
1090 } 1113 }
1091 else 1114 else
1092#endif 1115#endif
1093 { 1116 {
1094 rt_now = ev_time (); 1117 ev_rt_now = ev_time ();
1095 1118
1096 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1119 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1097 { 1120 {
1121#if EV_PERIODICS
1098 periodics_reschedule (EV_A); 1122 periodics_reschedule (EV_A);
1123#endif
1099 1124
1100 /* adjust timers. this is easy, as the offset is the same for all */ 1125 /* adjust timers. this is easy, as the offset is the same for all */
1101 for (i = 0; i < timercnt; ++i) 1126 for (i = 0; i < timercnt; ++i)
1102 ((WT)timers [i])->at += rt_now - mn_now; 1127 ((WT)timers [i])->at += ev_rt_now - mn_now;
1103 } 1128 }
1104 1129
1105 mn_now = rt_now; 1130 mn_now = ev_rt_now;
1106 } 1131 }
1107} 1132}
1108 1133
1109void 1134void
1110ev_ref (EV_P) 1135ev_ref (EV_P)
1150 if (expect_true (have_monotonic)) 1175 if (expect_true (have_monotonic))
1151 time_update_monotonic (EV_A); 1176 time_update_monotonic (EV_A);
1152 else 1177 else
1153#endif 1178#endif
1154 { 1179 {
1155 rt_now = ev_time (); 1180 ev_rt_now = ev_time ();
1156 mn_now = rt_now; 1181 mn_now = ev_rt_now;
1157 } 1182 }
1158 1183
1159 if (flags & EVLOOP_NONBLOCK || idlecnt) 1184 if (flags & EVLOOP_NONBLOCK || idlecnt)
1160 block = 0.; 1185 block = 0.;
1161 else 1186 else
1166 { 1191 {
1167 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1192 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
1168 if (block > to) block = to; 1193 if (block > to) block = to;
1169 } 1194 }
1170 1195
1196#if EV_PERIODICS
1171 if (periodiccnt) 1197 if (periodiccnt)
1172 { 1198 {
1173 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; 1199 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1174 if (block > to) block = to; 1200 if (block > to) block = to;
1175 } 1201 }
1202#endif
1176 1203
1177 if (block < 0.) block = 0.; 1204 if (block < 0.) block = 0.;
1178 } 1205 }
1179 1206
1180 method_poll (EV_A_ block); 1207 method_poll (EV_A_ block);
1181 1208
1182 /* update rt_now, do magic */ 1209 /* update ev_rt_now, do magic */
1183 time_update (EV_A); 1210 time_update (EV_A);
1184 1211
1185 /* queue pending timers and reschedule them */ 1212 /* queue pending timers and reschedule them */
1186 timers_reify (EV_A); /* relative timers called last */ 1213 timers_reify (EV_A); /* relative timers called last */
1214#if EV_PERIODICS
1187 periodics_reify (EV_A); /* absolute timers called first */ 1215 periodics_reify (EV_A); /* absolute timers called first */
1216#endif
1188 1217
1189 /* queue idle watchers unless io or timers are pending */ 1218 /* queue idle watchers unless io or timers are pending */
1190 if (idlecnt && !any_pending (EV_A)) 1219 if (idlecnt && !any_pending (EV_A))
1191 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1220 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1192 1221
1283{ 1312{
1284 ev_clear_pending (EV_A_ (W)w); 1313 ev_clear_pending (EV_A_ (W)w);
1285 if (!ev_is_active (w)) 1314 if (!ev_is_active (w))
1286 return; 1315 return;
1287 1316
1317 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1318
1288 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1319 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1289 ev_stop (EV_A_ (W)w); 1320 ev_stop (EV_A_ (W)w);
1290 1321
1291 fd_change (EV_A_ w->fd); 1322 fd_change (EV_A_ w->fd);
1292} 1323}
1322 { 1353 {
1323 timers [((W)w)->active - 1] = timers [timercnt]; 1354 timers [((W)w)->active - 1] = timers [timercnt];
1324 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1355 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1325 } 1356 }
1326 1357
1327 ((WT)w)->at = w->repeat; 1358 ((WT)w)->at -= mn_now;
1328 1359
1329 ev_stop (EV_A_ (W)w); 1360 ev_stop (EV_A_ (W)w);
1330} 1361}
1331 1362
1332void 1363void
1341 } 1372 }
1342 else if (w->repeat) 1373 else if (w->repeat)
1343 ev_timer_start (EV_A_ w); 1374 ev_timer_start (EV_A_ w);
1344} 1375}
1345 1376
1377#if EV_PERIODICS
1346void 1378void
1347ev_periodic_start (EV_P_ struct ev_periodic *w) 1379ev_periodic_start (EV_P_ struct ev_periodic *w)
1348{ 1380{
1349 if (ev_is_active (w)) 1381 if (ev_is_active (w))
1350 return; 1382 return;
1351 1383
1352 if (w->reschedule_cb) 1384 if (w->reschedule_cb)
1353 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1385 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1354 else if (w->interval) 1386 else if (w->interval)
1355 { 1387 {
1356 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1388 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1357 /* this formula differs from the one in periodic_reify because we do not always round up */ 1389 /* this formula differs from the one in periodic_reify because we do not always round up */
1358 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1390 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1359 } 1391 }
1360 1392
1361 ev_start (EV_A_ (W)w, ++periodiccnt); 1393 ev_start (EV_A_ (W)w, ++periodiccnt);
1362 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1394 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1363 periodics [periodiccnt - 1] = w; 1395 periodics [periodiccnt - 1] = w;
1389{ 1421{
1390 /* TODO: use adjustheap and recalculation */ 1422 /* TODO: use adjustheap and recalculation */
1391 ev_periodic_stop (EV_A_ w); 1423 ev_periodic_stop (EV_A_ w);
1392 ev_periodic_start (EV_A_ w); 1424 ev_periodic_start (EV_A_ w);
1393} 1425}
1426#endif
1394 1427
1395void 1428void
1396ev_idle_start (EV_P_ struct ev_idle *w) 1429ev_idle_start (EV_P_ struct ev_idle *w)
1397{ 1430{
1398 if (ev_is_active (w)) 1431 if (ev_is_active (w))
1449 1482
1450void 1483void
1451ev_check_stop (EV_P_ struct ev_check *w) 1484ev_check_stop (EV_P_ struct ev_check *w)
1452{ 1485{
1453 ev_clear_pending (EV_A_ (W)w); 1486 ev_clear_pending (EV_A_ (W)w);
1454 if (ev_is_active (w)) 1487 if (!ev_is_active (w))
1455 return; 1488 return;
1456 1489
1457 checks [((W)w)->active - 1] = checks [--checkcnt]; 1490 checks [((W)w)->active - 1] = checks [--checkcnt];
1458 ev_stop (EV_A_ (W)w); 1491 ev_stop (EV_A_ (W)w);
1459} 1492}
1520 1553
1521void 1554void
1522ev_child_stop (EV_P_ struct ev_child *w) 1555ev_child_stop (EV_P_ struct ev_child *w)
1523{ 1556{
1524 ev_clear_pending (EV_A_ (W)w); 1557 ev_clear_pending (EV_A_ (W)w);
1525 if (ev_is_active (w)) 1558 if (!ev_is_active (w))
1526 return; 1559 return;
1527 1560
1528 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1561 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1529 ev_stop (EV_A_ (W)w); 1562 ev_stop (EV_A_ (W)w);
1530} 1563}
1590 ev_timer_start (EV_A_ &once->to); 1623 ev_timer_start (EV_A_ &once->to);
1591 } 1624 }
1592 } 1625 }
1593} 1626}
1594 1627
1628#ifdef __cplusplus
1629}
1630#endif
1631

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines