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.88 by root, Sat Nov 10 04:40:27 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;
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
991} 1000}
992 1001
993static void 1002static void
994periodics_reify (EV_P) 1003periodics_reify (EV_P)
995{ 1004{
996 while (periodiccnt && ((WT)periodics [0])->at <= rt_now) 1005 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
997 { 1006 {
998 struct ev_periodic *w = periodics [0]; 1007 struct ev_periodic *w = periodics [0];
999 1008
1000 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1009 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1001 1010
1002 /* first reschedule or stop timer */ 1011 /* first reschedule or stop timer */
1003 if (w->reschedule_cb) 1012 if (w->reschedule_cb)
1004 { 1013 {
1005 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); 1014 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
1006 1015
1007 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); 1016 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1008 downheap ((WT *)periodics, periodiccnt, 0); 1017 downheap ((WT *)periodics, periodiccnt, 0);
1009 } 1018 }
1010 else if (w->interval) 1019 else if (w->interval)
1011 { 1020 {
1012 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 1021 ((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)); 1022 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); 1023 downheap ((WT *)periodics, periodiccnt, 0);
1015 } 1024 }
1016 else 1025 else
1017 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1026 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1018 1027
1029 for (i = 0; i < periodiccnt; ++i) 1038 for (i = 0; i < periodiccnt; ++i)
1030 { 1039 {
1031 struct ev_periodic *w = periodics [i]; 1040 struct ev_periodic *w = periodics [i];
1032 1041
1033 if (w->reschedule_cb) 1042 if (w->reschedule_cb)
1034 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1043 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1035 else if (w->interval) 1044 else if (w->interval)
1036 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1045 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1037 } 1046 }
1038 1047
1039 /* now rebuild the heap */ 1048 /* now rebuild the heap */
1040 for (i = periodiccnt >> 1; i--; ) 1049 for (i = periodiccnt >> 1; i--; )
1041 downheap ((WT *)periodics, periodiccnt, i); 1050 downheap ((WT *)periodics, periodiccnt, i);
1046{ 1055{
1047 mn_now = get_clock (); 1056 mn_now = get_clock ();
1048 1057
1049 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1058 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1050 { 1059 {
1051 rt_now = rtmn_diff + mn_now; 1060 ev_rt_now = rtmn_diff + mn_now;
1052 return 0; 1061 return 0;
1053 } 1062 }
1054 else 1063 else
1055 { 1064 {
1056 now_floor = mn_now; 1065 now_floor = mn_now;
1057 rt_now = ev_time (); 1066 ev_rt_now = ev_time ();
1058 return 1; 1067 return 1;
1059 } 1068 }
1060} 1069}
1061 1070
1062static void 1071static void
1071 { 1080 {
1072 ev_tstamp odiff = rtmn_diff; 1081 ev_tstamp odiff = rtmn_diff;
1073 1082
1074 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1083 for (i = 4; --i; ) /* loop a few times, before making important decisions */
1075 { 1084 {
1076 rtmn_diff = rt_now - mn_now; 1085 rtmn_diff = ev_rt_now - mn_now;
1077 1086
1078 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1087 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1079 return; /* all is well */ 1088 return; /* all is well */
1080 1089
1081 rt_now = ev_time (); 1090 ev_rt_now = ev_time ();
1082 mn_now = get_clock (); 1091 mn_now = get_clock ();
1083 now_floor = mn_now; 1092 now_floor = mn_now;
1084 } 1093 }
1085 1094
1086 periodics_reschedule (EV_A); 1095 periodics_reschedule (EV_A);
1089 } 1098 }
1090 } 1099 }
1091 else 1100 else
1092#endif 1101#endif
1093 { 1102 {
1094 rt_now = ev_time (); 1103 ev_rt_now = ev_time ();
1095 1104
1096 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1105 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1097 { 1106 {
1098 periodics_reschedule (EV_A); 1107 periodics_reschedule (EV_A);
1099 1108
1100 /* adjust timers. this is easy, as the offset is the same for all */ 1109 /* adjust timers. this is easy, as the offset is the same for all */
1101 for (i = 0; i < timercnt; ++i) 1110 for (i = 0; i < timercnt; ++i)
1102 ((WT)timers [i])->at += rt_now - mn_now; 1111 ((WT)timers [i])->at += ev_rt_now - mn_now;
1103 } 1112 }
1104 1113
1105 mn_now = rt_now; 1114 mn_now = ev_rt_now;
1106 } 1115 }
1107} 1116}
1108 1117
1109void 1118void
1110ev_ref (EV_P) 1119ev_ref (EV_P)
1150 if (expect_true (have_monotonic)) 1159 if (expect_true (have_monotonic))
1151 time_update_monotonic (EV_A); 1160 time_update_monotonic (EV_A);
1152 else 1161 else
1153#endif 1162#endif
1154 { 1163 {
1155 rt_now = ev_time (); 1164 ev_rt_now = ev_time ();
1156 mn_now = rt_now; 1165 mn_now = ev_rt_now;
1157 } 1166 }
1158 1167
1159 if (flags & EVLOOP_NONBLOCK || idlecnt) 1168 if (flags & EVLOOP_NONBLOCK || idlecnt)
1160 block = 0.; 1169 block = 0.;
1161 else 1170 else
1168 if (block > to) block = to; 1177 if (block > to) block = to;
1169 } 1178 }
1170 1179
1171 if (periodiccnt) 1180 if (periodiccnt)
1172 { 1181 {
1173 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; 1182 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1174 if (block > to) block = to; 1183 if (block > to) block = to;
1175 } 1184 }
1176 1185
1177 if (block < 0.) block = 0.; 1186 if (block < 0.) block = 0.;
1178 } 1187 }
1179 1188
1180 method_poll (EV_A_ block); 1189 method_poll (EV_A_ block);
1181 1190
1182 /* update rt_now, do magic */ 1191 /* update ev_rt_now, do magic */
1183 time_update (EV_A); 1192 time_update (EV_A);
1184 1193
1185 /* queue pending timers and reschedule them */ 1194 /* queue pending timers and reschedule them */
1186 timers_reify (EV_A); /* relative timers called last */ 1195 timers_reify (EV_A); /* relative timers called last */
1187 periodics_reify (EV_A); /* absolute timers called first */ 1196 periodics_reify (EV_A); /* absolute timers called first */
1348{ 1357{
1349 if (ev_is_active (w)) 1358 if (ev_is_active (w))
1350 return; 1359 return;
1351 1360
1352 if (w->reschedule_cb) 1361 if (w->reschedule_cb)
1353 ((WT)w)->at = w->reschedule_cb (w, rt_now); 1362 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1354 else if (w->interval) 1363 else if (w->interval)
1355 { 1364 {
1356 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1365 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 */ 1366 /* 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; 1367 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1359 } 1368 }
1360 1369
1361 ev_start (EV_A_ (W)w, ++periodiccnt); 1370 ev_start (EV_A_ (W)w, ++periodiccnt);
1362 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); 1371 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1363 periodics [periodiccnt - 1] = w; 1372 periodics [periodiccnt - 1] = w;
1590 ev_timer_start (EV_A_ &once->to); 1599 ev_timer_start (EV_A_ &once->to);
1591 } 1600 }
1592 } 1601 }
1593} 1602}
1594 1603
1604#ifdef __cplusplus
1605}
1606#endif
1607

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines