Skip to content

Commit 3dc2ff1

Browse files
authored
Rollup merge of rust-lang#132607 - YohDeadfall:pthread-name-fn-with-result, r=tgross35
Used pthread name functions returning result for FreeBSD and DragonFly `pthread_getname_np` and `pthread_setname_np` received a wider adoption in past years and was added to: * FreeBSD by June 11 2020 via [`2ef84b7da9a6c3e23b4a135e6e863581f16d46e1`](freebsd/freebsd-src@2ef84b7), * DargonFly by March 8 2021 via [`ab5dc9aceb34419d1c4b6006739e61acee8ee999`](https://gitweb.dragonflybsd.org/dragonfly.git/commitdiff/ab5dc9aceb34419d1c4b6006739e61acee8ee999). There's not so much advantage except that the result can be checked in debug builds. Ideally it should be unified with Linux' implementation, but it trims the input.
2 parents a66278f + aebc703 commit 3dc2ff1

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

Diff for: library/std/src/sys/pal/unix/thread.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -130,25 +130,28 @@ impl Thread {
130130
}
131131
}
132132

133-
#[cfg(target_os = "linux")]
133+
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly"))]
134134
pub fn set_name(name: &CStr) {
135-
const TASK_COMM_LEN: usize = 16;
136-
137135
unsafe {
138-
// Available since glibc 2.12, musl 1.1.16, and uClibc 1.0.20.
139-
let name = truncate_cstr::<{ TASK_COMM_LEN }>(name);
136+
cfg_if::cfg_if! {
137+
if #[cfg(target_os = "linux")] {
138+
// Linux limits the allowed length of the name.
139+
const TASK_COMM_LEN: usize = 16;
140+
let name = &truncate_cstr::<{ TASK_COMM_LEN }>(name);
141+
} else {
142+
// FreeBSD and DragonFly BSD do not enforce length limits.
143+
let name = name.to_bytes();
144+
}
145+
};
146+
// Available since glibc 2.12, musl 1.1.16, and uClibc 1.0.20 for Linux,
147+
// FreeBSD 12.2 and 13.0, and DragonFly BSD 6.0.
140148
let res = libc::pthread_setname_np(libc::pthread_self(), name.as_ptr());
141149
// We have no good way of propagating errors here, but in debug-builds let's check that this actually worked.
142150
debug_assert_eq!(res, 0);
143151
}
144152
}
145153

146-
#[cfg(any(
147-
target_os = "freebsd",
148-
target_os = "dragonfly",
149-
target_os = "openbsd",
150-
target_os = "nuttx"
151-
))]
154+
#[cfg(any(target_os = "openbsd", target_os = "nuttx"))]
152155
pub fn set_name(name: &CStr) {
153156
unsafe {
154157
libc::pthread_set_name_np(libc::pthread_self(), name.as_ptr());

0 commit comments

Comments
 (0)