… | |
… | |
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 |
338 | extern "C" { |
345 | extern "C" { |
… | |
… | |
557 | |
564 | |
558 | #endif |
565 | #endif |
559 | |
566 | |
560 | /*****************************************************************************/ |
567 | /*****************************************************************************/ |
561 | |
568 | |
|
|
569 | #ifndef EV_HAVE_EV_TIME |
562 | ev_tstamp |
570 | ev_tstamp |
563 | ev_time (void) |
571 | ev_time (void) |
564 | { |
572 | { |
565 | #if EV_USE_REALTIME |
573 | #if EV_USE_REALTIME |
566 | if (expect_true (have_realtime)) |
574 | if (expect_true (have_realtime)) |
… | |
… | |
573 | |
581 | |
574 | struct timeval tv; |
582 | struct timeval tv; |
575 | gettimeofday (&tv, 0); |
583 | gettimeofday (&tv, 0); |
576 | return tv.tv_sec + tv.tv_usec * 1e-6; |
584 | return tv.tv_sec + tv.tv_usec * 1e-6; |
577 | } |
585 | } |
|
|
586 | #endif |
578 | |
587 | |
579 | inline_size ev_tstamp |
588 | inline_size ev_tstamp |
580 | get_clock (void) |
589 | get_clock (void) |
581 | { |
590 | { |
582 | #if EV_USE_MONOTONIC |
591 | #if EV_USE_MONOTONIC |
… | |
… | |
618 | |
627 | |
619 | tv.tv_sec = (time_t)delay; |
628 | tv.tv_sec = (time_t)delay; |
620 | tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); |
629 | tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); |
621 | |
630 | |
622 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
631 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
623 | /* somehting nto guaranteed by newer posix versions, but guaranteed */ |
632 | /* somehting not guaranteed by newer posix versions, but guaranteed */ |
624 | /* by older ones */ |
633 | /* by older ones */ |
625 | select (0, 0, 0, 0, &tv); |
634 | select (0, 0, 0, 0, &tv); |
626 | #endif |
635 | #endif |
627 | } |
636 | } |
628 | } |
637 | } |
… | |
… | |
1349 | ev_loop_count (EV_P) |
1358 | ev_loop_count (EV_P) |
1350 | { |
1359 | { |
1351 | return loop_count; |
1360 | return loop_count; |
1352 | } |
1361 | } |
1353 | |
1362 | |
|
|
1363 | unsigned int |
|
|
1364 | ev_loop_depth (EV_P) |
|
|
1365 | { |
|
|
1366 | return loop_depth; |
|
|
1367 | } |
|
|
1368 | |
1354 | void |
1369 | void |
1355 | ev_set_io_collect_interval (EV_P_ ev_tstamp interval) |
1370 | ev_set_io_collect_interval (EV_P_ ev_tstamp interval) |
1356 | { |
1371 | { |
1357 | io_blocktime = interval; |
1372 | io_blocktime = interval; |
1358 | } |
1373 | } |
… | |
… | |
2011 | |
2026 | |
2012 | mn_now = ev_rt_now; |
2027 | mn_now = ev_rt_now; |
2013 | } |
2028 | } |
2014 | } |
2029 | } |
2015 | |
2030 | |
2016 | static int loop_done; |
|
|
2017 | |
|
|
2018 | void |
2031 | void |
2019 | ev_loop (EV_P_ int flags) |
2032 | ev_loop (EV_P_ int flags) |
2020 | { |
2033 | { |
|
|
2034 | ++loop_depth; |
|
|
2035 | |
2021 | loop_done = EVUNLOOP_CANCEL; |
2036 | loop_done = EVUNLOOP_CANCEL; |
2022 | |
2037 | |
2023 | call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ |
2038 | call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ |
2024 | |
2039 | |
2025 | do |
2040 | do |
… | |
… | |
2066 | ev_tstamp waittime = 0.; |
2081 | ev_tstamp waittime = 0.; |
2067 | ev_tstamp sleeptime = 0.; |
2082 | ev_tstamp sleeptime = 0.; |
2068 | |
2083 | |
2069 | if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) |
2084 | if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) |
2070 | { |
2085 | { |
|
|
2086 | /* remember old timestamp for io_blocktime calculation */ |
|
|
2087 | ev_tstamp prev_mn_now = mn_now; |
|
|
2088 | |
2071 | /* update time to cancel out callback processing overhead */ |
2089 | /* update time to cancel out callback processing overhead */ |
2072 | time_update (EV_A_ 1e100); |
2090 | time_update (EV_A_ 1e100); |
2073 | |
2091 | |
2074 | waittime = MAX_BLOCKTIME; |
2092 | waittime = MAX_BLOCKTIME; |
2075 | |
2093 | |
… | |
… | |
2085 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; |
2103 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; |
2086 | if (waittime > to) waittime = to; |
2104 | if (waittime > to) waittime = to; |
2087 | } |
2105 | } |
2088 | #endif |
2106 | #endif |
2089 | |
2107 | |
|
|
2108 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
2090 | if (expect_false (waittime < timeout_blocktime)) |
2109 | if (expect_false (waittime < timeout_blocktime)) |
2091 | waittime = timeout_blocktime; |
2110 | waittime = timeout_blocktime; |
2092 | |
2111 | |
2093 | sleeptime = waittime - backend_fudge; |
2112 | /* extra check because io_blocktime is commonly 0 */ |
2094 | |
|
|
2095 | if (expect_true (sleeptime > io_blocktime)) |
2113 | if (expect_false (io_blocktime)) |
2096 | sleeptime = io_blocktime; |
|
|
2097 | |
|
|
2098 | if (sleeptime) |
|
|
2099 | { |
2114 | { |
|
|
2115 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
|
|
2116 | |
|
|
2117 | if (sleeptime > waittime - backend_fudge) |
|
|
2118 | sleeptime = waittime - backend_fudge; |
|
|
2119 | |
|
|
2120 | if (expect_true (sleeptime > 0.)) |
|
|
2121 | { |
2100 | ev_sleep (sleeptime); |
2122 | ev_sleep (sleeptime); |
2101 | waittime -= sleeptime; |
2123 | waittime -= sleeptime; |
|
|
2124 | } |
2102 | } |
2125 | } |
2103 | } |
2126 | } |
2104 | |
2127 | |
2105 | ++loop_count; |
2128 | ++loop_count; |
2106 | backend_poll (EV_A_ waittime); |
2129 | backend_poll (EV_A_ waittime); |
… | |
… | |
2132 | && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) |
2155 | && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) |
2133 | )); |
2156 | )); |
2134 | |
2157 | |
2135 | if (loop_done == EVUNLOOP_ONE) |
2158 | if (loop_done == EVUNLOOP_ONE) |
2136 | loop_done = EVUNLOOP_CANCEL; |
2159 | loop_done = EVUNLOOP_CANCEL; |
|
|
2160 | |
|
|
2161 | --loop_depth; |
2137 | } |
2162 | } |
2138 | |
2163 | |
2139 | void |
2164 | void |
2140 | ev_unloop (EV_P_ int how) |
2165 | ev_unloop (EV_P_ int how) |
2141 | { |
2166 | { |