2022-05-20 10:11:32 -05:00
|
|
|
use alloc::collections::VecDeque;
|
|
|
|
use spin::{Lazy, Mutex};
|
|
|
|
|
|
|
|
pub static SCHEDULER: Lazy<Mutex<Scheduler>> = Lazy::new(|| Mutex::new(Scheduler::default()));
|
|
|
|
|
2022-06-14 10:27:30 -05:00
|
|
|
/// Task's stack size (in kiB)
|
|
|
|
const STACK_SIZE: usize = 512;
|
|
|
|
|
2022-05-20 10:11:32 -05:00
|
|
|
pub enum Task {
|
|
|
|
Suspended(u64),
|
2022-06-14 10:27:30 -05:00
|
|
|
Spawn { function: fn(), stack_start: usize },
|
2022-05-20 10:11:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Scheduler {
|
|
|
|
task_queue: VecDeque<Task>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Scheduler {
|
2022-06-14 10:27:30 -05:00
|
|
|
pub fn enqueue_spawn(&mut self, function: fn()) {
|
|
|
|
self.task_queue.push_back(Task::Spawn {
|
|
|
|
function,
|
|
|
|
stack_start: unsafe {
|
|
|
|
let size = STACK_SIZE * 1024;
|
|
|
|
alloc::alloc::alloc(
|
|
|
|
alloc::alloc::Layout::from_size_align(size, 1).expect("invalid layout"),
|
|
|
|
)
|
|
|
|
.add(size - 1) as _
|
|
|
|
},
|
|
|
|
});
|
2022-05-20 10:11:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enqueue_suspended(&mut self, sp: u64) {
|
|
|
|
self.task_queue.push_back(Task::Suspended(sp));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pop(&mut self) -> Option<Task> {
|
|
|
|
self.task_queue.pop_front()
|
|
|
|
}
|
|
|
|
}
|
2022-07-28 03:39:20 -05:00
|
|
|
|
|
|
|
pub enum WakeConditions {
|
|
|
|
Time,
|
|
|
|
ReceivedMessageInChannel,
|
|
|
|
GainedAccessToChannel,
|
|
|
|
}
|