From 297c5329c36eada6a48148c1b72f9a42ef320454 Mon Sep 17 00:00:00 2001 From: Valentyn Kit Date: Mon, 3 Aug 2026 12:09:02 +0300 Subject: [PATCH] Add pthread_gettid_np for Android and glibc Returns the Linux TID for a given thread, unlike gettid, which only returns the id of the calling thread. This is useful for std in the thread spawn path: the parent can set the TID of the child it just created, whereas today the TID can only be read once the child itself starts running. The declaration is limited to Android and linux-gnu. musl and ohos do not re-export it. ctest is gated on the glibc version. The function was added in 2.42, so older glibc has no declaration to check against. Link: https://github.com/bminor/glibc/blob/d2097651cc57834dbfcaa102ddfacae0d86cfb66/sysdeps/nptl/pthread.h#L1320-L1322 Link: https://android.googlesource.com/platform/bionic/+/731631f300090436d7f5df80d50b6275c8c60a93/libc/include/pthread.h#173 --- libc-test/build.rs | 2 ++ libc-test/semver/android.txt | 1 + libc-test/semver/linux-gnu.txt | 1 + src/new/bionic_libc/pthread.rs | 1 + src/new/common/linux_like/pthread.rs | 3 +++ src/new/glibc/sysdeps/nptl/pthread.rs | 1 + 6 files changed, 9 insertions(+) diff --git a/libc-test/build.rs b/libc-test/build.rs index b13a8369849c9..c1bfa119282b4 100755 --- a/libc-test/build.rs +++ b/libc-test/build.rs @@ -5090,6 +5090,8 @@ fn test_linux(target: &str) { "posix_spawn_file_actions_addclosefrom_np" if gnu && sparc64 => true, // Needs glibc 2.35 or later. "posix_spawn_file_actions_addtcsetpgrp_np" if gnu && sparc64 => true, + // Needs glibc 2.42 or later. + "pthread_gettid_np" if gnu && versions.glibc.unwrap() < (2, 42) => true, // FIXME(linux): Deprecated since glibc 2.30. Remove fn once upstream does. "sysctl" if gnu => true, diff --git a/libc-test/semver/android.txt b/libc-test/semver/android.txt index e765b15d5c2e6..3cf879f39e390 100644 --- a/libc-test/semver/android.txt +++ b/libc-test/semver/android.txt @@ -3829,6 +3829,7 @@ pthread_getattr_np pthread_getcpuclockid pthread_getschedparam pthread_getspecific +pthread_gettid_np pthread_join pthread_key_create pthread_key_delete diff --git a/libc-test/semver/linux-gnu.txt b/libc-test/semver/linux-gnu.txt index 88437e7b8485e..ed131210d845b 100644 --- a/libc-test/semver/linux-gnu.txt +++ b/libc-test/semver/linux-gnu.txt @@ -659,6 +659,7 @@ process_vm_readv process_vm_writev pthread_attr_getaffinity_np pthread_attr_setaffinity_np +pthread_gettid_np pthread_rwlockattr_getkind_np pthread_rwlockattr_getpshared pthread_rwlockattr_setkind_np diff --git a/src/new/bionic_libc/pthread.rs b/src/new/bionic_libc/pthread.rs index 4997547ff0905..db62dc4ce7844 100644 --- a/src/new/bionic_libc/pthread.rs +++ b/src/new/bionic_libc/pthread.rs @@ -2,6 +2,7 @@ pub use crate::new::common::linux_like::pthread::{ pthread_getattr_np, + pthread_gettid_np, pthread_setname_np, }; pub use crate::new::common::posix::pthread::{ diff --git a/src/new/common/linux_like/pthread.rs b/src/new/common/linux_like/pthread.rs index b54d385f9c05f..e76b704142a7c 100644 --- a/src/new/common/linux_like/pthread.rs +++ b/src/new/common/linux_like/pthread.rs @@ -13,6 +13,9 @@ extern "C" { #[cfg(target_os = "linux")] pub fn pthread_getname_np(thread: crate::pthread_t, name: *mut c_char, len: size_t) -> c_int; + #[cfg(any(target_os = "android", all(target_os = "linux", target_env = "gnu")))] + pub fn pthread_gettid_np(thread: crate::pthread_t) -> crate::pid_t; + #[cfg(any(target_os = "linux", target_os = "l4re"))] pub fn pthread_setaffinity_np( thread: crate::pthread_t, diff --git a/src/new/glibc/sysdeps/nptl/pthread.rs b/src/new/glibc/sysdeps/nptl/pthread.rs index cefc9e3002a41..bb45d2851fc50 100644 --- a/src/new/glibc/sysdeps/nptl/pthread.rs +++ b/src/new/glibc/sysdeps/nptl/pthread.rs @@ -28,6 +28,7 @@ pub use crate::new::common::linux_like::pthread::{ pthread_getaffinity_np, pthread_getattr_np, pthread_getname_np, + pthread_gettid_np, pthread_setaffinity_np, pthread_setname_np, };