[libc-commits] [libc] [libc] add rwlock (PR #94156)

via libc-commits libc-commits at lists.llvm.org
Thu Jun 13 04:03:27 PDT 2024


================
@@ -0,0 +1,564 @@
+//===--- 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/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/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 {
+// 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.
+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;
+
+    LIBC_INLINE constexpr Guard(WaitingQueue &queue, bool is_pshared)
+        : queue(queue), is_pshared(is_pshared) {
+      queue.lock(cpp::nullopt, is_pshared);
+    }
+
+  public:
+    LIBC_INLINE ~Guard() { queue.unlock(is_pshared); }
+    template <Role role> LIBC_INLINE FutexWordType &pending_count() {
+      if constexpr (role == Role::Reader)
+        return queue.pending_readers;
+      else
+        return queue.pending_writers;
+    }
+    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:
+  LIBC_INLINE constexpr WaitingQueue()
+      : RawMutex(), pending_readers(0), pending_writers(0),
+        reader_serialization(0), writer_serialization(0) {}
+
+  LIBC_INLINE Guard acquire(bool is_pshared) {
+    return Guard(*this, is_pshared);
+  }
+
+  template <Role role>
+  LIBC_INLINE long wait(FutexWordType expected,
+                        cpp::optional<Futex::Timeout> timeout,
+                        bool is_pshared) {
+    if constexpr (role == Role::Reader)
+      return reader_serialization.wait(expected, timeout, is_pshared);
+    else
+      return writer_serialization.wait(expected, timeout, is_pshared);
+  }
+
+  template <Role role> LIBC_INLINE long notify(bool is_pshared) {
+    if constexpr (role == Role::Reader)
+      return reader_serialization.notify_all(is_pshared);
+    else
+      return writer_serialization.notify_one(is_pshared);
+  }
+};
+
+// The State 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 State {
+
+  // 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 =
+      1 << PENDING_WRITER_SHIFT;
+  LIBC_INLINE_VAR static constexpr int ACTIVE_READER_COUNT_UNIT =
+      1 << ACTIVE_READER_SHIFT;
+  LIBC_INLINE_VAR static constexpr int ACTIVE_WRITER_BIT =
+      1 << ACTIVE_WRITER_SHIFT;
+  LIBC_INLINE_VAR static constexpr int PENDING_MASK =
+      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 State(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;
+  }
+  LIBC_INLINE constexpr bool has_acitve_owner() const {
+    return has_active_reader() || has_active_writer();
+  }
+  LIBC_INLINE constexpr bool has_last_reader() const {
+    return (state >> ACTIVE_READER_SHIFT) == 1;
+  }
+  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 State set_writer_bit() const {
+    return State(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) {
+      case Role::Reader:
+        return !has_active_writer();
+      case Role::Writer:
+        return !has_active_writer() && !has_pending_writer();
+      }
+    } else
+      return !has_acitve_owner();
+  }
+
+  // This function check if it is possible to grow the reader count without
+  // overflowing the state.
+  LIBC_INLINE cpp::optional<State> try_increase_reader_count() const {
+    LIBC_ASSERT(!has_active_writer() &&
+                "try_increase_reader_count shall only be called when there "
+                "is no active writer.");
+    State res;
+    if (LIBC_UNLIKELY(__builtin_sadd_overflow(state, ACTIVE_READER_COUNT_UNIT,
+                                              &res.state)))
+      return cpp::nullopt;
+    return res;
+  }
+
+  // Utilities to do atomic operations on the state.
+  LIBC_INLINE static State
+  fetch_sub_reader_count(cpp::Atomic<int> &target,
+                         cpp::MemoryOrder order = cpp::MemoryOrder::SEQ_CST) {
+    return State(target.fetch_sub(ACTIVE_READER_COUNT_UNIT, order));
+  }
+
+  LIBC_INLINE static State
+  load(cpp::Atomic<int> &target,
+       cpp::MemoryOrder order = cpp::MemoryOrder::SEQ_CST) {
+    return State(target.load(order));
+  }
+
+  template <Role role>
+  LIBC_INLINE static State
+  fetch_set_pending_bit(cpp::Atomic<int> &target,
+                        cpp::MemoryOrder order = cpp::MemoryOrder::SEQ_CST) {
+    if constexpr (role == Role::Reader)
+      return State(target.fetch_or(PENDING_READER_BIT, order));
+    else
+      return State(target.fetch_or(PENDING_WRITER_BIT, order));
+  }
+  template <Role role>
+  LIBC_INLINE static State
+  fetch_clear_pending_bit(cpp::Atomic<int> &target,
+                          cpp::MemoryOrder order = cpp::MemoryOrder::SEQ_CST) {
+    if constexpr (role == Role::Reader)
+      return State(target.fetch_and(~PENDING_READER_BIT, order));
+    else
+      return State(target.fetch_and(~PENDING_WRITER_BIT, order));
+  }
+  LIBC_INLINE static State
+  fetch_set_active_writer(cpp::Atomic<int> &target,
+                          cpp::MemoryOrder order = cpp::MemoryOrder::SEQ_CST) {
+    return State(target.fetch_or(ACTIVE_WRITER_BIT, order));
+  }
+  LIBC_INLINE static State fetch_clear_active_writer(
+      cpp::Atomic<int> &target,
+      cpp::MemoryOrder order = cpp::MemoryOrder::SEQ_CST) {
+    return State(target.fetch_and(~ACTIVE_WRITER_BIT, order));
+  }
+
+  LIBC_INLINE bool compare_exchange_weak_with(cpp::Atomic<int> &target,
+                                              State desired,
+                                              cpp::MemoryOrder success_order,
+                                              cpp::MemoryOrder failure_order) {
+    return target.compare_exchange_weak(state, desired, success_order,
+                                        failure_order);
+  }
+
+  // Utilities to spin and reload the state.
+private:
+  template <class F>
+  LIBC_INLINE static State spin_reload_until(cpp::Atomic<int> &target, F &&func,
+                                             unsigned spin_count) {
+    for (;;) {
+      auto state = State::load(target, cpp::MemoryOrder::RELAXED);
+      if (func(state) || spin_count == 0)
+        return state;
+      sleep_briefly();
+      spin_count--;
+    }
+  }
+
+public:
+  template <Role role>
+  LIBC_INLINE static State 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.
----------------
QuarticCat wrote:

Seems like a format accident.

https://github.com/llvm/llvm-project/pull/94156


More information about the libc-commits mailing list