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

Comparing libev/ev.c (file contents):
Revision 1.222 by root, Sun Apr 6 12:45:58 2008 UTC vs.
Revision 1.229 by root, Fri May 2 08:08:45 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines