@@ -3,6 +3,7 @@ use std::ffi::CStr;
3
3
use std:: thread;
4
4
5
5
fn main ( ) {
6
+ let short_name = "test_named" . to_owned ( ) ;
6
7
let long_name = std:: iter:: once ( "test_named_thread_truncation" )
7
8
. chain ( std:: iter:: repeat ( " yada" ) . take ( 100 ) )
8
9
. collect :: < String > ( ) ;
@@ -54,24 +55,37 @@ fn main() {
54
55
}
55
56
}
56
57
57
- let result = thread :: Builder :: new ( ) . name ( long_name . clone ( ) ) . spawn ( move || {
58
- // Rust remembers the full thread name itself.
59
- assert_eq ! ( thread:: current( ) . name( ) , Some ( long_name . as_str( ) ) ) ;
58
+ fn test_using ( name : String ) {
59
+ let result = thread:: Builder :: new ( ) . name ( name . clone ( ) ) . spawn ( move || {
60
+ assert_eq ! ( thread:: current( ) . name( ) , Some ( name . as_str( ) ) ) ;
60
61
61
- // But the system is limited -- make sure we successfully set a truncation.
62
- let mut buf = vec ! [ 0u8 ; long_name. len( ) + 1 ] ;
63
- assert_eq ! ( get_thread_name( & mut buf) , 0 ) ;
64
- let cstr = CStr :: from_bytes_until_nul ( & buf) . unwrap ( ) ;
65
- assert ! ( cstr. to_bytes( ) . len( ) >= 15 , "name is too short: len={}" , cstr. to_bytes( ) . len( ) ) ; // POSIX seems to promise at least 15 chars
66
- assert ! ( long_name. as_bytes( ) . starts_with( cstr. to_bytes( ) ) ) ;
62
+ let mut buf = vec ! [ 0u8 ; name. len( ) + 1 ] ;
63
+ assert_eq ! ( get_thread_name( & mut buf) , 0 ) ;
64
+ let cstr = CStr :: from_bytes_until_nul ( & buf) . unwrap ( ) ;
65
+ if name. len ( ) >= 15 {
66
+ assert ! (
67
+ cstr. to_bytes( ) . len( ) >= 15 ,
68
+ "name is too short: len={}" ,
69
+ cstr. to_bytes( ) . len( )
70
+ ) ; // POSIX seems to promise at least 15 chars
71
+ assert ! ( name. as_bytes( ) . starts_with( cstr. to_bytes( ) ) ) ;
72
+ } else {
73
+ assert_eq ! ( name. as_bytes( ) , cstr. to_bytes( ) ) ;
74
+ }
75
+
76
+ // Also test directly calling pthread_setname to check its return value.
77
+ assert_eq ! ( set_thread_name( & cstr) , 0 ) ;
78
+ // But with a too long name it should fail except:
79
+ // * on FreeBSD where the function has no return, hence cannot indicate failure,
80
+ // * on Android where prctl silently truncates the string.
81
+ #[ cfg( not( any( target_os = "freebsd" , target_os = "android" ) ) ) ]
82
+ assert_ne ! ( set_thread_name( & std:: ffi:: CString :: new( name) . unwrap( ) ) , 0 ) ;
83
+ } ) ;
84
+ result. unwrap ( ) . join ( ) . unwrap ( ) ;
85
+ }
67
86
68
- // Also test directly calling pthread_setname to check its return value.
69
- assert_eq ! ( set_thread_name( & cstr) , 0 ) ;
70
- // But with a too long name it should fail except:
71
- // * on FreeBSD where the function has no return, hence cannot indicate failure,
72
- // * on Android where prctl silently truncates the string.
73
- #[ cfg( not( any( target_os = "freebsd" , target_os = "android" ) ) ) ]
74
- assert_ne ! ( set_thread_name( & std:: ffi:: CString :: new( long_name) . unwrap( ) ) , 0 ) ;
75
- } ) ;
76
- result. unwrap ( ) . join ( ) . unwrap ( ) ;
87
+ test_using ( short_name) ;
88
+ // Rust remembers the full thread name itself.
89
+ // But the system is limited -- make sure we successfully set a truncation.
90
+ test_using ( long_name) ;
77
91
}
0 commit comments