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

Comparing libev/ev.c (file contents):
Revision 1.171 by root, Sun Dec 9 02:12:43 2007 UTC vs.
Revision 1.177 by root, Tue Dec 11 15:06:50 2007 UTC

216# include <sys/inotify.h> 216# include <sys/inotify.h>
217#endif 217#endif
218 218
219/**/ 219/**/
220 220
221/*
222 * This is used to avoid floating point rounding problems.
223 * It is added to ev_rt_now when scheduling periodics
224 * to ensure progress, time-wise, even when rounding
225 * errors are against us.
226 * This value is good at least till the year 4000.
227 * Better solutions welcome.
228 */
229#define TIME_EPSILON 0.0001220703125 /* 1/8192 */
230
221#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 231#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
222#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ 232#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
223/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ 233/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds, TODO */
224 234
225#if __GNUC__ >= 3 235#if __GNUC__ >= 3
226# define expect(expr,value) __builtin_expect ((expr),(value)) 236# define expect(expr,value) __builtin_expect ((expr),(value))
227# define noinline __attribute__ ((noinline)) 237# define noinline __attribute__ ((noinline))
228#else 238#else
1231 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1241 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1232 1242
1233 /* first reschedule or stop timer */ 1243 /* first reschedule or stop timer */
1234 if (w->reschedule_cb) 1244 if (w->reschedule_cb)
1235 { 1245 {
1236 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); 1246 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON);
1237 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); 1247 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1238 downheap ((WT *)periodics, periodiccnt, 0); 1248 downheap ((WT *)periodics, periodiccnt, 0);
1239 } 1249 }
1240 else if (w->interval) 1250 else if (w->interval)
1241 { 1251 {
1242 ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 1252 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1253 if (((WT)w)->at - ev_rt_now <= TIME_EPSILON) ((WT)w)->at += w->interval;
1243 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); 1254 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
1244 downheap ((WT *)periodics, periodiccnt, 0); 1255 downheap ((WT *)periodics, periodiccnt, 0);
1245 } 1256 }
1246 else 1257 else
1247 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1258 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1261 ev_periodic *w = periodics [i]; 1272 ev_periodic *w = periodics [i];
1262 1273
1263 if (w->reschedule_cb) 1274 if (w->reschedule_cb)
1264 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1275 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1265 else if (w->interval) 1276 else if (w->interval)
1266 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1277 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1267 } 1278 }
1268 1279
1269 /* now rebuild the heap */ 1280 /* now rebuild the heap */
1270 for (i = periodiccnt >> 1; i--; ) 1281 for (i = periodiccnt >> 1; i--; )
1271 downheap ((WT *)periodics, periodiccnt, i); 1282 downheap ((WT *)periodics, periodiccnt, i);
1360 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1371 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1361 { 1372 {
1362#if EV_PERIODIC_ENABLE 1373#if EV_PERIODIC_ENABLE
1363 periodics_reschedule (EV_A); 1374 periodics_reschedule (EV_A);
1364#endif 1375#endif
1365
1366 /* adjust timers. this is easy, as the offset is the same for all of them */ 1376 /* adjust timers. this is easy, as the offset is the same for all of them */
1367 for (i = 0; i < timercnt; ++i) 1377 for (i = 0; i < timercnt; ++i)
1368 ((WT)timers [i])->at += ev_rt_now - mn_now; 1378 ((WT)timers [i])->at += ev_rt_now - mn_now;
1369 } 1379 }
1370 1380
1546ev_clear_pending (EV_P_ void *w) 1556ev_clear_pending (EV_P_ void *w)
1547{ 1557{
1548 W w_ = (W)w; 1558 W w_ = (W)w;
1549 int pending = w_->pending; 1559 int pending = w_->pending;
1550 1560
1551 if (!pending) 1561 if (expect_true (pending))
1562 {
1563 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
1564 w_->pending = 0;
1565 p->w = 0;
1566 return p->events;
1567 }
1568 else
1552 return 0; 1569 return 0;
1553
1554 w_->pending = 0;
1555 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
1556 p->w = 0;
1557
1558 return p->events;
1559} 1570}
1560 1571
1561void inline_size 1572void inline_size
1562pri_adjust (EV_P_ W w) 1573pri_adjust (EV_P_ W w)
1563{ 1574{
1689 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1700 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1690 else if (w->interval) 1701 else if (w->interval)
1691 { 1702 {
1692 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1703 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1693 /* this formula differs from the one in periodic_reify because we do not always round up */ 1704 /* this formula differs from the one in periodic_reify because we do not always round up */
1694 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1705 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1695 } 1706 }
1707 else
1708 ((WT)w)->at = w->offset;
1696 1709
1697 ev_start (EV_A_ (W)w, ++periodiccnt); 1710 ev_start (EV_A_ (W)w, ++periodiccnt);
1698 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1711 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1699 periodics [periodiccnt - 1] = w; 1712 periodics [periodiccnt - 1] = w;
1700 upheap ((WT *)periodics, periodiccnt - 1); 1713 upheap ((WT *)periodics, periodiccnt - 1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines