1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use alloc::boxed::Box;
use core::future::Future;
use async_task::{Runnable, Task};
use lazy_static::lazy_static;
use riscv::register::{stvec, utvec::TrapMode};
use crate::{
constant::TRAMPOLINE,
executor::scheduler::{Scheduler, TaskQueue},
sync::Mutex,
};
mod context;
mod future;
mod scheduler;
pub use context::TrapContext;
pub use future::{spawn_thread, yield_now, ControlFlow};
pub fn init() {
unsafe {
stvec::write(TRAMPOLINE, TrapMode::Direct);
}
}
lazy_static! {
static ref SCHEDULER: Mutex<Box<dyn Scheduler>> = Mutex::new(Box::new(TaskQueue::new()));
}
fn spawn<F>(future: F) -> (Runnable, Task<F::Output>)
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
async_task::spawn(future, |runnable| {
SCHEDULER.lock().schedule(runnable);
})
}
pub fn run_until_complete() {
loop {
let task = SCHEDULER.lock().task();
if let Some(task) = task {
task.run();
} else {
break;
}
}
}