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

Comparing libev/ev.c (file contents):
Revision 1.221 by root, Sun Apr 6 12:44:49 2008 UTC vs.
Revision 1.228 by root, Fri May 2 08:07:37 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines