dope-core 0.76.0

Owned native I/O and completion driver for dope
Documentation
use std::marker;

use o3::alloc;

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

pub(in crate::driver::schedule::ready) mod cells;
pub(in crate::driver::schedule::ready) mod layout;
pub(in crate::driver::schedule::ready) mod payload;
pub(in crate::driver::schedule::ready) mod pool;

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;
        }
        let resident = self.entries.slots.resident(key::Local(index.0));
        if resident.generation() != key.epoch() {
            return None;
        }
        match resident.kind() {
            Kind::Dispatch => Some(Entry::Dispatch(index)),
            Kind::Task => {
                let task = Resident::<state::Task> {
                    cell: resident.cell,
                    index: index.0,
                    proof: marker::PhantomData,
                };
                Some(Entry::Task(task.bound(), TaskIndex::from_code(index.0)))
            }
            Kind::Free | Kind::Reserved | Kind::Topology | Kind::Retired => None,
        }
    }

    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 {
            Entry::Task(task, _) => TaskParent::Task(access.bind(task.record().parent())),
            Entry::Dispatch(_) => 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()) {
            match entry {
                Entry::Task(task, index) => {
                    let record = task.record();
                    let topology = Root::at(&self.entries.slots, record.topology());
                    if topology.activate(index) != task::Activation::Queued {
                        break;
                    }
                    key = access.bind(record.parent());
                }
                Entry::Dispatch(index) => {
                    self.ready.insert(index);
                    break;
                }
            }
        }
    }

    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.resident(key::Local(index.0)).payload().dispatch())
        }
    }

    fn try_index(&self, key: ready::Stored) -> Option<DynamicIndex> {
        match self.resolve(key)? {
            Entry::Dispatch(index) => Some(index),
            Entry::Task(_, _) => None,
        }
    }
}