use std::{marker, ops, pin};
use o3::{
base::{cell::region, num::Epoch},
depot::arena::slab::{self, external, key},
};
use crate::{
driver::schedule::{app, ready::task::owner},
executor::task::{
context::wake,
storage::fixed::{
Id, Slab, direct, local,
schedule::{self, Pool, queue, ready::owned, result::Selection},
},
},
};
#[pin_project::pin_project]
pub struct Scope<'s, S> {
#[pin]
pub(in crate::executor::task::storage) source: S,
pub(in crate::executor::task::storage) proof: region::Identity<'s>,
}
impl<'s, S> Scope<'s, S> {
pub fn new(source: S, token: region::Token<region::Identity<'s>>) -> Self {
Self {
source,
proof: token.proof(),
}
}
pub fn reborrow(self: pin::Pin<&mut Self>) -> Scope<'s, pin::Pin<&mut S>> {
let this = self.project();
Scope {
source: this.source,
proof: *this.proof,
}
}
pub fn as_pin_ref(self: pin::Pin<&Self>) -> pin::Pin<&S> {
self.project_ref().source
}
}
impl<S: Unpin> Scope<'_, S> {
pub fn as_mut(&mut self) -> pin::Pin<&mut Self> {
pin::Pin::new(self)
}
pub fn as_ref(&self) -> pin::Pin<&Self> {
pin::Pin::new(self)
}
}
impl<S> ops::Deref for Scope<'_, S> {
type Target = S;
fn deref(&self) -> &S {
&self.source
}
}
impl<'s, 'd, F, const N: usize, D, S: slab::Storage>
Scope<'s, pin::Pin<&mut Slab<'d, F, N, D, S>>>
{
pub fn admit(&mut self) -> Option<direct::raw::Admit<'_, 's, 'd, F, D, S>> {
Some(direct::raw::Admit {
slot: slab::raw::Anchored::vacant(self.source.as_mut().project().inner)?,
proof: self.proof,
driver: marker::PhantomData,
})
}
pub fn entry<'a, K: Copy + Into<key::Parts>>(
&'a mut self,
id: &Id<'s, 'd, D, K>,
) -> Option<slab::raw::Anchored<'a, F, D, { u32::MAX }, false, 1, S>> {
slab::raw::Anchored::at(self.source.as_mut().project().inner, id.key.into())
}
pub fn remove<K: Copy + Into<key::Parts>>(&mut self, id: Id<'s, 'd, D, K>) -> local::Removal {
match self.entry(&id) {
Some(entry) => {
entry.discard();
local::Removal::Removed
}
None => local::Removal::Absent,
}
}
}
impl<'s, 'a, 'd, F, D, T: Copy, Source> Scope<'s, pin::Pin<&'a mut Pool<'d, F, D, T, Source>>> {
pub fn try_admit(
&mut self,
input: owner::Owner<Source>,
parent: wake::RootWaker<'d>,
) -> Result<schedule::raw::Admission<'_, 's, 'd, F, D, T, Source>, queue::Error<Source>> {
self.source.as_mut().admit(self.proof, input, parent)
}
pub fn into_admit(
self,
input: owner::Owner<Source>,
parent: wake::RootWaker<'d>,
) -> Result<schedule::raw::Admission<'a, 's, 'd, F, D, T, Source>, queue::Error<Source>> {
self.source.admit(self.proof, input, parent)
}
pub fn entry(
&mut self,
id: &Id<'s, 'd, D, external::Key<Epoch>>,
) -> Option<schedule::raw::Record<'_, 'd, F, T, schedule::raw::Attached<'_, 'd, Source>>> {
self.source.as_mut().record(id.key)
}
pub fn remove(&mut self, id: Id<'s, 'd, D, external::Key<Epoch>>) -> local::Removal {
match self.entry(&id) {
Some(entry) => {
entry.discard();
local::Removal::Removed
}
None => local::Removal::Absent,
}
}
}
impl<'d, F, D, T: Copy, Source> Scope<'_, pin::Pin<&mut Pool<'d, F, D, T, Source>>> {
pub fn owners(self: pin::Pin<&Self>) -> pin::Pin<&owned::Queue<'d, Source>> {
self.get_ref().source.as_ref().owners()
}
}
impl<'d, F, D, T: Copy, Source: Copy> Scope<'_, pin::Pin<&mut Pool<'d, F, D, T, Source>>> {
pub fn select<'plane, 'turn>(
self: pin::Pin<&'plane mut Self>,
scope: owner::Scope<Source>,
work: app::Work<'turn, 'd>,
) -> Selection<'plane, 'turn, 'd, F, T, Source> {
self.get_mut().source.as_mut().select(scope, work)
}
pub fn wake(self: pin::Pin<&mut Self>, scope: owner::Scope<Source>) {
self.get_mut().source.as_mut().wake(scope);
}
pub fn try_wake(self: pin::Pin<&mut Self>, scope: owner::Scope<Source>) -> queue::Wake {
self.get_mut().source.as_mut().try_wake(scope)
}
}
pub(crate) mod caps;
pub mod fixed;
pub(crate) mod slots;