use crate::{
Backend,
driver::{Key, NativeSlot, engine::Engine},
};
use dope_sys::{
engine::{
admit::{self, Admit as _, Context},
ring,
},
io::{
reactor::submit::Queue as _,
request,
socket::{addr, option, option::tuning},
},
ring::ready::Ready as _,
table::token::{Codec, Tuning as _},
};
use o3::depot::capacity::Limit;
type Target<C> = <<C as Context>::Inputs as request::Context>::Target;
unsafe fn submit<C, I, O, const N: usize>(
backend: &mut Engine<C>,
input: I,
slot: Target<C>,
(count, options): (Limit<N>, O),
terminal: Option<addr::Addr<'_>>,
retry: &mut bool,
) -> Result<C::Output, C::Error>
where
C: ring::Context<Tuning = crate::driver::raw::Table<Key<<C as ring::Context>::Token>, N>>
+ Context<Native = Backend, Key = Key<<C as ring::Context>::Token>>,
NativeSlot<C>: From<Target<C>>,
C: admit::raw::Flight<I>,
<C as Context>::Key: for<'a> From<&'a C::Reserved>,
O: option::Cursor,
{
let token = C::Token::tuning(<C::Token as Codec>::Tuning::new(NativeSlot::<C>::from(
slot,
)));
let slot = <Backend as request::Source<C::Inputs>>::target(slot);
let ready = match backend.ring.submission().try_reserve(count) {
Ok(ready) => ready,
Err(full) => {
*retry = true;
return Err(full.into());
}
};
let reserved: C::Reserved = unsafe { C::front(input) }?.into();
let terminal = terminal.map(|address| unsafe { crate::raw::Deferred::new(slot, address) });
let state = unsafe { backend.tuning.admitted(slot) };
let values = unsafe { state.begin((&reserved).into(), count.count(), terminal) };
unsafe { tuning::raw::Chain::submit(ready, values, options, slot, token) };
Ok(reserved.into())
}
impl<C, const N: usize> admit::raw::Socket<C, N> for Engine<C>
where
C: ring::Context<Tuning = crate::driver::raw::Table<Key<<C as ring::Context>::Token>, N>>
+ Context<Native = Backend, Key = Key<<C as ring::Context>::Token>>,
NativeSlot<C>: From<Target<C>>,
{
unsafe fn connect<I, O>(
&mut self,
input: I,
slot: Target<C>,
options: Option<(Limit<N>, O)>,
address: addr::Addr<'_>,
retry: &mut bool,
) -> Result<C::Output, C::Error>
where
C: admit::raw::Flight<I>,
<C as Context>::Key: for<'a> From<&'a C::Reserved>,
O: option::Cursor,
{
if let Some(options) = options {
return unsafe { submit(self, input, slot, options, Some(address), retry) };
}
let front = unsafe { C::front(input) }?;
let permit = match self.try_admit() {
Ok(permit) => permit,
Err(full) => {
*retry = true;
return Err(full.into());
}
};
let reserved: C::Reserved = front.into();
let slot = <Backend as request::Source<C::Inputs>>::target(slot);
unsafe { admit::raw::Retain::<C>::connect(permit, slot, address, (&reserved).into()) };
Ok(reserved.into())
}
unsafe fn tune<I, O, D, R, E>(
&mut self,
input: I,
fd: D,
slot: Target<C>,
options: Option<(Limit<N>, O)>,
retry: &mut bool,
) -> Result<R, E>
where
C: admit::raw::Flight<I>,
<C as Context>::Key: for<'a> From<&'a C::Reserved>,
O: option::Cursor,
R: From<D> + From<(D, C::Output, <C::Native as admit::Native>::Permit)>,
E: From<(D, C::Error)>,
{
let Some(options) = options else {
return Ok(fd.into());
};
match unsafe { submit(self, input, slot, options, None, retry) } {
Ok(flight) => Ok((fd, flight, ()).into()),
Err(cause) => Err((fd, cause).into()),
}
}
unsafe fn peer<I, D, R, E>(
&mut self,
input: I,
fd: D,
slot: Target<C>,
retry: &mut bool,
) -> Result<R, E>
where
C: admit::raw::Flight<I>,
<C as Context>::Key: for<'a> From<&'a C::Reserved>,
R: From<(D, u32)> + From<(D, C::Output, <C::Native as admit::Native>::Permit)>,
E: From<(D, C::Error)>,
{
let token = C::Token::tuning(<C::Token as Codec>::Tuning::new(NativeSlot::<C>::from(
slot,
)));
let slot = <Backend as request::Source<C::Inputs>>::target(slot);
let ready = match self.ring.submission().try_one() {
Ok(ready) => ready,
Err(full) => {
*retry = true;
return Err((fd, C::Error::from(full)).into());
}
};
let front = match unsafe { C::front(input) } {
Ok(front) => front,
Err(cause) => return Err((fd, cause).into()),
};
let reserved: C::Reserved = front.into();
let state = unsafe { self.tuning.admitted(slot) };
unsafe { state.peer(ready, slot, (&reserved).into(), token) };
let flight: C::Output = reserved.into();
Ok((fd, flight, ()).into())
}
}