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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines