12#include "wpi/util/Compiler.hpp"
20 std::atomic_flag lock_flag;
25 LLVM_ATTRIBUTE_ALWAYS_INLINE
26 bool try_lock() {
return !lock_flag.test_and_set(std::memory_order_acquire); }
28 LLVM_ATTRIBUTE_ALWAYS_INLINE
30 for (
unsigned int i = 1; !
try_lock(); ++i) {
31 if ((i & 0xff) == 0) {
32 std::this_thread::yield();
37 LLVM_ATTRIBUTE_ALWAYS_INLINE
38 void unlock() { lock_flag.clear(std::memory_order_release); }
47 std::atomic<std::thread::id> owner_thread_id{std::thread::id{}};
48 int32_t recursive_counter{0};
49 std::atomic_flag lock_flag;
54 LLVM_ATTRIBUTE_ALWAYS_INLINE
56 if (!lock_flag.test_and_set(std::memory_order_acquire)) {
57 owner_thread_id.store(std::this_thread::get_id(),
58 std::memory_order_release);
60 if (owner_thread_id.load(std::memory_order_acquire) !=
61 std::this_thread::get_id()) {
69 LLVM_ATTRIBUTE_ALWAYS_INLINE
71 for (
unsigned int i = 1; !
try_lock(); ++i) {
72 if ((i & 0xffff) == 0) {
73 std::this_thread::yield();
78 LLVM_ATTRIBUTE_ALWAYS_INLINE
80 assert(owner_thread_id.load(std::memory_order_acquire) ==
81 std::this_thread::get_id());
82 assert(recursive_counter > 0);
84 if (--recursive_counter == 0) {
85 owner_thread_id.store(std::thread::id{}, std::memory_order_release);
86 lock_flag.clear(std::memory_order_release);
97 std::atomic<std::thread::id> owner_thread_id{std::thread::id{}};
98 int32_t recursive_counter{0};
101 LLVM_ATTRIBUTE_ALWAYS_INLINE
103 auto owner = std::thread::id{};
104 auto us = std::this_thread::get_id();
105 if (!owner_thread_id.compare_exchange_weak(owner, us,
106 std::memory_order_acquire)) {
115 LLVM_ATTRIBUTE_ALWAYS_INLINE
117 for (
unsigned int i = 1; !
try_lock(); ++i) {
118 if ((i & 0xffff) == 0) {
119 std::this_thread::yield();
124 LLVM_ATTRIBUTE_ALWAYS_INLINE
126 assert(owner_thread_id.load(std::memory_order_acquire) ==
127 std::this_thread::get_id());
128 assert(recursive_counter > 0);
130 if (--recursive_counter == 0) {
131 owner_thread_id.store(std::thread::id{}, std::memory_order_release);
A recursive spinlock mutex.
Definition spinlock.hpp:46
recursive_spinlock1() noexcept
Definition spinlock.hpp:52
LLVM_ATTRIBUTE_ALWAYS_INLINE void lock()
Definition spinlock.hpp:70
LLVM_ATTRIBUTE_ALWAYS_INLINE bool try_lock()
Definition spinlock.hpp:55
LLVM_ATTRIBUTE_ALWAYS_INLINE void unlock()
Definition spinlock.hpp:79
A recursive spinlock mutex.
Definition spinlock.hpp:96
LLVM_ATTRIBUTE_ALWAYS_INLINE void unlock()
Definition spinlock.hpp:125
LLVM_ATTRIBUTE_ALWAYS_INLINE void lock()
Definition spinlock.hpp:116
LLVM_ATTRIBUTE_ALWAYS_INLINE bool try_lock()
Definition spinlock.hpp:102
spinlock() noexcept
Definition spinlock.hpp:23
LLVM_ATTRIBUTE_ALWAYS_INLINE bool try_lock()
Definition spinlock.hpp:26
LLVM_ATTRIBUTE_ALWAYS_INLINE void unlock()
Definition spinlock.hpp:38
LLVM_ATTRIBUTE_ALWAYS_INLINE void lock()
Definition spinlock.hpp:29
Definition raw_os_ostream.hpp:19
recursive_spinlock1 recursive_spinlock
Definition spinlock.hpp:141