dope-uring 0.77.0

Owned io_uring backend for dope
Documentation
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());
        }
    };
    // SAFETY: native capacity is held and no host free-list access intervenes.
    let reserved: C::Reserved = unsafe { C::front(input) }?.into();
    // SAFETY: the caller retains the address through the entire two-stage flight.
    let terminal = terminal.map(|address| unsafe { crate::raw::Deferred::new(slot, address) });
    // SAFETY: the descriptor proves this native table slot remains admitted.
    let state = unsafe { backend.tuning.admitted(slot) };
    // SAFETY: every scalar and deferred address remains pinned through terminal CQ.
    let values = unsafe { state.begin((&reserved).into(), count.count(), terminal) };
    // SAFETY: count and cursor are the same admitted nonempty option set.
    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 {
            // SAFETY: the caller retains this descriptor, address and exact host flight.
            return unsafe { submit(self, input, slot, options, Some(address), retry) };
        }
        // SAFETY: native admission does not touch the host's exclusively lent free list.
        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);
        // SAFETY: the caller keeps the descriptor and address live through terminal CQ.
        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());
        };
        // SAFETY: the caller retains this descriptor and its typed host flight.
        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());
            }
        };
        // SAFETY: no host operation runs between this loan and its reservation.
        let front = match unsafe { C::front(input) } {
            Ok(front) => front,
            Err(cause) => return Err((fd, cause).into()),
        };
        let reserved: C::Reserved = front.into();
        // SAFETY: the descriptor admits this pinned slot for the full native flight.
        let state = unsafe { self.tuning.admitted(slot) };
        // SAFETY: the same pinned state and host owner survive the terminal CQ.
        unsafe { state.peer(ready, slot, (&reserved).into(), token) };
        let flight: C::Output = reserved.into();
        Ok((fd, flight, ()).into())
    }
}