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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines