-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for prctl handling thread names
- Loading branch information
1 parent
894c264
commit 4dc0032
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//@ignore-target: windows # No pthreads on Windows | ||
Check failure on line 1 in tests/pass-dep/libc/prctl.rs GitHub Actions / build (ubuntu-latest, x86_64-unknown-linux-gnu)test got exit status: 1
Check failure on line 1 in tests/pass-dep/libc/prctl.rs GitHub Actions / build (ubuntu-latest, x86_64-unknown-linux-gnu)test generated output
Check failure on line 1 in tests/pass-dep/libc/prctl.rs GitHub Actions / build (macos-14, aarch64-apple-darwin)test got exit status: 1
|
||
use std::ffi::CStr; | ||
#[cfg(not(target_os = "freebsd"))] | ||
use std::ffi::CString; | ||
use std::thread; | ||
|
||
fn main() { | ||
let long_name = std::iter::once("test_named_thread_truncation") | ||
.chain(std::iter::repeat(" yada").take(100)) | ||
.collect::<String>(); | ||
|
||
fn set_thread_name(name: &CStr) -> i32 { | ||
cfg_if::cfg_if! { | ||
if #[cfg(any(target_os = "linux", target_os = "illumos", target_os = "solaris", target_os = "freebsd"))] { | ||
unsafe { libc::pthread_setname_np(libc::pthread_self(), name.as_ptr().cast()) } | ||
} else { | ||
compile_error!("set_thread_name not supported for this OS") | ||
} | ||
} | ||
} | ||
|
||
fn get_thread_name(name: &mut [u8]) -> i32 { | ||
cfg_if::cfg_if! { | ||
if #[cfg(any( | ||
target_os = "linux", | ||
target_os = "illumos", | ||
target_os = "solaris", | ||
target_os = "freebsd", | ||
))] { | ||
unsafe { | ||
libc::pthread_getname_np(libc::pthread_self(), name.as_mut_ptr().cast(), name.len()) | ||
} | ||
} else { | ||
compile_error!("get_thread_name not supported for this OS") | ||
} | ||
} | ||
} | ||
|
||
let result = thread::Builder::new().name(long_name.clone()).spawn(move || { | ||
// Rust remembers the full thread name itself. | ||
assert_eq!(thread::current().name(), Some(long_name.as_str())); | ||
|
||
// But the system is limited -- make sure we successfully set a truncation. | ||
let mut buf = vec![0u8; long_name.len() + 1]; | ||
assert_eq!(get_thread_name(&mut buf), 0); | ||
let cstr = CStr::from_bytes_until_nul(&buf).unwrap(); | ||
assert!(cstr.to_bytes().len() >= 15, "name is too short: len={}", cstr.to_bytes().len()); // POSIX seems to promise at least 15 chars | ||
assert!(long_name.as_bytes().starts_with(cstr.to_bytes())); | ||
|
||
// Also test directly calling pthread_setname to check its return value. | ||
assert_eq!(set_thread_name(&cstr), 0); | ||
// But with a too long name it should fail. | ||
assert_ne!(set_thread_name(&CString::new(long_name).unwrap()), 0); | ||
}); | ||
result.unwrap().join().unwrap(); | ||
} |