深入了解setTimeout源码之前,本有两个选择。一是通过chromium源码分析,二是通过Node.js源码分析。后来发现第一种方案的源码获取成本太大,于是从Node官网获取了几十兆的代码用来了解。

当前的Node版本为:v10.16.0

setTimeout方法定义于timers.js文件中,源码整体如下:

function setTimeout(callback, after, arg1, arg2, arg3) {// 基础校验if (typeof callback !== 'function') {throw new ERR_INVALID_CALLBACK();}// 参数校验var i, args;switch (arguments.length) {// fast casescase 1:case 2:break;case 3:args = [arg1];break;case 4:args = [arg1, arg2];break;default:args = [arg1, arg2, arg3];for (i = 5; i < arguments.length; i++) {// extend array dynamically, makes .apply run much faster in v6.0.0args[i - 2] = arguments[i];}break;}// 新建Timeout对象const timeout = new Timeout(callback, after, args, false, false);active(timeout);// 最后返回Timeout对象return timeout;
}

Timeout构造函数内部如下:

// Timer constructor function.
// The entire prototype is defined in lib/timers.js
function Timeout(callback, after, args, isRepeat, isUnrefed) {after *= 1; // coalesce to number or NaNif (!(after >= 1 && after <= TIMEOUT_MAX)) {if (after > TIMEOUT_MAX) {process.emitWarning(`${after} does not fit into` +' a 32-bit signed integer.' +'\nTimeout duration was set to 1.','TimeoutOverflowWarning');}after = 1; // schedule on next tick, follows browser behavior}this._called = false;this._idleTimeout = after;this._idlePrev = this;this._idleNext = this;this._idleStart = null;// this must be set to null first to avoid function tracking// on the hidden class, revisit in V8 versions after 6.2this._onTimeout = null;this._onTimeout = callback;this._timerArgs = args;this._repeat = isRepeat ? after : null;this._destroyed = false;this[unrefedSymbol] = isUnrefed;initAsyncResource(this, 'Timeout');
}

我们这里分析只关注两个参数:1.callback, 2.after

  this._idleTimeout = after;this._onTimeout = callback;

基本初始化完成,进入active方法。active方法的item参数为新new的Timeout对象。

// Schedule or re-schedule a timer.
// The item must have been enroll()'d first.
const active = exports.active = function(item) {insert(item, false);
};

进入insert方法,item = Timeout对象, unrefed = false, start = undefined.

// The underlying logic for scheduling or re-scheduling a timer.
//
// Appends a timer onto the end of an existing timers list, or creates a new
// TimerWrap backed list if one does not already exist for the specified timeout
// duration.
function insert(item, unrefed, start) { // timeout, false // 对after做校验const msecs = item._idleTimeout;if (msecs < 0 || msecs === undefined) return;if (typeof start === 'number') {item._idleStart = start;} else {item._idleStart = TimerWrap.now();}const lists = unrefed === true ? unrefedLists : refedLists;// Use an existing list if there is one, otherwise we need to make a new one.var list = lists[msecs];if (list === undefined) {debug('no %d list was found in insert, creating a new one', msecs);lists[msecs] = list = new TimersList(msecs, unrefed);}if (!item[async_id_symbol] || item._destroyed) {item._destroyed = false;initAsyncResource(item, 'Timeout');}L.append(list, item); // list = timerlist, item = timeout, 增加一个节点在队列中,节点类型不同。assert(!L.isEmpty(list)); // list is not empty
}

TimerWrap.now()方法返回的应当是当前的时间,具体的执行代码为:

  static void Now(const FunctionCallbackInfo<Value>& args) {Environment* env = Environment::GetCurrent(args);args.GetReturnValue().Set(env->GetNow());}

这时,Timeout对象有三个关键属性:

  item._idleTimeout = after; // 延迟多少秒执行item._onTimeout = callback; // 延迟执行回调函数item._idleStart = TimerWrap.now(); // 当下时间

然后进行到lists[after] = refedLists[after] = list = new TimersList(after, false);

也就是说refedLists对象的after属性对应一个TimersList对象,而refedLists对象是全局的。

function TimersList(msecs, unrefed) {this._idleNext = this; // Create the list with the linkedlist properties tothis._idlePrev = this; // prevent any unnecessary hidden class changes.this._unrefed = unrefed;this.msecs = msecs;const timer = this._timer = new TimerWrap();timer._list = this;if (unrefed === true)timer.unref();timer.start(msecs);
}

可以将TimersList对象视作为一个双向链表节点,它内部有指向上下节点的指针,当一个节点新建时,这个节点的的上下节点会指向自己。节点的内容为:

  this.msecs = after;this._timer = new TimerWrap(); // 这里的TimerWrap为一个Native对象timer._list = this;

