use crate::driver::schedule::ready::{
self,
task::{self, Index, pool, topology},
};
use core::borrow::Borrow;
use o3::{
depot::array,
mem::{self, Lease, Return, lease::Open},
};
pub(crate) struct Scheduler<'d, const N: usize> {
pool: pool::Pool<'d, N>,
pub(crate) ready: topology::Endpoint<'d, Index<N>>,
}
pub(crate) struct Bindings<'d, S, const N: usize> {
owner: S,
keys: [mem::raw::Storage<ready::TaskKey<'d>>; N],
}
pub(crate) struct Slots<'a, 'd, const N: usize> {
owner: &'a Scheduler<'d, N>,
keys: &'a mut [mem::raw::Storage<ready::TaskKey<'d>>; N],
}
struct Slot<'a, 'd, const N: usize> {
pool: &'a pool::Pool<'d, N>,
key: &'a mut mem::raw::Storage<ready::TaskKey<'d>>,
}
struct Active<'a, 'd, const N: usize> {
pool: &'a pool::Pool<'d, N>,
key: mem::raw::Occupied<'a, ready::TaskKey<'d>>,
id: ready::Key<'d>,
}
impl<'d, const N: usize> Scheduler<'d, N> {
pub(crate) fn new(pool: pool::Pool<'d, N>, ready: topology::Endpoint<'d, Index<N>>) -> Self {
Self { pool, ready }
}
pub(crate) fn borrow_slots(&mut self) -> Bindings<'d, &Self, N> {
Bindings::new(self)
}
pub(crate) fn into_slots(self) -> Bindings<'d, Self, N> {
Bindings::new(self)
}
pub(crate) fn wake(&self, root: pool::Root<'d>) {
self.pool.parent(root).wake();
}
}
impl<'d, S: Borrow<Scheduler<'d, N>>, const N: usize> Bindings<'d, S, N> {
fn new(owner: S) -> Self {
let () = Index::<N>::VALID;
Self {
owner,
keys: [const { mem::raw::Storage::new(()) }; N],
}
}
pub(crate) fn owner(&self) -> &Scheduler<'d, N> {
self.owner.borrow()
}
pub(crate) fn split(&mut self) -> (&Scheduler<'d, N>, Slots<'_, 'd, N>) {
let owner = self.owner.borrow();
(
owner,
Slots {
owner,
keys: &mut self.keys,
},
)
}
}
impl<'d, const N: usize> Slots<'_, 'd, N> {
pub(crate) fn has_ready(&self) -> bool {
!self.owner.ready.is_empty()
}
fn slot(&mut self, index: Index<N>) -> Slot<'_, 'd, N> {
Slot {
pool: &self.owner.pool,
key: task::raw::at_mut(index, self.keys),
}
}
pub(crate) fn start(
&mut self,
root: pool::Root<'d>,
index: Index<N>,
) -> Result<impl task::raw::Binding<'d> + '_, task::Error> {
let admit = self.owner.pool.try_admit(root)?;
let state = unsafe { task::raw::State::bind(admit.take(), self.owner.ready.attach(index)) };
Ok(Lease::open(self.slot(index), state.child))
}
pub(crate) fn resume(
&mut self,
wake: topology::Wake<'d, Index<N>>,
) -> Option<impl task::raw::Binding<'d> + '_> {
let slot = self.slot(wake.local());
let key = slot.key.occupied()?;
let id = wake.identify(key.get())?;
Some(Active {
pool: slot.pool,
key,
id,
})
}
pub(crate) fn cancel(&mut self, index: Index<N>) -> Option<task::Reclaim> {
self.slot(index).cancel()
}
pub(crate) fn drain(self, len: array::Len<N>) -> impl Iterator<Item = Option<task::Reclaim>> {
let pool = &self.owner.pool;
len.prefix_mut(self.keys)
.iter_mut()
.map(move |key| Slot { pool, key }.cancel())
}
}
impl<const N: usize> Slot<'_, '_, N> {
fn cancel(self) -> Option<task::Reclaim> {
Some(self.pool.reclaim(self.key.occupied()?.take().0))
}
}
impl<'d, const N: usize> Return for Slot<'_, 'd, N> {
type Item = ready::TaskKey<'d>;
fn return_item(self, key: Self::Item) {
self.key.insert(key);
}
}
impl<'d, const N: usize> task::raw::Binding<'d> for Lease<Slot<'_, 'd, N>, Open> {
fn waker(&self) -> task::Parent<'d> {
self.sink().pool.waker(self.item().key())
}
fn reclaim(self) -> task::Reclaim {
let (slot, key) = self.into_parts();
slot.pool.reclaim(key)
}
}
impl<'d, const N: usize> task::raw::Binding<'d> for Active<'_, 'd, N> {
fn waker(&self) -> task::Parent<'d> {
self.pool.waker(self.id)
}
fn reclaim(self) -> task::Reclaim {
self.pool.reclaim(self.key.take().0)
}
}
const _: () = {
assert!(size_of::<mem::raw::Storage<ready::TaskKey<'static>>>() == size_of::<u64>());
};