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

Comparing libev/ev.c (file contents):
Revision 1.292 by root, Mon Jun 29 07:22:56 2009 UTC vs.
Revision 1.296 by root, Thu Jul 9 09:11:20 2009 UTC

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;
627 632
628 tv.tv_sec = (time_t)delay; 633 tv.tv_sec = (time_t)delay;
629 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 634 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
630 635
631 /* 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 */
632 /* somehting nto guaranteed by newer posix versions, but guaranteed */ 637 /* somehting not guaranteed by newer posix versions, but guaranteed */
633 /* by older ones */ 638 /* by older ones */
634 select (0, 0, 0, 0, &tv); 639 select (0, 0, 0, 0, &tv);
635#endif 640#endif
636 } 641 }
637} 642}
1358ev_loop_count (EV_P) 1363ev_loop_count (EV_P)
1359{ 1364{
1360 return loop_count; 1365 return loop_count;
1361} 1366}
1362 1367
1368unsigned int
1369ev_loop_depth (EV_P)
1370{
1371 return loop_depth;
1372}
1373
1363void 1374void
1364ev_set_io_collect_interval (EV_P_ ev_tstamp interval) 1375ev_set_io_collect_interval (EV_P_ ev_tstamp interval)
1365{ 1376{
1366 io_blocktime = interval; 1377 io_blocktime = interval;
1367} 1378}
1400 1411
1401 ev_rt_now = ev_time (); 1412 ev_rt_now = ev_time ();
1402 mn_now = get_clock (); 1413 mn_now = get_clock ();
1403 now_floor = mn_now; 1414 now_floor = mn_now;
1404 rtmn_diff = ev_rt_now - mn_now; 1415 rtmn_diff = ev_rt_now - mn_now;
1416 invoke_cb = ev_invoke_pending;
1405 1417
1406 io_blocktime = 0.; 1418 io_blocktime = 0.;
1407 timeout_blocktime = 0.; 1419 timeout_blocktime = 0.;
1408 backend = 0; 1420 backend = 0;
1409 backend_fd = -1; 1421 backend_fd = -1;
1774ev_invoke (EV_P_ void *w, int revents) 1786ev_invoke (EV_P_ void *w, int revents)
1775{ 1787{
1776 EV_CB_INVOKE ((W)w, revents); 1788 EV_CB_INVOKE ((W)w, revents);
1777} 1789}
1778 1790
1779inline_speed void 1791void
1780call_pending (EV_P) 1792ev_invoke_pending (EV_P)
1781{ 1793{
1782 int pri; 1794 int pri;
1783 1795
1784 for (pri = NUMPRI; pri--; ) 1796 for (pri = NUMPRI; pri--; )
1785 while (pendingcnt [pri]) 1797 while (pendingcnt [pri])
2020 2032
2021 mn_now = ev_rt_now; 2033 mn_now = ev_rt_now;
2022 } 2034 }
2023} 2035}
2024 2036
2025static int loop_done;
2026
2027void 2037void
2028ev_loop (EV_P_ int flags) 2038ev_loop (EV_P_ int flags)
2029{ 2039{
2040 ++loop_depth;
2041
2030 loop_done = EVUNLOOP_CANCEL; 2042 loop_done = EVUNLOOP_CANCEL;
2031 2043
2032 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 */
2033 2045
2034 do 2046 do
2035 { 2047 {
2036#if EV_VERIFY >= 2 2048#if EV_VERIFY >= 2
2037 ev_loop_verify (EV_A); 2049 ev_loop_verify (EV_A);
2050 /* we might have forked, so queue fork handlers */ 2062 /* we might have forked, so queue fork handlers */
2051 if (expect_false (postfork)) 2063 if (expect_false (postfork))
2052 if (forkcnt) 2064 if (forkcnt)
2053 { 2065 {
2054 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 2066 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
2055 call_pending (EV_A); 2067 invoke_cb (EV_A);
2056 } 2068 }
2057#endif 2069#endif
2058 2070
2059 /* queue prepare watchers (and execute them) */ 2071 /* queue prepare watchers (and execute them) */
2060 if (expect_false (preparecnt)) 2072 if (expect_false (preparecnt))
2061 { 2073 {
2062 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 2074 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
2063 call_pending (EV_A); 2075 invoke_cb (EV_A);
2064 } 2076 }
2065 2077
2066 /* we might have forked, so reify kernel state if necessary */ 2078 /* we might have forked, so reify kernel state if necessary */
2067 if (expect_false (postfork)) 2079 if (expect_false (postfork))
2068 loop_fork (EV_A); 2080 loop_fork (EV_A);
2075 ev_tstamp waittime = 0.; 2087 ev_tstamp waittime = 0.;
2076 ev_tstamp sleeptime = 0.; 2088 ev_tstamp sleeptime = 0.;
2077 2089
2078 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) 2090 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt)))
2079 { 2091 {
2092 /* remember old timestamp for io_blocktime calculation */
2093 ev_tstamp prev_mn_now = mn_now;
2094
2080 /* update time to cancel out callback processing overhead */ 2095 /* update time to cancel out callback processing overhead */
2081 time_update (EV_A_ 1e100); 2096 time_update (EV_A_ 1e100);
2082 2097
2083 waittime = MAX_BLOCKTIME; 2098 waittime = MAX_BLOCKTIME;
2084 2099
2094 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;
2095 if (waittime > to) waittime = to; 2110 if (waittime > to) waittime = to;
2096 } 2111 }
2097#endif 2112#endif
2098 2113
2114 /* don't let timeouts decrease the waittime below timeout_blocktime */
2099 if (expect_false (waittime < timeout_blocktime)) 2115 if (expect_false (waittime < timeout_blocktime))
2100 waittime = timeout_blocktime; 2116 waittime = timeout_blocktime;
2101 2117
2102 sleeptime = waittime - backend_fudge; 2118 /* extra check because io_blocktime is commonly 0 */
2103
2104 if (expect_true (sleeptime > io_blocktime)) 2119 if (expect_false (io_blocktime))
2105 sleeptime = io_blocktime;
2106
2107 if (sleeptime)
2108 { 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 {
2109 ev_sleep (sleeptime); 2128 ev_sleep (sleeptime);
2110 waittime -= sleeptime; 2129 waittime -= sleeptime;
2130 }
2111 } 2131 }
2112 } 2132 }
2113 2133
2114 ++loop_count; 2134 ++loop_count;
2115 backend_poll (EV_A_ waittime); 2135 backend_poll (EV_A_ waittime);
2131 2151
2132 /* queue check watchers, to be executed first */ 2152 /* queue check watchers, to be executed first */
2133 if (expect_false (checkcnt)) 2153 if (expect_false (checkcnt))
2134 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 2154 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
2135 2155
2136 call_pending (EV_A); 2156 invoke_cb (EV_A);
2137 } 2157 }
2138 while (expect_true ( 2158 while (expect_true (
2139 activecnt 2159 activecnt
2140 && !loop_done 2160 && !loop_done
2141 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 2161 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK))
2142 )); 2162 ));
2143 2163
2144 if (loop_done == EVUNLOOP_ONE) 2164 if (loop_done == EVUNLOOP_ONE)
2145 loop_done = EVUNLOOP_CANCEL; 2165 loop_done = EVUNLOOP_CANCEL;
2166
2167 --loop_depth;
2146} 2168}
2147 2169
2148void 2170void
2149ev_unloop (EV_P_ int how) 2171ev_unloop (EV_P_ int how)
2150{ 2172{
2242} 2264}
2243 2265
2244inline_size void 2266inline_size void
2245pri_adjust (EV_P_ W w) 2267pri_adjust (EV_P_ W w)
2246{ 2268{
2247 int pri = w->priority; 2269 int pri = ev_priority (w);
2248 pri = pri < EV_MINPRI ? EV_MINPRI : pri; 2270 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
2249 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; 2271 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
2250 w->priority = pri; 2272 ev_set_priority (w, pri);
2251} 2273}
2252 2274
2253inline_speed void 2275inline_speed void
2254ev_start (EV_P_ W w, int active) 2276ev_start (EV_P_ W w, int active)
2255{ 2277{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines