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

Comparing libev/ev.c (file contents):
Revision 1.163 by root, Wed Dec 5 13:54:36 2007 UTC vs.
Revision 1.168 by root, Sat Dec 8 14:12:07 2007 UTC

241 241
242#define expect_false(expr) expect ((expr) != 0, 0) 242#define expect_false(expr) expect ((expr) != 0, 0)
243#define expect_true(expr) expect ((expr) != 0, 1) 243#define expect_true(expr) expect ((expr) != 0, 1)
244 244
245#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 245#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
246#define ABSPRI(w) ((w)->priority - EV_MINPRI) 246#define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
247 247
248#define EMPTY0 /* required for microsofts broken pseudo-c compiler */ 248#define EMPTY /* required for microsofts broken pseudo-c compiler */
249#define EMPTY2(a,b) /* used to suppress some warnings */ 249#define EMPTY2(a,b) /* used to suppress some warnings */
250 250
251typedef ev_watcher *W; 251typedef ev_watcher *W;
252typedef ev_watcher_list *WL; 252typedef ev_watcher_list *WL;
253typedef ev_watcher_time *WT; 253typedef ev_watcher_time *WT;
507} 507}
508 508
509void 509void
510ev_feed_fd_event (EV_P_ int fd, int revents) 510ev_feed_fd_event (EV_P_ int fd, int revents)
511{ 511{
512 if (fd >= 0 && fd < anfdmax)
512 fd_event (EV_A_ fd, revents); 513 fd_event (EV_A_ fd, revents);
513} 514}
514 515
515void inline_size 516void inline_size
516fd_reify (EV_P) 517fd_reify (EV_P)
517{ 518{
786 ev_child *w; 787 ev_child *w;
787 788
788 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 789 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
789 if (w->pid == pid || !w->pid) 790 if (w->pid == pid || !w->pid)
790 { 791 {
791 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 792 ev_set_priority (w, ev_priority (sw)); /* need to do it *now* */
792 w->rpid = pid; 793 w->rpid = pid;
793 w->rstatus = status; 794 w->rstatus = status;
794 ev_feed_event (EV_A_ (W)w, EV_CHILD); 795 ev_feed_event (EV_A_ (W)w, EV_CHILD);
795 } 796 }
796} 797}
797 798
798#ifndef WCONTINUED 799#ifndef WCONTINUED
1003#if EV_USE_SELECT 1004#if EV_USE_SELECT
1004 if (backend == EVBACKEND_SELECT) select_destroy (EV_A); 1005 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
1005#endif 1006#endif
1006 1007
1007 for (i = NUMPRI; i--; ) 1008 for (i = NUMPRI; i--; )
1009 {
1008 array_free (pending, [i]); 1010 array_free (pending, [i]);
1011#if EV_IDLE_ENABLE
1012 array_free (idle, [i]);
1013#endif
1014 }
1009 1015
1010 /* have to use the microsoft-never-gets-it-right macro */ 1016 /* have to use the microsoft-never-gets-it-right macro */
1011 array_free (fdchange, EMPTY0); 1017 array_free (fdchange, EMPTY);
1012 array_free (timer, EMPTY0); 1018 array_free (timer, EMPTY);
1013#if EV_PERIODIC_ENABLE 1019#if EV_PERIODIC_ENABLE
1014 array_free (periodic, EMPTY0); 1020 array_free (periodic, EMPTY);
1015#endif 1021#endif
1016 array_free (idle, EMPTY0);
1017 array_free (prepare, EMPTY0); 1022 array_free (prepare, EMPTY);
1018 array_free (check, EMPTY0); 1023 array_free (check, EMPTY);
1019 1024
1020 backend = 0; 1025 backend = 0;
1021} 1026}
1022 1027
1023void inline_size infy_fork (EV_P); 1028void inline_size infy_fork (EV_P);
1159 postfork = 1; 1164 postfork = 1;
1160} 1165}
1161 1166
1162/*****************************************************************************/ 1167/*****************************************************************************/
1163 1168
1164int inline_size 1169void
1165any_pending (EV_P) 1170ev_invoke (EV_P_ void *w, int revents)
1166{ 1171{
1167 int pri; 1172 EV_CB_INVOKE ((W)w, revents);
1168
1169 for (pri = NUMPRI; pri--; )
1170 if (pendingcnt [pri])
1171 return 1;
1172
1173 return 0;
1174} 1173}
1175 1174
1176void inline_speed 1175void inline_speed
1177call_pending (EV_P) 1176call_pending (EV_P)
1178{ 1177{
1270 for (i = periodiccnt >> 1; i--; ) 1269 for (i = periodiccnt >> 1; i--; )
1271 downheap ((WT *)periodics, periodiccnt, i); 1270 downheap ((WT *)periodics, periodiccnt, i);
1272} 1271}
1273#endif 1272#endif
1274 1273
1274#if EV_IDLE_ENABLE
1275void inline_size
1276idle_reify (EV_P)
1277{
1278 if (expect_false (idleall))
1279 {
1280 int pri;
1281
1282 for (pri = NUMPRI; pri--; )
1283 {
1284 if (pendingcnt [pri])
1285 break;
1286
1287 if (idlecnt [pri])
1288 {
1289 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1290 break;
1291 }
1292 }
1293 }
1294}
1295#endif
1296
1275int inline_size 1297int inline_size
1276time_update_monotonic (EV_P) 1298time_update_monotonic (EV_P)
1277{ 1299{
1278 mn_now = get_clock (); 1300 mn_now = get_clock ();
1279 1301
1412 1434
1413 /* calculate blocking time */ 1435 /* calculate blocking time */
1414 { 1436 {
1415 ev_tstamp block; 1437 ev_tstamp block;
1416 1438
1417 if (expect_false (flags & EVLOOP_NONBLOCK || idlecnt || !activecnt)) 1439 if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt))
1418 block = 0.; /* do not block at all */ 1440 block = 0.; /* do not block at all */
1419 else 1441 else
1420 { 1442 {
1421 /* update time to cancel out callback processing overhead */ 1443 /* update time to cancel out callback processing overhead */
1422#if EV_USE_MONOTONIC 1444#if EV_USE_MONOTONIC
1459 timers_reify (EV_A); /* relative timers called last */ 1481 timers_reify (EV_A); /* relative timers called last */
1460#if EV_PERIODIC_ENABLE 1482#if EV_PERIODIC_ENABLE
1461 periodics_reify (EV_A); /* absolute timers called first */ 1483 periodics_reify (EV_A); /* absolute timers called first */
1462#endif 1484#endif
1463 1485
1486#if EV_IDLE_ENABLE
1464 /* queue idle watchers unless other events are pending */ 1487 /* queue idle watchers unless other events are pending */
1465 if (idlecnt && !any_pending (EV_A)) 1488 idle_reify (EV_A);
1466 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1489#endif
1467 1490
1468 /* queue check watchers, to be executed first */ 1491 /* queue check watchers, to be executed first */
1469 if (expect_false (checkcnt)) 1492 if (expect_false (checkcnt))
1470 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1493 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1471 1494
1507 head = &(*head)->next; 1530 head = &(*head)->next;
1508 } 1531 }
1509} 1532}
1510 1533
1511void inline_speed 1534void inline_speed
1512ev_clear_pending (EV_P_ W w) 1535clear_pending (EV_P_ W w)
1513{ 1536{
1514 if (w->pending) 1537 if (w->pending)
1515 { 1538 {
1516 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1539 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1517 w->pending = 0; 1540 w->pending = 0;
1518 } 1541 }
1519} 1542}
1520 1543
1544int
1545ev_clear_pending (EV_P_ void *w)
1546{
1547 W w_ = (W)w;
1548 int pending = w_->pending;
1549
1550 if (!pending)
1551 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}
1559
1560void inline_size
1561pri_adjust (EV_P_ W w)
1562{
1563 int pri = w->priority;
1564 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
1565 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
1566 w->priority = pri;
1567}
1568
1521void inline_speed 1569void inline_speed
1522ev_start (EV_P_ W w, int active) 1570ev_start (EV_P_ W w, int active)
1523{ 1571{
1524 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1572 pri_adjust (EV_A_ w);
1525 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1526
1527 w->active = active; 1573 w->active = active;
1528 ev_ref (EV_A); 1574 ev_ref (EV_A);
1529} 1575}
1530 1576
1531void inline_size 1577void inline_size
1555} 1601}
1556 1602
1557void 1603void
1558ev_io_stop (EV_P_ ev_io *w) 1604ev_io_stop (EV_P_ ev_io *w)
1559{ 1605{
1560 ev_clear_pending (EV_A_ (W)w); 1606 clear_pending (EV_A_ (W)w);
1561 if (expect_false (!ev_is_active (w))) 1607 if (expect_false (!ev_is_active (w)))
1562 return; 1608 return;
1563 1609
1564 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 1610 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1565 1611
1588} 1634}
1589 1635
1590void 1636void
1591ev_timer_stop (EV_P_ ev_timer *w) 1637ev_timer_stop (EV_P_ ev_timer *w)
1592{ 1638{
1593 ev_clear_pending (EV_A_ (W)w); 1639 clear_pending (EV_A_ (W)w);
1594 if (expect_false (!ev_is_active (w))) 1640 if (expect_false (!ev_is_active (w)))
1595 return; 1641 return;
1596 1642
1597 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1643 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1598 1644
1656} 1702}
1657 1703
1658void 1704void
1659ev_periodic_stop (EV_P_ ev_periodic *w) 1705ev_periodic_stop (EV_P_ ev_periodic *w)
1660{ 1706{
1661 ev_clear_pending (EV_A_ (W)w); 1707 clear_pending (EV_A_ (W)w);
1662 if (expect_false (!ev_is_active (w))) 1708 if (expect_false (!ev_is_active (w)))
1663 return; 1709 return;
1664 1710
1665 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1711 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1666 1712
1720} 1766}
1721 1767
1722void 1768void
1723ev_signal_stop (EV_P_ ev_signal *w) 1769ev_signal_stop (EV_P_ ev_signal *w)
1724{ 1770{
1725 ev_clear_pending (EV_A_ (W)w); 1771 clear_pending (EV_A_ (W)w);
1726 if (expect_false (!ev_is_active (w))) 1772 if (expect_false (!ev_is_active (w)))
1727 return; 1773 return;
1728 1774
1729 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1775 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1730 ev_stop (EV_A_ (W)w); 1776 ev_stop (EV_A_ (W)w);
1747} 1793}
1748 1794
1749void 1795void
1750ev_child_stop (EV_P_ ev_child *w) 1796ev_child_stop (EV_P_ ev_child *w)
1751{ 1797{
1752 ev_clear_pending (EV_A_ (W)w); 1798 clear_pending (EV_A_ (W)w);
1753 if (expect_false (!ev_is_active (w))) 1799 if (expect_false (!ev_is_active (w)))
1754 return; 1800 return;
1755 1801
1756 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 1802 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1757 ev_stop (EV_A_ (W)w); 1803 ev_stop (EV_A_ (W)w);
1993} 2039}
1994 2040
1995void 2041void
1996ev_stat_stop (EV_P_ ev_stat *w) 2042ev_stat_stop (EV_P_ ev_stat *w)
1997{ 2043{
1998 ev_clear_pending (EV_A_ (W)w); 2044 clear_pending (EV_A_ (W)w);
1999 if (expect_false (!ev_is_active (w))) 2045 if (expect_false (!ev_is_active (w)))
2000 return; 2046 return;
2001 2047
2002#if EV_USE_INOTIFY 2048#if EV_USE_INOTIFY
2003 infy_del (EV_A_ w); 2049 infy_del (EV_A_ w);
2006 2052
2007 ev_stop (EV_A_ (W)w); 2053 ev_stop (EV_A_ (W)w);
2008} 2054}
2009#endif 2055#endif
2010 2056
2057#if EV_IDLE_ENABLE
2011void 2058void
2012ev_idle_start (EV_P_ ev_idle *w) 2059ev_idle_start (EV_P_ ev_idle *w)
2013{ 2060{
2014 if (expect_false (ev_is_active (w))) 2061 if (expect_false (ev_is_active (w)))
2015 return; 2062 return;
2016 2063
2064 pri_adjust (EV_A_ (W)w);
2065
2066 {
2067 int active = ++idlecnt [ABSPRI (w)];
2068
2069 ++idleall;
2017 ev_start (EV_A_ (W)w, ++idlecnt); 2070 ev_start (EV_A_ (W)w, active);
2071
2018 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); 2072 array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2);
2019 idles [idlecnt - 1] = w; 2073 idles [ABSPRI (w)][active - 1] = w;
2074 }
2020} 2075}
2021 2076
2022void 2077void
2023ev_idle_stop (EV_P_ ev_idle *w) 2078ev_idle_stop (EV_P_ ev_idle *w)
2024{ 2079{
2025 ev_clear_pending (EV_A_ (W)w); 2080 clear_pending (EV_A_ (W)w);
2026 if (expect_false (!ev_is_active (w))) 2081 if (expect_false (!ev_is_active (w)))
2027 return; 2082 return;
2028 2083
2029 { 2084 {
2030 int active = ((W)w)->active; 2085 int active = ((W)w)->active;
2031 idles [active - 1] = idles [--idlecnt]; 2086
2087 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2032 ((W)idles [active - 1])->active = active; 2088 ((W)idles [ABSPRI (w)][active - 1])->active = active;
2089
2090 ev_stop (EV_A_ (W)w);
2091 --idleall;
2033 } 2092 }
2034
2035 ev_stop (EV_A_ (W)w);
2036} 2093}
2094#endif
2037 2095
2038void 2096void
2039ev_prepare_start (EV_P_ ev_prepare *w) 2097ev_prepare_start (EV_P_ ev_prepare *w)
2040{ 2098{
2041 if (expect_false (ev_is_active (w))) 2099 if (expect_false (ev_is_active (w)))
2047} 2105}
2048 2106
2049void 2107void
2050ev_prepare_stop (EV_P_ ev_prepare *w) 2108ev_prepare_stop (EV_P_ ev_prepare *w)
2051{ 2109{
2052 ev_clear_pending (EV_A_ (W)w); 2110 clear_pending (EV_A_ (W)w);
2053 if (expect_false (!ev_is_active (w))) 2111 if (expect_false (!ev_is_active (w)))
2054 return; 2112 return;
2055 2113
2056 { 2114 {
2057 int active = ((W)w)->active; 2115 int active = ((W)w)->active;
2074} 2132}
2075 2133
2076void 2134void
2077ev_check_stop (EV_P_ ev_check *w) 2135ev_check_stop (EV_P_ ev_check *w)
2078{ 2136{
2079 ev_clear_pending (EV_A_ (W)w); 2137 clear_pending (EV_A_ (W)w);
2080 if (expect_false (!ev_is_active (w))) 2138 if (expect_false (!ev_is_active (w)))
2081 return; 2139 return;
2082 2140
2083 { 2141 {
2084 int active = ((W)w)->active; 2142 int active = ((W)w)->active;
2126} 2184}
2127 2185
2128void 2186void
2129ev_embed_stop (EV_P_ ev_embed *w) 2187ev_embed_stop (EV_P_ ev_embed *w)
2130{ 2188{
2131 ev_clear_pending (EV_A_ (W)w); 2189 clear_pending (EV_A_ (W)w);
2132 if (expect_false (!ev_is_active (w))) 2190 if (expect_false (!ev_is_active (w)))
2133 return; 2191 return;
2134 2192
2135 ev_io_stop (EV_A_ &w->io); 2193 ev_io_stop (EV_A_ &w->io);
2136 2194
2151} 2209}
2152 2210
2153void 2211void
2154ev_fork_stop (EV_P_ ev_fork *w) 2212ev_fork_stop (EV_P_ ev_fork *w)
2155{ 2213{
2156 ev_clear_pending (EV_A_ (W)w); 2214 clear_pending (EV_A_ (W)w);
2157 if (expect_false (!ev_is_active (w))) 2215 if (expect_false (!ev_is_active (w)))
2158 return; 2216 return;
2159 2217
2160 { 2218 {
2161 int active = ((W)w)->active; 2219 int active = ((W)w)->active;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines