[libc-commits] [libc] [libc] Implement mtx_timedlock and pthread_mutex_timedlock (#240021) (PR #240028)
Aman Maurya via libc-commits
libc-commits at lists.llvm.org
Sat Nov 7 12:35:04 PST 2026
https://github.com/amanmaurya92 created https://github.com/llvm/llvm-project/pull/240028
This PR implements `mtx_timedlock` in `<threads.h>` and `pthread_mutex_timedlock` in `<pthread.h>`.
- Connects to existing internal `Mutex::timed_lock(AbsTimeout)` and handles timeout/expired specifications.
- Adds `mtx_timedlock` entrypoint in `libc/src/threads`.
- Adds `pthread_mutex_timedlock` entrypoint in `libc/src/pthread`.
- Adds `timed_lock` implementation for `Mutex` in single-thread mode (`LIBC_THREAD_MODE_SINGLE`).
- Registers public declarations in `threads.yaml` and `pthread.yaml`.
- Registers entrypoints across Linux targets (`x86_64`, `aarch64`, `riscv`).
- Adds timedlock tests to `mtx_test` and `pthread_mutex_test`.
Fixes #240021.
>From eef2f6e97ea78613172e937c4842d2303f15c904 Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Sun, 8 Nov 2026 02:04:22 +0530
Subject: [PATCH] [libc] Implement mtx_timedlock and pthread_mutex_timedlock
(#240021)
Implement mtx_timedlock in <threads.h> and pthread_mutex_timedlock in <pthread.h>.
- Connects to existing internal Mutex::timed_lock(AbsTimeout).
- Adds mtx_timedlock in libc/src/threads.
- Adds pthread_mutex_timedlock in libc/src/pthread.
- Registers public declarations in threads.yaml and pthread.yaml.
- Adds entrypoints across Linux targets (x86_64, aarch64, riscv).
- Adds timedlock test cases to mtx_test and pthread_mutex_test.
Fixes #240021.
---
libc/config/linux/aarch64/entrypoints.txt | 2 +
libc/config/linux/riscv/entrypoints.txt | 2 +
libc/config/linux/x86_64/entrypoints.txt | 2 +
libc/include/pthread.yaml | 5 ++
libc/include/threads.yaml | 7 +++
libc/src/__support/threads/mutex.h | 4 ++
libc/src/pthread/CMakeLists.txt | 17 ++++++
libc/src/pthread/pthread_mutex_timedlock.cpp | 57 +++++++++++++++++++
libc/src/pthread/pthread_mutex_timedlock.h | 23 ++++++++
libc/src/threads/CMakeLists.txt | 16 ++++++
libc/src/threads/mtx_timedlock.cpp | 53 +++++++++++++++++
libc/src/threads/mtx_timedlock.h | 23 ++++++++
.../integration/src/pthread/CMakeLists.txt | 2 +
.../src/pthread/pthread_mutex_test.cpp | 24 ++++++++
.../integration/src/threads/CMakeLists.txt | 1 +
.../test/integration/src/threads/mtx_test.cpp | 27 +++++++++
16 files changed, 265 insertions(+)
create mode 100644 libc/src/pthread/pthread_mutex_timedlock.cpp
create mode 100644 libc/src/pthread/pthread_mutex_timedlock.h
create mode 100644 libc/src/threads/mtx_timedlock.cpp
create mode 100644 libc/src/threads/mtx_timedlock.h
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 8ba560989af6af..0b2e94416d5df6 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1265,6 +1265,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.pthread.pthread_mutex_destroy
libc.src.pthread.pthread_mutex_init
libc.src.pthread.pthread_mutex_lock
+ libc.src.pthread.pthread_mutex_timedlock
libc.src.pthread.pthread_mutex_trylock
libc.src.pthread.pthread_mutex_unlock
libc.src.pthread.pthread_mutexattr_destroy
@@ -1461,6 +1462,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.threads.mtx_destroy
libc.src.threads.mtx_init
libc.src.threads.mtx_lock
+ libc.src.threads.mtx_timedlock
libc.src.threads.mtx_trylock
libc.src.threads.mtx_unlock
libc.src.threads.thrd_create
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 56813aaf66a91f..8d6973a9b117b6 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1458,6 +1458,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.pthread.pthread_mutex_destroy
libc.src.pthread.pthread_mutex_init
libc.src.pthread.pthread_mutex_lock
+ libc.src.pthread.pthread_mutex_timedlock
libc.src.pthread.pthread_mutex_trylock
libc.src.pthread.pthread_mutex_unlock
libc.src.pthread.pthread_mutexattr_destroy
@@ -1655,6 +1656,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.threads.mtx_destroy
libc.src.threads.mtx_init
libc.src.threads.mtx_lock
+ libc.src.threads.mtx_timedlock
libc.src.threads.mtx_trylock
libc.src.threads.mtx_unlock
libc.src.threads.thrd_create
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 25b24740400b97..3ae88d0df58b58 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1480,6 +1480,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.pthread.pthread_mutex_destroy
libc.src.pthread.pthread_mutex_init
libc.src.pthread.pthread_mutex_lock
+ libc.src.pthread.pthread_mutex_timedlock
libc.src.pthread.pthread_mutex_trylock
libc.src.pthread.pthread_mutex_unlock
libc.src.pthread.pthread_mutexattr_destroy
@@ -1681,6 +1682,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.threads.mtx_destroy
libc.src.threads.mtx_init
libc.src.threads.mtx_lock
+ libc.src.threads.mtx_timedlock
libc.src.threads.mtx_trylock
libc.src.threads.mtx_unlock
libc.src.threads.thrd_create
diff --git a/libc/include/pthread.yaml b/libc/include/pthread.yaml
index 9f2fffd38a088b..2b62df4ab59935 100644
--- a/libc/include/pthread.yaml
+++ b/libc/include/pthread.yaml
@@ -351,6 +351,11 @@ functions:
return_type: int
arguments:
- type: pthread_mutex_t *
+ - name: pthread_mutex_timedlock
+ return_type: int
+ arguments:
+ - type: pthread_mutex_t *__restrict
+ - type: const struct timespec *__restrict
- name: pthread_mutex_trylock
return_type: int
arguments:
diff --git a/libc/include/threads.yaml b/libc/include/threads.yaml
index cab48108385898..1843d17d14269e 100644
--- a/libc/include/threads.yaml
+++ b/libc/include/threads.yaml
@@ -101,6 +101,13 @@ functions:
return_type: int
arguments:
- type: mtx_t *
+ - name: mtx_timedlock
+ standards:
+ - stdc
+ return_type: int
+ arguments:
+ - type: mtx_t *__restrict
+ - type: const struct timespec *__restrict
- name: mtx_trylock
standards:
- stdc
diff --git a/libc/src/__support/threads/mutex.h b/libc/src/__support/threads/mutex.h
index 1ddf63d6d15404..9dd389e8a1f7d0 100644
--- a/libc/src/__support/threads/mutex.h
+++ b/libc/src/__support/threads/mutex.h
@@ -62,6 +62,10 @@ struct Mutex {
LIBC_INLINE MutexError unlock() { return MutexError::NONE; }
LIBC_INLINE MutexError reset() { return MutexError::NONE; }
LIBC_INLINE MutexError try_lock() { return MutexError::NONE; }
+ template <typename TimeoutType>
+ LIBC_INLINE MutexError timed_lock(TimeoutType) {
+ return MutexError::NONE;
+ }
LIBC_INLINE bool is_robust() const { return false; }
};
diff --git a/libc/src/pthread/CMakeLists.txt b/libc/src/pthread/CMakeLists.txt
index 5190c0ceacaa74..2404e7d48ca27d 100644
--- a/libc/src/pthread/CMakeLists.txt
+++ b/libc/src/pthread/CMakeLists.txt
@@ -574,6 +574,23 @@ add_entrypoint_object(
libc.src.__support.threads.mutex
)
+add_entrypoint_object(
+ pthread_mutex_timedlock
+ SRCS
+ pthread_mutex_timedlock.cpp
+ HDRS
+ pthread_mutex_timedlock.h
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.types.struct_timespec
+ libc.include.pthread
+ libc.src.__support.common
+ libc.src.__support.macros.config
+ libc.src.__support.macros.null_check
+ libc.src.__support.threads.mutex
+ libc.src.__support.time.abs_timeout
+)
+
add_entrypoint_object(
pthread_mutex_trylock
SRCS
diff --git a/libc/src/pthread/pthread_mutex_timedlock.cpp b/libc/src/pthread/pthread_mutex_timedlock.cpp
new file mode 100644
index 00000000000000..6220b60ae8cd95
--- /dev/null
+++ b/libc/src/pthread/pthread_mutex_timedlock.cpp
@@ -0,0 +1,57 @@
+//===-- Linux implementation of the pthread_mutex_timedlock ---------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/pthread/pthread_mutex_timedlock.h"
+
+#include "hdr/errno_macros.h"
+#include "hdr/types/struct_timespec.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
+#include "src/__support/threads/mutex.h"
+#include "src/__support/time/abs_timeout.h"
+
+#include <pthread.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, pthread_mutex_timedlock,
+ (pthread_mutex_t *__restrict mutex,
+ const struct timespec *__restrict abstime)) {
+ LIBC_CRASH_ON_NULLPTR(abstime);
+ auto *m = reinterpret_cast<Mutex *>(mutex);
+
+ auto timeout =
+ internal::AbsTimeout::from_timespec(*abstime, /*is_realtime=*/true);
+
+ if (!timeout.has_value()) {
+ switch (timeout.error()) {
+ case internal::AbsTimeout::Error::BeforeEpoch:
+ if (m->try_lock() == MutexError::NONE)
+ return 0;
+ return ETIMEDOUT;
+ case internal::AbsTimeout::Error::Invalid:
+ return EINVAL;
+ }
+ __builtin_unreachable();
+ }
+
+ auto err = m->timed_lock(timeout.value());
+ switch (err) {
+ case MutexError::NONE:
+ return 0;
+ case MutexError::TIMEOUT:
+ return ETIMEDOUT;
+ case MutexError::DEADLOCK:
+ return EDEADLK;
+ default:
+ return EINVAL;
+ }
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/pthread/pthread_mutex_timedlock.h b/libc/src/pthread/pthread_mutex_timedlock.h
new file mode 100644
index 00000000000000..185c2f213ecc6e
--- /dev/null
+++ b/libc/src/pthread/pthread_mutex_timedlock.h
@@ -0,0 +1,23 @@
+//===-- Implementation header for pthread_mutex_timedlock -------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEX_TIMEDLOCK_H
+#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEX_TIMEDLOCK_H
+
+#include "hdr/types/struct_timespec.h"
+#include "src/__support/macros/config.h"
+#include <pthread.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+int pthread_mutex_timedlock(pthread_mutex_t *__restrict mutex,
+ const struct timespec *__restrict abstime);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEX_TIMEDLOCK_H
diff --git a/libc/src/threads/CMakeLists.txt b/libc/src/threads/CMakeLists.txt
index b5560730781b9d..6207820fd9f06d 100644
--- a/libc/src/threads/CMakeLists.txt
+++ b/libc/src/threads/CMakeLists.txt
@@ -113,6 +113,22 @@ add_entrypoint_object(
libc.src.__support.threads.mutex
)
+add_entrypoint_object(
+ mtx_timedlock
+ SRCS
+ mtx_timedlock.cpp
+ HDRS
+ mtx_timedlock.h
+ DEPENDS
+ libc.hdr.types.struct_timespec
+ libc.include.threads
+ libc.src.__support.common
+ libc.src.__support.macros.config
+ libc.src.__support.macros.null_check
+ libc.src.__support.threads.mutex
+ libc.src.__support.time.abs_timeout
+)
+
add_entrypoint_object(
mtx_trylock
SRCS
diff --git a/libc/src/threads/mtx_timedlock.cpp b/libc/src/threads/mtx_timedlock.cpp
new file mode 100644
index 00000000000000..7f897002671cb4
--- /dev/null
+++ b/libc/src/threads/mtx_timedlock.cpp
@@ -0,0 +1,53 @@
+//===-- Implementation of the mtx_timedlock function ---------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/threads/mtx_timedlock.h"
+#include "hdr/types/struct_timespec.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
+#include "src/__support/threads/mutex.h"
+#include "src/__support/time/abs_timeout.h"
+
+#include <threads.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, mtx_timedlock,
+ (mtx_t *__restrict mutex,
+ const struct timespec *__restrict time_point)) {
+ LIBC_CRASH_ON_NULLPTR(time_point);
+ auto *m = reinterpret_cast<Mutex *>(mutex);
+
+ auto timeout =
+ internal::AbsTimeout::from_timespec(*time_point, /*is_realtime=*/true);
+
+ if (!timeout.has_value()) {
+ switch (timeout.error()) {
+ case internal::AbsTimeout::Error::BeforeEpoch:
+ if (m->try_lock() == MutexError::NONE)
+ return thrd_success;
+ return thrd_timedout;
+ case internal::AbsTimeout::Error::Invalid:
+ return thrd_error;
+ }
+ __builtin_unreachable();
+ }
+
+ auto err = m->timed_lock(timeout.value());
+ switch (err) {
+ case MutexError::NONE:
+ return thrd_success;
+ case MutexError::TIMEOUT:
+ return thrd_timedout;
+ default:
+ return thrd_error;
+ }
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/threads/mtx_timedlock.h b/libc/src/threads/mtx_timedlock.h
new file mode 100644
index 00000000000000..fc64cd91e3f856
--- /dev/null
+++ b/libc/src/threads/mtx_timedlock.h
@@ -0,0 +1,23 @@
+//===-- Implementation header for mtx_timedlock function --------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_THREADS_MTX_TIMEDLOCK_H
+#define LLVM_LIBC_SRC_THREADS_MTX_TIMEDLOCK_H
+
+#include "hdr/types/struct_timespec.h"
+#include "src/__support/macros/config.h"
+#include <threads.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+int mtx_timedlock(mtx_t *__restrict mutex,
+ const struct timespec *__restrict time_point);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_THREADS_MTX_TIMEDLOCK_H
diff --git a/libc/test/integration/src/pthread/CMakeLists.txt b/libc/test/integration/src/pthread/CMakeLists.txt
index cbf89eaf6abea4..bfbb3b7e61fe1a 100644
--- a/libc/test/integration/src/pthread/CMakeLists.txt
+++ b/libc/test/integration/src/pthread/CMakeLists.txt
@@ -13,6 +13,7 @@ add_integration_test(
libc.src.pthread.pthread_mutex_destroy
libc.src.pthread.pthread_mutex_init
libc.src.pthread.pthread_mutex_lock
+ libc.src.pthread.pthread_mutex_timedlock
libc.src.pthread.pthread_mutex_trylock
libc.src.pthread.pthread_mutex_unlock
libc.src.pthread.pthread_mutexattr_destroy
@@ -436,6 +437,7 @@ add_integration_test(
libc.src.pthread.pthread_mutex_destroy
libc.src.pthread.pthread_mutex_init
libc.src.pthread.pthread_mutex_lock
+ libc.src.pthread.pthread_mutex_timedlock
libc.src.pthread.pthread_mutex_trylock
libc.src.pthread.pthread_mutex_unlock
)
diff --git a/libc/test/integration/src/pthread/pthread_mutex_test.cpp b/libc/test/integration/src/pthread/pthread_mutex_test.cpp
index 1a27fec139f982..6ba5df54cd913d 100644
--- a/libc/test/integration/src/pthread/pthread_mutex_test.cpp
+++ b/libc/test/integration/src/pthread/pthread_mutex_test.cpp
@@ -13,6 +13,7 @@
#include "src/pthread/pthread_mutex_destroy.h"
#include "src/pthread/pthread_mutex_init.h"
#include "src/pthread/pthread_mutex_lock.h"
+#include "src/pthread/pthread_mutex_timedlock.h"
#include "src/pthread/pthread_mutex_trylock.h"
#include "src/pthread/pthread_mutex_unlock.h"
#include "src/pthread/pthread_mutexattr_destroy.h"
@@ -156,6 +157,28 @@ void trylock_test() {
LIBC_NAMESPACE::pthread_mutex_destroy(&trylock_mutex);
}
+void timedlock_test() {
+ pthread_mutex_t timed_mutex;
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_init(&timed_mutex, nullptr), 0);
+
+ // Invalid timespec (tv_nsec >= 1'000'000'000)
+ struct timespec invalid_ts = {0, 1000000000L};
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_timedlock(&timed_mutex, &invalid_ts),
+ EINVAL);
+
+ // Locking an unlocked mutex succeeds
+ struct timespec ts = {2000000000L, 0};
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_timedlock(&timed_mutex, &ts), 0);
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_unlock(&timed_mutex), 0);
+
+ // Past timestamp on unlocked mutex should still succeed per POSIX
+ struct timespec past_ts = {0, 0};
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_timedlock(&timed_mutex, &past_ts), 0);
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_unlock(&timed_mutex), 0);
+
+ LIBC_NAMESPACE::pthread_mutex_destroy(&timed_mutex);
+}
+
void *trylock_other_thread(void *arg) {
auto *mutex = reinterpret_cast<pthread_mutex_t *>(arg);
int result = LIBC_NAMESPACE::pthread_mutex_trylock(mutex);
@@ -330,5 +353,6 @@ TEST_MAIN() {
initializer_acts_the_same_as_null_attr();
error_checking_mutex_test();
multiple_waiters();
+ timedlock_test();
return 0;
}
diff --git a/libc/test/integration/src/threads/CMakeLists.txt b/libc/test/integration/src/threads/CMakeLists.txt
index 8481fb98039b82..4c62bfb710706c 100644
--- a/libc/test/integration/src/threads/CMakeLists.txt
+++ b/libc/test/integration/src/threads/CMakeLists.txt
@@ -11,6 +11,7 @@ add_integration_test(
libc.src.threads.mtx_destroy
libc.src.threads.mtx_init
libc.src.threads.mtx_lock
+ libc.src.threads.mtx_timedlock
libc.src.threads.mtx_trylock
libc.src.threads.mtx_unlock
libc.src.threads.thrd_create
diff --git a/libc/test/integration/src/threads/mtx_test.cpp b/libc/test/integration/src/threads/mtx_test.cpp
index 4609e97d25fa91..b090f0eda6efc9 100644
--- a/libc/test/integration/src/threads/mtx_test.cpp
+++ b/libc/test/integration/src/threads/mtx_test.cpp
@@ -10,6 +10,7 @@
#include "src/threads/mtx_destroy.h"
#include "src/threads/mtx_init.h"
#include "src/threads/mtx_lock.h"
+#include "src/threads/mtx_timedlock.h"
#include "src/threads/mtx_trylock.h"
#include "src/threads/mtx_unlock.h"
#include "src/threads/thrd_create.h"
@@ -256,11 +257,37 @@ void trylock_test() {
LIBC_NAMESPACE::mtx_destroy(&recursive_mutex);
}
+void timedlock_test() {
+ mtx_t timed_mutex;
+ ASSERT_EQ(LIBC_NAMESPACE::mtx_init(&timed_mutex, mtx_timed),
+ int(thrd_success));
+
+ // Invalid timespec (tv_nsec >= 1'000'000'000)
+ struct timespec invalid_ts = {0, 1000000000L};
+ ASSERT_EQ(LIBC_NAMESPACE::mtx_timedlock(&timed_mutex, &invalid_ts),
+ int(thrd_error));
+
+ // Locking an unlocked mutex succeeds
+ struct timespec ts = {2000000000L, 0};
+ ASSERT_EQ(LIBC_NAMESPACE::mtx_timedlock(&timed_mutex, &ts),
+ int(thrd_success));
+ ASSERT_EQ(LIBC_NAMESPACE::mtx_unlock(&timed_mutex), int(thrd_success));
+
+ // Past timestamp on unlocked mutex should still succeed per spec
+ struct timespec past_ts = {0, 0};
+ ASSERT_EQ(LIBC_NAMESPACE::mtx_timedlock(&timed_mutex, &past_ts),
+ int(thrd_success));
+ ASSERT_EQ(LIBC_NAMESPACE::mtx_unlock(&timed_mutex), int(thrd_success));
+
+ LIBC_NAMESPACE::mtx_destroy(&timed_mutex);
+}
+
TEST_MAIN() {
relay_counter();
wait_and_step();
recursive_mutex_test();
multiple_waiters();
trylock_test();
+ timedlock_test();
return 0;
}
More information about the libc-commits
mailing list