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.93 by root, Sun Nov 11 01:07:35 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
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;
242 249
243#endif 250#endif
244 251
245/*****************************************************************************/ 252/*****************************************************************************/
246 253
247inline ev_tstamp 254ev_tstamp
248ev_time (void) 255ev_time (void)
249{ 256{
250#if EV_USE_REALTIME 257#if EV_USE_REALTIME
251 struct timespec ts; 258 struct timespec ts;
252 clock_gettime (CLOCK_REALTIME, &ts); 259 clock_gettime (CLOCK_REALTIME, &ts);
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)) \
735 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 744 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
736 have_monotonic = 1; 745 have_monotonic = 1;
737 } 746 }
738#endif 747#endif
739 748
740 rt_now = ev_time (); 749 ev_rt_now = ev_time ();
741 mn_now = get_clock (); 750 mn_now = get_clock ();
742 now_floor = mn_now; 751 now_floor = mn_now;
743 rtmn_diff = rt_now - mn_now; 752 rtmn_diff = ev_rt_now - mn_now;
744 753
745 if (methods == EVMETHOD_AUTO) 754 if (methods == EVMETHOD_AUTO)
746 if (!enable_secure () && getenv ("LIBEV_METHODS")) 755 if (!enable_secure () && getenv ("LIBEV_METHODS"))
747 methods = atoi (getenv ("LIBEV_METHODS")); 756 methods = atoi (getenv ("LIBEV_METHODS"));
748 else 757 else
795 array_free (pending, [i]); 804 array_free (pending, [i]);
796 805
797 /* have to use the microsoft-never-gets-it-right macro */ 806 /* have to use the microsoft-never-gets-it-right macro */
798 array_free_microshit (fdchange); 807 array_free_microshit (fdchange);
799 array_free_microshit (timer); 808 array_free_microshit (timer);
809#if EV_PERIODICS
800 array_free_microshit (periodic); 810 array_free_microshit (periodic);
811#endif
801 array_free_microshit (idle); 812 array_free_microshit (idle);
802 array_free_microshit (prepare); 813 array_free_microshit (prepare);
803 array_free_microshit (check); 814 array_free_microshit (check);
804 815
805 method = 0; 816 method = 0;
978 989
979 /* first reschedule or stop timer */ 990 /* first reschedule or stop timer */
980 if (w->repeat) 991 if (w->repeat)
981 { 992 {
982 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 993 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
994
983 ((WT)w)->at = mn_now + w->repeat; 995 ((WT)w)->at += w->repeat;
996 if (((WT)w)->at < mn_now)
997 ((WT)w)->at = mn_now;
998
984 downheap ((WT *)timers, timercnt, 0); 999 downheap ((WT *)timers, timercnt, 0);
985 } 1000 }
986 else 1001 else
987 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1002 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
988 1003
989 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1004 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
990 } 1005 }
991} 1006}
992 1007
1008#if EV_PERIODICS
993static void 1009static void
994periodics_reify (EV_P) 1010periodics_reify (EV_P)
995{ 1011{
996 while (periodiccnt && ((WT)periodics [0])->at <= rt_now) 1012 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
997 { 1013 {
998 struct ev_periodic *w = periodics [0]; 1014 struct ev_periodic *w = periodics [0];
999 1015
1000 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1016 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1001 1017
1002 /* first reschedule or stop timer */ 1018 /* first reschedule or stop timer */
1003 if (w->reschedule_cb) 1019 if (w->reschedule_cb)
1004 { 1020 {
1005 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); 1021 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
1006 1022
1007 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); 1023 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1008 downheap ((WT *)periodics, periodiccnt, 0); 1024 downheap ((WT *)periodics, periodiccnt, 0);
1009 } 1025 }
1010 else if (w->interval) 1026 else if (w->interval)
1011 { 1027 {
1012 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 1028 ((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)); 1029 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); 1030 downheap ((WT *)periodics, periodiccnt, 0);
1015 } 1031 }
1016 else 1032 else
1017 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1033 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1018 1034
1029 for (i = 0; i < periodiccnt; ++i) 1045 for (i = 0; i < periodiccnt; ++i)
1030 { 1046 {
1031 struct ev_periodic *w = periodics [i]; 1047 struct ev_periodic *w = periodics [i];
1032 1048
1033 if (w->reschedule_cb) 1049 if (w->reschedule_cb)
1034 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1050 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1035 else if (w->interval) 1051 else if (w->interval)
1036 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1052 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1037 } 1053 }
1038 1054
1039 /* now rebuild the heap */ 1055 /* now rebuild the heap */
1040 for (i = periodiccnt >> 1; i--; ) 1056 for (i = periodiccnt >> 1; i--; )
1041 downheap ((WT *)periodics, periodiccnt, i); 1057 downheap ((WT *)periodics, periodiccnt, i);
1042} 1058}
1059#endif
1043 1060
1044inline int 1061inline int
1045time_update_monotonic (EV_P) 1062time_update_monotonic (EV_P)
1046{ 1063{
1047 mn_now = get_clock (); 1064 mn_now = get_clock ();
1048 1065
1049 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1066 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1050 { 1067 {
1051 rt_now = rtmn_diff + mn_now; 1068 ev_rt_now = rtmn_diff + mn_now;
1052 return 0; 1069 return 0;
1053 } 1070 }
1054 else 1071 else
1055 { 1072 {
1056 now_floor = mn_now; 1073 now_floor = mn_now;
1057 rt_now = ev_time (); 1074 ev_rt_now = ev_time ();
1058 return 1; 1075 return 1;
1059 } 1076 }
1060} 1077}
1061 1078
1062static void 1079static void
1071 { 1088 {
1072 ev_tstamp odiff = rtmn_diff; 1089 ev_tstamp odiff = rtmn_diff;
1073 1090
1074 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1091 for (i = 4; --i; ) /* loop a few times, before making important decisions */
1075 { 1092 {
1076 rtmn_diff = rt_now - mn_now; 1093 rtmn_diff = ev_rt_now - mn_now;
1077 1094
1078 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1095 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1079 return; /* all is well */ 1096 return; /* all is well */
1080 1097
1081 rt_now = ev_time (); 1098 ev_rt_now = ev_time ();
1082 mn_now = get_clock (); 1099 mn_now = get_clock ();
1083 now_floor = mn_now; 1100 now_floor = mn_now;
1084 } 1101 }
1085 1102
1103# if EV_PERIODICS
1086 periodics_reschedule (EV_A); 1104 periodics_reschedule (EV_A);
1105# endif
1087 /* no timer adjustment, as the monotonic clock doesn't jump */ 1106 /* no timer adjustment, as the monotonic clock doesn't jump */
1088 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1107 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1089 } 1108 }
1090 } 1109 }
1091 else 1110 else
1092#endif 1111#endif
1093 { 1112 {
1094 rt_now = ev_time (); 1113 ev_rt_now = ev_time ();
1095 1114
1096 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1115 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1097 { 1116 {
1117#if EV_PERIODICS
1098 periodics_reschedule (EV_A); 1118 periodics_reschedule (EV_A);
1119#endif
1099 1120
1100 /* adjust timers. this is easy, as the offset is the same for all */ 1121 /* adjust timers. this is easy, as the offset is the same for all */
1101 for (i = 0; i < timercnt; ++i) 1122 for (i = 0; i < timercnt; ++i)
1102 ((WT)timers [i])->at += rt_now - mn_now; 1123 ((WT)timers [i])->at += ev_rt_now - mn_now;
1103 } 1124 }
1104 1125
1105 mn_now = rt_now; 1126 mn_now = ev_rt_now;
1106 } 1127 }
1107} 1128}
1108 1129
1109void 1130void
1110ev_ref (EV_P) 1131ev_ref (EV_P)
1150 if (expect_true (have_monotonic)) 1171 if (expect_true (have_monotonic))
1151 time_update_monotonic (EV_A); 1172 time_update_monotonic (EV_A);
1152 else 1173 else
1153#endif 1174#endif
1154 { 1175 {
1155 rt_now = ev_time (); 1176 ev_rt_now = ev_time ();
1156 mn_now = rt_now; 1177 mn_now = ev_rt_now;
1157 } 1178 }
1158 1179
1159 if (flags & EVLOOP_NONBLOCK || idlecnt) 1180 if (flags & EVLOOP_NONBLOCK || idlecnt)
1160 block = 0.; 1181 block = 0.;
1161 else 1182 else
1166 { 1187 {
1167 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1188 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
1168 if (block > to) block = to; 1189 if (block > to) block = to;
1169 } 1190 }
1170 1191
1192#if EV_PERIODICS
1171 if (periodiccnt) 1193 if (periodiccnt)
1172 { 1194 {
1173 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; 1195 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1174 if (block > to) block = to; 1196 if (block > to) block = to;
1175 } 1197 }
1198#endif
1176 1199
1177 if (block < 0.) block = 0.; 1200 if (block < 0.) block = 0.;
1178 } 1201 }
1179 1202
1180 method_poll (EV_A_ block); 1203 method_poll (EV_A_ block);
1181 1204
1182 /* update rt_now, do magic */ 1205 /* update ev_rt_now, do magic */
1183 time_update (EV_A); 1206 time_update (EV_A);
1184 1207
1185 /* queue pending timers and reschedule them */ 1208 /* queue pending timers and reschedule them */
1186 timers_reify (EV_A); /* relative timers called last */ 1209 timers_reify (EV_A); /* relative timers called last */
1210#if EV_PERIODICS
1187 periodics_reify (EV_A); /* absolute timers called first */ 1211 periodics_reify (EV_A); /* absolute timers called first */
1212#endif
1188 1213
1189 /* queue idle watchers unless io or timers are pending */ 1214 /* queue idle watchers unless io or timers are pending */
1190 if (idlecnt && !any_pending (EV_A)) 1215 if (idlecnt && !any_pending (EV_A))
1191 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1216 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1192 1217
1283{ 1308{
1284 ev_clear_pending (EV_A_ (W)w); 1309 ev_clear_pending (EV_A_ (W)w);
1285 if (!ev_is_active (w)) 1310 if (!ev_is_active (w))
1286 return; 1311 return;
1287 1312
1313 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1314
1288 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1315 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1289 ev_stop (EV_A_ (W)w); 1316 ev_stop (EV_A_ (W)w);
1290 1317
1291 fd_change (EV_A_ w->fd); 1318 fd_change (EV_A_ w->fd);
1292} 1319}
1322 { 1349 {
1323 timers [((W)w)->active - 1] = timers [timercnt]; 1350 timers [((W)w)->active - 1] = timers [timercnt];
1324 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1351 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1325 } 1352 }
1326 1353
1327 ((WT)w)->at = w->repeat; 1354 ((WT)w)->at -= mn_now;
1328 1355
1329 ev_stop (EV_A_ (W)w); 1356 ev_stop (EV_A_ (W)w);
1330} 1357}
1331 1358
1332void 1359void
1341 } 1368 }
1342 else if (w->repeat) 1369 else if (w->repeat)
1343 ev_timer_start (EV_A_ w); 1370 ev_timer_start (EV_A_ w);
1344} 1371}
1345 1372
1373#if EV_PERIODICS
1346void 1374void
1347ev_periodic_start (EV_P_ struct ev_periodic *w) 1375ev_periodic_start (EV_P_ struct ev_periodic *w)
1348{ 1376{
1349 if (ev_is_active (w)) 1377 if (ev_is_active (w))
1350 return; 1378 return;
1351 1379
1352 if (w->reschedule_cb) 1380 if (w->reschedule_cb)
1353 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1381 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1354 else if (w->interval) 1382 else if (w->interval)
1355 { 1383 {
1356 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1384 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 */ 1385 /* 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; 1386 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1359 } 1387 }
1360 1388
1361 ev_start (EV_A_ (W)w, ++periodiccnt); 1389 ev_start (EV_A_ (W)w, ++periodiccnt);
1362 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1390 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1363 periodics [periodiccnt - 1] = w; 1391 periodics [periodiccnt - 1] = w;
1389{ 1417{
1390 /* TODO: use adjustheap and recalculation */ 1418 /* TODO: use adjustheap and recalculation */
1391 ev_periodic_stop (EV_A_ w); 1419 ev_periodic_stop (EV_A_ w);
1392 ev_periodic_start (EV_A_ w); 1420 ev_periodic_start (EV_A_ w);
1393} 1421}
1422#endif
1394 1423
1395void 1424void
1396ev_idle_start (EV_P_ struct ev_idle *w) 1425ev_idle_start (EV_P_ struct ev_idle *w)
1397{ 1426{
1398 if (ev_is_active (w)) 1427 if (ev_is_active (w))
1590 ev_timer_start (EV_A_ &once->to); 1619 ev_timer_start (EV_A_ &once->to);
1591 } 1620 }
1592 } 1621 }
1593} 1622}
1594 1623
1624#ifdef __cplusplus
1625}
1626#endif
1627

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines