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

Comparing libev/ev.c (file contents):
Revision 1.223 by root, Sun Apr 6 14:34: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
360 perror (msg); 362 perror (msg);
361 abort (); 363 abort ();
362 } 364 }
363} 365}
364 366
367static void *
368ev_realloc_emul (void *ptr, long size)
369{
370 /* some systems, notably openbsd and darwin, fail to properly
371 * implement realloc (x, 0) (as required by both ansi c-98 and
372 * the single unix specification, so work around them here.
373 */
374
375 if (size)
376 return realloc (ptr, size);
377
378 free (ptr);
379 return 0;
380}
381
365static void *(*alloc)(void *ptr, long size); 382static void *(*alloc)(void *ptr, long size) = ev_realloc_emul;
366 383
367void 384void
368ev_set_allocator (void *(*cb)(void *ptr, long size)) 385ev_set_allocator (void *(*cb)(void *ptr, long size))
369{ 386{
370 alloc = cb; 387 alloc = cb;
371} 388}
372 389
373inline_speed void * 390inline_speed void *
374ev_realloc (void *ptr, long size) 391ev_realloc (void *ptr, long size)
375{ 392{
376 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); 393 ptr = alloc (ptr, size);
377 394
378 if (!ptr && size) 395 if (!ptr && size)
379 { 396 {
380 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); 397 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
381 abort (); 398 abort ();
739 } 756 }
740} 757}
741 758
742/*****************************************************************************/ 759/*****************************************************************************/
743 760
761/* towards the root */
744void inline_speed 762void inline_speed
745upheap (WT *heap, int k) 763upheap (WT *heap, int k)
746{ 764{
747 WT w = heap [k]; 765 WT w = heap [k];
748 766
749 while (k) 767 for (;;)
750 { 768 {
751 int p = (k - 1) >> 1; 769 int p = k >> 1;
752 770
771 /* maybe we could use a dummy element at heap [0]? */
753 if (heap [p]->at <= w->at) 772 if (!p || heap [p]->at <= w->at)
754 break; 773 break;
755 774
756 heap [k] = heap [p]; 775 heap [k] = heap [p];
757 ((W)heap [k])->active = k + 1; 776 ((W)heap [k])->active = k;
758 k = p; 777 k = p;
759 } 778 }
760 779
761 heap [k] = w; 780 heap [k] = w;
762 ((W)heap [k])->active = k + 1; 781 ((W)heap [k])->active = k;
763} 782}
764 783
784/* away from the root */
765void inline_speed 785void inline_speed
766downheap (WT *heap, int N, int k) 786downheap (WT *heap, int N, int k)
767{ 787{
768 WT w = heap [k]; 788 WT w = heap [k];
769 789
770 for (;;) 790 for (;;)
771 { 791 {
772 int c = (k << 1) + 1; 792 int c = k << 1;
773 793
774 if (c >= N) 794 if (c > N)
775 break; 795 break;
776 796
777 c += c + 1 < N && heap [c]->at > heap [c + 1]->at 797 c += c < N && heap [c]->at > heap [c + 1]->at
778 ? 1 : 0; 798 ? 1 : 0;
779 799
780 if (w->at <= heap [c]->at) 800 if (w->at <= heap [c]->at)
781 break; 801 break;
782 802
783 heap [k] = heap [c]; 803 heap [k] = heap [c];
784 ((W)heap [k])->active = k + 1; 804 ((W)heap [k])->active = k;
785 805
786 k = c; 806 k = c;
787 } 807 }
788 808
789 heap [k] = w; 809 heap [k] = w;
790 ((W)heap [k])->active = k + 1; 810 ((W)heap [k])->active = k;
791} 811}
792 812
793void inline_size 813void inline_size
794adjustheap (WT *heap, int N, int k) 814adjustheap (WT *heap, int N, int k)
795{ 815{
1170 if (!(flags & EVFLAG_NOENV) 1190 if (!(flags & EVFLAG_NOENV)
1171 && !enable_secure () 1191 && !enable_secure ()
1172 && getenv ("LIBEV_FLAGS")) 1192 && getenv ("LIBEV_FLAGS"))
1173 flags = atoi (getenv ("LIBEV_FLAGS")); 1193 flags = atoi (getenv ("LIBEV_FLAGS"));
1174 1194
1175 if (!(flags & 0x0000ffffUL)) 1195 if (!(flags & 0x0000ffffU))
1176 flags |= ev_recommended_backends (); 1196 flags |= ev_recommended_backends ();
1177 1197
1178#if EV_USE_PORT 1198#if EV_USE_PORT
1179 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); 1199 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
1180#endif 1200#endif
1268#endif 1288#endif
1269 1289
1270 backend = 0; 1290 backend = 0;
1271} 1291}
1272 1292
1293#if EV_USE_INOTIFY
1273void inline_size infy_fork (EV_P); 1294void inline_size infy_fork (EV_P);
1295#endif
1274 1296
1275void inline_size 1297void inline_size
1276loop_fork (EV_P) 1298loop_fork (EV_P)
1277{ 1299{
1278#if EV_USE_PORT 1300#if EV_USE_PORT
1439} 1461}
1440 1462
1441void inline_size 1463void inline_size
1442timers_reify (EV_P) 1464timers_reify (EV_P)
1443{ 1465{
1444 while (timercnt && ((WT)timers [0])->at <= mn_now) 1466 while (timercnt && ev_at (timers [1]) <= mn_now)
1445 { 1467 {
1446 ev_timer *w = (ev_timer *)timers [0]; 1468 ev_timer *w = (ev_timer *)timers [1];
1447 1469
1448 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ 1470 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1449 1471
1450 /* first reschedule or stop timer */ 1472 /* first reschedule or stop timer */
1451 if (w->repeat) 1473 if (w->repeat)
1452 { 1474 {
1453 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.));
1454 1476
1455 ((WT)w)->at += w->repeat; 1477 ev_at (w) += w->repeat;
1456 if (((WT)w)->at < mn_now) 1478 if (ev_at (w) < mn_now)
1457 ((WT)w)->at = mn_now; 1479 ev_at (w) = mn_now;
1458 1480
1459 downheap (timers, timercnt, 0); 1481 downheap (timers, timercnt, 1);
1460 } 1482 }
1461 else 1483 else
1462 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1484 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1463 1485
1464 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1486 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1467 1489
1468#if EV_PERIODIC_ENABLE 1490#if EV_PERIODIC_ENABLE
1469void inline_size 1491void inline_size
1470periodics_reify (EV_P) 1492periodics_reify (EV_P)
1471{ 1493{
1472 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1494 while (periodiccnt && ev_at (periodics [1]) <= ev_rt_now)
1473 { 1495 {
1474 ev_periodic *w = (ev_periodic *)periodics [0]; 1496 ev_periodic *w = (ev_periodic *)periodics [1];
1475 1497
1476 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1498 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1477 1499
1478 /* first reschedule or stop timer */ 1500 /* first reschedule or stop timer */
1479 if (w->reschedule_cb) 1501 if (w->reschedule_cb)
1480 { 1502 {
1481 ((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);
1482 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));
1483 downheap (periodics, periodiccnt, 0); 1505 downheap (periodics, periodiccnt, 1);
1484 } 1506 }
1485 else if (w->interval) 1507 else if (w->interval)
1486 { 1508 {
1487 ((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;
1488 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;
1489 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));
1490 downheap (periodics, periodiccnt, 0); 1512 downheap (periodics, periodiccnt, 1);
1491 } 1513 }
1492 else 1514 else
1493 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1515 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1494 1516
1495 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1517 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1505 for (i = 0; i < periodiccnt; ++i) 1527 for (i = 0; i < periodiccnt; ++i)
1506 { 1528 {
1507 ev_periodic *w = (ev_periodic *)periodics [i]; 1529 ev_periodic *w = (ev_periodic *)periodics [i];
1508 1530
1509 if (w->reschedule_cb) 1531 if (w->reschedule_cb)
1510 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1532 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1511 else if (w->interval) 1533 else if (w->interval)
1512 ((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;
1513 } 1535 }
1514 1536
1515 /* now rebuild the heap */ 1537 /* now rebuild the heap */
1516 for (i = periodiccnt >> 1; i--; ) 1538 for (i = periodiccnt >> 1; i--; )
1517 downheap (periodics, periodiccnt, i); 1539 downheap (periodics, periodiccnt, i);
1599 { 1621 {
1600#if EV_PERIODIC_ENABLE 1622#if EV_PERIODIC_ENABLE
1601 periodics_reschedule (EV_A); 1623 periodics_reschedule (EV_A);
1602#endif 1624#endif
1603 /* 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 */
1604 for (i = 0; i < timercnt; ++i) 1626 for (i = 1; i <= timercnt; ++i)
1605 ((WT)timers [i])->at += ev_rt_now - mn_now; 1627 ev_at (timers [i]) += ev_rt_now - mn_now;
1606 } 1628 }
1607 1629
1608 mn_now = ev_rt_now; 1630 mn_now = ev_rt_now;
1609 } 1631 }
1610} 1632}
1680 1702
1681 waittime = MAX_BLOCKTIME; 1703 waittime = MAX_BLOCKTIME;
1682 1704
1683 if (timercnt) 1705 if (timercnt)
1684 { 1706 {
1685 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1707 ev_tstamp to = ev_at (timers [1]) - mn_now + backend_fudge;
1686 if (waittime > to) waittime = to; 1708 if (waittime > to) waittime = to;
1687 } 1709 }
1688 1710
1689#if EV_PERIODIC_ENABLE 1711#if EV_PERIODIC_ENABLE
1690 if (periodiccnt) 1712 if (periodiccnt)
1691 { 1713 {
1692 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;
1693 if (waittime > to) waittime = to; 1715 if (waittime > to) waittime = to;
1694 } 1716 }
1695#endif 1717#endif
1696 1718
1697 if (expect_false (waittime < timeout_blocktime)) 1719 if (expect_false (waittime < timeout_blocktime))
1863ev_timer_start (EV_P_ ev_timer *w) 1885ev_timer_start (EV_P_ ev_timer *w)
1864{ 1886{
1865 if (expect_false (ev_is_active (w))) 1887 if (expect_false (ev_is_active (w)))
1866 return; 1888 return;
1867 1889
1868 ((WT)w)->at += mn_now; 1890 ev_at (w) += mn_now;
1869 1891
1870 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.));
1871 1893
1872 ev_start (EV_A_ (W)w, ++timercnt); 1894 ev_start (EV_A_ (W)w, ++timercnt);
1873 array_needsize (WT, timers, timermax, timercnt, EMPTY2); 1895 array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2);
1874 timers [timercnt - 1] = (WT)w; 1896 timers [timercnt] = (WT)w;
1875 upheap (timers, timercnt - 1); 1897 upheap (timers, timercnt);
1876 1898
1877 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 1899 /*assert (("internal timer heap corruption", timers [((W)w)->active] == w));*/
1878} 1900}
1879 1901
1880void noinline 1902void noinline
1881ev_timer_stop (EV_P_ ev_timer *w) 1903ev_timer_stop (EV_P_ ev_timer *w)
1882{ 1904{
1883 clear_pending (EV_A_ (W)w); 1905 clear_pending (EV_A_ (W)w);
1884 if (expect_false (!ev_is_active (w))) 1906 if (expect_false (!ev_is_active (w)))
1885 return; 1907 return;
1886 1908
1887 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w)); 1909 assert (("internal timer heap corruption", timers [((W)w)->active] == (WT)w));
1888 1910
1889 { 1911 {
1890 int active = ((W)w)->active; 1912 int active = ((W)w)->active;
1891 1913
1892 if (expect_true (--active < --timercnt)) 1914 if (expect_true (active < timercnt))
1893 { 1915 {
1894 timers [active] = timers [timercnt]; 1916 timers [active] = timers [timercnt];
1895 adjustheap (timers, timercnt, active); 1917 adjustheap (timers, timercnt, active);
1896 } 1918 }
1919
1920 --timercnt;
1897 } 1921 }
1898 1922
1899 ((WT)w)->at -= mn_now; 1923 ev_at (w) -= mn_now;
1900 1924
1901 ev_stop (EV_A_ (W)w); 1925 ev_stop (EV_A_ (W)w);
1902} 1926}
1903 1927
1904void noinline 1928void noinline
1906{ 1930{
1907 if (ev_is_active (w)) 1931 if (ev_is_active (w))
1908 { 1932 {
1909 if (w->repeat) 1933 if (w->repeat)
1910 { 1934 {
1911 ((WT)w)->at = mn_now + w->repeat; 1935 ev_at (w) = mn_now + w->repeat;
1912 adjustheap (timers, timercnt, ((W)w)->active - 1); 1936 adjustheap (timers, timercnt, ((W)w)->active);
1913 } 1937 }
1914 else 1938 else
1915 ev_timer_stop (EV_A_ w); 1939 ev_timer_stop (EV_A_ w);
1916 } 1940 }
1917 else if (w->repeat) 1941 else if (w->repeat)
1927{ 1951{
1928 if (expect_false (ev_is_active (w))) 1952 if (expect_false (ev_is_active (w)))
1929 return; 1953 return;
1930 1954
1931 if (w->reschedule_cb) 1955 if (w->reschedule_cb)
1932 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1956 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1933 else if (w->interval) 1957 else if (w->interval)
1934 { 1958 {
1935 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.));
1936 /* 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 */
1937 ((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;
1938 } 1962 }
1939 else 1963 else
1940 ((WT)w)->at = w->offset; 1964 ev_at (w) = w->offset;
1941 1965
1942 ev_start (EV_A_ (W)w, ++periodiccnt); 1966 ev_start (EV_A_ (W)w, ++periodiccnt);
1943 array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2); 1967 array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2);
1944 periodics [periodiccnt - 1] = (WT)w; 1968 periodics [periodiccnt] = (WT)w;
1945 upheap (periodics, periodiccnt - 1); 1969 upheap (periodics, periodiccnt);
1946 1970
1947 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 1971 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/
1948} 1972}
1949 1973
1950void noinline 1974void noinline
1952{ 1976{
1953 clear_pending (EV_A_ (W)w); 1977 clear_pending (EV_A_ (W)w);
1954 if (expect_false (!ev_is_active (w))) 1978 if (expect_false (!ev_is_active (w)))
1955 return; 1979 return;
1956 1980
1957 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w)); 1981 assert (("internal periodic heap corruption", periodics [((W)w)->active] == (WT)w));
1958 1982
1959 { 1983 {
1960 int active = ((W)w)->active; 1984 int active = ((W)w)->active;
1961 1985
1962 if (expect_true (--active < --periodiccnt)) 1986 if (expect_true (active < periodiccnt))
1963 { 1987 {
1964 periodics [active] = periodics [periodiccnt]; 1988 periodics [active] = periodics [periodiccnt];
1965 adjustheap (periodics, periodiccnt, active); 1989 adjustheap (periodics, periodiccnt, active);
1966 } 1990 }
1991
1992 --periodiccnt;
1967 } 1993 }
1968 1994
1969 ev_stop (EV_A_ (W)w); 1995 ev_stop (EV_A_ (W)w);
1970} 1996}
1971 1997

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines