11//@ignore-target-windows: No libc on Windows
2- use std:: ffi:: CStr ;
3- #[ cfg( not( target_os = "freebsd" ) ) ]
4- use std:: ffi:: CString ;
5- use std:: thread;
62
73fn main ( ) {
8- test_named_thread_truncation ( ) ;
9-
10- #[ cfg( not( target_os = "freebsd" ) ) ]
114 test_mutex_libc_init_recursive ( ) ;
12- #[ cfg( not( target_os = "freebsd" ) ) ]
135 test_mutex_libc_init_normal ( ) ;
14- #[ cfg( not( target_os = "freebsd" ) ) ]
156 test_mutex_libc_init_errorcheck ( ) ;
16- #[ cfg( not( target_os = "freebsd" ) ) ]
177 test_rwlock_libc_static_initializer ( ) ;
188
199 #[ cfg( target_os = "linux" ) ]
2010 test_mutex_libc_static_initializer_recursive ( ) ;
2111}
2212
23- #[ cfg( not( target_os = "freebsd" ) ) ]
2413fn test_mutex_libc_init_recursive ( ) {
2514 unsafe {
2615 let mut attr: libc:: pthread_mutexattr_t = std:: mem:: zeroed ( ) ;
@@ -45,7 +34,6 @@ fn test_mutex_libc_init_recursive() {
4534 }
4635}
4736
48- #[ cfg( not( target_os = "freebsd" ) ) ]
4937fn test_mutex_libc_init_normal ( ) {
5038 unsafe {
5139 let mut mutexattr: libc:: pthread_mutexattr_t = std:: mem:: zeroed ( ) ;
@@ -68,7 +56,6 @@ fn test_mutex_libc_init_normal() {
6856 }
6957}
7058
71- #[ cfg( not( target_os = "freebsd" ) ) ]
7259fn test_mutex_libc_init_errorcheck ( ) {
7360 unsafe {
7461 let mut mutexattr: libc:: pthread_mutexattr_t = std:: mem:: zeroed ( ) ;
@@ -114,7 +101,6 @@ fn test_mutex_libc_static_initializer_recursive() {
114101// Testing the behavior of std::sync::RwLock does not fully exercise the pthread rwlock shims, we
115102// need to go a layer deeper and test the behavior of the libc functions, because
116103// std::sys::unix::rwlock::RWLock itself keeps track of write_locked and num_readers.
117- #[ cfg( not( target_os = "freebsd" ) ) ]
118104fn test_rwlock_libc_static_initializer ( ) {
119105 let rw = std:: cell:: UnsafeCell :: new ( libc:: PTHREAD_RWLOCK_INITIALIZER ) ;
120106 unsafe {
@@ -139,49 +125,3 @@ fn test_rwlock_libc_static_initializer() {
139125 assert_eq ! ( libc:: pthread_rwlock_destroy( rw. get( ) ) , 0 ) ;
140126 }
141127}
142-
143- fn test_named_thread_truncation ( ) {
144- let long_name = std:: iter:: once ( "test_named_thread_truncation" )
145- . chain ( std:: iter:: repeat ( " yada" ) . take ( 100 ) )
146- . collect :: < String > ( ) ;
147-
148- fn set_thread_name ( name : & CStr ) -> i32 {
149- #[ cfg( target_os = "linux" ) ]
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
154- libc:: pthread_set_name_np ( libc:: pthread_self ( ) , name. as_ptr ( ) . cast ( ) ) ;
155- return 0 ;
156- } ;
157- #[ cfg( target_os = "macos" ) ]
158- return unsafe { libc:: pthread_setname_np ( name. as_ptr ( ) . cast ( ) ) } ;
159- }
160-
161- let result = thread:: Builder :: new ( ) . name ( long_name. clone ( ) ) . spawn ( move || {
162- // Rust remembers the full thread name itself.
163- assert_eq ! ( thread:: current( ) . name( ) , Some ( long_name. as_str( ) ) ) ;
164-
165- // But the system is limited -- make sure we successfully set a truncation.
166- let mut buf = vec ! [ 0u8 ; long_name. len( ) + 1 ] ;
167- #[ cfg( not( target_os = "freebsd" ) ) ]
168- unsafe {
169- libc:: pthread_getname_np ( libc:: pthread_self ( ) , buf. as_mut_ptr ( ) . cast ( ) , buf. len ( ) )
170- } ;
171- #[ cfg( target_os = "freebsd" ) ]
172- unsafe {
173- libc:: pthread_get_name_np ( libc:: pthread_self ( ) , buf. as_mut_ptr ( ) . cast ( ) , buf. len ( ) )
174- } ;
175- let cstr = CStr :: from_bytes_until_nul ( & buf) . unwrap ( ) ;
176- assert ! ( cstr. to_bytes( ) . len( ) >= 15 , "name is too short: len={}" , cstr. to_bytes( ) . len( ) ) ; // POSIX seems to promise at least 15 chars
177- assert ! ( long_name. as_bytes( ) . starts_with( cstr. to_bytes( ) ) ) ;
178-
179- // Also test directly calling pthread_setname to check its return value.
180- assert_eq ! ( set_thread_name( & cstr) , 0 ) ;
181- // But with a too long name it should fail (except on FreeBSD where the
182- // function has no return, hence cannot indicate failure).
183- #[ cfg( not( target_os = "freebsd" ) ) ]
184- assert_ne ! ( set_thread_name( & CString :: new( long_name) . unwrap( ) ) , 0 ) ;
185- } ) ;
186- result. unwrap ( ) . join ( ) . unwrap ( ) ;
187- }
0 commit comments