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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use std::cell::RefCell;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::time::Duration;
use crossbeam_channel::Receiver;
use crossbeam_channel::RecvTimeoutError;
use crossbeam_channel::Select;
use crossbeam_channel::SelectedOperation;
use super::super::ErrorKind;
use super::super::Result;
type MapThreadFn<T> = Box<dyn FnMut() -> Result<T>>;
pub struct MapThread<T: Send + 'static> {
join: RefCell<Option<MapThreadFn<T>>>,
join_check: Receiver<()>,
shutdown: Arc<AtomicBool>,
}
impl<T: Send + 'static> MapThread<T> {
pub(crate) fn new<F>(
join: F,
join_check: Receiver<()>,
shutdown: Arc<AtomicBool>,
) -> MapThread<T>
where
F: FnMut() -> Result<T> + 'static,
{
let join: MapThreadFn<T> = Box::new(join);
let join = RefCell::new(Some(join));
MapThread {
join,
join_check,
shutdown,
}
}
pub fn join(&self) -> Result<T> {
let handle = self
.join
.try_borrow_mut()
.map_err(|_| ErrorKind::JoinedAlready)?
.take();
let mut handle = match handle {
None => return Err(ErrorKind::JoinedAlready.into()),
Some(handle) => handle,
};
handle()
}
pub fn join_timeout(&self, timeout: Duration) -> Result<T> {
match self.join_check.recv_timeout(timeout) {
Err(RecvTimeoutError::Timeout) => Err(ErrorKind::JoinTimeout.into()),
_ => self.join(),
}
}
pub fn request_shutdown(&self) {
self.shutdown.store(true, Ordering::Relaxed);
}
pub fn select_add<'a>(&'a self, select: &mut Select<'a>) -> usize {
select.recv(&self.join_check)
}
pub fn select_join(&self, operation: SelectedOperation) -> Result<T> {
let _ = operation.recv(&self.join_check);
self.join()
}
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use crossbeam_channel::Select;
use super::super::super::Builder;
#[test]
fn spawn_and_join() {
let flag: bool = Builder::new("spawn_and_join")
.spawn(|_| {})
.expect("failed to spawn thread")
.map(|_| true)
.join()
.expect("failed to join thread");
assert_eq!(true, flag);
}
#[test]
fn request_shutdown() {
let thread = Builder::new("request_shutdown")
.spawn(|scope| loop {
::std::thread::sleep(Duration::from_millis(10));
if scope.should_shutdown() {
break;
}
})
.expect("to spawn test thread")
.map(|_| true);
thread.request_shutdown();
let flag = thread.join().expect("the thread to stop");
assert_eq!(true, flag);
}
#[test]
fn select_interface() {
let thread = Builder::new("select_interface")
.spawn(|_| {
::std::thread::sleep(Duration::from_millis(10));
})
.expect("to spawn test thread")
.map(|_| true);
let mut set = Select::new();
let idx = thread.select_add(&mut set);
let op = set.select_timeout(Duration::from_millis(30)).unwrap();
thread.select_join(op).unwrap();
assert_eq!(0, idx);
}
#[test]
fn select_multiple_threads() {
let thread1 = Builder::new("select_multiple_threads_1")
.spawn(|_| {
::std::thread::sleep(Duration::from_millis(50));
})
.expect("to spawn test thread")
.map(|_| true);
let thread2 = Builder::new("select_multiple_threads_2")
.spawn(|_| {
::std::thread::sleep(Duration::from_millis(10));
})
.expect("to spawn test thread")
.map(|_| true);
let mut set = Select::new();
thread1.select_add(&mut set);
thread2.select_add(&mut set);
let op = set.select_timeout(Duration::from_millis(30)).unwrap();
let idx = op.index();
thread2.select_join(op).unwrap();
assert_eq!(1, idx);
}
#[test]
fn select_panic() {
let thread = Builder::new("select_panic")
.spawn(|_| {
::std::thread::sleep(Duration::from_millis(10));
panic!("this panic is expected");
})
.expect("to spawn test thread")
.map(|_| true);
let mut set = Select::new();
thread.select_add(&mut set);
let op = set.select_timeout(Duration::from_millis(30)).unwrap();
let idx = op.index();
let result = thread.select_join(op);
assert_eq!(0, idx);
assert_eq!(true, result.is_err());
}
#[test]
fn select_ready_interface() {
let thread = Builder::new("select_panic")
.spawn(|_| {
::std::thread::sleep(Duration::from_millis(10));
})
.expect("to spawn test thread")
.map(|_| true);
let mut set = Select::new();
thread.select_add(&mut set);
let idx = set.ready_timeout(Duration::from_millis(30)).unwrap();
assert_eq!(0, idx);
thread.join_timeout(Duration::from_millis(10)).unwrap();
}
}