Skip to content

timer #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
huenchao opened this issue Nov 9, 2019 · 3 comments
Open

timer #4

huenchao opened this issue Nov 9, 2019 · 3 comments

Comments

@huenchao
Copy link
Owner

huenchao commented Nov 9, 2019

No description provided.

@huenchao
Copy link
Owner Author

huenchao commented Nov 9, 2019

@huenchao
Copy link
Owner Author

huenchao commented Nov 9, 2019

  1. timer其实分c++和js两个部分。具体比较的实现在c++和js层面都是一样的,用的链表+最小二叉堆。
  2. js层面会有一个map,这个map根据传进来的timeout为key,组层N个列表。
  3. js和c++怎么连在一起的呢?主要是通过scheduleTimer,其实最终就是调用 uv_timer_start(timer_handle(), RunTimers, duration_ms, 0);RunTimers从哪里来?其实是internal/bootstrap/nodejs的时候,注册进来的:
// We initialize the tick callbacks and the timer callbacks last during
// bootstrap to make sure that any operation done before this are synchronous.
// If any ticks or timers are scheduled before this they are unlikely to work.
{
 const { nextTick, runNextTicks } = setupTaskQueue();
 process.nextTick = nextTick;
 // Used to emulate a tick manually in the JS land.
 // A better name for this function would be `runNextTicks` but
 // it has been exposed to the process object so we keep this legacy name
 // TODO(joyeecheung): either remove it or make it public
 process._tickCallback = runNextTicks;

 const { getTimerCallbacks } = require('internal/timers');
 const { setupTimers } = internalBinding('timers');
 const { processImmediate, processTimers } = getTimerCallbacks(runNextTicks);
 // Sets two per-Environment callbacks that will be run from libuv:
 // - processImmediate will be run in the callback of the per-Environment
 //   check handle.
 // - processTimers will be run in the callback of the per-Environment timer.
 setupTimers(processImmediate, processTimers);
 // Note: only after this point are the timers effective
}

@huenchao
Copy link
Owner Author

huenchao commented Nov 9, 2019

我自己总计起来就是,js层面宏观控制,libuv部分微观控制。因为通过scheduleTimer 的调用是有条件的。除了第一次必定会注册到libuv里,其他都必须过期时间,比预期的长,才会注册。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant