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.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;
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/*****************************************************************************/
485} 507}
486 508
487void 509void
488ev_feed_fd_event (EV_P_ int fd, int revents) 510ev_feed_fd_event (EV_P_ int fd, int revents)
489{ 511{
512 if (fd >= 0 && fd < anfdmax)
490 fd_event (EV_A_ fd, revents); 513 fd_event (EV_A_ fd, revents);
491} 514}
492 515
493void inline_size 516void inline_size
494fd_reify (EV_P) 517fd_reify (EV_P)
495{ 518{
764 ev_child *w; 787 ev_child *w;
765 788
766 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)
767 if (w->pid == pid || !w->pid) 790 if (w->pid == pid || !w->pid)
768 { 791 {
769 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 792 ev_set_priority (w, ev_priority (sw)); /* need to do it *now* */
770 w->rpid = pid; 793 w->rpid = pid;
771 w->rstatus = status; 794 w->rstatus = status;
772 ev_feed_event (EV_A_ (W)w, EV_CHILD); 795 ev_feed_event (EV_A_ (W)w, EV_CHILD);
773 } 796 }
774} 797}
775 798
776#ifndef WCONTINUED 799#ifndef WCONTINUED
981#if EV_USE_SELECT 1004#if EV_USE_SELECT
982 if (backend == EVBACKEND_SELECT) select_destroy (EV_A); 1005 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
983#endif 1006#endif
984 1007
985 for (i = NUMPRI; i--; ) 1008 for (i = NUMPRI; i--; )
1009 {
986 array_free (pending, [i]); 1010 array_free (pending, [i]);
1011#if EV_IDLE_ENABLE
1012 array_free (idle, [i]);
1013#endif
1014 }
987 1015
988 /* have to use the microsoft-never-gets-it-right macro */ 1016 /* have to use the microsoft-never-gets-it-right macro */
989 array_free (fdchange, EMPTY0); 1017 array_free (fdchange, EMPTY);
990 array_free (timer, EMPTY0); 1018 array_free (timer, EMPTY);
991#if EV_PERIODIC_ENABLE 1019#if EV_PERIODIC_ENABLE
992 array_free (periodic, EMPTY0); 1020 array_free (periodic, EMPTY);
993#endif 1021#endif
994 array_free (idle, EMPTY0);
995 array_free (prepare, EMPTY0); 1022 array_free (prepare, EMPTY);
996 array_free (check, EMPTY0); 1023 array_free (check, EMPTY);
997 1024
998 backend = 0; 1025 backend = 0;
999} 1026}
1000 1027
1001void inline_size infy_fork (EV_P); 1028void inline_size infy_fork (EV_P);
1137 postfork = 1; 1164 postfork = 1;
1138} 1165}
1139 1166
1140/*****************************************************************************/ 1167/*****************************************************************************/
1141 1168
1142int inline_size 1169void
1143any_pending (EV_P) 1170ev_invoke (EV_P_ void *w, int revents)
1144{ 1171{
1145 int pri; 1172 EV_CB_INVOKE ((W)w, revents);
1146
1147 for (pri = NUMPRI; pri--; )
1148 if (pendingcnt [pri])
1149 return 1;
1150
1151 return 0;
1152} 1173}
1153 1174
1154void inline_speed 1175void inline_speed
1155call_pending (EV_P) 1176call_pending (EV_P)
1156{ 1177{
1248 for (i = periodiccnt >> 1; i--; ) 1269 for (i = periodiccnt >> 1; i--; )
1249 downheap ((WT *)periodics, periodiccnt, i); 1270 downheap ((WT *)periodics, periodiccnt, i);
1250} 1271}
1251#endif 1272#endif
1252 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
1253int inline_size 1297int inline_size
1254time_update_monotonic (EV_P) 1298time_update_monotonic (EV_P)
1255{ 1299{
1256 mn_now = get_clock (); 1300 mn_now = get_clock ();
1257 1301
1390 1434
1391 /* calculate blocking time */ 1435 /* calculate blocking time */
1392 { 1436 {
1393 ev_tstamp block; 1437 ev_tstamp block;
1394 1438
1395 if (expect_false (flags & EVLOOP_NONBLOCK || idlecnt || !activecnt)) 1439 if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt))
1396 block = 0.; /* do not block at all */ 1440 block = 0.; /* do not block at all */
1397 else 1441 else
1398 { 1442 {
1399 /* update time to cancel out callback processing overhead */ 1443 /* update time to cancel out callback processing overhead */
1400#if EV_USE_MONOTONIC 1444#if EV_USE_MONOTONIC
1437 timers_reify (EV_A); /* relative timers called last */ 1481 timers_reify (EV_A); /* relative timers called last */
1438#if EV_PERIODIC_ENABLE 1482#if EV_PERIODIC_ENABLE
1439 periodics_reify (EV_A); /* absolute timers called first */ 1483 periodics_reify (EV_A); /* absolute timers called first */
1440#endif 1484#endif
1441 1485
1486#if EV_IDLE_ENABLE
1442 /* queue idle watchers unless other events are pending */ 1487 /* queue idle watchers unless other events are pending */
1443 if (idlecnt && !any_pending (EV_A)) 1488 idle_reify (EV_A);
1444 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1489#endif
1445 1490
1446 /* queue check watchers, to be executed first */ 1491 /* queue check watchers, to be executed first */
1447 if (expect_false (checkcnt)) 1492 if (expect_false (checkcnt))
1448 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1493 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1449 1494
1485 head = &(*head)->next; 1530 head = &(*head)->next;
1486 } 1531 }
1487} 1532}
1488 1533
1489void inline_speed 1534void inline_speed
1490ev_clear_pending (EV_P_ W w) 1535clear_pending (EV_P_ W w)
1491{ 1536{
1492 if (w->pending) 1537 if (w->pending)
1493 { 1538 {
1494 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1539 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1495 w->pending = 0; 1540 w->pending = 0;
1496 } 1541 }
1497} 1542}
1498 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
1499void inline_speed 1569void inline_speed
1500ev_start (EV_P_ W w, int active) 1570ev_start (EV_P_ W w, int active)
1501{ 1571{
1502 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1572 pri_adjust (EV_A_ w);
1503 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1504
1505 w->active = active; 1573 w->active = active;
1506 ev_ref (EV_A); 1574 ev_ref (EV_A);
1507} 1575}
1508 1576
1509void inline_size 1577void inline_size
1533} 1601}
1534 1602
1535void 1603void
1536ev_io_stop (EV_P_ ev_io *w) 1604ev_io_stop (EV_P_ ev_io *w)
1537{ 1605{
1538 ev_clear_pending (EV_A_ (W)w); 1606 clear_pending (EV_A_ (W)w);
1539 if (expect_false (!ev_is_active (w))) 1607 if (expect_false (!ev_is_active (w)))
1540 return; 1608 return;
1541 1609
1542 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));
1543 1611
1566} 1634}
1567 1635
1568void 1636void
1569ev_timer_stop (EV_P_ ev_timer *w) 1637ev_timer_stop (EV_P_ ev_timer *w)
1570{ 1638{
1571 ev_clear_pending (EV_A_ (W)w); 1639 clear_pending (EV_A_ (W)w);
1572 if (expect_false (!ev_is_active (w))) 1640 if (expect_false (!ev_is_active (w)))
1573 return; 1641 return;
1574 1642
1575 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1643 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1576 1644
1634} 1702}
1635 1703
1636void 1704void
1637ev_periodic_stop (EV_P_ ev_periodic *w) 1705ev_periodic_stop (EV_P_ ev_periodic *w)
1638{ 1706{
1639 ev_clear_pending (EV_A_ (W)w); 1707 clear_pending (EV_A_ (W)w);
1640 if (expect_false (!ev_is_active (w))) 1708 if (expect_false (!ev_is_active (w)))
1641 return; 1709 return;
1642 1710
1643 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1711 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1644 1712
1698} 1766}
1699 1767
1700void 1768void
1701ev_signal_stop (EV_P_ ev_signal *w) 1769ev_signal_stop (EV_P_ ev_signal *w)
1702{ 1770{
1703 ev_clear_pending (EV_A_ (W)w); 1771 clear_pending (EV_A_ (W)w);
1704 if (expect_false (!ev_is_active (w))) 1772 if (expect_false (!ev_is_active (w)))
1705 return; 1773 return;
1706 1774
1707 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1775 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1708 ev_stop (EV_A_ (W)w); 1776 ev_stop (EV_A_ (W)w);
1725} 1793}
1726 1794
1727void 1795void
1728ev_child_stop (EV_P_ ev_child *w) 1796ev_child_stop (EV_P_ ev_child *w)
1729{ 1797{
1730 ev_clear_pending (EV_A_ (W)w); 1798 clear_pending (EV_A_ (W)w);
1731 if (expect_false (!ev_is_active (w))) 1799 if (expect_false (!ev_is_active (w)))
1732 return; 1800 return;
1733 1801
1734 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);
1735 ev_stop (EV_A_ (W)w); 1803 ev_stop (EV_A_ (W)w);
1971} 2039}
1972 2040
1973void 2041void
1974ev_stat_stop (EV_P_ ev_stat *w) 2042ev_stat_stop (EV_P_ ev_stat *w)
1975{ 2043{
1976 ev_clear_pending (EV_A_ (W)w); 2044 clear_pending (EV_A_ (W)w);
1977 if (expect_false (!ev_is_active (w))) 2045 if (expect_false (!ev_is_active (w)))
1978 return; 2046 return;
1979 2047
1980#if EV_USE_INOTIFY 2048#if EV_USE_INOTIFY
1981 infy_del (EV_A_ w); 2049 infy_del (EV_A_ w);
1984 2052
1985 ev_stop (EV_A_ (W)w); 2053 ev_stop (EV_A_ (W)w);
1986} 2054}
1987#endif 2055#endif
1988 2056
2057#if EV_IDLE_ENABLE
1989void 2058void
1990ev_idle_start (EV_P_ ev_idle *w) 2059ev_idle_start (EV_P_ ev_idle *w)
1991{ 2060{
1992 if (expect_false (ev_is_active (w))) 2061 if (expect_false (ev_is_active (w)))
1993 return; 2062 return;
1994 2063
2064 pri_adjust (EV_A_ (W)w);
2065
2066 {
2067 int active = ++idlecnt [ABSPRI (w)];
2068
2069 ++idleall;
1995 ev_start (EV_A_ (W)w, ++idlecnt); 2070 ev_start (EV_A_ (W)w, active);
2071
1996 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); 2072 array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2);
1997 idles [idlecnt - 1] = w; 2073 idles [ABSPRI (w)][active - 1] = w;
2074 }
1998} 2075}
1999 2076
2000void 2077void
2001ev_idle_stop (EV_P_ ev_idle *w) 2078ev_idle_stop (EV_P_ ev_idle *w)
2002{ 2079{
2003 ev_clear_pending (EV_A_ (W)w); 2080 clear_pending (EV_A_ (W)w);
2004 if (expect_false (!ev_is_active (w))) 2081 if (expect_false (!ev_is_active (w)))
2005 return; 2082 return;
2006 2083
2007 { 2084 {
2008 int active = ((W)w)->active; 2085 int active = ((W)w)->active;
2009 idles [active - 1] = idles [--idlecnt]; 2086
2087 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2010 ((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;
2011 } 2092 }
2012
2013 ev_stop (EV_A_ (W)w);
2014} 2093}
2094#endif
2015 2095
2016void 2096void
2017ev_prepare_start (EV_P_ ev_prepare *w) 2097ev_prepare_start (EV_P_ ev_prepare *w)
2018{ 2098{
2019 if (expect_false (ev_is_active (w))) 2099 if (expect_false (ev_is_active (w)))
2025} 2105}
2026 2106
2027void 2107void
2028ev_prepare_stop (EV_P_ ev_prepare *w) 2108ev_prepare_stop (EV_P_ ev_prepare *w)
2029{ 2109{
2030 ev_clear_pending (EV_A_ (W)w); 2110 clear_pending (EV_A_ (W)w);
2031 if (expect_false (!ev_is_active (w))) 2111 if (expect_false (!ev_is_active (w)))
2032 return; 2112 return;
2033 2113
2034 { 2114 {
2035 int active = ((W)w)->active; 2115 int active = ((W)w)->active;
2052} 2132}
2053 2133
2054void 2134void
2055ev_check_stop (EV_P_ ev_check *w) 2135ev_check_stop (EV_P_ ev_check *w)
2056{ 2136{
2057 ev_clear_pending (EV_A_ (W)w); 2137 clear_pending (EV_A_ (W)w);
2058 if (expect_false (!ev_is_active (w))) 2138 if (expect_false (!ev_is_active (w)))
2059 return; 2139 return;
2060 2140
2061 { 2141 {
2062 int active = ((W)w)->active; 2142 int active = ((W)w)->active;
2104} 2184}
2105 2185
2106void 2186void
2107ev_embed_stop (EV_P_ ev_embed *w) 2187ev_embed_stop (EV_P_ ev_embed *w)
2108{ 2188{
2109 ev_clear_pending (EV_A_ (W)w); 2189 clear_pending (EV_A_ (W)w);
2110 if (expect_false (!ev_is_active (w))) 2190 if (expect_false (!ev_is_active (w)))
2111 return; 2191 return;
2112 2192
2113 ev_io_stop (EV_A_ &w->io); 2193 ev_io_stop (EV_A_ &w->io);
2114 2194
2129} 2209}
2130 2210
2131void 2211void
2132ev_fork_stop (EV_P_ ev_fork *w) 2212ev_fork_stop (EV_P_ ev_fork *w)
2133{ 2213{
2134 ev_clear_pending (EV_A_ (W)w); 2214 clear_pending (EV_A_ (W)w);
2135 if (expect_false (!ev_is_active (w))) 2215 if (expect_false (!ev_is_active (w)))
2136 return; 2216 return;
2137 2217
2138 { 2218 {
2139 int active = ((W)w)->active; 2219 int active = ((W)w)->active;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines