dope_runtime/process/failure/
execute.rs1use dope_core::{
3 dope_sys::thread::signal::Signal,
4 platform::{affinity, signal, thread},
5};
6use o3::alloc;
7
8use crate::process::failure::{bus, config};
9
10#[derive(Debug)]
12pub enum Cause<S> {
13 Config(config::Error),
14 Allocation(alloc::Error),
15 Signal(signal::Acquire),
16 Entropy(dope_core::Errno),
17 WorkerBus {
18 cpu: affinity::Cpu,
19 source: bus::Create,
20 },
21 BusRead {
22 cpu: affinity::Cpu,
23 source: bus::Read,
24 },
25 BusState(bus::State),
26 WorkerFailed {
27 cpu: affinity::Cpu,
28 },
29 WorkerMissing {
30 cpu: affinity::Cpu,
31 },
32 WorkerSpawn {
33 cpu: affinity::Cpu,
34 source: thread::Error,
35 },
36 Poll(dope_core::Errno),
37 TerminationWait(<signal::Source<'static> as Signal>::Error),
38 WatchReady(S),
39 WatchPulse(S),
40 WatchDrain(S),
41}
42
43impl<S> Cause<S> {
44 pub(in crate::process) fn received(
45 result: Result<bus::Event, bus::Read>,
46 cpu: affinity::Cpu,
47 ) -> Result<bus::Event, Self> {
48 match result {
49 Ok(bus::Event::Failed) => Err(Self::WorkerFailed { cpu }),
50 Ok(event) => Ok(event),
51 Err(source) => Err(Self::BusRead { cpu, source }),
52 }
53 }
54}
55
56#[derive(Debug)]
58pub enum Bus {
59 Write {
60 source: bus::Fault,
61 handoff: Option<bus::Read>,
62 },
63 Read(bus::Read),
64}
65
66#[derive(Debug)]
68pub enum Worker<W> {
69 Failed {
70 cpu: affinity::Cpu,
71 handoff: Option<bus::Read>,
72 },
73 Affinity {
74 cpu: affinity::Cpu,
75 source: affinity::Error,
76 bus: bus::Report,
77 handoff: Option<bus::Read>,
78 },
79 Entry {
80 cpu: affinity::Cpu,
81 source: W,
82 bus: bus::Report,
83 handoff: Option<bus::Read>,
84 },
85 Bus {
86 cpu: affinity::Cpu,
87 source: Bus,
88 },
89}
90
91#[derive(Debug)]
93pub enum Fault<'r, S, W> {
94 Control(Cause<S>),
95 Stop(dope_core::Errno),
96 Release(signal::Release<'r>),
97 Worker(Worker<W>),
98}
99
100#[must_use = "every process failure must be consumed"]
102#[derive(Debug)]
103pub struct Error<'r, S, W> {
104 control: Option<Cause<S>>,
105 stop: Option<dope_core::Errno>,
106 release: Option<signal::Release<'r>>,
107 workers: Vec<Worker<W>>,
108}
109
110impl<'r, S, W> Error<'r, S, W> {
111 pub(in crate::process) fn failed(
112 control: Cause<S>,
113 stop: Option<dope_core::Errno>,
114 workers: Vec<Worker<W>>,
115 ) -> Self {
116 Self {
117 control: Some(control),
118 stop,
119 release: None,
120 workers,
121 }
122 }
123
124 pub(in crate::process) fn finish(
125 stop: Option<dope_core::Errno>,
126 workers: Vec<Worker<W>>,
127 ) -> Result<(), Self> {
128 if stop.is_none() && workers.is_empty() {
129 return Ok(());
130 }
131 Err(Self {
132 control: None,
133 stop,
134 release: None,
135 workers,
136 })
137 }
138
139 pub fn fold<R>(self, mut state: R, mut apply: impl FnMut(R, Fault<'r, S, W>) -> R) -> R {
140 if let Some(cause) = self.control {
141 state = apply(state, Fault::Control(cause));
142 }
143 if let Some(source) = self.stop {
144 state = apply(state, Fault::Stop(source));
145 }
146 let state = self
147 .workers
148 .into_iter()
149 .fold(state, |state, failure| apply(state, Fault::Worker(failure)));
150 match self.release {
151 Some(cause) => apply(state, Fault::Release(cause)),
152 None => state,
153 }
154 }
155}
156
157impl<'r, S, W> From<Cause<S>> for Error<'r, S, W> {
158 fn from(cause: Cause<S>) -> Self {
159 Self::failed(cause, None, Vec::new())
160 }
161}
162
163impl<S> From<alloc::Error> for Cause<S> {
164 fn from(error: alloc::Error) -> Self {
165 Self::Allocation(error)
166 }
167}
168
169impl<'r, S, W> From<signal::Error<'r, Error<'r, S, W>>> for Error<'r, S, W> {
170 fn from(error: signal::Error<'r, Error<'r, S, W>>) -> Self {
171 match error {
172 signal::Error::Acquire(error) => Self::failed(Cause::Signal(error), None, Vec::new()),
173 signal::Error::Run(error) => error,
174 signal::Error::Release { run, cause } => {
175 let mut error = run.unwrap_or(Self {
176 control: None,
177 stop: None,
178 release: None,
179 workers: Vec::new(),
180 });
181 error.release = Some(cause);
182 error
183 }
184 }
185 }
186}