dope-core 0.75.0

Owned native I/O and completion driver for dope
Documentation
use dope_sys::{
    engine::admit,
    io::{request::Source, socket::addr},
};

use crate::{
    Backend,
    driver::{
        life::{
            self,
            native::{host::Host, request},
        },
        retained::flight,
        route::{self, kind},
    },
    io::{
        fd::handles,
        socket::{option, pending},
    },
};

#[cfg(target_os = "linux")]
use dope_sys::{
    engine::ring::Backend as _,
    io::{reactor::submit::Queue as _, socket::option::tuning::raw::Chain as _},
    ring::ready::Ready as _,
    table::token::{Codec, Tuning as _},
};

#[cfg(target_os = "linux")]
type NativeToken = <Host<Backend> as dope_sys::engine::ring::Context>::Token;

#[cfg(target_os = "linux")]
/// # Safety
/// The descriptor, route and flight storage belong to this driver. Retain the address
/// and descriptor through terminal completion or native quiescence.
pub(crate) unsafe fn connect<'d, Tag>(
    local: &mut life::Local<'d>,
    slots: &flight::Slots<'d, Tag>,
    target: route::Operation<'d, Tag, kind::Connect>,
    fd: &handles::Descriptor<'d>,
    options: &option::Set,
    address: addr::Addr<'_>,
) -> Result<flight::Flight<'d, kind::Connect>, pending::Cause>
where
    Tag: route::Tag + route::Select<kind::Connect>,
{
    if let Some(options) = options.non_empty() {
        return unsafe { submit_options(local, slots, target, fd, options, Some(address)) };
    }
    let front = match unsafe { slots.front(target) } {
        Ok(front) => front,
        Err(error) => return Err(pending::Cause::Submit(error.into())),
    };
    let permit = match admit::Admit::<Host<Backend>>::try_admit(&mut *local.backend) {
        Ok(permit) => permit,
        Err(full) => {
            local.retry = true;
            return Err(pending::Cause::Submit(full.into()));
        }
    };
    let reserved = front.reserve();
    let slot = <Backend as Source<request::Context>>::target(fd.slot());
    unsafe { admit::raw::Retain::<Host<Backend>>::connect(permit, slot, address, reserved.key()) };
    Ok(reserved.commit())
}

#[cfg(target_os = "macos")]
/// # Safety
/// The descriptor, route and flight storage belong to this driver. Retain the address
/// and descriptor through terminal completion or native quiescence.
pub(crate) unsafe fn connect<'d, Tag>(
    local: &mut life::Local<'d>,
    slots: &flight::Slots<'d, Tag>,
    target: route::Operation<'d, Tag, kind::Connect>,
    fd: &handles::Descriptor<'d>,
    options: &option::Set,
    address: addr::Addr<'_>,
) -> Result<flight::Flight<'d, kind::Connect>, pending::Cause>
where
    Tag: route::Tag + route::Select<kind::Connect>,
{
    let front = match unsafe { slots.front(target) } {
        Ok(front) => front,
        Err(error) => return Err(pending::Cause::Submit(error.into())),
    };
    let permit = match admit::Admit::<Host<Backend>>::try_admit(&mut *local.backend) {
        Ok(permit) => permit,
        Err(full) => {
            local.retry = true;
            return Err(pending::Cause::Submit(full.into()));
        }
    };
    let slot = <Backend as Source<request::Context>>::target(fd.slot());
    if let Some(options) = options.non_empty() {
        admit::Options::<Host<Backend>>::set(&permit, slot, Some(options.split().1))
            .map_err(pending::Cause::Option)?;
    }
    let reserved = front.reserve();
    unsafe { admit::raw::Retain::<Host<Backend>>::connect(permit, slot, address, reserved.key()) };
    Ok(reserved.commit())
}

#[cfg(target_os = "linux")]
/// # Safety
/// Retain the admitted descriptor and any terminal address through native completion.
pub(crate) unsafe fn submit_options<'d, Tag, C>(
    local: &mut life::Local<'d>,
    slots: &flight::Slots<'d, Tag>,
    target: route::Operation<'d, Tag, C>,
    fd: &handles::Descriptor<'d>,
    options: option::NonEmpty<'_>,
    terminal: Option<addr::Addr<'_>>,
) -> Result<flight::Flight<'d, C>, pending::Cause>
where
    Tag: route::Tag + route::Select<C>,
    C: kind::Flight,
{
    let (count, options) = options.split();
    let slot = <Backend as Source<request::Context>>::target(fd.slot());
    let token = <NativeToken as Codec>::tuning(<NativeToken as Codec>::Tuning::new(fd.index()));
    let (ring, table) = local.backend.split();
    let ready = match ring.submission().try_reserve(count) {
        Ok(ready) => ready,
        Err(full) => {
            local.retry = true;
            return Err(pending::Cause::Submit(full.into()));
        }
    };
    let front = match unsafe { slots.front(target) } {
        Ok(front) => front,
        Err(error) => return Err(pending::Cause::Submit(error.into())),
    };
    let reserved = front.reserve();
    let terminal = match terminal {
        Some(address) => Some(unsafe { dope_uring::Deferred::new(slot, address) }),
        None => None,
    };
    let state = unsafe { table.admitted(slot) };
    let values = unsafe { state.begin(reserved.key(), count.count(), terminal) };
    unsafe { ready.submit(values, options, slot, token) };
    Ok(reserved.commit())
}

#[cfg(target_os = "linux")]
/// # Safety
/// The descriptor and typed route belong to these slots and remain live through completion.
pub(crate) unsafe fn peer<'d, Tag>(
    local: &mut life::Local<'d>,
    slots: &flight::Slots<'d, Tag>,
    target: route::Operation<'d, Tag, kind::Tuning>,
    fd: &handles::Descriptor<'d>,
) -> Result<flight::Flight<'d, kind::Tuning>, pending::Cause>
where
    Tag: route::Tag + route::Select<kind::Tuning>,
{
    let slot = <Backend as Source<request::Context>>::target(fd.slot());
    let token = <NativeToken as Codec>::tuning(<NativeToken as Codec>::Tuning::new(fd.index()));
    let (ring, table) = local.backend.split();
    let ready = match ring.submission().try_one() {
        Ok(ready) => ready,
        Err(full) => {
            local.retry = true;
            return Err(pending::Cause::Submit(full.into()));
        }
    };
    let front = match unsafe { slots.front(target) } {
        Ok(front) => front,
        Err(error) => return Err(pending::Cause::Submit(error.into())),
    };
    let reserved = front.reserve();
    let state = unsafe { table.admitted(slot) };
    unsafe { state.peer(ready, slot, reserved.key(), token) };
    Ok(reserved.commit())
}

#[cfg(target_os = "macos")]
pub(crate) fn peer<'d>(
    local: &mut life::Local<'d>,
    fd: &handles::Descriptor<'d>,
) -> Result<u32, dope_sys::Errno> {
    use dope_sys::{
        io::reactor::ready::{Backend as _, store::State as _},
        table::Files as _,
    };
    let slot = <Backend as Source<request::Context>>::target(fd.slot());
    let native = local
        .backend
        .state()
        .view()
        .files
        .borrow(slot)
        .ok_or(dope_sys::Errno::BAD_DESCRIPTOR)?;
    dope_kqueue::peer_uid(native)
}