[libc-commits] [libc] [libc] separate raw_rwlock and unix_rwlock to make it internally usable (PR #189773)
Schrodinger ZHU Yifan via libc-commits
libc-commits at lists.llvm.org
Wed Apr 1 07:08:15 PDT 2026
https://github.com/SchrodingerZhu updated https://github.com/llvm/llvm-project/pull/189773
>From 8f4b053f014403dd93058e10a77a707ef7b386f6 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <i at zhuyi.fan>
Date: Tue, 31 Mar 2026 19:58:49 -0400
Subject: [PATCH 1/6] [libc] posix cleanup: rework and enable unix_* locks for
macOS
---
libc/config/darwin/aarch64/entrypoints.txt | 33 +++
libc/config/darwin/x86_64/entrypoints.txt | 33 +++
.../include/llvm-libc-macros/pthread-macros.h | 18 +-
.../llvm-libc-types/pthread_rwlock_t.h | 18 +-
libc/src/__support/threads/CMakeLists.txt | 25 ++
.../__support/threads/linux/CMakeLists.txt | 17 --
.../threads/{linux/rwlock.h => raw_rwlock.h} | 247 +++++-------------
libc/src/__support/threads/unix_rwlock.h | 88 +++++++
libc/src/pthread/CMakeLists.txt | 22 +-
.../pthread/pthread_rwlock_clockrdlock.cpp | 2 +-
.../pthread/pthread_rwlock_clockwrlock.cpp | 2 +-
libc/src/pthread/pthread_rwlock_destroy.cpp | 2 +-
libc/src/pthread/pthread_rwlock_init.cpp | 2 +-
libc/src/pthread/pthread_rwlock_rdlock.cpp | 2 +-
.../pthread/pthread_rwlock_timedrdlock.cpp | 2 +-
.../pthread/pthread_rwlock_timedwrlock.cpp | 2 +-
libc/src/pthread/pthread_rwlock_tryrdlock.cpp | 2 +-
libc/src/pthread/pthread_rwlock_trywrlock.cpp | 2 +-
libc/src/pthread/pthread_rwlock_unlock.cpp | 2 +-
libc/src/pthread/pthread_rwlock_wrlock.cpp | 2 +-
.../integration/src/pthread/CMakeLists.txt | 1 +
.../src/pthread/pthread_rwlock_test.cpp | 20 +-
.../__support/threads/linux/CMakeLists.txt | 10 +
.../threads/linux/raw_rwlock_test.cpp | 47 ++++
24 files changed, 359 insertions(+), 242 deletions(-)
rename libc/src/__support/threads/{linux/rwlock.h => raw_rwlock.h} (66%)
create mode 100644 libc/src/__support/threads/unix_rwlock.h
create mode 100644 libc/test/src/__support/threads/linux/raw_rwlock_test.cpp
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 0888f4b0d922b..312249ca353f0 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -100,6 +100,39 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.realloc
libc.src.stdlib.free
+ # pthread mutex entrypoints
+ libc.src.pthread.pthread_mutex_destroy
+ libc.src.pthread.pthread_mutex_init
+ libc.src.pthread.pthread_mutex_lock
+ libc.src.pthread.pthread_mutex_unlock
+ libc.src.pthread.pthread_mutexattr_destroy
+ libc.src.pthread.pthread_mutexattr_getpshared
+ libc.src.pthread.pthread_mutexattr_getrobust
+ libc.src.pthread.pthread_mutexattr_gettype
+ libc.src.pthread.pthread_mutexattr_init
+ libc.src.pthread.pthread_mutexattr_setpshared
+ libc.src.pthread.pthread_mutexattr_setrobust
+ libc.src.pthread.pthread_mutexattr_settype
+
+ # pthread rwlock entrypoints
+ libc.src.pthread.pthread_rwlock_clockrdlock
+ libc.src.pthread.pthread_rwlock_clockwrlock
+ libc.src.pthread.pthread_rwlock_destroy
+ libc.src.pthread.pthread_rwlock_init
+ libc.src.pthread.pthread_rwlock_rdlock
+ libc.src.pthread.pthread_rwlock_timedrdlock
+ libc.src.pthread.pthread_rwlock_timedwrlock
+ libc.src.pthread.pthread_rwlock_tryrdlock
+ libc.src.pthread.pthread_rwlock_trywrlock
+ libc.src.pthread.pthread_rwlock_unlock
+ libc.src.pthread.pthread_rwlock_wrlock
+ libc.src.pthread.pthread_rwlockattr_destroy
+ libc.src.pthread.pthread_rwlockattr_getkind_np
+ libc.src.pthread.pthread_rwlockattr_getpshared
+ libc.src.pthread.pthread_rwlockattr_init
+ libc.src.pthread.pthread_rwlockattr_setkind_np
+ libc.src.pthread.pthread_rwlockattr_setpshared
+
# wctype.h entrypoints
libc.src.wctype.iswalpha
libc.src.wctype.iswgraph
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index 046d1b409742f..7c861d02fc683 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -93,6 +93,39 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.calloc
libc.src.stdlib.realloc
libc.src.stdlib.free
+
+ # pthread mutex entrypoints
+ libc.src.pthread.pthread_mutex_destroy
+ libc.src.pthread.pthread_mutex_init
+ libc.src.pthread.pthread_mutex_lock
+ libc.src.pthread.pthread_mutex_unlock
+ libc.src.pthread.pthread_mutexattr_destroy
+ libc.src.pthread.pthread_mutexattr_getpshared
+ libc.src.pthread.pthread_mutexattr_getrobust
+ libc.src.pthread.pthread_mutexattr_gettype
+ libc.src.pthread.pthread_mutexattr_init
+ libc.src.pthread.pthread_mutexattr_setpshared
+ libc.src.pthread.pthread_mutexattr_setrobust
+ libc.src.pthread.pthread_mutexattr_settype
+
+ # pthread rwlock entrypoints
+ libc.src.pthread.pthread_rwlock_clockrdlock
+ libc.src.pthread.pthread_rwlock_clockwrlock
+ libc.src.pthread.pthread_rwlock_destroy
+ libc.src.pthread.pthread_rwlock_init
+ libc.src.pthread.pthread_rwlock_rdlock
+ libc.src.pthread.pthread_rwlock_timedrdlock
+ libc.src.pthread.pthread_rwlock_timedwrlock
+ libc.src.pthread.pthread_rwlock_tryrdlock
+ libc.src.pthread.pthread_rwlock_trywrlock
+ libc.src.pthread.pthread_rwlock_unlock
+ libc.src.pthread.pthread_rwlock_wrlock
+ libc.src.pthread.pthread_rwlockattr_destroy
+ libc.src.pthread.pthread_rwlockattr_getkind_np
+ libc.src.pthread.pthread_rwlockattr_getpshared
+ libc.src.pthread.pthread_rwlockattr_init
+ libc.src.pthread.pthread_rwlockattr_setkind_np
+ libc.src.pthread.pthread_rwlockattr_setpshared
)
set(TARGET_LIBM_ENTRYPOINTS
diff --git a/libc/include/llvm-libc-macros/pthread-macros.h b/libc/include/llvm-libc-macros/pthread-macros.h
index bba3faa11c4e2..e6ade6507e070 100644
--- a/libc/include/llvm-libc-macros/pthread-macros.h
+++ b/libc/include/llvm-libc-macros/pthread-macros.h
@@ -47,15 +47,17 @@
#define PTHREAD_RWLOCK_INITIALIZER \
{ \
- /* .__is_pshared = */ 0, \
- /* .__preference = */ 0, \
- /* .__state = */ 0, \
+ /* .__raw = */ { \
+ /* .__is_pshared = */ 0, \
+ /* .__preference = */ 0, \
+ /* .__state = */ 0, \
+ /* .__wait_queue_mutex = */ {0}, \
+ /* .__pending_readers = */ {0}, \
+ /* .__pending_writers = */ {0}, \
+ /* .__reader_serialization = */ {0}, \
+ /* .__writer_serialization = */ {0}, \
+ }, \
/* .__write_tid = */ 0, \
- /* .__wait_queue_mutex = */ {0}, \
- /* .__pending_readers = */ {0}, \
- /* .__pending_writers = */ {0}, \
- /* .__reader_serialization = */ {0}, \
- /* .__writer_serialization = */ {0}, \
}
// glibc extensions
diff --git a/libc/include/llvm-libc-types/pthread_rwlock_t.h b/libc/include/llvm-libc-types/pthread_rwlock_t.h
index 4a7c6c75250a8..38778868409d7 100644
--- a/libc/include/llvm-libc-types/pthread_rwlock_t.h
+++ b/libc/include/llvm-libc-types/pthread_rwlock_t.h
@@ -12,15 +12,17 @@
#include "__futex_word.h"
#include "pid_t.h"
typedef struct {
- unsigned __is_pshared : 1;
- unsigned __preference : 1;
- int __state;
+ struct {
+ unsigned __is_pshared : 1;
+ unsigned __preference : 1;
+ int __state;
+ __futex_word __wait_queue_mutex;
+ __futex_word __pending_readers;
+ __futex_word __pending_writers;
+ __futex_word __reader_serialization;
+ __futex_word __writer_serialization;
+ } __raw;
pid_t __writer_tid;
- __futex_word __wait_queue_mutex;
- __futex_word __pending_readers;
- __futex_word __pending_writers;
- __futex_word __reader_serialization;
- __futex_word __writer_serialization;
} pthread_rwlock_t;
#endif // LLVM_LIBC_TYPES_PTHREAD_RWLOCK_T_H
diff --git a/libc/src/__support/threads/CMakeLists.txt b/libc/src/__support/threads/CMakeLists.txt
index dc1445634da2b..317c7fa296658 100644
--- a/libc/src/__support/threads/CMakeLists.txt
+++ b/libc/src/__support/threads/CMakeLists.txt
@@ -24,6 +24,8 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
endif()
if(TARGET libc.src.__support.threads.${LIBC_TARGET_OS}.futex_utils)
+ libc_set_definition(rwlock_default_spin_count "LIBC_COPT_RWLOCK_DEFAULT_SPIN_COUNT=${LIBC_CONF_RWLOCK_DEFAULT_SPIN_COUNT}")
+
add_header_library(
raw_mutex
HDRS
@@ -39,6 +41,29 @@ if(TARGET libc.src.__support.threads.${LIBC_TARGET_OS}.futex_utils)
libc.hdr.types.pid_t
)
+ add_header_library(
+ raw_rwlock
+ HDRS
+ raw_rwlock.h
+ COMPILE_OPTIONS
+ ${rwlock_default_spin_count}
+ ${monotonicity_flags}
+ DEPENDS
+ .raw_mutex
+ libc.src.__support.common
+ libc.src.__support.CPP.limits
+ )
+
+ add_header_library(
+ unix_rwlock
+ HDRS
+ unix_rwlock.h
+ DEPENDS
+ .raw_rwlock
+ libc.hdr.types.pid_t
+ libc.src.__support.threads.identifier
+ )
+
add_header_library(
unix_mutex
HDRS
diff --git a/libc/src/__support/threads/linux/CMakeLists.txt b/libc/src/__support/threads/linux/CMakeLists.txt
index 9d292003f21cb..5ada10ed04b88 100644
--- a/libc/src/__support/threads/linux/CMakeLists.txt
+++ b/libc/src/__support/threads/linux/CMakeLists.txt
@@ -31,23 +31,6 @@ else()
libc_set_definition(monotonicity_flags LIBC_COPT_TIMEOUT_ENSURE_MONOTONICITY=0)
endif()
-libc_set_definition(rwlock_default_spin_count "LIBC_COPT_RWLOCK_DEFAULT_SPIN_COUNT=${LIBC_CONF_RWLOCK_DEFAULT_SPIN_COUNT}")
-add_header_library(
- rwlock
- HDRS
- rwlock.h
- COMPILE_OPTIONS
- ${rwlock_default_spin_count}
- ${monotonicity_flags}
- DEPENDS
- .futex_utils
- libc.src.__support.threads.raw_mutex
- libc.src.__support.common
- libc.src.__support.OSUtil.osutil
- libc.src.__support.CPP.limits
- libc.src.__support.threads.identifier
-)
-
add_object_library(
thread
SRCS
diff --git a/libc/src/__support/threads/linux/rwlock.h b/libc/src/__support/threads/raw_rwlock.h
similarity index 66%
rename from libc/src/__support/threads/linux/rwlock.h
rename to libc/src/__support/threads/raw_rwlock.h
index 9fb3ff972b588..570757209d1d3 100644
--- a/libc/src/__support/threads/linux/rwlock.h
+++ b/libc/src/__support/threads/raw_rwlock.h
@@ -1,27 +1,22 @@
-//===--- Implementation of a Linux RwLock class ---------------*- C++ -*-===//
+//===--- Implementation of the RawRwLock class -----------------*- 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_SUPPORT_THREADS_LINUX_RWLOCK_H
-#define LLVM_LIBC_SRC_SUPPORT_THREADS_LINUX_RWLOCK_H
+#ifndef LLVM_LIBC_SRC___SUPPORT_THREADS_RAW_RWLOCK_H
+#define LLVM_LIBC_SRC___SUPPORT_THREADS_RAW_RWLOCK_H
#include "hdr/errno_macros.h"
-#include "hdr/types/pid_t.h"
#include "src/__support/CPP/atomic.h"
#include "src/__support/CPP/limits.h"
#include "src/__support/CPP/optional.h"
-#include "src/__support/OSUtil/syscall.h"
#include "src/__support/common.h"
#include "src/__support/libc_assert.h"
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/optimization.h"
-#include "src/__support/threads/identifier.h"
-#include "src/__support/threads/linux/futex_utils.h"
-#include "src/__support/threads/linux/futex_word.h"
#include "src/__support/threads/raw_mutex.h"
#include "src/__support/threads/sleep.h"
@@ -39,28 +34,28 @@
#endif
namespace LIBC_NAMESPACE_DECL {
-// Forward declaration of the RwLock class.
-class RwLock;
-// A namespace to rwlock specific utilities.
namespace rwlock {
-// The role of the thread in the RwLock.
+
enum class Role { Reader = 0, Writer = 1 };
-// A waiting queue to keep track of the pending readers and writers.
+enum class LockResult : int {
+ Success = 0,
+ TimedOut = ETIMEDOUT,
+ Overflow = EAGAIN,
+ Busy = EBUSY,
+ Deadlock = EDEADLOCK,
+ PermissionDenied = EPERM,
+};
+
class WaitingQueue final : private RawMutex {
/* FutexWordType raw_mutex; (from base class) */
- // Pending reader count (protected by the mutex)
FutexWordType pending_readers;
- // Pending writer count (protected by the mutex)
FutexWordType pending_writers;
- // Reader serialization (increases on each reader-waking operation)
Futex reader_serialization;
- // Writer serialization (increases on each writer-waking operation)
Futex writer_serialization;
public:
- // RAII guard to lock and unlock the waiting queue.
class Guard {
WaitingQueue &queue;
bool is_pshared;
@@ -114,28 +109,13 @@ class WaitingQueue final : private RawMutex {
}
};
-// The RwState of the RwLock is stored in an integer word, consisting of the
-// following components:
-// -----------------------------------------------
-// | Range | Description |
-// ===============================================
-// | 0 | Pending Reader Bit |
-// -----------------------------------------------
-// | 1 | Pending Writer Bit |
-// -----------------------------------------------
-// | [2, MSB) | Active Reader Count |
-// -----------------------------------------------
-// | MSB | Active Writer Bit |
-// -----------------------------------------------
class RwState {
- // Shift amounts to access the components of the state.
LIBC_INLINE_VAR static constexpr int PENDING_READER_SHIFT = 0;
LIBC_INLINE_VAR static constexpr int PENDING_WRITER_SHIFT = 1;
LIBC_INLINE_VAR static constexpr int ACTIVE_READER_SHIFT = 2;
LIBC_INLINE_VAR static constexpr int ACTIVE_WRITER_SHIFT =
cpp::numeric_limits<int>::digits;
- // Bitmasks to access the components of the state.
LIBC_INLINE_VAR static constexpr int PENDING_READER_BIT =
1 << PENDING_READER_SHIFT;
LIBC_INLINE_VAR static constexpr int PENDING_WRITER_BIT =
@@ -148,16 +128,12 @@ class RwState {
PENDING_READER_BIT | PENDING_WRITER_BIT;
private:
- // We use the signed integer as the state type. It is easier
- // to reason about the state transitions using signness.
int state;
public:
- // Construction and conversion functions.
LIBC_INLINE constexpr RwState(int state = 0) : state(state) {}
LIBC_INLINE constexpr operator int() const { return state; }
- // Utilities to check the state of the RwLock.
LIBC_INLINE constexpr bool has_active_writer() const { return state < 0; }
LIBC_INLINE constexpr bool has_active_reader() const {
return state >= ACTIVE_READER_COUNT_UNIT;
@@ -171,18 +147,12 @@ class RwState {
LIBC_INLINE constexpr bool has_pending_writer() const {
return state & PENDING_WRITER_BIT;
}
- LIBC_INLINE constexpr bool has_pending() const {
- return state & PENDING_MASK;
- }
+ LIBC_INLINE constexpr bool has_pending() const { return state & PENDING_MASK; }
LIBC_INLINE constexpr RwState set_writer_bit() const {
return RwState(state | ACTIVE_WRITER_BIT);
}
- // The preference parameter changes the behavior of the lock acquisition
- // if there are both readers and writers waiting for the lock. If writers
- // are preferred, reader acquisition will be blocked until all pending
- // writers are served.
template <Role role> LIBC_INLINE bool can_acquire(Role preference) const {
if constexpr (role == Role::Reader) {
switch (preference) {
@@ -192,12 +162,11 @@ class RwState {
return !has_active_writer() && !has_pending_writer();
}
__builtin_unreachable();
- } else
+ } else {
return !has_active_owner();
+ }
}
- // This function check if it is possible to grow the reader count without
- // overflowing the state.
LIBC_INLINE cpp::optional<RwState> try_increase_reader_count() const {
LIBC_ASSERT(!has_active_writer() &&
"try_increase_reader_count shall only be called when there "
@@ -209,7 +178,6 @@ class RwState {
return res;
}
- // Utilities to do atomic operations on the state.
LIBC_INLINE static RwState fetch_sub_reader_count(cpp::Atomic<int> &target,
cpp::MemoryOrder order) {
return RwState(target.fetch_sub(ACTIVE_READER_COUNT_UNIT, order));
@@ -228,6 +196,7 @@ class RwState {
else
return RwState(target.fetch_or(PENDING_WRITER_BIT, order));
}
+
template <Role role>
LIBC_INLINE static RwState fetch_clear_pending_bit(cpp::Atomic<int> &target,
cpp::MemoryOrder order) {
@@ -250,7 +219,6 @@ class RwState {
failure_order);
}
- // Utilities to spin and reload the state.
private:
template <class F>
LIBC_INLINE static RwState spin_reload_until(cpp::Atomic<int> &target,
@@ -269,8 +237,6 @@ class RwState {
LIBC_INLINE static RwState spin_reload(cpp::Atomic<int> &target,
Role preference, unsigned spin_count) {
if constexpr (role == Role::Reader) {
- // Return the reader state if either the lock is available or there is
- // any ongoing contention.
return spin_reload_until(
target,
[=](RwState state) {
@@ -279,9 +245,6 @@ class RwState {
},
spin_count);
} else {
- // Return the writer state if either the lock is available or there is
- // any contention *between writers*. Since writers can be way less than
- // readers, we allow them to spin more to improve the fairness.
return spin_reload_until(
target,
[=](RwState state) {
@@ -294,46 +257,26 @@ class RwState {
friend class RwLockTester;
};
+
} // namespace rwlock
-class RwLock {
+class RawRwLock {
using RwState = rwlock::RwState;
- using Role = rwlock::Role;
using WaitingQueue = rwlock::WaitingQueue;
public:
- // Return types for the lock functions.
- // All the locking routines returning this type are marked as [[nodiscard]]
- // because it is a common error to assume the lock success without checking
- // the return value, which can lead to undefined behaviors or other subtle
- // bugs that are hard to reason about.
- enum class LockResult : int {
- Success = 0,
- TimedOut = ETIMEDOUT,
- Overflow = EAGAIN, /* EAGAIN is specified in the standard for overflow. */
- Busy = EBUSY,
- Deadlock = EDEADLOCK,
- PermissionDenied = EPERM,
- };
+ using LockResult = rwlock::LockResult;
+ using Role = rwlock::Role;
private:
- // Whether the RwLock is shared between processes.
LIBC_PREFERED_TYPE(bool)
unsigned is_pshared : 1;
- // Reader/Writer preference.
LIBC_PREFERED_TYPE(Role)
unsigned preference : 1;
- // RwState to keep track of the RwLock.
cpp::Atomic<int> state;
- // writer_tid is used to keep track of the thread id of the writer. Notice
- // that TLS address is not a good idea here since it may remains the same
- // across forked processes.
- cpp::Atomic<pid_t> writer_tid;
- // Waiting queue to keep track of the readers and writers.
WaitingQueue queue;
private:
- // Load the bitfield preference.
LIBC_INLINE Role get_preference() const {
return static_cast<Role>(preference);
}
@@ -348,147 +291,65 @@ class RwLock {
state, *next, cpp::MemoryOrder::ACQUIRE,
cpp::MemoryOrder::RELAXED)))
return LockResult::Success;
- // Notice that old is updated by the compare_exchange_weak_with
- // function.
}
return LockResult::Busy;
} else {
- // This while loop should terminate quickly
while (LIBC_LIKELY(old.can_acquire<Role::Writer>(get_preference()))) {
if (LIBC_LIKELY(old.compare_exchange_weak_with(
state, old.set_writer_bit(), cpp::MemoryOrder::ACQUIRE,
- cpp::MemoryOrder::RELAXED))) {
- writer_tid.store(internal::gettid(), cpp::MemoryOrder::RELAXED);
+ cpp::MemoryOrder::RELAXED)))
return LockResult::Success;
- }
- // Notice that old is updated by the compare_exchange_weak_with
- // function.
}
return LockResult::Busy;
}
}
-public:
- LIBC_INLINE constexpr RwLock(Role preference = Role::Reader,
- bool is_pshared = false)
- : is_pshared(is_pshared),
- preference(static_cast<unsigned>(preference) & 1u), state(0),
- writer_tid(0), queue() {}
-
- [[nodiscard]]
- LIBC_INLINE LockResult try_read_lock() {
- RwState old = RwState::load(state, cpp::MemoryOrder::RELAXED);
- return try_lock<Role::Reader>(old);
- }
- [[nodiscard]]
- LIBC_INLINE LockResult try_write_lock() {
- RwState old = RwState::load(state, cpp::MemoryOrder::RELAXED);
- return try_lock<Role::Writer>(old);
- }
-
-private:
template <Role role>
LIBC_INLINE LockResult
lock_slow(cpp::optional<Futex::Timeout> timeout = cpp::nullopt,
unsigned spin_count = LIBC_COPT_RWLOCK_DEFAULT_SPIN_COUNT) {
- // Phase 1: deadlock detection.
- // A deadlock happens if this is a RAW/WAW lock in the same thread.
- if (writer_tid.load(cpp::MemoryOrder::RELAXED) == internal::gettid())
- return LockResult::Deadlock;
-
#if LIBC_COPT_TIMEOUT_ENSURE_MONOTONICITY
- // Phase 2: convert the timeout if necessary.
if (timeout)
ensure_monotonicity(*timeout);
#endif
- // Phase 3: spin to get the initial state. We ignore the timing due to
- // spin since it should end quickly.
RwState old =
RwState::spin_reload<role>(state, get_preference(), spin_count);
- // Enter the main acquisition loop.
for (;;) {
- // Phase 4: if the lock can be acquired, try to acquire it.
LockResult result = try_lock<role>(old);
if (result != LockResult::Busy)
return result;
- // Phase 5: register ourselves as a reader.
int serial_number;
{
- // The queue need to be protected by a mutex since the operations in
- // this block must be executed as a whole transaction. It is possible
- // that this lock will make the timeout imprecise, but this is the
- // best we can do. The transaction is small and everyone should make
- // progress rather quickly.
WaitingQueue::Guard guard = queue.acquire(is_pshared);
guard.template pending_count<role>()++;
-
- // Use atomic operation to guarantee the total order of the operations
- // on the state. The pending flag update should be visible to any
- // succeeding unlock events. Or, if a unlock does happen before we
- // sleep on the futex, we can avoid such waiting.
old = RwState::fetch_set_pending_bit<role>(state,
cpp::MemoryOrder::RELAXED);
- // no need to use atomic since it is already protected by the mutex.
serial_number = guard.serialization<role>();
}
- // Phase 6: do futex wait until the lock is available or timeout is
- // reached.
bool timeout_flag = false;
if (!old.can_acquire<role>(get_preference()))
timeout_flag = (queue.wait<role>(serial_number, timeout, is_pshared) ==
-ETIMEDOUT);
- // Phase 7: unregister ourselves as a pending reader/writer.
{
- // Similarly, the unregister operation should also be an atomic
- // transaction.
WaitingQueue::Guard guard = queue.acquire(is_pshared);
guard.pending_count<role>()--;
- // Clear the flag if we are the last reader. The flag must be
- // cleared otherwise operations like trylock may fail even though
- // there is no competitors.
if (guard.pending_count<role>() == 0)
RwState::fetch_clear_pending_bit<role>(state,
cpp::MemoryOrder::RELAXED);
}
- // Phase 8: exit the loop is timeout is reached.
if (timeout_flag)
return LockResult::TimedOut;
- // Phase 9: reload the state and retry the acquisition.
old = RwState::spin_reload<role>(state, get_preference(), spin_count);
}
}
-public:
- [[nodiscard]]
- LIBC_INLINE LockResult
- read_lock(cpp::optional<Futex::Timeout> timeout = cpp::nullopt,
- unsigned spin_count = LIBC_COPT_RWLOCK_DEFAULT_SPIN_COUNT) {
- LockResult result = try_read_lock();
- if (LIBC_LIKELY(result != LockResult::Busy))
- return result;
- return lock_slow<Role::Reader>(timeout, spin_count);
- }
- [[nodiscard]]
- LIBC_INLINE LockResult
- write_lock(cpp::optional<Futex::Timeout> timeout = cpp::nullopt,
- unsigned spin_count = LIBC_COPT_RWLOCK_DEFAULT_SPIN_COUNT) {
- LockResult result = try_write_lock();
- if (LIBC_LIKELY(result != LockResult::Busy))
- return result;
- return lock_slow<Role::Writer>(timeout, spin_count);
- }
-
-private:
- // Compiler (clang 19.0) somehow decides that this function may be inlined,
- // which leads to a larger unlock function that is infeasible to be inlined.
- // Since notifcation routine is colder we mark it as noinline explicitly.
[[gnu::noinline]]
LIBC_INLINE void notify_pending_threads() {
enum class WakeTarget { Readers, Writers, None };
@@ -502,8 +363,9 @@ class RwLock {
} else if (guard.pending_count<Role::Reader>() != 0) {
guard.serialization<Role::Reader>()++;
status = WakeTarget::Readers;
- } else
+ } else {
status = WakeTarget::None;
+ }
}
if (status == WakeTarget::Readers)
@@ -513,38 +375,68 @@ class RwLock {
}
public:
+ LIBC_INLINE bool has_active_writer() {
+ return RwState::load(state, cpp::MemoryOrder::RELAXED).has_active_writer();
+ }
+
+ LIBC_INLINE constexpr RawRwLock(Role preference = Role::Reader,
+ bool is_pshared = false)
+ : is_pshared(is_pshared),
+ preference(static_cast<unsigned>(preference) & 1u), state(0),
+ queue() {}
+
+ [[nodiscard]]
+ LIBC_INLINE LockResult try_read_lock() {
+ RwState old = RwState::load(state, cpp::MemoryOrder::RELAXED);
+ return try_lock<Role::Reader>(old);
+ }
+
+ [[nodiscard]]
+ LIBC_INLINE LockResult try_write_lock() {
+ RwState old = RwState::load(state, cpp::MemoryOrder::RELAXED);
+ return try_lock<Role::Writer>(old);
+ }
+
+ [[nodiscard]]
+ LIBC_INLINE LockResult
+ read_lock(cpp::optional<Futex::Timeout> timeout = cpp::nullopt,
+ unsigned spin_count = LIBC_COPT_RWLOCK_DEFAULT_SPIN_COUNT) {
+ LockResult result = try_read_lock();
+ if (LIBC_LIKELY(result != LockResult::Busy))
+ return result;
+ return lock_slow<Role::Reader>(timeout, spin_count);
+ }
+
+ [[nodiscard]]
+ LIBC_INLINE LockResult
+ write_lock(cpp::optional<Futex::Timeout> timeout = cpp::nullopt,
+ unsigned spin_count = LIBC_COPT_RWLOCK_DEFAULT_SPIN_COUNT) {
+ LockResult result = try_write_lock();
+ if (LIBC_LIKELY(result != LockResult::Busy))
+ return result;
+ return lock_slow<Role::Writer>(timeout, spin_count);
+ }
+
[[nodiscard]]
LIBC_INLINE LockResult unlock() {
RwState old = RwState::load(state, cpp::MemoryOrder::RELAXED);
if (old.has_active_writer()) {
- // The lock is held by a writer.
- // Check if we are the owner of the lock.
- if (writer_tid.load(cpp::MemoryOrder::RELAXED) != internal::gettid())
- return LockResult::PermissionDenied;
- // clear writer tid.
- writer_tid.store(0, cpp::MemoryOrder::RELAXED);
- // clear the writer bit.
old =
RwState::fetch_clear_active_writer(state, cpp::MemoryOrder::RELEASE);
- // If there is no pending readers or writers, we are done.
if (!old.has_pending())
return LockResult::Success;
} else if (old.has_active_reader()) {
- // The lock is held by readers.
- // Decrease the reader count.
old = RwState::fetch_sub_reader_count(state, cpp::MemoryOrder::RELEASE);
- // If there is no pending readers or writers, we are done.
if (!old.has_last_reader() || !old.has_pending())
return LockResult::Success;
- } else
+ } else {
return LockResult::PermissionDenied;
+ }
notify_pending_threads();
return LockResult::Success;
}
- // We do not allocate any special resources for the RwLock, so this function
- // will only check if the lock is currently held by any thread.
[[nodiscard]]
LIBC_INLINE LockResult check_for_destroy() {
RwState old = RwState::load(state, cpp::MemoryOrder::RELAXED);
@@ -553,6 +445,7 @@ class RwLock {
return LockResult::Success;
}
};
+
} // namespace LIBC_NAMESPACE_DECL
-#endif // LLVM_LIBC_SRC_SUPPORT_THREADS_LINUX_RWLOCK_H
+#endif // LLVM_LIBC_SRC___SUPPORT_THREADS_RAW_RWLOCK_H
diff --git a/libc/src/__support/threads/unix_rwlock.h b/libc/src/__support/threads/unix_rwlock.h
new file mode 100644
index 0000000000000..92efee4a4f033
--- /dev/null
+++ b/libc/src/__support/threads/unix_rwlock.h
@@ -0,0 +1,88 @@
+//===--- Implementation of a Unix RwLock class -----------------*- 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___SUPPORT_THREADS_UNIX_RWLOCK_H
+#define LLVM_LIBC_SRC___SUPPORT_THREADS_UNIX_RWLOCK_H
+
+#include "hdr/types/pid_t.h"
+#include "src/__support/common.h"
+#include "src/__support/CPP/atomic.h"
+#include "src/__support/threads/identifier.h"
+#include "src/__support/threads/raw_rwlock.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+class RwLock final {
+ RawRwLock raw;
+ cpp::Atomic<pid_t> writer_tid;
+
+ LIBC_INLINE pid_t get_writer_tid() {
+ return writer_tid.load(cpp::MemoryOrder::RELAXED);
+ }
+
+ LIBC_INLINE void set_writer_tid(pid_t tid) {
+ writer_tid.store(tid, cpp::MemoryOrder::RELAXED);
+ }
+
+public:
+ using LockResult = rwlock::LockResult;
+ using Role = rwlock::Role;
+
+ LIBC_INLINE constexpr RwLock(Role preference = Role::Reader,
+ bool is_pshared = false)
+ : raw(preference, is_pshared), writer_tid(0) {}
+
+ [[nodiscard]] LIBC_INLINE LockResult try_read_lock() {
+ return raw.try_read_lock();
+ }
+
+ [[nodiscard]] LIBC_INLINE LockResult try_write_lock() {
+ LockResult result = raw.try_write_lock();
+ if (result == LockResult::Success)
+ set_writer_tid(internal::gettid());
+ return result;
+ }
+
+ [[nodiscard]]
+ LIBC_INLINE LockResult
+ read_lock(cpp::optional<Futex::Timeout> timeout = cpp::nullopt,
+ unsigned spin_count = LIBC_COPT_RWLOCK_DEFAULT_SPIN_COUNT) {
+ if (get_writer_tid() == internal::gettid())
+ return LockResult::Deadlock;
+ return raw.read_lock(timeout, spin_count);
+ }
+
+ [[nodiscard]]
+ LIBC_INLINE LockResult
+ write_lock(cpp::optional<Futex::Timeout> timeout = cpp::nullopt,
+ unsigned spin_count = LIBC_COPT_RWLOCK_DEFAULT_SPIN_COUNT) {
+ if (get_writer_tid() == internal::gettid())
+ return LockResult::Deadlock;
+
+ LockResult result = raw.write_lock(timeout, spin_count);
+ if (result == LockResult::Success)
+ set_writer_tid(internal::gettid());
+ return result;
+ }
+
+ [[nodiscard]] LIBC_INLINE LockResult unlock() {
+ bool is_writer_unlock = raw.has_active_writer();
+ LockResult result = raw.unlock();
+ if (is_writer_unlock && result == LockResult::Success)
+ set_writer_tid(0);
+ return result;
+ }
+
+ [[nodiscard]] LIBC_INLINE LockResult check_for_destroy() {
+ return raw.check_for_destroy();
+ }
+};
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_THREADS_UNIX_RWLOCK_H
diff --git a/libc/src/pthread/CMakeLists.txt b/libc/src/pthread/CMakeLists.txt
index d7f03c8f18d23..5e41dbdf1c84f 100644
--- a/libc/src/pthread/CMakeLists.txt
+++ b/libc/src/pthread/CMakeLists.txt
@@ -591,7 +591,7 @@ add_entrypoint_object(
pthread_rwlock_init.h
DEPENDS
libc.include.pthread
- libc.src.__support.threads.linux.rwlock
+ libc.src.__support.threads.unix_rwlock
libc.src.__support.CPP.new
)
@@ -603,7 +603,7 @@ add_entrypoint_object(
pthread_rwlock_tryrdlock.h
DEPENDS
libc.include.pthread
- libc.src.__support.threads.linux.rwlock
+ libc.src.__support.threads.unix_rwlock
)
add_entrypoint_object(
@@ -614,7 +614,7 @@ add_entrypoint_object(
pthread_rwlock_trywrlock.h
DEPENDS
libc.include.pthread
- libc.src.__support.threads.linux.rwlock
+ libc.src.__support.threads.unix_rwlock
libc.src.errno.errno
)
@@ -626,7 +626,7 @@ add_entrypoint_object(
pthread_rwlock_clockrdlock.h
DEPENDS
libc.include.pthread
- libc.src.__support.threads.linux.rwlock
+ libc.src.__support.threads.unix_rwlock
)
add_entrypoint_object(
@@ -637,7 +637,7 @@ add_entrypoint_object(
pthread_rwlock_clockwrlock.h
DEPENDS
libc.include.pthread
- libc.src.__support.threads.linux.rwlock
+ libc.src.__support.threads.unix_rwlock
)
add_entrypoint_object(
@@ -648,7 +648,7 @@ add_entrypoint_object(
pthread_rwlock_timedrdlock.h
DEPENDS
libc.include.pthread
- libc.src.__support.threads.linux.rwlock
+ libc.src.__support.threads.unix_rwlock
libc.src.errno.errno
)
@@ -660,7 +660,7 @@ add_entrypoint_object(
pthread_rwlock_timedwrlock.h
DEPENDS
libc.include.pthread
- libc.src.__support.threads.linux.rwlock
+ libc.src.__support.threads.unix_rwlock
)
add_entrypoint_object(
@@ -671,7 +671,7 @@ add_entrypoint_object(
pthread_rwlock_rdlock.h
DEPENDS
libc.include.pthread
- libc.src.__support.threads.linux.rwlock
+ libc.src.__support.threads.unix_rwlock
)
add_entrypoint_object(
@@ -682,7 +682,7 @@ add_entrypoint_object(
pthread_rwlock_wrlock.h
DEPENDS
libc.include.pthread
- libc.src.__support.threads.linux.rwlock
+ libc.src.__support.threads.unix_rwlock
)
add_entrypoint_object(
@@ -693,7 +693,7 @@ add_entrypoint_object(
pthread_rwlock_unlock.h
DEPENDS
libc.include.pthread
- libc.src.__support.threads.linux.rwlock
+ libc.src.__support.threads.unix_rwlock
libc.src.errno.errno
)
@@ -705,7 +705,7 @@ add_entrypoint_object(
pthread_rwlock_destroy.h
DEPENDS
libc.include.pthread
- libc.src.__support.threads.linux.rwlock
+ libc.src.__support.threads.unix_rwlock
)
add_entrypoint_object(
diff --git a/libc/src/pthread/pthread_rwlock_clockrdlock.cpp b/libc/src/pthread/pthread_rwlock_clockrdlock.cpp
index 1e44e6d7694f6..2a15ce443aaca 100644
--- a/libc/src/pthread/pthread_rwlock_clockrdlock.cpp
+++ b/libc/src/pthread/pthread_rwlock_clockrdlock.cpp
@@ -11,7 +11,7 @@
#include "hdr/errno_macros.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
-#include "src/__support/threads/linux/rwlock.h"
+#include "src/__support/threads/unix_rwlock.h"
#include <pthread.h>
diff --git a/libc/src/pthread/pthread_rwlock_clockwrlock.cpp b/libc/src/pthread/pthread_rwlock_clockwrlock.cpp
index 787a1b1484df7..7d9613fbeb665 100644
--- a/libc/src/pthread/pthread_rwlock_clockwrlock.cpp
+++ b/libc/src/pthread/pthread_rwlock_clockwrlock.cpp
@@ -11,7 +11,7 @@
#include "hdr/errno_macros.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
-#include "src/__support/threads/linux/rwlock.h"
+#include "src/__support/threads/unix_rwlock.h"
#include "src/__support/time/abs_timeout.h"
#include <pthread.h>
diff --git a/libc/src/pthread/pthread_rwlock_destroy.cpp b/libc/src/pthread/pthread_rwlock_destroy.cpp
index afc5622e54a00..85cf127630fe1 100644
--- a/libc/src/pthread/pthread_rwlock_destroy.cpp
+++ b/libc/src/pthread/pthread_rwlock_destroy.cpp
@@ -10,7 +10,7 @@
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
-#include "src/__support/threads/linux/rwlock.h"
+#include "src/__support/threads/unix_rwlock.h"
#include <pthread.h>
diff --git a/libc/src/pthread/pthread_rwlock_init.cpp b/libc/src/pthread/pthread_rwlock_init.cpp
index dc5424cbc4838..2c840665c99e1 100644
--- a/libc/src/pthread/pthread_rwlock_init.cpp
+++ b/libc/src/pthread/pthread_rwlock_init.cpp
@@ -12,7 +12,7 @@
#include "src/__support/common.h"
#include "src/__support/libc_assert.h"
#include "src/__support/macros/config.h"
-#include "src/__support/threads/linux/rwlock.h"
+#include "src/__support/threads/unix_rwlock.h"
#include <pthread.h>
diff --git a/libc/src/pthread/pthread_rwlock_rdlock.cpp b/libc/src/pthread/pthread_rwlock_rdlock.cpp
index 7dee8eb9a44b1..697a4a8e17654 100644
--- a/libc/src/pthread/pthread_rwlock_rdlock.cpp
+++ b/libc/src/pthread/pthread_rwlock_rdlock.cpp
@@ -10,7 +10,7 @@
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
-#include "src/__support/threads/linux/rwlock.h"
+#include "src/__support/threads/unix_rwlock.h"
#include <pthread.h>
diff --git a/libc/src/pthread/pthread_rwlock_timedrdlock.cpp b/libc/src/pthread/pthread_rwlock_timedrdlock.cpp
index 745da508cf140..6128f39fa359f 100644
--- a/libc/src/pthread/pthread_rwlock_timedrdlock.cpp
+++ b/libc/src/pthread/pthread_rwlock_timedrdlock.cpp
@@ -12,7 +12,7 @@
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/optimization.h"
-#include "src/__support/threads/linux/rwlock.h"
+#include "src/__support/threads/unix_rwlock.h"
#include "src/__support/time/abs_timeout.h"
#include <pthread.h>
diff --git a/libc/src/pthread/pthread_rwlock_timedwrlock.cpp b/libc/src/pthread/pthread_rwlock_timedwrlock.cpp
index 9666fc5b47284..848895e21b353 100644
--- a/libc/src/pthread/pthread_rwlock_timedwrlock.cpp
+++ b/libc/src/pthread/pthread_rwlock_timedwrlock.cpp
@@ -12,7 +12,7 @@
#include "src/__support/libc_assert.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/optimization.h"
-#include "src/__support/threads/linux/rwlock.h"
+#include "src/__support/threads/unix_rwlock.h"
#include "src/__support/time/abs_timeout.h"
#include <pthread.h>
diff --git a/libc/src/pthread/pthread_rwlock_tryrdlock.cpp b/libc/src/pthread/pthread_rwlock_tryrdlock.cpp
index d54b57f056090..206ad92688322 100644
--- a/libc/src/pthread/pthread_rwlock_tryrdlock.cpp
+++ b/libc/src/pthread/pthread_rwlock_tryrdlock.cpp
@@ -10,7 +10,7 @@
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
-#include "src/__support/threads/linux/rwlock.h"
+#include "src/__support/threads/unix_rwlock.h"
#include <pthread.h>
diff --git a/libc/src/pthread/pthread_rwlock_trywrlock.cpp b/libc/src/pthread/pthread_rwlock_trywrlock.cpp
index 660c15a87b36c..5278bb2a8cce0 100644
--- a/libc/src/pthread/pthread_rwlock_trywrlock.cpp
+++ b/libc/src/pthread/pthread_rwlock_trywrlock.cpp
@@ -11,7 +11,7 @@
#include "src/__support/common.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
-#include "src/__support/threads/linux/rwlock.h"
+#include "src/__support/threads/unix_rwlock.h"
#include <pthread.h>
diff --git a/libc/src/pthread/pthread_rwlock_unlock.cpp b/libc/src/pthread/pthread_rwlock_unlock.cpp
index 5496bea929c51..73989b1fbfded 100644
--- a/libc/src/pthread/pthread_rwlock_unlock.cpp
+++ b/libc/src/pthread/pthread_rwlock_unlock.cpp
@@ -11,7 +11,7 @@
#include "src/__support/common.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
-#include "src/__support/threads/linux/rwlock.h"
+#include "src/__support/threads/unix_rwlock.h"
#include <pthread.h>
diff --git a/libc/src/pthread/pthread_rwlock_wrlock.cpp b/libc/src/pthread/pthread_rwlock_wrlock.cpp
index f02fb6b5db9c0..1934b6d12c795 100644
--- a/libc/src/pthread/pthread_rwlock_wrlock.cpp
+++ b/libc/src/pthread/pthread_rwlock_wrlock.cpp
@@ -10,7 +10,7 @@
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
-#include "src/__support/threads/linux/rwlock.h"
+#include "src/__support/threads/unix_rwlock.h"
#include <pthread.h>
diff --git a/libc/test/integration/src/pthread/CMakeLists.txt b/libc/test/integration/src/pthread/CMakeLists.txt
index b7414f2bea120..62f6ed777e522 100644
--- a/libc/test/integration/src/pthread/CMakeLists.txt
+++ b/libc/test/integration/src/pthread/CMakeLists.txt
@@ -63,6 +63,7 @@ add_integration_test(
libc.src.pthread.pthread_rwlockattr_setpshared
libc.src.pthread.pthread_rwlockattr_setkind_np
libc.src.__support.threads.raw_mutex
+ libc.src.__support.threads.raw_rwlock
libc.src.stdio.printf
libc.src.stdlib.getenv
libc.src.sys.mman.mmap
diff --git a/libc/test/integration/src/pthread/pthread_rwlock_test.cpp b/libc/test/integration/src/pthread/pthread_rwlock_test.cpp
index dddfb282ce167..67551e4142378 100644
--- a/libc/test/integration/src/pthread/pthread_rwlock_test.cpp
+++ b/libc/test/integration/src/pthread/pthread_rwlock_test.cpp
@@ -12,7 +12,7 @@
#include "src/__support/CPP/new.h"
#include "src/__support/OSUtil/syscall.h"
#include "src/__support/macros/config.h"
-#include "src/__support/threads/linux/rwlock.h"
+#include "src/__support/threads/raw_rwlock.h"
#include "src/__support/threads/raw_mutex.h"
#include "src/__support/threads/sleep.h"
#include "src/pthread/pthread_create.h"
@@ -131,7 +131,7 @@ static void nullptr_test() {
// counts.
static void high_reader_count_test() {
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
- rwlock.__state = LIBC_NAMESPACE::rwlock::RwLockTester::full_reader_state();
+ rwlock.__raw.__state = LIBC_NAMESPACE::rwlock::RwLockTester::full_reader_state();
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_rdlock(&rwlock), EAGAIN);
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_tryrdlock(&rwlock), EAGAIN);
// allocate 4 reader slots.
@@ -374,20 +374,20 @@ static void randomized_thread_operation(SharedData *data, ThreadGuard &guard) {
case Operation::READ: {
LIBC_NAMESPACE::pthread_rwlock_rdlock(&data->lock);
read_ops();
- LIBC_NAMESPACE::pthread_rwlock_unlock(&data->lock);
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_unlock(&data->lock), 0);
break;
}
case Operation::WRITE: {
LIBC_NAMESPACE::pthread_rwlock_wrlock(&data->lock);
write_ops();
- LIBC_NAMESPACE::pthread_rwlock_unlock(&data->lock);
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_unlock(&data->lock), 0);
break;
}
case Operation::TIMED_READ: {
timespec ts = get_ts();
if (LIBC_NAMESPACE::pthread_rwlock_timedrdlock(&data->lock, &ts) == 0) {
read_ops();
- LIBC_NAMESPACE::pthread_rwlock_unlock(&data->lock);
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_unlock(&data->lock), 0);
}
break;
}
@@ -395,7 +395,7 @@ static void randomized_thread_operation(SharedData *data, ThreadGuard &guard) {
timespec ts = get_ts();
if (LIBC_NAMESPACE::pthread_rwlock_timedwrlock(&data->lock, &ts) == 0) {
write_ops();
- LIBC_NAMESPACE::pthread_rwlock_unlock(&data->lock);
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_unlock(&data->lock), 0);
}
break;
}
@@ -404,7 +404,7 @@ static void randomized_thread_operation(SharedData *data, ThreadGuard &guard) {
if (LIBC_NAMESPACE::pthread_rwlock_clockrdlock(&data->lock, CLOCK_MONOTONIC,
&ts) == 0) {
read_ops();
- LIBC_NAMESPACE::pthread_rwlock_unlock(&data->lock);
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_unlock(&data->lock), 0);
}
break;
}
@@ -413,21 +413,21 @@ static void randomized_thread_operation(SharedData *data, ThreadGuard &guard) {
if (LIBC_NAMESPACE::pthread_rwlock_clockwrlock(&data->lock, CLOCK_MONOTONIC,
&ts) == 0) {
write_ops();
- LIBC_NAMESPACE::pthread_rwlock_unlock(&data->lock);
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_unlock(&data->lock), 0);
}
break;
}
case Operation::TRY_READ: {
if (LIBC_NAMESPACE::pthread_rwlock_tryrdlock(&data->lock) == 0) {
read_ops();
- LIBC_NAMESPACE::pthread_rwlock_unlock(&data->lock);
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_unlock(&data->lock), 0);
}
break;
}
case Operation::TRY_WRITE: {
if (LIBC_NAMESPACE::pthread_rwlock_trywrlock(&data->lock) == 0) {
write_ops();
- LIBC_NAMESPACE::pthread_rwlock_unlock(&data->lock);
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_unlock(&data->lock), 0);
}
break;
}
diff --git a/libc/test/src/__support/threads/linux/CMakeLists.txt b/libc/test/src/__support/threads/linux/CMakeLists.txt
index a660e7ceb4490..855676b1ca05e 100644
--- a/libc/test/src/__support/threads/linux/CMakeLists.txt
+++ b/libc/test/src/__support/threads/linux/CMakeLists.txt
@@ -11,3 +11,13 @@ add_libc_test(
libc.src.stdlib.exit
libc.hdr.signal_macros
)
+
+add_libc_test(
+ raw_rwlock_test
+ SUITE
+ libc-support-threads-tests
+ SRCS
+ raw_rwlock_test.cpp
+ DEPENDS
+ libc.src.__support.threads.raw_rwlock
+)
diff --git a/libc/test/src/__support/threads/linux/raw_rwlock_test.cpp b/libc/test/src/__support/threads/linux/raw_rwlock_test.cpp
new file mode 100644
index 0000000000000..fa643f3df9fcf
--- /dev/null
+++ b/libc/test/src/__support/threads/linux/raw_rwlock_test.cpp
@@ -0,0 +1,47 @@
+//===-- Unittests for RawRwLock -------------------------------------------===//
+//
+// 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 "hdr/time_macros.h"
+#include "src/__support/threads/raw_rwlock.h"
+#include "src/__support/time/clock_gettime.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcSupportThreadsRawRwLockTest, SmokeTest) {
+ LIBC_NAMESPACE::RawRwLock rwlock;
+ using LockResult = LIBC_NAMESPACE::RawRwLock::LockResult;
+
+ ASSERT_EQ(rwlock.read_lock(), LockResult::Success);
+ ASSERT_EQ(rwlock.try_read_lock(), LockResult::Success);
+ ASSERT_EQ(rwlock.try_write_lock(), LockResult::Busy);
+ ASSERT_EQ(rwlock.unlock(), LockResult::Success);
+ ASSERT_EQ(rwlock.unlock(), LockResult::Success);
+
+ ASSERT_EQ(rwlock.write_lock(), LockResult::Success);
+ ASSERT_EQ(rwlock.try_read_lock(), LockResult::Busy);
+ ASSERT_EQ(rwlock.try_write_lock(), LockResult::Busy);
+ ASSERT_EQ(rwlock.unlock(), LockResult::Success);
+ ASSERT_EQ(rwlock.unlock(), LockResult::PermissionDenied);
+}
+
+TEST(LlvmLibcSupportThreadsRawRwLockTest, TimeoutWithoutDeadlockDetection) {
+ LIBC_NAMESPACE::RawRwLock rwlock;
+ using LockResult = LIBC_NAMESPACE::RawRwLock::LockResult;
+
+ ASSERT_EQ(rwlock.write_lock(), LockResult::Success);
+
+ timespec ts;
+ LIBC_NAMESPACE::internal::clock_gettime(CLOCK_MONOTONIC, &ts);
+ ts.tv_sec += 1;
+ auto timeout = LIBC_NAMESPACE::internal::AbsTimeout::from_timespec(
+ ts, /*is_realtime=*/false);
+ ASSERT_TRUE(timeout.has_value());
+
+ ASSERT_EQ(rwlock.write_lock(*timeout), LockResult::TimedOut);
+ ASSERT_EQ(rwlock.read_lock(*timeout), LockResult::TimedOut);
+ ASSERT_EQ(rwlock.unlock(), LockResult::Success);
+}
>From f23228ec0623c94c0415a4fb0f49ded0260056a2 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <i at zhuyi.fan>
Date: Tue, 31 Mar 2026 20:02:38 -0400
Subject: [PATCH 2/6] restore comments
---
libc/src/__support/threads/raw_rwlock.h | 108 ++++++++++++++++++++++--
1 file changed, 99 insertions(+), 9 deletions(-)
diff --git a/libc/src/__support/threads/raw_rwlock.h b/libc/src/__support/threads/raw_rwlock.h
index 570757209d1d3..a29667221c5d0 100644
--- a/libc/src/__support/threads/raw_rwlock.h
+++ b/libc/src/__support/threads/raw_rwlock.h
@@ -34,8 +34,9 @@
#endif
namespace LIBC_NAMESPACE_DECL {
+// A namespace to rwlock specific utilities.
namespace rwlock {
-
+// The role of the thread in the RwLock.
enum class Role { Reader = 0, Writer = 1 };
enum class LockResult : int {
@@ -47,15 +48,21 @@ enum class LockResult : int {
PermissionDenied = EPERM,
};
+// A waiting queue to keep track of the pending readers and writers.
class WaitingQueue final : private RawMutex {
/* FutexWordType raw_mutex; (from base class) */
+ // Pending reader count (protected by the mutex)
FutexWordType pending_readers;
+ // Pending writer count (protected by the mutex)
FutexWordType pending_writers;
+ // Reader serialization (increases on each reader-waking operation)
Futex reader_serialization;
+ // Writer serialization (increases on each writer-waking operation)
Futex writer_serialization;
public:
+ // RAII guard to lock and unlock the waiting queue.
class Guard {
WaitingQueue &queue;
bool is_pshared;
@@ -109,13 +116,28 @@ class WaitingQueue final : private RawMutex {
}
};
+// The RwState of the RwLock is stored in an integer word, consisting of the
+// following components:
+// -----------------------------------------------
+// | Range | Description |
+// ===============================================
+// | 0 | Pending Reader Bit |
+// -----------------------------------------------
+// | 1 | Pending Writer Bit |
+// -----------------------------------------------
+// | [2, MSB) | Active Reader Count |
+// -----------------------------------------------
+// | MSB | Active Writer Bit |
+// -----------------------------------------------
class RwState {
+ // Shift amounts to access the components of the state.
LIBC_INLINE_VAR static constexpr int PENDING_READER_SHIFT = 0;
LIBC_INLINE_VAR static constexpr int PENDING_WRITER_SHIFT = 1;
LIBC_INLINE_VAR static constexpr int ACTIVE_READER_SHIFT = 2;
LIBC_INLINE_VAR static constexpr int ACTIVE_WRITER_SHIFT =
cpp::numeric_limits<int>::digits;
+ // Bitmasks to access the components of the state.
LIBC_INLINE_VAR static constexpr int PENDING_READER_BIT =
1 << PENDING_READER_SHIFT;
LIBC_INLINE_VAR static constexpr int PENDING_WRITER_BIT =
@@ -128,12 +150,16 @@ class RwState {
PENDING_READER_BIT | PENDING_WRITER_BIT;
private:
+ // We use the signed integer as the state type. It is easier
+ // to reason about the state transitions using signness.
int state;
public:
+ // Construction and conversion functions.
LIBC_INLINE constexpr RwState(int state = 0) : state(state) {}
LIBC_INLINE constexpr operator int() const { return state; }
+ // Utilities to check the state of the RwLock.
LIBC_INLINE constexpr bool has_active_writer() const { return state < 0; }
LIBC_INLINE constexpr bool has_active_reader() const {
return state >= ACTIVE_READER_COUNT_UNIT;
@@ -147,12 +173,18 @@ class RwState {
LIBC_INLINE constexpr bool has_pending_writer() const {
return state & PENDING_WRITER_BIT;
}
- LIBC_INLINE constexpr bool has_pending() const { return state & PENDING_MASK; }
+ LIBC_INLINE constexpr bool has_pending() const {
+ return state & PENDING_MASK;
+ }
LIBC_INLINE constexpr RwState set_writer_bit() const {
return RwState(state | ACTIVE_WRITER_BIT);
}
+ // The preference parameter changes the behavior of the lock acquisition
+ // if there are both readers and writers waiting for the lock. If writers
+ // are preferred, reader acquisition will be blocked until all pending
+ // writers are served.
template <Role role> LIBC_INLINE bool can_acquire(Role preference) const {
if constexpr (role == Role::Reader) {
switch (preference) {
@@ -162,11 +194,12 @@ class RwState {
return !has_active_writer() && !has_pending_writer();
}
__builtin_unreachable();
- } else {
+ } else
return !has_active_owner();
- }
}
+ // This function check if it is possible to grow the reader count without
+ // overflowing the state.
LIBC_INLINE cpp::optional<RwState> try_increase_reader_count() const {
LIBC_ASSERT(!has_active_writer() &&
"try_increase_reader_count shall only be called when there "
@@ -178,6 +211,7 @@ class RwState {
return res;
}
+ // Utilities to do atomic operations on the state.
LIBC_INLINE static RwState fetch_sub_reader_count(cpp::Atomic<int> &target,
cpp::MemoryOrder order) {
return RwState(target.fetch_sub(ACTIVE_READER_COUNT_UNIT, order));
@@ -196,7 +230,6 @@ class RwState {
else
return RwState(target.fetch_or(PENDING_WRITER_BIT, order));
}
-
template <Role role>
LIBC_INLINE static RwState fetch_clear_pending_bit(cpp::Atomic<int> &target,
cpp::MemoryOrder order) {
@@ -219,6 +252,7 @@ class RwState {
failure_order);
}
+ // Utilities to spin and reload the state.
private:
template <class F>
LIBC_INLINE static RwState spin_reload_until(cpp::Atomic<int> &target,
@@ -237,6 +271,8 @@ class RwState {
LIBC_INLINE static RwState spin_reload(cpp::Atomic<int> &target,
Role preference, unsigned spin_count) {
if constexpr (role == Role::Reader) {
+ // Return the reader state if either the lock is available or there is
+ // any ongoing contention.
return spin_reload_until(
target,
[=](RwState state) {
@@ -245,6 +281,9 @@ class RwState {
},
spin_count);
} else {
+ // Return the writer state if either the lock is available or there is
+ // any contention *between writers*. Since writers can be way less than
+ // readers, we allow them to spin more to improve the fairness.
return spin_reload_until(
target,
[=](RwState state) {
@@ -257,26 +296,35 @@ class RwState {
friend class RwLockTester;
};
-
} // namespace rwlock
class RawRwLock {
using RwState = rwlock::RwState;
+ using Role = rwlock::Role;
using WaitingQueue = rwlock::WaitingQueue;
public:
+ // Return types for the lock functions.
+ // All the locking routines returning this type are marked as [[nodiscard]]
+ // because it is a common error to assume the lock success without checking
+ // the return value, which can lead to undefined behaviors or other subtle
+ // bugs that are hard to reason about.
using LockResult = rwlock::LockResult;
- using Role = rwlock::Role;
private:
+ // Whether the RwLock is shared between processes.
LIBC_PREFERED_TYPE(bool)
unsigned is_pshared : 1;
+ // Reader/Writer preference.
LIBC_PREFERED_TYPE(Role)
unsigned preference : 1;
+ // RwState to keep track of the RwLock.
cpp::Atomic<int> state;
+ // Waiting queue to keep track of the readers and writers.
WaitingQueue queue;
private:
+ // Load the bitfield preference.
LIBC_INLINE Role get_preference() const {
return static_cast<Role>(preference);
}
@@ -291,14 +339,19 @@ class RawRwLock {
state, *next, cpp::MemoryOrder::ACQUIRE,
cpp::MemoryOrder::RELAXED)))
return LockResult::Success;
+ // Notice that old is updated by the compare_exchange_weak_with
+ // function.
}
return LockResult::Busy;
} else {
+ // This while loop should terminate quickly
while (LIBC_LIKELY(old.can_acquire<Role::Writer>(get_preference()))) {
if (LIBC_LIKELY(old.compare_exchange_weak_with(
state, old.set_writer_bit(), cpp::MemoryOrder::ACQUIRE,
cpp::MemoryOrder::RELAXED)))
return LockResult::Success;
+ // Notice that old is updated by the compare_exchange_weak_with
+ // function.
}
return LockResult::Busy;
}
@@ -309,47 +362,77 @@ class RawRwLock {
lock_slow(cpp::optional<Futex::Timeout> timeout = cpp::nullopt,
unsigned spin_count = LIBC_COPT_RWLOCK_DEFAULT_SPIN_COUNT) {
#if LIBC_COPT_TIMEOUT_ENSURE_MONOTONICITY
+ // Phase 2: convert the timeout if necessary.
if (timeout)
ensure_monotonicity(*timeout);
#endif
+ // Phase 3: spin to get the initial state. We ignore the timing due to
+ // spin since it should end quickly.
RwState old =
RwState::spin_reload<role>(state, get_preference(), spin_count);
+ // Enter the main acquisition loop.
for (;;) {
+ // Phase 4: if the lock can be acquired, try to acquire it.
LockResult result = try_lock<role>(old);
if (result != LockResult::Busy)
return result;
+ // Phase 5: register ourselves as a reader.
int serial_number;
{
+ // The queue need to be protected by a mutex since the operations in
+ // this block must be executed as a whole transaction. It is possible
+ // that this lock will make the timeout imprecise, but this is the
+ // best we can do. The transaction is small and everyone should make
+ // progress rather quickly.
WaitingQueue::Guard guard = queue.acquire(is_pshared);
guard.template pending_count<role>()++;
+
+ // Use atomic operation to guarantee the total order of the operations
+ // on the state. The pending flag update should be visible to any
+ // succeeding unlock events. Or, if a unlock does happen before we
+ // sleep on the futex, we can avoid such waiting.
old = RwState::fetch_set_pending_bit<role>(state,
cpp::MemoryOrder::RELAXED);
+ // no need to use atomic since it is already protected by the mutex.
serial_number = guard.serialization<role>();
}
+ // Phase 6: do futex wait until the lock is available or timeout is
+ // reached.
bool timeout_flag = false;
if (!old.can_acquire<role>(get_preference()))
timeout_flag = (queue.wait<role>(serial_number, timeout, is_pshared) ==
-ETIMEDOUT);
+ // Phase 7: unregister ourselves as a pending reader/writer.
{
+ // Similarly, the unregister operation should also be an atomic
+ // transaction.
WaitingQueue::Guard guard = queue.acquire(is_pshared);
guard.pending_count<role>()--;
+ // Clear the flag if we are the last reader. The flag must be
+ // cleared otherwise operations like trylock may fail even though
+ // there is no competitors.
if (guard.pending_count<role>() == 0)
RwState::fetch_clear_pending_bit<role>(state,
cpp::MemoryOrder::RELAXED);
}
+ // Phase 8: exit the loop is timeout is reached.
if (timeout_flag)
return LockResult::TimedOut;
+ // Phase 9: reload the state and retry the acquisition.
old = RwState::spin_reload<role>(state, get_preference(), spin_count);
}
}
+ // Compiler (clang 19.0) somehow decides that this function may be inlined,
+ // which leads to a larger unlock function that is infeasible to be inlined.
+ // Since notifcation routine is colder we mark it as noinline explicitly.
[[gnu::noinline]]
LIBC_INLINE void notify_pending_threads() {
enum class WakeTarget { Readers, Writers, None };
@@ -382,8 +465,7 @@ class RawRwLock {
LIBC_INLINE constexpr RawRwLock(Role preference = Role::Reader,
bool is_pshared = false)
: is_pshared(is_pshared),
- preference(static_cast<unsigned>(preference) & 1u), state(0),
- queue() {}
+ preference(static_cast<unsigned>(preference) & 1u), state(0), queue() {}
[[nodiscard]]
LIBC_INLINE LockResult try_read_lock() {
@@ -421,12 +503,18 @@ class RawRwLock {
LIBC_INLINE LockResult unlock() {
RwState old = RwState::load(state, cpp::MemoryOrder::RELAXED);
if (old.has_active_writer()) {
+ // The lock is held by a writer.
+ // clear the writer bit.
old =
RwState::fetch_clear_active_writer(state, cpp::MemoryOrder::RELEASE);
+ // If there is no pending readers or writers, we are done.
if (!old.has_pending())
return LockResult::Success;
} else if (old.has_active_reader()) {
+ // The lock is held by readers.
+ // Decrease the reader count.
old = RwState::fetch_sub_reader_count(state, cpp::MemoryOrder::RELEASE);
+ // If there is no pending readers or writers, we are done.
if (!old.has_last_reader() || !old.has_pending())
return LockResult::Success;
} else {
@@ -439,6 +527,8 @@ class RawRwLock {
[[nodiscard]]
LIBC_INLINE LockResult check_for_destroy() {
+ // We do not allocate any special resources for the RwLock, so this function
+ // will only check if the lock is currently held by any thread.
RwState old = RwState::load(state, cpp::MemoryOrder::RELAXED);
if (old.has_active_owner())
return LockResult::Busy;
>From 632d4a15c3c511d9eabe4a5d9fdefae6402c1b31 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <i at zhuyi.fan>
Date: Tue, 31 Mar 2026 20:20:46 -0400
Subject: [PATCH 3/6] fix
---
libc/src/__support/threads/unix_rwlock.h | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/libc/src/__support/threads/unix_rwlock.h b/libc/src/__support/threads/unix_rwlock.h
index 92efee4a4f033..8301465647bc3 100644
--- a/libc/src/__support/threads/unix_rwlock.h
+++ b/libc/src/__support/threads/unix_rwlock.h
@@ -72,10 +72,12 @@ class RwLock final {
[[nodiscard]] LIBC_INLINE LockResult unlock() {
bool is_writer_unlock = raw.has_active_writer();
- LockResult result = raw.unlock();
- if (is_writer_unlock && result == LockResult::Success)
+ if (is_writer_unlock) {
+ if (get_writer_tid() != internal::gettid())
+ return LockResult::PermissionDenied;
set_writer_tid(0);
- return result;
+ }
+ return raw.unlock();
}
[[nodiscard]] LIBC_INLINE LockResult check_for_destroy() {
>From a975dde3111399279e65289a1a685781d4ea05be Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <i at zhuyi.fan>
Date: Tue, 31 Mar 2026 20:26:40 -0400
Subject: [PATCH 4/6] fmt
---
libc/src/__support/threads/unix_rwlock.h | 2 +-
libc/test/integration/src/pthread/pthread_rwlock_test.cpp | 5 +++--
2 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/libc/src/__support/threads/unix_rwlock.h b/libc/src/__support/threads/unix_rwlock.h
index 8301465647bc3..f752208376009 100644
--- a/libc/src/__support/threads/unix_rwlock.h
+++ b/libc/src/__support/threads/unix_rwlock.h
@@ -10,8 +10,8 @@
#define LLVM_LIBC_SRC___SUPPORT_THREADS_UNIX_RWLOCK_H
#include "hdr/types/pid_t.h"
-#include "src/__support/common.h"
#include "src/__support/CPP/atomic.h"
+#include "src/__support/common.h"
#include "src/__support/threads/identifier.h"
#include "src/__support/threads/raw_rwlock.h"
diff --git a/libc/test/integration/src/pthread/pthread_rwlock_test.cpp b/libc/test/integration/src/pthread/pthread_rwlock_test.cpp
index 67551e4142378..57d336d0704b1 100644
--- a/libc/test/integration/src/pthread/pthread_rwlock_test.cpp
+++ b/libc/test/integration/src/pthread/pthread_rwlock_test.cpp
@@ -12,8 +12,8 @@
#include "src/__support/CPP/new.h"
#include "src/__support/OSUtil/syscall.h"
#include "src/__support/macros/config.h"
-#include "src/__support/threads/raw_rwlock.h"
#include "src/__support/threads/raw_mutex.h"
+#include "src/__support/threads/raw_rwlock.h"
#include "src/__support/threads/sleep.h"
#include "src/pthread/pthread_create.h"
#include "src/pthread/pthread_join.h"
@@ -131,7 +131,8 @@ static void nullptr_test() {
// counts.
static void high_reader_count_test() {
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
- rwlock.__raw.__state = LIBC_NAMESPACE::rwlock::RwLockTester::full_reader_state();
+ rwlock.__raw.__state =
+ LIBC_NAMESPACE::rwlock::RwLockTester::full_reader_state();
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_rdlock(&rwlock), EAGAIN);
ASSERT_EQ(LIBC_NAMESPACE::pthread_rwlock_tryrdlock(&rwlock), EAGAIN);
// allocate 4 reader slots.
>From b3197b74b000517bdb1755700411b41ec195bd83 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <i at zhuyi.fan>
Date: Tue, 31 Mar 2026 20:42:35 -0400
Subject: [PATCH 5/6] remove macOS entrypoint change for now
---
libc/config/darwin/aarch64/entrypoints.txt | 33 ----------------------
libc/config/darwin/x86_64/entrypoints.txt | 32 ---------------------
2 files changed, 65 deletions(-)
diff --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 312249ca353f0..0888f4b0d922b 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -100,39 +100,6 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.realloc
libc.src.stdlib.free
- # pthread mutex entrypoints
- libc.src.pthread.pthread_mutex_destroy
- libc.src.pthread.pthread_mutex_init
- libc.src.pthread.pthread_mutex_lock
- libc.src.pthread.pthread_mutex_unlock
- libc.src.pthread.pthread_mutexattr_destroy
- libc.src.pthread.pthread_mutexattr_getpshared
- libc.src.pthread.pthread_mutexattr_getrobust
- libc.src.pthread.pthread_mutexattr_gettype
- libc.src.pthread.pthread_mutexattr_init
- libc.src.pthread.pthread_mutexattr_setpshared
- libc.src.pthread.pthread_mutexattr_setrobust
- libc.src.pthread.pthread_mutexattr_settype
-
- # pthread rwlock entrypoints
- libc.src.pthread.pthread_rwlock_clockrdlock
- libc.src.pthread.pthread_rwlock_clockwrlock
- libc.src.pthread.pthread_rwlock_destroy
- libc.src.pthread.pthread_rwlock_init
- libc.src.pthread.pthread_rwlock_rdlock
- libc.src.pthread.pthread_rwlock_timedrdlock
- libc.src.pthread.pthread_rwlock_timedwrlock
- libc.src.pthread.pthread_rwlock_tryrdlock
- libc.src.pthread.pthread_rwlock_trywrlock
- libc.src.pthread.pthread_rwlock_unlock
- libc.src.pthread.pthread_rwlock_wrlock
- libc.src.pthread.pthread_rwlockattr_destroy
- libc.src.pthread.pthread_rwlockattr_getkind_np
- libc.src.pthread.pthread_rwlockattr_getpshared
- libc.src.pthread.pthread_rwlockattr_init
- libc.src.pthread.pthread_rwlockattr_setkind_np
- libc.src.pthread.pthread_rwlockattr_setpshared
-
# wctype.h entrypoints
libc.src.wctype.iswalpha
libc.src.wctype.iswgraph
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index 7c861d02fc683..fc61699c84f33 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -94,38 +94,6 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.realloc
libc.src.stdlib.free
- # pthread mutex entrypoints
- libc.src.pthread.pthread_mutex_destroy
- libc.src.pthread.pthread_mutex_init
- libc.src.pthread.pthread_mutex_lock
- libc.src.pthread.pthread_mutex_unlock
- libc.src.pthread.pthread_mutexattr_destroy
- libc.src.pthread.pthread_mutexattr_getpshared
- libc.src.pthread.pthread_mutexattr_getrobust
- libc.src.pthread.pthread_mutexattr_gettype
- libc.src.pthread.pthread_mutexattr_init
- libc.src.pthread.pthread_mutexattr_setpshared
- libc.src.pthread.pthread_mutexattr_setrobust
- libc.src.pthread.pthread_mutexattr_settype
-
- # pthread rwlock entrypoints
- libc.src.pthread.pthread_rwlock_clockrdlock
- libc.src.pthread.pthread_rwlock_clockwrlock
- libc.src.pthread.pthread_rwlock_destroy
- libc.src.pthread.pthread_rwlock_init
- libc.src.pthread.pthread_rwlock_rdlock
- libc.src.pthread.pthread_rwlock_timedrdlock
- libc.src.pthread.pthread_rwlock_timedwrlock
- libc.src.pthread.pthread_rwlock_tryrdlock
- libc.src.pthread.pthread_rwlock_trywrlock
- libc.src.pthread.pthread_rwlock_unlock
- libc.src.pthread.pthread_rwlock_wrlock
- libc.src.pthread.pthread_rwlockattr_destroy
- libc.src.pthread.pthread_rwlockattr_getkind_np
- libc.src.pthread.pthread_rwlockattr_getpshared
- libc.src.pthread.pthread_rwlockattr_init
- libc.src.pthread.pthread_rwlockattr_setkind_np
- libc.src.pthread.pthread_rwlockattr_setpshared
)
set(TARGET_LIBM_ENTRYPOINTS
>From f7254e8fdca161b6d058b459edd0e29f82902fa8 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <i at zhuyi.fan>
Date: Wed, 1 Apr 2026 10:07:59 -0400
Subject: [PATCH 6/6] fix format nits
---
libc/config/darwin/x86_64/entrypoints.txt | 1 -
libc/src/__support/threads/CMakeLists.txt | 56 +++++++++++------------
2 files changed, 28 insertions(+), 29 deletions(-)
diff --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index fc61699c84f33..046d1b409742f 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -93,7 +93,6 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.calloc
libc.src.stdlib.realloc
libc.src.stdlib.free
-
)
set(TARGET_LIBM_ENTRYPOINTS
diff --git a/libc/src/__support/threads/CMakeLists.txt b/libc/src/__support/threads/CMakeLists.txt
index 317c7fa296658..9b0b77be098e9 100644
--- a/libc/src/__support/threads/CMakeLists.txt
+++ b/libc/src/__support/threads/CMakeLists.txt
@@ -29,74 +29,74 @@ if(TARGET libc.src.__support.threads.${LIBC_TARGET_OS}.futex_utils)
add_header_library(
raw_mutex
HDRS
- raw_mutex.h
+ raw_mutex.h
COMPILE_OPTIONS
- ${monotonicity_flags}
+ ${monotonicity_flags}
DEPENDS
- .${LIBC_TARGET_OS}.futex_utils
- libc.src.__support.threads.sleep
- libc.src.__support.time.abs_timeout
- libc.src.__support.time.monotonicity
- libc.src.__support.CPP.optional
- libc.hdr.types.pid_t
+ .${LIBC_TARGET_OS}.futex_utils
+ libc.src.__support.threads.sleep
+ libc.src.__support.time.abs_timeout
+ libc.src.__support.time.monotonicity
+ libc.src.__support.CPP.optional
+ libc.hdr.types.pid_t
)
add_header_library(
raw_rwlock
HDRS
- raw_rwlock.h
+ raw_rwlock.h
COMPILE_OPTIONS
- ${rwlock_default_spin_count}
- ${monotonicity_flags}
+ ${rwlock_default_spin_count}
+ ${monotonicity_flags}
DEPENDS
- .raw_mutex
- libc.src.__support.common
- libc.src.__support.CPP.limits
+ .raw_mutex
+ libc.src.__support.common
+ libc.src.__support.CPP.limits
)
add_header_library(
unix_rwlock
HDRS
- unix_rwlock.h
+ unix_rwlock.h
DEPENDS
- .raw_rwlock
- libc.hdr.types.pid_t
- libc.src.__support.threads.identifier
+ .raw_rwlock
+ libc.hdr.types.pid_t
+ libc.src.__support.threads.identifier
)
add_header_library(
unix_mutex
HDRS
- unix_mutex.h
+ unix_mutex.h
DEPENDS
- .raw_mutex
+ .raw_mutex
)
add_header_library(
mutex
HDRS
- mutex.h
+ mutex.h
DEPENDS
- .unix_mutex
+ .unix_mutex
)
add_object_library(
fork_callbacks
SRCS
- fork_callbacks.cpp
+ fork_callbacks.cpp
HDRS
- fork_callbacks.h
+ fork_callbacks.h
DEPENDS
- .mutex
- libc.src.__support.CPP.mutex
+ .mutex
+ libc.src.__support.CPP.mutex
)
elseif(NOT (LIBC_CONF_THREAD_MODE STREQUAL LIBC_THREAD_MODE_PLATFORM))
add_header_library(
mutex
HDRS
- mutex.h
+ mutex.h
DEPENDS
- .mutex_common
+ .mutex_common
)
endif()
More information about the libc-commits
mailing list