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

Comparing libev/ev.c (file contents):
Revision 1.224 by root, Wed Apr 9 22:07:50 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
754 } 756 }
755} 757}
756 758
757/*****************************************************************************/ 759/*****************************************************************************/
758 760
761/* towards the root */
759void inline_speed 762void inline_speed
760upheap (WT *heap, int k) 763upheap (WT *heap, int k)
761{ 764{
762 WT w = heap [k]; 765 WT w = heap [k];
763 766
764 while (k) 767 for (;;)
765 { 768 {
766 int p = (k - 1) >> 1; 769 int p = k >> 1;
767 770
771 /* maybe we could use a dummy element at heap [0]? */
768 if (heap [p]->at <= w->at) 772 if (!p || heap [p]->at <= w->at)
769 break; 773 break;
770 774
771 heap [k] = heap [p]; 775 heap [k] = heap [p];
772 ((W)heap [k])->active = k + 1; 776 ((W)heap [k])->active = k;
773 k = p; 777 k = p;
774 } 778 }
775 779
776 heap [k] = w; 780 heap [k] = w;
777 ((W)heap [k])->active = k + 1; 781 ((W)heap [k])->active = k;
778} 782}
779 783
784/* away from the root */
780void inline_speed 785void inline_speed
781downheap (WT *heap, int N, int k) 786downheap (WT *heap, int N, int k)
782{ 787{
783 WT w = heap [k]; 788 WT w = heap [k];
784 789
785 for (;;) 790 for (;;)
786 { 791 {
787 int c = (k << 1) + 1; 792 int c = k << 1;
788 793
789 if (c >= N) 794 if (c > N)
790 break; 795 break;
791 796
792 c += c + 1 < N && heap [c]->at > heap [c + 1]->at 797 c += c < N && heap [c]->at > heap [c + 1]->at
793 ? 1 : 0; 798 ? 1 : 0;
794 799
795 if (w->at <= heap [c]->at) 800 if (w->at <= heap [c]->at)
796 break; 801 break;
797 802
798 heap [k] = heap [c]; 803 heap [k] = heap [c];
799 ((W)heap [k])->active = k + 1; 804 ((W)heap [k])->active = k;
800 805
801 k = c; 806 k = c;
802 } 807 }
803 808
804 heap [k] = w; 809 heap [k] = w;
805 ((W)heap [k])->active = k + 1; 810 ((W)heap [k])->active = k;
806} 811}
807 812
808void inline_size 813void inline_size
809adjustheap (WT *heap, int N, int k) 814adjustheap (WT *heap, int N, int k)
810{ 815{
1185 if (!(flags & EVFLAG_NOENV) 1190 if (!(flags & EVFLAG_NOENV)
1186 && !enable_secure () 1191 && !enable_secure ()
1187 && getenv ("LIBEV_FLAGS")) 1192 && getenv ("LIBEV_FLAGS"))
1188 flags = atoi (getenv ("LIBEV_FLAGS")); 1193 flags = atoi (getenv ("LIBEV_FLAGS"));
1189 1194
1190 if (!(flags & 0x0000ffffUL)) 1195 if (!(flags & 0x0000ffffU))
1191 flags |= ev_recommended_backends (); 1196 flags |= ev_recommended_backends ();
1192 1197
1193#if EV_USE_PORT 1198#if EV_USE_PORT
1194 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); 1199 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
1195#endif 1200#endif
1283#endif 1288#endif
1284 1289
1285 backend = 0; 1290 backend = 0;
1286} 1291}
1287 1292
1293#if EV_USE_INOTIFY
1288void inline_size infy_fork (EV_P); 1294void inline_size infy_fork (EV_P);
1295#endif
1289 1296
1290void inline_size 1297void inline_size
1291loop_fork (EV_P) 1298loop_fork (EV_P)
1292{ 1299{
1293#if EV_USE_PORT 1300#if EV_USE_PORT
1454} 1461}
1455 1462
1456void inline_size 1463void inline_size
1457timers_reify (EV_P) 1464timers_reify (EV_P)
1458{ 1465{
1459 while (timercnt && ((WT)timers [0])->at <= mn_now) 1466 while (timercnt && ev_at (timers [1]) <= mn_now)
1460 { 1467 {
1461 ev_timer *w = (ev_timer *)timers [0]; 1468 ev_timer *w = (ev_timer *)timers [1];
1462 1469
1463 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ 1470 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1464 1471
1465 /* first reschedule or stop timer */ 1472 /* first reschedule or stop timer */
1466 if (w->repeat) 1473 if (w->repeat)
1467 { 1474 {
1468 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.));
1469 1476
1470 ((WT)w)->at += w->repeat; 1477 ev_at (w) += w->repeat;
1471 if (((WT)w)->at < mn_now) 1478 if (ev_at (w) < mn_now)
1472 ((WT)w)->at = mn_now; 1479 ev_at (w) = mn_now;
1473 1480
1474 downheap (timers, timercnt, 0); 1481 downheap (timers, timercnt, 1);
1475 } 1482 }
1476 else 1483 else
1477 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1484 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1478 1485
1479 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1486 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1482 1489
1483#if EV_PERIODIC_ENABLE 1490#if EV_PERIODIC_ENABLE
1484void inline_size 1491void inline_size
1485periodics_reify (EV_P) 1492periodics_reify (EV_P)
1486{ 1493{
1487 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1494 while (periodiccnt && ev_at (periodics [1]) <= ev_rt_now)
1488 { 1495 {
1489 ev_periodic *w = (ev_periodic *)periodics [0]; 1496 ev_periodic *w = (ev_periodic *)periodics [1];
1490 1497
1491 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1498 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1492 1499
1493 /* first reschedule or stop timer */ 1500 /* first reschedule or stop timer */
1494 if (w->reschedule_cb) 1501 if (w->reschedule_cb)
1495 { 1502 {
1496 ((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);
1497 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));
1498 downheap (periodics, periodiccnt, 0); 1505 downheap (periodics, periodiccnt, 1);
1499 } 1506 }
1500 else if (w->interval) 1507 else if (w->interval)
1501 { 1508 {
1502 ((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;
1503 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;
1504 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));
1505 downheap (periodics, periodiccnt, 0); 1512 downheap (periodics, periodiccnt, 1);
1506 } 1513 }
1507 else 1514 else
1508 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1515 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1509 1516
1510 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1517 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1520 for (i = 0; i < periodiccnt; ++i) 1527 for (i = 0; i < periodiccnt; ++i)
1521 { 1528 {
1522 ev_periodic *w = (ev_periodic *)periodics [i]; 1529 ev_periodic *w = (ev_periodic *)periodics [i];
1523 1530
1524 if (w->reschedule_cb) 1531 if (w->reschedule_cb)
1525 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1532 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1526 else if (w->interval) 1533 else if (w->interval)
1527 ((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;
1528 } 1535 }
1529 1536
1530 /* now rebuild the heap */ 1537 /* now rebuild the heap */
1531 for (i = periodiccnt >> 1; i--; ) 1538 for (i = periodiccnt >> 1; i--; )
1532 downheap (periodics, periodiccnt, i); 1539 downheap (periodics, periodiccnt, i);
1614 { 1621 {
1615#if EV_PERIODIC_ENABLE 1622#if EV_PERIODIC_ENABLE
1616 periodics_reschedule (EV_A); 1623 periodics_reschedule (EV_A);
1617#endif 1624#endif
1618 /* 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 */
1619 for (i = 0; i < timercnt; ++i) 1626 for (i = 1; i <= timercnt; ++i)
1620 ((WT)timers [i])->at += ev_rt_now - mn_now; 1627 ev_at (timers [i]) += ev_rt_now - mn_now;
1621 } 1628 }
1622 1629
1623 mn_now = ev_rt_now; 1630 mn_now = ev_rt_now;
1624 } 1631 }
1625} 1632}
1695 1702
1696 waittime = MAX_BLOCKTIME; 1703 waittime = MAX_BLOCKTIME;
1697 1704
1698 if (timercnt) 1705 if (timercnt)
1699 { 1706 {
1700 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1707 ev_tstamp to = ev_at (timers [1]) - mn_now + backend_fudge;
1701 if (waittime > to) waittime = to; 1708 if (waittime > to) waittime = to;
1702 } 1709 }
1703 1710
1704#if EV_PERIODIC_ENABLE 1711#if EV_PERIODIC_ENABLE
1705 if (periodiccnt) 1712 if (periodiccnt)
1706 { 1713 {
1707 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;
1708 if (waittime > to) waittime = to; 1715 if (waittime > to) waittime = to;
1709 } 1716 }
1710#endif 1717#endif
1711 1718
1712 if (expect_false (waittime < timeout_blocktime)) 1719 if (expect_false (waittime < timeout_blocktime))
1878ev_timer_start (EV_P_ ev_timer *w) 1885ev_timer_start (EV_P_ ev_timer *w)
1879{ 1886{
1880 if (expect_false (ev_is_active (w))) 1887 if (expect_false (ev_is_active (w)))
1881 return; 1888 return;
1882 1889
1883 ((WT)w)->at += mn_now; 1890 ev_at (w) += mn_now;
1884 1891
1885 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.));
1886 1893
1887 ev_start (EV_A_ (W)w, ++timercnt); 1894 ev_start (EV_A_ (W)w, ++timercnt);
1888 array_needsize (WT, timers, timermax, timercnt, EMPTY2); 1895 array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2);
1889 timers [timercnt - 1] = (WT)w; 1896 timers [timercnt] = (WT)w;
1890 upheap (timers, timercnt - 1); 1897 upheap (timers, timercnt);
1891 1898
1892 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 1899 /*assert (("internal timer heap corruption", timers [((W)w)->active] == w));*/
1893} 1900}
1894 1901
1895void noinline 1902void noinline
1896ev_timer_stop (EV_P_ ev_timer *w) 1903ev_timer_stop (EV_P_ ev_timer *w)
1897{ 1904{
1898 clear_pending (EV_A_ (W)w); 1905 clear_pending (EV_A_ (W)w);
1899 if (expect_false (!ev_is_active (w))) 1906 if (expect_false (!ev_is_active (w)))
1900 return; 1907 return;
1901 1908
1902 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w)); 1909 assert (("internal timer heap corruption", timers [((W)w)->active] == (WT)w));
1903 1910
1904 { 1911 {
1905 int active = ((W)w)->active; 1912 int active = ((W)w)->active;
1906 1913
1907 if (expect_true (--active < --timercnt)) 1914 if (expect_true (active < timercnt))
1908 { 1915 {
1909 timers [active] = timers [timercnt]; 1916 timers [active] = timers [timercnt];
1910 adjustheap (timers, timercnt, active); 1917 adjustheap (timers, timercnt, active);
1911 } 1918 }
1919
1920 --timercnt;
1912 } 1921 }
1913 1922
1914 ((WT)w)->at -= mn_now; 1923 ev_at (w) -= mn_now;
1915 1924
1916 ev_stop (EV_A_ (W)w); 1925 ev_stop (EV_A_ (W)w);
1917} 1926}
1918 1927
1919void noinline 1928void noinline
1921{ 1930{
1922 if (ev_is_active (w)) 1931 if (ev_is_active (w))
1923 { 1932 {
1924 if (w->repeat) 1933 if (w->repeat)
1925 { 1934 {
1926 ((WT)w)->at = mn_now + w->repeat; 1935 ev_at (w) = mn_now + w->repeat;
1927 adjustheap (timers, timercnt, ((W)w)->active - 1); 1936 adjustheap (timers, timercnt, ((W)w)->active);
1928 } 1937 }
1929 else 1938 else
1930 ev_timer_stop (EV_A_ w); 1939 ev_timer_stop (EV_A_ w);
1931 } 1940 }
1932 else if (w->repeat) 1941 else if (w->repeat)
1942{ 1951{
1943 if (expect_false (ev_is_active (w))) 1952 if (expect_false (ev_is_active (w)))
1944 return; 1953 return;
1945 1954
1946 if (w->reschedule_cb) 1955 if (w->reschedule_cb)
1947 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1956 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1948 else if (w->interval) 1957 else if (w->interval)
1949 { 1958 {
1950 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.));
1951 /* 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 */
1952 ((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;
1953 } 1962 }
1954 else 1963 else
1955 ((WT)w)->at = w->offset; 1964 ev_at (w) = w->offset;
1956 1965
1957 ev_start (EV_A_ (W)w, ++periodiccnt); 1966 ev_start (EV_A_ (W)w, ++periodiccnt);
1958 array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2); 1967 array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2);
1959 periodics [periodiccnt - 1] = (WT)w; 1968 periodics [periodiccnt] = (WT)w;
1960 upheap (periodics, periodiccnt - 1); 1969 upheap (periodics, periodiccnt);
1961 1970
1962 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 1971 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/
1963} 1972}
1964 1973
1965void noinline 1974void noinline
1967{ 1976{
1968 clear_pending (EV_A_ (W)w); 1977 clear_pending (EV_A_ (W)w);
1969 if (expect_false (!ev_is_active (w))) 1978 if (expect_false (!ev_is_active (w)))
1970 return; 1979 return;
1971 1980
1972 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w)); 1981 assert (("internal periodic heap corruption", periodics [((W)w)->active] == (WT)w));
1973 1982
1974 { 1983 {
1975 int active = ((W)w)->active; 1984 int active = ((W)w)->active;
1976 1985
1977 if (expect_true (--active < --periodiccnt)) 1986 if (expect_true (active < periodiccnt))
1978 { 1987 {
1979 periodics [active] = periodics [periodiccnt]; 1988 periodics [active] = periodics [periodiccnt];
1980 adjustheap (periodics, periodiccnt, active); 1989 adjustheap (periodics, periodiccnt, active);
1981 } 1990 }
1991
1992 --periodiccnt;
1982 } 1993 }
1983 1994
1984 ev_stop (EV_A_ (W)w); 1995 ev_stop (EV_A_ (W)w);
1985} 1996}
1986 1997

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines