Skip to content

Commit de29546

Browse files
committed
Add and rearrange mutex tests
1 parent 735fc12 commit de29546

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

tests/run-pass/sync.rs

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ use std::sync::{Mutex, RwLock, TryLockError};
55
extern crate libc;
66

77
fn main() {
8-
test_mutex();
8+
test_mutex_stdlib();
99
#[cfg(not(target_os = "windows"))] // TODO: implement RwLock on Windows
1010
{
11-
test_mutex_libc_recursive();
11+
test_mutex_libc_init_recursive();
12+
test_mutex_libc_init_normal();
13+
test_mutex_libc_static_initializer_recursive();
1214
test_rwlock_stdlib();
13-
test_mutex_libc_init();
1415
test_rwlock_libc_static_initializer();
1516
}
1617
}
1718

18-
fn test_mutex() {
19+
fn test_mutex_stdlib() {
1920
let m = Mutex::new(0);
2021
{
2122
let _guard = m.lock();
@@ -26,7 +27,7 @@ fn test_mutex() {
2627
}
2728

2829
#[cfg(not(target_os = "windows"))]
29-
fn test_mutex_libc_recursive() {
30+
fn test_mutex_libc_init_recursive() {
3031
unsafe {
3132
let mut attr: libc::pthread_mutexattr_t = std::mem::zeroed();
3233
assert_eq!(libc::pthread_mutexattr_init(&mut attr as *mut _), 0);
@@ -47,6 +48,39 @@ fn test_mutex_libc_recursive() {
4748
}
4849
}
4950

51+
#[cfg(not(target_os = "windows"))]
52+
fn test_mutex_libc_init_normal() {
53+
unsafe {
54+
let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed();
55+
assert_eq!(libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, libc::PTHREAD_MUTEX_NORMAL), 0);
56+
let mut mutex: libc::pthread_mutex_t = std::mem::zeroed();
57+
assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0);
58+
assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
59+
assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), libc::EBUSY);
60+
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
61+
assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), 0);
62+
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
63+
assert_eq!(libc::pthread_mutex_destroy(&mut mutex as *mut _), 0);
64+
}
65+
}
66+
67+
#[cfg(not(target_os = "windows"))]
68+
fn test_mutex_libc_static_initializer_recursive() {
69+
let mutex = std::cell::UnsafeCell::new(libc::PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
70+
unsafe {
71+
assert_eq!(libc::pthread_mutex_lock(mutex.get()), 0);
72+
assert_eq!(libc::pthread_mutex_trylock(mutex.get()), 0);
73+
assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
74+
assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
75+
assert_eq!(libc::pthread_mutex_trylock(mutex.get()), 0);
76+
assert_eq!(libc::pthread_mutex_lock(mutex.get()), 0);
77+
assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
78+
assert_eq!(libc::pthread_mutex_unlock(mutex.get()), 0);
79+
assert_eq!(libc::pthread_mutex_unlock(mutex.get()), libc::EPERM);
80+
assert_eq!(libc::pthread_mutex_destroy(mutex.get()), 0);
81+
}
82+
}
83+
5084
#[cfg(not(target_os = "windows"))]
5185
fn test_rwlock_stdlib() {
5286
let rw = RwLock::new(0);
@@ -67,20 +101,6 @@ fn test_rwlock_stdlib() {
67101
// need to go a layer deeper and test the behavior of libc functions, because
68102
// std::sys::unix::rwlock::RWLock keeps track of write_locked and num_readers
69103

70-
#[cfg(not(target_os = "windows"))]
71-
fn test_mutex_libc_init() {
72-
unsafe {
73-
let mut mutex: libc::pthread_mutex_t = std::mem::zeroed();
74-
assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, std::ptr::null_mut()), 0);
75-
assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
76-
assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), libc::EBUSY);
77-
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
78-
assert_eq!(libc::pthread_mutex_trylock(&mut mutex as *mut _), 0);
79-
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
80-
assert_eq!(libc::pthread_mutex_destroy(&mut mutex as *mut _), 0);
81-
}
82-
}
83-
84104
#[cfg(not(target_os = "windows"))]
85105
fn test_rwlock_libc_static_initializer() {
86106
let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER);

0 commit comments

Comments
 (0)