mod bindings;
mod root;
pub(crate) use bindings::{Bindings, Scheduler, Slots};
use o3::{
mem,
mem::{Return, lease::Open},
};
pub use root::Root;
use task::topology;
use crate::driver::schedule::{
ready,
ready::task::{self, Index},
};
use core::{array::from_fn, num::NonZero, pin::Pin};
use o3::depot::array::{self, Vacancy};
pub(crate) trait Binding<'d> {
fn waker(&self) -> task::Parent<'d>;
fn reclaim(self) -> task::Reclaim;
}
#[repr(C)]
struct State<'d> {
parent: task::Parent<'d>,
child: ready::TaskKey<'d>,
}
impl<'d> State<'d> {
unsafe fn bind<I: topology::raw::Index>(
admit: Admission<'d>,
edge: topology::Edge<'_, 'd, I>,
) -> Self {
let (parent, held) = admit.into_parts();
let (endpoint, local) = edge.into_parts();
let arena = parent.driver().ready().arena();
let root = endpoint.key();
let task = ready::Task::new(parent.key(), root, local.raw());
let staged = arena.entries.slots.task(held, task);
let child = ready::Root::get(&arena.entries.slots, root).attach(staged);
Self { parent, child }
}
fn waker(&self) -> task::Parent<'d> {
self.parent.retarget(self.child.key())
}
}
impl Return for State<'_> {
type Item = ();
fn return_item(self, (): ()) {
let entries = &self.parent.driver().ready().arena().entries;
entries.pool.free_task(&entries.slots, self.child);
}
}
#[must_use = "an admitted task must be bound or released"]
pub struct Admission<'d> {
held: mem::Lease<Release<'d>, Open>,
}
struct Release<'d>(task::Parent<'d>);
impl<'d> Admission<'d> {
pub(super) const fn new(parent: task::Parent<'d>, child: ready::ReservedKey<'d>) -> Self {
Self {
held: mem::Lease::open(Release(parent), child),
}
}
pub(super) fn into_parts(self) -> (task::Parent<'d>, ready::ReservedKey<'d>) {
let (Release(parent), child) = self.held.into_parts();
(parent, child)
}
}
impl<'d> Return for Release<'d> {
type Item = ready::ReservedKey<'d>;
fn return_item(self, child: Self::Item) {
let entries = &self.0.driver().ready().arena().entries;
entries.pool.recycle(&entries.slots, child);
}
}
#[must_use = "an admitted task must be bound or released"]
pub struct Lease<'lease, 'd> {
held: mem::Lease<Recycle<'lease, 'd>, Open>,
}
struct Recycle<'lease, 'd> {
parent: task::Parent<'d>,
chain: &'lease ready::Chain,
}
impl<'lease, 'd> Lease<'lease, 'd> {
pub(super) fn new(
parent: task::Parent<'d>,
child: ready::ReservedKey<'d>,
chain: &'lease ready::Chain,
) -> Self {
Self {
held: mem::Lease::open(Recycle { parent, chain }, child),
}
}
pub(super) fn take(self) -> Admission<'d> {
let (Recycle { parent, .. }, child) = self.held.into_parts();
Admission::new(parent, child)
}
}
impl<'d> Return for Recycle<'_, 'd> {
type Item = ready::ReservedKey<'d>;
fn return_item(self, child: Self::Item) {
self.parent.return_lease(child, self.chain);
}
}
const _: () = {
assert!(size_of::<Admission<'static>>() == 3 * size_of::<usize>());
assert!(size_of::<Option<Admission<'static>>>() == size_of::<Admission<'static>>());
assert!(size_of::<Lease<'static, 'static>>() == 4 * size_of::<usize>());
assert!(size_of::<Option<Lease<'static, 'static>>>() == size_of::<Lease<'static, 'static>>());
};
pub(crate) fn map<T, const N: usize>(mut make: impl FnMut(Index<N>) -> T) -> [T; N] {
from_fn(|i| {
make(unsafe { from_pos(i) })
})
}
unsafe fn from_pos<const N: usize>(i: usize) -> Index<N> {
let () = Index::<N>::VALID;
Index(unsafe { NonZero::new_unchecked((i + 1) as u16) })
}
pub(crate) fn from_vacant<const N: usize>(slot: &Vacancy<N>) -> Index<N> {
unsafe { from_pos(slot.index()) }
}
pub(crate) fn at_mut<T, const N: usize>(index: Index<N>, items: &mut [T; N]) -> &mut T {
unsafe { items.get_unchecked_mut(index.get()) }
}
pub(crate) fn project<T, const N: usize>(index: Index<N>, items: Pin<&mut [T; N]>) -> Pin<&mut T> {
unsafe { items.map_unchecked_mut(|items| at_mut(index, items)) }
}
unsafe impl<const N: usize> array::raw::BitIndex<N> for Index<N> {
fn index(self) -> usize {
self.get()
}
unsafe fn from_index(i: usize) -> Self {
unsafe { from_pos(i) }
}
}
unsafe impl<const N: usize> topology::raw::Index for Index<N> {
fn raw(self) -> u32 {
u32::from(self.0.get())
}
unsafe fn from_raw(raw: u32) -> Self {
Self(unsafe { NonZero::new_unchecked(raw as u16) })
}
}
const _: () = {
assert!(size_of::<Index<1>>() == size_of::<u16>());
assert!(size_of::<Option<Index<1>>>() == size_of::<u16>());
};