Skip to content

Commit

Permalink
linux: Use pthread_setname_np instead of prctl
Browse files Browse the repository at this point in the history
This function is available on Linux since glibc 2.12, musl 1.1.16, and
uClibc 1.0.20. The main advantage over `prctl` is that it properly
represents the pointer argument, rather than a multi-purpose `long`,
so we're better representing strict provenance (rust-lang#95496).
  • Loading branch information
cuviper committed Aug 8, 2022
1 parent f03ce30 commit 013986b
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions library/std/src/sys/unix/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,9 @@ impl Thread {
debug_assert_eq!(ret, 0);
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(target_os = "android")]
pub fn set_name(name: &CStr) {
const PR_SET_NAME: libc::c_int = 15;
// pthread wrapper only appeared in glibc 2.12, so we use syscall
// directly.
unsafe {
libc::prctl(
PR_SET_NAME,
Expand All @@ -132,6 +130,14 @@ impl Thread {
}
}

#[cfg(target_os = "linux")]
pub fn set_name(name: &CStr) {
unsafe {
// Available since glibc 2.12, musl 1.1.16, and uClibc 1.0.20.
libc::pthread_setname_np(libc::pthread_self(), name.as_ptr());
}
}

#[cfg(any(target_os = "freebsd", target_os = "dragonfly", target_os = "openbsd"))]
pub fn set_name(name: &CStr) {
unsafe {
Expand Down

0 comments on commit 013986b

Please sign in to comment.