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

Comparing libev/ev.c (file contents):
Revision 1.228 by root, Fri May 2 08:07:37 2008 UTC vs.
Revision 1.241 by root, Fri May 9 13:57:00 2008 UTC

325 325
326typedef ev_watcher *W; 326typedef ev_watcher *W;
327typedef ev_watcher_list *WL; 327typedef ev_watcher_list *WL;
328typedef ev_watcher_time *WT; 328typedef ev_watcher_time *WT;
329 329
330#define ev_active(w) ((W)(w))->active
330#define ev_at(w) ((WT)(w))->at 331#define ev_at(w) ((WT)(w))->at
331 332
332#if EV_USE_MONOTONIC 333#if EV_USE_MONOTONIC
333/* sig_atomic_t is used to avoid per-thread variables or locking but still */ 334/* sig_atomic_t is used to avoid per-thread variables or locking but still */
334/* giving it a reasonably high chance of working on typical architetcures */ 335/* giving it a reasonably high chance of working on typical architetcures */
421 W w; 422 W w;
422 int events; 423 int events;
423} ANPENDING; 424} ANPENDING;
424 425
425#if EV_USE_INOTIFY 426#if EV_USE_INOTIFY
427/* hash table entry per inotify-id */
426typedef struct 428typedef struct
427{ 429{
428 WL head; 430 WL head;
429} ANFS; 431} ANFS;
432#endif
433
434/* Heap Entry */
435#if EV_HEAP_CACHE_AT
436 typedef struct {
437 WT w;
438 ev_tstamp at;
439 } ANHE;
440
441 #define ANHE_w(he) (he) /* access watcher, read-write */
442 #define ANHE_at(he) (he)->at /* acces cahced at, read-only */
443 #define ANHE_at_set(he) (he)->at = (he)->w->at /* update at from watcher */
444#else
445 typedef WT ANHE;
446
447 #define ANHE_w(he) (he)
448 #define ANHE_at(he) (he)->at
449 #define ANHE_at_set(he)
430#endif 450#endif
431 451
432#if EV_MULTIPLICITY 452#if EV_MULTIPLICITY
433 453
434 struct ev_loop 454 struct ev_loop
519 } 539 }
520} 540}
521 541
522/*****************************************************************************/ 542/*****************************************************************************/
523 543
544#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
545
524int inline_size 546int inline_size
525array_nextsize (int elem, int cur, int cnt) 547array_nextsize (int elem, int cur, int cnt)
526{ 548{
527 int ncur = cur + 1; 549 int ncur = cur + 1;
528 550
529 do 551 do
530 ncur <<= 1; 552 ncur <<= 1;
531 while (cnt > ncur); 553 while (cnt > ncur);
532 554
533 /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */ 555 /* if size is large, round to MALLOC_ROUND - 4 * longs to accomodate malloc overhead */
534 if (elem * ncur > 4096) 556 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
535 { 557 {
536 ncur *= elem; 558 ncur *= elem;
537 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095; 559 ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
538 ncur = ncur - sizeof (void *) * 4; 560 ncur = ncur - sizeof (void *) * 4;
539 ncur /= elem; 561 ncur /= elem;
540 } 562 }
541 563
542 return ncur; 564 return ncur;
756 } 778 }
757} 779}
758 780
759/*****************************************************************************/ 781/*****************************************************************************/
760 782
783/*
784 * the heap functions want a real array index. array index 0 uis guaranteed to not
785 * be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives
786 * the branching factor of the d-tree.
787 */
788
789/*
790 * at the moment we allow libev the luxury of two heaps,
791 * a small-code-size 2-heap one and a ~1.5kb larger 4-heap
792 * which is more cache-efficient.
793 * the difference is about 5% with 50000+ watchers.
794 */
795#define EV_USE_4HEAP !EV_MINIMAL
796#if EV_USE_4HEAP
797
798#define DHEAP 4
799#define HEAP0 (DHEAP - 1) /* index of first element in heap */
800
761/* towards the root */ 801/* towards the root */
762void inline_speed 802void inline_speed
763upheap (WT *heap, int k) 803upheap (ANHE *heap, int k)
764{ 804{
765 WT w = heap [k]; 805 ANHE he = heap [k];
766 806
767 for (;;) 807 for (;;)
768 { 808 {
769 int p = k >> 1; 809 int p = ((k - HEAP0 - 1) / DHEAP) + HEAP0;
770 810
771 /* maybe we could use a dummy element at heap [0]? */ 811 if (p == k || ANHE_at (heap [p]) <= ANHE_at (he))
772 if (!p || heap [p]->at <= w->at)
773 break; 812 break;
774 813
775 heap [k] = heap [p]; 814 heap [k] = heap [p];
776 ((W)heap [k])->active = k; 815 ev_active (ANHE_w (heap [k])) = k;
777 k = p; 816 k = p;
778 } 817 }
779 818
819 ev_active (ANHE_w (he)) = k;
780 heap [k] = w; 820 heap [k] = he;
781 ((W)heap [k])->active = k;
782} 821}
783 822
784/* away from the root */ 823/* away from the root */
785void inline_speed 824void inline_speed
786downheap (WT *heap, int N, int k) 825downheap (ANHE *heap, int N, int k)
787{ 826{
788 WT w = heap [k]; 827 ANHE he = heap [k];
828 ANHE *E = heap + N + HEAP0;
829
830 for (;;)
831 {
832 ev_tstamp minat;
833 ANHE *minpos;
834 ANHE *pos = heap + DHEAP * (k - HEAP0) + HEAP0;
835
836 // find minimum child
837 if (expect_true (pos + DHEAP - 1 < E))
838 {
839 /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
840 if (ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos));
841 if (ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos));
842 if (ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos));
843 }
844 else if (pos < E)
845 {
846 /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
847 if (pos + 1 < E && ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos));
848 if (pos + 2 < E && ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos));
849 if (pos + 3 < E && ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos));
850 }
851 else
852 break;
853
854 if (ANHE_at (he) <= minat)
855 break;
856
857 ev_active (ANHE_w (*minpos)) = k;
858 heap [k] = *minpos;
859
860 k = minpos - heap;
861 }
862
863 ev_active (ANHE_w (he)) = k;
864 heap [k] = he;
865}
866
867#else // 4HEAP
868
869#define HEAP0 1
870
871/* towards the root */
872void inline_speed
873upheap (ANHE *heap, int k)
874{
875 ANHE he = heap [k];
876
877 for (;;)
878 {
879 int p = k >> 1;
880
881 /* maybe we could use a dummy element at heap [0]? */
882 if (!p || ANHE_at (heap [p]) <= ANHE_at (he))
883 break;
884
885 heap [k] = heap [p];
886 ev_active (ANHE_w (heap [k])) = k;
887 k = p;
888 }
889
890 heap [k] = w;
891 ev_active (ANHE_w (heap [k])) = k;
892}
893
894/* away from the root */
895void inline_speed
896downheap (ANHE *heap, int N, int k)
897{
898 ANHE he = heap [k];
789 899
790 for (;;) 900 for (;;)
791 { 901 {
792 int c = k << 1; 902 int c = k << 1;
793 903
794 if (c > N) 904 if (c > N)
795 break; 905 break;
796 906
797 c += c < N && heap [c]->at > heap [c + 1]->at 907 c += c + 1 < N && ANHE_at (heap [c]) > ANHE_at (heap [c + 1])
798 ? 1 : 0; 908 ? 1 : 0;
799 909
800 if (w->at <= heap [c]->at) 910 if (w->at <= ANHE_at (heap [c]))
801 break; 911 break;
802 912
803 heap [k] = heap [c]; 913 heap [k] = heap [c];
804 ((W)heap [k])->active = k; 914 ev_active (ANHE_w (heap [k])) = k;
805 915
806 k = c; 916 k = c;
807 } 917 }
808 918
809 heap [k] = w; 919 heap [k] = he;
810 ((W)heap [k])->active = k; 920 ev_active (ANHE_w (he)) = k;
811} 921}
922#endif
812 923
813void inline_size 924void inline_size
814adjustheap (WT *heap, int N, int k) 925adjustheap (ANHE *heap, int N, int k)
815{ 926{
816 upheap (heap, k); 927 upheap (heap, k);
817 downheap (heap, N, k); 928 downheap (heap, N, k);
818} 929}
819 930
911pipecb (EV_P_ ev_io *iow, int revents) 1022pipecb (EV_P_ ev_io *iow, int revents)
912{ 1023{
913#if EV_USE_EVENTFD 1024#if EV_USE_EVENTFD
914 if (evfd >= 0) 1025 if (evfd >= 0)
915 { 1026 {
916 uint64_t counter = 1; 1027 uint64_t counter;
917 read (evfd, &counter, sizeof (uint64_t)); 1028 read (evfd, &counter, sizeof (uint64_t));
918 } 1029 }
919 else 1030 else
920#endif 1031#endif
921 { 1032 {
1367void 1478void
1368ev_loop_fork (EV_P) 1479ev_loop_fork (EV_P)
1369{ 1480{
1370 postfork = 1; /* must be in line with ev_default_fork */ 1481 postfork = 1; /* must be in line with ev_default_fork */
1371} 1482}
1372
1373#endif 1483#endif
1374 1484
1375#if EV_MULTIPLICITY 1485#if EV_MULTIPLICITY
1376struct ev_loop * 1486struct ev_loop *
1377ev_default_loop_init (unsigned int flags) 1487ev_default_loop_init (unsigned int flags)
1458 EV_CB_INVOKE (p->w, p->events); 1568 EV_CB_INVOKE (p->w, p->events);
1459 } 1569 }
1460 } 1570 }
1461} 1571}
1462 1572
1573#if EV_IDLE_ENABLE
1574void inline_size
1575idle_reify (EV_P)
1576{
1577 if (expect_false (idleall))
1578 {
1579 int pri;
1580
1581 for (pri = NUMPRI; pri--; )
1582 {
1583 if (pendingcnt [pri])
1584 break;
1585
1586 if (idlecnt [pri])
1587 {
1588 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1589 break;
1590 }
1591 }
1592 }
1593}
1594#endif
1595
1463void inline_size 1596void inline_size
1464timers_reify (EV_P) 1597timers_reify (EV_P)
1465{ 1598{
1466 while (timercnt && ev_at (timers [1]) <= mn_now) 1599 while (timercnt && ANHE_at (timers [HEAP0]) <= mn_now)
1467 { 1600 {
1468 ev_timer *w = (ev_timer *)timers [1]; 1601 ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]);
1469 1602
1470 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ 1603 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1471 1604
1472 /* first reschedule or stop timer */ 1605 /* first reschedule or stop timer */
1473 if (w->repeat) 1606 if (w->repeat)
1476 1609
1477 ev_at (w) += w->repeat; 1610 ev_at (w) += w->repeat;
1478 if (ev_at (w) < mn_now) 1611 if (ev_at (w) < mn_now)
1479 ev_at (w) = mn_now; 1612 ev_at (w) = mn_now;
1480 1613
1481 downheap (timers, timercnt, 1); 1614 downheap (timers, timercnt, HEAP0);
1482 } 1615 }
1483 else 1616 else
1484 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1617 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1485 1618
1486 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1619 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1489 1622
1490#if EV_PERIODIC_ENABLE 1623#if EV_PERIODIC_ENABLE
1491void inline_size 1624void inline_size
1492periodics_reify (EV_P) 1625periodics_reify (EV_P)
1493{ 1626{
1494 while (periodiccnt && ev_at (periodics [1]) <= ev_rt_now) 1627 while (periodiccnt && ANHE_at (periodics [HEAP0]) <= ev_rt_now)
1495 { 1628 {
1496 ev_periodic *w = (ev_periodic *)periodics [1]; 1629 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
1497 1630
1498 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1631 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1499 1632
1500 /* first reschedule or stop timer */ 1633 /* first reschedule or stop timer */
1501 if (w->reschedule_cb) 1634 if (w->reschedule_cb)
1507 else if (w->interval) 1640 else if (w->interval)
1508 { 1641 {
1509 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 1642 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1510 if (ev_at (w) - ev_rt_now <= TIME_EPSILON) ev_at (w) += w->interval; 1643 if (ev_at (w) - ev_rt_now <= TIME_EPSILON) ev_at (w) += w->interval;
1511 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now)); 1644 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now));
1512 downheap (periodics, periodiccnt, 1); 1645 downheap (periodics, periodiccnt, HEAP0);
1513 } 1646 }
1514 else 1647 else
1515 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1648 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1516 1649
1517 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1650 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1522periodics_reschedule (EV_P) 1655periodics_reschedule (EV_P)
1523{ 1656{
1524 int i; 1657 int i;
1525 1658
1526 /* adjust periodics after time jump */ 1659 /* adjust periodics after time jump */
1527 for (i = 0; i < periodiccnt; ++i) 1660 for (i = HEAP0; i < periodiccnt + HEAP0; ++i)
1528 { 1661 {
1529 ev_periodic *w = (ev_periodic *)periodics [i]; 1662 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]);
1530 1663
1531 if (w->reschedule_cb) 1664 if (w->reschedule_cb)
1532 ev_at (w) = w->reschedule_cb (w, ev_rt_now); 1665 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1533 else if (w->interval) 1666 else if (w->interval)
1534 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 1667 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1535 } 1668 }
1536 1669
1537 /* now rebuild the heap */ 1670 /* now rebuild the heap, this for the 2-heap, inefficient for the 4-heap, but correct */
1538 for (i = periodiccnt >> 1; i--; ) 1671 for (i = periodiccnt >> 1; --i; )
1539 downheap (periodics, periodiccnt, i); 1672 downheap (periodics, periodiccnt, i + HEAP0);
1540}
1541#endif
1542
1543#if EV_IDLE_ENABLE
1544void inline_size
1545idle_reify (EV_P)
1546{
1547 if (expect_false (idleall))
1548 {
1549 int pri;
1550
1551 for (pri = NUMPRI; pri--; )
1552 {
1553 if (pendingcnt [pri])
1554 break;
1555
1556 if (idlecnt [pri])
1557 {
1558 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1559 break;
1560 }
1561 }
1562 }
1563} 1673}
1564#endif 1674#endif
1565 1675
1566void inline_speed 1676void inline_speed
1567time_update (EV_P_ ev_tstamp max_block) 1677time_update (EV_P_ ev_tstamp max_block)
1596 */ 1706 */
1597 for (i = 4; --i; ) 1707 for (i = 4; --i; )
1598 { 1708 {
1599 rtmn_diff = ev_rt_now - mn_now; 1709 rtmn_diff = ev_rt_now - mn_now;
1600 1710
1601 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1711 if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP))
1602 return; /* all is well */ 1712 return; /* all is well */
1603 1713
1604 ev_rt_now = ev_time (); 1714 ev_rt_now = ev_time ();
1605 mn_now = get_clock (); 1715 mn_now = get_clock ();
1606 now_floor = mn_now; 1716 now_floor = mn_now;
1621 { 1731 {
1622#if EV_PERIODIC_ENABLE 1732#if EV_PERIODIC_ENABLE
1623 periodics_reschedule (EV_A); 1733 periodics_reschedule (EV_A);
1624#endif 1734#endif
1625 /* adjust timers. this is easy, as the offset is the same for all of them */ 1735 /* adjust timers. this is easy, as the offset is the same for all of them */
1626 for (i = 1; i <= timercnt; ++i) 1736 for (i = 0; i < timercnt; ++i)
1627 ev_at (timers [i]) += ev_rt_now - mn_now; 1737 {
1738 ANHE *he = timers + i + HEAP0;
1739 ANHE_w (*he)->at += ev_rt_now - mn_now;
1740 ANHE_at_set (*he);
1741 }
1628 } 1742 }
1629 1743
1630 mn_now = ev_rt_now; 1744 mn_now = ev_rt_now;
1631 } 1745 }
1632} 1746}
1702 1816
1703 waittime = MAX_BLOCKTIME; 1817 waittime = MAX_BLOCKTIME;
1704 1818
1705 if (timercnt) 1819 if (timercnt)
1706 { 1820 {
1707 ev_tstamp to = ev_at (timers [1]) - mn_now + backend_fudge; 1821 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge;
1708 if (waittime > to) waittime = to; 1822 if (waittime > to) waittime = to;
1709 } 1823 }
1710 1824
1711#if EV_PERIODIC_ENABLE 1825#if EV_PERIODIC_ENABLE
1712 if (periodiccnt) 1826 if (periodiccnt)
1713 { 1827 {
1714 ev_tstamp to = ev_at (periodics [1]) - ev_rt_now + backend_fudge; 1828 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
1715 if (waittime > to) waittime = to; 1829 if (waittime > to) waittime = to;
1716 } 1830 }
1717#endif 1831#endif
1718 1832
1719 if (expect_false (waittime < timeout_blocktime)) 1833 if (expect_false (waittime < timeout_blocktime))
1889 2003
1890 ev_at (w) += mn_now; 2004 ev_at (w) += mn_now;
1891 2005
1892 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 2006 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1893 2007
1894 ev_start (EV_A_ (W)w, ++timercnt); 2008 ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1);
1895 array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2); 2009 array_needsize (ANHE, timers, timermax, ev_active (w) + 1, EMPTY2);
1896 timers [timercnt] = (WT)w; 2010 ANHE_w (timers [ev_active (w)]) = (WT)w;
2011 ANHE_at_set (timers [ev_active (w)]);
1897 upheap (timers, timercnt); 2012 upheap (timers, ev_active (w));
1898 2013
1899 /*assert (("internal timer heap corruption", timers [((W)w)->active] == w));*/ 2014 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/
1900} 2015}
1901 2016
1902void noinline 2017void noinline
1903ev_timer_stop (EV_P_ ev_timer *w) 2018ev_timer_stop (EV_P_ ev_timer *w)
1904{ 2019{
1905 clear_pending (EV_A_ (W)w); 2020 clear_pending (EV_A_ (W)w);
1906 if (expect_false (!ev_is_active (w))) 2021 if (expect_false (!ev_is_active (w)))
1907 return; 2022 return;
1908 2023
1909 assert (("internal timer heap corruption", timers [((W)w)->active] == (WT)w));
1910
1911 { 2024 {
1912 int active = ((W)w)->active; 2025 int active = ev_active (w);
1913 2026
2027 assert (("internal timer heap corruption", ANHE_w (timers [active]) == (WT)w));
2028
1914 if (expect_true (active < timercnt)) 2029 if (expect_true (active < timercnt + HEAP0 - 1))
1915 { 2030 {
1916 timers [active] = timers [timercnt]; 2031 timers [active] = timers [timercnt + HEAP0 - 1];
1917 adjustheap (timers, timercnt, active); 2032 adjustheap (timers, timercnt, active);
1918 } 2033 }
1919 2034
1920 --timercnt; 2035 --timercnt;
1921 } 2036 }
1931 if (ev_is_active (w)) 2046 if (ev_is_active (w))
1932 { 2047 {
1933 if (w->repeat) 2048 if (w->repeat)
1934 { 2049 {
1935 ev_at (w) = mn_now + w->repeat; 2050 ev_at (w) = mn_now + w->repeat;
2051 ANHE_at_set (timers [ev_active (w)]);
1936 adjustheap (timers, timercnt, ((W)w)->active); 2052 adjustheap (timers, timercnt, ev_active (w));
1937 } 2053 }
1938 else 2054 else
1939 ev_timer_stop (EV_A_ w); 2055 ev_timer_stop (EV_A_ w);
1940 } 2056 }
1941 else if (w->repeat) 2057 else if (w->repeat)
1942 { 2058 {
1943 w->at = w->repeat; 2059 ev_at (w) = w->repeat;
1944 ev_timer_start (EV_A_ w); 2060 ev_timer_start (EV_A_ w);
1945 } 2061 }
1946} 2062}
1947 2063
1948#if EV_PERIODIC_ENABLE 2064#if EV_PERIODIC_ENABLE
1961 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 2077 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1962 } 2078 }
1963 else 2079 else
1964 ev_at (w) = w->offset; 2080 ev_at (w) = w->offset;
1965 2081
1966 ev_start (EV_A_ (W)w, ++periodiccnt); 2082 ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1);
1967 array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2); 2083 array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, EMPTY2);
1968 periodics [periodiccnt] = (WT)w; 2084 ANHE_w (periodics [ev_active (w)]) = (WT)w;
1969 upheap (periodics, periodiccnt); 2085 upheap (periodics, ev_active (w));
1970 2086
1971 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 2087 /*assert (("internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/
1972} 2088}
1973 2089
1974void noinline 2090void noinline
1975ev_periodic_stop (EV_P_ ev_periodic *w) 2091ev_periodic_stop (EV_P_ ev_periodic *w)
1976{ 2092{
1977 clear_pending (EV_A_ (W)w); 2093 clear_pending (EV_A_ (W)w);
1978 if (expect_false (!ev_is_active (w))) 2094 if (expect_false (!ev_is_active (w)))
1979 return; 2095 return;
1980 2096
1981 assert (("internal periodic heap corruption", periodics [((W)w)->active] == (WT)w));
1982
1983 { 2097 {
1984 int active = ((W)w)->active; 2098 int active = ev_active (w);
1985 2099
2100 assert (("internal periodic heap corruption", ANHE_w (periodics [active]) == (WT)w));
2101
1986 if (expect_true (active < periodiccnt)) 2102 if (expect_true (active < periodiccnt + HEAP0 - 1))
1987 { 2103 {
1988 periodics [active] = periodics [periodiccnt]; 2104 periodics [active] = periodics [periodiccnt + HEAP0 - 1];
1989 adjustheap (periodics, periodiccnt, active); 2105 adjustheap (periodics, periodiccnt, active);
1990 } 2106 }
1991 2107
1992 --periodiccnt; 2108 --periodiccnt;
1993 } 2109 }
2113 if (w->wd < 0) 2229 if (w->wd < 0)
2114 { 2230 {
2115 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ 2231 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */
2116 2232
2117 /* monitor some parent directory for speedup hints */ 2233 /* monitor some parent directory for speedup hints */
2234 /* note that exceeding the hardcoded limit is not a correctness issue, */
2235 /* but an efficiency issue only */
2118 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2236 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2119 { 2237 {
2120 char path [4096]; 2238 char path [4096];
2121 strcpy (path, w->path); 2239 strcpy (path, w->path);
2122 2240
2367 clear_pending (EV_A_ (W)w); 2485 clear_pending (EV_A_ (W)w);
2368 if (expect_false (!ev_is_active (w))) 2486 if (expect_false (!ev_is_active (w)))
2369 return; 2487 return;
2370 2488
2371 { 2489 {
2372 int active = ((W)w)->active; 2490 int active = ev_active (w);
2373 2491
2374 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; 2492 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2375 ((W)idles [ABSPRI (w)][active - 1])->active = active; 2493 ev_active (idles [ABSPRI (w)][active - 1]) = active;
2376 2494
2377 ev_stop (EV_A_ (W)w); 2495 ev_stop (EV_A_ (W)w);
2378 --idleall; 2496 --idleall;
2379 } 2497 }
2380} 2498}
2397 clear_pending (EV_A_ (W)w); 2515 clear_pending (EV_A_ (W)w);
2398 if (expect_false (!ev_is_active (w))) 2516 if (expect_false (!ev_is_active (w)))
2399 return; 2517 return;
2400 2518
2401 { 2519 {
2402 int active = ((W)w)->active; 2520 int active = ev_active (w);
2521
2403 prepares [active - 1] = prepares [--preparecnt]; 2522 prepares [active - 1] = prepares [--preparecnt];
2404 ((W)prepares [active - 1])->active = active; 2523 ev_active (prepares [active - 1]) = active;
2405 } 2524 }
2406 2525
2407 ev_stop (EV_A_ (W)w); 2526 ev_stop (EV_A_ (W)w);
2408} 2527}
2409 2528
2424 clear_pending (EV_A_ (W)w); 2543 clear_pending (EV_A_ (W)w);
2425 if (expect_false (!ev_is_active (w))) 2544 if (expect_false (!ev_is_active (w)))
2426 return; 2545 return;
2427 2546
2428 { 2547 {
2429 int active = ((W)w)->active; 2548 int active = ev_active (w);
2549
2430 checks [active - 1] = checks [--checkcnt]; 2550 checks [active - 1] = checks [--checkcnt];
2431 ((W)checks [active - 1])->active = active; 2551 ev_active (checks [active - 1]) = active;
2432 } 2552 }
2433 2553
2434 ev_stop (EV_A_ (W)w); 2554 ev_stop (EV_A_ (W)w);
2435} 2555}
2436 2556
2532 clear_pending (EV_A_ (W)w); 2652 clear_pending (EV_A_ (W)w);
2533 if (expect_false (!ev_is_active (w))) 2653 if (expect_false (!ev_is_active (w)))
2534 return; 2654 return;
2535 2655
2536 { 2656 {
2537 int active = ((W)w)->active; 2657 int active = ev_active (w);
2658
2538 forks [active - 1] = forks [--forkcnt]; 2659 forks [active - 1] = forks [--forkcnt];
2539 ((W)forks [active - 1])->active = active; 2660 ev_active (forks [active - 1]) = active;
2540 } 2661 }
2541 2662
2542 ev_stop (EV_A_ (W)w); 2663 ev_stop (EV_A_ (W)w);
2543} 2664}
2544#endif 2665#endif
2563 clear_pending (EV_A_ (W)w); 2684 clear_pending (EV_A_ (W)w);
2564 if (expect_false (!ev_is_active (w))) 2685 if (expect_false (!ev_is_active (w)))
2565 return; 2686 return;
2566 2687
2567 { 2688 {
2568 int active = ((W)w)->active; 2689 int active = ev_active (w);
2690
2569 asyncs [active - 1] = asyncs [--asynccnt]; 2691 asyncs [active - 1] = asyncs [--asynccnt];
2570 ((W)asyncs [active - 1])->active = active; 2692 ev_active (asyncs [active - 1]) = active;
2571 } 2693 }
2572 2694
2573 ev_stop (EV_A_ (W)w); 2695 ev_stop (EV_A_ (W)w);
2574} 2696}
2575 2697

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines