Skip to main content

dope_runtime/process/
shutdown.rs

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