|
| 1 | +#[macro_use] |
1 | 2 | extern crate arrayfire as af; |
2 | 3 |
|
3 | 4 | use std::error::Error; |
4 | 5 | use std::thread; |
5 | 6 | use std::time::Duration; |
6 | 7 | use af::*; |
7 | 8 |
|
8 | | -pub fn handler_sample1(error_code: AfError) { |
9 | | - println!("Error handler sample1"); |
10 | | - match error_code { |
11 | | - AfError::SUCCESS => {}, /* No-op */ |
12 | | - _ => panic!("Error message: {}", error_code.description()), |
13 | | - } |
14 | | -} |
| 9 | +macro_rules! implement_handler { |
| 10 | + ($fn_name:ident, $msg: expr) => ( |
15 | 11 |
|
16 | | -pub fn handler_sample2(error_code: AfError) { |
17 | | - println!("Error handler sample2"); |
18 | | - match error_code { |
19 | | - AfError::SUCCESS => {}, /* No-op */ |
20 | | - _ => panic!("Error message: {}", error_code.description()), |
21 | | - } |
| 12 | + pub fn $fn_name(error_code: AfError) { |
| 13 | + println!("{:?}", $msg); |
| 14 | + match error_code { |
| 15 | + AfError::SUCCESS => {}, /* No-op */ |
| 16 | + _ => panic!("Error message: {}", error_code.description()), |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + ) |
22 | 21 | } |
23 | 22 |
|
| 23 | +implement_handler!(handler_sample1, "Error Handler Sample1"); |
| 24 | +implement_handler!(handler_sample2, "Error Handler Sample2"); |
| 25 | +implement_handler!(handler_sample3, "Error Handler Sample3"); |
| 26 | +implement_handler!(handler_sample4, "Error Handler Sample4"); |
| 27 | + |
24 | 28 | pub static HANDLE1: &'static ErrorCallback = &handler_sample1; |
25 | 29 | pub static HANDLE2: &'static ErrorCallback = &handler_sample2; |
| 30 | +pub static HANDLE3: &'static ErrorCallback = &handler_sample3; |
| 31 | +pub static HANDLE4: &'static ErrorCallback = &handler_sample4; |
26 | 32 |
|
| 33 | +#[allow(unused_must_use)] |
27 | 34 | #[test] |
28 | 35 | fn check_error_handler_mutation() { |
29 | 36 |
|
30 | 37 | for i in 0..4 { |
31 | | - thread::spawn(move || { |
32 | | - if i%2==0 { |
33 | | - register_error_handler(HANDLE2); |
34 | | - } else { |
35 | | - register_error_handler(HANDLE1); |
| 38 | + thread::Builder::new().name(format!("child {}",i+1).to_string()).spawn(move || { |
| 39 | + println!("{:?}", thread::current()); |
| 40 | + match i { |
| 41 | + 0 => register_error_handler(HANDLE1.clone()), |
| 42 | + 1 => register_error_handler(HANDLE2.clone()), |
| 43 | + 2 => register_error_handler(HANDLE3.clone()), |
| 44 | + 3 => register_error_handler(HANDLE4.clone()), |
| 45 | + _ => panic!("Impossible scenario"), |
36 | 46 | } |
| 47 | + //match i { |
| 48 | + // 1 => thread::sleep(Duration::from_millis(20)), |
| 49 | + // 2 => thread::sleep(Duration::from_millis(10)), |
| 50 | + // _ => (), |
| 51 | + //} |
37 | 52 | }); |
38 | 53 | } |
39 | 54 |
|
| 55 | + af::info(); |
40 | 56 | thread::sleep(Duration::from_millis(50)); |
41 | 57 |
|
42 | 58 | } |
0 commit comments