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

Comparing libev/ev.c (file contents):
Revision 1.159 by root, Sat Dec 1 19:48: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;
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
886ev_backend (EV_P) 909ev_backend (EV_P)
887{ 910{
888 return backend; 911 return backend;
889} 912}
890 913
914unsigned int
915ev_loop_count (EV_P)
916{
917 return loop_count;
918}
919
891static void noinline 920static void noinline
892loop_init (EV_P_ unsigned int flags) 921loop_init (EV_P_ unsigned int flags)
893{ 922{
894 if (!backend) 923 if (!backend)
895 { 924 {
975#if EV_USE_SELECT 1004#if EV_USE_SELECT
976 if (backend == EVBACKEND_SELECT) select_destroy (EV_A); 1005 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
977#endif 1006#endif
978 1007
979 for (i = NUMPRI; i--; ) 1008 for (i = NUMPRI; i--; )
1009 {
980 array_free (pending, [i]); 1010 array_free (pending, [i]);
1011#if EV_IDLE_ENABLE
1012 array_free (idle, [i]);
1013#endif
1014 }
981 1015
982 /* have to use the microsoft-never-gets-it-right macro */ 1016 /* have to use the microsoft-never-gets-it-right macro */
983 array_free (fdchange, EMPTY0); 1017 array_free (fdchange, EMPTY);
984 array_free (timer, EMPTY0); 1018 array_free (timer, EMPTY);
985#if EV_PERIODIC_ENABLE 1019#if EV_PERIODIC_ENABLE
986 array_free (periodic, EMPTY0); 1020 array_free (periodic, EMPTY);
987#endif 1021#endif
988 array_free (idle, EMPTY0);
989 array_free (prepare, EMPTY0); 1022 array_free (prepare, EMPTY);
990 array_free (check, EMPTY0); 1023 array_free (check, EMPTY);
991 1024
992 backend = 0; 1025 backend = 0;
993} 1026}
994 1027
995void inline_size infy_fork (EV_P); 1028void inline_size infy_fork (EV_P);
1131 postfork = 1; 1164 postfork = 1;
1132} 1165}
1133 1166
1134/*****************************************************************************/ 1167/*****************************************************************************/
1135 1168
1136int inline_size 1169void
1137any_pending (EV_P) 1170ev_invoke (EV_P_ void *w, int revents)
1138{ 1171{
1139 int pri; 1172 EV_CB_INVOKE ((W)w, revents);
1140
1141 for (pri = NUMPRI; pri--; )
1142 if (pendingcnt [pri])
1143 return 1;
1144
1145 return 0;
1146} 1173}
1147 1174
1148void inline_speed 1175void inline_speed
1149call_pending (EV_P) 1176call_pending (EV_P)
1150{ 1177{
1242 for (i = periodiccnt >> 1; i--; ) 1269 for (i = periodiccnt >> 1; i--; )
1243 downheap ((WT *)periodics, periodiccnt, i); 1270 downheap ((WT *)periodics, periodiccnt, i);
1244} 1271}
1245#endif 1272#endif
1246 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
1247int inline_size 1297int inline_size
1248time_update_monotonic (EV_P) 1298time_update_monotonic (EV_P)
1249{ 1299{
1250 mn_now = get_clock (); 1300 mn_now = get_clock ();
1251 1301
1342 ? EVUNLOOP_ONE 1392 ? EVUNLOOP_ONE
1343 : EVUNLOOP_CANCEL; 1393 : EVUNLOOP_CANCEL;
1344 1394
1345 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 1395 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1346 1396
1347 for (;;) 1397 do
1348 { 1398 {
1349#ifndef _WIN32 1399#ifndef _WIN32
1350 if (expect_false (curpid)) /* penalise the forking check even more */ 1400 if (expect_false (curpid)) /* penalise the forking check even more */
1351 if (expect_false (getpid () != curpid)) 1401 if (expect_false (getpid () != curpid))
1352 { 1402 {
1384 1434
1385 /* calculate blocking time */ 1435 /* calculate blocking time */
1386 { 1436 {
1387 ev_tstamp block; 1437 ev_tstamp block;
1388 1438
1389 if (flags & EVLOOP_NONBLOCK || idlecnt) 1439 if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt))
1390 block = 0.; /* do not block at all */ 1440 block = 0.; /* do not block at all */
1391 else 1441 else
1392 { 1442 {
1393 /* update time to cancel out callback processing overhead */ 1443 /* update time to cancel out callback processing overhead */
1394#if EV_USE_MONOTONIC 1444#if EV_USE_MONOTONIC
1418#endif 1468#endif
1419 1469
1420 if (expect_false (block < 0.)) block = 0.; 1470 if (expect_false (block < 0.)) block = 0.;
1421 } 1471 }
1422 1472
1473 ++loop_count;
1423 backend_poll (EV_A_ block); 1474 backend_poll (EV_A_ block);
1424 } 1475 }
1425 1476
1426 /* update ev_rt_now, do magic */ 1477 /* update ev_rt_now, do magic */
1427 time_update (EV_A); 1478 time_update (EV_A);
1430 timers_reify (EV_A); /* relative timers called last */ 1481 timers_reify (EV_A); /* relative timers called last */
1431#if EV_PERIODIC_ENABLE 1482#if EV_PERIODIC_ENABLE
1432 periodics_reify (EV_A); /* absolute timers called first */ 1483 periodics_reify (EV_A); /* absolute timers called first */
1433#endif 1484#endif
1434 1485
1486#if EV_IDLE_ENABLE
1435 /* queue idle watchers unless other events are pending */ 1487 /* queue idle watchers unless other events are pending */
1436 if (idlecnt && !any_pending (EV_A)) 1488 idle_reify (EV_A);
1437 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1489#endif
1438 1490
1439 /* queue check watchers, to be executed first */ 1491 /* queue check watchers, to be executed first */
1440 if (expect_false (checkcnt)) 1492 if (expect_false (checkcnt))
1441 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1493 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1442 1494
1443 call_pending (EV_A); 1495 call_pending (EV_A);
1444 1496
1445 if (expect_false (loop_done))
1446 break;
1447 } 1497 }
1498 while (expect_true (activecnt && !loop_done));
1448 1499
1449 if (loop_done == EVUNLOOP_ONE) 1500 if (loop_done == EVUNLOOP_ONE)
1450 loop_done = EVUNLOOP_CANCEL; 1501 loop_done = EVUNLOOP_CANCEL;
1451} 1502}
1452 1503
1479 head = &(*head)->next; 1530 head = &(*head)->next;
1480 } 1531 }
1481} 1532}
1482 1533
1483void inline_speed 1534void inline_speed
1484ev_clear_pending (EV_P_ W w) 1535clear_pending (EV_P_ W w)
1485{ 1536{
1486 if (w->pending) 1537 if (w->pending)
1487 { 1538 {
1488 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1539 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1489 w->pending = 0; 1540 w->pending = 0;
1490 } 1541 }
1491} 1542}
1492 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
1493void inline_speed 1569void inline_speed
1494ev_start (EV_P_ W w, int active) 1570ev_start (EV_P_ W w, int active)
1495{ 1571{
1496 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1572 pri_adjust (EV_A_ w);
1497 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1498
1499 w->active = active; 1573 w->active = active;
1500 ev_ref (EV_A); 1574 ev_ref (EV_A);
1501} 1575}
1502 1576
1503void inline_size 1577void inline_size
1527} 1601}
1528 1602
1529void 1603void
1530ev_io_stop (EV_P_ ev_io *w) 1604ev_io_stop (EV_P_ ev_io *w)
1531{ 1605{
1532 ev_clear_pending (EV_A_ (W)w); 1606 clear_pending (EV_A_ (W)w);
1533 if (expect_false (!ev_is_active (w))) 1607 if (expect_false (!ev_is_active (w)))
1534 return; 1608 return;
1535 1609
1536 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));
1537 1611
1560} 1634}
1561 1635
1562void 1636void
1563ev_timer_stop (EV_P_ ev_timer *w) 1637ev_timer_stop (EV_P_ ev_timer *w)
1564{ 1638{
1565 ev_clear_pending (EV_A_ (W)w); 1639 clear_pending (EV_A_ (W)w);
1566 if (expect_false (!ev_is_active (w))) 1640 if (expect_false (!ev_is_active (w)))
1567 return; 1641 return;
1568 1642
1569 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1643 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1570 1644
1628} 1702}
1629 1703
1630void 1704void
1631ev_periodic_stop (EV_P_ ev_periodic *w) 1705ev_periodic_stop (EV_P_ ev_periodic *w)
1632{ 1706{
1633 ev_clear_pending (EV_A_ (W)w); 1707 clear_pending (EV_A_ (W)w);
1634 if (expect_false (!ev_is_active (w))) 1708 if (expect_false (!ev_is_active (w)))
1635 return; 1709 return;
1636 1710
1637 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1711 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1638 1712
1692} 1766}
1693 1767
1694void 1768void
1695ev_signal_stop (EV_P_ ev_signal *w) 1769ev_signal_stop (EV_P_ ev_signal *w)
1696{ 1770{
1697 ev_clear_pending (EV_A_ (W)w); 1771 clear_pending (EV_A_ (W)w);
1698 if (expect_false (!ev_is_active (w))) 1772 if (expect_false (!ev_is_active (w)))
1699 return; 1773 return;
1700 1774
1701 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1775 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1702 ev_stop (EV_A_ (W)w); 1776 ev_stop (EV_A_ (W)w);
1719} 1793}
1720 1794
1721void 1795void
1722ev_child_stop (EV_P_ ev_child *w) 1796ev_child_stop (EV_P_ ev_child *w)
1723{ 1797{
1724 ev_clear_pending (EV_A_ (W)w); 1798 clear_pending (EV_A_ (W)w);
1725 if (expect_false (!ev_is_active (w))) 1799 if (expect_false (!ev_is_active (w)))
1726 return; 1800 return;
1727 1801
1728 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);
1729 ev_stop (EV_A_ (W)w); 1803 ev_stop (EV_A_ (W)w);
1965} 2039}
1966 2040
1967void 2041void
1968ev_stat_stop (EV_P_ ev_stat *w) 2042ev_stat_stop (EV_P_ ev_stat *w)
1969{ 2043{
1970 ev_clear_pending (EV_A_ (W)w); 2044 clear_pending (EV_A_ (W)w);
1971 if (expect_false (!ev_is_active (w))) 2045 if (expect_false (!ev_is_active (w)))
1972 return; 2046 return;
1973 2047
1974#if EV_USE_INOTIFY 2048#if EV_USE_INOTIFY
1975 infy_del (EV_A_ w); 2049 infy_del (EV_A_ w);
1978 2052
1979 ev_stop (EV_A_ (W)w); 2053 ev_stop (EV_A_ (W)w);
1980} 2054}
1981#endif 2055#endif
1982 2056
2057#if EV_IDLE_ENABLE
1983void 2058void
1984ev_idle_start (EV_P_ ev_idle *w) 2059ev_idle_start (EV_P_ ev_idle *w)
1985{ 2060{
1986 if (expect_false (ev_is_active (w))) 2061 if (expect_false (ev_is_active (w)))
1987 return; 2062 return;
1988 2063
2064 pri_adjust (EV_A_ (W)w);
2065
2066 {
2067 int active = ++idlecnt [ABSPRI (w)];
2068
2069 ++idleall;
1989 ev_start (EV_A_ (W)w, ++idlecnt); 2070 ev_start (EV_A_ (W)w, active);
2071
1990 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); 2072 array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2);
1991 idles [idlecnt - 1] = w; 2073 idles [ABSPRI (w)][active - 1] = w;
2074 }
1992} 2075}
1993 2076
1994void 2077void
1995ev_idle_stop (EV_P_ ev_idle *w) 2078ev_idle_stop (EV_P_ ev_idle *w)
1996{ 2079{
1997 ev_clear_pending (EV_A_ (W)w); 2080 clear_pending (EV_A_ (W)w);
1998 if (expect_false (!ev_is_active (w))) 2081 if (expect_false (!ev_is_active (w)))
1999 return; 2082 return;
2000 2083
2001 { 2084 {
2002 int active = ((W)w)->active; 2085 int active = ((W)w)->active;
2003 idles [active - 1] = idles [--idlecnt]; 2086
2087 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2004 ((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;
2005 } 2092 }
2006
2007 ev_stop (EV_A_ (W)w);
2008} 2093}
2094#endif
2009 2095
2010void 2096void
2011ev_prepare_start (EV_P_ ev_prepare *w) 2097ev_prepare_start (EV_P_ ev_prepare *w)
2012{ 2098{
2013 if (expect_false (ev_is_active (w))) 2099 if (expect_false (ev_is_active (w)))
2019} 2105}
2020 2106
2021void 2107void
2022ev_prepare_stop (EV_P_ ev_prepare *w) 2108ev_prepare_stop (EV_P_ ev_prepare *w)
2023{ 2109{
2024 ev_clear_pending (EV_A_ (W)w); 2110 clear_pending (EV_A_ (W)w);
2025 if (expect_false (!ev_is_active (w))) 2111 if (expect_false (!ev_is_active (w)))
2026 return; 2112 return;
2027 2113
2028 { 2114 {
2029 int active = ((W)w)->active; 2115 int active = ((W)w)->active;
2046} 2132}
2047 2133
2048void 2134void
2049ev_check_stop (EV_P_ ev_check *w) 2135ev_check_stop (EV_P_ ev_check *w)
2050{ 2136{
2051 ev_clear_pending (EV_A_ (W)w); 2137 clear_pending (EV_A_ (W)w);
2052 if (expect_false (!ev_is_active (w))) 2138 if (expect_false (!ev_is_active (w)))
2053 return; 2139 return;
2054 2140
2055 { 2141 {
2056 int active = ((W)w)->active; 2142 int active = ((W)w)->active;
2098} 2184}
2099 2185
2100void 2186void
2101ev_embed_stop (EV_P_ ev_embed *w) 2187ev_embed_stop (EV_P_ ev_embed *w)
2102{ 2188{
2103 ev_clear_pending (EV_A_ (W)w); 2189 clear_pending (EV_A_ (W)w);
2104 if (expect_false (!ev_is_active (w))) 2190 if (expect_false (!ev_is_active (w)))
2105 return; 2191 return;
2106 2192
2107 ev_io_stop (EV_A_ &w->io); 2193 ev_io_stop (EV_A_ &w->io);
2108 2194
2123} 2209}
2124 2210
2125void 2211void
2126ev_fork_stop (EV_P_ ev_fork *w) 2212ev_fork_stop (EV_P_ ev_fork *w)
2127{ 2213{
2128 ev_clear_pending (EV_A_ (W)w); 2214 clear_pending (EV_A_ (W)w);
2129 if (expect_false (!ev_is_active (w))) 2215 if (expect_false (!ev_is_active (w)))
2130 return; 2216 return;
2131 2217
2132 { 2218 {
2133 int active = ((W)w)->active; 2219 int active = ((W)w)->active;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines