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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines