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.176 by root, Tue Dec 11 04:31:55 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 * and intervals up to 20 years.
228 * Better solutions welcome.
229 */
230#define TIME_EPSILON 0.0001220703125 /* 1/8192 */
231
221#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 232#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) */ 233#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 */ 234/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds, TODO */
224 235
225#if __GNUC__ >= 3 236#if __GNUC__ >= 3
226# define expect(expr,value) __builtin_expect ((expr),(value)) 237# define expect(expr,value) __builtin_expect ((expr),(value))
227# define noinline __attribute__ ((noinline)) 238# define noinline __attribute__ ((noinline))
228#else 239#else
1231 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1242 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1232 1243
1233 /* first reschedule or stop timer */ 1244 /* first reschedule or stop timer */
1234 if (w->reschedule_cb) 1245 if (w->reschedule_cb)
1235 { 1246 {
1236 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); 1247 ((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)); 1248 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1238 downheap ((WT *)periodics, periodiccnt, 0); 1249 downheap ((WT *)periodics, periodiccnt, 0);
1239 } 1250 }
1240 else if (w->interval) 1251 else if (w->interval)
1241 { 1252 {
1242 ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 1253 ((WT)w)->at = w->offset + floor ((ev_rt_now + TIME_EPSILON - w->offset) / w->interval + 1.) * 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);
1546ev_clear_pending (EV_P_ void *w) 1557ev_clear_pending (EV_P_ void *w)
1547{ 1558{
1548 W w_ = (W)w; 1559 W w_ = (W)w;
1549 int pending = w_->pending; 1560 int pending = w_->pending;
1550 1561
1551 if (!pending) 1562 if (expect_true (pending))
1563 {
1564 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
1565 w_->pending = 0;
1566 p->w = 0;
1567 return p->events;
1568 }
1569 else
1552 return 0; 1570 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} 1571}
1560 1572
1561void inline_size 1573void inline_size
1562pri_adjust (EV_P_ W w) 1574pri_adjust (EV_P_ W w)
1563{ 1575{
1689 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1701 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1690 else if (w->interval) 1702 else if (w->interval)
1691 { 1703 {
1692 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1704 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 */ 1705 /* 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; 1706 ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1695 } 1707 }
1708 else
1709 ((WT)w)->at = w->offset;
1696 1710
1697 ev_start (EV_A_ (W)w, ++periodiccnt); 1711 ev_start (EV_A_ (W)w, ++periodiccnt);
1698 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1712 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1699 periodics [periodiccnt - 1] = w; 1713 periodics [periodiccnt - 1] = w;
1700 upheap ((WT *)periodics, periodiccnt - 1); 1714 upheap ((WT *)periodics, periodiccnt - 1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines