Skip to main content

dope_runtime/process/
context.rs

1use dope_core::{
2    driver::{life::creation, settings},
3    platform::{affinity, affinity::Binding},
4    random,
5};
6
7use crate::{
8    executor::{self, create},
9    process::{self, gate, runtime::bus},
10};
11
12pub struct Context<'run> {
13    affinity: Binding,
14    seed: random::Seed,
15    shutdown: process::Shutdown<'run>,
16}
17
18impl<'run> Context<'run> {
19    pub(super) const fn new(bind: Binding, seed: random::Seed, tx: bus::Tx<'run>) -> Self {
20        Self {
21            affinity: bind,
22            seed,
23            shutdown: process::Shutdown::new(tx),
24        }
25    }
26
27    pub fn cpu(&self) -> affinity::Cpu {
28        self.affinity.cpu()
29    }
30
31    pub fn terminal(&self) -> process::Terminal<'run> {
32        self.shutdown.terminal()
33    }
34
35    pub(super) fn gated(self) -> (Self, gate::Closed<'run>) {
36        let gate = self.shutdown.gate();
37        (self, gate)
38    }
39}
40
41impl<'run> executor::Build for Context<'run> {
42    type Shutdown = process::Shutdown<'run>;
43
44    fn try_build(
45        self,
46        attempt: &mut creation::Attempt,
47        config: settings::Config,
48    ) -> Result<executor::Executor<(), Self::Shutdown>, create::Error<'_>> {
49        executor::Executor::with_seed(attempt, config, self.seed)?
50            .shutdown(self.shutdown)
51            .map_err(create::Error::from)
52    }
53}