use std::mem;
use o3::base::num;
use crate::{
driver::{
route,
route::{indices, indices::SlotIndex},
},
io::fd::{domain::resident, identity},
};
const SIDE_BIT: u32 = 1 << route::SLOT_BITS;
pub(in crate::io::fd::domain) struct Retirement {
index_and_side: u32,
pub(super) generation: num::Generation,
}
#[repr(u8)]
pub(in crate::io::fd::domain) enum Reuse {
Reusable,
Exhausted,
}
#[must_use = "a validated descriptor release must settle or enqueue its registry entry"]
pub(in crate::io::fd::domain) struct Release<'a> {
store: &'a resident::Store,
index: indices::SlotIndex,
entry: resident::Entry<'a>,
phase: resident::Phase,
}
impl Release<'_> {
pub(in crate::io::fd::domain) fn cancel(self) {
self.entry.set_status(resident::Phase::Ready, false);
}
pub(in crate::io::fd::domain) fn enqueue(self) {
self.entry.advance();
self.entry.set_status(resident::Phase::Pending, false);
self.entry.queue(self.store, self.index);
}
pub(in crate::io::fd::domain) fn release(self) {
self.entry.advance();
match self.phase {
resident::Phase::Ready | resident::Phase::Quiescent => self.entry.recycle(),
resident::Phase::Pending | resident::Phase::InFlight => {
self.entry.set_status(self.phase, false);
}
}
}
}
const _: () = {
assert!(SIDE_BIT as u64 == route::SLOT_MASK + 1);
assert!(mem::size_of::<resident::Phase>() == mem::size_of::<u8>());
assert!(mem::size_of::<Retirement>() == mem::size_of::<u64>());
assert!(mem::size_of::<Reuse>() == mem::size_of::<u8>());
};
impl Retirement {
pub(in crate::io::fd::domain) const fn new(
index: indices::SlotIndex,
epoch: num::Generation,
side: resident::Side,
) -> Self {
Self {
index_and_side: index.raw()
| match side {
resident::Side::Zero => 0,
resident::Side::One => SIDE_BIT,
},
generation: epoch,
}
}
pub(in crate::io::fd::domain) const fn index(&self) -> indices::SlotIndex {
indices::SlotIndex::from_field(self.index_and_side as u64)
}
pub(in crate::io::fd::domain) const fn generation(&self) -> num::Generation {
self.generation
}
pub(in crate::io::fd::domain) const fn side(&self) -> resident::Side {
if self.index_and_side & SIDE_BIT == 0 {
resident::Side::Zero
} else {
resident::Side::One
}
}
}
impl<'a> Release<'a> {
pub(in crate::io::fd::domain) fn new(
store: &'a resident::Store,
id: identity::Identity<'_>,
) -> Self {
let slot = id.slot();
let index = SlotIndex::from(slot);
let entry = store.at(resident::Bound::slot(slot));
let phase = entry.phase();
Self {
store,
index,
phase,
entry,
}
}
}