How do libuv and Node.js actually schedule timers?

How does libuv and the operating system actually schedule timers like setTimeout and setInterval in Node.js? I see that no cpu is being used by the node process until the timer fires. Does this mean the OS schedules the timer and wakes up the node process when the timer starts? If so, how does the OS schedule the timer and how exactly does the hardware execute it?

+3


source to share


1 answer


Node uses the libuv at the bottom to take care of this. While setTimeout has its own internal controls, it ends up using the uv_timer_t facility provided by libuv.

Let's assume that the only thing the event loop does is a timer. libuv will calculate the poll timeout, which will actually be the timer time (in this example). The event loop will then block for I / O using the appropriate syscall (epoll_wait, kevent, etc.). At this point, the kernel will decide what to do, but the current thread of execution is blocked until the kernel wakes it up again, so there is no processor here because nothing is happening.



Once the timeout expires, the above syscall will return and libuv will process the appropriate timers and i / o.

+4


source







All Articles