Skip to content

Commit 3c01bf5

Browse files
committed
rustup + attempt to fix test racyness
1 parent 97a989d commit 3c01bf5

File tree

3 files changed

+41
-26
lines changed

3 files changed

+41
-26
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ description = "An API to POSIX IPC primitives"
77
repository = "http://github.com/codius/rust-posix-ipc"
88
license = "MIT"
99
readme = "README.md"
10+
11+
[dependencies]
12+
enum_primitive = "0.0.2"
13+
num = "0.1.24"
14+
libc = "0.1.7"

src/lib.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
#![feature(trace_macros)]
1+
#![feature(trace_macros,alloc)]
2+
#[macro_use] extern crate enum_primitive;
23
pub mod signals {
34
extern crate libc;
4-
use std::os;
5+
use std::io;
56
use std::mem;
67

7-
#[derive(Hash, Eq, PartialEq, Copy, Debug, FromPrimitive)]
8+
enum_from_primitive! {
9+
#[derive(Hash, Eq, PartialEq, Copy, Clone, Debug)]
810
pub enum Signal {
911
None = 0,
1012
Hup,
@@ -39,25 +41,26 @@ pub mod signals {
3941
Pwr,
4042
Sys
4143
}
44+
}
4245

4346
impl Signal {
44-
pub fn raise(self) -> Result<(), usize> {
47+
pub fn raise(self) -> Result<(), libc::c_int> {
4548
match unsafe { raise(self as libc::c_int) } {
4649
0 => Result::Ok(()),
47-
_ => Result::Err(os::errno())
50+
_ => Result::Err(io::Error::last_os_error().raw_os_error().unwrap())
4851
}
4952
}
5053

51-
pub fn kill(self, pid: libc::pid_t) -> Result<(), usize> {
54+
pub fn kill(self, pid: libc::pid_t) -> Result<(), libc::c_int> {
5255
match unsafe { kill(pid, self as libc::c_int) } {
5356
0 => Result::Ok(()),
54-
_ => Result::Err(os::errno())
57+
_ => Result::Err(io::Error::last_os_error().raw_os_error().unwrap())
5558
}
5659
}
5760

58-
pub unsafe fn handle(self, handler: Box<FnMut(Signal)>) -> Result<(), usize> {
59-
match unsafe { signal (self as libc::c_int, mem::transmute(glue::rust_signal_handler)) } {
60-
-1 => Result::Err(os::errno()),
61+
pub unsafe fn handle(self, handler: Box<FnMut(Signal)>) -> Result<(), libc::c_int> {
62+
match signal (self as libc::c_int, mem::transmute(glue::rust_signal_handler)) {
63+
-1 => Result::Err(io::Error::last_os_error().raw_os_error().unwrap()),
6164
_ => { glue::set_handler(self, handler); Result::Ok(()) }
6265
}
6366
}
@@ -66,14 +69,12 @@ pub mod signals {
6669
mod glue {
6770
extern crate libc;
6871
extern crate alloc;
72+
extern crate num;
73+
use self::num::FromPrimitive;
6974
use super::Signal;
70-
use std::num::FromPrimitive;
71-
use self::alloc::arc::Arc;
72-
use std::rc::Rc;
7375
use std::mem;
74-
use std::ptr;
7576

76-
#[derive(Copy,Debug)]
77+
#[derive(Copy, Clone, Debug)]
7778
struct FnPtr {
7879
foo: usize,
7980
bar: usize
@@ -104,13 +105,11 @@ pub mod signals {
104105
handlers[sig as usize] = mem::transmute(f);
105106
}
106107

107-
fn null_handler(s: Signal) {}
108-
109108
pub unsafe extern "C" fn rust_signal_handler(sig: libc::c_int) {
110109
let f: *mut FnMut(Signal) = mem::transmute(handlers[sig as usize]);
111110
let p: FnPtr = mem::transmute(f);
112111
if p.foo != 0 && p.bar != 0 {
113-
match FromPrimitive::from_i32(sig) {
112+
match Signal::from_i32(sig) {
114113
Some(s) => (*f)(s),
115114
None => panic!("Unknown signal {}", sig)
116115
}

tests/tests.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
1-
extern crate "posix-ipc" as ipc;
1+
#![feature(std_misc)]
2+
extern crate posix_ipc as ipc;
23

34
use ipc::signals;
5+
use std::thread;
6+
use std::sync::{Arc, Semaphore, Mutex};
47

58
#[test]
69
fn raise_and_catch_with_closure() {
7-
let mut caught = false;
10+
let mut caught: Arc<Mutex<bool>> = Arc::new(Mutex::new(false));
11+
let mut sem: Arc<Semaphore> = Arc::new(Semaphore::new(1));
12+
sem.acquire();
813
{
9-
let f = |Signal| {caught = true;println!("Caught!")};
14+
let sem = sem.clone();
15+
let caught = caught.clone();
16+
let f = move |s: signals::Signal| {
17+
*caught.lock().unwrap() = true; sem.release()
18+
};
1019
unsafe {
11-
signals::Signal::Usr1.handle(Box::new(f));
20+
signals::Signal::Usr1.handle(Box::new(f)).unwrap();
1221
}
1322
}
14-
signals::Signal::Usr1.raise();
15-
assert!(unsafe { caught });
23+
signals::Signal::Usr1.raise().unwrap();
24+
sem.acquire();
25+
assert!(*caught.lock().unwrap());
1626
}
1727

1828
#[test]
@@ -21,9 +31,10 @@ fn raise_and_catch_with_func() {
2131
{
2232
fn f(s: signals::Signal) {unsafe { caught = true }}
2333
unsafe {
24-
signals::Signal::Usr1.handle(Box::new(f));
34+
signals::Signal::Usr1.handle(Box::new(f)).unwrap();
2535
}
2636
}
27-
signals::Signal::Usr1.raise();
37+
signals::Signal::Usr1.raise().unwrap();
38+
thread::sleep_ms(500); // this is really racy :(
2839
assert!(unsafe { caught });
2940
}

0 commit comments

Comments
 (0)