@@ -129,25 +129,28 @@ impl Thread {
129
129
}
130
130
}
131
131
132
- #[ cfg( target_os = "linux" ) ]
132
+ #[ cfg( any ( target_os = "linux" , target_os = "freebsd" , target_os = "dragonfly" ) ) ]
133
133
pub fn set_name ( name : & CStr ) {
134
- const TASK_COMM_LEN : usize = 16 ;
135
-
136
134
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) ;
135
+ cfg_if:: cfg_if! {
136
+ if #[ cfg( target_os = "linux" ) ] {
137
+ // Linux limits the allowed length of the name.
138
+ const TASK_COMM_LEN : usize = 16 ;
139
+ let name = & truncate_cstr:: <{ TASK_COMM_LEN } >( name) ;
140
+ } else {
141
+ // FreeBSD and DragonFly BSD do not enforce length limits.
142
+ let name = name. as_ptr( ) ;
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 DragonFly BSD 6.0.
139
147
let res = libc:: pthread_setname_np ( libc:: pthread_self ( ) , name. as_ptr ( ) ) ;
140
148
// We have no good way of propagating errors here, but in debug-builds let's check that this actually worked.
141
149
debug_assert_eq ! ( res, 0 ) ;
142
150
}
143
151
}
144
152
145
- #[ cfg( any(
146
- target_os = "freebsd" ,
147
- target_os = "dragonfly" ,
148
- target_os = "openbsd" ,
149
- target_os = "nuttx"
150
- ) ) ]
153
+ #[ cfg( any( target_os = "openbsd" , target_os = "nuttx" ) ) ]
151
154
pub fn set_name ( name : & CStr ) {
152
155
unsafe {
153
156
libc:: pthread_set_name_np ( libc:: pthread_self ( ) , name. as_ptr ( ) ) ;
0 commit comments