dope-runtime 0.75.0

Thread-per-core execution and process lifecycle for dope
Documentation
use dope_core::executor::{self, create};
use std::{mem, os::fd};

use dope_core::driver::{
    life::{creation, domain},
    settings,
};

use crate::{
    process::{gate, lifecycle, runtime::bus},
};

/// Shutdown source borrowed from the exact process run that owns this worker.
///
/// A proof issued by the standalone shutdown API cannot be retagged as a
/// production process proof.
pub struct Shutdown<'run> {
    pub(in crate::process) source: bus::Tx<'run>,
}

impl<'run> Shutdown<'run> {
    pub(super) const fn new(tx: bus::Tx<'run>) -> Self {
        Self { source: tx }
    }

    pub(super) fn gate(&self) -> gate::Closed<'run> {
        gate::Closed::new(self.source)
    }

    pub fn control(&self) -> lifecycle::Control<'_> {
        lifecycle::Control::new(self.source.as_fd())
    }

    pub fn terminal(&self) -> crate::process::Terminal<'run> {
        crate::process::Terminal {
            source: self.source,
        }
    }

    pub(super) fn attach<'a>(
        self,
        generation: Result<domain::Generation, create::Error<'a>>,
    ) -> Result<domain::Generation<Self>, create::Error<'a>> {
        let terminal = self.terminal();
        generation
            .and_then(|generation| {
                generation
                    .poll(self, <Self as executor::Startup>::INTEREST)
                    .map_err(create::Error::from)
            })
            .inspect_err(|_| {
                let _ = terminal.fail();
            })
    }
}

impl executor::Build for Shutdown<'_> {
    type Shutdown = Self;

    fn try_build(
        self,
        attempt: &mut creation::Attempt,
        config: settings::Config,
    ) -> Result<domain::Generation<Self>, create::Error<'_>> {
        self.attach(
            attempt
                .try_driver(config)
                .map(domain::Generation::new)
                .map_err(create::Error::from),
        )
    }
}

impl fd::AsFd for Shutdown<'_> {
    fn as_fd(&self) -> fd::BorrowedFd<'_> {
        self.source.as_fd()
    }
}

impl dope_core::executor::Startup for Shutdown<'_> {
    type Error = crate::process::failure::bus::Fault;

    fn installed(&self) -> Result<(), Self::Error> {
        self.source.installed()
    }

    fn failed(&self) {
        let _ = self.terminal().fail();
    }
}

const _: () = {
    assert!(mem::size_of::<Shutdown<'static>>() == mem::size_of::<bus::Tx<'static>>());
    assert!(mem::align_of::<Shutdown<'static>>() == mem::align_of::<bus::Tx<'static>>());
};