forked from arrayfire/arrayfire-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_handler.rs
50 lines (45 loc) · 1.56 KB
/
error_handler.rs
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
use std::thread;
use ::arrayfire::*;
macro_rules! implement_handler {
($fn_name:ident) => {
pub fn $fn_name(error_code: AfError) {
match error_code {
AfError::SUCCESS => {} /* No-op */
_ => panic!("Error message: {}", error_code),
}
}
};
}
implement_handler!(handler_sample1);
implement_handler!(handler_sample2);
implement_handler!(handler_sample3);
implement_handler!(handler_sample4);
#[allow(unused_must_use)]
#[test]
fn check_error_handler_mutation() {
let children = (0..4)
.map(|i| {
thread::Builder::new()
.name(format!("child {}", i + 1).to_string())
.spawn(move || {
let target_device = i % arrayfire::device_count();
println!(
"Thread {:?} 's target device is {}",
thread::current(),
target_device
);
match i {
0 => register_error_handler(Callback::new(handler_sample1)),
1 => register_error_handler(Callback::new(handler_sample2)),
2 => register_error_handler(Callback::new(handler_sample3)),
3 => register_error_handler(Callback::new(handler_sample4)),
_ => panic!("Impossible scenario"),
}
})
.expect("Failed to launch a thread")
})
.collect::<Vec<_>>();
for c in children {
c.join();
}
}