From e5c5ed00a5e8ecf453dca2072d54e51316594a5e Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen@gmail.com>
Date: Sun, 31 Mar 2024 11:39:06 +0100
Subject: [PATCH] std::thread: adding get_name haiku implementation.

follow-up #123233
---
 library/std/src/sys/pal/unix/thread.rs | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/library/std/src/sys/pal/unix/thread.rs b/library/std/src/sys/pal/unix/thread.rs
index 01e3c3cbc6fde..77e31d802a38f 100644
--- a/library/std/src/sys/pal/unix/thread.rs
+++ b/library/std/src/sys/pal/unix/thread.rs
@@ -257,6 +257,23 @@ impl Thread {
         CString::new(name).ok()
     }
 
+    #[cfg(target_os = "haiku")]
+    pub fn get_name() -> Option<CString> {
+        unsafe {
+            let mut tinfo = mem::MaybeUninit::<libc::thread_info>::uninit();
+            // See BeOS teams group and threads api.
+            // https://www.haiku-os.org/legacy-docs/bebook/TheKernelKit_ThreadsAndTeams_Overview.html
+            let thread_self = libc::find_thread(ptr::null_mut());
+            let res = libc::get_thread_info(thread_self, tinfo.as_mut_ptr());
+            if res != libc::B_OK {
+                return None;
+            }
+            let info = tinfo.assume_init();
+            let name = slice::from_raw_parts(info.name.as_ptr() as *const u8, info.name.len());
+            CStr::from_bytes_until_nul(name).map(CStr::to_owned).ok()
+        }
+    }
+
     #[cfg(not(any(
         target_os = "linux",
         target_os = "freebsd",
@@ -264,7 +281,8 @@ impl Thread {
         target_os = "macos",
         target_os = "ios",
         target_os = "tvos",
-        target_os = "watchos"
+        target_os = "watchos",
+        target_os = "haiku"
     )))]
     pub fn get_name() -> Option<CString> {
         None