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

Comparing libev/ev.c (file contents):
Revision 1.142 by root, Tue Nov 27 06:19:08 2007 UTC vs.
Revision 1.149 by root, Tue Nov 27 19:23:31 2007 UTC

156 156
157#ifndef EV_USE_PORT 157#ifndef EV_USE_PORT
158# define EV_USE_PORT 0 158# define EV_USE_PORT 0
159#endif 159#endif
160 160
161#ifndef EV_PID_HASHSIZE
162# if EV_MINIMAL
163# define EV_PID_HASHSIZE 1
164# else
165# define EV_PID_HASHSIZE 16
166# endif
167#endif
168
161/**/ 169/**/
162 170
163#ifndef CLOCK_MONOTONIC 171#ifndef CLOCK_MONOTONIC
164# undef EV_USE_MONOTONIC 172# undef EV_USE_MONOTONIC
165# define EV_USE_MONOTONIC 0 173# define EV_USE_MONOTONIC 0
176 184
177/**/ 185/**/
178 186
179#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 187#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
180#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ 188#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
181#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
182/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ 189/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
183 190
184#ifdef EV_H 191#ifdef EV_H
185# include EV_H 192# include EV_H
186#else 193#else
198# define inline_speed static inline 205# define inline_speed static inline
199# endif 206# endif
200#else 207#else
201# define expect(expr,value) (expr) 208# define expect(expr,value) (expr)
202# define inline_speed static 209# define inline_speed static
203# define inline_minimal static 210# define inline_size static
204# define noinline 211# define noinline
205#endif 212#endif
206 213
207#define expect_false(expr) expect ((expr) != 0, 0) 214#define expect_false(expr) expect ((expr) != 0, 0)
208#define expect_true(expr) expect ((expr) != 0, 1) 215#define expect_true(expr) expect ((expr) != 0, 1)
709 ev_unref (EV_A); /* child watcher should not keep loop alive */ 716 ev_unref (EV_A); /* child watcher should not keep loop alive */
710} 717}
711 718
712/*****************************************************************************/ 719/*****************************************************************************/
713 720
714static ev_child *childs [PID_HASHSIZE]; 721static ev_child *childs [EV_PID_HASHSIZE];
715 722
716#ifndef _WIN32 723#ifndef _WIN32
717 724
718static ev_signal childev; 725static ev_signal childev;
719 726
720void inline_speed 727void inline_speed
721child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status) 728child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status)
722{ 729{
723 ev_child *w; 730 ev_child *w;
724 731
725 for (w = (ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 732 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
726 if (w->pid == pid || !w->pid) 733 if (w->pid == pid || !w->pid)
727 { 734 {
728 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 735 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
729 w->rpid = pid; 736 w->rpid = pid;
730 w->rstatus = status; 737 w->rstatus = status;
751 /* make sure we are called again until all childs have been reaped */ 758 /* make sure we are called again until all childs have been reaped */
752 /* we need to do it this way so that the callback gets called before we continue */ 759 /* we need to do it this way so that the callback gets called before we continue */
753 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); 760 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
754 761
755 child_reap (EV_A_ sw, pid, pid, status); 762 child_reap (EV_A_ sw, pid, pid, status);
763 if (EV_PID_HASHSIZE > 1)
756 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */ 764 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
757} 765}
758 766
759#endif 767#endif
760 768
761/*****************************************************************************/ 769/*****************************************************************************/
1276 ? EVUNLOOP_ONE 1284 ? EVUNLOOP_ONE
1277 : EVUNLOOP_CANCEL; 1285 : EVUNLOOP_CANCEL;
1278 1286
1279 while (activecnt) 1287 while (activecnt)
1280 { 1288 {
1289 /* we might have forked, so reify kernel state if necessary */
1290 #if EV_FORK_ENABLE
1291 if (expect_false (postfork))
1292 if (forkcnt)
1293 {
1294 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
1295 call_pending (EV_A);
1296 }
1297 #endif
1298
1281 /* queue check watchers (and execute them) */ 1299 /* queue check watchers (and execute them) */
1282 if (expect_false (preparecnt)) 1300 if (expect_false (preparecnt))
1283 { 1301 {
1284 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1302 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1285 call_pending (EV_A); 1303 call_pending (EV_A);
1558 ev_periodic_stop (EV_A_ w); 1576 ev_periodic_stop (EV_A_ w);
1559 ev_periodic_start (EV_A_ w); 1577 ev_periodic_start (EV_A_ w);
1560} 1578}
1561#endif 1579#endif
1562 1580
1563void
1564ev_idle_start (EV_P_ ev_idle *w)
1565{
1566 if (expect_false (ev_is_active (w)))
1567 return;
1568
1569 ev_start (EV_A_ (W)w, ++idlecnt);
1570 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1571 idles [idlecnt - 1] = w;
1572}
1573
1574void
1575ev_idle_stop (EV_P_ ev_idle *w)
1576{
1577 ev_clear_pending (EV_A_ (W)w);
1578 if (expect_false (!ev_is_active (w)))
1579 return;
1580
1581 {
1582 int active = ((W)w)->active;
1583 idles [active - 1] = idles [--idlecnt];
1584 ((W)idles [active - 1])->active = active;
1585 }
1586
1587 ev_stop (EV_A_ (W)w);
1588}
1589
1590void
1591ev_prepare_start (EV_P_ ev_prepare *w)
1592{
1593 if (expect_false (ev_is_active (w)))
1594 return;
1595
1596 ev_start (EV_A_ (W)w, ++preparecnt);
1597 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1598 prepares [preparecnt - 1] = w;
1599}
1600
1601void
1602ev_prepare_stop (EV_P_ ev_prepare *w)
1603{
1604 ev_clear_pending (EV_A_ (W)w);
1605 if (expect_false (!ev_is_active (w)))
1606 return;
1607
1608 {
1609 int active = ((W)w)->active;
1610 prepares [active - 1] = prepares [--preparecnt];
1611 ((W)prepares [active - 1])->active = active;
1612 }
1613
1614 ev_stop (EV_A_ (W)w);
1615}
1616
1617void
1618ev_check_start (EV_P_ ev_check *w)
1619{
1620 if (expect_false (ev_is_active (w)))
1621 return;
1622
1623 ev_start (EV_A_ (W)w, ++checkcnt);
1624 array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
1625 checks [checkcnt - 1] = w;
1626}
1627
1628void
1629ev_check_stop (EV_P_ ev_check *w)
1630{
1631 ev_clear_pending (EV_A_ (W)w);
1632 if (expect_false (!ev_is_active (w)))
1633 return;
1634
1635 {
1636 int active = ((W)w)->active;
1637 checks [active - 1] = checks [--checkcnt];
1638 ((W)checks [active - 1])->active = active;
1639 }
1640
1641 ev_stop (EV_A_ (W)w);
1642}
1643
1644#ifndef SA_RESTART 1581#ifndef SA_RESTART
1645# define SA_RESTART 0 1582# define SA_RESTART 0
1646#endif 1583#endif
1647 1584
1648void 1585void
1696#endif 1633#endif
1697 if (expect_false (ev_is_active (w))) 1634 if (expect_false (ev_is_active (w)))
1698 return; 1635 return;
1699 1636
1700 ev_start (EV_A_ (W)w, 1); 1637 ev_start (EV_A_ (W)w, 1);
1701 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1638 wlist_add ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1702} 1639}
1703 1640
1704void 1641void
1705ev_child_stop (EV_P_ ev_child *w) 1642ev_child_stop (EV_P_ ev_child *w)
1706{ 1643{
1707 ev_clear_pending (EV_A_ (W)w); 1644 ev_clear_pending (EV_A_ (W)w);
1708 if (expect_false (!ev_is_active (w))) 1645 if (expect_false (!ev_is_active (w)))
1709 return; 1646 return;
1710 1647
1711 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1648 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1649 ev_stop (EV_A_ (W)w);
1650}
1651
1652#if EV_STAT_ENABLE
1653
1654# ifdef _WIN32
1655# undef lstat
1656# define lstat(a,b) _stati64 (a,b)
1657# endif
1658
1659#define DEF_STAT_INTERVAL 5.0074891
1660#define MIN_STAT_INTERVAL 0.1074891
1661
1662void
1663ev_stat_stat (EV_P_ ev_stat *w)
1664{
1665 if (lstat (w->path, &w->attr) < 0)
1666 w->attr.st_nlink = 0;
1667 else if (!w->attr.st_nlink)
1668 w->attr.st_nlink = 1;
1669}
1670
1671static void
1672stat_timer_cb (EV_P_ ev_timer *w_, int revents)
1673{
1674 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
1675
1676 /* we copy this here each the time so that */
1677 /* prev has the old value when the callback gets invoked */
1678 w->prev = w->attr;
1679 ev_stat_stat (EV_A_ w);
1680
1681 if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata)))
1682 ev_feed_event (EV_A_ w, EV_STAT);
1683}
1684
1685void
1686ev_stat_start (EV_P_ ev_stat *w)
1687{
1688 if (expect_false (ev_is_active (w)))
1689 return;
1690
1691 /* since we use memcmp, we need to clear any padding data etc. */
1692 memset (&w->prev, 0, sizeof (ev_statdata));
1693 memset (&w->attr, 0, sizeof (ev_statdata));
1694
1695 ev_stat_stat (EV_A_ w);
1696
1697 if (w->interval < MIN_STAT_INTERVAL)
1698 w->interval = w->interval ? MIN_STAT_INTERVAL : DEF_STAT_INTERVAL;
1699
1700 ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval);
1701 ev_set_priority (&w->timer, ev_priority (w));
1702 ev_timer_start (EV_A_ &w->timer);
1703
1704 ev_start (EV_A_ (W)w, 1);
1705}
1706
1707void
1708ev_stat_stop (EV_P_ ev_stat *w)
1709{
1710 ev_clear_pending (EV_A_ (W)w);
1711 if (expect_false (!ev_is_active (w)))
1712 return;
1713
1714 ev_timer_stop (EV_A_ &w->timer);
1715
1716 ev_stop (EV_A_ (W)w);
1717}
1718#endif
1719
1720void
1721ev_idle_start (EV_P_ ev_idle *w)
1722{
1723 if (expect_false (ev_is_active (w)))
1724 return;
1725
1726 ev_start (EV_A_ (W)w, ++idlecnt);
1727 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1728 idles [idlecnt - 1] = w;
1729}
1730
1731void
1732ev_idle_stop (EV_P_ ev_idle *w)
1733{
1734 ev_clear_pending (EV_A_ (W)w);
1735 if (expect_false (!ev_is_active (w)))
1736 return;
1737
1738 {
1739 int active = ((W)w)->active;
1740 idles [active - 1] = idles [--idlecnt];
1741 ((W)idles [active - 1])->active = active;
1742 }
1743
1744 ev_stop (EV_A_ (W)w);
1745}
1746
1747void
1748ev_prepare_start (EV_P_ ev_prepare *w)
1749{
1750 if (expect_false (ev_is_active (w)))
1751 return;
1752
1753 ev_start (EV_A_ (W)w, ++preparecnt);
1754 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1755 prepares [preparecnt - 1] = w;
1756}
1757
1758void
1759ev_prepare_stop (EV_P_ ev_prepare *w)
1760{
1761 ev_clear_pending (EV_A_ (W)w);
1762 if (expect_false (!ev_is_active (w)))
1763 return;
1764
1765 {
1766 int active = ((W)w)->active;
1767 prepares [active - 1] = prepares [--preparecnt];
1768 ((W)prepares [active - 1])->active = active;
1769 }
1770
1771 ev_stop (EV_A_ (W)w);
1772}
1773
1774void
1775ev_check_start (EV_P_ ev_check *w)
1776{
1777 if (expect_false (ev_is_active (w)))
1778 return;
1779
1780 ev_start (EV_A_ (W)w, ++checkcnt);
1781 array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
1782 checks [checkcnt - 1] = w;
1783}
1784
1785void
1786ev_check_stop (EV_P_ ev_check *w)
1787{
1788 ev_clear_pending (EV_A_ (W)w);
1789 if (expect_false (!ev_is_active (w)))
1790 return;
1791
1792 {
1793 int active = ((W)w)->active;
1794 checks [active - 1] = checks [--checkcnt];
1795 ((W)checks [active - 1])->active = active;
1796 }
1797
1712 ev_stop (EV_A_ (W)w); 1798 ev_stop (EV_A_ (W)w);
1713} 1799}
1714 1800
1715#if EV_EMBED_ENABLE 1801#if EV_EMBED_ENABLE
1716void noinline 1802void noinline
1759 1845
1760 ev_stop (EV_A_ (W)w); 1846 ev_stop (EV_A_ (W)w);
1761} 1847}
1762#endif 1848#endif
1763 1849
1764#if EV_STAT_ENABLE 1850#if EV_FORK_ENABLE
1765
1766# ifdef _WIN32
1767# define lstat(a,b) stat(a,b)
1768# endif
1769
1770void 1851void
1771ev_stat_stat (EV_P_ ev_stat *w)
1772{
1773 if (lstat (w->path, &w->attr) < 0)
1774 w->attr.st_nlink = 0;
1775 else if (!w->attr.st_nlink)
1776 w->attr.st_nlink = 1;
1777}
1778
1779static void
1780stat_timer_cb (EV_P_ ev_timer *w_, int revents)
1781{
1782 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
1783
1784 /* we copy this here each the time so that */
1785 /* prev has the old value when the callback gets invoked */
1786 w->prev = w->attr;
1787 ev_stat_stat (EV_A_ w);
1788
1789 if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata)))
1790 ev_feed_event (EV_A_ w, EV_STAT);
1791}
1792
1793void
1794ev_stat_start (EV_P_ ev_stat *w) 1852ev_fork_start (EV_P_ ev_fork *w)
1795{ 1853{
1796 if (expect_false (ev_is_active (w))) 1854 if (expect_false (ev_is_active (w)))
1797 return; 1855 return;
1798 1856
1799 /* since we use memcmp, we need to clear any padding data etc. */
1800 memset (&w->prev, 0, sizeof (ev_statdata));
1801 memset (&w->attr, 0, sizeof (ev_statdata));
1802
1803 ev_stat_stat (EV_A_ w);
1804
1805 ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval);
1806 ev_set_priority (&w->timer, ev_priority (w));
1807 ev_timer_start (EV_A_ &w->timer);
1808
1809 ev_start (EV_A_ (W)w, 1); 1857 ev_start (EV_A_ (W)w, ++forkcnt);
1858 array_needsize (ev_fork *, forks, forkmax, forkcnt, EMPTY2);
1859 forks [forkcnt - 1] = w;
1810} 1860}
1811 1861
1812void 1862void
1813ev_stat_stop (EV_P_ ev_stat *w) 1863ev_fork_stop (EV_P_ ev_fork *w)
1814{ 1864{
1815 ev_clear_pending (EV_A_ (W)w); 1865 ev_clear_pending (EV_A_ (W)w);
1816 if (expect_false (!ev_is_active (w))) 1866 if (expect_false (!ev_is_active (w)))
1817 return; 1867 return;
1818 1868
1819 ev_timer_stop (EV_A_ &w->timer); 1869 {
1870 int active = ((W)w)->active;
1871 forks [active - 1] = forks [--forkcnt];
1872 ((W)forks [active - 1])->active = active;
1873 }
1820 1874
1821 ev_stop (EV_A_ (W)w); 1875 ev_stop (EV_A_ (W)w);
1822} 1876}
1823#endif 1877#endif
1824 1878

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines