make pthread-threadname nicer with cfg-if
This commit is contained in:
parent
75921d2ca4
commit
cb445d0188
3 changed files with 39 additions and 18 deletions
|
|
@ -172,6 +172,7 @@ dependencies = [
|
|||
name = "miri-test-deps"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"getrandom 0.1.16",
|
||||
"getrandom 0.2.15",
|
||||
"libc",
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ edition = "2021"
|
|||
# all dependencies (and their transitive ones) listed here can be used in `tests/`.
|
||||
libc = "0.2"
|
||||
num_cpus = "1.10.1"
|
||||
cfg-if = "1"
|
||||
|
||||
getrandom_01 = { package = "getrandom", version = "0.1" }
|
||||
getrandom_02 = { package = "getrandom", version = "0.2", features = ["js"] }
|
||||
|
|
|
|||
|
|
@ -10,16 +10,42 @@ fn main() {
|
|||
.collect::<String>();
|
||||
|
||||
fn set_thread_name(name: &CStr) -> i32 {
|
||||
#[cfg(any(target_os = "linux", target_os = "illumos", target_os = "solaris"))]
|
||||
return unsafe { libc::pthread_setname_np(libc::pthread_self(), name.as_ptr().cast()) };
|
||||
#[cfg(target_os = "freebsd")]
|
||||
unsafe {
|
||||
// pthread_set_name_np does not return anything
|
||||
libc::pthread_set_name_np(libc::pthread_self(), name.as_ptr().cast());
|
||||
return 0;
|
||||
};
|
||||
#[cfg(target_os = "macos")]
|
||||
return unsafe { libc::pthread_setname_np(name.as_ptr().cast()) };
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(any(target_os = "linux", target_os = "illumos", target_os = "solaris"))] {
|
||||
unsafe { libc::pthread_setname_np(libc::pthread_self(), name.as_ptr().cast()) }
|
||||
} else if #[cfg(target_os = "freebsd")] {
|
||||
// pthread_set_name_np does not return anything
|
||||
unsafe { libc::pthread_set_name_np(libc::pthread_self(), name.as_ptr().cast()) };
|
||||
0
|
||||
} else if #[cfg(target_os = "macos")] {
|
||||
unsafe { libc::pthread_setname_np(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 = "macos"
|
||||
))] {
|
||||
unsafe {
|
||||
libc::pthread_getname_np(libc::pthread_self(), name.as_mut_ptr().cast(), name.len())
|
||||
}
|
||||
} else if #[cfg(target_os = "freebsd")] {
|
||||
// pthread_get_name_np does not return anything
|
||||
unsafe {
|
||||
libc::pthread_get_name_np(libc::pthread_self(), name.as_mut_ptr().cast(), name.len())
|
||||
};
|
||||
0
|
||||
} else {
|
||||
compile_error!("get_thread_name not supported for this OS")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let result = thread::Builder::new().name(long_name.clone()).spawn(move || {
|
||||
|
|
@ -28,14 +54,7 @@ fn main() {
|
|||
|
||||
// But the system is limited -- make sure we successfully set a truncation.
|
||||
let mut buf = vec![0u8; long_name.len() + 1];
|
||||
#[cfg(not(target_os = "freebsd"))]
|
||||
unsafe {
|
||||
libc::pthread_getname_np(libc::pthread_self(), buf.as_mut_ptr().cast(), buf.len())
|
||||
};
|
||||
#[cfg(target_os = "freebsd")]
|
||||
unsafe {
|
||||
libc::pthread_get_name_np(libc::pthread_self(), buf.as_mut_ptr().cast(), buf.len())
|
||||
};
|
||||
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()));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue