dope-core 0.76.0

Owned native I/O and completion driver for dope
Documentation
use std::{marker, num::NonZero, ops, pin};

use o3::{
    base::{cell::region, num::Epoch},
    depot::arena::slab::{self, external, key},
};

use crate::{
    driver::schedule::{
        app,
        ready::task::{self, owner},
    },
    executor::task::{
        context::wake,
        storage::fixed::{
            Id, Slab, direct, local,
            schedule::{self, Pool, queue, ready::owned, result::Selection},
        },
    },
};

/// Exclusive access carrying the original storage's invariant identity proof.
#[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: self.source.as_mut().project().inner.vacant()?,
            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>> {
        self.source.as_mut().project().inner.anchor(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>> {
        Scope {
            source: self.source.as_mut(),
            proof: self.proof,
        }
        .into_admit(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>> {
        let storage = self.source.project();
        let slot = storage.slots.try_grant().map_err(|_| {
            queue::Error::Task(task::Error::Capacity {
                requested: NonZero::<usize>::MIN,
                available: 0,
            })
        })?;
        let index = slot.index();
        let parent: wake::Waker<'d> = parent.into();
        let admit = parent.0.try_admit().map_err(queue::Error::Task)?;
        let (edge, prepared) = storage
            .queue
            .into_ref()
            .try_prepare(index, input, parent.0)?;
        // SAFETY: admission and edge retain the same original parent.
        let binding = unsafe { task::raw::Root::bind(admit, edge) };
        Ok(schedule::raw::Admission {
            slot,
            binding,
            prepared,
            proof: self.proof,
            domain: marker::PhantomData,
        })
    }

    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>>> {
        let storage = self.source.as_mut().project();
        let occupied = storage.slots.try_occupied(id.key).ok()?;
        // SAFETY: the checked identity retains this allocation and queue index.
        Some(unsafe { schedule::raw::Record::attach(occupied, storage.queue.into_ref()) })
    }

    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<'s, 'a, 'd, F, D, T: Copy, Source> Scope<'s, pin::Pin<&'a 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<'s, 'a, 'd, F, D, T: Copy, Source: Copy>
    Scope<'s, pin::Pin<&'a 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;