Skip to content

Commit d5d5a56

Browse files
committed
Add tests
1 parent e794441 commit d5d5a56

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// ignore-windows: No libc on Windows
2+
3+
#![feature(rustc_private)]
4+
5+
extern crate libc;
6+
7+
fn main() {
8+
unsafe {
9+
let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed();
10+
assert_eq!(libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, libc::PTHREAD_MUTEX_NORMAL), 0);
11+
let mut mutex: libc::pthread_mutex_t = std::mem::zeroed();
12+
assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0);
13+
assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
14+
libc::pthread_mutex_lock(&mut mutex as *mut _); //~ ERROR deadlock
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// ignore-windows: No libc on Windows
2+
3+
#![feature(rustc_private)]
4+
5+
extern crate libc;
6+
7+
fn main() {
8+
let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER);
9+
unsafe {
10+
assert_eq!(libc::pthread_rwlock_rdlock(rw.get()), 0);
11+
libc::pthread_rwlock_wrlock(rw.get()); //~ ERROR: deadlock
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// ignore-windows: No libc on Windows
2+
3+
#![feature(rustc_private)]
4+
5+
extern crate libc;
6+
7+
fn main() {
8+
let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER);
9+
unsafe {
10+
assert_eq!(libc::pthread_rwlock_wrlock(rw.get()), 0);
11+
libc::pthread_rwlock_rdlock(rw.get()); //~ ERROR: deadlock
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// ignore-windows: No libc on Windows
2+
3+
#![feature(rustc_private)]
4+
5+
extern crate libc;
6+
7+
fn main() {
8+
let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER);
9+
unsafe {
10+
assert_eq!(libc::pthread_rwlock_wrlock(rw.get()), 0);
11+
libc::pthread_rwlock_wrlock(rw.get()); //~ ERROR: deadlock
12+
}
13+
}

tests/run-pass/libc.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@ fn test_mutex_libc_init_normal() {
7878
}
7979
}
8080

81+
fn test_mutex_libc_init_errorcheck() {
82+
unsafe {
83+
let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed();
84+
assert_eq!(libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, libc::PTHREAD_MUTEX_ERRORCHECK), 0);
85+
let mut mutex: libc::pthread_mutex_t = std::mem::zeroed();
86+
assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0);
87+
assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
88+
assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), libc::EBUSY);
89+
assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), libc::EDEADLK);
90+
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
91+
assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), 0);
92+
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
93+
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), libc::EPERM);
94+
assert_eq!(libc::pthread_mutex_destroy(&mut mutex as *mut _), 0);
95+
}
96+
}
97+
8198
// Only linux provides PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
8299
// libc for macOS just has the default PTHREAD_MUTEX_INITIALIZER.
83100
#[cfg(target_os = "linux")]
@@ -126,6 +143,7 @@ fn main() {
126143

127144
test_mutex_libc_init_recursive();
128145
test_mutex_libc_init_normal();
146+
test_mutex_libc_init_errorcheck();
129147
test_rwlock_libc_static_initializer();
130148

131149
#[cfg(target_os = "linux")]

0 commit comments

Comments
 (0)