23 std::atomic_flag lock_flag;
26 spinlock() noexcept { lock_flag.clear(); }
28 LLVM_ATTRIBUTE_ALWAYS_INLINE
29 bool try_lock() {
return !lock_flag.test_and_set(std::memory_order_acquire); }
31 LLVM_ATTRIBUTE_ALWAYS_INLINE
33 for (
unsigned int i = 1; !try_lock(); ++i)
34 if ((i & 0xff) == 0) 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())
68 LLVM_ATTRIBUTE_ALWAYS_INLINE
70 for (
unsigned int i = 1; !try_lock(); ++i)
71 if ((i & 0xffff) == 0) std::this_thread::yield();
74 LLVM_ATTRIBUTE_ALWAYS_INLINE
76 assert(owner_thread_id.load(std::memory_order_acquire) ==
77 std::this_thread::get_id());
78 assert(recursive_counter > 0);
80 if (--recursive_counter == 0) {
81 owner_thread_id.store(std::thread::id{}, std::memory_order_release);
82 lock_flag.clear(std::memory_order_release);
93 std::atomic<std::thread::id> owner_thread_id{std::thread::id{}};
94 int32_t recursive_counter{0};
97 LLVM_ATTRIBUTE_ALWAYS_INLINE
99 auto owner = std::thread::id{};
100 auto us = std::this_thread::get_id();
101 if (!owner_thread_id.compare_exchange_weak(owner, us,
102 std::memory_order_acquire)) {
103 if (owner != us)
return false;
109 LLVM_ATTRIBUTE_ALWAYS_INLINE
111 for (
unsigned int i = 1; !try_lock(); ++i)
112 if ((i & 0xffff) == 0) std::this_thread::yield();
115 LLVM_ATTRIBUTE_ALWAYS_INLINE
117 assert(owner_thread_id.load(std::memory_order_acquire) ==
118 std::this_thread::get_id());
119 assert(recursive_counter > 0);
121 if (--recursive_counter == 0)
122 owner_thread_id.store(std::thread::id{}, std::memory_order_release);
A spinlock mutex.
Definition: spinlock.h:22
A recursive spinlock mutex.
Definition: spinlock.h:46
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
A recursive spinlock mutex.
Definition: spinlock.h:92