1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use {
crate::{
sync::{create_delivery, create_lock, Accepter, Deliverer, Trigger},
ConsumeFailure, Consumer, Producer,
},
core::convert::TryFrom,
fehler::{throw, throws},
std::{
any::Any,
panic::{catch_unwind, AssertUnwindSafe, RefUnwindSafe},
thread::spawn,
},
};
type Panic = Box<dyn Any + Send + 'static>;
#[derive(Debug, parse_display::Display)]
enum Status<S, E> {
#[display("{0}")]
Success(S),
#[display("ERROR: {0}")]
Error(E),
#[display("PANIC")]
Panic(Panic),
}
impl<S, E> Status<S, E> {
const fn is_success(&self) -> bool {
matches!(*self, Self::Success(_))
}
}
#[derive(Clone, Copy, Debug)]
pub enum Kind {
Single,
Cancelable,
}
#[derive(Debug)]
pub struct Thread<S, E> {
consumer: Accepter<Status<S, E>>,
trigger: Option<Trigger>,
name: String,
}
impl<S: Send + 'static, E: TryFrom<ConsumeFailure<E>> + Send + 'static> Thread<S, E> {
#[inline]
pub fn new<
P: Send + 'static,
F: FnMut(&mut P) -> Result<S, E> + RefUnwindSafe + Send + 'static,
>(
name: String,
kind: Kind,
mut parameters: P,
mut call: F,
) -> Self {
let (producer, consumer) = create_delivery::<Status<S, E>>();
match kind {
Kind::Single => {
let _ = spawn(move || {
Self::produce_outcome(Self::run(&mut parameters, &mut call), &producer);
});
Self {
name,
consumer,
trigger: None,
}
}
Kind::Cancelable => {
let (trigger, hammer) = create_lock();
let _ = spawn(move || {
let mut status = Self::run(&mut parameters, &mut call);
while hammer.consume().is_err() && status.is_success() {
status = Self::run(&mut parameters, &mut call);
}
Self::produce_outcome(status, &producer);
});
Self {
name,
consumer,
trigger: Some(trigger),
}
}
}
}
fn run<P, F: FnMut(&mut P) -> Result<S, E> + RefUnwindSafe + Send + 'static>(
mut parameters: &mut P,
call: &mut F,
) -> Status<S, E> {
match catch_unwind(AssertUnwindSafe(|| (call)(&mut parameters))) {
Ok(Ok(success)) => Status::Success(success),
Ok(Err(error)) => Status::Error(error),
Err(panic) => Status::Panic(panic),
}
}
fn produce_outcome(status: Status<S, E>, producer: &Deliverer<Status<S, E>>) {
#[allow(clippy::unwrap_used)]
producer.produce(status).unwrap();
}
#[inline]
pub fn cancel(&self) {
if let Some(trigger) = self.trigger.as_ref() {
#[allow(clippy::unwrap_used)]
trigger.produce(()).unwrap();
}
}
}
impl<S, E: TryFrom<ConsumeFailure<E>>> Consumer for Thread<S, E> {
type Good = S;
type Failure = ConsumeFailure<E>;
#[allow(clippy::panic_in_result_fn)]
#[inline]
#[throws(Self::Failure)]
fn consume(&self) -> Self::Good {
match self.consumer.consume() {
Ok(status) => match status {
Status::Success(success) => success,
Status::Error(error) => throw!(error),
Status::Panic(panic) => {
log::error!("Panic was caught in thread `{}`", self.name);
#[allow(clippy::panic)]
{
panic!(panic);
}
}
},
Err(_) => throw!(ConsumeFailure::EmptyStock),
}
}
}