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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines