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

Comparing libev/ev.c (file contents):
Revision 1.168 by root, Sat Dec 8 14:12:07 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 inline_size static inline /* inline for codesize */
228# if EV_MINIMAL
229# define noinline __attribute__ ((noinline)) 237# define noinline __attribute__ ((noinline))
230# define inline_speed static noinline
231# else
232# define noinline
233# define inline_speed static inline
234# endif
235#else 238#else
236# define expect(expr,value) (expr) 239# define expect(expr,value) (expr)
237# define inline_speed static
238# define inline_size static
239# define noinline 240# define noinline
241# if __STDC_VERSION__ < 199901L
242# define inline
243# endif
240#endif 244#endif
241 245
242#define expect_false(expr) expect ((expr) != 0, 0) 246#define expect_false(expr) expect ((expr) != 0, 0)
243#define expect_true(expr) expect ((expr) != 0, 1) 247#define expect_true(expr) expect ((expr) != 0, 1)
248#define inline_size static inline
249
250#if EV_MINIMAL
251# define inline_speed static noinline
252#else
253# define inline_speed static inline
254#endif
244 255
245#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 256#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
246#define ABSPRI(w) (((W)w)->priority - EV_MINPRI) 257#define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
247 258
248#define EMPTY /* required for microsofts broken pseudo-c compiler */ 259#define EMPTY /* required for microsofts broken pseudo-c compiler */
417 } 428 }
418 429
419 return ncur; 430 return ncur;
420} 431}
421 432
422inline_speed void * 433static noinline void *
423array_realloc (int elem, void *base, int *cur, int cnt) 434array_realloc (int elem, void *base, int *cur, int cnt)
424{ 435{
425 *cur = array_nextsize (elem, *cur, cnt); 436 *cur = array_nextsize (elem, *cur, cnt);
426 return ev_realloc (base, elem * *cur); 437 return ev_realloc (base, elem * *cur);
427} 438}
452 463
453void noinline 464void noinline
454ev_feed_event (EV_P_ void *w, int revents) 465ev_feed_event (EV_P_ void *w, int revents)
455{ 466{
456 W w_ = (W)w; 467 W w_ = (W)w;
468 int pri = ABSPRI (w_);
457 469
458 if (expect_false (w_->pending)) 470 if (expect_false (w_->pending))
471 pendings [pri][w_->pending - 1].events |= revents;
472 else
459 { 473 {
474 w_->pending = ++pendingcnt [pri];
475 array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2);
476 pendings [pri][w_->pending - 1].w = w_;
460 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; 477 pendings [pri][w_->pending - 1].events = revents;
461 return;
462 } 478 }
463
464 w_->pending = ++pendingcnt [ABSPRI (w_)];
465 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2);
466 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
467 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
468} 479}
469 480
470void inline_size 481void inline_size
471queue_events (EV_P_ W *events, int eventcnt, int type) 482queue_events (EV_P_ W *events, int eventcnt, int type)
472{ 483{
748 for (signum = signalmax; signum--; ) 759 for (signum = signalmax; signum--; )
749 if (signals [signum].gotsig) 760 if (signals [signum].gotsig)
750 ev_feed_signal_event (EV_A_ signum + 1); 761 ev_feed_signal_event (EV_A_ signum + 1);
751} 762}
752 763
753void inline_size 764void inline_speed
754fd_intern (int fd) 765fd_intern (int fd)
755{ 766{
756#ifdef _WIN32 767#ifdef _WIN32
757 int arg = 1; 768 int arg = 1;
758 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 769 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
1230 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1241 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1231 1242
1232 /* first reschedule or stop timer */ 1243 /* first reschedule or stop timer */
1233 if (w->reschedule_cb) 1244 if (w->reschedule_cb)
1234 { 1245 {
1235 ((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);
1236 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));
1237 downheap ((WT *)periodics, periodiccnt, 0); 1248 downheap ((WT *)periodics, periodiccnt, 0);
1238 } 1249 }
1239 else if (w->interval) 1250 else if (w->interval)
1240 { 1251 {
1241 ((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;
1242 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));
1243 downheap ((WT *)periodics, periodiccnt, 0); 1255 downheap ((WT *)periodics, periodiccnt, 0);
1244 } 1256 }
1245 else 1257 else
1246 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1258 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1260 ev_periodic *w = periodics [i]; 1272 ev_periodic *w = periodics [i];
1261 1273
1262 if (w->reschedule_cb) 1274 if (w->reschedule_cb)
1263 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1275 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1264 else if (w->interval) 1276 else if (w->interval)
1265 ((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;
1266 } 1278 }
1267 1279
1268 /* now rebuild the heap */ 1280 /* now rebuild the heap */
1269 for (i = periodiccnt >> 1; i--; ) 1281 for (i = periodiccnt >> 1; i--; )
1270 downheap ((WT *)periodics, periodiccnt, i); 1282 downheap ((WT *)periodics, periodiccnt, i);
1359 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))
1360 { 1372 {
1361#if EV_PERIODIC_ENABLE 1373#if EV_PERIODIC_ENABLE
1362 periodics_reschedule (EV_A); 1374 periodics_reschedule (EV_A);
1363#endif 1375#endif
1364
1365 /* 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 */
1366 for (i = 0; i < timercnt; ++i) 1377 for (i = 0; i < timercnt; ++i)
1367 ((WT)timers [i])->at += ev_rt_now - mn_now; 1378 ((WT)timers [i])->at += ev_rt_now - mn_now;
1368 } 1379 }
1369 1380
1413 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 1424 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
1414 call_pending (EV_A); 1425 call_pending (EV_A);
1415 } 1426 }
1416#endif 1427#endif
1417 1428
1418 /* queue check watchers (and execute them) */ 1429 /* queue prepare watchers (and execute them) */
1419 if (expect_false (preparecnt)) 1430 if (expect_false (preparecnt))
1420 { 1431 {
1421 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1432 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1422 call_pending (EV_A); 1433 call_pending (EV_A);
1423 } 1434 }
1545ev_clear_pending (EV_P_ void *w) 1556ev_clear_pending (EV_P_ void *w)
1546{ 1557{
1547 W w_ = (W)w; 1558 W w_ = (W)w;
1548 int pending = w_->pending; 1559 int pending = w_->pending;
1549 1560
1550 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
1551 return 0; 1569 return 0;
1552
1553 w_->pending = 0;
1554 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
1555 p->w = 0;
1556
1557 return p->events;
1558} 1570}
1559 1571
1560void inline_size 1572void inline_size
1561pri_adjust (EV_P_ W w) 1573pri_adjust (EV_P_ W w)
1562{ 1574{
1581 w->active = 0; 1593 w->active = 0;
1582} 1594}
1583 1595
1584/*****************************************************************************/ 1596/*****************************************************************************/
1585 1597
1586void 1598void noinline
1587ev_io_start (EV_P_ ev_io *w) 1599ev_io_start (EV_P_ ev_io *w)
1588{ 1600{
1589 int fd = w->fd; 1601 int fd = w->fd;
1590 1602
1591 if (expect_false (ev_is_active (w))) 1603 if (expect_false (ev_is_active (w)))
1598 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1610 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1599 1611
1600 fd_change (EV_A_ fd); 1612 fd_change (EV_A_ fd);
1601} 1613}
1602 1614
1603void 1615void noinline
1604ev_io_stop (EV_P_ ev_io *w) 1616ev_io_stop (EV_P_ ev_io *w)
1605{ 1617{
1606 clear_pending (EV_A_ (W)w); 1618 clear_pending (EV_A_ (W)w);
1607 if (expect_false (!ev_is_active (w))) 1619 if (expect_false (!ev_is_active (w)))
1608 return; 1620 return;
1613 ev_stop (EV_A_ (W)w); 1625 ev_stop (EV_A_ (W)w);
1614 1626
1615 fd_change (EV_A_ w->fd); 1627 fd_change (EV_A_ w->fd);
1616} 1628}
1617 1629
1618void 1630void noinline
1619ev_timer_start (EV_P_ ev_timer *w) 1631ev_timer_start (EV_P_ ev_timer *w)
1620{ 1632{
1621 if (expect_false (ev_is_active (w))) 1633 if (expect_false (ev_is_active (w)))
1622 return; 1634 return;
1623 1635
1631 upheap ((WT *)timers, timercnt - 1); 1643 upheap ((WT *)timers, timercnt - 1);
1632 1644
1633 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 1645 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/
1634} 1646}
1635 1647
1636void 1648void noinline
1637ev_timer_stop (EV_P_ ev_timer *w) 1649ev_timer_stop (EV_P_ ev_timer *w)
1638{ 1650{
1639 clear_pending (EV_A_ (W)w); 1651 clear_pending (EV_A_ (W)w);
1640 if (expect_false (!ev_is_active (w))) 1652 if (expect_false (!ev_is_active (w)))
1641 return; 1653 return;
1655 ((WT)w)->at -= mn_now; 1667 ((WT)w)->at -= mn_now;
1656 1668
1657 ev_stop (EV_A_ (W)w); 1669 ev_stop (EV_A_ (W)w);
1658} 1670}
1659 1671
1660void 1672void noinline
1661ev_timer_again (EV_P_ ev_timer *w) 1673ev_timer_again (EV_P_ ev_timer *w)
1662{ 1674{
1663 if (ev_is_active (w)) 1675 if (ev_is_active (w))
1664 { 1676 {
1665 if (w->repeat) 1677 if (w->repeat)
1676 ev_timer_start (EV_A_ w); 1688 ev_timer_start (EV_A_ w);
1677 } 1689 }
1678} 1690}
1679 1691
1680#if EV_PERIODIC_ENABLE 1692#if EV_PERIODIC_ENABLE
1681void 1693void noinline
1682ev_periodic_start (EV_P_ ev_periodic *w) 1694ev_periodic_start (EV_P_ ev_periodic *w)
1683{ 1695{
1684 if (expect_false (ev_is_active (w))) 1696 if (expect_false (ev_is_active (w)))
1685 return; 1697 return;
1686 1698
1688 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1700 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1689 else if (w->interval) 1701 else if (w->interval)
1690 { 1702 {
1691 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.));
1692 /* 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 */
1693 ((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;
1694 } 1706 }
1707 else
1708 ((WT)w)->at = w->offset;
1695 1709
1696 ev_start (EV_A_ (W)w, ++periodiccnt); 1710 ev_start (EV_A_ (W)w, ++periodiccnt);
1697 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1711 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1698 periodics [periodiccnt - 1] = w; 1712 periodics [periodiccnt - 1] = w;
1699 upheap ((WT *)periodics, periodiccnt - 1); 1713 upheap ((WT *)periodics, periodiccnt - 1);
1700 1714
1701 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 1715 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/
1702} 1716}
1703 1717
1704void 1718void noinline
1705ev_periodic_stop (EV_P_ ev_periodic *w) 1719ev_periodic_stop (EV_P_ ev_periodic *w)
1706{ 1720{
1707 clear_pending (EV_A_ (W)w); 1721 clear_pending (EV_A_ (W)w);
1708 if (expect_false (!ev_is_active (w))) 1722 if (expect_false (!ev_is_active (w)))
1709 return; 1723 return;
1721 } 1735 }
1722 1736
1723 ev_stop (EV_A_ (W)w); 1737 ev_stop (EV_A_ (W)w);
1724} 1738}
1725 1739
1726void 1740void noinline
1727ev_periodic_again (EV_P_ ev_periodic *w) 1741ev_periodic_again (EV_P_ ev_periodic *w)
1728{ 1742{
1729 /* TODO: use adjustheap and recalculation */ 1743 /* TODO: use adjustheap and recalculation */
1730 ev_periodic_stop (EV_A_ w); 1744 ev_periodic_stop (EV_A_ w);
1731 ev_periodic_start (EV_A_ w); 1745 ev_periodic_start (EV_A_ w);
1734 1748
1735#ifndef SA_RESTART 1749#ifndef SA_RESTART
1736# define SA_RESTART 0 1750# define SA_RESTART 0
1737#endif 1751#endif
1738 1752
1739void 1753void noinline
1740ev_signal_start (EV_P_ ev_signal *w) 1754ev_signal_start (EV_P_ ev_signal *w)
1741{ 1755{
1742#if EV_MULTIPLICITY 1756#if EV_MULTIPLICITY
1743 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1757 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1744#endif 1758#endif
1763 sigaction (w->signum, &sa, 0); 1777 sigaction (w->signum, &sa, 0);
1764#endif 1778#endif
1765 } 1779 }
1766} 1780}
1767 1781
1768void 1782void noinline
1769ev_signal_stop (EV_P_ ev_signal *w) 1783ev_signal_stop (EV_P_ ev_signal *w)
1770{ 1784{
1771 clear_pending (EV_A_ (W)w); 1785 clear_pending (EV_A_ (W)w);
1772 if (expect_false (!ev_is_active (w))) 1786 if (expect_false (!ev_is_active (w)))
1773 return; 1787 return;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines