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.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{
1185 if (!(flags & EVFLAG_NOENV) 1191 if (!(flags & EVFLAG_NOENV)
1186 && !enable_secure () 1192 && !enable_secure ()
1187 && getenv ("LIBEV_FLAGS")) 1193 && getenv ("LIBEV_FLAGS"))
1188 flags = atoi (getenv ("LIBEV_FLAGS")); 1194 flags = atoi (getenv ("LIBEV_FLAGS"));
1189 1195
1190 if (!(flags & 0x0000ffffUL)) 1196 if (!(flags & 0x0000ffffU))
1191 flags |= ev_recommended_backends (); 1197 flags |= ev_recommended_backends ();
1192 1198
1193#if EV_USE_PORT 1199#if EV_USE_PORT
1194 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); 1200 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
1195#endif 1201#endif
1283#endif 1289#endif
1284 1290
1285 backend = 0; 1291 backend = 0;
1286} 1292}
1287 1293
1294#if EV_USE_INOTIFY
1288void inline_size infy_fork (EV_P); 1295void inline_size infy_fork (EV_P);
1296#endif
1289 1297
1290void inline_size 1298void inline_size
1291loop_fork (EV_P) 1299loop_fork (EV_P)
1292{ 1300{
1293#if EV_USE_PORT 1301#if EV_USE_PORT
1454} 1462}
1455 1463
1456void inline_size 1464void inline_size
1457timers_reify (EV_P) 1465timers_reify (EV_P)
1458{ 1466{
1459 while (timercnt && ((WT)timers [0])->at <= mn_now) 1467 while (timercnt && ev_at (timers [1]) <= mn_now)
1460 { 1468 {
1461 ev_timer *w = (ev_timer *)timers [0]; 1469 ev_timer *w = (ev_timer *)timers [1];
1462 1470
1463 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ 1471 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1464 1472
1465 /* first reschedule or stop timer */ 1473 /* first reschedule or stop timer */
1466 if (w->repeat) 1474 if (w->repeat)
1467 { 1475 {
1468 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.));
1469 1477
1470 ((WT)w)->at += w->repeat; 1478 ev_at (w) += w->repeat;
1471 if (((WT)w)->at < mn_now) 1479 if (ev_at (w) < mn_now)
1472 ((WT)w)->at = mn_now; 1480 ev_at (w) = mn_now;
1473 1481
1474 downheap (timers, timercnt, 0); 1482 downheap (timers, timercnt, 1);
1475 } 1483 }
1476 else 1484 else
1477 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1485 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1478 1486
1479 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1487 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1482 1490
1483#if EV_PERIODIC_ENABLE 1491#if EV_PERIODIC_ENABLE
1484void inline_size 1492void inline_size
1485periodics_reify (EV_P) 1493periodics_reify (EV_P)
1486{ 1494{
1487 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1495 while (periodiccnt && ev_at (periodics [1]) <= ev_rt_now)
1488 { 1496 {
1489 ev_periodic *w = (ev_periodic *)periodics [0]; 1497 ev_periodic *w = (ev_periodic *)periodics [1];
1490 1498
1491 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1499 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1492 1500
1493 /* first reschedule or stop timer */ 1501 /* first reschedule or stop timer */
1494 if (w->reschedule_cb) 1502 if (w->reschedule_cb)
1495 { 1503 {
1496 ((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);
1497 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));
1498 downheap (periodics, periodiccnt, 0); 1506 downheap (periodics, periodiccnt, 1);
1499 } 1507 }
1500 else if (w->interval) 1508 else if (w->interval)
1501 { 1509 {
1502 ((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;
1503 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;
1504 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));
1505 downheap (periodics, periodiccnt, 0); 1513 downheap (periodics, periodiccnt, 1);
1506 } 1514 }
1507 else 1515 else
1508 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1516 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1509 1517
1510 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1518 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1515periodics_reschedule (EV_P) 1523periodics_reschedule (EV_P)
1516{ 1524{
1517 int i; 1525 int i;
1518 1526
1519 /* adjust periodics after time jump */ 1527 /* adjust periodics after time jump */
1520 for (i = 0; i < periodiccnt; ++i) 1528 for (i = 1; i <= periodiccnt; ++i)
1521 { 1529 {
1522 ev_periodic *w = (ev_periodic *)periodics [i]; 1530 ev_periodic *w = (ev_periodic *)periodics [i];
1523 1531
1524 if (w->reschedule_cb) 1532 if (w->reschedule_cb)
1525 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1533 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1526 else if (w->interval) 1534 else if (w->interval)
1527 ((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;
1528 } 1536 }
1529 1537
1530 /* now rebuild the heap */ 1538 /* now rebuild the heap */
1531 for (i = periodiccnt >> 1; i--; ) 1539 for (i = periodiccnt >> 1; i--; )
1532 downheap (periodics, periodiccnt, i); 1540 downheap (periodics, periodiccnt, i);
1614 { 1622 {
1615#if EV_PERIODIC_ENABLE 1623#if EV_PERIODIC_ENABLE
1616 periodics_reschedule (EV_A); 1624 periodics_reschedule (EV_A);
1617#endif 1625#endif
1618 /* 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 */
1619 for (i = 0; i < timercnt; ++i) 1627 for (i = 1; i <= timercnt; ++i)
1620 ((WT)timers [i])->at += ev_rt_now - mn_now; 1628 ev_at (timers [i]) += ev_rt_now - mn_now;
1621 } 1629 }
1622 1630
1623 mn_now = ev_rt_now; 1631 mn_now = ev_rt_now;
1624 } 1632 }
1625} 1633}
1695 1703
1696 waittime = MAX_BLOCKTIME; 1704 waittime = MAX_BLOCKTIME;
1697 1705
1698 if (timercnt) 1706 if (timercnt)
1699 { 1707 {
1700 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1708 ev_tstamp to = ev_at (timers [1]) - mn_now + backend_fudge;
1701 if (waittime > to) waittime = to; 1709 if (waittime > to) waittime = to;
1702 } 1710 }
1703 1711
1704#if EV_PERIODIC_ENABLE 1712#if EV_PERIODIC_ENABLE
1705 if (periodiccnt) 1713 if (periodiccnt)
1706 { 1714 {
1707 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;
1708 if (waittime > to) waittime = to; 1716 if (waittime > to) waittime = to;
1709 } 1717 }
1710#endif 1718#endif
1711 1719
1712 if (expect_false (waittime < timeout_blocktime)) 1720 if (expect_false (waittime < timeout_blocktime))
1878ev_timer_start (EV_P_ ev_timer *w) 1886ev_timer_start (EV_P_ ev_timer *w)
1879{ 1887{
1880 if (expect_false (ev_is_active (w))) 1888 if (expect_false (ev_is_active (w)))
1881 return; 1889 return;
1882 1890
1883 ((WT)w)->at += mn_now; 1891 ev_at (w) += mn_now;
1884 1892
1885 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.));
1886 1894
1887 ev_start (EV_A_ (W)w, ++timercnt); 1895 ev_start (EV_A_ (W)w, ++timercnt);
1888 array_needsize (WT, timers, timermax, timercnt, EMPTY2); 1896 array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2);
1889 timers [timercnt - 1] = (WT)w; 1897 timers [timercnt] = (WT)w;
1890 upheap (timers, timercnt - 1); 1898 upheap (timers, timercnt);
1891 1899
1892 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 1900 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/
1893} 1901}
1894 1902
1895void noinline 1903void noinline
1896ev_timer_stop (EV_P_ ev_timer *w) 1904ev_timer_stop (EV_P_ ev_timer *w)
1897{ 1905{
1898 clear_pending (EV_A_ (W)w); 1906 clear_pending (EV_A_ (W)w);
1899 if (expect_false (!ev_is_active (w))) 1907 if (expect_false (!ev_is_active (w)))
1900 return; 1908 return;
1901 1909
1902 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w));
1903
1904 { 1910 {
1905 int active = ((W)w)->active; 1911 int active = ev_active (w);
1906 1912
1913 assert (("internal timer heap corruption", timers [active] == (WT)w));
1914
1907 if (expect_true (--active < --timercnt)) 1915 if (expect_true (active < timercnt))
1908 { 1916 {
1909 timers [active] = timers [timercnt]; 1917 timers [active] = timers [timercnt];
1910 adjustheap (timers, timercnt, active); 1918 adjustheap (timers, timercnt, active);
1911 } 1919 }
1920
1921 --timercnt;
1912 } 1922 }
1913 1923
1914 ((WT)w)->at -= mn_now; 1924 ev_at (w) -= mn_now;
1915 1925
1916 ev_stop (EV_A_ (W)w); 1926 ev_stop (EV_A_ (W)w);
1917} 1927}
1918 1928
1919void noinline 1929void noinline
1921{ 1931{
1922 if (ev_is_active (w)) 1932 if (ev_is_active (w))
1923 { 1933 {
1924 if (w->repeat) 1934 if (w->repeat)
1925 { 1935 {
1926 ((WT)w)->at = mn_now + w->repeat; 1936 ev_at (w) = mn_now + w->repeat;
1927 adjustheap (timers, timercnt, ((W)w)->active - 1); 1937 adjustheap (timers, timercnt, ev_active (w));
1928 } 1938 }
1929 else 1939 else
1930 ev_timer_stop (EV_A_ w); 1940 ev_timer_stop (EV_A_ w);
1931 } 1941 }
1932 else if (w->repeat) 1942 else if (w->repeat)
1933 { 1943 {
1934 w->at = w->repeat; 1944 ev_at (w) = w->repeat;
1935 ev_timer_start (EV_A_ w); 1945 ev_timer_start (EV_A_ w);
1936 } 1946 }
1937} 1947}
1938 1948
1939#if EV_PERIODIC_ENABLE 1949#if EV_PERIODIC_ENABLE
1942{ 1952{
1943 if (expect_false (ev_is_active (w))) 1953 if (expect_false (ev_is_active (w)))
1944 return; 1954 return;
1945 1955
1946 if (w->reschedule_cb) 1956 if (w->reschedule_cb)
1947 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1957 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1948 else if (w->interval) 1958 else if (w->interval)
1949 { 1959 {
1950 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.));
1951 /* 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 */
1952 ((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;
1953 } 1963 }
1954 else 1964 else
1955 ((WT)w)->at = w->offset; 1965 ev_at (w) = w->offset;
1956 1966
1957 ev_start (EV_A_ (W)w, ++periodiccnt); 1967 ev_start (EV_A_ (W)w, ++periodiccnt);
1958 array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2); 1968 array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2);
1959 periodics [periodiccnt - 1] = (WT)w; 1969 periodics [periodiccnt] = (WT)w;
1960 upheap (periodics, periodiccnt - 1); 1970 upheap (periodics, periodiccnt);
1961 1971
1962 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 1972 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/
1963} 1973}
1964 1974
1965void noinline 1975void noinline
1966ev_periodic_stop (EV_P_ ev_periodic *w) 1976ev_periodic_stop (EV_P_ ev_periodic *w)
1967{ 1977{
1968 clear_pending (EV_A_ (W)w); 1978 clear_pending (EV_A_ (W)w);
1969 if (expect_false (!ev_is_active (w))) 1979 if (expect_false (!ev_is_active (w)))
1970 return; 1980 return;
1971 1981
1972 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w));
1973
1974 { 1982 {
1975 int active = ((W)w)->active; 1983 int active = ev_active (w);
1976 1984
1985 assert (("internal periodic heap corruption", periodics [active] == (WT)w));
1986
1977 if (expect_true (--active < --periodiccnt)) 1987 if (expect_true (active < periodiccnt))
1978 { 1988 {
1979 periodics [active] = periodics [periodiccnt]; 1989 periodics [active] = periodics [periodiccnt];
1980 adjustheap (periodics, periodiccnt, active); 1990 adjustheap (periodics, periodiccnt, active);
1981 } 1991 }
1992
1993 --periodiccnt;
1982 } 1994 }
1983 1995
1984 ev_stop (EV_A_ (W)w); 1996 ev_stop (EV_A_ (W)w);
1985} 1997}
1986 1998
2356 clear_pending (EV_A_ (W)w); 2368 clear_pending (EV_A_ (W)w);
2357 if (expect_false (!ev_is_active (w))) 2369 if (expect_false (!ev_is_active (w)))
2358 return; 2370 return;
2359 2371
2360 { 2372 {
2361 int active = ((W)w)->active; 2373 int active = ev_active (w);
2362 2374
2363 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; 2375 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2364 ((W)idles [ABSPRI (w)][active - 1])->active = active; 2376 ev_active (idles [ABSPRI (w)][active - 1]) = active;
2365 2377
2366 ev_stop (EV_A_ (W)w); 2378 ev_stop (EV_A_ (W)w);
2367 --idleall; 2379 --idleall;
2368 } 2380 }
2369} 2381}
2386 clear_pending (EV_A_ (W)w); 2398 clear_pending (EV_A_ (W)w);
2387 if (expect_false (!ev_is_active (w))) 2399 if (expect_false (!ev_is_active (w)))
2388 return; 2400 return;
2389 2401
2390 { 2402 {
2391 int active = ((W)w)->active; 2403 int active = ev_active (w);
2404
2392 prepares [active - 1] = prepares [--preparecnt]; 2405 prepares [active - 1] = prepares [--preparecnt];
2393 ((W)prepares [active - 1])->active = active; 2406 ev_active (prepares [active - 1]) = active;
2394 } 2407 }
2395 2408
2396 ev_stop (EV_A_ (W)w); 2409 ev_stop (EV_A_ (W)w);
2397} 2410}
2398 2411
2413 clear_pending (EV_A_ (W)w); 2426 clear_pending (EV_A_ (W)w);
2414 if (expect_false (!ev_is_active (w))) 2427 if (expect_false (!ev_is_active (w)))
2415 return; 2428 return;
2416 2429
2417 { 2430 {
2418 int active = ((W)w)->active; 2431 int active = ev_active (w);
2432
2419 checks [active - 1] = checks [--checkcnt]; 2433 checks [active - 1] = checks [--checkcnt];
2420 ((W)checks [active - 1])->active = active; 2434 ev_active (checks [active - 1]) = active;
2421 } 2435 }
2422 2436
2423 ev_stop (EV_A_ (W)w); 2437 ev_stop (EV_A_ (W)w);
2424} 2438}
2425 2439
2521 clear_pending (EV_A_ (W)w); 2535 clear_pending (EV_A_ (W)w);
2522 if (expect_false (!ev_is_active (w))) 2536 if (expect_false (!ev_is_active (w)))
2523 return; 2537 return;
2524 2538
2525 { 2539 {
2526 int active = ((W)w)->active; 2540 int active = ev_active (w);
2541
2527 forks [active - 1] = forks [--forkcnt]; 2542 forks [active - 1] = forks [--forkcnt];
2528 ((W)forks [active - 1])->active = active; 2543 ev_active (forks [active - 1]) = active;
2529 } 2544 }
2530 2545
2531 ev_stop (EV_A_ (W)w); 2546 ev_stop (EV_A_ (W)w);
2532} 2547}
2533#endif 2548#endif
2552 clear_pending (EV_A_ (W)w); 2567 clear_pending (EV_A_ (W)w);
2553 if (expect_false (!ev_is_active (w))) 2568 if (expect_false (!ev_is_active (w)))
2554 return; 2569 return;
2555 2570
2556 { 2571 {
2557 int active = ((W)w)->active; 2572 int active = ev_active (w);
2573
2558 asyncs [active - 1] = asyncs [--asynccnt]; 2574 asyncs [active - 1] = asyncs [--asynccnt];
2559 ((W)asyncs [active - 1])->active = active; 2575 ev_active (asyncs [active - 1]) = active;
2560 } 2576 }
2561 2577
2562 ev_stop (EV_A_ (W)w); 2578 ev_stop (EV_A_ (W)w);
2563} 2579}
2564 2580

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines