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

Comparing libev/ev.c (file contents):
Revision 1.290 by root, Mon Jun 29 04:41:34 2009 UTC vs.
Revision 1.296 by root, Thu Jul 9 09:11:20 2009 UTC

284 284
285#ifndef EV_HEAP_CACHE_AT 285#ifndef EV_HEAP_CACHE_AT
286# define EV_HEAP_CACHE_AT !EV_MINIMAL 286# define EV_HEAP_CACHE_AT !EV_MINIMAL
287#endif 287#endif
288 288
289/* this block fixes any misconfiguration where we know we run into trouble otherwise */
290
291#ifndef CLOCK_MONOTONIC
292# undef EV_USE_MONOTONIC
293# define EV_USE_MONOTONIC 0
294#endif
295
296#ifndef CLOCK_REALTIME
297# undef EV_USE_REALTIME
298# define EV_USE_REALTIME 0
299#endif
300
301#if !EV_STAT_ENABLE
302# undef EV_USE_INOTIFY
303# define EV_USE_INOTIFY 0
304#endif
305
306#if !EV_USE_NANOSLEEP
307# ifndef _WIN32
308# include <sys/select.h>
309# endif
310#endif
311
312#if EV_USE_INOTIFY
313# include <sys/utsname.h>
314# include <sys/statfs.h>
315# include <sys/inotify.h>
316/* some very old inotify.h headers don't have IN_DONT_FOLLOW */
317# ifndef IN_DONT_FOLLOW
318# undef EV_USE_INOTIFY
319# define EV_USE_INOTIFY 0
320# endif
321#endif
322
323#if EV_SELECT_IS_WINSOCKET
324# include <winsock.h>
325#endif
326
327/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */ 289/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
328/* which makes programs even slower. might work on other unices, too. */ 290/* which makes programs even slower. might work on other unices, too. */
329#if EV_USE_CLOCK_SYSCALL 291#if EV_USE_CLOCK_SYSCALL
330# include <syscall.h> 292# include <syscall.h>
331# ifdef SYS_clock_gettime 293# ifdef SYS_clock_gettime
336# undef EV_USE_CLOCK_SYSCALL 298# undef EV_USE_CLOCK_SYSCALL
337# define EV_USE_CLOCK_SYSCALL 0 299# define EV_USE_CLOCK_SYSCALL 0
338# endif 300# endif
339#endif 301#endif
340 302
303/* this block fixes any misconfiguration where we know we run into trouble otherwise */
304
305#ifndef CLOCK_MONOTONIC
306# undef EV_USE_MONOTONIC
307# define EV_USE_MONOTONIC 0
308#endif
309
310#ifndef CLOCK_REALTIME
311# undef EV_USE_REALTIME
312# define EV_USE_REALTIME 0
313#endif
314
315#if !EV_STAT_ENABLE
316# undef EV_USE_INOTIFY
317# define EV_USE_INOTIFY 0
318#endif
319
320#if !EV_USE_NANOSLEEP
321# ifndef _WIN32
322# include <sys/select.h>
323# endif
324#endif
325
326#if EV_USE_INOTIFY
327# include <sys/utsname.h>
328# include <sys/statfs.h>
329# include <sys/inotify.h>
330/* some very old inotify.h headers don't have IN_DONT_FOLLOW */
331# ifndef IN_DONT_FOLLOW
332# undef EV_USE_INOTIFY
333# define EV_USE_INOTIFY 0
334# endif
335#endif
336
337#if EV_SELECT_IS_WINSOCKET
338# include <winsock.h>
339#endif
340
341#if EV_USE_EVENTFD 341#if EV_USE_EVENTFD
342/* 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 */
343# include <stdint.h> 343# include <stdint.h>
344# ifdef __cplusplus 344# ifdef __cplusplus
345extern "C" { 345extern "C" {
391# define inline_speed static noinline 391# define inline_speed static noinline
392#else 392#else
393# define inline_speed static inline 393# define inline_speed static inline
394#endif 394#endif
395 395
396#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
397#define ABSPRI(w) (((W)w)->priority - EV_MINPRI) 401# define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
402#endif
398 403
399#define EMPTY /* required for microsofts broken pseudo-c compiler */ 404#define EMPTY /* required for microsofts broken pseudo-c compiler */
400#define EMPTY2(a,b) /* used to suppress some warnings */ 405#define EMPTY2(a,b) /* used to suppress some warnings */
401 406
402typedef ev_watcher *W; 407typedef ev_watcher *W;
564 569
565#endif 570#endif
566 571
567/*****************************************************************************/ 572/*****************************************************************************/
568 573
574#ifndef EV_HAVE_EV_TIME
569ev_tstamp 575ev_tstamp
570ev_time (void) 576ev_time (void)
571{ 577{
572#if EV_USE_REALTIME 578#if EV_USE_REALTIME
573 if (expect_true (have_realtime)) 579 if (expect_true (have_realtime))
580 586
581 struct timeval tv; 587 struct timeval tv;
582 gettimeofday (&tv, 0); 588 gettimeofday (&tv, 0);
583 return tv.tv_sec + tv.tv_usec * 1e-6; 589 return tv.tv_sec + tv.tv_usec * 1e-6;
584} 590}
591#endif
585 592
586inline_size ev_tstamp 593inline_size ev_tstamp
587get_clock (void) 594get_clock (void)
588{ 595{
589#if EV_USE_MONOTONIC 596#if EV_USE_MONOTONIC
625 632
626 tv.tv_sec = (time_t)delay; 633 tv.tv_sec = (time_t)delay;
627 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 634 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
628 635
629 /* 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 */
630 /* somehting nto guaranteed by newer posix versions, but guaranteed */ 637 /* somehting not guaranteed by newer posix versions, but guaranteed */
631 /* by older ones */ 638 /* by older ones */
632 select (0, 0, 0, 0, &tv); 639 select (0, 0, 0, 0, &tv);
633#endif 640#endif
634 } 641 }
635} 642}
1356ev_loop_count (EV_P) 1363ev_loop_count (EV_P)
1357{ 1364{
1358 return loop_count; 1365 return loop_count;
1359} 1366}
1360 1367
1368unsigned int
1369ev_loop_depth (EV_P)
1370{
1371 return loop_depth;
1372}
1373
1361void 1374void
1362ev_set_io_collect_interval (EV_P_ ev_tstamp interval) 1375ev_set_io_collect_interval (EV_P_ ev_tstamp interval)
1363{ 1376{
1364 io_blocktime = interval; 1377 io_blocktime = interval;
1365} 1378}
1398 1411
1399 ev_rt_now = ev_time (); 1412 ev_rt_now = ev_time ();
1400 mn_now = get_clock (); 1413 mn_now = get_clock ();
1401 now_floor = mn_now; 1414 now_floor = mn_now;
1402 rtmn_diff = ev_rt_now - mn_now; 1415 rtmn_diff = ev_rt_now - mn_now;
1416 invoke_cb = ev_invoke_pending;
1403 1417
1404 io_blocktime = 0.; 1418 io_blocktime = 0.;
1405 timeout_blocktime = 0.; 1419 timeout_blocktime = 0.;
1406 backend = 0; 1420 backend = 0;
1407 backend_fd = -1; 1421 backend_fd = -1;
1772ev_invoke (EV_P_ void *w, int revents) 1786ev_invoke (EV_P_ void *w, int revents)
1773{ 1787{
1774 EV_CB_INVOKE ((W)w, revents); 1788 EV_CB_INVOKE ((W)w, revents);
1775} 1789}
1776 1790
1777inline_speed void 1791void
1778call_pending (EV_P) 1792ev_invoke_pending (EV_P)
1779{ 1793{
1780 int pri; 1794 int pri;
1781 1795
1782 for (pri = NUMPRI; pri--; ) 1796 for (pri = NUMPRI; pri--; )
1783 while (pendingcnt [pri]) 1797 while (pendingcnt [pri])
2018 2032
2019 mn_now = ev_rt_now; 2033 mn_now = ev_rt_now;
2020 } 2034 }
2021} 2035}
2022 2036
2023static int loop_done;
2024
2025void 2037void
2026ev_loop (EV_P_ int flags) 2038ev_loop (EV_P_ int flags)
2027{ 2039{
2040 ++loop_depth;
2041
2028 loop_done = EVUNLOOP_CANCEL; 2042 loop_done = EVUNLOOP_CANCEL;
2029 2043
2030 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 2044 invoke_cb (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
2031 2045
2032 do 2046 do
2033 { 2047 {
2034#if EV_VERIFY >= 2 2048#if EV_VERIFY >= 2
2035 ev_loop_verify (EV_A); 2049 ev_loop_verify (EV_A);
2048 /* we might have forked, so queue fork handlers */ 2062 /* we might have forked, so queue fork handlers */
2049 if (expect_false (postfork)) 2063 if (expect_false (postfork))
2050 if (forkcnt) 2064 if (forkcnt)
2051 { 2065 {
2052 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 2066 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
2053 call_pending (EV_A); 2067 invoke_cb (EV_A);
2054 } 2068 }
2055#endif 2069#endif
2056 2070
2057 /* queue prepare watchers (and execute them) */ 2071 /* queue prepare watchers (and execute them) */
2058 if (expect_false (preparecnt)) 2072 if (expect_false (preparecnt))
2059 { 2073 {
2060 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 2074 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
2061 call_pending (EV_A); 2075 invoke_cb (EV_A);
2062 } 2076 }
2063 2077
2064 /* we might have forked, so reify kernel state if necessary */ 2078 /* we might have forked, so reify kernel state if necessary */
2065 if (expect_false (postfork)) 2079 if (expect_false (postfork))
2066 loop_fork (EV_A); 2080 loop_fork (EV_A);
2073 ev_tstamp waittime = 0.; 2087 ev_tstamp waittime = 0.;
2074 ev_tstamp sleeptime = 0.; 2088 ev_tstamp sleeptime = 0.;
2075 2089
2076 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) 2090 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt)))
2077 { 2091 {
2092 /* remember old timestamp for io_blocktime calculation */
2093 ev_tstamp prev_mn_now = mn_now;
2094
2078 /* update time to cancel out callback processing overhead */ 2095 /* update time to cancel out callback processing overhead */
2079 time_update (EV_A_ 1e100); 2096 time_update (EV_A_ 1e100);
2080 2097
2081 waittime = MAX_BLOCKTIME; 2098 waittime = MAX_BLOCKTIME;
2082 2099
2092 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; 2109 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
2093 if (waittime > to) waittime = to; 2110 if (waittime > to) waittime = to;
2094 } 2111 }
2095#endif 2112#endif
2096 2113
2114 /* don't let timeouts decrease the waittime below timeout_blocktime */
2097 if (expect_false (waittime < timeout_blocktime)) 2115 if (expect_false (waittime < timeout_blocktime))
2098 waittime = timeout_blocktime; 2116 waittime = timeout_blocktime;
2099 2117
2100 sleeptime = waittime - backend_fudge; 2118 /* extra check because io_blocktime is commonly 0 */
2101
2102 if (expect_true (sleeptime > io_blocktime)) 2119 if (expect_false (io_blocktime))
2103 sleeptime = io_blocktime;
2104
2105 if (sleeptime)
2106 { 2120 {
2121 sleeptime = io_blocktime - (mn_now - prev_mn_now);
2122
2123 if (sleeptime > waittime - backend_fudge)
2124 sleeptime = waittime - backend_fudge;
2125
2126 if (expect_true (sleeptime > 0.))
2127 {
2107 ev_sleep (sleeptime); 2128 ev_sleep (sleeptime);
2108 waittime -= sleeptime; 2129 waittime -= sleeptime;
2130 }
2109 } 2131 }
2110 } 2132 }
2111 2133
2112 ++loop_count; 2134 ++loop_count;
2113 backend_poll (EV_A_ waittime); 2135 backend_poll (EV_A_ waittime);
2129 2151
2130 /* queue check watchers, to be executed first */ 2152 /* queue check watchers, to be executed first */
2131 if (expect_false (checkcnt)) 2153 if (expect_false (checkcnt))
2132 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 2154 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
2133 2155
2134 call_pending (EV_A); 2156 invoke_cb (EV_A);
2135 } 2157 }
2136 while (expect_true ( 2158 while (expect_true (
2137 activecnt 2159 activecnt
2138 && !loop_done 2160 && !loop_done
2139 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 2161 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK))
2140 )); 2162 ));
2141 2163
2142 if (loop_done == EVUNLOOP_ONE) 2164 if (loop_done == EVUNLOOP_ONE)
2143 loop_done = EVUNLOOP_CANCEL; 2165 loop_done = EVUNLOOP_CANCEL;
2166
2167 --loop_depth;
2144} 2168}
2145 2169
2146void 2170void
2147ev_unloop (EV_P_ int how) 2171ev_unloop (EV_P_ int how)
2148{ 2172{
2240} 2264}
2241 2265
2242inline_size void 2266inline_size void
2243pri_adjust (EV_P_ W w) 2267pri_adjust (EV_P_ W w)
2244{ 2268{
2245 int pri = w->priority; 2269 int pri = ev_priority (w);
2246 pri = pri < EV_MINPRI ? EV_MINPRI : pri; 2270 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
2247 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; 2271 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
2248 w->priority = pri; 2272 ev_set_priority (w, pri);
2249} 2273}
2250 2274
2251inline_speed void 2275inline_speed void
2252ev_start (EV_P_ W w, int active) 2276ev_start (EV_P_ W w, int active)
2253{ 2277{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines