use dope_core::executor::{self, create};
use dope_core::{
driver::{
life::{creation, domain},
settings,
},
platform::{affinity, affinity::Binding},
random,
};
use crate::{
process::{self, gate, runtime::bus},
};
pub struct Context<'run> {
affinity: Binding,
seed: random::Seed,
shutdown: process::Shutdown<'run>,
}
impl<'run> Context<'run> {
pub(super) const fn new(bind: Binding, seed: random::Seed, tx: bus::Tx<'run>) -> Self {
Self {
affinity: bind,
seed,
shutdown: process::Shutdown::new(tx),
}
}
pub fn cpu(&self) -> affinity::Cpu {
self.affinity.cpu()
}
pub fn terminal(&self) -> process::Terminal<'run> {
self.shutdown.terminal()
}
pub(super) fn gated(self) -> (Self, gate::Closed<'run>) {
let gate = self.shutdown.gate();
(self, gate)
}
}
impl<'run> executor::Build for Context<'run> {
type Shutdown = process::Shutdown<'run>;
fn try_build(
self,
attempt: &mut creation::Attempt,
config: settings::Config,
) -> Result<domain::Generation<Self::Shutdown>, create::Error<'_>> {
self.shutdown.attach(
attempt
.try_seed(config, self.seed)
.map(domain::Generation::new)
.map_err(create::Error::from),
)
}
}