@@ -5,17 +5,18 @@ use std::sync::{Mutex, RwLock, TryLockError};
5
5
extern crate libc;
6
6
7
7
fn main ( ) {
8
- test_mutex ( ) ;
8
+ test_mutex_stdlib ( ) ;
9
9
#[ cfg( not( target_os = "windows" ) ) ] // TODO: implement RwLock on Windows
10
10
{
11
- test_mutex_libc_recursive ( ) ;
11
+ test_mutex_libc_init_recursive ( ) ;
12
+ test_mutex_libc_init_normal ( ) ;
13
+ test_mutex_libc_static_initializer_recursive ( ) ;
12
14
test_rwlock_stdlib ( ) ;
13
- test_mutex_libc_init ( ) ;
14
15
test_rwlock_libc_static_initializer ( ) ;
15
16
}
16
17
}
17
18
18
- fn test_mutex ( ) {
19
+ fn test_mutex_stdlib ( ) {
19
20
let m = Mutex :: new ( 0 ) ;
20
21
{
21
22
let _guard = m. lock ( ) ;
@@ -26,7 +27,7 @@ fn test_mutex() {
26
27
}
27
28
28
29
#[ cfg( not( target_os = "windows" ) ) ]
29
- fn test_mutex_libc_recursive ( ) {
30
+ fn test_mutex_libc_init_recursive ( ) {
30
31
unsafe {
31
32
let mut attr: libc:: pthread_mutexattr_t = std:: mem:: zeroed ( ) ;
32
33
assert_eq ! ( libc:: pthread_mutexattr_init( & mut attr as * mut _) , 0 ) ;
@@ -47,6 +48,39 @@ fn test_mutex_libc_recursive() {
47
48
}
48
49
}
49
50
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
+
50
84
#[ cfg( not( target_os = "windows" ) ) ]
51
85
fn test_rwlock_stdlib ( ) {
52
86
let rw = RwLock :: new ( 0 ) ;
@@ -67,20 +101,6 @@ fn test_rwlock_stdlib() {
67
101
// need to go a layer deeper and test the behavior of libc functions, because
68
102
// std::sys::unix::rwlock::RWLock keeps track of write_locked and num_readers
69
103
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
-
84
104
#[ cfg( not( target_os = "windows" ) ) ]
85
105
fn test_rwlock_libc_static_initializer ( ) {
86
106
let rw = std:: cell:: UnsafeCell :: new ( libc:: PTHREAD_RWLOCK_INITIALIZER ) ;
0 commit comments