Skip to main content

dope_runtime/process/
shutdown.rs

1use dope_core::{dope_sys::engine::register::Interest, executor};
2use std::{mem, os::fd};
3
4use dope_core::driver::{Seed, life::domain, result::Kernel, settings};
5
6use crate::process::{control, failure::bus, gate};
7
8/// Shutdown source borrowed from the exact process run that owns this worker.
9///
10/// A proof issued by the standalone shutdown API cannot be retagged as a
11/// production process proof.
12pub struct Shutdown<'run> {
13    pub(in crate::process) source: control::Tx<'run>,
14}
15
16impl<'run> Shutdown<'run> {
17    pub(super) const fn new(tx: control::Tx<'run>) -> Self {
18        Self { source: tx }
19    }
20
21    pub(super) fn gate(&self) -> gate::Closed<'run> {
22        gate::Closed::new(self.source)
23    }
24
25    pub fn control(&self) -> control::Control<'_> {
26        control::Control::new(self.source.as_fd())
27    }
28
29    pub fn terminal(&self) -> crate::process::Terminal<'run> {
30        crate::process::Terminal {
31            source: self.source,
32        }
33    }
34
35    pub(super) fn try_bind<'p>(
36        self,
37        config: settings::Config,
38        seed: impl TryInto<Seed, Error: Into<Kernel>>,
39        place: &'p mut domain::Place<Self>,
40    ) -> Result<domain::Generation<'p, Self>, executor::Error<'p>> {
41        let terminal = self.terminal();
42        let built = domain::Generation::try_bind(config, seed, self, Interest::Readable, place);
43        if built.is_err() {
44            let _ = terminal.fail();
45        }
46        built
47    }
48}
49
50impl executor::Build for Shutdown<'_> {
51    type Shutdown = Self;
52
53    fn try_build<'p>(
54        self,
55        config: settings::Config,
56        place: &'p mut domain::Place<Self::Shutdown>,
57    ) -> Result<domain::Generation<'p, Self>, executor::Error<'p>> {
58        self.try_bind(config, (), place)
59    }
60}
61
62impl fd::AsFd for Shutdown<'_> {
63    fn as_fd(&self) -> fd::BorrowedFd<'_> {
64        self.source.as_fd()
65    }
66}
67
68impl executor::Startup for Shutdown<'_> {
69    type Error = bus::Fault;
70    type Target = Self;
71
72    fn target(&self) -> Option<&Self::Target> {
73        Some(self)
74    }
75
76    fn installed(&self) -> Result<(), Self::Error> {
77        self.source.installed()
78    }
79
80    fn failed(target: &Self::Target) {
81        let _ = target.terminal().fail();
82    }
83}
84
85const _: () = {
86    assert!(mem::size_of::<Shutdown<'static>>() == mem::size_of::<control::Tx<'static>>());
87    assert!(mem::align_of::<Shutdown<'static>>() == mem::align_of::<control::Tx<'static>>());
88};