use crate::driver::life::finish;
pub mod cancel;
mod complete;
mod echo;
pub mod raw;
pub mod storage;
use std::{marker, mem};
pub use complete::Complete;
use dope_sys::{
engine::admit::Cancelled,
io::reactor::event::{
self, Report, fault,
kind::{self, Native, Witness},
},
io::socket::Create,
kernel,
table::token::host,
};
pub use echo::Echo;
use o3::base::num::Epoch;
use route::table;
use table::conn;
use crate::driver::{
self,
route::{
self,
indices::{SlotIndex, SlotOverflow},
kind::{Any, Class, Code, Kind},
},
};
use crate::{
Backend,
driver::{
receive,
result::poll::{self, fault::Observation},
},
io::{
event::net::accept,
fd::handles,
transfer::{Len, NonZero},
},
};
pub(crate) const INDEX_BITS: u32 = route::SLOT_BITS;
pub(crate) const GENERATION_BITS: u32 = 32;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(C)]
pub(crate) struct Target {
epoch: Epoch,
slot: SlotIndex,
kind: Native,
route: route::Route,
}
impl Target {
fn new<Tag: route::Tag, C: Class + kind::Flight>(target: route::Operation<'_, Tag, C>) -> Self {
let parts = target.parts();
Self {
epoch: parts.epoch(),
slot: parts.slot(),
kind: C::FLIGHT,
route: Tag::ROUTE,
}
}
pub(crate) fn bind<'d, C: Class>(
self,
driver: driver::Invariant<'d>,
flight: Echo,
) -> route::Erased<'d, C> {
let code = if matches!(C::KIND, Kind::Unspecified) {
Code::native(self.kind)
} else {
C::CODE
};
route::Erased {
token: route::Token::from_flight(self.epoch, self.slot, code, self.route),
flight: Some(flight),
driver,
class: marker::PhantomData,
}
}
pub(crate) fn bind_native<'d, K: kind::Flight>(
self,
driver: driver::Invariant<'d>,
flight: Echo,
_: Witness<K>,
) -> route::Erased<'d, K> {
route::Erased {
token: route::Token::from_flight(
self.epoch,
self.slot,
Code::native(K::FLIGHT),
self.route,
),
flight: Some(flight),
driver,
class: marker::PhantomData,
}
}
}
#[repr(transparent)]
pub struct Arena(storage::Flights<Target, INDEX_BITS, GENERATION_BITS>);
#[derive(Clone, Copy, Eq, PartialEq)]
#[repr(transparent)]
pub struct Capacity(usize);
pub struct Need<Tags, const GROUPS: usize = 1, B = ()> {
caps: [Capacity; GROUPS],
tags: marker::PhantomData<(Tags, B)>,
}
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
pub enum ScaleError {
Product {
capacity: conn::Capacity,
factor: usize,
},
Limit {
actual: usize,
maximum: usize,
},
}
impl Capacity {
pub const fn fixed<const FLIGHTS: usize>() -> Self {
const { assert!(FLIGHTS > 0 && FLIGHTS <= route::SLOT_MASK as usize + 1) };
Self(FLIGHTS)
}
pub const fn table(capacity: table::Capacity) -> Self {
Self(capacity.count().get())
}
pub const fn connections<const PER_CONNECTION: usize>(
capacity: conn::Capacity,
) -> Result<Self, ScaleError> {
const { assert!(PER_CONNECTION > 0) };
let Some(total) = (capacity.raw() as usize).checked_mul(PER_CONNECTION) else {
return Err(ScaleError::Product {
capacity,
factor: PER_CONNECTION,
});
};
let maximum = route::SLOT_MASK as usize + 1;
if total > maximum {
return Err(ScaleError::Limit {
actual: total,
maximum,
});
}
Ok(Self(total))
}
pub const fn capped<const MAX: usize>(capacity: table::Capacity) -> Self {
const { assert!(MAX > 0) };
let capacity = capacity.count().get();
Self(if capacity < MAX { capacity } else { MAX })
}
pub(crate) const fn get(self) -> usize {
self.0
}
}
impl<Tag: route::Tag> Need<Tag> {
pub const fn new(cap: Capacity) -> Self {
Self {
caps: [cap],
tags: marker::PhantomData,
}
}
}
impl<Tag: route::Tag> Need<Tag> {
pub fn with_tail<B>(self) -> Need<Tag, 1, B> {
Need {
caps: self.caps,
tags: marker::PhantomData,
}
}
}
impl<A: route::Tag> Need<A> {
pub fn pair<C: route::Tag, B>(self, other: Need<C, 1, B>) -> Need<(A, C), 2, B> {
let Self {
caps: [first],
tags: marker::PhantomData,
} = self;
let Need {
caps: [second],
tags: marker::PhantomData,
} = other;
Need {
caps: [first, second],
tags: marker::PhantomData,
}
}
}
impl<A: route::Tag, B: route::Tag> Need<(A, B), 2> {
pub fn push<C: route::Tag>(self, other: Need<C>) -> Need<(A, B, C), 3> {
let Self {
caps: [first, second],
tags: marker::PhantomData,
} = self;
let Need {
caps: [third],
tags: marker::PhantomData,
} = other;
Need {
caps: [first, second, third],
tags: marker::PhantomData,
}
}
}
impl<Tags, const GROUPS: usize, B> Need<Tags, GROUPS, B> {
const fn into_caps(self) -> [Capacity; GROUPS] {
self.caps
}
}
#[must_use = "prepared flight slots must be committed"]
pub struct Prepared<'a, 'd, Tags, const GROUPS: usize = 1, B = ()> {
prepared: storage::Prepared<'a, 'd, Target, INDEX_BITS, GENERATION_BITS, GROUPS, B>,
driver: driver::Invariant<'d>,
tags: marker::PhantomData<Tags>,
}
#[must_use = "a reserved flight must be committed or released"]
#[repr(transparent)]
pub(crate) struct Reserved<'d, C: Class = Any> {
reservation: storage::Reserved<'d, Target, INDEX_BITS, GENERATION_BITS>,
driver: driver::Invariant<'d>,
class: marker::PhantomData<C>,
}
#[must_use = "a live flight must reach terminal completion or quiescence"]
#[repr(transparent)]
pub struct Flight<'d, C: Class = Any> {
flight: storage::Lease<'d, Target, INDEX_BITS, GENERATION_BITS>,
driver: driver::Invariant<'d>,
class: marker::PhantomData<C>,
}
impl Arena {
pub(crate) fn new() -> Self {
Self(storage::Flights::new())
}
pub(in crate::driver) fn finish(&self) {
self.0.finish();
}
}
impl<'d, Tags, const GROUPS: usize, B> Prepared<'_, 'd, Tags, GROUPS, B> {
fn commit(
self,
) -> (
[storage::raw::Slots<'d, Target, INDEX_BITS, GENERATION_BITS>; GROUPS],
driver::Invariant<'d>,
) {
let Self {
prepared,
driver,
tags: marker::PhantomData,
} = self;
let slots = prepared.groups();
(slots, driver)
}
}
impl<'d, Tag: route::Tag> Prepared<'_, 'd, Tag> {
pub fn slots(self) -> raw::Slots<'d, Tag> {
let ([slots], driver) = self.commit();
raw::Slots::new(slots, driver)
}
}
impl<'d, A: route::Tag, B: route::Tag> Prepared<'_, 'd, (A, B), 2> {
pub fn slots(self) -> (raw::Slots<'d, A>, raw::Slots<'d, B>) {
let ([first, second], driver) = self.commit();
(
raw::Slots::new(first, driver),
raw::Slots::new(second, driver),
)
}
}
impl<'d, A: route::Tag, B: route::Tag, C: route::Tag> Prepared<'_, 'd, (A, B, C), 3> {
pub fn slots(self) -> (raw::Slots<'d, A>, raw::Slots<'d, B>, raw::Slots<'d, C>) {
let ([first, second, third], driver) = self.commit();
(
raw::Slots::new(first, driver),
raw::Slots::new(second, driver),
raw::Slots::new(third, driver),
)
}
}
impl<'d, A: route::Tag, C: route::Tag, B> Prepared<'_, 'd, (A, C), 2, B> {
pub fn split(self) -> (raw::Slots<'d, A>, raw::Backing<'d, C, B>) {
let ([first, second], driver) = self.commit();
(
raw::Slots::new(first, driver),
raw::Backing::new(second, driver),
)
}
}
impl<'d, C: Class> From<Reserved<'d, C>> for Flight<'d, C> {
fn from(reserved: Reserved<'d, C>) -> Self {
Self {
flight: reserved.reservation.commit(),
driver: reserved.driver,
class: marker::PhantomData,
}
}
}
impl host::raw::Drain for Arena {
type Driver<'d> = driver::Reference<'d>;
type Key = Echo;
type Flight<'d> = Complete<'d>;
type Fault = fault::Fault<
Observation,
fault::Subject<route::Token, route::Control, fault::Echo<Echo>, SlotIndex>,
fault::Violation<
receive::Selection,
fault::Echo<Echo>,
Len,
NonZero,
<Backend as kernel::Source>::Returned,
SlotIndex,
SlotOverflow,
table::Capacity,
handles::Retirement,
>,
>;
type Reported = event::Reclaimed<
Self::Fault,
poll::Cause,
cancel::Failure,
route::Token,
handles::PhysicalSlot,
accept::Failure,
fault::Echo<Echo>,
Len,
poll::Missing,
handles::Reclaimed,
Create,
>;
unsafe fn bind<'d>(&'d self, key: Echo, driver: &Self::Driver<'d>) -> Option<Self::Flight<'d>> {
let bound = self.0.drain().bind(key.into_inner())?;
Some(Complete {
completion: bound,
driver: *driver,
})
}
fn is_pending(&self) -> bool {
self.0.is_pending()
}
}
impl<'d, C: Class> Flight<'d, C> {
pub(crate) fn is_pending(&self) -> bool {
self.flight.is_pending()
}
pub(crate) fn key(&self) -> Echo {
Echo::from_inner(self.flight.echo())
}
pub fn target(&self) -> route::Erased<'d, C> {
self.flight.value().bind(self.driver, self.key())
}
pub fn matches(&self, target: route::Erased<'d, C, impl route::Selection>) -> bool {
target.flight == Some(self.key())
}
pub(in crate::driver) fn finish(
mut self,
proof: &mut finish::Finish<'_, 'd, impl Report<Event = crate::Reported>>,
) {
self.driver = proof.brand();
}
}
const _: () = {
assert!(mem::size_of::<Arena>() == 4 * mem::size_of::<usize>());
assert!(mem::align_of::<Arena>() == mem::align_of::<usize>());
assert!(mem::size_of::<Target>() == mem::size_of::<route::Token>());
assert!(mem::align_of::<Target>() == mem::align_of::<route::Token>());
assert!(mem::size_of::<Echo>() == mem::size_of::<usize>());
assert!(mem::size_of::<Flight<'static>>() == mem::size_of::<usize>());
assert!(mem::size_of::<Option<Flight<'static>>>() == mem::size_of::<usize>());
assert!(mem::size_of::<Reserved<'static>>() == mem::size_of::<usize>());
assert!(mem::size_of::<Complete<'static>>() == 2 * mem::size_of::<usize>());
assert!(mem::size_of::<raw::Slots<'static, route::KeyTag<1>>>() == mem::size_of::<usize>());
};
impl<'d, C: Class> Flight<'d, C> {
pub fn try_cancel(
&mut self,
driver: &mut driver::Local<'d>,
) -> Result<Cancelled, cancel::Error> {
driver.native().try_cancel(self)
}
}
impl<'d, C: Class> Flight<'d, C> {
pub fn try_stop(
&mut self,
driver: &mut driver::Local<'d>,
) -> Result<cancel::Outcome, cancel::Failure> {
cancel::Outcome::try_from(self.try_cancel(driver))
}
}