Skip to content

Commit 88efb69

Browse files
authored
lec11-p2 updating
1 parent fd826c2 commit 88efb69

1 file changed

Lines changed: 4 additions & 154 deletions

File tree

lec11/p2-coroutine.md

Lines changed: 4 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ backgroundColor: white
3939
### 1. 协程的概念
4040
2. 协程的实现
4141
3. 协程示例
42-
4. 协程与操作系统内核
4342

4443
![bg right:60% 70%](figs/coroutine-2.png)
4544

@@ -71,10 +70,12 @@ backgroundColor: white
7170

7271
---
7372

74-
#### 协程的定义
73+
#### 协程(Stackless Coroutine)的定义
74+
75+
**本课程**中的协程限指无栈协程。
7576

7677
<!-- 并发编程漫谈之 协程详解--以python协程入手(三) https://blog.csdn.net/u013597671/article/details/89762233 -->
77-
- Wiki的定义:协程是一种程序组件,是由子例程(过程、函数、例程、方法、子程序)的概念泛化而来的,子例程只有一个入口点且只返回一次,协程允许**多个入口点**,可在**指定位置挂起和恢复**执行
78+
- 协程是一种通过**状态机**来管理执行流上下文,以支持在**指定位置挂起和恢复**执行流的轻量级并发编程结构
7879

7980
协程的核心思想:控制流的主动让出与恢复
8081

@@ -88,7 +89,6 @@ backgroundColor: white
8889
<!-- C++20协程原理和应用 https://zhuanlan.zhihu.com/p/498253158 -->
8990
- 相比普通函数,协程的函数体可以挂起并在任意时刻恢复执行
9091
- **无栈协程是普通函数的泛化**
91-
- **本课程**中的协程限指无栈协程(Stackless Coroutine)
9292

9393
![w:900](figs/function-coroutine.png)
9494

@@ -129,7 +129,6 @@ def func()://协程函数
129129
1. 协程的概念
130130
### 2. 协程的实现
131131
3. 协程示例
132-
4. 协程与操作系统内核
133132

134133
![bg right:60% 70%](figs/coroutine-2.png)
135134

@@ -382,7 +381,6 @@ async fn example(min_len: usize) -> String {
382381
1. 协程的概念
383382
2. 协程的实现
384383
### 3. 协程示例
385-
4. 协程与操作系统内核
386384

387385
![bg right:60% 70%](figs/coroutine-2.png)
388386

@@ -676,159 +674,11 @@ fn main() { //Asynchronous multi-threaded concurrent webserver
676674

677675
---
678676

679-
**提纲**
680-
681-
1. 协程的概念
682-
2. 协程的实现
683-
3. 协程示例
684-
### 4. 协程与操作系统内核
685-
686-
![bg right:52% 95%](figs/coroutine-2.png)
687-
688-
---
689-
690-
#### 共享调度器:[一种支持优先级的协程调度框架](https://github.com/zflcs/SharedScheduler)(本科生毕设:赵方亮、廖东海;选学,不考核)
691-
692-
* 将协程作为操作系统和应用程序的最小任务单元
693-
* 引入协程的优先级属性,基于优先级位图,操作系统和应用程序实现协程调度
694-
695-
<!--
696-
-->
697-
698-
---
699-
700-
#### Architecture of SharedScheduler
701-
702-
![bg right:50% 95% arch](figs/arch.png)
703-
704-
1. 操作系统与用户程序各自的 Executor 维护协程
705-
2. SharedScheduler 通过 vDSO (virtual Dynamic Shared Object) 共享给用户进程
706-
3. 通过 Global Bitmap 进行操作系统与用户进程之间协调调度
707-
708-
<!--
709-
-->
710-
711-
---
712-
713-
#### Coroutine Control Block
714-
715-
```rust
716-
pub struct Coroutine{
717-
/// Immutable fields
718-
pub cid: CoroutineId,
719-
pub kind: CoroutineKind,
720-
/// Mutable fields
721-
pub priority: usize,
722-
pub future: Pin<Box<dyn Future<Output=()> + 'static + Send + Sync>>,
723-
pub waker: Arc<Waker>,
724-
}
725-
```
726-
727-
<font size=5>
728-
729-
1. future、waker 字段由 Rust 协程特性决定
730-
731-
2. cid 字段用于标识协程
732-
733-
3. kind 字段标识协程任务类型,根据类型进行不同处理
734-
735-
4. priority 字段表示优先级,实现优先级调度的关键
736-
737-
</font>
738-
739-
<!--
740-
-->
741-
742-
---
743-
744-
#### Coroutine state transition model
745-
746-
![bg right:55% 90% cstate](figs/cstate.png)
747-
748-
根据 CPU 和 stack 占用的情况划分为三类
749-
* 状态转换
750-
1. 就绪 <==> 运行
751-
2. 运行 <==> 运行挂起
752-
3. 运行 <==> 阻塞
753-
4. 阻塞 ==> 就绪
754-
755-
<!--
756-
根据 CPU 和 stack 占用的情况划分为三类(创建、退出、就绪、阻塞 | 运行 | 运行挂起)
757-
-->
758-
759-
---
760-
761-
#### Asynchronous system call
762-
763-
```rust
764-
read!(fd, buffer, cid); // Async call
765-
read!(fd, buffer); // Sync call
766-
```
767-
768-
* 用户态系统调用接口,通过参数区分
769-
* 内核协程与异步 I/O 机制结合,内核协程完成读取、复制数据操作
770-
![width:900px async_syscall](figs/async_syscall.png)
771-
772-
<!--
773-
-->
774-
775-
---
776-
777-
#### Message throughput
778-
779-
<!--![width:750px throughput]-->
780-
![bg right:51% 100%](figs/throughput.png)
781-
782-
1. kcuc: 内核协程 + 用户协程
783-
2. kcut:内核协程 + 用户线程
784-
3. ktut:内核线程 + 用户线程
785-
4. ktuc:内核线程 + 用户协程
786-
787-
<!--
788-
-->
789-
790-
---
791-
792-
#### Message latency
793-
794-
![bg right:51% 100%](figs/latency.png)
795-
796-
1. SharedScheduler 同步互斥开销,不适用于低并发或低响应要求的场景
797-
2. 协程切换开销小
798-
3. SharedScheduler 适用于高并发场景
799-
800-
<!--
801-
-->
802-
803-
---
804-
805-
#### Throughput of different priority connections
806-
807-
![width:600px prio-throughput](figs/prio-throughput.png)
808-
809-
结论:在资源有限的条件下,高优先级协程能够得到保证
810-
811-
<!--
812-
-->
813-
814-
---
815-
816-
#### Message latency of different priority connections
817-
818-
![width:550px prio-latency](figs/prio-latency.png)
819-
结论:在资源有限的条件下,高优先级协程能够得到保证
820-
821-
<!--
822-
-->
823-
824-
---
825-
826677
### 小结
827678

828679
1. 协程的概念
829680
2. 协程的实现
830681
3. 协程示例
831-
4. 协程与操作系统内核
832682

833683
![bg right:40% 100%](figs/coroutine-2.png)
834684

0 commit comments

Comments
 (0)