最后到timer.start(after),它的函数内部如下:

  static void Start(const FunctionCallbackInfo<Value>& args) {TimerWrap* wrap;ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());CHECK(HandleWrap::IsAlive(wrap));int64_t timeout = args[0]->IntegerValue(); // 这里的timeout为js代码传入的afterint err = uv_timer_start(&wrap->handle_, OnTimeout, timeout, 0);args.GetReturnValue().Set(err);}

这里关键的地方是uv_timer_start,方法的内部如下:

int uv_timer_start(uv_timer_t* handle,uv_timer_cb cb,uint64_t timeout,uint64_t repeat) {uint64_t clamped_timeout;if (cb == NULL)return UV_EINVAL;if (uv__is_active(handle))uv_timer_stop(handle);// 这里是关键。clamped_timeout的值等于当前时间加上未来要执行的时间clamped_timeout = handle->loop->time + timeout;if (clamped_timeout < timeout)clamped_timeout = (uint64_t) -1;handle->timer_cb = cb;handle->timeout = clamped_timeout; // timeout为未来要执行的时间handle->repeat = repeat;/* start_id is the second index to be compared in uv__timer_cmp() */handle->start_id = handle->loop->timer_counter++;heap_insert(timer_heap(handle->loop),(struct heap_node*) &handle->heap_node,timer_less_than);uv__handle_start(handle);return 0;
}

这里我关注的是传入参数是怎么被操作的:

  handle->timer_cb = cb;handle->timeout = clamped_timeout;

好,到这里设置完成,我们回到insert方法内部继续向下,继续执行:

  L.append(list, item); // list = timerlist, item = timeout, 增加一个节点在队列中,节点类型不同。

append方法将item追加到了list中。list对象是一个由item节点组成的双向链表。然后到这里添加结束。

你可能会疑惑,到这里就结束了?其实过程中有很多细节被我们忽略了,不过没关系。既然L.append用来追加节点,那它一定要取出节点,我们从上下文可知:

listOnTimeout方法中取出了这个节点(这个过程后面再涉及):

function listOnTimeout(handle, now) {const list = handle._list;const msecs = list.msecs;debug('timeout callback %d', msecs);debug('now: %d', now);var diff, timer;while (timer = L.peek(list)) {diff = now - timer._idleStart;// Check if this loop iteration is too early for the next timer.// This happens if there are more timers scheduled for later in the list.if (diff < msecs) {var timeRemaining = msecs - (TimerWrap.now() - timer._idleStart);if (timeRemaining <= 0) {timeRemaining = 1;}handle.start(timeRemaining);debug('%d list wait because diff is %d', msecs, diff);return true;}// The actual logic for when a timeout happens.L.remove(timer);assert(timer !== L.peek(list));if (!timer._onTimeout) {if (destroyHooksExist() && !timer._destroyed &&typeof timer[async_id_symbol] === 'number') {emitDestroy(timer[async_id_symbol]);timer._destroyed = true;}continue;}tryOnTimeout(timer);}// If `L.peek(list)` returned nothing, the list was either empty or we have// called all of the timer timeouts.// As such, we can remove the list and clean up the TimerWrap C++ handle.debug('%d list empty', msecs);assert(L.isEmpty(list));// Either refedLists[msecs] or unrefedLists[msecs] may have been removed and// recreated since the reference to `list` was created. Make sure they're// the same instance of the list before destroying.if (list._unrefed === true && list === unrefedLists[msecs]) {delete unrefedLists[msecs];} else if (list === refedLists[msecs]) {delete refedLists[msecs];}// Do not close the underlying handle if its ownership has changed// (e.g it was unrefed in its callback).if (!handle[owner_symbol])handle.close();return true;
}

listOnTimeout方法其实是整个JS层处理队列事件的核心。方法内部的handle对象实为TimerWrap。handle._list为TimersList。方法内的msecs为after。接下来while不断从TimersList中取timer。peek总是返回队首的数据。

然后到了关键处理阶段:

    diff = now - timer._idleStart; // 计算方法执行时的差额// 如果时间还不到,则进行:if (diff < msecs) {var timeRemaining = msecs - (TimerWrap.now() - timer._idleStart);if (timeRemaining <= 0) {timeRemaining = 1;}// 计算出剩余时间,再次执行Native的start方法。handle.start(timeRemaining);debug('%d list wait because diff is %d', msecs, diff);return true;}

handle.start方法的内部如下:

  static void Start(const FunctionCallbackInfo<Value>& args) {TimerWrap* wrap;ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());CHECK(HandleWrap::IsAlive(wrap));int64_t timeout = args[0]->IntegerValue();int err = uv_timer_start(&wrap->handle_, OnTimeout, timeout, 0);args.GetReturnValue().Set(err);}

