经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
深入出不来nodejs源码-timer模块(C++篇)
来源:cnblogs  作者:书生小龙  时间:2018/12/10 9:46:51  对本文有异议

  终于可以填上坑了。

  简单回顾一下之前JS篇内容,每一次setTimeout的调用,会在一个对象中添加一个键值对,键为延迟时间,值为一个链表,将所有该时间对应的事件串起来,图如下:

  而每一个延迟键值对的触发,则是在链表头生成的时候就已经开始了,如下:

  1. function TimersList(msecs, unrefed) {
  2. //...
  3.  
  4. // 来源于C++内置模块
  5. const timer = this._timer = new TimerWrap();
  6. /// ...
  7. // 触发
  8. timer.start(msecs);
  9. }

  回顾完毕。

 

  与JS篇一样,这一节也简单介绍libuv内部的一个数据结构:二叉树。源码来源于:uv/src/heap-inl.h。

  因为二叉树的介绍网上一堆,所以这里只看一下API,首先是节点:

  1. struct heap_node {
  2. struct heap_node* left;
  3. struct heap_node* right;
  4. struct heap_node* parent;
  5. };

  分别代表左右、父节点。

  1. /* A binary min heap. The usual properties hold: the root is the lowest
  2. * element in the set, the height of the tree is at most log2(nodes) and
  3. * it's always a complete binary tree.
  4. *
  5. * The heap function try hard to detect corrupted tree nodes at the cost
  6. * of a minor reduction in performance. Compile with -DNDEBUG to disable.
  7. */
  8. struct heap {
  9. struct heap_node* min;
  10. unsigned int nelts;
  11. };

  这里的注释可以看一下,这个结构体是独立的,min指向当前树的最小值。

  另外还有三个操作方法:

  1. HEAP_EXPORT(void heap_insert(struct heap* heap,
  2. struct heap_node* newnode,
  3. heap_compare_fn less_than));
  4. HEAP_EXPORT(void heap_remove(struct heap* heap,
  5. struct heap_node* node,
  6. heap_compare_fn less_than));
  7. HEAP_EXPORT(void heap_dequeue(struct heap* heap, heap_compare_fn less_than));

  分别代表树的插入、移除,以及将小于指定值的节点移除并重新整理树,实现自己去看,懒得讲。

 

  进入正题,从JS的触发代码开始看。

  废话不多说,直接进入timer_wrapper.cc看start方法,源码如下:

  1. static void Start(const FunctionCallbackInfo<Value>& args) {
  2. // 不管这3行
  3. TimerWrap* wrap;
  4. ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
  5. CHECK(HandleWrap::IsAlive(wrap));
  6. // 这个args就是JS函数参数的包装 可以理解成数组
  7. int64_t timeout = args[0]->IntegerValue();
  8. // libuv的方法 第三个参数代表延迟时间
  9. int err = uv_timer_start(&wrap->handle_, OnTimeout, timeout, 0);
  10. // 设置该函数返回值
  11. args.GetReturnValue().Set(err);
  12. }

  可以看到,这里涉及到了libuv,继续深入,看该方法:

  1. /*
  2. handle => 时间模块对象
  3. cb => 延迟回调函数
  4. timeout => 延迟时间
  5. repeat => 区分interval/setTimeout
  6. */
  7. int uv_timer_start(uv_timer_t* handle,
  8. uv_timer_cb cb,
  9. uint64_t timeout,
  10. uint64_t repeat) {
  11. uint64_t clamped_timeout;
  12. if (cb == NULL)
  13. return UV_EINVAL;
  14. if (uv__is_active(handle))
  15. uv_timer_stop(handle);
  16. // 当前时间戳加上延迟的时间 也就是回调函数触发的时间戳
  17. clamped_timeout = handle->loop->time + timeout;
  18. if (clamped_timeout < timeout)
  19. clamped_timeout = (uint64_t)-1;
  20. // 对象赋值
  21. handle->timer_cb = cb;
  22. handle->timeout = clamped_timeout;
  23. handle->repeat = repeat;
  24. /* start_id is the second index to be compared in uv__timer_cmp() */
  25. handle->start_id = handle->loop->timer_counter++;
  26. // 注意这里,用了insert方法将对应的handle对象插入到了树中
  27. heap_insert(timer_heap(handle->loop),
  28. (struct heap_node*) &handle->heap_node,
  29. timer_less_than);
  30. uv__handle_start(handle);
  31. return 0;
  32. }

  简单说明一下,首先第一个参数可以直接当成个空对象,在一开始是啥都没有的。

  然后是clamped_timeout,在上一节中讲过,libuv内部获取的是一个相对时间,所以这里用当前轮询的时间点加上延迟时间,得到的就是理论上的触发时间点。

  而timer_cb就很好理解了,对应的是回调函数。

  repeat这个值,如果是setInterval,那么值为interval的间隔时间,setTimeout就是0,表示是否循环触发。

  最后将这几个值都挂载到handle上面,通过insert方法插入这一节一开始讲的树上。

 

  至此,一个setTimeout方法所完成的操作已经讲完了。

  显然我又错了,这个start并没有触发什么东西,最终只是把一个对象加到一个树结构上,那么又是在哪里触发的延迟调用呢?

  答案就在uv_run中,因为偷懒,所以之前没有贴完整代码,在每一轮的事件轮询中,有两个首要操作,如下:

  1. int uv_run(uv_loop_t *loop, uv_run_mode mode) {
  2. // ...略
  3.  
  4. while (r != 0 && loop->stop_flag == 0) {
  5. // 上一节的更新时间
  6. uv_update_time(loop);
  7. // 这一节的内容
  8. uv__run_timers(loop);
  9. // ...
  10. }
  11. }

  第一个就是上一节讲的更新时间,第二个就涉及到延迟触发了,进入源码看一下:

  1. void uv__run_timers(uv_loop_t* loop) {
  2. struct heap_node* heap_node;
  3. uv_timer_t* handle;
  4. // 死循环 保证触发所有应该触发的延迟事件
  5. for (;;) {
  6. // 该方法返回延迟事件树中最小的时间点
  7. heap_node = heap_min(timer_heap(loop));
  8. // 代表没有延迟事件
  9. if (heap_node == NULL)
  10. break;
  11. // 取出handle
  12. handle = container_of(heap_node, uv_timer_t, heap_node);
  13. // 比较handle的时间点与当前的时间点
  14. if (handle->timeout > loop->time)
  15. break;
  16. // 移除当前的handle
  17. uv_timer_stop(handle);
  18. // 如果是interval 需要重新插入一个新的handle到树中
  19. uv_timer_again(handle);
  20. // 触发延迟事件
  21. handle->timer_cb(handle);
  22. }
  23. }

  这里就把上面的树与事件轮询链接起来了,每一次轮询,首先触发的就是延迟事件,触发的方式就是去树里面找,有没有比当前时间点小的handle,取出一个,删除并触发。

  下面用一个图来总结一下:

  完结撒花!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号