Skip to main content

dope_runtime/process/
shutdown.rs

1use dope_core::executor;
2use std::{mem, os::fd};
3
4use dope_core::driver::{life::domain, 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
36impl executor::Build for Shutdown<'_> {
37    type Shutdown = Self;
38
39    fn try_build<'p>(
40        self,
41        config: settings::Config,
42        place: &'p mut domain::Place<Self::Shutdown>,
43    ) -> Result<domain::Generation<'p, Self>, executor::Error<'p>> {
44        let terminal = self.terminal();
45        domain::Generation::try_registered(
46            config,
47            self,
48            <Self as executor::Startup>::INTEREST,
49            place,
50        )
51        .inspect_err(|_| {
52            let _ = terminal.fail();
53        })
54    }
55}
56
57impl fd::AsFd for Shutdown<'_> {
58    fn as_fd(&self) -> fd::BorrowedFd<'_> {
59        self.source.as_fd()
60    }
61}
62
63impl executor::Startup for Shutdown<'_> {
64    type Error = bus::Fault;
65    type Target = Self;
66
67    fn target(&self) -> Option<&Self::Target> {
68        Some(self)
69    }
70
71    fn installed(&self) -> Result<(), Self::Error> {
72        self.source.installed()
73    }
74
75    fn failed(target: &Self::Target) {
76        let _ = target.terminal().fail();
77    }
78}
79
80const _: () = {
81    assert!(mem::size_of::<Shutdown<'static>>() == mem::size_of::<control::Tx<'static>>());
82    assert!(mem::align_of::<Shutdown<'static>>() == mem::align_of::<control::Tx<'static>>());
83};