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

Comparing libev/ev.c (file contents):
Revision 1.226 by root, Fri Apr 18 17:16:44 2008 UTC vs.
Revision 1.231 by root, Mon May 5 20:47:33 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
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 */
333static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 336static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
334#endif 337#endif
754 } 757 }
755} 758}
756 759
757/*****************************************************************************/ 760/*****************************************************************************/
758 761
762/* towards the root */
759void inline_speed 763void inline_speed
760upheap (WT *heap, int k) 764upheap (WT *heap, int k)
761{ 765{
762 WT w = heap [k]; 766 WT w = heap [k];
763 767
764 while (k) 768 for (;;)
765 { 769 {
766 int p = (k - 1) >> 1; 770 int p = k >> 1;
767 771
772 /* maybe we could use a dummy element at heap [0]? */
768 if (heap [p]->at <= w->at) 773 if (!p || heap [p]->at <= w->at)
769 break; 774 break;
770 775
771 heap [k] = heap [p]; 776 heap [k] = heap [p];
772 ((W)heap [k])->active = k + 1; 777 ev_active (heap [k]) = k;
773 k = p; 778 k = p;
774 } 779 }
775 780
776 heap [k] = w; 781 heap [k] = w;
777 ((W)heap [k])->active = k + 1; 782 ev_active (heap [k]) = k;
778} 783}
779 784
785/* away from the root */
780void inline_speed 786void inline_speed
781downheap (WT *heap, int N, int k) 787downheap (WT *heap, int N, int k)
782{ 788{
783 WT w = heap [k]; 789 WT w = heap [k];
784 790
785 for (;;) 791 for (;;)
786 { 792 {
787 int c = (k << 1) + 1; 793 int c = k << 1;
788 794
789 if (c >= N) 795 if (c > N)
790 break; 796 break;
791 797
792 c += c + 1 < N && heap [c]->at > heap [c + 1]->at 798 c += c < N && heap [c]->at > heap [c + 1]->at
793 ? 1 : 0; 799 ? 1 : 0;
794 800
795 if (w->at <= heap [c]->at) 801 if (w->at <= heap [c]->at)
796 break; 802 break;
797 803
798 heap [k] = heap [c]; 804 heap [k] = heap [c];
799 ((W)heap [k])->active = k + 1; 805 ev_active (heap [k]) = k;
800 806
801 k = c; 807 k = c;
802 } 808 }
803 809
804 heap [k] = w; 810 heap [k] = w;
805 ((W)heap [k])->active = k + 1; 811 ev_active (heap [k]) = k;
806} 812}
807 813
808void inline_size 814void inline_size
809adjustheap (WT *heap, int N, int k) 815adjustheap (WT *heap, int N, int k)
810{ 816{
1456} 1462}
1457 1463
1458void inline_size 1464void inline_size
1459timers_reify (EV_P) 1465timers_reify (EV_P)
1460{ 1466{
1461 while (timercnt && ((WT)timers [0])->at <= mn_now) 1467 while (timercnt && ev_at (timers [1]) <= mn_now)
1462 { 1468 {
1463 ev_timer *w = (ev_timer *)timers [0]; 1469 ev_timer *w = (ev_timer *)timers [1];
1464 1470
1465 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ 1471 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1466 1472
1467 /* first reschedule or stop timer */ 1473 /* first reschedule or stop timer */
1468 if (w->repeat) 1474 if (w->repeat)
1469 { 1475 {
1470 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.));
1471 1477
1472 ((WT)w)->at += w->repeat; 1478 ev_at (w) += w->repeat;
1473 if (((WT)w)->at < mn_now) 1479 if (ev_at (w) < mn_now)
1474 ((WT)w)->at = mn_now; 1480 ev_at (w) = mn_now;
1475 1481
1476 downheap (timers, timercnt, 0); 1482 downheap (timers, timercnt, 1);
1477 } 1483 }
1478 else 1484 else
1479 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1485 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1480 1486
1481 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1487 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1484 1490
1485#if EV_PERIODIC_ENABLE 1491#if EV_PERIODIC_ENABLE
1486void inline_size 1492void inline_size
1487periodics_reify (EV_P) 1493periodics_reify (EV_P)
1488{ 1494{
1489 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1495 while (periodiccnt && ev_at (periodics [1]) <= ev_rt_now)
1490 { 1496 {
1491 ev_periodic *w = (ev_periodic *)periodics [0]; 1497 ev_periodic *w = (ev_periodic *)periodics [1];
1492 1498
1493 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1499 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1494 1500
1495 /* first reschedule or stop timer */ 1501 /* first reschedule or stop timer */
1496 if (w->reschedule_cb) 1502 if (w->reschedule_cb)
1497 { 1503 {
1498 ((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);
1499 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));
1500 downheap (periodics, periodiccnt, 0); 1506 downheap (periodics, periodiccnt, 1);
1501 } 1507 }
1502 else if (w->interval) 1508 else if (w->interval)
1503 { 1509 {
1504 ((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;
1505 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;
1506 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));
1507 downheap (periodics, periodiccnt, 0); 1513 downheap (periodics, periodiccnt, 1);
1508 } 1514 }
1509 else 1515 else
1510 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1516 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1511 1517
1512 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1518 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1517periodics_reschedule (EV_P) 1523periodics_reschedule (EV_P)
1518{ 1524{
1519 int i; 1525 int i;
1520 1526
1521 /* adjust periodics after time jump */ 1527 /* adjust periodics after time jump */
1522 for (i = 0; i < periodiccnt; ++i) 1528 for (i = 1; i <= periodiccnt; ++i)
1523 { 1529 {
1524 ev_periodic *w = (ev_periodic *)periodics [i]; 1530 ev_periodic *w = (ev_periodic *)periodics [i];
1525 1531
1526 if (w->reschedule_cb) 1532 if (w->reschedule_cb)
1527 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1533 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1528 else if (w->interval) 1534 else if (w->interval)
1529 ((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;
1530 } 1536 }
1531 1537
1532 /* now rebuild the heap */ 1538 /* now rebuild the heap */
1533 for (i = periodiccnt >> 1; i--; ) 1539 for (i = periodiccnt >> 1; i--; )
1534 downheap (periodics, periodiccnt, i); 1540 downheap (periodics, periodiccnt, i);
1616 { 1622 {
1617#if EV_PERIODIC_ENABLE 1623#if EV_PERIODIC_ENABLE
1618 periodics_reschedule (EV_A); 1624 periodics_reschedule (EV_A);
1619#endif 1625#endif
1620 /* 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 */
1621 for (i = 0; i < timercnt; ++i) 1627 for (i = 1; i <= timercnt; ++i)
1622 ((WT)timers [i])->at += ev_rt_now - mn_now; 1628 ev_at (timers [i]) += ev_rt_now - mn_now;
1623 } 1629 }
1624 1630
1625 mn_now = ev_rt_now; 1631 mn_now = ev_rt_now;
1626 } 1632 }
1627} 1633}
1697 1703
1698 waittime = MAX_BLOCKTIME; 1704 waittime = MAX_BLOCKTIME;
1699 1705
1700 if (timercnt) 1706 if (timercnt)
1701 { 1707 {
1702 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1708 ev_tstamp to = ev_at (timers [1]) - mn_now + backend_fudge;
1703 if (waittime > to) waittime = to; 1709 if (waittime > to) waittime = to;
1704 } 1710 }
1705 1711
1706#if EV_PERIODIC_ENABLE 1712#if EV_PERIODIC_ENABLE
1707 if (periodiccnt) 1713 if (periodiccnt)
1708 { 1714 {
1709 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;
1710 if (waittime > to) waittime = to; 1716 if (waittime > to) waittime = to;
1711 } 1717 }
1712#endif 1718#endif
1713 1719
1714 if (expect_false (waittime < timeout_blocktime)) 1720 if (expect_false (waittime < timeout_blocktime))
1880ev_timer_start (EV_P_ ev_timer *w) 1886ev_timer_start (EV_P_ ev_timer *w)
1881{ 1887{
1882 if (expect_false (ev_is_active (w))) 1888 if (expect_false (ev_is_active (w)))
1883 return; 1889 return;
1884 1890
1885 ((WT)w)->at += mn_now; 1891 ev_at (w) += mn_now;
1886 1892
1887 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.));
1888 1894
1889 ev_start (EV_A_ (W)w, ++timercnt); 1895 ev_start (EV_A_ (W)w, ++timercnt);
1890 array_needsize (WT, timers, timermax, timercnt, EMPTY2); 1896 array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2);
1891 timers [timercnt - 1] = (WT)w; 1897 timers [timercnt] = (WT)w;
1892 upheap (timers, timercnt - 1); 1898 upheap (timers, timercnt);
1893 1899
1894 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 1900 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/
1895} 1901}
1896 1902
1897void noinline 1903void noinline
1898ev_timer_stop (EV_P_ ev_timer *w) 1904ev_timer_stop (EV_P_ ev_timer *w)
1899{ 1905{
1900 clear_pending (EV_A_ (W)w); 1906 clear_pending (EV_A_ (W)w);
1901 if (expect_false (!ev_is_active (w))) 1907 if (expect_false (!ev_is_active (w)))
1902 return; 1908 return;
1903 1909
1904 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w));
1905
1906 { 1910 {
1907 int active = ((W)w)->active; 1911 int active = ev_active (w);
1908 1912
1913 assert (("internal timer heap corruption", timers [active] == (WT)w));
1914
1909 if (expect_true (--active < --timercnt)) 1915 if (expect_true (active < timercnt))
1910 { 1916 {
1911 timers [active] = timers [timercnt]; 1917 timers [active] = timers [timercnt];
1912 adjustheap (timers, timercnt, active); 1918 adjustheap (timers, timercnt, active);
1913 } 1919 }
1920
1921 --timercnt;
1914 } 1922 }
1915 1923
1916 ((WT)w)->at -= mn_now; 1924 ev_at (w) -= mn_now;
1917 1925
1918 ev_stop (EV_A_ (W)w); 1926 ev_stop (EV_A_ (W)w);
1919} 1927}
1920 1928
1921void noinline 1929void noinline
1923{ 1931{
1924 if (ev_is_active (w)) 1932 if (ev_is_active (w))
1925 { 1933 {
1926 if (w->repeat) 1934 if (w->repeat)
1927 { 1935 {
1928 ((WT)w)->at = mn_now + w->repeat; 1936 ev_at (w) = mn_now + w->repeat;
1929 adjustheap (timers, timercnt, ((W)w)->active - 1); 1937 adjustheap (timers, timercnt, ev_active (w));
1930 } 1938 }
1931 else 1939 else
1932 ev_timer_stop (EV_A_ w); 1940 ev_timer_stop (EV_A_ w);
1933 } 1941 }
1934 else if (w->repeat) 1942 else if (w->repeat)
1935 { 1943 {
1936 w->at = w->repeat; 1944 ev_at (w) = w->repeat;
1937 ev_timer_start (EV_A_ w); 1945 ev_timer_start (EV_A_ w);
1938 } 1946 }
1939} 1947}
1940 1948
1941#if EV_PERIODIC_ENABLE 1949#if EV_PERIODIC_ENABLE
1944{ 1952{
1945 if (expect_false (ev_is_active (w))) 1953 if (expect_false (ev_is_active (w)))
1946 return; 1954 return;
1947 1955
1948 if (w->reschedule_cb) 1956 if (w->reschedule_cb)
1949 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1957 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1950 else if (w->interval) 1958 else if (w->interval)
1951 { 1959 {
1952 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.));
1953 /* 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 */
1954 ((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;
1955 } 1963 }
1956 else 1964 else
1957 ((WT)w)->at = w->offset; 1965 ev_at (w) = w->offset;
1958 1966
1959 ev_start (EV_A_ (W)w, ++periodiccnt); 1967 ev_start (EV_A_ (W)w, ++periodiccnt);
1960 array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2); 1968 array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2);
1961 periodics [periodiccnt - 1] = (WT)w; 1969 periodics [periodiccnt] = (WT)w;
1962 upheap (periodics, periodiccnt - 1); 1970 upheap (periodics, periodiccnt);
1963 1971
1964 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 1972 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/
1965} 1973}
1966 1974
1967void noinline 1975void noinline
1968ev_periodic_stop (EV_P_ ev_periodic *w) 1976ev_periodic_stop (EV_P_ ev_periodic *w)
1969{ 1977{
1970 clear_pending (EV_A_ (W)w); 1978 clear_pending (EV_A_ (W)w);
1971 if (expect_false (!ev_is_active (w))) 1979 if (expect_false (!ev_is_active (w)))
1972 return; 1980 return;
1973 1981
1974 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w));
1975
1976 { 1982 {
1977 int active = ((W)w)->active; 1983 int active = ev_active (w);
1978 1984
1985 assert (("internal periodic heap corruption", periodics [active] == (WT)w));
1986
1979 if (expect_true (--active < --periodiccnt)) 1987 if (expect_true (active < periodiccnt))
1980 { 1988 {
1981 periodics [active] = periodics [periodiccnt]; 1989 periodics [active] = periodics [periodiccnt];
1982 adjustheap (periodics, periodiccnt, active); 1990 adjustheap (periodics, periodiccnt, active);
1983 } 1991 }
1992
1993 --periodiccnt;
1984 } 1994 }
1985 1995
1986 ev_stop (EV_A_ (W)w); 1996 ev_stop (EV_A_ (W)w);
1987} 1997}
1988 1998
2358 clear_pending (EV_A_ (W)w); 2368 clear_pending (EV_A_ (W)w);
2359 if (expect_false (!ev_is_active (w))) 2369 if (expect_false (!ev_is_active (w)))
2360 return; 2370 return;
2361 2371
2362 { 2372 {
2363 int active = ((W)w)->active; 2373 int active = ev_active (w);
2364 2374
2365 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; 2375 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2366 ((W)idles [ABSPRI (w)][active - 1])->active = active; 2376 ev_active (idles [ABSPRI (w)][active - 1]) = active;
2367 2377
2368 ev_stop (EV_A_ (W)w); 2378 ev_stop (EV_A_ (W)w);
2369 --idleall; 2379 --idleall;
2370 } 2380 }
2371} 2381}
2388 clear_pending (EV_A_ (W)w); 2398 clear_pending (EV_A_ (W)w);
2389 if (expect_false (!ev_is_active (w))) 2399 if (expect_false (!ev_is_active (w)))
2390 return; 2400 return;
2391 2401
2392 { 2402 {
2393 int active = ((W)w)->active; 2403 int active = ev_active (w);
2404
2394 prepares [active - 1] = prepares [--preparecnt]; 2405 prepares [active - 1] = prepares [--preparecnt];
2395 ((W)prepares [active - 1])->active = active; 2406 ev_active (prepares [active - 1]) = active;
2396 } 2407 }
2397 2408
2398 ev_stop (EV_A_ (W)w); 2409 ev_stop (EV_A_ (W)w);
2399} 2410}
2400 2411
2415 clear_pending (EV_A_ (W)w); 2426 clear_pending (EV_A_ (W)w);
2416 if (expect_false (!ev_is_active (w))) 2427 if (expect_false (!ev_is_active (w)))
2417 return; 2428 return;
2418 2429
2419 { 2430 {
2420 int active = ((W)w)->active; 2431 int active = ev_active (w);
2432
2421 checks [active - 1] = checks [--checkcnt]; 2433 checks [active - 1] = checks [--checkcnt];
2422 ((W)checks [active - 1])->active = active; 2434 ev_active (checks [active - 1]) = active;
2423 } 2435 }
2424 2436
2425 ev_stop (EV_A_ (W)w); 2437 ev_stop (EV_A_ (W)w);
2426} 2438}
2427 2439
2523 clear_pending (EV_A_ (W)w); 2535 clear_pending (EV_A_ (W)w);
2524 if (expect_false (!ev_is_active (w))) 2536 if (expect_false (!ev_is_active (w)))
2525 return; 2537 return;
2526 2538
2527 { 2539 {
2528 int active = ((W)w)->active; 2540 int active = ev_active (w);
2541
2529 forks [active - 1] = forks [--forkcnt]; 2542 forks [active - 1] = forks [--forkcnt];
2530 ((W)forks [active - 1])->active = active; 2543 ev_active (forks [active - 1]) = active;
2531 } 2544 }
2532 2545
2533 ev_stop (EV_A_ (W)w); 2546 ev_stop (EV_A_ (W)w);
2534} 2547}
2535#endif 2548#endif
2554 clear_pending (EV_A_ (W)w); 2567 clear_pending (EV_A_ (W)w);
2555 if (expect_false (!ev_is_active (w))) 2568 if (expect_false (!ev_is_active (w)))
2556 return; 2569 return;
2557 2570
2558 { 2571 {
2559 int active = ((W)w)->active; 2572 int active = ev_active (w);
2573
2560 asyncs [active - 1] = asyncs [--asynccnt]; 2574 asyncs [active - 1] = asyncs [--asynccnt];
2561 ((W)asyncs [active - 1])->active = active; 2575 ev_active (asyncs [active - 1]) = active;
2562 } 2576 }
2563 2577
2564 ev_stop (EV_A_ (W)w); 2578 ev_stop (EV_A_ (W)w);
2565} 2579}
2566 2580

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines