Linux高效能網路:協程系列08-協程實現之排程器
阿新 • • 發佈:2018-11-09
目錄
- Linux高效能網路:協程系列01-前言
- Linux高效能網路:協程系列02-協程的起源
- Linux高效能網路:協程系列03-協程的案例
- Linux高效能網路:協程系列04-協程實現之工作原理
- Linux高效能網路:協程系列05-協程實現之原語操作
- Linux高效能網路:協程系列06-協程實現之切換
- Linux高效能網路:協程系列07-協程實現之定義
- Linux高效能網路:協程系列08-協程實現之排程器
- Linux高效能網路:協程系列09-協程效能測試
- [Linux高效能網路:協程系列10 待續]()
8.協程實現之排程器
8.0 前言
問題:協程如何被排程?
排程器的實現,有兩種方案,一種是生產者消費者模式,另一種多狀態執行。
8.1 生產者消費者模式
邏輯程式碼如下:
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); } }
8.2 多狀態下執行
邏輯程式碼如下:
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 protected]
email: [email protected]
email: [email protected]
協程技術交流群:829348971