这时在执行start方法时就是一个after距离真正执行的剩余时间,再次执行uv_timer_start方法,

还记得上文中提到的这段代码吗?这段代码位于uv_timer_start方法内。

  handle->timer_cb = cb;handle->timeout = clamped_timeout;

刚刚我们并没有继续深究,现在不得不深究一下。

上面说到timeout被赋值了,那一定有地方再取出它进行执行。通过上下文可知:

void uv__run_timers(uv_loop_t* loop) {struct heap_node* heap_node;uv_timer_t* handle;for (;;) {heap_node = heap_min(timer_heap(loop));if (heap_node == NULL)break;handle = container_of(heap_node, uv_timer_t, heap_node);// 这里为处理的关键。if (handle->timeout > loop->time)break;uv_timer_stop(handle);uv_timer_again(handle);handle->timer_cb(handle);}
}

uv__run_timers是一个无限循环的方法,内部永远在循环执行timer_cb方法。还记得上文中提到的clamped_timeout吗,如果clamped_timeout为任务触发的时间,这里的无限循环一直在判断时间是否到期,如果到期了则会向下执行,否则一直循环。

这个方法是如何被触发的我们暂时不在这里深究。

我们从上文的代码可以知道,上文中的timer_cb是OnTimeout方法,它的内部实现如下:

  static void OnTimeout(uv_timer_t* handle) {TimerWrap* wrap = static_cast<TimerWrap*>(handle->data);Environment* env = wrap->env();HandleScope handle_scope(env->isolate());Context::Scope context_scope(env->context());MaybeLocal<Value> ret;Local<Value> args[1];do {// 这里是关键所在args[0] = env->GetNow(); // 获取当前时间ret = wrap->MakeCallback(env->timers_callback_function(), 1, args); // 执行调用} while ((ret.IsEmpty() || ret.ToLocalChecked()->IsUndefined()) &&!env->tick_info()->has_thrown() &&env->can_call_into_js() &&wrap->object()->Get(env->context(),env->owner_symbol()).ToLocalChecked()->IsUndefined());}

这里的timers_callback_function()为js层传入的processTimers方法,设置的代码位于lib/timers.js文件中:

const [immediateInfo, toggleImmediateRef] =setupTimers(processImmediate, processTimers);

processTimers内部如下:

function processTimers(now) {if (this[owner_symbol])return unrefdHandle(this[owner_symbol], now);return listOnTimeout(this, now);
}

到这里是不是见过listOnTimeout方法?没错,我们回到了listOnTimeout方法调用处。这个从listOnTimeout方法到listOnTimeout方法会不断循环,直到if (diff < msecs) 条件不成立。也就是说当条件成立时才会继续执行。

真正的业务回调代码如下:

 // 将这次的timer任务从队列中取出L.remove(timer);assert(timer !== L.peek(list));// 这里不成立if (!timer._onTimeout) {if (destroyHooksExist() && !timer._destroyed &&typeof timer[async_id_symbol] === 'number') {emitDestroy(timer[async_id_symbol]);timer._destroyed = true;}continue;}// 关键在于这里tryOnTimeout(timer);
function tryOnTimeout(timer, start) {timer._called = true;const timerAsyncId = (typeof timer[async_id_symbol] === 'number') ?timer[async_id_symbol] : null;var threw = true;if (timerAsyncId !== null)emitBefore(timerAsyncId, timer[trigger_async_id_symbol]);try {ontimeout(timer, start);threw = false;} finally {if (timerAsyncId !== null) {if (!threw)emitAfter(timerAsyncId);if ((threw || !timer._repeat) && destroyHooksExist() &&!timer._destroyed) {emitDestroy(timerAsyncId);timer._destroyed = true;}}}
}

这里的关键在于ontimeout方法,该方法内部实现如下:

function ontimeout(timer, start) {const args = timer._timerArgs;if (typeof timer._onTimeout !== 'function')return Promise.resolve(timer._onTimeout, args[0]);if (start === undefined && timer._repeat)start = TimerWrap.now();if (!args)timer._onTimeout();elseReflect.apply(timer._onTimeout, timer, args);if (timer._repeat)rearm(timer, start);
}

上面的方法执行javascript timer._onTimeout();这里是真正的回调执行。

至此,一个普通的setTimeout方法执行完毕。

最后总结一下调用流程图:

深入解析Node.js setTimeout方法的执行过程相关推荐

  1. JS引擎线程的执行过程的三个阶段

    浏览器首先按顺序加载由<script>标签分割的js代码块,加载js代码块完毕后,立刻进入以下三个阶段,然后再按顺序查找下一个代码块,再继续执行以下三个阶段,无论是外部脚本文件(不异步加载 ...

  2. JS引擎线程的执行过程的三个阶段(二)

    继续 JS引擎线程的执行过程的三个阶段(一) 内容, 如下: 三. 执行阶段 1. 网页的线程 永远只有JS引擎线程在执行JS脚本程序,其他三个线程只负责将满足触发条件的处理函数推进事件队列,等待JS ...

  3. java 调用对象的方法_JAVA调用对象方法的执行过程

    JAVA调用对象方法的执行过程: ①.编译器查看对象的声明类型和方法名.假设调用x.f(parameter),  且隐式参数x声明为C类型的对象,有可能在C对象中存在多个参数类型和参数个数不同的f的方 ...

  4. 1.简述一个Activity跳转到另一个Activity时,两个Activity生命周期方法的执行过程。2.编写一个程序,要求在第一个界面中输入两个数字,在第二个界面显示第一个界面两个数字的和。

    1.简述一个Activity跳转到另一个Activity时,两个Activity生命周期方法的执行过程. 首先,我创建了一个MainActivity和SecondActivity两个Activity. ...

  5. 利用setTimeout方法控制JS中方法的执行顺序

    JS方面中有A和B方法,B必须在A执行完之后才能执行,怎么保证呢? 1.首先设一个标志(isAFinish),用来标识A是否执行完. 2.B方法执行的时候,判断 var isAFinish=false ...

  6. c语言socket句柄函数传递,通过源码解析 Node.js 中进程间通信中的 socket 句柄传递...

    在 Node.js 中,当我们使用 child_process 模块创建子进程后,会返回一个 ChildProcess 类的实例,通过调用 ChildProcess#send(message[, se ...

  7. 解析Node.js v6.9.5官方文档的第一个例子的知识点

    2019独角兽企业重金招聘Python工程师标准>>> 这段代码是nodejs界的"hello word",使用nodejs搭建了服务器并返回"hell ...

  8. 解析Node.js通过axios实现网络请求

    本次给大家分享一篇node.js通过axios实现网络请求的方法,写的十分的全面细致,具有一定的参考价值,对此有需要的朋友可以参考学习下.如有不足之处,欢迎批评指正. 1.使用Npm 下载axios ...

  9. ATECC508A芯片开发笔记(八):ECDH算法配置方法、执行过程及实现原理

    目录 AATECC508A芯片开发笔记(八):ECDH算法配置方法.过程原理及示例代码 1.ECDH介绍及原理 2.ECDH执行过程 3.508A某一slot执行Ecdh需要配置的参数 4.示例代码 ...

最新文章

  1. pytorch maxout实现
  2. python 协程原理_Python协程greenlet实现原理
  3. 【利好工具】JavaScript及时运行调试工具
  4. Universal-Image-Loader,android-Volley,Picasso、Fresco和Glide开源组件加载网络图片的优缺点比较...
  5. OPT和LRU页面置换算法C语言代码,页面置换算法模拟——OPT、FIFO和LRU算法.doc
  6. java: cannot execute binary file 如果遇到这个错,一般是操作系统位数出问题了。
  7. C#LeetCode刷题之#189-旋转数组(Rotate Array)
  8. iPhone 12 Pro长这样:重回iPhone 5外观 心动了
  9. 【转】常见面试之机器学习算法思想简单梳理
  10. 在多个线程中使用QNetworkAccessManager
  11. 多行文字cad提取数据_怎样从cad中提取excel表格数据-如何快速提取CAD图纸表格数据...
  12. wifi模块php,什么是Wifi模块 Wifi模块功能有哪些
  13. 硬盘格式化数据恢复的软件推荐
  14. ZigBee无线通信--BasicRF无线点对点通信
  15. 特斯拉Q4财报:底部反弹70%,为信仰打call
  16. pdf如何做到批量格式转换?
  17. 技术学习:Python(21)|爬虫篇|selenium自动化操作浏览器
  18. CHROME源码剖析 上《转》
  19. 基于Umi的移动端框架Alita-v2.6.7源码
  20. 我的Android开发半年工作经验总结

热门文章

  1. 互斥锁mutex的使用方法
  2. 在STM32单片机上跑神经网络算法
  3. Linux ALSA 图解
  4. 一文带你了解V4L2
  5. C语言中sizeof详解——面试C/C++
  6. nyoj91 阶乘之和
  7. centos 启动一个redis_基于prometheus+grafana体系监控redis缓存服务
  8. LeetCode 270. 最接近的二叉搜索树值
  9. LeetCode 525. 连续数组(前缀和+哈希)
  10. LeetCode 1310. 子数组异或查询(前缀异或)