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

Comparing libev/ev.c (file contents):
Revision 1.220 by root, Sun Apr 6 09:53:17 2008 UTC vs.
Revision 1.228 by root, Fri May 2 08:07:37 2008 UTC

268# include <winsock.h> 268# include <winsock.h>
269#endif 269#endif
270 270
271#if EV_USE_EVENTFD 271#if EV_USE_EVENTFD
272/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ 272/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
273# include <stdint.h>
274# ifdef __cplusplus
275extern "C" {
276# endif
273int eventfd (unsigned int initval, int flags); 277int eventfd (unsigned int initval, int flags);
278# ifdef __cplusplus
279}
280# endif
274#endif 281#endif
275 282
276/**/ 283/**/
277 284
278/* 285/*
293# define expect(expr,value) __builtin_expect ((expr),(value)) 300# define expect(expr,value) __builtin_expect ((expr),(value))
294# define noinline __attribute__ ((noinline)) 301# define noinline __attribute__ ((noinline))
295#else 302#else
296# define expect(expr,value) (expr) 303# define expect(expr,value) (expr)
297# define noinline 304# define noinline
298# if __STDC_VERSION__ < 199901L 305# if __STDC_VERSION__ < 199901L && __GNUC__ < 2
299# define inline 306# define inline
300# endif 307# endif
301#endif 308#endif
302 309
303#define expect_false(expr) expect ((expr) != 0, 0) 310#define expect_false(expr) expect ((expr) != 0, 0)
318 325
319typedef ev_watcher *W; 326typedef ev_watcher *W;
320typedef ev_watcher_list *WL; 327typedef ev_watcher_list *WL;
321typedef ev_watcher_time *WT; 328typedef ev_watcher_time *WT;
322 329
330#define ev_at(w) ((WT)(w))->at
331
323#if EV_USE_MONOTONIC 332#if EV_USE_MONOTONIC
324/* 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 */
325/* giving it a reasonably high chance of working on typical architetcures */ 334/* giving it a reasonably high chance of working on typical architetcures */
326static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 335static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
327#endif 336#endif
353 perror (msg); 362 perror (msg);
354 abort (); 363 abort ();
355 } 364 }
356} 365}
357 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
358static void *(*alloc)(void *ptr, long size); 382static void *(*alloc)(void *ptr, long size) = ev_realloc_emul;
359 383
360void 384void
361ev_set_allocator (void *(*cb)(void *ptr, long size)) 385ev_set_allocator (void *(*cb)(void *ptr, long size))
362{ 386{
363 alloc = cb; 387 alloc = cb;
364} 388}
365 389
366inline_speed void * 390inline_speed void *
367ev_realloc (void *ptr, long size) 391ev_realloc (void *ptr, long size)
368{ 392{
369 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); 393 ptr = alloc (ptr, size);
370 394
371 if (!ptr && size) 395 if (!ptr && size)
372 { 396 {
373 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); 397 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
374 abort (); 398 abort ();
732 } 756 }
733} 757}
734 758
735/*****************************************************************************/ 759/*****************************************************************************/
736 760
761/* towards the root */
737void inline_speed 762void inline_speed
738upheap (WT *heap, int k) 763upheap (WT *heap, int k)
739{ 764{
740 WT w = heap [k]; 765 WT w = heap [k];
741 766
742 while (k) 767 for (;;)
743 { 768 {
744 int p = (k - 1) >> 1; 769 int p = k >> 1;
745 770
771 /* maybe we could use a dummy element at heap [0]? */
746 if (heap [p]->at <= w->at) 772 if (!p || heap [p]->at <= w->at)
747 break; 773 break;
748 774
749 heap [k] = heap [p]; 775 heap [k] = heap [p];
750 ((W)heap [k])->active = k + 1; 776 ((W)heap [k])->active = k;
751 k = p; 777 k = p;
752 } 778 }
753 779
754 heap [k] = w; 780 heap [k] = w;
755 ((W)heap [k])->active = k + 1; 781 ((W)heap [k])->active = k;
756} 782}
757 783
784/* away from the root */
758void inline_speed 785void inline_speed
759downheap (WT *heap, int N, int k) 786downheap (WT *heap, int N, int k)
760{ 787{
761 WT w = heap [k]; 788 WT w = heap [k];
762 789
763 for (;;) 790 for (;;)
764 { 791 {
765 int c = (k << 1) + 1; 792 int c = k << 1;
766 793
767 if (c >= N) 794 if (c > N)
768 break; 795 break;
769 796
770 c += c + 1 < N && heap [c]->at > heap [c + 1]->at 797 c += c < N && heap [c]->at > heap [c + 1]->at
771 ? 1 : 0; 798 ? 1 : 0;
772 799
773 if (w->at <= heap [c]->at) 800 if (w->at <= heap [c]->at)
774 break; 801 break;
775 802
776 heap [k] = heap [c]; 803 heap [k] = heap [c];
777 ((W)heap [k])->active = k + 1; 804 ((W)heap [k])->active = k;
778 805
779 k = c; 806 k = c;
780 } 807 }
781 808
782 heap [k] = w; 809 heap [k] = w;
783 ((W)heap [k])->active = k + 1; 810 ((W)heap [k])->active = k;
784} 811}
785 812
786void inline_size 813void inline_size
787adjustheap (WT *heap, int N, int k) 814adjustheap (WT *heap, int N, int k)
788{ 815{
1163 if (!(flags & EVFLAG_NOENV) 1190 if (!(flags & EVFLAG_NOENV)
1164 && !enable_secure () 1191 && !enable_secure ()
1165 && getenv ("LIBEV_FLAGS")) 1192 && getenv ("LIBEV_FLAGS"))
1166 flags = atoi (getenv ("LIBEV_FLAGS")); 1193 flags = atoi (getenv ("LIBEV_FLAGS"));
1167 1194
1168 if (!(flags & 0x0000ffffUL)) 1195 if (!(flags & 0x0000ffffU))
1169 flags |= ev_recommended_backends (); 1196 flags |= ev_recommended_backends ();
1170 1197
1171#if EV_USE_PORT 1198#if EV_USE_PORT
1172 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); 1199 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
1173#endif 1200#endif
1261#endif 1288#endif
1262 1289
1263 backend = 0; 1290 backend = 0;
1264} 1291}
1265 1292
1293#if EV_USE_INOTIFY
1266void inline_size infy_fork (EV_P); 1294void inline_size infy_fork (EV_P);
1295#endif
1267 1296
1268void inline_size 1297void inline_size
1269loop_fork (EV_P) 1298loop_fork (EV_P)
1270{ 1299{
1271#if EV_USE_PORT 1300#if EV_USE_PORT
1432} 1461}
1433 1462
1434void inline_size 1463void inline_size
1435timers_reify (EV_P) 1464timers_reify (EV_P)
1436{ 1465{
1437 while (timercnt && ((WT)timers [0])->at <= mn_now) 1466 while (timercnt && ev_at (timers [1]) <= mn_now)
1438 { 1467 {
1439 ev_timer *w = (ev_timer *)timers [0]; 1468 ev_timer *w = (ev_timer *)timers [1];
1440 1469
1441 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ 1470 /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/
1442 1471
1443 /* first reschedule or stop timer */ 1472 /* first reschedule or stop timer */
1444 if (w->repeat) 1473 if (w->repeat)
1445 { 1474 {
1446 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.));
1447 1476
1448 ((WT)w)->at += w->repeat; 1477 ev_at (w) += w->repeat;
1449 if (((WT)w)->at < mn_now) 1478 if (ev_at (w) < mn_now)
1450 ((WT)w)->at = mn_now; 1479 ev_at (w) = mn_now;
1451 1480
1452 downheap (timers, timercnt, 0); 1481 downheap (timers, timercnt, 1);
1453 } 1482 }
1454 else 1483 else
1455 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1484 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1456 1485
1457 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1486 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1460 1489
1461#if EV_PERIODIC_ENABLE 1490#if EV_PERIODIC_ENABLE
1462void inline_size 1491void inline_size
1463periodics_reify (EV_P) 1492periodics_reify (EV_P)
1464{ 1493{
1465 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1494 while (periodiccnt && ev_at (periodics [1]) <= ev_rt_now)
1466 { 1495 {
1467 ev_periodic *w = (ev_periodic *)periodics [0]; 1496 ev_periodic *w = (ev_periodic *)periodics [1];
1468 1497
1469 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1498 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1470 1499
1471 /* first reschedule or stop timer */ 1500 /* first reschedule or stop timer */
1472 if (w->reschedule_cb) 1501 if (w->reschedule_cb)
1473 { 1502 {
1474 ((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);
1475 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));
1476 downheap (periodics, periodiccnt, 0); 1505 downheap (periodics, periodiccnt, 1);
1477 } 1506 }
1478 else if (w->interval) 1507 else if (w->interval)
1479 { 1508 {
1480 ((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;
1481 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;
1482 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));
1483 downheap (periodics, periodiccnt, 0); 1512 downheap (periodics, periodiccnt, 1);
1484 } 1513 }
1485 else 1514 else
1486 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 1515 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1487 1516
1488 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1517 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1498 for (i = 0; i < periodiccnt; ++i) 1527 for (i = 0; i < periodiccnt; ++i)
1499 { 1528 {
1500 ev_periodic *w = (ev_periodic *)periodics [i]; 1529 ev_periodic *w = (ev_periodic *)periodics [i];
1501 1530
1502 if (w->reschedule_cb) 1531 if (w->reschedule_cb)
1503 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1532 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1504 else if (w->interval) 1533 else if (w->interval)
1505 ((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;
1506 } 1535 }
1507 1536
1508 /* now rebuild the heap */ 1537 /* now rebuild the heap */
1509 for (i = periodiccnt >> 1; i--; ) 1538 for (i = periodiccnt >> 1; i--; )
1510 downheap (periodics, periodiccnt, i); 1539 downheap (periodics, periodiccnt, i);
1592 { 1621 {
1593#if EV_PERIODIC_ENABLE 1622#if EV_PERIODIC_ENABLE
1594 periodics_reschedule (EV_A); 1623 periodics_reschedule (EV_A);
1595#endif 1624#endif
1596 /* 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 */
1597 for (i = 0; i < timercnt; ++i) 1626 for (i = 1; i <= timercnt; ++i)
1598 ((WT)timers [i])->at += ev_rt_now - mn_now; 1627 ev_at (timers [i]) += ev_rt_now - mn_now;
1599 } 1628 }
1600 1629
1601 mn_now = ev_rt_now; 1630 mn_now = ev_rt_now;
1602 } 1631 }
1603} 1632}
1673 1702
1674 waittime = MAX_BLOCKTIME; 1703 waittime = MAX_BLOCKTIME;
1675 1704
1676 if (timercnt) 1705 if (timercnt)
1677 { 1706 {
1678 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1707 ev_tstamp to = ev_at (timers [1]) - mn_now + backend_fudge;
1679 if (waittime > to) waittime = to; 1708 if (waittime > to) waittime = to;
1680 } 1709 }
1681 1710
1682#if EV_PERIODIC_ENABLE 1711#if EV_PERIODIC_ENABLE
1683 if (periodiccnt) 1712 if (periodiccnt)
1684 { 1713 {
1685 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;
1686 if (waittime > to) waittime = to; 1715 if (waittime > to) waittime = to;
1687 } 1716 }
1688#endif 1717#endif
1689 1718
1690 if (expect_false (waittime < timeout_blocktime)) 1719 if (expect_false (waittime < timeout_blocktime))
1856ev_timer_start (EV_P_ ev_timer *w) 1885ev_timer_start (EV_P_ ev_timer *w)
1857{ 1886{
1858 if (expect_false (ev_is_active (w))) 1887 if (expect_false (ev_is_active (w)))
1859 return; 1888 return;
1860 1889
1861 ((WT)w)->at += mn_now; 1890 ev_at (w) += mn_now;
1862 1891
1863 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.));
1864 1893
1865 ev_start (EV_A_ (W)w, ++timercnt); 1894 ev_start (EV_A_ (W)w, ++timercnt);
1866 array_needsize (WT, timers, timermax, timercnt, EMPTY2); 1895 array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2);
1867 timers [timercnt - 1] = (WT)w; 1896 timers [timercnt] = (WT)w;
1868 upheap (timers, timercnt - 1); 1897 upheap (timers, timercnt);
1869 1898
1870 /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ 1899 /*assert (("internal timer heap corruption", timers [((W)w)->active] == w));*/
1871} 1900}
1872 1901
1873void noinline 1902void noinline
1874ev_timer_stop (EV_P_ ev_timer *w) 1903ev_timer_stop (EV_P_ ev_timer *w)
1875{ 1904{
1876 clear_pending (EV_A_ (W)w); 1905 clear_pending (EV_A_ (W)w);
1877 if (expect_false (!ev_is_active (w))) 1906 if (expect_false (!ev_is_active (w)))
1878 return; 1907 return;
1879 1908
1880 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w)); 1909 assert (("internal timer heap corruption", timers [((W)w)->active] == (WT)w));
1881 1910
1882 { 1911 {
1883 int active = ((W)w)->active; 1912 int active = ((W)w)->active;
1884 1913
1885 if (expect_true (--active < --timercnt)) 1914 if (expect_true (active < timercnt))
1886 { 1915 {
1887 timers [active] = timers [timercnt]; 1916 timers [active] = timers [timercnt];
1888 adjustheap (timers, timercnt, active); 1917 adjustheap (timers, timercnt, active);
1889 } 1918 }
1919
1920 --timercnt;
1890 } 1921 }
1891 1922
1892 ((WT)w)->at -= mn_now; 1923 ev_at (w) -= mn_now;
1893 1924
1894 ev_stop (EV_A_ (W)w); 1925 ev_stop (EV_A_ (W)w);
1895} 1926}
1896 1927
1897void noinline 1928void noinline
1899{ 1930{
1900 if (ev_is_active (w)) 1931 if (ev_is_active (w))
1901 { 1932 {
1902 if (w->repeat) 1933 if (w->repeat)
1903 { 1934 {
1904 ((WT)w)->at = mn_now + w->repeat; 1935 ev_at (w) = mn_now + w->repeat;
1905 adjustheap (timers, timercnt, ((W)w)->active - 1); 1936 adjustheap (timers, timercnt, ((W)w)->active);
1906 } 1937 }
1907 else 1938 else
1908 ev_timer_stop (EV_A_ w); 1939 ev_timer_stop (EV_A_ w);
1909 } 1940 }
1910 else if (w->repeat) 1941 else if (w->repeat)
1920{ 1951{
1921 if (expect_false (ev_is_active (w))) 1952 if (expect_false (ev_is_active (w)))
1922 return; 1953 return;
1923 1954
1924 if (w->reschedule_cb) 1955 if (w->reschedule_cb)
1925 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1956 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1926 else if (w->interval) 1957 else if (w->interval)
1927 { 1958 {
1928 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.));
1929 /* 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 */
1930 ((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;
1931 } 1962 }
1932 else 1963 else
1933 ((WT)w)->at = w->offset; 1964 ev_at (w) = w->offset;
1934 1965
1935 ev_start (EV_A_ (W)w, ++periodiccnt); 1966 ev_start (EV_A_ (W)w, ++periodiccnt);
1936 array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2); 1967 array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2);
1937 periodics [periodiccnt - 1] = (WT)w; 1968 periodics [periodiccnt] = (WT)w;
1938 upheap (periodics, periodiccnt - 1); 1969 upheap (periodics, periodiccnt);
1939 1970
1940 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 1971 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/
1941} 1972}
1942 1973
1943void noinline 1974void noinline
1945{ 1976{
1946 clear_pending (EV_A_ (W)w); 1977 clear_pending (EV_A_ (W)w);
1947 if (expect_false (!ev_is_active (w))) 1978 if (expect_false (!ev_is_active (w)))
1948 return; 1979 return;
1949 1980
1950 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w)); 1981 assert (("internal periodic heap corruption", periodics [((W)w)->active] == (WT)w));
1951 1982
1952 { 1983 {
1953 int active = ((W)w)->active; 1984 int active = ((W)w)->active;
1954 1985
1955 if (expect_true (--active < --periodiccnt)) 1986 if (expect_true (active < periodiccnt))
1956 { 1987 {
1957 periodics [active] = periodics [periodiccnt]; 1988 periodics [active] = periodics [periodiccnt];
1958 adjustheap (periodics, periodiccnt, active); 1989 adjustheap (periodics, periodiccnt, active);
1959 } 1990 }
1991
1992 --periodiccnt;
1960 } 1993 }
1961 1994
1962 ev_stop (EV_A_ (W)w); 1995 ev_stop (EV_A_ (W)w);
1963} 1996}
1964 1997

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines