From 05d49498192a2b447bec06c7e2b787f1e981f172 Mon Sep 17 00:00:00 2001 From: Florian Engelhardt Date: Fri, 8 Nov 2024 10:18:59 +0100 Subject: [PATCH] fix nts thread names --- profiling/src/profiling/thread_utils.rs | 43 +++++++++++---------- profiling/tests/correctness/exceptions.json | 4 +- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/profiling/src/profiling/thread_utils.rs b/profiling/src/profiling/thread_utils.rs index bce572dac8..e8f9acbb6b 100644 --- a/profiling/src/profiling/thread_utils.rs +++ b/profiling/src/profiling/thread_utils.rs @@ -73,25 +73,28 @@ pub fn join_timeout(handle: JoinHandle<()>, timeout: Duration, impact: &str) { } pub fn get_current_thread_name() -> String { - let mut name = [0u8; 32]; - - let result = unsafe { - libc::pthread_getname_np( - libc::pthread_self(), - name.as_mut_ptr() as *mut c_char, - name.len(), - ) - }; - let mut thread_name = SAPI.to_string(); - if result == 0 { - // If successful, convert the result to a Rust String - let cstr = unsafe { std::ffi::CStr::from_ptr(name.as_ptr() as *const c_char) }; - let str_slice: &str = cstr.to_str().unwrap(); - if !str_slice.is_empty() { - thread_name.push_str(": "); - thread_name.push_str(str_slice); + #[cfg(php_zts)] + { + let mut name = [0u8; 32]; + + let result = unsafe { + libc::pthread_getname_np( + libc::pthread_self(), + name.as_mut_ptr() as *mut c_char, + name.len(), + ) + }; + + if result == 0 { + // If successful, convert the result to a Rust String + let cstr = unsafe { std::ffi::CStr::from_ptr(name.as_ptr() as *const c_char) }; + let str_slice: &str = cstr.to_str().unwrap(); + if !str_slice.is_empty() { + thread_name.push_str(": "); + thread_name.push_str(str_slice); + } } } @@ -110,7 +113,7 @@ mod tests { libc::pthread_setname_np( #[cfg(target_os = "linux")] libc::pthread_self(), - b"\0".as_ptr() as *const c_char + b"\0".as_ptr() as *const c_char, ); } assert_eq!(get_current_thread_name(), "unknown"); @@ -119,7 +122,7 @@ mod tests { libc::pthread_setname_np( #[cfg(target_os = "linux")] libc::pthread_self(), - b"foo\0".as_ptr() as *const c_char + b"foo\0".as_ptr() as *const c_char, ); } assert_eq!(get_current_thread_name(), "unknown: foo"); @@ -128,7 +131,7 @@ mod tests { libc::pthread_setname_np( #[cfg(target_os = "linux")] libc::pthread_self(), - b"bar\0".as_ptr() as *const c_char + b"bar\0".as_ptr() as *const c_char, ); } assert_eq!(get_current_thread_name(), "unknown: bar"); diff --git a/profiling/tests/correctness/exceptions.json b/profiling/tests/correctness/exceptions.json index 937b9fbbfc..bbf4a0a765 100644 --- a/profiling/tests/correctness/exceptions.json +++ b/profiling/tests/correctness/exceptions.json @@ -21,7 +21,9 @@ }, { "key": "thread name", - "values_regex": "^cli.*$" + "values": [ + "cli" + ] } ] }