本文共 1433 字,大约阅读时间需要 4 分钟。
问题:协程如何被调度?
调度器的实现,有两种方案,一种是生产者消费者模式,另一种多状态运行。
逻辑代码如下:
while (1) { //遍历睡眠集合,将满足条件的加入到ready nty_coroutine *expired = NULL; while ((expired = sleep_tree_expired(sched)) != ) { TAILQ_ADD(&sched->ready, expired); } //遍历等待集合,将满足添加的加入到ready nty_coroutine *wait = NULL; int nready = epoll_wait(sched->epfd, events, EVENT_MAX, 1); for (i = 0;i < nready;i ++) { wait = wait_tree_search(events[i].data.fd); TAILQ_ADD(&sched->ready, wait); } // 使用resume回复ready的协程运行权 while (!TAILQ_EMPTY(&sched->ready)) { nty_coroutine *ready = TAILQ_POP(sched->ready); resume(ready); } }
逻辑代码如下:
while (1) { //遍历睡眠集合,使用resume恢复expired的协程运行权 nty_coroutine *expired = NULL; while ((expired = sleep_tree_expired(sched)) != ) { resume(expired); } //遍历等待集合,使用resume恢复wait的协程运行权 nty_coroutine *wait = NULL; int nready = epoll_wait(sched->epfd, events, EVENT_MAX, 1); for (i = 0;i < nready;i ++) { wait = wait_tree_search(events[i].data.fd); resume(wait); } // 使用resume恢复ready的协程运行权 while (!TAILQ_EMPTY(sched->ready)) { nty_coroutine *ready = TAILQ_POP(sched->ready); resume(ready); } }
email:
email: email: 协程技术交流群:829348971转载地址:http://hpfqj.baihongyu.com/