@@ -21,7 +21,7 @@ pub struct ExecutorRuntime {
21
21
strong_executor : Arc < Pin < Box < Executor > > > ,
22
22
23
23
// 该 executor 在执行完一次后就会被 drop
24
- weak_executor_vec : Vec < Option < Arc < Pin < Box < Executor > > > > > ,
24
+ weak_executors : Vec < Option < Arc < Pin < Box < Executor > > > > > ,
25
25
26
26
// 当前正在执行的 executor
27
27
current_executor : Option < Arc < Pin < Box < Executor > > > > ,
@@ -38,7 +38,7 @@ impl ExecutorRuntime {
38
38
cpu_id,
39
39
task_collection,
40
40
strong_executor : Arc :: new ( Executor :: new ( tc_clone) ) ,
41
- weak_executor_vec : vec ! [ ] ,
41
+ weak_executors : vec ! [ ] ,
42
42
current_executor : None ,
43
43
context : Context :: default ( ) ,
44
44
}
@@ -49,11 +49,16 @@ impl ExecutorRuntime {
49
49
}
50
50
51
51
pub ( crate ) fn weak_executor_num ( & self ) -> usize {
52
- self . weak_executor_vec . len ( )
52
+ self . weak_executors . len ( )
53
+ }
54
+
55
+ // return task number of current cpu.
56
+ pub fn task_num ( & self ) -> usize {
57
+ self . task_collection . task_num ( )
53
58
}
54
59
55
60
fn add_weak_executor ( & mut self , weak_executor : Arc < Pin < Box < Executor > > > ) {
56
- self . weak_executor_vec . push ( Some ( weak_executor) ) ;
61
+ self . weak_executors . push ( Some ( weak_executor) ) ;
57
62
}
58
63
59
64
fn downgrade_strong_executor ( & mut self ) {
@@ -66,11 +71,6 @@ impl ExecutorRuntime {
66
71
self . strong_executor = Arc :: new ( Executor :: new ( self . task_collection . clone ( ) ) ) ;
67
72
}
68
73
69
- // return task number of current cpu.
70
- fn task_num ( & self ) -> usize {
71
- self . task_collection . task_num ( )
72
- }
73
-
74
74
// 添加一个task,它的初始状态是 notified,也就是说它可以被执行.
75
75
fn add_task < F : Future < Output = ( ) > + ' static + Send > ( & self , priority : usize , future : F ) -> Key {
76
76
debug_assert ! ( priority < MAX_PRIORITY ) ;
@@ -135,30 +135,31 @@ pub fn run_until_idle() -> bool {
135
135
runtime. current_executor = Some ( runtime. strong_executor . clone ( ) ) ;
136
136
// 释放保护 global_runtime 的锁
137
137
drop ( runtime) ;
138
- unsafe {
139
- crate :: switch ( runtime_cx as _ , executor_cx as _ ) ;
140
- // 该函数返回说明当前 strong_executor 执行的 future 超时或者主动 yield 了,
141
- // 需要重新创建一个 executor 执行后续的 future, 并且将
142
- // 新的 executor 作为 strong_executor,旧的 executor 添
143
- // 加到 weak_exector 中。
138
+ switch ( runtime_cx, executor_cx) ;
139
+ // 该函数返回说明当前 strong_executor 执行的 future 超时或者主动 yield 了,
140
+ // 需要重新创建一个 executor 执行后续的 future, 并且将
141
+ // 新的 executor 作为 strong_executor,旧的 executor 添
142
+ // 加到 weak_exector 中。
143
+ runtime = get_current_runtime ( ) ;
144
+ if runtime. task_num ( ) == 0 {
145
+ return false ;
144
146
}
145
- let mut runtime = get_current_runtime ( ) ;
146
147
// 只有 strong_executor 主动 yield 时, 才会执行运行 weak_executor;
147
148
if runtime. strong_executor . is_running_future ( ) {
148
149
runtime. downgrade_strong_executor ( ) ;
149
150
continue ;
150
151
}
151
152
152
153
// 遍历全部的 weak_executor
153
- if runtime. weak_executor_vec . is_empty ( ) {
154
+ if runtime. weak_executors . is_empty ( ) {
154
155
drop ( runtime) ;
155
156
continue ;
156
157
}
157
158
runtime
158
- . weak_executor_vec
159
+ . weak_executors
159
160
. retain ( |executor| executor. is_some ( ) && !executor. as_ref ( ) . unwrap ( ) . killed ( ) ) ;
160
- for idx in 0 ..runtime. weak_executor_vec . len ( ) {
161
- if let Some ( executor) = & runtime. weak_executor_vec [ idx] {
161
+ for idx in 0 ..runtime. weak_executors . len ( ) {
162
+ if let Some ( executor) = & runtime. weak_executors [ idx] {
162
163
if executor. killed ( ) {
163
164
continue ;
164
165
}
@@ -177,7 +178,7 @@ pub fn run_until_idle() -> bool {
177
178
}
178
179
179
180
pub fn spawn ( future : impl Future < Output = ( ) > + Send + ' static ) {
180
- spawn_task ( future, None , Some ( crate :: cpu_id ( ) as _ ) ) ;
181
+ spawn_task ( future, None , Some ( crate :: arch :: cpu_id ( ) as _ ) ) ;
181
182
}
182
183
183
184
/// Spawn a coroutine with `priority` and `cpu_id`
@@ -230,9 +231,7 @@ pub fn sched_yield() {
230
231
let executor_cx = executor. context . get_context ( ) ;
231
232
let runtime_cx = runtime. get_context ( ) ;
232
233
drop ( runtime) ;
233
- trace ! ( "switch to runtime" ) ;
234
- switch ( executor_cx as _ , runtime_cx as _ ) ;
235
- trace ! ( "switch back to executor" ) ;
234
+ switch ( executor_cx, runtime_cx) ;
236
235
}
237
236
}
238
237
@@ -242,7 +241,7 @@ pub(crate) fn switch(from_ctx: usize, to_ctx: usize) {
242
241
// crate::intr_off();
243
242
// }
244
243
unsafe {
245
- crate :: switch ( from_ctx as _ , to_ctx as _ ) ;
244
+ crate :: arch :: switch ( from_ctx as _ , to_ctx as _ ) ;
246
245
}
247
246
// if intr_enable {
248
247
// crate::intr_on();
@@ -251,5 +250,5 @@ pub(crate) fn switch(from_ctx: usize, to_ctx: usize) {
251
250
252
251
/// return runtime `MutexGuard` of current cpu.
253
252
pub ( crate ) fn get_current_runtime ( ) -> MutexGuard < ' static , ExecutorRuntime > {
254
- GLOBAL_RUNTIME [ crate :: cpu_id ( ) as usize ] . lock ( )
253
+ GLOBAL_RUNTIME [ crate :: arch :: cpu_id ( ) as usize ] . lock ( )
255
254
}
0 commit comments