1
1
//@ignore-target-windows: No libc on Windows
2
- use std:: ffi:: { CStr , CString } ;
2
+ use std:: ffi:: CStr ;
3
+ #[ cfg( not( target_os = "freebsd" ) ) ]
4
+ use std:: ffi:: CString ;
3
5
use std:: thread;
4
6
5
7
fn main ( ) {
8
+ #[ cfg( not( target_os = "freebsd" ) ) ]
6
9
test_mutex_libc_init_recursive ( ) ;
10
+ #[ cfg( not( target_os = "freebsd" ) ) ]
7
11
test_mutex_libc_init_normal ( ) ;
12
+ #[ cfg( not( target_os = "freebsd" ) ) ]
8
13
test_mutex_libc_init_errorcheck ( ) ;
14
+ #[ cfg( not( target_os = "freebsd" ) ) ]
9
15
test_rwlock_libc_static_initializer ( ) ;
16
+
10
17
test_named_thread_truncation ( ) ;
11
18
12
19
#[ cfg( target_os = "linux" ) ]
13
20
test_mutex_libc_static_initializer_recursive ( ) ;
14
21
}
15
22
23
+ #[ cfg( not( target_os = "freebsd" ) ) ]
16
24
fn test_mutex_libc_init_recursive ( ) {
17
25
unsafe {
18
26
let mut attr: libc:: pthread_mutexattr_t = std:: mem:: zeroed ( ) ;
@@ -37,6 +45,7 @@ fn test_mutex_libc_init_recursive() {
37
45
}
38
46
}
39
47
48
+ #[ cfg( not( target_os = "freebsd" ) ) ]
40
49
fn test_mutex_libc_init_normal ( ) {
41
50
unsafe {
42
51
let mut mutexattr: libc:: pthread_mutexattr_t = std:: mem:: zeroed ( ) ;
@@ -59,6 +68,7 @@ fn test_mutex_libc_init_normal() {
59
68
}
60
69
}
61
70
71
+ #[ cfg( not( target_os = "freebsd" ) ) ]
62
72
fn test_mutex_libc_init_errorcheck ( ) {
63
73
unsafe {
64
74
let mut mutexattr: libc:: pthread_mutexattr_t = std:: mem:: zeroed ( ) ;
@@ -104,6 +114,7 @@ fn test_mutex_libc_static_initializer_recursive() {
104
114
// Testing the behavior of std::sync::RwLock does not fully exercise the pthread rwlock shims, we
105
115
// need to go a layer deeper and test the behavior of the libc functions, because
106
116
// std::sys::unix::rwlock::RWLock itself keeps track of write_locked and num_readers.
117
+ #[ cfg( not( target_os = "freebsd" ) ) ]
107
118
fn test_rwlock_libc_static_initializer ( ) {
108
119
let rw = std:: cell:: UnsafeCell :: new ( libc:: PTHREAD_RWLOCK_INITIALIZER ) ;
109
120
unsafe {
@@ -137,6 +148,14 @@ fn test_named_thread_truncation() {
137
148
fn set_thread_name ( name : & CStr ) -> i32 {
138
149
#[ cfg( target_os = "linux" ) ]
139
150
return unsafe { libc:: pthread_setname_np ( libc:: pthread_self ( ) , name. as_ptr ( ) . cast ( ) ) } ;
151
+ #[ cfg( target_os = "freebsd" ) ]
152
+ unsafe {
153
+ // pthread_set_name_np does not return anything only it can fail if the
154
+ // thread id is invalid
155
+ libc:: pthread_set_name_np ( libc:: pthread_self ( ) , name. as_ptr ( ) . cast ( ) )
156
+ } ;
157
+ #[ cfg( target_os = "freebsd" ) ]
158
+ return 0 ;
140
159
#[ cfg( target_os = "macos" ) ]
141
160
return unsafe { libc:: pthread_setname_np ( name. as_ptr ( ) . cast ( ) ) } ;
142
161
}
@@ -147,16 +166,25 @@ fn test_named_thread_truncation() {
147
166
148
167
// But the system is limited -- make sure we successfully set a truncation.
149
168
let mut buf = vec ! [ 0u8 ; long_name. len( ) + 1 ] ;
169
+ #[ cfg( not( target_os = "freebsd" ) ) ]
150
170
unsafe {
151
171
libc:: pthread_getname_np ( libc:: pthread_self ( ) , buf. as_mut_ptr ( ) . cast ( ) , buf. len ( ) )
152
172
} ;
173
+ #[ cfg( target_os = "freebsd" ) ]
174
+ unsafe {
175
+ // pthread_get_name_np does not return anything only it can fail if the
176
+ // thread id is invalid
177
+ libc:: pthread_get_name_np ( libc:: pthread_self ( ) , buf. as_mut_ptr ( ) . cast ( ) , buf. len ( ) )
178
+ } ;
153
179
let cstr = CStr :: from_bytes_until_nul ( & buf) . unwrap ( ) ;
154
- assert ! ( cstr. to_bytes( ) . len( ) >= 15 ) ; // POSIX seems to promise at least 15 chars
180
+ assert ! ( cstr. to_bytes( ) . len( ) >= 15 , "name is too short: len={}" , cstr . to_bytes ( ) . len ( ) ) ; // POSIX seems to promise at least 15 chars
155
181
assert ! ( long_name. as_bytes( ) . starts_with( cstr. to_bytes( ) ) ) ;
156
182
157
183
// Also test directly calling pthread_setname to check its return value.
158
184
assert_eq ! ( set_thread_name( & cstr) , 0 ) ;
159
- // But with a too long name it should fail.
185
+ // But with a too long name it should fail (except on FreeBSD where the
186
+ // function has no return, hence cannot indicate failure).
187
+ #[ cfg( not( target_os = "freebsd" ) ) ]
160
188
assert_ne ! ( set_thread_name( & CString :: new( long_name) . unwrap( ) ) , 0 ) ;
161
189
} ) ;
162
190
result. unwrap ( ) . join ( ) . unwrap ( ) ;
0 commit comments