@@ -129,22 +129,21 @@ impl Thread {
129129 }
130130 }
131131
132- #[ cfg( target_os = "linux" ) ]
133- pub fn set_name ( name : & CStr ) {
134- const TASK_COMM_LEN : usize = 16 ;
135-
136- unsafe {
137- // Available since glibc 2.12, musl 1.1.16, and uClibc 1.0.20.
138- let name = truncate_cstr :: < { TASK_COMM_LEN } > ( name) ;
139- let res = libc:: pthread_setname_np ( libc:: pthread_self ( ) , name. as_ptr ( ) ) ;
140- // We have no good way of propagating errors here, but in debug-builds let's check that this actually worked.
141- debug_assert_eq ! ( res, 0 ) ;
142- }
143- }
144-
145- #[ cfg( any( target_os = "freebsd" , target_os = "dragonfly" ) ) ]
132+ #[ cfg( any( target_os = "linux" , target_os = "freebsd" , target_os = "dragonfly" ) ) ]
146133 pub fn set_name ( name : & CStr ) {
147134 unsafe {
135+ let name = {
136+ cfg_if:: cfg_if! {
137+ if #[ cfg( target_os = "linux" ) ] {
138+ const TASK_COMM_LEN : usize = 16 ;
139+ & truncate_cstr:: <{ TASK_COMM_LEN } >( name)
140+ } else {
141+ name. to_bytes( )
142+ }
143+ }
144+ } ;
145+ // Available since glibc 2.12, musl 1.1.16, and uClibc 1.0.20 for Linux,
146+ // FreeBSD 12.2 and 13.0, and DragonFlyBSD 6.0.
148147 let res = libc:: pthread_setname_np ( libc:: pthread_self ( ) , name. as_ptr ( ) ) ;
149148 // We have no good way of propagating errors here, but in debug-builds let's check that this actually worked.
150149 debug_assert_eq ! ( res, 0 ) ;
0 commit comments