… | |
… | |
325 | |
325 | |
326 | typedef ev_watcher *W; |
326 | typedef ev_watcher *W; |
327 | typedef ev_watcher_list *WL; |
327 | typedef ev_watcher_list *WL; |
328 | typedef ev_watcher_time *WT; |
328 | typedef ev_watcher_time *WT; |
329 | |
329 | |
|
|
330 | #define ev_active(w) ((W)(w))->active |
|
|
331 | #define ev_at(w) ((WT)(w))->at |
|
|
332 | |
330 | #if EV_USE_MONOTONIC |
333 | #if EV_USE_MONOTONIC |
331 | /* 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 */ |
332 | /* giving it a reasonably high chance of working on typical architetcures */ |
335 | /* giving it a reasonably high chance of working on typical architetcures */ |
333 | static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
336 | static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
334 | #endif |
337 | #endif |
… | |
… | |
760 | void inline_speed |
763 | void inline_speed |
761 | upheap (WT *heap, int k) |
764 | upheap (WT *heap, int k) |
762 | { |
765 | { |
763 | WT w = heap [k]; |
766 | WT w = heap [k]; |
764 | |
767 | |
765 | while (k) |
768 | for (;;) |
766 | { |
769 | { |
767 | int p = (k - 1) >> 1; |
770 | int p = k >> 1; |
768 | |
771 | |
|
|
772 | /* maybe we could use a dummy element at heap [0]? */ |
769 | if (heap [p]->at <= w->at) |
773 | if (!p || heap [p]->at <= w->at) |
770 | break; |
774 | break; |
771 | |
775 | |
772 | heap [k] = heap [p]; |
776 | heap [k] = heap [p]; |
773 | ((W)heap [k])->active = k + 1; |
777 | ((W)heap [k])->active = k; |
774 | k = p; |
778 | k = p; |
775 | } |
779 | } |
776 | |
780 | |
777 | heap [k] = w; |
781 | heap [k] = w; |
778 | ((W)heap [k])->active = k + 1; |
782 | ((W)heap [k])->active = k; |
779 | } |
783 | } |
780 | |
784 | |
781 | /* away from the root */ |
785 | /* away from the root */ |
782 | void inline_speed |
786 | void inline_speed |
783 | downheap (WT *heap, int N, int k) |
787 | downheap (WT *heap, int N, int k) |
784 | { |
788 | { |
785 | WT w = heap [k]; |
789 | WT w = heap [k]; |
786 | |
790 | |
787 | for (;;) |
791 | for (;;) |
788 | { |
792 | { |
789 | int c = (k << 1) + 1; |
793 | int c = k << 1; |
790 | |
794 | |
791 | if (c >= N) |
795 | if (c > N) |
792 | break; |
796 | break; |
793 | |
797 | |
794 | c += c + 1 < N && heap [c]->at > heap [c + 1]->at |
798 | c += c < N && heap [c]->at > heap [c + 1]->at |
795 | ? 1 : 0; |
799 | ? 1 : 0; |
796 | |
800 | |
797 | if (w->at <= heap [c]->at) |
801 | if (w->at <= heap [c]->at) |
798 | break; |
802 | break; |
799 | |
803 | |
800 | heap [k] = heap [c]; |
804 | heap [k] = heap [c]; |
801 | ((W)heap [k])->active = k + 1; |
805 | ((W)heap [k])->active = k; |
802 | |
806 | |
803 | k = c; |
807 | k = c; |
804 | } |
808 | } |
805 | |
809 | |
806 | heap [k] = w; |
810 | heap [k] = w; |
807 | ((W)heap [k])->active = k + 1; |
811 | ((W)heap [k])->active = k; |
808 | } |
812 | } |
809 | |
813 | |
810 | void inline_size |
814 | void inline_size |
811 | adjustheap (WT *heap, int N, int k) |
815 | adjustheap (WT *heap, int N, int k) |
812 | { |
816 | { |
… | |
… | |
1458 | } |
1462 | } |
1459 | |
1463 | |
1460 | void inline_size |
1464 | void inline_size |
1461 | timers_reify (EV_P) |
1465 | timers_reify (EV_P) |
1462 | { |
1466 | { |
1463 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
1467 | while (timercnt && ev_at (timers [1]) <= mn_now) |
1464 | { |
1468 | { |
1465 | ev_timer *w = (ev_timer *)timers [0]; |
1469 | ev_timer *w = (ev_timer *)timers [1]; |
1466 | |
1470 | |
1467 | /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ |
1471 | /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ |
1468 | |
1472 | |
1469 | /* first reschedule or stop timer */ |
1473 | /* first reschedule or stop timer */ |
1470 | if (w->repeat) |
1474 | if (w->repeat) |
1471 | { |
1475 | { |
1472 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
1476 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
1473 | |
1477 | |
1474 | ((WT)w)->at += w->repeat; |
1478 | ev_at (w) += w->repeat; |
1475 | if (((WT)w)->at < mn_now) |
1479 | if (ev_at (w) < mn_now) |
1476 | ((WT)w)->at = mn_now; |
1480 | ev_at (w) = mn_now; |
1477 | |
1481 | |
1478 | downheap (timers, timercnt, 0); |
1482 | downheap (timers, timercnt, 1); |
1479 | } |
1483 | } |
1480 | else |
1484 | else |
1481 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1485 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1482 | |
1486 | |
1483 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1487 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
… | |
… | |
1486 | |
1490 | |
1487 | #if EV_PERIODIC_ENABLE |
1491 | #if EV_PERIODIC_ENABLE |
1488 | void inline_size |
1492 | void inline_size |
1489 | periodics_reify (EV_P) |
1493 | periodics_reify (EV_P) |
1490 | { |
1494 | { |
1491 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1495 | while (periodiccnt && ev_at (periodics [1]) <= ev_rt_now) |
1492 | { |
1496 | { |
1493 | ev_periodic *w = (ev_periodic *)periodics [0]; |
1497 | ev_periodic *w = (ev_periodic *)periodics [1]; |
1494 | |
1498 | |
1495 | /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ |
1499 | /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ |
1496 | |
1500 | |
1497 | /* first reschedule or stop timer */ |
1501 | /* first reschedule or stop timer */ |
1498 | if (w->reschedule_cb) |
1502 | if (w->reschedule_cb) |
1499 | { |
1503 | { |
1500 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON); |
1504 | ev_at (w) = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON); |
1501 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
1505 | assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) > ev_rt_now)); |
1502 | downheap (periodics, periodiccnt, 0); |
1506 | downheap (periodics, periodiccnt, 1); |
1503 | } |
1507 | } |
1504 | else if (w->interval) |
1508 | else if (w->interval) |
1505 | { |
1509 | { |
1506 | ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
1510 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
1507 | if (((WT)w)->at - ev_rt_now <= TIME_EPSILON) ((WT)w)->at += w->interval; |
1511 | if (ev_at (w) - ev_rt_now <= TIME_EPSILON) ev_at (w) += w->interval; |
1508 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); |
1512 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now)); |
1509 | downheap (periodics, periodiccnt, 0); |
1513 | downheap (periodics, periodiccnt, 1); |
1510 | } |
1514 | } |
1511 | else |
1515 | else |
1512 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1516 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1513 | |
1517 | |
1514 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
1518 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
… | |
… | |
1524 | for (i = 0; i < periodiccnt; ++i) |
1528 | for (i = 0; i < periodiccnt; ++i) |
1525 | { |
1529 | { |
1526 | ev_periodic *w = (ev_periodic *)periodics [i]; |
1530 | ev_periodic *w = (ev_periodic *)periodics [i]; |
1527 | |
1531 | |
1528 | if (w->reschedule_cb) |
1532 | if (w->reschedule_cb) |
1529 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1533 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
1530 | else if (w->interval) |
1534 | else if (w->interval) |
1531 | ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
1535 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
1532 | } |
1536 | } |
1533 | |
1537 | |
1534 | /* now rebuild the heap */ |
1538 | /* now rebuild the heap */ |
1535 | for (i = periodiccnt >> 1; i--; ) |
1539 | for (i = periodiccnt >> 1; i--; ) |
1536 | downheap (periodics, periodiccnt, i); |
1540 | downheap (periodics, periodiccnt, i); |
… | |
… | |
1618 | { |
1622 | { |
1619 | #if EV_PERIODIC_ENABLE |
1623 | #if EV_PERIODIC_ENABLE |
1620 | periodics_reschedule (EV_A); |
1624 | periodics_reschedule (EV_A); |
1621 | #endif |
1625 | #endif |
1622 | /* adjust timers. this is easy, as the offset is the same for all of them */ |
1626 | /* adjust timers. this is easy, as the offset is the same for all of them */ |
1623 | for (i = 0; i < timercnt; ++i) |
1627 | for (i = 1; i <= timercnt; ++i) |
1624 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1628 | ev_at (timers [i]) += ev_rt_now - mn_now; |
1625 | } |
1629 | } |
1626 | |
1630 | |
1627 | mn_now = ev_rt_now; |
1631 | mn_now = ev_rt_now; |
1628 | } |
1632 | } |
1629 | } |
1633 | } |
… | |
… | |
1699 | |
1703 | |
1700 | waittime = MAX_BLOCKTIME; |
1704 | waittime = MAX_BLOCKTIME; |
1701 | |
1705 | |
1702 | if (timercnt) |
1706 | if (timercnt) |
1703 | { |
1707 | { |
1704 | ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; |
1708 | ev_tstamp to = ev_at (timers [1]) - mn_now + backend_fudge; |
1705 | if (waittime > to) waittime = to; |
1709 | if (waittime > to) waittime = to; |
1706 | } |
1710 | } |
1707 | |
1711 | |
1708 | #if EV_PERIODIC_ENABLE |
1712 | #if EV_PERIODIC_ENABLE |
1709 | if (periodiccnt) |
1713 | if (periodiccnt) |
1710 | { |
1714 | { |
1711 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; |
1715 | ev_tstamp to = ev_at (periodics [1]) - ev_rt_now + backend_fudge; |
1712 | if (waittime > to) waittime = to; |
1716 | if (waittime > to) waittime = to; |
1713 | } |
1717 | } |
1714 | #endif |
1718 | #endif |
1715 | |
1719 | |
1716 | if (expect_false (waittime < timeout_blocktime)) |
1720 | if (expect_false (waittime < timeout_blocktime)) |
… | |
… | |
1882 | ev_timer_start (EV_P_ ev_timer *w) |
1886 | ev_timer_start (EV_P_ ev_timer *w) |
1883 | { |
1887 | { |
1884 | if (expect_false (ev_is_active (w))) |
1888 | if (expect_false (ev_is_active (w))) |
1885 | return; |
1889 | return; |
1886 | |
1890 | |
1887 | ((WT)w)->at += mn_now; |
1891 | ev_at (w) += mn_now; |
1888 | |
1892 | |
1889 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1893 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1890 | |
1894 | |
1891 | ev_start (EV_A_ (W)w, ++timercnt); |
1895 | ev_start (EV_A_ (W)w, ++timercnt); |
1892 | array_needsize (WT, timers, timermax, timercnt, EMPTY2); |
1896 | array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2); |
1893 | timers [timercnt - 1] = (WT)w; |
1897 | timers [timercnt] = (WT)w; |
1894 | upheap (timers, timercnt - 1); |
1898 | upheap (timers, timercnt); |
1895 | |
1899 | |
1896 | /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ |
1900 | /*assert (("internal timer heap corruption", timers [((W)w)->active] == w));*/ |
1897 | } |
1901 | } |
1898 | |
1902 | |
1899 | void noinline |
1903 | void noinline |
1900 | ev_timer_stop (EV_P_ ev_timer *w) |
1904 | ev_timer_stop (EV_P_ ev_timer *w) |
1901 | { |
1905 | { |
1902 | clear_pending (EV_A_ (W)w); |
1906 | clear_pending (EV_A_ (W)w); |
1903 | if (expect_false (!ev_is_active (w))) |
1907 | if (expect_false (!ev_is_active (w))) |
1904 | return; |
1908 | return; |
1905 | |
1909 | |
1906 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w)); |
1910 | assert (("internal timer heap corruption", timers [((W)w)->active] == (WT)w)); |
1907 | |
1911 | |
1908 | { |
1912 | { |
1909 | int active = ((W)w)->active; |
1913 | int active = ((W)w)->active; |
1910 | |
1914 | |
1911 | if (expect_true (--active < --timercnt)) |
1915 | if (expect_true (active < timercnt)) |
1912 | { |
1916 | { |
1913 | timers [active] = timers [timercnt]; |
1917 | timers [active] = timers [timercnt]; |
1914 | adjustheap (timers, timercnt, active); |
1918 | adjustheap (timers, timercnt, active); |
1915 | } |
1919 | } |
|
|
1920 | |
|
|
1921 | --timercnt; |
1916 | } |
1922 | } |
1917 | |
1923 | |
1918 | ((WT)w)->at -= mn_now; |
1924 | ev_at (w) -= mn_now; |
1919 | |
1925 | |
1920 | ev_stop (EV_A_ (W)w); |
1926 | ev_stop (EV_A_ (W)w); |
1921 | } |
1927 | } |
1922 | |
1928 | |
1923 | void noinline |
1929 | void noinline |
… | |
… | |
1925 | { |
1931 | { |
1926 | if (ev_is_active (w)) |
1932 | if (ev_is_active (w)) |
1927 | { |
1933 | { |
1928 | if (w->repeat) |
1934 | if (w->repeat) |
1929 | { |
1935 | { |
1930 | ((WT)w)->at = mn_now + w->repeat; |
1936 | ev_at (w) = mn_now + w->repeat; |
1931 | adjustheap (timers, timercnt, ((W)w)->active - 1); |
1937 | adjustheap (timers, timercnt, ((W)w)->active); |
1932 | } |
1938 | } |
1933 | else |
1939 | else |
1934 | ev_timer_stop (EV_A_ w); |
1940 | ev_timer_stop (EV_A_ w); |
1935 | } |
1941 | } |
1936 | else if (w->repeat) |
1942 | else if (w->repeat) |
1937 | { |
1943 | { |
1938 | w->at = w->repeat; |
1944 | ev_at (w) = w->repeat; |
1939 | ev_timer_start (EV_A_ w); |
1945 | ev_timer_start (EV_A_ w); |
1940 | } |
1946 | } |
1941 | } |
1947 | } |
1942 | |
1948 | |
1943 | #if EV_PERIODIC_ENABLE |
1949 | #if EV_PERIODIC_ENABLE |
… | |
… | |
1946 | { |
1952 | { |
1947 | if (expect_false (ev_is_active (w))) |
1953 | if (expect_false (ev_is_active (w))) |
1948 | return; |
1954 | return; |
1949 | |
1955 | |
1950 | if (w->reschedule_cb) |
1956 | if (w->reschedule_cb) |
1951 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1957 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
1952 | else if (w->interval) |
1958 | else if (w->interval) |
1953 | { |
1959 | { |
1954 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1960 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1955 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1961 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1956 | ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
1962 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
1957 | } |
1963 | } |
1958 | else |
1964 | else |
1959 | ((WT)w)->at = w->offset; |
1965 | ev_at (w) = w->offset; |
1960 | |
1966 | |
1961 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1967 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1962 | array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2); |
1968 | array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2); |
1963 | periodics [periodiccnt - 1] = (WT)w; |
1969 | periodics [periodiccnt] = (WT)w; |
1964 | upheap (periodics, periodiccnt - 1); |
1970 | upheap (periodics, periodiccnt); |
1965 | |
1971 | |
1966 | /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ |
1972 | /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ |
1967 | } |
1973 | } |
1968 | |
1974 | |
1969 | void noinline |
1975 | void noinline |
… | |
… | |
1971 | { |
1977 | { |
1972 | clear_pending (EV_A_ (W)w); |
1978 | clear_pending (EV_A_ (W)w); |
1973 | if (expect_false (!ev_is_active (w))) |
1979 | if (expect_false (!ev_is_active (w))) |
1974 | return; |
1980 | return; |
1975 | |
1981 | |
1976 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w)); |
1982 | assert (("internal periodic heap corruption", periodics [((W)w)->active] == (WT)w)); |
1977 | |
1983 | |
1978 | { |
1984 | { |
1979 | int active = ((W)w)->active; |
1985 | int active = ((W)w)->active; |
1980 | |
1986 | |
1981 | if (expect_true (--active < --periodiccnt)) |
1987 | if (expect_true (active < periodiccnt)) |
1982 | { |
1988 | { |
1983 | periodics [active] = periodics [periodiccnt]; |
1989 | periodics [active] = periodics [periodiccnt]; |
1984 | adjustheap (periodics, periodiccnt, active); |
1990 | adjustheap (periodics, periodiccnt, active); |
1985 | } |
1991 | } |
|
|
1992 | |
|
|
1993 | --periodiccnt; |
1986 | } |
1994 | } |
1987 | |
1995 | |
1988 | ev_stop (EV_A_ (W)w); |
1996 | ev_stop (EV_A_ (W)w); |
1989 | } |
1997 | } |
1990 | |
1998 | |