[libc-commits] [libc] [libc] add posix_mutex_trylock support (PR #191531)

Schrodinger ZHU Yifan via libc-commits libc-commits at lists.llvm.org
Mon Apr 13 06:54:51 PDT 2026


https://github.com/SchrodingerZhu updated https://github.com/llvm/llvm-project/pull/191531

>From 7bfb769b1ba3499527ac643db452392cb8e99176 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Fri, 10 Apr 2026 17:32:59 -0400
Subject: [PATCH 1/2] [libc] add posix_mutex_trylock support

---
 libc/config/linux/aarch64/entrypoints.txt     |  1 +
 libc/config/linux/riscv/entrypoints.txt       |  1 +
 libc/config/linux/x86_64/entrypoints.txt      |  1 +
 libc/include/pthread.yaml                     |  4 +++
 libc/src/pthread/CMakeLists.txt               | 11 ++++++++
 libc/src/pthread/pthread_mutex_trylock.cpp    | 26 +++++++++++++++++++
 libc/src/pthread/pthread_mutex_trylock.h      | 21 +++++++++++++++
 .../integration/src/pthread/CMakeLists.txt    |  1 +
 .../src/pthread/pthread_mutex_test.cpp        | 16 ++++++++++++
 9 files changed, 82 insertions(+)
 create mode 100644 libc/src/pthread/pthread_mutex_trylock.cpp
 create mode 100644 libc/src/pthread/pthread_mutex_trylock.h

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 72836d031c0e8..1ef6eaa6a2722 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1010,6 +1010,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_trylock
     libc.src.pthread.pthread_mutex_unlock
     libc.src.pthread.pthread_mutexattr_destroy
     libc.src.pthread.pthread_mutexattr_getpshared
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 2038b776e539b..91620ea4cb93f 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1143,6 +1143,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_trylock
     libc.src.pthread.pthread_mutex_unlock
     libc.src.pthread.pthread_mutexattr_destroy
     libc.src.pthread.pthread_mutexattr_getpshared
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index b43ec51d1ece8..3aa4da495a50e 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1201,6 +1201,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_trylock
     libc.src.pthread.pthread_mutex_unlock
     libc.src.pthread.pthread_mutexattr_destroy
     libc.src.pthread.pthread_mutexattr_getpshared
diff --git a/libc/include/pthread.yaml b/libc/include/pthread.yaml
index cfbd726fa1f0c..782a37c8ffad2 100644
--- a/libc/include/pthread.yaml
+++ b/libc/include/pthread.yaml
@@ -221,6 +221,10 @@ functions:
     return_type: int
     arguments:
       - type: pthread_mutex_t *
+  - name: pthread_mutex_trylock
+    return_type: int
+    arguments:
+      - type: pthread_mutex_t *
   - name: pthread_mutex_unlock
     return_type: int
     arguments:
diff --git a/libc/src/pthread/CMakeLists.txt b/libc/src/pthread/CMakeLists.txt
index 5e41dbdf1c84f..943b29d99f9ee 100644
--- a/libc/src/pthread/CMakeLists.txt
+++ b/libc/src/pthread/CMakeLists.txt
@@ -361,6 +361,17 @@ add_entrypoint_object(
     libc.src.__support.threads.mutex
 )
 
+add_entrypoint_object(
+  pthread_mutex_trylock
+  SRCS
+    pthread_mutex_trylock.cpp
+  HDRS
+    pthread_mutex_trylock.h
+  DEPENDS
+    libc.include.pthread
+    libc.src.__support.threads.mutex
+)
+
 add_entrypoint_object(
   pthread_mutex_unlock
   SRCS
diff --git a/libc/src/pthread/pthread_mutex_trylock.cpp b/libc/src/pthread/pthread_mutex_trylock.cpp
new file mode 100644
index 0000000000000..1920a49f2a8d3
--- /dev/null
+++ b/libc/src/pthread/pthread_mutex_trylock.cpp
@@ -0,0 +1,26 @@
+//===-- Linux implementation of the pthread_mutex_trylock 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 "pthread_mutex_trylock.h"
+
+#include "hdr/errno_macros.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/threads/mutex.h"
+#include "src/__support/threads/mutex_common.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+// The implementation currently handles only plain mutexes.
+LLVM_LIBC_FUNCTION(int, pthread_mutex_trylock, (pthread_mutex_t * mutex)) {
+  if (reinterpret_cast<Mutex *>(mutex)->try_lock() == MutexError::BUSY)
+    return EBUSY;
+  return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/pthread/pthread_mutex_trylock.h b/libc/src/pthread/pthread_mutex_trylock.h
new file mode 100644
index 0000000000000..0a826da2e8a10
--- /dev/null
+++ b/libc/src/pthread/pthread_mutex_trylock.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for pthread_mutex_trylock 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEX_TRYLOCK_H
+#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEX_TRYLOCK_H
+
+#include "include/llvm-libc-types/pthread_mutex_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int pthread_mutex_trylock(pthread_mutex_t *mutex);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEX_TRYLOCK_H
diff --git a/libc/test/integration/src/pthread/CMakeLists.txt b/libc/test/integration/src/pthread/CMakeLists.txt
index 62f6ed777e522..1cfac7aadf111 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_trylock
     libc.src.pthread.pthread_mutex_unlock
     libc.src.pthread.pthread_create
     libc.src.pthread.pthread_join
diff --git a/libc/test/integration/src/pthread/pthread_mutex_test.cpp b/libc/test/integration/src/pthread/pthread_mutex_test.cpp
index d482dcc13f443..488954c0ef255 100644
--- a/libc/test/integration/src/pthread/pthread_mutex_test.cpp
+++ b/libc/test/integration/src/pthread/pthread_mutex_test.cpp
@@ -6,12 +6,14 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "hdr/errno_macros.h"
 #include "hdr/stdint_proxy.h" // uintptr_t
 #include "src/pthread/pthread_create.h"
 #include "src/pthread/pthread_join.h"
 #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_trylock.h"
 #include "src/pthread/pthread_mutex_unlock.h"
 #include "test/IntegrationTest/test.h"
 
@@ -128,6 +130,19 @@ void wait_and_step() {
   LIBC_NAMESPACE::pthread_mutex_destroy(&step_lock);
 }
 
+void trylock_test() {
+  pthread_mutex_t trylock_mutex;
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_init(&trylock_mutex, nullptr), 0);
+
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_trylock(&trylock_mutex), 0);
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_trylock(&trylock_mutex), EBUSY);
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_unlock(&trylock_mutex), 0);
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_trylock(&trylock_mutex), 0);
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_unlock(&trylock_mutex), 0);
+
+  LIBC_NAMESPACE::pthread_mutex_destroy(&trylock_mutex);
+}
+
 static constexpr int THREAD_COUNT = 10;
 static pthread_mutex_t multiple_waiter_lock;
 static pthread_mutex_t counter_lock;
