dope-core 0.77.0

Owned native I/O and completion driver for dope
Documentation
use crate::{
    driver::{
        route,
        route::{indices::SlotIndex, table},
    },
    io::fd::{
        domain::{
            Index,
            resident::{
                Counter, EXHAUSTED_MASK, Entry, LIFECYCLE_MASK, LIFECYCLE_SHIFT, Phase, STATE_MASK,
                TARGET_WORDS,
            },
        },
        states,
    },
};
use o3::{
    alloc::{self, array},
    base::marker::ThreadBound,
};
use std::cell;

mod words;
pub(super) use words::Words;

pub(in crate::io::fd::domain) struct Rows {
    storage: array::raw::Block<[cell::Cell<u64>; TARGET_WORDS + 1], table::Capacity>,
    thread: ThreadBound,
}

impl Rows {
    pub(super) fn try_new(capacity: table::Capacity) -> Result<Self, alloc::Error> {
        Ok(Self {
            storage: array::raw::Block::try_new(capacity, ())?.repeat_with(Default::default),
            thread: ThreadBound::NEW,
        })
    }

    pub(in crate::io::fd::domain) fn get(&self, index: SlotIndex) -> Option<Entry<'_>> {
        if self.storage.extent().contains(index) {
            Some(self.at(Index(index)))
        } else {
            None
        }
    }

    pub(in crate::io::fd::domain) fn at(&self, index: impl Into<Index>) -> Entry<'_> {
        let index = index.into().0.raw() as usize;
        let counter = self.storage.len() * TARGET_WORDS + index;
        let words = self.storage.as_flattened();
        // SAFETY: the admitted index addresses both the target-pair prefix and
        // its generation in this driver's same-capacity counter tail.
        unsafe {
            Entry {
                words: Words::new(
                    words.as_chunks::<TARGET_WORDS>().0.get_unchecked(index),
                    self.thread,
                ),
                generation: Counter {
                    word: words.get_unchecked(counter),
                },
            }
        }
    }

    pub(in crate::io::fd::domain) fn has_retiring(&self) -> bool {
        let solo = u64::from(states::SlotState::STANDALONE.raw()) << LIFECYCLE_SHIFT;
        self.storage
            .as_flattened()
            .as_chunks::<TARGET_WORDS>()
            .0
            .iter()
            .take(self.storage.len())
            .any(|words| {
                let state = words[1].get() & (STATE_MASK | LIFECYCLE_MASK | EXHAUSTED_MASK);
                state == solo | ((Phase::Pending as u64) << route::AUX_SHIFT)
                    || state == solo | ((Phase::InFlight as u64) << route::AUX_SHIFT)
            })
    }
}