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

Comparing libev/ev.c (file contents):
Revision 1.289 by root, Sat Jun 6 11:13:16 2009 UTC vs.
Revision 1.297 by root, Fri Jul 10 00:36:21 2009 UTC

57# endif 57# endif
58# ifndef EV_USE_MONOTONIC 58# ifndef EV_USE_MONOTONIC
59# define EV_USE_MONOTONIC 1 59# define EV_USE_MONOTONIC 1
60# endif 60# endif
61# endif 61# endif
62# elif !defined(EV_USE_CLOCK_SYSCALL)
63# define EV_USE_CLOCK_SYSCALL 0
62# endif 64# endif
63 65
64# if HAVE_CLOCK_GETTIME 66# if HAVE_CLOCK_GETTIME
65# ifndef EV_USE_MONOTONIC 67# ifndef EV_USE_MONOTONIC
66# define EV_USE_MONOTONIC 1 68# define EV_USE_MONOTONIC 1
282 284
283#ifndef EV_HEAP_CACHE_AT 285#ifndef EV_HEAP_CACHE_AT
284# define EV_HEAP_CACHE_AT !EV_MINIMAL 286# define EV_HEAP_CACHE_AT !EV_MINIMAL
285#endif 287#endif
286 288
289/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
290/* which makes programs even slower. might work on other unices, too. */
291#if EV_USE_CLOCK_SYSCALL
292# include <syscall.h>
293# ifdef SYS_clock_gettime
294# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
295# undef EV_USE_MONOTONIC
296# define EV_USE_MONOTONIC 1
297# else
298# undef EV_USE_CLOCK_SYSCALL
299# define EV_USE_CLOCK_SYSCALL 0
300# endif
301#endif
302
287/* this block fixes any misconfiguration where we know we run into trouble otherwise */ 303/* this block fixes any misconfiguration where we know we run into trouble otherwise */
288 304
289#ifndef CLOCK_MONOTONIC 305#ifndef CLOCK_MONOTONIC
290# undef EV_USE_MONOTONIC 306# undef EV_USE_MONOTONIC
291# define EV_USE_MONOTONIC 0 307# define EV_USE_MONOTONIC 0
320 336
321#if EV_SELECT_IS_WINSOCKET 337#if EV_SELECT_IS_WINSOCKET
322# include <winsock.h> 338# include <winsock.h>
323#endif 339#endif
324 340
325/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
326/* which makes programs even slower. might work on other unices, too. */
327#if EV_USE_CLOCK_SYSCALL
328# include <syscall.h>
329# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
330# undef EV_USE_MONOTONIC
331# define EV_USE_MONOTONIC 1
332#endif
333
334#if EV_USE_EVENTFD 341#if EV_USE_EVENTFD
335/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ 342/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
336# include <stdint.h> 343# include <stdint.h>
337# ifdef __cplusplus 344# ifdef __cplusplus
338extern "C" { 345extern "C" {
384# define inline_speed static noinline 391# define inline_speed static noinline
385#else 392#else
386# define inline_speed static inline 393# define inline_speed static inline
387#endif 394#endif
388 395
389#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 396#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
397
398#if EV_MINPRI == EV_MAXPRI
399# define ABSPRI(w) (((W)w), 0)
400#else
390#define ABSPRI(w) (((W)w)->priority - EV_MINPRI) 401# define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
402#endif
391 403
392#define EMPTY /* required for microsofts broken pseudo-c compiler */ 404#define EMPTY /* required for microsofts broken pseudo-c compiler */
393#define EMPTY2(a,b) /* used to suppress some warnings */ 405#define EMPTY2(a,b) /* used to suppress some warnings */
394 406
395typedef ev_watcher *W; 407typedef ev_watcher *W;
555 567
556 static int ev_default_loop_ptr; 568 static int ev_default_loop_ptr;
557 569
558#endif 570#endif
559 571
572#if EV_MINIMAL < 2
573# define EV_SUSPEND_CB if (expect_false (suspend_cb)) suspend_cb (EV_A)
574# define EV_RESUME_CB if (expect_false (resume_cb )) resume_cb (EV_A)
575# define EV_INVOKE_PENDING invoke_cb (EV_A)
576#else
577# define EV_SUSPEND_CB (void)0
578# define EV_RESUME_CB (void)0
579# define EV_INVOKE_PENDING ev_invoke_pending (EV_A)
580#endif
581
560/*****************************************************************************/ 582/*****************************************************************************/
561 583
584#ifndef EV_HAVE_EV_TIME
562ev_tstamp 585ev_tstamp
563ev_time (void) 586ev_time (void)
564{ 587{
565#if EV_USE_REALTIME 588#if EV_USE_REALTIME
566 if (expect_true (have_realtime)) 589 if (expect_true (have_realtime))
573 596
574 struct timeval tv; 597 struct timeval tv;
575 gettimeofday (&tv, 0); 598 gettimeofday (&tv, 0);
576 return tv.tv_sec + tv.tv_usec * 1e-6; 599 return tv.tv_sec + tv.tv_usec * 1e-6;
577} 600}
601#endif
578 602
579inline_size ev_tstamp 603inline_size ev_tstamp
580get_clock (void) 604get_clock (void)
581{ 605{
582#if EV_USE_MONOTONIC 606#if EV_USE_MONOTONIC
618 642
619 tv.tv_sec = (time_t)delay; 643 tv.tv_sec = (time_t)delay;
620 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 644 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
621 645
622 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ 646 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
623 /* somehting nto guaranteed by newer posix versions, but guaranteed */ 647 /* somehting not guaranteed by newer posix versions, but guaranteed */
624 /* by older ones */ 648 /* by older ones */
625 select (0, 0, 0, 0, &tv); 649 select (0, 0, 0, 0, &tv);
626#endif 650#endif
627 } 651 }
628} 652}
1343ev_backend (EV_P) 1367ev_backend (EV_P)
1344{ 1368{
1345 return backend; 1369 return backend;
1346} 1370}
1347 1371
1372#if EV_MINIMAL < 2
1348unsigned int 1373unsigned int
1349ev_loop_count (EV_P) 1374ev_loop_count (EV_P)
1350{ 1375{
1351 return loop_count; 1376 return loop_count;
1352} 1377}
1353 1378
1379unsigned int
1380ev_loop_depth (EV_P)
1381{
1382 return loop_depth;
1383}
1384
1354void 1385void
1355ev_set_io_collect_interval (EV_P_ ev_tstamp interval) 1386ev_set_io_collect_interval (EV_P_ ev_tstamp interval)
1356{ 1387{
1357 io_blocktime = interval; 1388 io_blocktime = interval;
1358} 1389}
1360void 1391void
1361ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 1392ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval)
1362{ 1393{
1363 timeout_blocktime = interval; 1394 timeout_blocktime = interval;
1364} 1395}
1396
1397void
1398ev_set_userdata (EV_P_ void *data)
1399{
1400 userdata = data;
1401}
1402
1403void *
1404ev_userdata (EV_P)
1405{
1406 return userdata;
1407}
1408
1409void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P))
1410{
1411 invoke_cb = invoke_pending_cb;
1412}
1413
1414void ev_set_blocking_cb (EV_P_ void (*suspend_cb_)(EV_P), void (*resume_cb_)(EV_P))
1415{
1416 suspend_cb = suspend_cb_;
1417 resume_cb = resume_cb_;
1418}
1419#endif
1365 1420
1366/* initialise a loop structure, must be zero-initialised */ 1421/* initialise a loop structure, must be zero-initialised */
1367static void noinline 1422static void noinline
1368loop_init (EV_P_ unsigned int flags) 1423loop_init (EV_P_ unsigned int flags)
1369{ 1424{
1391 1446
1392 ev_rt_now = ev_time (); 1447 ev_rt_now = ev_time ();
1393 mn_now = get_clock (); 1448 mn_now = get_clock ();
1394 now_floor = mn_now; 1449 now_floor = mn_now;
1395 rtmn_diff = ev_rt_now - mn_now; 1450 rtmn_diff = ev_rt_now - mn_now;
1451#if EV_MINIMAL < 2
1452 invoke_cb = ev_invoke_pending;
1453#endif
1396 1454
1397 io_blocktime = 0.; 1455 io_blocktime = 0.;
1398 timeout_blocktime = 0.; 1456 timeout_blocktime = 0.;
1399 backend = 0; 1457 backend = 0;
1400 backend_fd = -1; 1458 backend_fd = -1;
1594void 1652void
1595ev_loop_fork (EV_P) 1653ev_loop_fork (EV_P)
1596{ 1654{
1597 postfork = 1; /* must be in line with ev_default_fork */ 1655 postfork = 1; /* must be in line with ev_default_fork */
1598} 1656}
1657#endif /* multiplicity */
1599 1658
1600#if EV_VERIFY 1659#if EV_VERIFY
1601static void noinline 1660static void noinline
1602verify_watcher (EV_P_ W w) 1661verify_watcher (EV_P_ W w)
1603{ 1662{
1631 verify_watcher (EV_A_ ws [cnt]); 1690 verify_watcher (EV_A_ ws [cnt]);
1632 } 1691 }
1633} 1692}
1634#endif 1693#endif
1635 1694
1695#if EV_MINIMAL < 2
1636void 1696void
1637ev_loop_verify (EV_P) 1697ev_loop_verify (EV_P)
1638{ 1698{
1639#if EV_VERIFY 1699#if EV_VERIFY
1640 int i; 1700 int i;
1693 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 1753 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
1694 for (signum = signalmax; signum--; ) if (signals [signum].gotsig) 1754 for (signum = signalmax; signum--; ) if (signals [signum].gotsig)
1695# endif 1755# endif
1696#endif 1756#endif
1697} 1757}
1698 1758#endif
1699#endif /* multiplicity */
1700 1759
1701#if EV_MULTIPLICITY 1760#if EV_MULTIPLICITY
1702struct ev_loop * 1761struct ev_loop *
1703ev_default_loop_init (unsigned int flags) 1762ev_default_loop_init (unsigned int flags)
1704#else 1763#else
1765ev_invoke (EV_P_ void *w, int revents) 1824ev_invoke (EV_P_ void *w, int revents)
1766{ 1825{
1767 EV_CB_INVOKE ((W)w, revents); 1826 EV_CB_INVOKE ((W)w, revents);
1768} 1827}
1769 1828
1770inline_speed void 1829void noinline
1771call_pending (EV_P) 1830ev_invoke_pending (EV_P)
1772{ 1831{
1773 int pri; 1832 int pri;
1774 1833
1775 for (pri = NUMPRI; pri--; ) 1834 for (pri = NUMPRI; pri--; )
1776 while (pendingcnt [pri]) 1835 while (pendingcnt [pri])
2011 2070
2012 mn_now = ev_rt_now; 2071 mn_now = ev_rt_now;
2013 } 2072 }
2014} 2073}
2015 2074
2016static int loop_done;
2017
2018void 2075void
2019ev_loop (EV_P_ int flags) 2076ev_loop (EV_P_ int flags)
2020{ 2077{
2078#if EV_MINIMAL < 2
2079 ++loop_depth;
2080#endif
2081
2021 loop_done = EVUNLOOP_CANCEL; 2082 loop_done = EVUNLOOP_CANCEL;
2022 2083
2023 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 2084 EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */
2024 2085
2025 do 2086 do
2026 { 2087 {
2027#if EV_VERIFY >= 2 2088#if EV_VERIFY >= 2
2028 ev_loop_verify (EV_A); 2089 ev_loop_verify (EV_A);
2041 /* we might have forked, so queue fork handlers */ 2102 /* we might have forked, so queue fork handlers */
2042 if (expect_false (postfork)) 2103 if (expect_false (postfork))
2043 if (forkcnt) 2104 if (forkcnt)
2044 { 2105 {
2045 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 2106 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
2046 call_pending (EV_A); 2107 EV_INVOKE_PENDING;
2047 } 2108 }
2048#endif 2109#endif
2049 2110
2050 /* queue prepare watchers (and execute them) */ 2111 /* queue prepare watchers (and execute them) */
2051 if (expect_false (preparecnt)) 2112 if (expect_false (preparecnt))
2052 { 2113 {
2053 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 2114 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
2054 call_pending (EV_A); 2115 EV_INVOKE_PENDING;
2055 } 2116 }
2056 2117
2057 /* we might have forked, so reify kernel state if necessary */ 2118 /* we might have forked, so reify kernel state if necessary */
2058 if (expect_false (postfork)) 2119 if (expect_false (postfork))
2059 loop_fork (EV_A); 2120 loop_fork (EV_A);
2066 ev_tstamp waittime = 0.; 2127 ev_tstamp waittime = 0.;
2067 ev_tstamp sleeptime = 0.; 2128 ev_tstamp sleeptime = 0.;
2068 2129
2069 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) 2130 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt)))
2070 { 2131 {
2132 /* remember old timestamp for io_blocktime calculation */
2133 ev_tstamp prev_mn_now = mn_now;
2134
2071 /* update time to cancel out callback processing overhead */ 2135 /* update time to cancel out callback processing overhead */
2072 time_update (EV_A_ 1e100); 2136 time_update (EV_A_ 1e100);
2073 2137
2074 waittime = MAX_BLOCKTIME; 2138 waittime = MAX_BLOCKTIME;
2075 2139
2085 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; 2149 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
2086 if (waittime > to) waittime = to; 2150 if (waittime > to) waittime = to;
2087 } 2151 }
2088#endif 2152#endif
2089 2153
2154 /* don't let timeouts decrease the waittime below timeout_blocktime */
2090 if (expect_false (waittime < timeout_blocktime)) 2155 if (expect_false (waittime < timeout_blocktime))
2091 waittime = timeout_blocktime; 2156 waittime = timeout_blocktime;
2092 2157
2093 sleeptime = waittime - backend_fudge; 2158 /* extra check because io_blocktime is commonly 0 */
2094
2095 if (expect_true (sleeptime > io_blocktime)) 2159 if (expect_false (io_blocktime))
2096 sleeptime = io_blocktime;
2097
2098 if (sleeptime)
2099 { 2160 {
2161 sleeptime = io_blocktime - (mn_now - prev_mn_now);
2162
2163 if (sleeptime > waittime - backend_fudge)
2164 sleeptime = waittime - backend_fudge;
2165
2166 if (expect_true (sleeptime > 0.))
2167 {
2100 ev_sleep (sleeptime); 2168 ev_sleep (sleeptime);
2101 waittime -= sleeptime; 2169 waittime -= sleeptime;
2170 }
2102 } 2171 }
2103 } 2172 }
2104 2173
2174#if EV_MINIMAL < 2
2105 ++loop_count; 2175 ++loop_count;
2176#endif
2106 backend_poll (EV_A_ waittime); 2177 backend_poll (EV_A_ waittime);
2107 2178
2108 /* update ev_rt_now, do magic */ 2179 /* update ev_rt_now, do magic */
2109 time_update (EV_A_ waittime + sleeptime); 2180 time_update (EV_A_ waittime + sleeptime);
2110 } 2181 }
2122 2193
2123 /* queue check watchers, to be executed first */ 2194 /* queue check watchers, to be executed first */
2124 if (expect_false (checkcnt)) 2195 if (expect_false (checkcnt))
2125 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 2196 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
2126 2197
2127 call_pending (EV_A); 2198 EV_INVOKE_PENDING;
2128 } 2199 }
2129 while (expect_true ( 2200 while (expect_true (
2130 activecnt 2201 activecnt
2131 && !loop_done 2202 && !loop_done
2132 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 2203 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK))
2133 )); 2204 ));
2134 2205
2135 if (loop_done == EVUNLOOP_ONE) 2206 if (loop_done == EVUNLOOP_ONE)
2136 loop_done = EVUNLOOP_CANCEL; 2207 loop_done = EVUNLOOP_CANCEL;
2208
2209#if EV_MINIMAL < 2
2210 --loop_depth;
2211#endif
2137} 2212}
2138 2213
2139void 2214void
2140ev_unloop (EV_P_ int how) 2215ev_unloop (EV_P_ int how)
2141{ 2216{
2233} 2308}
2234 2309
2235inline_size void 2310inline_size void
2236pri_adjust (EV_P_ W w) 2311pri_adjust (EV_P_ W w)
2237{ 2312{
2238 int pri = w->priority; 2313 int pri = ev_priority (w);
2239 pri = pri < EV_MINPRI ? EV_MINPRI : pri; 2314 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
2240 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; 2315 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
2241 w->priority = pri; 2316 ev_set_priority (w, pri);
2242} 2317}
2243 2318
2244inline_speed void 2319inline_speed void
2245ev_start (EV_P_ W w, int active) 2320ev_start (EV_P_ W w, int active)
2246{ 2321{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines