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        process::Terminal {
33            source: self.shutdown.source,
34        }
35    }
36
37    pub(super) fn gated(self) -> (Self, gate::Closed<'run>) {
38        let gate = self.shutdown.gate();
39        (self, gate)
40    }
41}
42
43impl<'run> executor::Build for Context<'run> {
44    type Shutdown = process::Shutdown<'run>;
45
46    fn try_build(
47        self,
48        attempt: &mut creation::Attempt,
49        config: settings::Config,
50    ) -> Result<executor::Executor<(), Self::Shutdown>, create::Error<'_>> {
51        executor::Executor::with_seed(attempt, config, self.seed)?
52            .shutdown(self.shutdown)
53            .map_err(create::Error::from)
54    }
55}