Skip to content

Commit 364989e

Browse files
committed
return false when all tasks is done
1 parent 7289d01 commit 364989e

File tree

4 files changed

+35
-35
lines changed

4 files changed

+35
-35
lines changed

src/executor.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use {
88
core::task::{Context, Poll},
99
};
1010

11-
use crate::executor_entry;
11+
use crate::arch::executor_entry;
1212
use crate::task_collection::TaskCollection;
1313

1414
#[derive(Debug, PartialEq, Eq)]
@@ -78,7 +78,7 @@ impl Executor {
7878
let context_data = ContextData::new(
7979
executor_entry as *const () as usize,
8080
stack_top,
81-
crate::pg_base_register(),
81+
crate::arch::pg_base_register(),
8282
);
8383
stack_top = unsafe { push_stack(stack_top, context_data) };
8484
stack_top
@@ -107,13 +107,18 @@ impl Executor {
107107

108108
match ret {
109109
Poll::Ready(()) => {
110-
// self.task_collection.remove_task(key);
111110
droper.drop_by_ref();
112111
}
113112
Poll::Pending => (),
114113
}
115114
} else {
116-
let runtime = crate::runtime::get_current_runtime();
115+
let mut runtime = crate::runtime::get_current_runtime();
116+
if runtime.task_num() == 0 {
117+
trace!("all done! return to runtime");
118+
drop(runtime);
119+
crate::runtime::sched_yield();
120+
runtime = crate::runtime::get_current_runtime();
121+
}
117122
let has_other_task = runtime.weak_executor_num() != 0;
118123
drop(runtime);
119124
if has_other_task {

src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ cfg_if::cfg_if! {
1717
}
1818
}
1919

20-
use arch::*;
21-
2220
extern crate alloc;
2321
#[macro_use]
2422
extern crate log;
@@ -29,5 +27,4 @@ mod runtime;
2927
mod task_collection;
3028
mod waker_page;
3129

32-
pub(crate) use context::{Context, ContextData};
3330
pub use runtime::{handle_timeout, run_until_idle, sched_yield, spawn};

src/runtime.rs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub struct ExecutorRuntime {
2121
strong_executor: Arc<Pin<Box<Executor>>>,
2222

2323
// 该 executor 在执行完一次后就会被 drop
24-
weak_executor_vec: Vec<Option<Arc<Pin<Box<Executor>>>>>,
24+
weak_executors: Vec<Option<Arc<Pin<Box<Executor>>>>>,
2525

2626
// 当前正在执行的 executor
2727
current_executor: Option<Arc<Pin<Box<Executor>>>>,
@@ -38,7 +38,7 @@ impl ExecutorRuntime {
3838
cpu_id,
3939
task_collection,
4040
strong_executor: Arc::new(Executor::new(tc_clone)),
41-
weak_executor_vec: vec![],
41+
weak_executors: vec![],
4242
current_executor: None,
4343
context: Context::default(),
4444
}
@@ -49,11 +49,16 @@ impl ExecutorRuntime {
4949
}
5050

5151
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()
5358
}
5459

5560
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));
5762
}
5863

5964
fn downgrade_strong_executor(&mut self) {
@@ -66,11 +71,6 @@ impl ExecutorRuntime {
6671
self.strong_executor = Arc::new(Executor::new(self.task_collection.clone()));
6772
}
6873

69-
// return task number of current cpu.
70-
fn task_num(&self) -> usize {
71-
self.task_collection.task_num()
72-
}
73-
7474
// 添加一个task,它的初始状态是 notified,也就是说它可以被执行.
7575
fn add_task<F: Future<Output = ()> + 'static + Send>(&self, priority: usize, future: F) -> Key {
7676
debug_assert!(priority < MAX_PRIORITY);
@@ -135,30 +135,31 @@ pub fn run_until_idle() -> bool {
135135
runtime.current_executor = Some(runtime.strong_executor.clone());
136136
// 释放保护 global_runtime 的锁
137137
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;
144146
}
145-
let mut runtime = get_current_runtime();
146147
// 只有 strong_executor 主动 yield 时, 才会执行运行 weak_executor;
147148
if runtime.strong_executor.is_running_future() {
148149
runtime.downgrade_strong_executor();
149150
continue;
150151
}
151152

152153
// 遍历全部的 weak_executor
153-
if runtime.weak_executor_vec.is_empty() {
154+
if runtime.weak_executors.is_empty() {
154155
drop(runtime);
155156
continue;
156157
}
157158
runtime
158-
.weak_executor_vec
159+
.weak_executors
159160
.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] {
162163
if executor.killed() {
163164
continue;
164165
}
@@ -177,7 +178,7 @@ pub fn run_until_idle() -> bool {
177178
}
178179

179180
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 _));
181182
}
182183

183184
/// Spawn a coroutine with `priority` and `cpu_id`
@@ -230,9 +231,7 @@ pub fn sched_yield() {
230231
let executor_cx = executor.context.get_context();
231232
let runtime_cx = runtime.get_context();
232233
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);
236235
}
237236
}
238237

@@ -242,7 +241,7 @@ pub(crate) fn switch(from_ctx: usize, to_ctx: usize) {
242241
// crate::intr_off();
243242
// }
244243
unsafe {
245-
crate::switch(from_ctx as _, to_ctx as _);
244+
crate::arch::switch(from_ctx as _, to_ctx as _);
246245
}
247246
// if intr_enable {
248247
// crate::intr_on();
@@ -251,5 +250,5 @@ pub(crate) fn switch(from_ctx: usize, to_ctx: usize) {
251250

252251
/// return runtime `MutexGuard` of current cpu.
253252
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()
255254
}

src/task_collection.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ impl TaskCollection {
146146

147147
/// remove the task correponding to the key.
148148
pub fn remove_task(&self, key: Key) {
149-
debug!("remove task key = 0x{:x?}", key);
150149
let mut inner = self.get_mut_inner(key >> PRIORITY_SHIFT);
151150
inner.remove(unmask_priority(key), true);
152151
self.task_num.fetch_sub(1, Ordering::Relaxed);
@@ -217,6 +216,7 @@ impl TaskCollection {
217216
for subpage_idx in BitIter::from(dropped) {
218217
// the key corresponding to the task
219218
let key = pack_key(priority, page_idx, subpage_idx);
219+
self.task_num.fetch_sub(1, Ordering::Relaxed);
220220
inner.remove(key, true);
221221
}
222222
}
@@ -241,7 +241,6 @@ pub mod key {
241241
pub const DEFAULT_PRIORITY: usize = 4;
242242

243243
pub const PAGE_INDEX_SHIFT: usize = 6;
244-
pub const TASK_NUM_PER_PAGE: usize = 1 << PAGE_INDEX_SHIFT;
245244

246245
pub fn unpack_key(key: Key) -> (usize, usize, usize) {
247246
let subpage_idx = key & 0x3F;

0 commit comments

Comments
 (0)