dope-uring 0.75.0

Owned io_uring backend for dope
Documentation
use std::ptr;

use dope_sys::{
    engine::admit::{self, Context},
    io::{
        self,
        reactor::{complete, submit},
        socket::addr,
    },
    table::token::Codec,
};
use o3::{base::marker::ThreadBound, buffer::bytes};

use super::{Entry, Fields};
use crate::{
    Uring,
    driver::Key,
    reactor::{
        op::{self, buffers, messages, sockets},
        ring, sq,
    },
};

impl<T: complete::raw::Token> sq::raw::Vacancy<'_, T> {
    /// # Safety
    /// The fields must describe the selected operation and all encoded addresses
    /// must remain valid through terminal completion or quiescence.
    unsafe fn fixed(self, slot: sq::Fixed, token: T, mut fields: Fields) {
        fields.flags = sq::Flags::FIXED_FILE.raw();
        fields.fd = slot.index() as i32;
        let entry = sq::Borrowed::new(Entry(fields, ThreadBound::NEW));
        // SAFETY: the caller retains this exact request graph through its terminal flight.
        unsafe { submit::raw::Vacancy::push(self, entry, token) };
    }

    unsafe fn write(self, opcode: op::Code, slot: sq::Fixed, token: T, address: u64, length: u32) {
        let fields = Fields {
            opcode: opcode.raw(),
            address,
            length,
            options: libc::MSG_NOSIGNAL.unsigned_abs(),
            ..Fields::default()
        };
        // SAFETY: the host retains this buffer or complete message graph.
        unsafe { self.fixed(slot, token, fields) };
    }
}

impl sq::raw::Deferred {
    pub(crate) fn submit<T: complete::raw::Token>(
        self,
        vacancy: sq::raw::Vacancy<'_, T>,
        token: T,
    ) {
        let (slot, address, length) = self.into_parts();
        let fields = Fields {
            opcode: sockets::CONNECT.raw(),
            address: address as u64,
            offset: (length.get() as u64).to_ne_bytes(),
            ..Fields::default()
        };
        // SAFETY: construction retained this address through completion.
        unsafe { vacancy.fixed(slot, token, fields) };
    }
}

impl<C, T> admit::raw::Retain<C> for sq::raw::Vacancy<'_, T>
where
    C: Context<Native = Uring, Key = Key<T>>,
    T: Codec,
{
    unsafe fn send(
        self,
        slot: sq::Fixed,
        input: bytes::Prefix<'_, { i32::MAX as usize }>,
        key: Key<T>,
    ) {
        let bytes = input.as_slice();
        let address = bytes.as_ptr() as u64;
        let length = bytes.len() as u32;
        // SAFETY: the host retains the bytes through terminal completion.
        unsafe { self.write(buffers::SEND, slot, T::flight(key), address, length) };
    }

    unsafe fn send_msg<'a>(self, slot: sq::Fixed, message: impl io::raw::Message<'a>, key: Key<T>) {
        let address = ptr::from_ref(message.header()) as u64;
        // SAFETY: the host retains the complete immutable message graph.
        unsafe { self.write(messages::SEND, slot, T::flight(key), address, 1) };
    }

    unsafe fn accept(self, listener: sq::Fixed, peer: &mut addr::Storage, key: Key<T>) {
        let (address, length) = peer.prepare_output();
        let fields = Fields {
            opcode: sockets::ACCEPT.raw(),
            address: ptr::from_mut(address) as u64,
            offset: (ptr::from_mut(length) as u64).to_ne_bytes(),
            index: ring::ffi::FILE_INDEX_ALLOC,
            ..Fields::default()
        };
        // SAFETY: the host retains both exclusive outputs through completion.
        unsafe { self.fixed(listener, T::flight(key), fields) };
    }

    unsafe fn connect(self, slot: sq::Fixed, address: addr::Addr<'_>, key: Key<T>) {
        // SAFETY: the host retains the address through this flight.
        let request = unsafe { sq::raw::Deferred::new(slot, address) };
        request.submit(self, T::flight(key));
    }

    unsafe fn file(self, request: sq::Borrowed<'_>, key: Key<T>) {
        // SAFETY: the host retains every encoded file borrow through this flight's completion.
        unsafe { submit::raw::Vacancy::push(self, request, T::flight(key)) };
    }
}