11//@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 ;
35use std:: thread;
46
57fn main ( ) {
8+ #[ cfg( not( target_os = "freebsd" ) ) ]
69 test_mutex_libc_init_recursive ( ) ;
10+ #[ cfg( not( target_os = "freebsd" ) ) ]
711 test_mutex_libc_init_normal ( ) ;
12+ #[ cfg( not( target_os = "freebsd" ) ) ]
813 test_mutex_libc_init_errorcheck ( ) ;
14+ #[ cfg( not( target_os = "freebsd" ) ) ]
915 test_rwlock_libc_static_initializer ( ) ;
16+
1017 test_named_thread_truncation ( ) ;
1118
1219 #[ cfg( target_os = "linux" ) ]
1320 test_mutex_libc_static_initializer_recursive ( ) ;
1421}
1522
23+ #[ cfg( not( target_os = "freebsd" ) ) ]
1624fn test_mutex_libc_init_recursive ( ) {
1725 unsafe {
1826 let mut attr: libc:: pthread_mutexattr_t = std:: mem:: zeroed ( ) ;
@@ -37,6 +45,7 @@ fn test_mutex_libc_init_recursive() {
3745 }
3846}
3947
48+ #[ cfg( not( target_os = "freebsd" ) ) ]
4049fn test_mutex_libc_init_normal ( ) {
4150 unsafe {
4251 let mut mutexattr: libc:: pthread_mutexattr_t = std:: mem:: zeroed ( ) ;
@@ -59,6 +68,7 @@ fn test_mutex_libc_init_normal() {
5968 }
6069}
6170
71+ #[ cfg( not( target_os = "freebsd" ) ) ]
6272fn test_mutex_libc_init_errorcheck ( ) {
6373 unsafe {
6474 let mut mutexattr: libc:: pthread_mutexattr_t = std:: mem:: zeroed ( ) ;
@@ -104,6 +114,7 @@ fn test_mutex_libc_static_initializer_recursive() {
104114// Testing the behavior of std::sync::RwLock does not fully exercise the pthread rwlock shims, we
105115// need to go a layer deeper and test the behavior of the libc functions, because
106116// std::sys::unix::rwlock::RWLock itself keeps track of write_locked and num_readers.
117+ #[ cfg( not( target_os = "freebsd" ) ) ]
107118fn test_rwlock_libc_static_initializer ( ) {
108119 let rw = std:: cell:: UnsafeCell :: new ( libc:: PTHREAD_RWLOCK_INITIALIZER ) ;
109120 unsafe {
@@ -137,6 +148,14 @@ fn test_named_thread_truncation() {
137148 fn set_thread_name ( name : & CStr ) -> i32 {
138149 #[ cfg( target_os = "linux" ) ]
139150 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 ;
140159 #[ cfg( target_os = "macos" ) ]
141160 return unsafe { libc:: pthread_setname_np ( name. as_ptr ( ) . cast ( ) ) } ;
142161 }
@@ -147,16 +166,25 @@ fn test_named_thread_truncation() {
147166
148167 // But the system is limited -- make sure we successfully set a truncation.
149168 let mut buf = vec ! [ 0u8 ; long_name. len( ) + 1 ] ;
169+ #[ cfg( not( target_os = "freebsd" ) ) ]
150170 unsafe {
151171 libc:: pthread_getname_np ( libc:: pthread_self ( ) , buf. as_mut_ptr ( ) . cast ( ) , buf. len ( ) )
152172 } ;
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+ } ;
153179 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
155181 assert ! ( long_name. as_bytes( ) . starts_with( cstr. to_bytes( ) ) ) ;
156182
157183 // Also test directly calling pthread_setname to check its return value.
158184 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" ) ) ]
160188 assert_ne ! ( set_thread_name( & CString :: new( long_name) . unwrap( ) ) , 0 ) ;
161189 } ) ;
162190 result. unwrap ( ) . join ( ) . unwrap ( ) ;
0 commit comments