As mentioned in the NodeJS doc, in poll phrase, node will block when appropriate. This post will discuss the block conditions and why this phrase blocks as Node should perfom a non-blocking I/O operations.
if (mode == UV_RUN_ONCE) { /* UV_RUN_ONCE implies forward progress: at least one callback must have * been invoked when it returns. uv__io_poll() can return without doing * I/O (meaning: no callbacks) when its timeout expires - which means we * have pending timers that satisfy the forward progress constraint. * * UV_RUN_NOWAIT makes no guarantees about progress so it's omitted from * the check. */ uv__update_time(loop); uv__run_timers(loop); }
r = uv__loop_alive(loop); if (mode == UV_RUN_ONCE || mode == UV_RUN_NOWAIT) break; }
/* The if statement lets gcc compile it to a conditional store. Avoids * dirtying a cache line. */ if (loop->stop_flag != 0) loop->stop_flag = 0;
return r; }
uv__io_poll takes timeout as the second argument. When the event loop runs UV_RUN_DEFAULT mode or runs under UV_RUN_ONCE mode with no pending query, timeout will be calculated with uv_backend_timeout function. Otherwise it is set to 0. Here’s the uv_backend_timeout code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
intuv_backend_timeout(constuv_loop_t* loop){ if (loop->stop_flag != 0) return0;
if (!uv__has_active_handles(loop) && !uv__has_active_reqs(loop)) return0;
if (!QUEUE_EMPTY(&loop->idle_handles)) return0;
if (!QUEUE_EMPTY(&loop->pending_queue)) return0;
if (loop->closing_handles) return0;
return uv__next_timeout(loop); }
(loop->stop_flag determines whether the event loop is about to exit.)
Timers are stored in heap, if timer stack is empty, it will block indefinitely. Otherwise it returns the closest timer, which means that the loop will only be blocked when there are no pending tasks to do and it will wait until the next timeout.