@@ -191,6 +206,7 @@ static pthread_mutex_t test_initializer = PTHREAD_MUTEX_INITIALIZER;
 TEST_MAIN() {
   relay_counter();
   wait_and_step();
+  trylock_test();
   multiple_waiters();
   return 0;
 }

>From a2339f5e6250d00150ef305087cd5d788c7858d5 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <i at zhuyi.fan>
Date: Mon, 13 Apr 2026 09:54:27 -0400
Subject: [PATCH 2/2] [NFC] cleanup

---
 libc/src/__support/threads/mutex.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/libc/src/__support/threads/mutex.h b/libc/src/__support/threads/mutex.h
index 25feea891e429..76a656729eef7 100644
--- a/libc/src/__support/threads/mutex.h
+++ b/libc/src/__support/threads/mutex.h
@@ -22,7 +22,7 @@
 //
 // MutexError lock();
 // MutexError trylock();
-// MutexError timedlock(...);
+// MutexError timed_lock(...);
 // MutexError unlock();
 // MutexError reset(); // Used to reset inconsistent robust mutexes.
 //
@@ -54,12 +54,14 @@ namespace LIBC_NAMESPACE_DECL {
 /// complete Mutex locks in general cannot be implemented on the GPU, or on some
 /// baremetal platforms. We simply define the Mutex interface and require that
 /// only a single thread executes code requiring a mutex lock.
+// TODO: declare abstract interface for timed_lock
 struct Mutex {
   LIBC_INLINE constexpr Mutex(bool, bool, bool, bool) {}
 
   LIBC_INLINE MutexError lock() { return MutexError::NONE; }
   LIBC_INLINE MutexError unlock() { return MutexError::NONE; }
   LIBC_INLINE MutexError reset() { return MutexError::NONE; }
+  LIBC_INLINE MutexError trylock() { return MutexError::NONE; }
 };
 
 } // namespace LIBC_NAMESPACE_DECL



More information about the libc-commits mailing list