[libc-commits] [libc] [llvm] [libc] Implement pthread_getcpuclockid (PR #243852)
via libc-commits
libc-commits at lists.llvm.org
Wed Nov 18 05:52:56 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Pavel Labath (labath)
<details>
<summary>Changes</summary>
On Linux, the ID of a thread's CPU clock is derived from its TID, using an encoding defined by the kernel. clock_getcpuclockid already did the same thing for process clocks, so I'm moving the encoding into a header which can be shared by both functions.
pthread_getcpuclockid doesn't check whether the thread is still running, as POSIX.1-2024 says that the pthread_t of a terminated thread remains valid until the thread is joined or detached. This means that the returned clock ID can be invalid, or (worse) refer to a different thread, as the kernel can reuse the TID once the thread exits. However, the same thing can happen to a clock ID obtained while the thread was still running, so I don't think it makes sense to try to detect this.
POSIX doesn't explicitly say what clock_gettime should return for threads in this dead-but-unjoined state. A reasonable interpretation would be to return the CPU time the thread used while it was alive. That's something we *could* implement -- by saving the CPU time just before the thread exits, and then reading it from clock_gettime. For that, we would need to use our own encoding of clockid_t (something derived from pthread_t), independent of what the kernel uses for the underlying syscall. However, that would be a very novel approach (I'm not aware of any implementation doing that), and would slow down the clock_gettime fast path, which is why I'm not attempting it.
A related corner case is fork(). The TID field in the thread attributes doubles as the TID cache, and fork() (as well as some clone() invocations) sets it to zero for the duration of the syscall. If another thread calls pthread_getcpuclockid on the forking thread at that time, it will get a clock ID encoding a zero TID, which the kernel interprets as "the calling thread". The same problem already exists for pthread_getschedparam and pthread_setschedparam (the sched_* syscalls also treat zero as the calling thread), so I'm not trying to fix it here.
With pthread_getcpuclockid in place, we support the Thread CPU-Time Clocks option, so I'm also changing _POSIX_THREAD_CPUTIME (and the value returned by sysconf(_SC_THREAD_CPUTIME)) to say so.
Assisted-by: Claude
---
Patch is 21.64 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/243852.diff
21 Files Affected:
- (modified) libc/config/linux/aarch64/entrypoints.txt (+1)
- (modified) libc/config/linux/riscv/entrypoints.txt (+1)
- (modified) libc/config/linux/x86_64/entrypoints.txt (+1)
- (modified) libc/include/llvm-libc-macros/linux/unistd-macros.h (+1-1)
- (modified) libc/include/pthread.yaml (+5)
- (modified) libc/src/__support/threads/CMakeLists.txt (+1)
- (modified) libc/src/__support/threads/linux/CMakeLists.txt (+1)
- (modified) libc/src/__support/threads/linux/thread.cpp (+10)
- (modified) libc/src/__support/threads/thread.h (+4)
- (modified) libc/src/__support/time/linux/CMakeLists.txt (+11)
- (added) libc/src/__support/time/linux/cpu_clock.h (+50)
- (modified) libc/src/pthread/CMakeLists.txt (+15)
- (added) libc/src/pthread/pthread_getcpuclockid.cpp (+35)
- (added) libc/src/pthread/pthread_getcpuclockid.h (+27)
- (modified) libc/src/time/linux/CMakeLists.txt (+1)
- (modified) libc/src/time/linux/clock_getcpuclockid.cpp (+2-5)
- (modified) libc/test/integration/src/pthread/CMakeLists.txt (+22)
- (added) libc/test/integration/src/pthread/pthread_getcpuclockid_test.cpp (+127)
- (modified) libc/test/src/unistd/sysconf_test.cpp (+1-1)
- (modified) utils/bazel/llvm-project-overlay/libc/src/__support/threads/BUILD.bazel (+2)
- (modified) utils/bazel/llvm-project-overlay/libc/src/__support/time/BUILD.bazel (+15)
``````````diff
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index c46362ef148499..9b134d5bd3b15d 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1272,6 +1272,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.pthread.pthread_equal
libc.src.pthread.pthread_exit
libc.src.pthread.pthread_getattr_np
+ libc.src.pthread.pthread_getcpuclockid
libc.src.pthread.pthread_getname_np
libc.src.pthread.pthread_getschedparam
libc.src.pthread.pthread_getspecific
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 35ab0d8640830e..18b784c4b7d61c 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1488,6 +1488,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.pthread.pthread_equal
libc.src.pthread.pthread_exit
libc.src.pthread.pthread_getattr_np
+ libc.src.pthread.pthread_getcpuclockid
libc.src.pthread.pthread_getname_np
libc.src.pthread.pthread_getschedparam
libc.src.pthread.pthread_getspecific
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index e381e45d9bba7f..cec8256e095435 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1488,6 +1488,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.pthread.pthread_equal
libc.src.pthread.pthread_exit
libc.src.pthread.pthread_getattr_np
+ libc.src.pthread.pthread_getcpuclockid
libc.src.pthread.pthread_getname_np
libc.src.pthread.pthread_getschedparam
libc.src.pthread.pthread_getspecific
diff --git a/libc/include/llvm-libc-macros/linux/unistd-macros.h b/libc/include/llvm-libc-macros/linux/unistd-macros.h
index 85f3e441ed2060..e2c6c55817bfb6 100644
--- a/libc/include/llvm-libc-macros/linux/unistd-macros.h
+++ b/libc/include/llvm-libc-macros/linux/unistd-macros.h
@@ -89,7 +89,7 @@
#define _POSIX_THREAD_ATTR_STACKSIZE _POSIX_VERSION
#define _POSIX_THREAD_PROCESS_SHARED _POSIX_VERSION
#define _POSIX_THREAD_PRIORITY_SCHEDULING _POSIX_VERSION
-#define _POSIX_THREAD_CPUTIME -1
+#define _POSIX_THREAD_CPUTIME _POSIX_VERSION
#define _POSIX_THREAD_PRIO_INHERIT -1
#define _POSIX_THREAD_PRIO_PROTECT -1
#define _POSIX_THREAD_ROBUST_PRIO_INHERIT -1
diff --git a/libc/include/pthread.yaml b/libc/include/pthread.yaml
index 2b62df4ab59935..bdace97d414555 100644
--- a/libc/include/pthread.yaml
+++ b/libc/include/pthread.yaml
@@ -298,6 +298,11 @@ functions:
arguments:
- type: pthread_t
- type: pthread_attr_t *
+ - name: pthread_getcpuclockid
+ return_type: int
+ arguments:
+ - type: pthread_t
+ - type: clockid_t *
- name: pthread_getname_np
standards:
- gnu
diff --git a/libc/src/__support/threads/CMakeLists.txt b/libc/src/__support/threads/CMakeLists.txt
index 4439cb811e7a77..81bcd610be143a 100644
--- a/libc/src/__support/threads/CMakeLists.txt
+++ b/libc/src/__support/threads/CMakeLists.txt
@@ -149,6 +149,7 @@ add_header_library(
.tcb
.thread_attributes
libc.hdr.stdint_proxy
+ libc.hdr.types.clockid_t
libc.hdr.types.struct_sched_param
libc.src.__support.common
libc.src.__support.error_or
diff --git a/libc/src/__support/threads/linux/CMakeLists.txt b/libc/src/__support/threads/linux/CMakeLists.txt
index 14c0782394e8fd..42640b2da2b1ce 100644
--- a/libc/src/__support/threads/linux/CMakeLists.txt
+++ b/libc/src/__support/threads/linux/CMakeLists.txt
@@ -90,6 +90,7 @@ add_object_library(
libc.src.__support.OSUtil.linux.syscall_wrappers.write
libc.src.__support.threads.thread_common
libc.src.__support.threads.thread_attributes
+ libc.src.__support.time.linux.cpu_clock
COMPILE_OPTIONS
${libc_opt_high_flag}
)
diff --git a/libc/src/__support/threads/linux/thread.cpp b/libc/src/__support/threads/linux/thread.cpp
index 2c33a6c5a5836d..a121cc0ecc709c 100644
--- a/libc/src/__support/threads/linux/thread.cpp
+++ b/libc/src/__support/threads/linux/thread.cpp
@@ -39,6 +39,7 @@
#include "src/__support/threads/linux/futex_utils.h" // For FutexWordType
#include "src/__support/threads/linux/futex_word.h"
#include "src/__support/threads/thread_attributes.h"
+#include "src/__support/time/linux/cpu_clock.h"
#include "hdr/errno_macros.h"
#include "hdr/fcntl_macros.h"
@@ -494,6 +495,15 @@ ErrorOr<SchedParameters> Thread::getschedparam() const {
return SchedParameters{pol_result.value(), param};
}
+clockid_t Thread::getcpuclockid() const {
+ // NB: Once the thread exits, the kernel can reuse its TID, so the clock ID
+ // may stop working, or even refer to a different thread. We don't check for
+ // that since POSIX.1-2024 considers the pthread_t of an exited thread valid
+ // until it is joined or detached, and a clock ID obtained before the thread
+ // exited is affected the same way.
+ return internal::make_thread_cpu_clock(attrib->tid);
+}
+
ErrorOr<void> Thread::kill(int sig) {
auto state = static_cast<DetachState>(
attrib->detach_state.load(cpp::MemoryOrder::RELAXED));
diff --git a/libc/src/__support/threads/thread.h b/libc/src/__support/threads/thread.h
index 18e779aefbc7c3..cdd0cd9f64cbbc 100644
--- a/libc/src/__support/threads/thread.h
+++ b/libc/src/__support/threads/thread.h
@@ -15,6 +15,7 @@
#define LLVM_LIBC_SRC___SUPPORT_THREADS_THREAD_H
#include "hdr/stdint_proxy.h"
+#include "hdr/types/clockid_t.h"
#include "hdr/types/struct_sched_param.h"
#include "src/__support/CPP/atomic.h"
#include "src/__support/CPP/optional.h"
@@ -162,6 +163,9 @@ struct Thread {
// Return SchedParameters on success, or an error number on failure.
ErrorOr<SchedParameters> getschedparam() const;
+ // Get the ID of the CPU-time clock of the thread.
+ clockid_t getcpuclockid() const;
+
// Send a signal to the thread.
ErrorOr<void> kill(int sig);
};
diff --git a/libc/src/__support/time/linux/CMakeLists.txt b/libc/src/__support/time/linux/CMakeLists.txt
index 2d5d9f10faa44d..d4a850fdffa29f 100644
--- a/libc/src/__support/time/linux/CMakeLists.txt
+++ b/libc/src/__support/time/linux/CMakeLists.txt
@@ -15,6 +15,17 @@ add_object_library(
libc.src.__support.macros.config
)
+add_header_library(
+ cpu_clock
+ HDRS
+ cpu_clock.h
+ DEPENDS
+ libc.hdr.types.clockid_t
+ libc.hdr.types.pid_t
+ libc.src.__support.macros.attributes
+ libc.src.__support.macros.config
+)
+
add_object_library(
clock_settime
HDRS
diff --git a/libc/src/__support/time/linux/cpu_clock.h b/libc/src/__support/time/linux/cpu_clock.h
new file mode 100644
index 00000000000000..ddbd54e0900501
--- /dev/null
+++ b/libc/src/__support/time/linux/cpu_clock.h
@@ -0,0 +1,50 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Construction of Linux CPU-time clock IDs for a given process or thread.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CPU_CLOCK_H
+#define LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CPU_CLOCK_H
+
+#include "hdr/types/clockid_t.h"
+#include "hdr/types/pid_t.h"
+#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace internal {
+
+// The CPU-time clock of a specific process or thread is identified by a clock
+// ID which encodes the process or thread ID. The functions below match the
+// encoding used by the Linux kernel, which is not a part of the userspace API
+// headers.
+
+LIBC_INLINE_VAR constexpr unsigned int CPUCLOCK_SCHED = 2;
+LIBC_INLINE_VAR constexpr unsigned int CPUCLOCK_PERTHREAD_MASK = 4;
+
+LIBC_INLINE constexpr clockid_t make_cpu_clock(pid_t id, unsigned int flags) {
+ // Shift as unsigned: ~id is negative for valid IDs, and left-shifting a
+ // negative value is UB.
+ return static_cast<clockid_t>((~static_cast<unsigned int>(id) << 3) | flags);
+}
+
+LIBC_INLINE constexpr clockid_t make_process_cpu_clock(pid_t pid) {
+ return make_cpu_clock(pid, CPUCLOCK_SCHED);
+}
+
+LIBC_INLINE constexpr clockid_t make_thread_cpu_clock(pid_t tid) {
+ return make_cpu_clock(tid, CPUCLOCK_SCHED | CPUCLOCK_PERTHREAD_MASK);
+}
+
+} // namespace internal
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_TIME_LINUX_CPU_CLOCK_H
diff --git a/libc/src/pthread/CMakeLists.txt b/libc/src/pthread/CMakeLists.txt
index 9169748a5c6c78..48ca5db6563b8e 100644
--- a/libc/src/pthread/CMakeLists.txt
+++ b/libc/src/pthread/CMakeLists.txt
@@ -1115,6 +1115,21 @@ add_entrypoint_object(
libc.src.__support.threads.thread
)
+add_entrypoint_object(
+ pthread_getcpuclockid
+ SRCS
+ pthread_getcpuclockid.cpp
+ HDRS
+ pthread_getcpuclockid.h
+ DEPENDS
+ libc.hdr.types.clockid_t
+ libc.hdr.types.pthread_t
+ libc.src.__support.common
+ libc.src.__support.macros.config
+ libc.src.__support.macros.null_check
+ libc.src.__support.threads.thread
+)
+
add_entrypoint_object(
pthread_getattr_np
SRCS
diff --git a/libc/src/pthread/pthread_getcpuclockid.cpp b/libc/src/pthread/pthread_getcpuclockid.cpp
new file mode 100644
index 00000000000000..37a6563d742d13
--- /dev/null
+++ b/libc/src/pthread/pthread_getcpuclockid.cpp
@@ -0,0 +1,35 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of the pthread_getcpuclockid function.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/pthread/pthread_getcpuclockid.h"
+
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
+#include "src/__support/threads/thread.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+static_assert(sizeof(pthread_t) == sizeof(LIBC_NAMESPACE::Thread),
+ "Mismatch between pthread_t and internal Thread.");
+
+LLVM_LIBC_FUNCTION(int, pthread_getcpuclockid,
+ (pthread_t th, clockid_t *clock_id)) {
+ LIBC_CRASH_ON_NULLPTR(clock_id);
+ auto *thread = reinterpret_cast<Thread *>(&th);
+ LIBC_CRASH_ON_NULLPTR(thread->attrib);
+ *clock_id = thread->getcpuclockid();
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/pthread/pthread_getcpuclockid.h b/libc/src/pthread/pthread_getcpuclockid.h
new file mode 100644
index 00000000000000..cfe8d5c2678daa
--- /dev/null
+++ b/libc/src/pthread/pthread_getcpuclockid.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for pthread_getcpuclockid.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_PTHREAD_PTHREAD_GETCPUCLOCKID_H
+#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_GETCPUCLOCKID_H
+
+#include "hdr/types/clockid_t.h"
+#include "hdr/types/pthread_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int pthread_getcpuclockid(pthread_t thread, clockid_t *clock_id);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_GETCPUCLOCKID_H
diff --git a/libc/src/time/linux/CMakeLists.txt b/libc/src/time/linux/CMakeLists.txt
index 74891dce27445a..3c17d6e4a9b774 100644
--- a/libc/src/time/linux/CMakeLists.txt
+++ b/libc/src/time/linux/CMakeLists.txt
@@ -215,4 +215,5 @@ add_entrypoint_object(
libc.src.__support.OSUtil.linux.syscall_wrappers.clock_getres
libc.src.__support.common
libc.src.__support.macros.config
+ libc.src.__support.time.linux.cpu_clock
)
diff --git a/libc/src/time/linux/clock_getcpuclockid.cpp b/libc/src/time/linux/clock_getcpuclockid.cpp
index b71af65a425936..cdf134c44a238d 100644
--- a/libc/src/time/linux/clock_getcpuclockid.cpp
+++ b/libc/src/time/linux/clock_getcpuclockid.cpp
@@ -18,17 +18,14 @@
#include "src/__support/OSUtil/linux/syscall_wrappers/clock_getres.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
+#include "src/__support/time/linux/cpu_clock.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(int, clock_getcpuclockid, (pid_t pid, clockid_t *clock_id)) {
if (pid < 0)
return ESRCH;
- // On Linux, process CPU clocks are encoded as (~pid << 3) | clock_id with
- // clock_id CPUCLOCK_SCHED being 2:
- // https://github.com/torvalds/linux/blob/master/include/linux/posix-timers.h#L22
- const clockid_t pid_clockid =
- static_cast<clockid_t>((static_cast<unsigned int>(~pid) << 3) | 2);
+ const clockid_t pid_clockid = internal::make_process_cpu_clock(pid);
auto result = linux_syscalls::clock_getres(pid_clockid, nullptr);
// Note that the clock_getcpuclockid doesn't set errno, it returns the
diff --git a/libc/test/integration/src/pthread/CMakeLists.txt b/libc/test/integration/src/pthread/CMakeLists.txt
index 77e245ba83893a..729a2f8769bb5f 100644
--- a/libc/test/integration/src/pthread/CMakeLists.txt
+++ b/libc/test/integration/src/pthread/CMakeLists.txt
@@ -398,6 +398,28 @@ add_integration_test(
libc.src.pthread.pthread_setschedparam
)
+add_integration_test(
+ pthread_getcpuclockid_test
+ SUITE
+ libc-pthread-integration-tests
+ SRCS
+ pthread_getcpuclockid_test.cpp
+ DEPENDS
+ libc.hdr.time_macros
+ libc.hdr.types.clockid_t
+ libc.hdr.types.pthread_t
+ libc.hdr.types.struct_timespec
+ libc.hdr.types.time_t
+ libc.src.__support.CPP.atomic
+ libc.src.__support.threads.futex_utils
+ libc.src.__support.threads.thread
+ libc.src.pthread.pthread_create
+ libc.src.pthread.pthread_getcpuclockid
+ libc.src.pthread.pthread_join
+ libc.src.pthread.pthread_self
+ libc.src.time.clock_gettime
+)
+
add_integration_test(
pthread_kill_test
SUITE
diff --git a/libc/test/integration/src/pthread/pthread_getcpuclockid_test.cpp b/libc/test/integration/src/pthread/pthread_getcpuclockid_test.cpp
new file mode 100644
index 00000000000000..6cd4f79c8baaf8
--- /dev/null
+++ b/libc/test/integration/src/pthread/pthread_getcpuclockid_test.cpp
@@ -0,0 +1,127 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Integration tests for pthread_getcpuclockid.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/time_macros.h"
+#include "hdr/types/clockid_t.h"
+#include "hdr/types/pthread_t.h"
+#include "hdr/types/struct_timespec.h"
+#include "hdr/types/time_t.h"
+#include "src/__support/CPP/atomic.h"
+#include "src/__support/threads/futex_utils.h"
+#include "src/__support/threads/thread.h"
+#include "src/pthread/pthread_create.h"
+#include "src/pthread/pthread_getcpuclockid.h"
+#include "src/pthread/pthread_join.h"
+#include "src/pthread/pthread_self.h"
+#include "src/time/clock_gettime.h"
+#include "test/IntegrationTest/test.h"
+
+// How much CPU time the busy worker accumulates before reporting back.
+static constexpr time_t BUSY_TIME_NS = 10'000'000;
+
+static LIBC_NAMESPACE::Futex worker_busy_done(0);
+static LIBC_NAMESPACE::Futex worker_release(0);
+
+static time_t cpu_time_ns(clockid_t clock) {
+ timespec ts;
+ ASSERT_EQ(LIBC_NAMESPACE::clock_gettime(clock, &ts), 0);
+ return ts.tv_sec * 1'000'000'000 + ts.tv_nsec;
+}
+
+static void test_self() {
+ clockid_t clock;
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_getcpuclockid(
+ LIBC_NAMESPACE::pthread_self(), &clock),
+ 0);
+ // The clock must identify this specific thread, not "the calling thread".
+ ASSERT_NE(clock, CLOCK_THREAD_CPUTIME_ID);
+
+ // Both clocks measure the CPU time of this thread, so the first reading
+ // cannot be larger than the second one.
+ time_t first = cpu_time_ns(clock);
+ time_t second = cpu_time_ns(CLOCK_THREAD_CPUTIME_ID);
+ ASSERT_TRUE(first <= second);
+}
+
+static void *busy_worker(void *) {
+ while (cpu_time_ns(CLOCK_THREAD_CPUTIME_ID) < BUSY_TIME_NS)
+ ;
+ worker_busy_done.store(1, LIBC_NAMESPACE::cpp::MemoryOrder::RELEASE);
+ worker_busy_done.notify_one();
+
+ // Stay alive until the main thread is done reading our clock.
+ while (worker_release.load(LIBC_NAMESPACE::cpp::MemoryOrder::ACQUIRE) == 0)
+ worker_release.wait(0);
+ return nullptr;
+}
+
+static void test_running_thread() {
+ pthread_t th;
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_create(&th, nullptr, busy_worker, nullptr),
+ 0);
+
+ clockid_t worker_clock;
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_getcpuclockid(th, &worker_clock), 0);
+ clockid_t self_clock;
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_getcpuclockid(
+ LIBC_NAMESPACE::pthread_self(), &self_clock),
+ 0);
+ ASSERT_NE(worker_clock, self_clock);
+
+ while (worker_busy_done.load(LIBC_NAMESPACE::cpp::MemoryOrder::ACQUIRE) == 0)
+ worker_busy_done.wait(0);
+ // Reading the worker's clock from this thread must show the CPU time it
+ // has accumulated.
+ ASSERT_TRUE(cpu_time_ns(worker_clock) >= BUSY_TIME_NS);
+
+ worker_release.store(1, LIBC_NAMESPACE::cpp::MemoryOrder::RELEASE);
+ worker_release.notify_one();
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_join(th, nullptr), 0);
+}
+
+static LIBC_NAMESPACE::Futex exit_release(0);
+
+static void *exiting_worker(void *) {
+ // Don't exit until the main thread has obtained our clock ID.
+ while (exit_release.load(LIBC_NAMESPACE::cpp::MemoryOrder::ACQUIRE) == 0)
+ exit_release.wait(0);
+ return nullptr;
+}
+
+static void test_exited_thread() {
+ pthread_t th;
+ ASSERT_EQ(
+ LIBC_NAMESPACE::pthread_create(&th, nullptr, exiting_worker, nullptr), 0);
+ clockid_t clock_before;
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_getcpuclockid(th, &clock_before), 0);
+
+ // Let the thread exit and wait until it has done so, but don't join it yet.
+ exit_release.store(1, LIBC_NAMESPACE::cpp::MemoryOrder::RELEASE);
+ exit_release.notify_one();
+ auto *thread_internal = reinterpret_cast<LIBC_NAMESPACE::Thread *>(&th);
+ thread_internal->wait();
+
+ // The pthread_t remains valid until the thread is joined.
+ clockid_t clock_after;
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_getcpuclockid(th, &clock_after), 0);
+ ASSERT_EQ(clock_before, clock_after);
+
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_join(th, nullptr), 0);
+}
+
+TEST_MAIN() {
+ test_self();
+ test_running_thread();
+ test_exited_thread();
+ return 0;
+}
diff --git a/libc/test/src/unistd/sysconf_test.cpp b/libc/test/src/unistd/sysconf_test.cpp
index 2f0e973543d2a5..83e955f5f5ec75 100644
--- a/libc/test/src/unistd/sysconf_test.cpp
+++ b/libc/test/src/unistd/sysconf_test.cpp
@@ -46,7 +46,7 @@ TEST_F(LlvmLibcSysconfTest, PosixThreadsConstantsTest) {
Succeeds(202405L));
EXPECT_THAT(LIBC_NAMESPACE::sysconf(_SC_THREAD_ATTR_STACKSIZE),
Succeeds(202405L));
- EXPECT_THAT(LIBC_NAMESPACE::sysconf(_SC_THREAD_CPUTIME), Succeeds(-1L));
+ EXPECT_THAT(LIBC_NAMESPACE::sysconf(_SC_THREAD_CPUTIME), Succeeds(202405L));
EXPECT_THAT(LIBC_NAMESPACE::sysconf(_SC_THREAD_PRIO_INHERIT), Succeeds(-1L));
EXPECT_THAT(LIBC_NAMESPACE::sysconf(_SC_THREAD_PRIO_PROTECT), Succeeds(-1L));
EXPECT_THAT(LIBC_NAMESPACE::sysconf(_SC_THREAD_PRIORITY_SCHEDULING),
diff --git a/utils/bazel/llvm-project-overlay/libc/src/__support/threads/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/src/__supp...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/243852
More information about the libc-commits
mailing list