task: move Scheduler into a trait, define static-sized scheduler implementation, expose as a system task runner in platform

This commit is contained in:
2024-12-02 19:32:18 +01:00
parent 132d7c0a33
commit f789f6ded9
4 changed files with 41 additions and 31 deletions

View File

@@ -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);
}