Table of Contents generated with DocToc
- are handles and thus inherit
uv_handle_t
omitted error handling for brevity
uv_timer_t timer;
int repeat = 0;
int timeout = 100; // in ms
// initialize and start
r = uv_timer_init(uv_default_loop(), &timer);
// repeat is 0 to fire once or greater to fire multiple times
r = uv_timer_start(timer, timer_cb, timeout, repeat);
// run event loop
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
// no need to explicitly stop timer if they run to completion (including all repeats)
If a timer is stopped before the event loop is run, it'll never fire.
- initializes timer handle with the provided
loop
viauv__handle_init
- sets
timer_cb
andrepeat
to default values
- checks for given
cb
otherwise bails - if handle is active, stops it first via
uv_timer_stop
- adjusts given timeout
- sets
timer_cb
,timeout
andrepeat
- sets
start_id
and increasesloop->timer_counter
- adds handle to the heap
- starts handle via
uv__handle_start
NOTE: repeat
is a flag (on/off) NOT the number of repeats
- returns if handle isn't active
- removes handle from heap
- stops handle via
uv__handle_stop
- only for repeating timers does the following
- stops timer
- starts timer via
uv_timer_start
- timers are run in this function when it is called by
uv_run
right afteruv__update_time
- if
mode == UV_RUN_ONCE
, they are executed twice and then the loop is exited - if
mode == UV_RUN_NOWAIT
, they are executed once and then the loop is exited - if
mode == UV_RUN_DEFAULT
, thery are executed indefinitely until a loop stop is signaled - read this comment for more info
- if
- for each timer on the heap
- if timer expired does nothing
- otherwise it stops the timer, runs
uv_timer_again
and invokes thetimer_cb
- returns
-1
if no timers are found - return
0
if next timer expired - otherwise returns time until next timer expires
- assigns loop and type to given handle
- references handle
- adds handle to the end of the queue
#define uv__handle_init(loop_, h, type_) \
do { \
(h)->loop = (loop_); \
(h)->type = (type_); \
(h)->flags = UV__HANDLE_REF; /* Ref the loop when active. */ \
QUEUE_INSERT_TAIL(&(loop_)->handle_queue, &(h)->handle_queue); \
uv__handle_platform_init(h); \
} \
while (0)
- assumes handle isn't closing
- breaks if handle already active
- activates handle
- if handle is referenced adds handle to active handles
#define uv__handle_start(h) \
do { \
assert(((h)->flags & UV__HANDLE_CLOSING) == 0); \
if (((h)->flags & UV__HANDLE_ACTIVE) != 0) break; \
(h)->flags |= UV__HANDLE_ACTIVE; \
if (((h)->flags & UV__HANDLE_REF) != 0) uv__active_handle_add(h); \
} \
while (0)
- assumes handle isn't closing
- breaks if handle isn't active
- deactivates handle
- if handle is referenced removes handle from active handles
#define uv__handle_stop(h) \
do { \
assert(((h)->flags & UV__HANDLE_CLOSING) == 0); \
if (((h)->flags & UV__HANDLE_ACTIVE) == 0) break; \
(h)->flags &= ~UV__HANDLE_ACTIVE; \
if (((h)->flags & UV__HANDLE_REF) != 0) uv__active_handle_rm(h); \
} \
while (0)
- breaks if handle is referenced
- references handle
- breaks if handle is closing
- if handle is active adds it to active handles
#define uv__handle_ref(h) \
do { \
if (((h)->flags & UV__HANDLE_REF) != 0) break; \
(h)->flags |= UV__HANDLE_REF; \
if (((h)->flags & UV__HANDLE_CLOSING) != 0) break; \
if (((h)->flags & UV__HANDLE_ACTIVE) != 0) uv__active_handle_add(h); \
} \
while (0)
- breaks if handle is not referenced
- dereferences handle
- breaks if handle is closing
- if handle is active removes it from active handles
#define uv__handle_unref(h) \
do { \
if (((h)->flags & UV__HANDLE_REF) == 0) break; \
(h)->flags &= ~UV__HANDLE_REF; \
if (((h)->flags & UV__HANDLE_CLOSING) != 0) break; \
if (((h)->flags & UV__HANDLE_ACTIVE) != 0) uv__active_handle_rm(h); \
} \
while (0)
UV_RUN_DEFAULT
: Runs the event loop until there are no more active and referenced handles or requests. Always returns zero.UV_RUN_ONCE
: Poll for i/o once. Note that this function blocks if there are no pending callbacks. Returns zero when done (no active handles or requests left), or non-zero if more callbacks are expected (meaning you should run the event loop again sometime in the future).UV_RUN_NOWAIT
: Poll for i/o once but don’t block if there are no pending callbacks. Returns zero if done (no active handles or requests left), or non-zero if more callbacks are expected (meaning you should run the event loop again sometime in the future).
- checks if loop is alive and if not
uv__update_time
- runs the following until
loop->stop_flag == 0
-
- update time, run timers
-
- run pending and assign result to
ran_pending
flag which will be set unless theQUEUE
was empty
- run pending and assign result to
-
- idle and prepare loop
-
- get timeout via
uv_backend_timeout
- get timeout via
-
- polls io for given timeout
-
- finishes closing for all
loop->closing_handles
- finishes closing for all
-
- checks if loop is alive and if not
uv__update_time
- runs the following once
- 1 - 3 as above
- get timeout via
uv_backend_timeout
if pending reqs or handles, otherwise leave timeout as0
- 5 -6 as above
- update time, run timers one last time
- checks if loop is alive and if not
uv__update_time
- runs the following once
- 1 - 3 as above
- 5 -6 as above
- returns
0
if either of the following are true- the loop stop flag is set
- no more handles or requests are active
- the
QUEUE
ofidle_handles
is NOT empty - the loop has
closing_handles
uv__next_timeout
returns0