1
1
//@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;
6
2
7
3
fn main ( ) {
8
- test_named_thread_truncation ( ) ;
9
-
10
- #[ cfg( not( target_os = "freebsd" ) ) ]
11
4
test_mutex_libc_init_recursive ( ) ;
12
- #[ cfg( not( target_os = "freebsd" ) ) ]
13
5
test_mutex_libc_init_normal ( ) ;
14
- #[ cfg( not( target_os = "freebsd" ) ) ]
15
6
test_mutex_libc_init_errorcheck ( ) ;
16
- #[ cfg( not( target_os = "freebsd" ) ) ]
17
7
test_rwlock_libc_static_initializer ( ) ;
18
8
19
9
#[ cfg( target_os = "linux" ) ]
20
10
test_mutex_libc_static_initializer_recursive ( ) ;
21
11
}
22
12
23
- #[ cfg( not( target_os = "freebsd" ) ) ]
24
13
fn test_mutex_libc_init_recursive ( ) {
25
14
unsafe {
26
15
let mut attr: libc:: pthread_mutexattr_t = std:: mem:: zeroed ( ) ;
@@ -45,7 +34,6 @@ fn test_mutex_libc_init_recursive() {
45
34
}
46
35
}
47
36
48
- #[ cfg( not( target_os = "freebsd" ) ) ]
49
37
fn test_mutex_libc_init_normal ( ) {
50
38
unsafe {
51
39
let mut mutexattr: libc:: pthread_mutexattr_t = std:: mem:: zeroed ( ) ;
@@ -68,7 +56,6 @@ fn test_mutex_libc_init_normal() {
68
56
}
69
57
}
70
58
71
- #[ cfg( not( target_os = "freebsd" ) ) ]
72
59
fn test_mutex_libc_init_errorcheck ( ) {
73
60
unsafe {
74
61
let mut mutexattr: libc:: pthread_mutexattr_t = std:: mem:: zeroed ( ) ;
@@ -114,7 +101,6 @@ fn test_mutex_libc_static_initializer_recursive() {
114
101
// Testing the behavior of std::sync::RwLock does not fully exercise the pthread rwlock shims, we
115
102
// need to go a layer deeper and test the behavior of the libc functions, because
116
103
// std::sys::unix::rwlock::RWLock itself keeps track of write_locked and num_readers.
117
- #[ cfg( not( target_os = "freebsd" ) ) ]
118
104
fn test_rwlock_libc_static_initializer ( ) {
119
105
let rw = std:: cell:: UnsafeCell :: new ( libc:: PTHREAD_RWLOCK_INITIALIZER ) ;
120
106
unsafe {
@@ -139,49 +125,3 @@ fn test_rwlock_libc_static_initializer() {
139
125
assert_eq ! ( libc:: pthread_rwlock_destroy( rw. get( ) ) , 0 ) ;
140
126
}
141
127
}
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