How is setTimeout implemented in node.js

后端 未结 2 1467
萌比男神i
萌比男神i 2020-12-04 14:39

I was wondering if anybody knows how setTimeout is implemented in node.js. I believe I have read somewhere that this is not part of V8. I quickly tried to find the implement

2条回答
  •  一向
    一向 (楼主)
    2020-12-04 15:20

    No problem with many timers! When uv loop call poll, it pass timeout argument to it with closest timer of all timers.

    [closest timer of all timers]
    https://github.com/joyent/node/blob/master/deps/uv/src/unix/timer.c #120

    RB_MIN(uv__timers, &loop->timer_handles)  
    

    [pass timeout argument to poll api]
    https://github.com/joyent/node/blob/master/deps/uv/src/unix/core.c #276

    timeout = 0;  
    if ((mode & UV_RUN_NOWAIT) == 0)  
        timeout = uv_backend_timeout(loop);  
    
    uv__io_poll(loop, timeout); 
    

    Note: on Windows OS, it's almost same logic

提交回复
热议问题