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

Comparing libev/ev.c (file contents):
Revision 1.162 by root, Mon Dec 3 13:41:24 2007 UTC vs.
Revision 1.167 by root, Sat Dec 8 04:02:31 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;
396{ 396{
397 return ev_rt_now; 397 return ev_rt_now;
398} 398}
399#endif 399#endif
400 400
401#define array_roundsize(type,n) (((n) | 4) & ~3) 401int inline_size
402array_nextsize (int elem, int cur, int cnt)
403{
404 int ncur = cur + 1;
405
406 do
407 ncur <<= 1;
408 while (cnt > ncur);
409
410 /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */
411 if (elem * ncur > 4096)
412 {
413 ncur *= elem;
414 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095;
415 ncur = ncur - sizeof (void *) * 4;
416 ncur /= elem;
417 }
418
419 return ncur;
420}
421
422inline_speed void *
423array_realloc (int elem, void *base, int *cur, int cnt)
424{
425 *cur = array_nextsize (elem, *cur, cnt);
426 return ev_realloc (base, elem * *cur);
427}
402 428
403#define array_needsize(type,base,cur,cnt,init) \ 429#define array_needsize(type,base,cur,cnt,init) \
404 if (expect_false ((cnt) > cur)) \ 430 if (expect_false ((cnt) > (cur))) \
405 { \ 431 { \
406 int newcnt = cur; \ 432 int ocur_ = (cur); \
407 do \ 433 (base) = (type *)array_realloc \
408 { \ 434 (sizeof (type), (base), &(cur), (cnt)); \
409 newcnt = array_roundsize (type, newcnt << 1); \ 435 init ((base) + (ocur_), (cur) - ocur_); \
410 } \
411 while ((cnt) > newcnt); \
412 \
413 base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
414 init (base + cur, newcnt - cur); \
415 cur = newcnt; \
416 } 436 }
417 437
438#if 0
418#define array_slim(type,stem) \ 439#define array_slim(type,stem) \
419 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 440 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
420 { \ 441 { \
421 stem ## max = array_roundsize (stem ## cnt >> 1); \ 442 stem ## max = array_roundsize (stem ## cnt >> 1); \
422 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 443 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
423 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 444 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
424 } 445 }
446#endif
425 447
426#define array_free(stem, idx) \ 448#define array_free(stem, idx) \
427 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 449 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
428 450
429/*****************************************************************************/ 451/*****************************************************************************/
764 ev_child *w; 786 ev_child *w;
765 787
766 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 788 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
767 if (w->pid == pid || !w->pid) 789 if (w->pid == pid || !w->pid)
768 { 790 {
769 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 791 ev_set_priority (w, ev_priority (sw)); /* need to do it *now* */
770 w->rpid = pid; 792 w->rpid = pid;
771 w->rstatus = status; 793 w->rstatus = status;
772 ev_feed_event (EV_A_ (W)w, EV_CHILD); 794 ev_feed_event (EV_A_ (W)w, EV_CHILD);
773 } 795 }
774} 796}
775 797
776#ifndef WCONTINUED 798#ifndef WCONTINUED
981#if EV_USE_SELECT 1003#if EV_USE_SELECT
982 if (backend == EVBACKEND_SELECT) select_destroy (EV_A); 1004 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
983#endif 1005#endif
984 1006
985 for (i = NUMPRI; i--; ) 1007 for (i = NUMPRI; i--; )
1008 {
986 array_free (pending, [i]); 1009 array_free (pending, [i]);
1010#if EV_IDLE_ENABLE
1011 array_free (idle, [i]);
1012#endif
1013 }
987 1014
988 /* have to use the microsoft-never-gets-it-right macro */ 1015 /* have to use the microsoft-never-gets-it-right macro */
989 array_free (fdchange, EMPTY0); 1016 array_free (fdchange, EMPTY);
990 array_free (timer, EMPTY0); 1017 array_free (timer, EMPTY);
991#if EV_PERIODIC_ENABLE 1018#if EV_PERIODIC_ENABLE
992 array_free (periodic, EMPTY0); 1019 array_free (periodic, EMPTY);
993#endif 1020#endif
994 array_free (idle, EMPTY0);
995 array_free (prepare, EMPTY0); 1021 array_free (prepare, EMPTY);
996 array_free (check, EMPTY0); 1022 array_free (check, EMPTY);
997 1023
998 backend = 0; 1024 backend = 0;
999} 1025}
1000 1026
1001void inline_size infy_fork (EV_P); 1027void inline_size infy_fork (EV_P);
1136 if (backend) 1162 if (backend)
1137 postfork = 1; 1163 postfork = 1;
1138} 1164}
1139 1165
1140/*****************************************************************************/ 1166/*****************************************************************************/
1141
1142int inline_size
1143any_pending (EV_P)
1144{
1145 int pri;
1146
1147 for (pri = NUMPRI; pri--; )
1148 if (pendingcnt [pri])
1149 return 1;
1150
1151 return 0;
1152}
1153 1167
1154void inline_speed 1168void inline_speed
1155call_pending (EV_P) 1169call_pending (EV_P)
1156{ 1170{
1157 int pri; 1171 int pri;
1248 for (i = periodiccnt >> 1; i--; ) 1262 for (i = periodiccnt >> 1; i--; )
1249 downheap ((WT *)periodics, periodiccnt, i); 1263 downheap ((WT *)periodics, periodiccnt, i);
1250} 1264}
1251#endif 1265#endif
1252 1266
1267#if EV_IDLE_ENABLE
1268void inline_size
1269idle_reify (EV_P)
1270{
1271 if (expect_false (idleall))
1272 {
1273 int pri;
1274
1275 for (pri = NUMPRI; pri--; )
1276 {
1277 if (pendingcnt [pri])
1278 break;
1279
1280 if (idlecnt [pri])
1281 {
1282 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1283 break;
1284 }
1285 }
1286 }
1287}
1288#endif
1289
1253int inline_size 1290int inline_size
1254time_update_monotonic (EV_P) 1291time_update_monotonic (EV_P)
1255{ 1292{
1256 mn_now = get_clock (); 1293 mn_now = get_clock ();
1257 1294
1390 1427
1391 /* calculate blocking time */ 1428 /* calculate blocking time */
1392 { 1429 {
1393 ev_tstamp block; 1430 ev_tstamp block;
1394 1431
1395 if (expect_false (flags & EVLOOP_NONBLOCK || idlecnt || !activecnt)) 1432 if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt))
1396 block = 0.; /* do not block at all */ 1433 block = 0.; /* do not block at all */
1397 else 1434 else
1398 { 1435 {
1399 /* update time to cancel out callback processing overhead */ 1436 /* update time to cancel out callback processing overhead */
1400#if EV_USE_MONOTONIC 1437#if EV_USE_MONOTONIC
1437 timers_reify (EV_A); /* relative timers called last */ 1474 timers_reify (EV_A); /* relative timers called last */
1438#if EV_PERIODIC_ENABLE 1475#if EV_PERIODIC_ENABLE
1439 periodics_reify (EV_A); /* absolute timers called first */ 1476 periodics_reify (EV_A); /* absolute timers called first */
1440#endif 1477#endif
1441 1478
1479#if EV_IDLE_ENABLE
1442 /* queue idle watchers unless other events are pending */ 1480 /* queue idle watchers unless other events are pending */
1443 if (idlecnt && !any_pending (EV_A)) 1481 idle_reify (EV_A);
1444 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1482#endif
1445 1483
1446 /* queue check watchers, to be executed first */ 1484 /* queue check watchers, to be executed first */
1447 if (expect_false (checkcnt)) 1485 if (expect_false (checkcnt))
1448 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1486 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1449 1487
1485 head = &(*head)->next; 1523 head = &(*head)->next;
1486 } 1524 }
1487} 1525}
1488 1526
1489void inline_speed 1527void inline_speed
1490ev_clear_pending (EV_P_ W w) 1528clear_pending (EV_P_ W w)
1491{ 1529{
1492 if (w->pending) 1530 if (w->pending)
1493 { 1531 {
1494 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1532 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1495 w->pending = 0; 1533 w->pending = 0;
1496 } 1534 }
1497} 1535}
1498 1536
1537int
1538ev_clear_pending (EV_P_ void *w)
1539{
1540 W w_ = (W)w;
1541 int pending = w_->pending;
1542
1543 if (!pending)
1544 return 0;
1545
1546 w_->pending = 0;
1547 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
1548 p->w = 0;
1549
1550 return p->events;
1551}
1552
1553void inline_size
1554pri_adjust (EV_P_ W w)
1555{
1556 int pri = w->priority;
1557 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
1558 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
1559 w->priority = pri;
1560}
1561
1499void inline_speed 1562void inline_speed
1500ev_start (EV_P_ W w, int active) 1563ev_start (EV_P_ W w, int active)
1501{ 1564{
1502 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1565 pri_adjust (EV_A_ w);
1503 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1504
1505 w->active = active; 1566 w->active = active;
1506 ev_ref (EV_A); 1567 ev_ref (EV_A);
1507} 1568}
1508 1569
1509void inline_size 1570void inline_size
1533} 1594}
1534 1595
1535void 1596void
1536ev_io_stop (EV_P_ ev_io *w) 1597ev_io_stop (EV_P_ ev_io *w)
1537{ 1598{
1538 ev_clear_pending (EV_A_ (W)w); 1599 clear_pending (EV_A_ (W)w);
1539 if (expect_false (!ev_is_active (w))) 1600 if (expect_false (!ev_is_active (w)))
1540 return; 1601 return;
1541 1602
1542 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 1603 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1543 1604
1566} 1627}
1567 1628
1568void 1629void
1569ev_timer_stop (EV_P_ ev_timer *w) 1630ev_timer_stop (EV_P_ ev_timer *w)
1570{ 1631{
1571 ev_clear_pending (EV_A_ (W)w); 1632 clear_pending (EV_A_ (W)w);
1572 if (expect_false (!ev_is_active (w))) 1633 if (expect_false (!ev_is_active (w)))
1573 return; 1634 return;
1574 1635
1575 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1636 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1576 1637
1634} 1695}
1635 1696
1636void 1697void
1637ev_periodic_stop (EV_P_ ev_periodic *w) 1698ev_periodic_stop (EV_P_ ev_periodic *w)
1638{ 1699{
1639 ev_clear_pending (EV_A_ (W)w); 1700 clear_pending (EV_A_ (W)w);
1640 if (expect_false (!ev_is_active (w))) 1701 if (expect_false (!ev_is_active (w)))
1641 return; 1702 return;
1642 1703
1643 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1704 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1644 1705
1698} 1759}
1699 1760
1700void 1761void
1701ev_signal_stop (EV_P_ ev_signal *w) 1762ev_signal_stop (EV_P_ ev_signal *w)
1702{ 1763{
1703 ev_clear_pending (EV_A_ (W)w); 1764 clear_pending (EV_A_ (W)w);
1704 if (expect_false (!ev_is_active (w))) 1765 if (expect_false (!ev_is_active (w)))
1705 return; 1766 return;
1706 1767
1707 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1768 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1708 ev_stop (EV_A_ (W)w); 1769 ev_stop (EV_A_ (W)w);
1725} 1786}
1726 1787
1727void 1788void
1728ev_child_stop (EV_P_ ev_child *w) 1789ev_child_stop (EV_P_ ev_child *w)
1729{ 1790{
1730 ev_clear_pending (EV_A_ (W)w); 1791 clear_pending (EV_A_ (W)w);
1731 if (expect_false (!ev_is_active (w))) 1792 if (expect_false (!ev_is_active (w)))
1732 return; 1793 return;
1733 1794
1734 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 1795 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1735 ev_stop (EV_A_ (W)w); 1796 ev_stop (EV_A_ (W)w);
1971} 2032}
1972 2033
1973void 2034void
1974ev_stat_stop (EV_P_ ev_stat *w) 2035ev_stat_stop (EV_P_ ev_stat *w)
1975{ 2036{
1976 ev_clear_pending (EV_A_ (W)w); 2037 clear_pending (EV_A_ (W)w);
1977 if (expect_false (!ev_is_active (w))) 2038 if (expect_false (!ev_is_active (w)))
1978 return; 2039 return;
1979 2040
1980#if EV_USE_INOTIFY 2041#if EV_USE_INOTIFY
1981 infy_del (EV_A_ w); 2042 infy_del (EV_A_ w);
1984 2045
1985 ev_stop (EV_A_ (W)w); 2046 ev_stop (EV_A_ (W)w);
1986} 2047}
1987#endif 2048#endif
1988 2049
2050#if EV_IDLE_ENABLE
1989void 2051void
1990ev_idle_start (EV_P_ ev_idle *w) 2052ev_idle_start (EV_P_ ev_idle *w)
1991{ 2053{
1992 if (expect_false (ev_is_active (w))) 2054 if (expect_false (ev_is_active (w)))
1993 return; 2055 return;
1994 2056
2057 pri_adjust (EV_A_ (W)w);
2058
2059 {
2060 int active = ++idlecnt [ABSPRI (w)];
2061
2062 ++idleall;
1995 ev_start (EV_A_ (W)w, ++idlecnt); 2063 ev_start (EV_A_ (W)w, active);
2064
1996 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); 2065 array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2);
1997 idles [idlecnt - 1] = w; 2066 idles [ABSPRI (w)][active - 1] = w;
2067 }
1998} 2068}
1999 2069
2000void 2070void
2001ev_idle_stop (EV_P_ ev_idle *w) 2071ev_idle_stop (EV_P_ ev_idle *w)
2002{ 2072{
2003 ev_clear_pending (EV_A_ (W)w); 2073 clear_pending (EV_A_ (W)w);
2004 if (expect_false (!ev_is_active (w))) 2074 if (expect_false (!ev_is_active (w)))
2005 return; 2075 return;
2006 2076
2007 { 2077 {
2008 int active = ((W)w)->active; 2078 int active = ((W)w)->active;
2009 idles [active - 1] = idles [--idlecnt]; 2079
2080 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2010 ((W)idles [active - 1])->active = active; 2081 ((W)idles [ABSPRI (w)][active - 1])->active = active;
2082
2083 ev_stop (EV_A_ (W)w);
2084 --idleall;
2011 } 2085 }
2012
2013 ev_stop (EV_A_ (W)w);
2014} 2086}
2087#endif
2015 2088
2016void 2089void
2017ev_prepare_start (EV_P_ ev_prepare *w) 2090ev_prepare_start (EV_P_ ev_prepare *w)
2018{ 2091{
2019 if (expect_false (ev_is_active (w))) 2092 if (expect_false (ev_is_active (w)))
2025} 2098}
2026 2099
2027void 2100void
2028ev_prepare_stop (EV_P_ ev_prepare *w) 2101ev_prepare_stop (EV_P_ ev_prepare *w)
2029{ 2102{
2030 ev_clear_pending (EV_A_ (W)w); 2103 clear_pending (EV_A_ (W)w);
2031 if (expect_false (!ev_is_active (w))) 2104 if (expect_false (!ev_is_active (w)))
2032 return; 2105 return;
2033 2106
2034 { 2107 {
2035 int active = ((W)w)->active; 2108 int active = ((W)w)->active;
2052} 2125}
2053 2126
2054void 2127void
2055ev_check_stop (EV_P_ ev_check *w) 2128ev_check_stop (EV_P_ ev_check *w)
2056{ 2129{
2057 ev_clear_pending (EV_A_ (W)w); 2130 clear_pending (EV_A_ (W)w);
2058 if (expect_false (!ev_is_active (w))) 2131 if (expect_false (!ev_is_active (w)))
2059 return; 2132 return;
2060 2133
2061 { 2134 {
2062 int active = ((W)w)->active; 2135 int active = ((W)w)->active;
2104} 2177}
2105 2178
2106void 2179void
2107ev_embed_stop (EV_P_ ev_embed *w) 2180ev_embed_stop (EV_P_ ev_embed *w)
2108{ 2181{
2109 ev_clear_pending (EV_A_ (W)w); 2182 clear_pending (EV_A_ (W)w);
2110 if (expect_false (!ev_is_active (w))) 2183 if (expect_false (!ev_is_active (w)))
2111 return; 2184 return;
2112 2185
2113 ev_io_stop (EV_A_ &w->io); 2186 ev_io_stop (EV_A_ &w->io);
2114 2187
2129} 2202}
2130 2203
2131void 2204void
2132ev_fork_stop (EV_P_ ev_fork *w) 2205ev_fork_stop (EV_P_ ev_fork *w)
2133{ 2206{
2134 ev_clear_pending (EV_A_ (W)w); 2207 clear_pending (EV_A_ (W)w);
2135 if (expect_false (!ev_is_active (w))) 2208 if (expect_false (!ev_is_active (w)))
2136 return; 2209 return;
2137 2210
2138 { 2211 {
2139 int active = ((W)w)->active; 2212 int active = ((W)w)->active;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines