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

Comparing libev/ev.c (file contents):
Revision 1.288 by root, Sat Apr 25 14:12:48 2009 UTC vs.
Revision 1.295 by root, Wed Jul 8 04:29:31 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;
557 569
558#endif 570#endif
559 571
560/*****************************************************************************/ 572/*****************************************************************************/
561 573
574#ifndef EV_HAVE_EV_TIME
562ev_tstamp 575ev_tstamp
563ev_time (void) 576ev_time (void)
564{ 577{
565#if EV_USE_REALTIME 578#if EV_USE_REALTIME
566 if (expect_true (have_realtime)) 579 if (expect_true (have_realtime))
573 586
574 struct timeval tv; 587 struct timeval tv;
575 gettimeofday (&tv, 0); 588 gettimeofday (&tv, 0);
576 return tv.tv_sec + tv.tv_usec * 1e-6; 589 return tv.tv_sec + tv.tv_usec * 1e-6;
577} 590}
591#endif
578 592
579inline_size ev_tstamp 593inline_size ev_tstamp
580get_clock (void) 594get_clock (void)
581{ 595{
582#if EV_USE_MONOTONIC 596#if EV_USE_MONOTONIC
618 632
619 tv.tv_sec = (time_t)delay; 633 tv.tv_sec = (time_t)delay;
620 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 634 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
621 635
622 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ 636 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
623 /* somehting nto guaranteed by newer posix versions, but guaranteed */ 637 /* somehting not guaranteed by newer posix versions, but guaranteed */
624 /* by older ones */ 638 /* by older ones */
625 select (0, 0, 0, 0, &tv); 639 select (0, 0, 0, 0, &tv);
626#endif 640#endif
627 } 641 }
628} 642}
1349ev_loop_count (EV_P) 1363ev_loop_count (EV_P)
1350{ 1364{
1351 return loop_count; 1365 return loop_count;
1352} 1366}
1353 1367
1368unsigned int
1369ev_loop_depth (EV_P)
1370{
1371 return loop_depth;
1372}
1373
1354void 1374void
1355ev_set_io_collect_interval (EV_P_ ev_tstamp interval) 1375ev_set_io_collect_interval (EV_P_ ev_tstamp interval)
1356{ 1376{
1357 io_blocktime = interval; 1377 io_blocktime = interval;
1358} 1378}
1948/* fetch new monotonic and realtime times from the kernel */ 1968/* fetch new monotonic and realtime times from the kernel */
1949/* also detetc if there was a timejump, and act accordingly */ 1969/* also detetc if there was a timejump, and act accordingly */
1950inline_speed void 1970inline_speed void
1951time_update (EV_P_ ev_tstamp max_block) 1971time_update (EV_P_ ev_tstamp max_block)
1952{ 1972{
1953 int i;
1954
1955#if EV_USE_MONOTONIC 1973#if EV_USE_MONOTONIC
1956 if (expect_true (have_monotonic)) 1974 if (expect_true (have_monotonic))
1957 { 1975 {
1976 int i;
1958 ev_tstamp odiff = rtmn_diff; 1977 ev_tstamp odiff = rtmn_diff;
1959 1978
1960 mn_now = get_clock (); 1979 mn_now = get_clock ();
1961 1980
1962 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ 1981 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */
2012 2031
2013 mn_now = ev_rt_now; 2032 mn_now = ev_rt_now;
2014 } 2033 }
2015} 2034}
2016 2035
2017static int loop_done;
2018
2019void 2036void
2020ev_loop (EV_P_ int flags) 2037ev_loop (EV_P_ int flags)
2021{ 2038{
2039 ++loop_depth;
2040
2022 loop_done = EVUNLOOP_CANCEL; 2041 loop_done = EVUNLOOP_CANCEL;
2023 2042
2024 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 2043 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
2025 2044
2026 do 2045 do
2067 ev_tstamp waittime = 0.; 2086 ev_tstamp waittime = 0.;
2068 ev_tstamp sleeptime = 0.; 2087 ev_tstamp sleeptime = 0.;
2069 2088
2070 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) 2089 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt)))
2071 { 2090 {
2091 /* remember old timestamp for io_blocktime calculation */
2092 ev_tstamp prev_mn_now = mn_now;
2093
2072 /* update time to cancel out callback processing overhead */ 2094 /* update time to cancel out callback processing overhead */
2073 time_update (EV_A_ 1e100); 2095 time_update (EV_A_ 1e100);
2074 2096
2075 waittime = MAX_BLOCKTIME; 2097 waittime = MAX_BLOCKTIME;
2076 2098
2086 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; 2108 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
2087 if (waittime > to) waittime = to; 2109 if (waittime > to) waittime = to;
2088 } 2110 }
2089#endif 2111#endif
2090 2112
2113 /* don't let timeouts decrease the waittime below timeout_blocktime */
2091 if (expect_false (waittime < timeout_blocktime)) 2114 if (expect_false (waittime < timeout_blocktime))
2092 waittime = timeout_blocktime; 2115 waittime = timeout_blocktime;
2093 2116
2094 sleeptime = waittime - backend_fudge; 2117 /* extra check because io_blocktime is commonly 0 */
2095
2096 if (expect_true (sleeptime > io_blocktime)) 2118 if (expect_false (io_blocktime))
2097 sleeptime = io_blocktime;
2098
2099 if (sleeptime)
2100 { 2119 {
2120 sleeptime = io_blocktime - (mn_now - prev_mn_now);
2121
2122 if (sleeptime > waittime - backend_fudge)
2123 sleeptime = waittime - backend_fudge;
2124
2125 if (expect_true (sleeptime > 0.))
2126 {
2101 ev_sleep (sleeptime); 2127 ev_sleep (sleeptime);
2102 waittime -= sleeptime; 2128 waittime -= sleeptime;
2129 }
2103 } 2130 }
2104 } 2131 }
2105 2132
2106 ++loop_count; 2133 ++loop_count;
2107 backend_poll (EV_A_ waittime); 2134 backend_poll (EV_A_ waittime);
2133 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 2160 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK))
2134 )); 2161 ));
2135 2162
2136 if (loop_done == EVUNLOOP_ONE) 2163 if (loop_done == EVUNLOOP_ONE)
2137 loop_done = EVUNLOOP_CANCEL; 2164 loop_done = EVUNLOOP_CANCEL;
2165
2166 --loop_depth;
2138} 2167}
2139 2168
2140void 2169void
2141ev_unloop (EV_P_ int how) 2170ev_unloop (EV_P_ int how)
2142{ 2171{
2234} 2263}
2235 2264
2236inline_size void 2265inline_size void
2237pri_adjust (EV_P_ W w) 2266pri_adjust (EV_P_ W w)
2238{ 2267{
2239 int pri = w->priority; 2268 int pri = ev_priority (w);
2240 pri = pri < EV_MINPRI ? EV_MINPRI : pri; 2269 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
2241 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; 2270 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
2242 w->priority = pri; 2271 ev_set_priority (w, pri);
2243} 2272}
2244 2273
2245inline_speed void 2274inline_speed void
2246ev_start (EV_P_ W w, int active) 2275ev_start (EV_P_ W w, int active)
2247{ 2276{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines