dope-core 0.77.0

Owned native I/O and completion driver for dope
Documentation
use o3::alloc;

use crate::{
    driver::{
        receive::{self, credit},
        route,
        schedule::ready::{
            self, Access, DynamicIndex, Key, QueueIndex, Retained, Stale, TaskParent,
            key::{self, Detached, DispatchKey, TaskKey},
            sealed::Set,
            task,
        },
        settings,
    },
    io::fd::{domain, identity},
};
use sealed::{Entry, Layout, Renewal, Table};
use task::topology::Root;

pub(in crate::driver::schedule::ready) mod pool;
pub(in crate::driver::schedule::ready) mod sealed;

pub(crate) struct Arena {
    descriptors: domain::Domain,
    pub(super) ready: Set,
    pub(super) entries: Table,
}

impl Arena {
    pub(in crate::driver) fn new(plan: settings::Plan<'_>) -> Result<Self, alloc::Error> {
        let layout = Layout::new(plan);
        Ok(Self {
            descriptors: domain::Domain::try_from(plan.config().file_slots())?,
            ready: Set::try_new(plan.ready())?,
            entries: Table::try_new(layout)?,
        })
    }

    pub(crate) const fn descriptors(&self) -> &domain::Domain {
        &self.descriptors
    }

    pub(crate) fn unqueue_fd(&self, slot: &impl QueueIndex) {
        self.ready.remove(slot.queue_index());
    }

    pub(crate) fn activate_fd(&self, id: identity::Identity<'_>) {
        self.ready.insert(QueueIndex::queue_index(&id));
    }

    fn resolve(&self, key: ready::Stored) -> Option<Entry<'_>> {
        let index = key.dense();
        let header = self.entries.slots.header();
        if let Some(slot) = header.descriptor(index) {
            self.descriptors.target_for(slot, key.epoch())?;
            return Some(Entry::dispatch(index));
        }
        if index.get() >= header.capacity.raw() as usize {
            return None;
        }
        self.entries
            .slots
            .resident(key::Local(index.0))
            .resolve(key)
    }

    pub(super) fn free_route<'d>(&'d self, key: DispatchKey<'d>) {
        let index = key.key().raw().dense();
        let held = self.entries.slots.unroute(key);
        self.ready.remove(index);
        self.entries.pool.recycle(&self.entries.slots, held);
    }

    pub(super) fn reclaim_task<'d>(&'d self, key: TaskKey<'d>) -> Renewal<'d> {
        let Detached { task, root } = key.detach(&self.entries.slots);
        if let Some(root) = root {
            self.entries.pool.recycle(&self.entries.slots, root);
        }
        self.entries.slots.renew(task)
    }

    pub(super) fn task_parent<'a, 'd>(
        &'a self,
        access: Access<'a, 'd>,
        key: Key<'d>,
    ) -> TaskParent<'d>
    where
        'd: 'a,
    {
        let Some(entry) = self.resolve(key.raw()) else {
            return TaskParent::Stale;
        };
        match entry.task() {
            Some(task) => TaskParent::Task(access.bind(task.record().parent())),
            None => TaskParent::Root,
        }
    }

    pub(in crate::driver) fn is_dispatch(&self, proof: receive::Proof) -> bool {
        self.try_index(proof.key()).is_some()
    }

    pub(in crate::driver) fn credit_ready(&self, proof: credit::Proof<'_>) {
        let _ = self.try_wake(proof.key());
    }

    pub(in crate::driver) fn try_receive(
        &self,
        proof: receive::Proof,
    ) -> Result<(), receive::Proof> {
        self.try_wake(proof.key()).map_err(|Stale| proof)
    }

    pub(super) fn try_wake(&self, key: ready::Stored) -> Result<(), Stale> {
        self.ready.insert(self.try_index(key).ok_or(Stale)?);
        Ok(())
    }

    /// Activates a completion key that may name a dispatch or nested task.
    pub(super) fn complete<'a, 'd>(&'a self, access: Access<'a, 'd>, key: Retained<'d>)
    where
        'd: 'a,
    {
        let mut key = key.0;
        while let Some(entry) = self.resolve(key.raw()) {
            let Some(task) = entry.task() else {
                self.ready.insert(entry.index);
                break;
            };
            let Some(parent) = Root::activate(&self.entries.slots, task) else {
                break;
            };
            key = access.bind(parent);
        }
    }

    pub(super) fn target(&self, index: DynamicIndex) -> Option<route::Token> {
        let slots = &self.entries.slots;
        if let Some(slot) = slots.header().descriptor(index) {
            self.descriptors.target_at(slot)
        } else {
            Some(slots.target(index))
        }
    }

    fn try_index(&self, key: ready::Stored) -> Option<DynamicIndex> {
        let entry = self.resolve(key)?;
        entry.task().is_none().then_some(entry.index)
    }
}