zoukankan      html  css  js  c++  java
  • 协程的实现之调度器

    一、协程如何被调度?

    调度器的实现,有两种方案,一种是生产者消费者模式,另一种多状态运行。

    1.1 生产者消费者模式

    逻辑代码如下:

     1 while (1) {
     2 
     3         //遍历睡眠集合,将满足条件的加入到ready
     4         nty_coroutine *expired = NULL;
     5         while ((expired = sleep_tree_expired(sched)) != ) {
     6             TAILQ_ADD(&sched->ready, expired);
     7         }
     8 
     9         //遍历等待集合,将满足添加的加入到ready
    10         nty_coroutine *wait = NULL;
    11         int nready = epoll_wait(sched->epfd, events, EVENT_MAX, 1);
    12         for (i = 0;i < nready;i ++) {
    13             wait = wait_tree_search(events[i].data.fd);
    14             TAILQ_ADD(&sched->ready, wait);
    15         }
    16 
    17         // 使用resume回复ready的协程运行权
    18         while (!TAILQ_EMPTY(&sched->ready)) {
    19             nty_coroutine *ready = TAILQ_POP(sched->ready);
    20             resume(ready);
    21         }
    22     }

    1.2 多状态运行

    实现逻辑代码如下:

     1 while (1) {
     2 
     3         //遍历睡眠集合,使用resume恢复expired的协程运行权
     4         nty_coroutine *expired = NULL;
     5         while ((expired = sleep_tree_expired(sched)) != ) {
     6             resume(expired);
     7         }
     8 
     9         //遍历等待集合,使用resume恢复wait的协程运行权
    10         nty_coroutine *wait = NULL;
    11         int nready = epoll_wait(sched->epfd, events, EVENT_MAX, 1);
    12         for (i = 0;i < nready;i ++) {
    13             wait = wait_tree_search(events[i].data.fd);
    14             resume(wait);
    15         }
    16 
    17         // 使用resume恢复ready的协程运行权
    18         while (!TAILQ_EMPTY(sched->ready)) {
    19             nty_coroutine *ready = TAILQ_POP(sched->ready);
    20             resume(ready);
    21         }
    22     }

    转载:

    https://blog.csdn.net/Yttsam/article/details/107388747

    本文来自博客园,作者:Mr-xxx,转载请注明原文链接:https://www.cnblogs.com/MrLiuZF/p/15055435.html

  • 相关阅读:
    收藏的一些前端学习的网址
    使用box-shadow 实现水波、音波的效果
    asyncjs,waterfall的使用
    兼容opacity的方法
    在php框架中写正规则表达式时的磕绊
    浏览器的渲染原理
    正规则表达式判断数字
    ie6,7,8不兼容rgba,写background时候不要写成rgba
    jquery-ias插件详解
    制作手机页面过程中遇到的一点问题
  • 原文地址:https://www.cnblogs.com/MrLiuZF/p/15055435.html
Copyright © 2011-2022 走看看