components/sched/
cooperative.rsuse core::mem::MaybeUninit;
use kernel::component::Component;
use kernel::process::Process;
use kernel::scheduler::cooperative::{CoopProcessNode, CooperativeSched};
#[macro_export]
macro_rules! cooperative_component_static {
($N:expr $(,)?) => {{
let coop_sched =
kernel::static_buf!(kernel::scheduler::cooperative::CooperativeSched<'static>);
let coop_nodes = kernel::static_buf!(
[core::mem::MaybeUninit<kernel::scheduler::cooperative::CoopProcessNode<'static>>; $N]
);
(coop_sched, coop_nodes)
};};
}
pub struct CooperativeComponent<const NUM_PROCS: usize> {
processes: &'static [Option<&'static dyn Process>],
}
impl<const NUM_PROCS: usize> CooperativeComponent<NUM_PROCS> {
pub fn new(
processes: &'static [Option<&'static dyn Process>],
) -> CooperativeComponent<NUM_PROCS> {
CooperativeComponent { processes }
}
}
impl<const NUM_PROCS: usize> Component for CooperativeComponent<NUM_PROCS> {
type StaticInput = (
&'static mut MaybeUninit<CooperativeSched<'static>>,
&'static mut MaybeUninit<[MaybeUninit<CoopProcessNode<'static>>; NUM_PROCS]>,
);
type Output = &'static mut CooperativeSched<'static>;
fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
let scheduler = static_buffer.0.write(CooperativeSched::new());
const UNINIT: MaybeUninit<CoopProcessNode<'static>> = MaybeUninit::uninit();
let nodes = static_buffer.1.write([UNINIT; NUM_PROCS]);
for (i, node) in nodes.iter_mut().enumerate() {
let init_node = node.write(CoopProcessNode::new(&self.processes[i]));
scheduler.processes.push_head(init_node);
}
scheduler
}
}