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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines