[libc-commits] [libc] [libc] add rwlock (PR #94156)
Michael Jones via libc-commits
libc-commits at lists.llvm.org
Thu Jun 6 14:45:58 PDT 2024
================
@@ -0,0 +1,525 @@
+//===--- Implementation of a Linux 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_LINUX_RWLOCK_H
+#define LLVM_LIBC_SRC_SUPPORT_THREADS_LINUX_RWLOCK_H
+
+#include "hdr/errno_macros.h"
+#include "hdr/types/pid_t.h"
+#include "src/__support/CPP/atomic.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/optimization.h"
+#include "src/__support/threads/linux/futex_utils.h"
+#include "src/__support/threads/linux/futex_word.h"
+#include "src/__support/threads/linux/raw_mutex.h"
+#include "src/__support/threads/sleep.h"
+
+#ifndef LIBC_COPT_RWLOCK_DEFAULT_SPIN_COUNT
+#define LIBC_COPT_RWLOCK_DEFAULT_SPIN_COUNT 100
+#endif
+
+#ifndef LIBC_COPT_TIMEOUT_ENSURE_MONOTONICITY
+#define LIBC_COPT_TIMEOUT_ENSURE_MONOTONICITY 1
+#warning "LIBC_COPT_TIMEOUT_ENSURE_MONOTONICITY is not defined, defaulting to 1"
+#endif
+
+#if LIBC_COPT_TIMEOUT_ENSURE_MONOTONICITY
+#include "src/__support/time/linux/monotonicity.h"
+#endif
+
+namespace LIBC_NAMESPACE {
+class RwLock {
+public:
+ enum class Role : char { Reader, Writer };
+
+private:
+ class WaitingQueue final : private RawMutex {
+ FutexWordType pending_reader;
+ FutexWordType pending_writer;
+ Futex reader_serialization;
+ Futex writer_serialization;
+
+ public:
+ class Guard {
+ WaitingQueue &queue;
+
+ LIBC_INLINE constexpr Guard(WaitingQueue &queue) : queue(queue) {}
+
+ public:
+ LIBC_INLINE ~Guard() { queue.unlock(); }
+ template <Role role> LIBC_INLINE FutexWordType &pending_count() {
+ if constexpr (role == Role::Reader)
+ return queue.pending_reader;
+ else
+ return queue.pending_writer;
+ }
+ template <Role role> LIBC_INLINE FutexWordType &serialization() {
+ if constexpr (role == Role::Reader)
+ return queue.reader_serialization.val;
+ else
+ return queue.writer_serialization.val;
+ }
+ friend WaitingQueue;
+ };
+
+ public:
----------------
michaelrj-google wrote:
I don't think this `public` is necessary
https://github.com/llvm/llvm-project/pull/94156
More information about the libc-commits
mailing list