task: move Scheduler into a trait, define static-sized scheduler implementation, expose as a system task runner in platform
This commit is contained in:
32
src/task.rs
32
src/task.rs
@@ -77,25 +77,35 @@ impl ScheduledTask {
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Scheduler {
|
||||
tasks: Vec<ScheduledTask>,
|
||||
pub struct FixedSizeScheduler<const TASK_COUNT: usize> {
|
||||
tasks: [Option<ScheduledTask>; TASK_COUNT],
|
||||
}
|
||||
|
||||
impl Scheduler {
|
||||
pub fn new(tasks: Vec<Box<dyn Task>>) -> Self {
|
||||
let mut scheduled = Vec::new();
|
||||
impl<const TASK_COUNT: usize> FixedSizeScheduler<TASK_COUNT> {
|
||||
pub fn new(tasks: [Box<dyn Task>; TASK_COUNT]) -> Self {
|
||||
let mut scheduled = [const { None }; TASK_COUNT];
|
||||
let mut idx = 0;
|
||||
for task in tasks {
|
||||
log::info!("Scheduling task {} {:?}", task.name(), task);
|
||||
scheduled.push(ScheduledTask::new(task));
|
||||
scheduled[idx] = Some(ScheduledTask::new(task));
|
||||
idx += 1;
|
||||
}
|
||||
Scheduler {
|
||||
FixedSizeScheduler {
|
||||
tasks: scheduled
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tick(&mut self) {
|
||||
for task in &mut self.tasks {
|
||||
task.tick();
|
||||
impl<const TASK_COUNT: usize> Scheduler for FixedSizeScheduler<TASK_COUNT> {
|
||||
fn tick(&mut self) {
|
||||
for slot in &mut self.tasks {
|
||||
match slot {
|
||||
Some(task) => task.tick(),
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Scheduler {
|
||||
fn tick(&mut self);
|
||||
}
|
Reference in New Issue
Block a user