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

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

422 W w; 422 W w;
423 int events; 423 int events;
424} ANPENDING; 424} ANPENDING;
425 425
426#if EV_USE_INOTIFY 426#if EV_USE_INOTIFY
427/* hash table entry per inotify-id */
427typedef struct 428typedef struct
428{ 429{
429 WL head; 430 WL head;
430} 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)
431#endif 450#endif
432 451
433#if EV_MULTIPLICITY 452#if EV_MULTIPLICITY
434 453
435 struct ev_loop 454 struct ev_loop
520 } 539 }
521} 540}
522 541
523/*****************************************************************************/ 542/*****************************************************************************/
524 543
544#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
545
525int inline_size 546int inline_size
526array_nextsize (int elem, int cur, int cnt) 547array_nextsize (int elem, int cur, int cnt)
527{ 548{
528 int ncur = cur + 1; 549 int ncur = cur + 1;
529 550
530 do 551 do
531 ncur <<= 1; 552 ncur <<= 1;
532 while (cnt > ncur); 553 while (cnt > ncur);
533 554
534 /* 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 */
535 if (elem * ncur > 4096) 556 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
536 { 557 {
537 ncur *= elem; 558 ncur *= elem;
538 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095; 559 ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
539 ncur = ncur - sizeof (void *) * 4; 560 ncur = ncur - sizeof (void *) * 4;
540 ncur /= elem; 561 ncur /= elem;
541 } 562 }
542 563
543 return ncur; 564 return ncur;
757 } 778 }
758} 779}
759 780
760/*****************************************************************************/ 781/*****************************************************************************/
761 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
762/* towards the root */ 801/* towards the root */
763void inline_speed 802void inline_speed
764upheap (WT *heap, int k) 803upheap (ANHE *heap, int k)
765{ 804{
766 WT w = heap [k]; 805 ANHE he = heap [k];
767 806
768 for (;;) 807 for (;;)
769 { 808 {
770 int p = k >> 1; 809 int p = ((k - HEAP0 - 1) / DHEAP) + HEAP0;
771 810
772 /* maybe we could use a dummy element at heap [0]? */ 811 if (p == k || ANHE_at (heap [p]) <= ANHE_at (he))
773 if (!p || heap [p]->at <= w->at)
774 break; 812 break;
775 813
776 heap [k] = heap [p]; 814 heap [k] = heap [p];
777 ((W)heap [k])->active = k; 815 ev_active (ANHE_w (heap [k])) = k;
778 k = p; 816 k = p;
779 } 817 }
780 818
819 ev_active (ANHE_w (he)) = k;
781 heap [k] = w; 820 heap [k] = he;
782 ((W)heap [k])->active = k;
783} 821}
784 822
785/* away from the root */ 823/* away from the root */
786void inline_speed 824void inline_speed
787downheap (WT *heap, int N, int k) 825downheap (ANHE *heap, int N, int k)
788{ 826{
789 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];
790 899
791 for (;;) 900 for (;;)
792 { 901 {
793 int c = k << 1; 902 int c = k << 1;
794 903
795 if (c > N) 904 if (c > N)
796 break; 905 break;
797 906
798 c += c < N && heap [c]->at > heap [c + 1]->at 907 c += c + 1 < N && ANHE_at (heap [c]) > ANHE_at (heap [c + 1])
799 ? 1 : 0; 908 ? 1 : 0;
800 909
801 if (w->at <= heap [c]->at) 910 if (w->at <= ANHE_at (heap [c]))
802 break; 911 break;
803 912
804 heap [k] = heap [c]; 913 heap [k] = heap [c];
805 ((W)heap [k])->active = k; 914 ev_active (ANHE_w (heap [k])) = k;
806 915
807 k = c; 916 k = c;
808 } 917 }
809 918
810 heap [k] = w; 919 heap [k] = he;
811 ((W)heap [k])->active = k; 920 ev_active (ANHE_w (he)) = k;
812} 921}
922#endif
813 923
814void inline_size 924void inline_size
815adjustheap (WT *heap, int N, int k) 925adjustheap (ANHE *heap, int N, int k)
816{ 926{
817 upheap (heap, k); 927 upheap (heap, k);
818 downheap (heap, N, k); 928 downheap (heap, N, k);
819} 929}
820 930
912pipecb (EV_P_ ev_io *iow, int revents) 1022pipecb (EV_P_ ev_io *iow, int revents)
913{ 1023{
914#if EV_USE_EVENTFD 1024#if EV_USE_EVENTFD
915 if (evfd >= 0) 1025 if (evfd >= 0)
916 { 1026 {
917 uint64_t counter = 1; 1027 uint64_t counter;
918 read (evfd, &counter, sizeof (uint64_t)); 1028 read (evfd, &counter, sizeof (uint64_t));
919 } 1029 }
920 else 1030 else
921#endif 1031#endif
922 { 1032 {
1368void 1478void
1369ev_loop_fork (EV_P) 1479ev_loop_fork (EV_P)
1370{ 1480{
1371 postfork = 1; /* must be in line with ev_default_fork */ 1481 postfork = 1; /* must be in line with ev_default_fork */
1372} 1482}
1373
1374#endif 1483#endif
1375 1484
1376#if EV_MULTIPLICITY 1485#if EV_MULTIPLICITY
1377struct ev_loop * 1486struct ev_loop *
1378ev_default_loop_init (unsigned int flags) 1487ev_default_loop_init (unsigned int flags)
1459 EV_CB_INVOKE (p->w, p->events); 1568 EV_CB_INVOKE (p->w, p->events);
1460 } 1569 }
1461 } 1570 }
1462} 1571}
1463 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
1464void inline_size 1596void inline_size
1465timers_reify (EV_P) 1597timers_reify (EV_P)
1466{ 1598{
1467 while (timercnt && ev_at (timers [1]) <= mn_now) 1599 while (timercnt && ANHE_at (timers [HEAP0]) <= mn_now)
1468 { 1600 {
1469 ev_timer *w = (ev_timer *)timers [1]; 1601 ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]);
1470 1602
1471 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ 1603 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1472 1604
1473 /* first reschedule or stop timer */ 1605 /* first reschedule or stop timer */
1474 if (w->repeat) 1606 if (w->repeat)
1477 1609
1478 ev_at (w) += w->repeat; 1610 ev_at (w) += w->repeat;
1479 if (ev_at (w) < mn_now) 1611 if (ev_at (w) < mn_now)
1480 ev_at (w) = mn_now; 1612 ev_at (w) = mn_now;
1481 1613
1482 downheap (timers, timercnt, 1); 1614 downheap (timers, timercnt, HEAP0);
1483 } 1615 }
1484 else 1616 else
1485 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1617 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1486 1618
1487 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1619 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1490 1622
1491#if EV_PERIODIC_ENABLE 1623#if EV_PERIODIC_ENABLE
1492void inline_size 1624void inline_size
1493periodics_reify (EV_P) 1625periodics_reify (EV_P)
1494{ 1626{
1495 while (periodiccnt && ev_at (periodics [1]) <= ev_rt_now) 1627 while (periodiccnt && ANHE_at (periodics [HEAP0]) <= ev_rt_now)
1496 { 1628 {
1497 ev_periodic *w = (ev_periodic *)periodics [1]; 1629 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
1498 1630
1499 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1631 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1500 1632
1501 /* first reschedule or stop timer */ 1633 /* first reschedule or stop timer */
1502 if (w->reschedule_cb) 1634 if (w->reschedule_cb)
1508 else if (w->interval) 1640 else if (w->interval)
1509 { 1641 {
1510 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;
1511 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;
1512 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));
1513 downheap (periodics, periodiccnt, 1); 1645 downheap (periodics, periodiccnt, HEAP0);
1514 } 1646 }
1515 else 1647 else
1516 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1648 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1517 1649
1518 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1650 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1523periodics_reschedule (EV_P) 1655periodics_reschedule (EV_P)
1524{ 1656{
1525 int i; 1657 int i;
1526 1658
1527 /* adjust periodics after time jump */ 1659 /* adjust periodics after time jump */
1528 for (i = 0; i < periodiccnt; ++i) 1660 for (i = HEAP0; i < periodiccnt + HEAP0; ++i)
1529 { 1661 {
1530 ev_periodic *w = (ev_periodic *)periodics [i]; 1662 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]);
1531 1663
1532 if (w->reschedule_cb) 1664 if (w->reschedule_cb)
1533 ev_at (w) = w->reschedule_cb (w, ev_rt_now); 1665 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1534 else if (w->interval) 1666 else if (w->interval)
1535 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;
1536 } 1668 }
1537 1669
1538 /* now rebuild the heap */ 1670 /* now rebuild the heap, this for the 2-heap, inefficient for the 4-heap, but correct */
1539 for (i = periodiccnt >> 1; i--; ) 1671 for (i = periodiccnt >> 1; --i; )
1540 downheap (periodics, periodiccnt, i); 1672 downheap (periodics, periodiccnt, i + HEAP0);
1541}
1542#endif
1543
1544#if EV_IDLE_ENABLE
1545void inline_size
1546idle_reify (EV_P)
1547{
1548 if (expect_false (idleall))
1549 {
1550 int pri;
1551
1552 for (pri = NUMPRI; pri--; )
1553 {
1554 if (pendingcnt [pri])
1555 break;
1556
1557 if (idlecnt [pri])
1558 {
1559 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1560 break;
1561 }
1562 }
1563 }
1564} 1673}
1565#endif 1674#endif
1566 1675
1567void inline_speed 1676void inline_speed
1568time_update (EV_P_ ev_tstamp max_block) 1677time_update (EV_P_ ev_tstamp max_block)
1597 */ 1706 */
1598 for (i = 4; --i; ) 1707 for (i = 4; --i; )
1599 { 1708 {
1600 rtmn_diff = ev_rt_now - mn_now; 1709 rtmn_diff = ev_rt_now - mn_now;
1601 1710
1602 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1711 if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP))
1603 return; /* all is well */ 1712 return; /* all is well */
1604 1713
1605 ev_rt_now = ev_time (); 1714 ev_rt_now = ev_time ();
1606 mn_now = get_clock (); 1715 mn_now = get_clock ();
1607 now_floor = mn_now; 1716 now_floor = mn_now;
1622 { 1731 {
1623#if EV_PERIODIC_ENABLE 1732#if EV_PERIODIC_ENABLE
1624 periodics_reschedule (EV_A); 1733 periodics_reschedule (EV_A);
1625#endif 1734#endif
1626 /* 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 */
1627 for (i = 1; i <= timercnt; ++i) 1736 for (i = 0; i < timercnt; ++i)
1628 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 }
1629 } 1742 }
1630 1743
1631 mn_now = ev_rt_now; 1744 mn_now = ev_rt_now;
1632 } 1745 }
1633} 1746}
1703 1816
1704 waittime = MAX_BLOCKTIME; 1817 waittime = MAX_BLOCKTIME;
1705 1818
1706 if (timercnt) 1819 if (timercnt)
1707 { 1820 {
1708 ev_tstamp to = ev_at (timers [1]) - mn_now + backend_fudge; 1821 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge;
1709 if (waittime > to) waittime = to; 1822 if (waittime > to) waittime = to;
1710 } 1823 }
1711 1824
1712#if EV_PERIODIC_ENABLE 1825#if EV_PERIODIC_ENABLE
1713 if (periodiccnt) 1826 if (periodiccnt)
1714 { 1827 {
1715 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;
1716 if (waittime > to) waittime = to; 1829 if (waittime > to) waittime = to;
1717 } 1830 }
1718#endif 1831#endif
1719 1832
1720 if (expect_false (waittime < timeout_blocktime)) 1833 if (expect_false (waittime < timeout_blocktime))
1890 2003
1891 ev_at (w) += mn_now; 2004 ev_at (w) += mn_now;
1892 2005
1893 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.));
1894 2007
1895 ev_start (EV_A_ (W)w, ++timercnt); 2008 ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1);
1896 array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2); 2009 array_needsize (ANHE, timers, timermax, ev_active (w) + 1, EMPTY2);
1897 timers [timercnt] = (WT)w; 2010 ANHE_w (timers [ev_active (w)]) = (WT)w;
2011 ANHE_at_set (timers [ev_active (w)]);
1898 upheap (timers, timercnt); 2012 upheap (timers, ev_active (w));
1899 2013
1900 /*assert (("internal timer heap corruption", timers [((W)w)->active] == w));*/ 2014 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/
1901} 2015}
1902 2016
1903void noinline 2017void noinline
1904ev_timer_stop (EV_P_ ev_timer *w) 2018ev_timer_stop (EV_P_ ev_timer *w)
1905{ 2019{
1906 clear_pending (EV_A_ (W)w); 2020 clear_pending (EV_A_ (W)w);
1907 if (expect_false (!ev_is_active (w))) 2021 if (expect_false (!ev_is_active (w)))
1908 return; 2022 return;
1909 2023
1910 assert (("internal timer heap corruption", timers [((W)w)->active] == (WT)w));
1911
1912 { 2024 {
1913 int active = ((W)w)->active; 2025 int active = ev_active (w);
1914 2026
2027 assert (("internal timer heap corruption", ANHE_w (timers [active]) == (WT)w));
2028
1915 if (expect_true (active < timercnt)) 2029 if (expect_true (active < timercnt + HEAP0 - 1))
1916 { 2030 {
1917 timers [active] = timers [timercnt]; 2031 timers [active] = timers [timercnt + HEAP0 - 1];
1918 adjustheap (timers, timercnt, active); 2032 adjustheap (timers, timercnt, active);
1919 } 2033 }
1920 2034
1921 --timercnt; 2035 --timercnt;
1922 } 2036 }
1932 if (ev_is_active (w)) 2046 if (ev_is_active (w))
1933 { 2047 {
1934 if (w->repeat) 2048 if (w->repeat)
1935 { 2049 {
1936 ev_at (w) = mn_now + w->repeat; 2050 ev_at (w) = mn_now + w->repeat;
2051 ANHE_at_set (timers [ev_active (w)]);
1937 adjustheap (timers, timercnt, ((W)w)->active); 2052 adjustheap (timers, timercnt, ev_active (w));
1938 } 2053 }
1939 else 2054 else
1940 ev_timer_stop (EV_A_ w); 2055 ev_timer_stop (EV_A_ w);
1941 } 2056 }
1942 else if (w->repeat) 2057 else if (w->repeat)
1962 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;
1963 } 2078 }
1964 else 2079 else
1965 ev_at (w) = w->offset; 2080 ev_at (w) = w->offset;
1966 2081
1967 ev_start (EV_A_ (W)w, ++periodiccnt); 2082 ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1);
1968 array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2); 2083 array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, EMPTY2);
1969 periodics [periodiccnt] = (WT)w; 2084 ANHE_w (periodics [ev_active (w)]) = (WT)w;
1970 upheap (periodics, periodiccnt); 2085 upheap (periodics, ev_active (w));
1971 2086
1972 /*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));*/
1973} 2088}
1974 2089
1975void noinline 2090void noinline
1976ev_periodic_stop (EV_P_ ev_periodic *w) 2091ev_periodic_stop (EV_P_ ev_periodic *w)
1977{ 2092{
1978 clear_pending (EV_A_ (W)w); 2093 clear_pending (EV_A_ (W)w);
1979 if (expect_false (!ev_is_active (w))) 2094 if (expect_false (!ev_is_active (w)))
1980 return; 2095 return;
1981 2096
1982 assert (("internal periodic heap corruption", periodics [((W)w)->active] == (WT)w));
1983
1984 { 2097 {
1985 int active = ((W)w)->active; 2098 int active = ev_active (w);
1986 2099
2100 assert (("internal periodic heap corruption", ANHE_w (periodics [active]) == (WT)w));
2101
1987 if (expect_true (active < periodiccnt)) 2102 if (expect_true (active < periodiccnt + HEAP0 - 1))
1988 { 2103 {
1989 periodics [active] = periodics [periodiccnt]; 2104 periodics [active] = periodics [periodiccnt + HEAP0 - 1];
1990 adjustheap (periodics, periodiccnt, active); 2105 adjustheap (periodics, periodiccnt, active);
1991 } 2106 }
1992 2107
1993 --periodiccnt; 2108 --periodiccnt;
1994 } 2109 }
2114 if (w->wd < 0) 2229 if (w->wd < 0)
2115 { 2230 {
2116 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 */
2117 2232
2118 /* 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 */
2119 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2236 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2120 { 2237 {
2121 char path [4096]; 2238 char path [4096];
2122 strcpy (path, w->path); 2239 strcpy (path, w->path);
2123 2240
2368 clear_pending (EV_A_ (W)w); 2485 clear_pending (EV_A_ (W)w);
2369 if (expect_false (!ev_is_active (w))) 2486 if (expect_false (!ev_is_active (w)))
2370 return; 2487 return;
2371 2488
2372 { 2489 {
2373 int active = ((W)w)->active; 2490 int active = ev_active (w);
2374 2491
2375 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; 2492 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2376 ((W)idles [ABSPRI (w)][active - 1])->active = active; 2493 ev_active (idles [ABSPRI (w)][active - 1]) = active;
2377 2494
2378 ev_stop (EV_A_ (W)w); 2495 ev_stop (EV_A_ (W)w);
2379 --idleall; 2496 --idleall;
2380 } 2497 }
2381} 2498}
2398 clear_pending (EV_A_ (W)w); 2515 clear_pending (EV_A_ (W)w);
2399 if (expect_false (!ev_is_active (w))) 2516 if (expect_false (!ev_is_active (w)))
2400 return; 2517 return;
2401 2518
2402 { 2519 {
2403 int active = ((W)w)->active; 2520 int active = ev_active (w);
2521
2404 prepares [active - 1] = prepares [--preparecnt]; 2522 prepares [active - 1] = prepares [--preparecnt];
2405 ((W)prepares [active - 1])->active = active; 2523 ev_active (prepares [active - 1]) = active;
2406 } 2524 }
2407 2525
2408 ev_stop (EV_A_ (W)w); 2526 ev_stop (EV_A_ (W)w);
2409} 2527}
2410 2528
2425 clear_pending (EV_A_ (W)w); 2543 clear_pending (EV_A_ (W)w);
2426 if (expect_false (!ev_is_active (w))) 2544 if (expect_false (!ev_is_active (w)))
2427 return; 2545 return;
2428 2546
2429 { 2547 {
2430 int active = ((W)w)->active; 2548 int active = ev_active (w);
2549
2431 checks [active - 1] = checks [--checkcnt]; 2550 checks [active - 1] = checks [--checkcnt];
2432 ((W)checks [active - 1])->active = active; 2551 ev_active (checks [active - 1]) = active;
2433 } 2552 }
2434 2553
2435 ev_stop (EV_A_ (W)w); 2554 ev_stop (EV_A_ (W)w);
2436} 2555}
2437 2556
2533 clear_pending (EV_A_ (W)w); 2652 clear_pending (EV_A_ (W)w);
2534 if (expect_false (!ev_is_active (w))) 2653 if (expect_false (!ev_is_active (w)))
2535 return; 2654 return;
2536 2655
2537 { 2656 {
2538 int active = ((W)w)->active; 2657 int active = ev_active (w);
2658
2539 forks [active - 1] = forks [--forkcnt]; 2659 forks [active - 1] = forks [--forkcnt];
2540 ((W)forks [active - 1])->active = active; 2660 ev_active (forks [active - 1]) = active;
2541 } 2661 }
2542 2662
2543 ev_stop (EV_A_ (W)w); 2663 ev_stop (EV_A_ (W)w);
2544} 2664}
2545#endif 2665#endif
2564 clear_pending (EV_A_ (W)w); 2684 clear_pending (EV_A_ (W)w);
2565 if (expect_false (!ev_is_active (w))) 2685 if (expect_false (!ev_is_active (w)))
2566 return; 2686 return;
2567 2687
2568 { 2688 {
2569 int active = ((W)w)->active; 2689 int active = ev_active (w);
2690
2570 asyncs [active - 1] = asyncs [--asynccnt]; 2691 asyncs [active - 1] = asyncs [--asynccnt];
2571 ((W)asyncs [active - 1])->active = active; 2692 ev_active (asyncs [active - 1]) = active;
2572 } 2693 }
2573 2694
2574 ev_stop (EV_A_ (W)w); 2695 ev_stop (EV_A_ (W)w);
2575} 2696}
2576 2697

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines