From 28501cd80fe15152dd9d4104a65f1b1260b53d13 Mon Sep 17 00:00:00 2001 From: Alik Aslanyan Date: Mon, 26 Apr 2021 18:37:08 +0400 Subject: [PATCH 1/2] Implement setting thread name for Fuchsia --- library/std/src/sys/unix/thread.rs | 34 ++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/library/std/src/sys/unix/thread.rs b/library/std/src/sys/unix/thread.rs index b8f43caec32..0cdea58ca9f 100644 --- a/library/std/src/sys/unix/thread.rs +++ b/library/std/src/sys/unix/thread.rs @@ -13,6 +13,23 @@ pub const DEFAULT_MIN_STACK_SIZE: usize = 1024 * 1024; #[cfg(target_os = "vxworks")] pub const DEFAULT_MIN_STACK_SIZE: usize = 256 * 1024; +#[cfg(target_os = "fuchsia")] +mod zircon { + type zx_handle_t = u32; + type zx_status_t = i32; + pub const ZX_PROP_NAME: u32 = 3; + + extern "C" { + pub fn zx_object_set_property( + handle: zx_handle_t, + property: u32, + value: *const libc::c_void, + value_size: libc::size_t, + ) -> zx_status_t; + pub fn zx_thread_self() -> zx_handle_t; + } +} + pub struct Thread { id: libc::pthread_t, } @@ -131,6 +148,19 @@ impl Thread { } } + #[cfg(target_os = "fuchsia")] + pub fn set_name(name: &CStr) { + use self::zircon::*; + unsafe { + zx_object_set_property( + zx_thread_self(), + ZX_PROP_NAME, + name.as_ptr() as *const libc::c_void, + libc::strlen(name.as_ptr()), + ); + } + } + #[cfg(any( target_env = "newlib", target_os = "haiku", @@ -142,10 +172,6 @@ impl Thread { pub fn set_name(_name: &CStr) { // Newlib, Haiku, Emscripten, and VxWorks have no way to set a thread name. } - #[cfg(target_os = "fuchsia")] - pub fn set_name(_name: &CStr) { - // FIXME: determine whether Fuchsia has a way to set a thread name. - } pub fn sleep(dur: Duration) { let mut secs = dur.as_secs(); From 2ac0b3ed54b3ddfb2f6abfd7f0c1cbd6a924fc1c Mon Sep 17 00:00:00 2001 From: Alik Aslanyan Date: Tue, 27 Apr 2021 18:44:37 +0000 Subject: [PATCH 2/2] Update library/std/src/sys/unix/thread.rs Co-authored-by: Joshua Nelson --- library/std/src/sys/unix/thread.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/sys/unix/thread.rs b/library/std/src/sys/unix/thread.rs index 0cdea58ca9f..40a634924f2 100644 --- a/library/std/src/sys/unix/thread.rs +++ b/library/std/src/sys/unix/thread.rs @@ -156,7 +156,7 @@ impl Thread { zx_thread_self(), ZX_PROP_NAME, name.as_ptr() as *const libc::c_void, - libc::strlen(name.as_ptr()), + name.to_bytes().len(), ); } }