[libc-commits] [libc] 54fa3c4 - [libc] Implement mtx_timedlock and pthread_mutex_timedlock (#240028)
via libc-commits
libc-commits at lists.llvm.org
Mon Nov 9 19:53:11 PST 2026
Author: Aman Maurya
Date: 2026-11-10T03:53:06Z
New Revision: 54fa3c47aade22484be740383bc9de3dbd47a50f
URL: https://github.com/llvm/llvm-project/commit/54fa3c47aade22484be740383bc9de3dbd47a50f
DIFF: https://github.com/llvm/llvm-project/commit/54fa3c47aade22484be740383bc9de3dbd47a50f.diff
LOG: [libc] Implement mtx_timedlock and pthread_mutex_timedlock (#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.
Added:
libc/src/pthread/pthread_mutex_timedlock.cpp
libc/src/pthread/pthread_mutex_timedlock.h
libc/src/threads/mtx_timedlock.cpp
libc/src/threads/mtx_timedlock.h
Modified:
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/riscv/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/include/pthread.yaml
libc/include/threads.yaml
libc/src/__support/threads/mutex.h
libc/src/pthread/CMakeLists.txt
libc/src/threads/CMakeLists.txt
libc/test/integration/src/pthread/CMakeLists.txt
libc/test/integration/src/pthread/pthread_mutex_test.cpp
libc/test/integration/src/threads/CMakeLists.txt
libc/test/integration/src/threads/mtx_test.cpp
Removed:
################################################################################
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index e8e81121b33f88..42408a6a4113ea 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1268,6 +1268,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
@@ -1464,6 +1465,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 110dac84f9f2c3..220d0f8ae397e4 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1461,6 +1461,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
@@ -1658,6 +1659,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 641c7957009f8f..9a7d37ac0920e6 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1483,6 +1483,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
@@ -1684,6 +1685,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 fd003b25846280..9169748a5c6c78 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..5ac78750764eb3
--- /dev/null
+++ b/libc/src/pthread/pthread_mutex_timedlock.cpp
@@ -0,0 +1,65 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Linux implementation of the pthread_mutex_timedlock function.
+///
+//===----------------------------------------------------------------------===//
+
+#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);
+
+ // According to POSIX.1, CLOCK_REALTIME is used for timedlock.
+ 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;
+ case MutexError::OVERFLOW:
+ return EAGAIN;
+ 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..1899b2634e5aa7
--- /dev/null
+++ b/libc/src/pthread/pthread_mutex_timedlock.h
@@ -0,0 +1,28 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_mutex_timedlock.
+///
+//===----------------------------------------------------------------------===//
+
+#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..4443087012ba73
--- /dev/null
+++ b/libc/src/threads/mtx_timedlock.cpp
@@ -0,0 +1,58 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 mtx_timedlock function.
+///
+//===----------------------------------------------------------------------===//
+
+#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..722d1662f79e1e
--- /dev/null
+++ b/libc/src/threads/mtx_timedlock.h
@@ -0,0 +1,28 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 mtx_timedlock function.
+///
+//===----------------------------------------------------------------------===//
+
+#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 78e5b6ceae0b18..77e245ba83893a 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
@@ -443,6 +444,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