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

Comparing libev/ev.c (file contents):
Revision 1.158 by root, Thu Nov 29 17:28:13 2007 UTC vs.
Revision 1.166 by root, Sat Dec 8 03:53:36 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
886ev_backend (EV_P) 908ev_backend (EV_P)
887{ 909{
888 return backend; 910 return backend;
889} 911}
890 912
913unsigned int
914ev_loop_count (EV_P)
915{
916 return loop_count;
917}
918
891static void noinline 919static void noinline
892loop_init (EV_P_ unsigned int flags) 920loop_init (EV_P_ unsigned int flags)
893{ 921{
894 if (!backend) 922 if (!backend)
895 { 923 {
975#if EV_USE_SELECT 1003#if EV_USE_SELECT
976 if (backend == EVBACKEND_SELECT) select_destroy (EV_A); 1004 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
977#endif 1005#endif
978 1006
979 for (i = NUMPRI; i--; ) 1007 for (i = NUMPRI; i--; )
1008 {
980 array_free (pending, [i]); 1009 array_free (pending, [i]);
1010#if EV_IDLE_ENABLE
1011 array_free (idle, [i]);
1012#endif
1013 }
981 1014
982 /* have to use the microsoft-never-gets-it-right macro */ 1015 /* have to use the microsoft-never-gets-it-right macro */
983 array_free (fdchange, EMPTY0); 1016 array_free (fdchange, EMPTY);
984 array_free (timer, EMPTY0); 1017 array_free (timer, EMPTY);
985#if EV_PERIODIC_ENABLE 1018#if EV_PERIODIC_ENABLE
986 array_free (periodic, EMPTY0); 1019 array_free (periodic, EMPTY);
987#endif 1020#endif
988 array_free (idle, EMPTY0);
989 array_free (prepare, EMPTY0); 1021 array_free (prepare, EMPTY);
990 array_free (check, EMPTY0); 1022 array_free (check, EMPTY);
991 1023
992 backend = 0; 1024 backend = 0;
993} 1025}
994 1026
995void inline_size infy_fork (EV_P); 1027void inline_size infy_fork (EV_P);
1130 if (backend) 1162 if (backend)
1131 postfork = 1; 1163 postfork = 1;
1132} 1164}
1133 1165
1134/*****************************************************************************/ 1166/*****************************************************************************/
1135
1136int inline_size
1137any_pending (EV_P)
1138{
1139 int pri;
1140
1141 for (pri = NUMPRI; pri--; )
1142 if (pendingcnt [pri])
1143 return 1;
1144
1145 return 0;
1146}
1147 1167
1148void inline_speed 1168void inline_speed
1149call_pending (EV_P) 1169call_pending (EV_P)
1150{ 1170{
1151 int pri; 1171 int pri;
1242 for (i = periodiccnt >> 1; i--; ) 1262 for (i = periodiccnt >> 1; i--; )
1243 downheap ((WT *)periodics, periodiccnt, i); 1263 downheap ((WT *)periodics, periodiccnt, i);
1244} 1264}
1245#endif 1265#endif
1246 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
1247int inline_size 1290int inline_size
1248time_update_monotonic (EV_P) 1291time_update_monotonic (EV_P)
1249{ 1292{
1250 mn_now = get_clock (); 1293 mn_now = get_clock ();
1251 1294
1342 ? EVUNLOOP_ONE 1385 ? EVUNLOOP_ONE
1343 : EVUNLOOP_CANCEL; 1386 : EVUNLOOP_CANCEL;
1344 1387
1345 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 1388 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1346 1389
1347 while (activecnt) 1390 do
1348 { 1391 {
1349#ifndef _WIN32 1392#ifndef _WIN32
1350 if (expect_false (curpid)) /* penalise the forking check even more */ 1393 if (expect_false (curpid)) /* penalise the forking check even more */
1351 if (expect_false (getpid () != curpid)) 1394 if (expect_false (getpid () != curpid))
1352 { 1395 {
1370 { 1413 {
1371 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1414 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1372 call_pending (EV_A); 1415 call_pending (EV_A);
1373 } 1416 }
1374 1417
1418 if (expect_false (!activecnt))
1419 break;
1420
1375 /* we might have forked, so reify kernel state if necessary */ 1421 /* we might have forked, so reify kernel state if necessary */
1376 if (expect_false (postfork)) 1422 if (expect_false (postfork))
1377 loop_fork (EV_A); 1423 loop_fork (EV_A);
1378 1424
1379 /* update fd-related kernel structures */ 1425 /* update fd-related kernel structures */
1381 1427
1382 /* calculate blocking time */ 1428 /* calculate blocking time */
1383 { 1429 {
1384 ev_tstamp block; 1430 ev_tstamp block;
1385 1431
1386 if (flags & EVLOOP_NONBLOCK || idlecnt) 1432 if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt))
1387 block = 0.; /* do not block at all */ 1433 block = 0.; /* do not block at all */
1388 else 1434 else
1389 { 1435 {
1390 /* update time to cancel out callback processing overhead */ 1436 /* update time to cancel out callback processing overhead */
1391#if EV_USE_MONOTONIC 1437#if EV_USE_MONOTONIC
1415#endif 1461#endif
1416 1462
1417 if (expect_false (block < 0.)) block = 0.; 1463 if (expect_false (block < 0.)) block = 0.;
1418 } 1464 }
1419 1465
1466 ++loop_count;
1420 backend_poll (EV_A_ block); 1467 backend_poll (EV_A_ block);
1421 } 1468 }
1422 1469
1423 /* update ev_rt_now, do magic */ 1470 /* update ev_rt_now, do magic */
1424 time_update (EV_A); 1471 time_update (EV_A);
1427 timers_reify (EV_A); /* relative timers called last */ 1474 timers_reify (EV_A); /* relative timers called last */
1428#if EV_PERIODIC_ENABLE 1475#if EV_PERIODIC_ENABLE
1429 periodics_reify (EV_A); /* absolute timers called first */ 1476 periodics_reify (EV_A); /* absolute timers called first */
1430#endif 1477#endif
1431 1478
1479#if EV_IDLE_ENABLE
1432 /* queue idle watchers unless other events are pending */ 1480 /* queue idle watchers unless other events are pending */
1433 if (idlecnt && !any_pending (EV_A)) 1481 idle_reify (EV_A);
1434 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1482#endif
1435 1483
1436 /* queue check watchers, to be executed first */ 1484 /* queue check watchers, to be executed first */
1437 if (expect_false (checkcnt)) 1485 if (expect_false (checkcnt))
1438 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1486 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1439 1487
1440 call_pending (EV_A); 1488 call_pending (EV_A);
1441 1489
1442 if (expect_false (loop_done))
1443 break;
1444 } 1490 }
1491 while (expect_true (activecnt && !loop_done));
1445 1492
1446 if (loop_done == EVUNLOOP_ONE) 1493 if (loop_done == EVUNLOOP_ONE)
1447 loop_done = EVUNLOOP_CANCEL; 1494 loop_done = EVUNLOOP_CANCEL;
1448} 1495}
1449 1496
1476 head = &(*head)->next; 1523 head = &(*head)->next;
1477 } 1524 }
1478} 1525}
1479 1526
1480void inline_speed 1527void inline_speed
1481ev_clear_pending (EV_P_ W w) 1528clear_pending (EV_P_ W w)
1482{ 1529{
1483 if (w->pending) 1530 if (w->pending)
1484 { 1531 {
1485 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1532 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1486 w->pending = 0; 1533 w->pending = 0;
1487 } 1534 }
1488} 1535}
1489 1536
1537void
1538ev_clear_pending (EV_P_ void *w, int invoke)
1539{
1540 W w_ = (W)w;
1541 int pending = w_->pending;
1542
1543 if (pending)
1544 {
1545 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
1546
1547 w_->pending = 0;
1548 p->w = 0;
1549
1550 if (invoke)
1551 EV_CB_INVOKE (w_, p->events);
1552 }
1553}
1554
1555void inline_size
1556pri_adjust (EV_P_ W w)
1557{
1558 int pri = w->priority;
1559 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
1560 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
1561 w->priority = pri;
1562}
1563
1490void inline_speed 1564void inline_speed
1491ev_start (EV_P_ W w, int active) 1565ev_start (EV_P_ W w, int active)
1492{ 1566{
1493 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1567 pri_adjust (EV_A_ w);
1494 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1495
1496 w->active = active; 1568 w->active = active;
1497 ev_ref (EV_A); 1569 ev_ref (EV_A);
1498} 1570}
1499 1571
1500void inline_size 1572void inline_size
1524} 1596}
1525 1597
1526void 1598void
1527ev_io_stop (EV_P_ ev_io *w) 1599ev_io_stop (EV_P_ ev_io *w)
1528{ 1600{
1529 ev_clear_pending (EV_A_ (W)w); 1601 clear_pending (EV_A_ (W)w);
1530 if (expect_false (!ev_is_active (w))) 1602 if (expect_false (!ev_is_active (w)))
1531 return; 1603 return;
1532 1604
1533 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 1605 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1534 1606
1557} 1629}
1558 1630
1559void 1631void
1560ev_timer_stop (EV_P_ ev_timer *w) 1632ev_timer_stop (EV_P_ ev_timer *w)
1561{ 1633{
1562 ev_clear_pending (EV_A_ (W)w); 1634 clear_pending (EV_A_ (W)w);
1563 if (expect_false (!ev_is_active (w))) 1635 if (expect_false (!ev_is_active (w)))
1564 return; 1636 return;
1565 1637
1566 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1638 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1567 1639
1625} 1697}
1626 1698
1627void 1699void
1628ev_periodic_stop (EV_P_ ev_periodic *w) 1700ev_periodic_stop (EV_P_ ev_periodic *w)
1629{ 1701{
1630 ev_clear_pending (EV_A_ (W)w); 1702 clear_pending (EV_A_ (W)w);
1631 if (expect_false (!ev_is_active (w))) 1703 if (expect_false (!ev_is_active (w)))
1632 return; 1704 return;
1633 1705
1634 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1706 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1635 1707
1689} 1761}
1690 1762
1691void 1763void
1692ev_signal_stop (EV_P_ ev_signal *w) 1764ev_signal_stop (EV_P_ ev_signal *w)
1693{ 1765{
1694 ev_clear_pending (EV_A_ (W)w); 1766 clear_pending (EV_A_ (W)w);
1695 if (expect_false (!ev_is_active (w))) 1767 if (expect_false (!ev_is_active (w)))
1696 return; 1768 return;
1697 1769
1698 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1770 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1699 ev_stop (EV_A_ (W)w); 1771 ev_stop (EV_A_ (W)w);
1716} 1788}
1717 1789
1718void 1790void
1719ev_child_stop (EV_P_ ev_child *w) 1791ev_child_stop (EV_P_ ev_child *w)
1720{ 1792{
1721 ev_clear_pending (EV_A_ (W)w); 1793 clear_pending (EV_A_ (W)w);
1722 if (expect_false (!ev_is_active (w))) 1794 if (expect_false (!ev_is_active (w)))
1723 return; 1795 return;
1724 1796
1725 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 1797 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1726 ev_stop (EV_A_ (W)w); 1798 ev_stop (EV_A_ (W)w);
1962} 2034}
1963 2035
1964void 2036void
1965ev_stat_stop (EV_P_ ev_stat *w) 2037ev_stat_stop (EV_P_ ev_stat *w)
1966{ 2038{
1967 ev_clear_pending (EV_A_ (W)w); 2039 clear_pending (EV_A_ (W)w);
1968 if (expect_false (!ev_is_active (w))) 2040 if (expect_false (!ev_is_active (w)))
1969 return; 2041 return;
1970 2042
1971#if EV_USE_INOTIFY 2043#if EV_USE_INOTIFY
1972 infy_del (EV_A_ w); 2044 infy_del (EV_A_ w);
1975 2047
1976 ev_stop (EV_A_ (W)w); 2048 ev_stop (EV_A_ (W)w);
1977} 2049}
1978#endif 2050#endif
1979 2051
2052#if EV_IDLE_ENABLE
1980void 2053void
1981ev_idle_start (EV_P_ ev_idle *w) 2054ev_idle_start (EV_P_ ev_idle *w)
1982{ 2055{
1983 if (expect_false (ev_is_active (w))) 2056 if (expect_false (ev_is_active (w)))
1984 return; 2057 return;
1985 2058
2059 pri_adjust (EV_A_ (W)w);
2060
2061 {
2062 int active = ++idlecnt [ABSPRI (w)];
2063
2064 ++idleall;
1986 ev_start (EV_A_ (W)w, ++idlecnt); 2065 ev_start (EV_A_ (W)w, active);
2066
1987 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); 2067 array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2);
1988 idles [idlecnt - 1] = w; 2068 idles [ABSPRI (w)][active - 1] = w;
2069 }
1989} 2070}
1990 2071
1991void 2072void
1992ev_idle_stop (EV_P_ ev_idle *w) 2073ev_idle_stop (EV_P_ ev_idle *w)
1993{ 2074{
1994 ev_clear_pending (EV_A_ (W)w); 2075 clear_pending (EV_A_ (W)w);
1995 if (expect_false (!ev_is_active (w))) 2076 if (expect_false (!ev_is_active (w)))
1996 return; 2077 return;
1997 2078
1998 { 2079 {
1999 int active = ((W)w)->active; 2080 int active = ((W)w)->active;
2000 idles [active - 1] = idles [--idlecnt]; 2081
2082 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2001 ((W)idles [active - 1])->active = active; 2083 ((W)idles [ABSPRI (w)][active - 1])->active = active;
2084
2085 ev_stop (EV_A_ (W)w);
2086 --idleall;
2002 } 2087 }
2003
2004 ev_stop (EV_A_ (W)w);
2005} 2088}
2089#endif
2006 2090
2007void 2091void
2008ev_prepare_start (EV_P_ ev_prepare *w) 2092ev_prepare_start (EV_P_ ev_prepare *w)
2009{ 2093{
2010 if (expect_false (ev_is_active (w))) 2094 if (expect_false (ev_is_active (w)))
2016} 2100}
2017 2101
2018void 2102void
2019ev_prepare_stop (EV_P_ ev_prepare *w) 2103ev_prepare_stop (EV_P_ ev_prepare *w)
2020{ 2104{
2021 ev_clear_pending (EV_A_ (W)w); 2105 clear_pending (EV_A_ (W)w);
2022 if (expect_false (!ev_is_active (w))) 2106 if (expect_false (!ev_is_active (w)))
2023 return; 2107 return;
2024 2108
2025 { 2109 {
2026 int active = ((W)w)->active; 2110 int active = ((W)w)->active;
2043} 2127}
2044 2128
2045void 2129void
2046ev_check_stop (EV_P_ ev_check *w) 2130ev_check_stop (EV_P_ ev_check *w)
2047{ 2131{
2048 ev_clear_pending (EV_A_ (W)w); 2132 clear_pending (EV_A_ (W)w);
2049 if (expect_false (!ev_is_active (w))) 2133 if (expect_false (!ev_is_active (w)))
2050 return; 2134 return;
2051 2135
2052 { 2136 {
2053 int active = ((W)w)->active; 2137 int active = ((W)w)->active;
2095} 2179}
2096 2180
2097void 2181void
2098ev_embed_stop (EV_P_ ev_embed *w) 2182ev_embed_stop (EV_P_ ev_embed *w)
2099{ 2183{
2100 ev_clear_pending (EV_A_ (W)w); 2184 clear_pending (EV_A_ (W)w);
2101 if (expect_false (!ev_is_active (w))) 2185 if (expect_false (!ev_is_active (w)))
2102 return; 2186 return;
2103 2187
2104 ev_io_stop (EV_A_ &w->io); 2188 ev_io_stop (EV_A_ &w->io);
2105 2189
2120} 2204}
2121 2205
2122void 2206void
2123ev_fork_stop (EV_P_ ev_fork *w) 2207ev_fork_stop (EV_P_ ev_fork *w)
2124{ 2208{
2125 ev_clear_pending (EV_A_ (W)w); 2209 clear_pending (EV_A_ (W)w);
2126 if (expect_false (!ev_is_active (w))) 2210 if (expect_false (!ev_is_active (w)))
2127 return; 2211 return;
2128 2212
2129 { 2213 {
2130 int active = ((W)w)->active; 2214 int active = ((W)w)->active;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines