Skip to content

Commit 81df62d

Browse files
committed
Add static mutex lock for mutable error handler fn handle
Add tests directory with a function to check the function `register_error_handler`
1 parent 0e19170 commit 81df62d

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ build = "build.rs"
1515
libc = "0.1.10"
1616
num = "0.1.27"
1717
time = "0.1.32"
18+
lazy_static = "0.1.*"
1819

1920
[build-dependencies]
2021
rustc-serialize = "0.3.16"

src/error.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
12
use defines::AfError;
23
use std::error::Error;
4+
use std::sync::Mutex;
35

46
pub type ErrorCallback = Fn(AfError);
57

68
pub static mut HANDLE_ERROR: &'static ErrorCallback = &handle_error_general;
79

10+
lazy_static! {
11+
static ref HANDLE_ERROR_LOCK: Mutex<i32> = Mutex::new(0);
12+
}
13+
14+
#[allow(unused_must_use)]
815
pub fn register_error_handler(callback: &'static ErrorCallback) {
16+
HANDLE_ERROR_LOCK.lock().unwrap();
917
unsafe {
1018
HANDLE_ERROR = callback;
1119
}

src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
33
html_root_url = "http://arrayfire.com/docs/rust")]
44

5+
#[macro_use]
6+
extern crate lazy_static;
7+
58
pub use array::Array;
69
pub use array::{print};
710
mod array;
@@ -50,7 +53,7 @@ mod defines;
5053
pub use dim4::Dim4;
5154
mod dim4;
5255

53-
pub use error::{register_error_handler,handle_error_general};
56+
pub use error::{ErrorCallback, register_error_handler,handle_error_general};
5457
mod error;
5558

5659
pub use index::{Indexer, index, row, rows, col, cols, slice, slices,

tests/lib.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
extern crate arrayfire as af;
2+
3+
use std::error::Error;
4+
use std::thread;
5+
use std::time::Duration;
6+
use af::*;
7+
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+
}
15+
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+
}
22+
}
23+
24+
pub static HANDLE1: &'static ErrorCallback = &handler_sample1;
25+
pub static HANDLE2: &'static ErrorCallback = &handler_sample2;
26+
27+
#[test]
28+
fn check_error_handler_mutation() {
29+
30+
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);
36+
}
37+
});
38+
}
39+
40+
thread::sleep(Duration::from_millis(50));
41+
42+
}

0 commit comments

Comments
 (0)