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
|
use std::any::Any;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread::sleep;
use std::time::Duration;
use crossbeam_utils::thread;
const THREADS: usize = 10;
const SMALL_STACK_SIZE: usize = 20;
#[test]
fn join() {
let counter = AtomicUsize::new(0);
thread::scope(|scope| {
let handle = scope.spawn(|_| {
counter.store(1, Ordering::Relaxed);
});
assert!(handle.join().is_ok());
let panic_handle = scope.spawn(|_| {
panic!("\"My honey is running out!\", said Pooh.");
});
assert!(panic_handle.join().is_err());
})
.unwrap();
// There should be sufficient synchronization.
assert_eq!(1, counter.load(Ordering::Relaxed));
}
#[test]
fn counter() {
let counter = AtomicUsize::new(0);
thread::scope(|scope| {
for _ in 0..THREADS {
scope.spawn(|_| {
counter.fetch_add(1, Ordering::Relaxed);
});
}
})
.unwrap();
assert_eq!(THREADS, counter.load(Ordering::Relaxed));
}
#[test]
fn counter_builder() {
let counter = AtomicUsize::new(0);
thread::scope(|scope| {
for i in 0..THREADS {
scope
.builder()
.name(format!("child-{}", i))
.stack_size(SMALL_STACK_SIZE)
.spawn(|_| {
counter.fetch_add(1, Ordering::Relaxed);
})
.unwrap();
}
})
.unwrap();
assert_eq!(THREADS, counter.load(Ordering::Relaxed));
}
#[test]
fn counter_panic() {
let counter = AtomicUsize::new(0);
let result = thread::scope(|scope| {
scope.spawn(|_| {
panic!("\"My honey is running out!\", said Pooh.");
});
sleep(Duration::from_millis(100));
for _ in 0..THREADS {
scope.spawn(|_| {
counter.fetch_add(1, Ordering::Relaxed);
});
}
});
assert_eq!(THREADS, counter.load(Ordering::Relaxed));
assert!(result.is_err());
}
#[test]
fn panic_twice() {
let result = thread::scope(|scope| {
scope.spawn(|_| {
sleep(Duration::from_millis(500));
panic!("thread #1");
});
scope.spawn(|_| {
panic!("thread #2");
});
});
let err = result.unwrap_err();
let vec = err
.downcast_ref::<Vec<Box<dyn Any + Send + 'static>>>()
.unwrap();
assert_eq!(2, vec.len());
let first = vec[0].downcast_ref::<&str>().unwrap();
let second = vec[1].downcast_ref::<&str>().unwrap();
assert_eq!("thread #1", *first);
assert_eq!("thread #2", *second)
}
#[test]
fn panic_many() {
let result = thread::scope(|scope| {
scope.spawn(|_| panic!("deliberate panic #1"));
scope.spawn(|_| panic!("deliberate panic #2"));
scope.spawn(|_| panic!("deliberate panic #3"));
});
let err = result.unwrap_err();
let vec = err
.downcast_ref::<Vec<Box<dyn Any + Send + 'static>>>()
.unwrap();
assert_eq!(3, vec.len());
for panic in vec.iter() {
let panic = panic.downcast_ref::<&str>().unwrap();
assert!(
*panic == "deliberate panic #1"
|| *panic == "deliberate panic #2"
|| *panic == "deliberate panic #3"
);
}
}
#[test]
fn nesting() {
let var = "foo".to_string();
struct Wrapper<'a> {
var: &'a String,
}
impl<'a> Wrapper<'a> {
fn recurse(&'a self, scope: &thread::Scope<'a>, depth: usize) {
assert_eq!(self.var, "foo");
if depth > 0 {
scope.spawn(move |scope| {
self.recurse(scope, depth - 1);
});
}
}
}
let wrapper = Wrapper { var: &var };
thread::scope(|scope| {
scope.spawn(|scope| {
scope.spawn(|scope| {
wrapper.recurse(scope, 5);
});
});
})
.unwrap();
}
#[test]
fn join_nested() {
thread::scope(|scope| {
scope.spawn(|scope| {
let handle = scope.spawn(|_| 7);
sleep(Duration::from_millis(200));
handle.join().unwrap();
});
sleep(Duration::from_millis(100));
})
.unwrap();
}
#[test]
fn scope_returns_ok() {
let result = thread::scope(|scope| scope.spawn(|_| 1234).join().unwrap()).unwrap();
assert_eq!(result, 1234);
}
#[cfg(unix)]
#[test]
fn as_pthread_t() {
use std::os::unix::thread::JoinHandleExt;
thread::scope(|scope| {
let handle = scope.spawn(|_scope| {
sleep(Duration::from_millis(100));
42
});
let _pthread_t = handle.as_pthread_t();
handle.join().unwrap();
})
.unwrap();
}
#[cfg(windows)]
#[test]
fn as_raw_handle() {
use std::os::windows::io::AsRawHandle;
thread::scope(|scope| {
let handle = scope.spawn(|_scope| {
sleep(Duration::from_millis(100));
42
});
let _raw_handle = handle.as_raw_handle();
handle.join().unwrap();
})
.unwrap();
}
|