22 std::atomic_flag lock_flag;
25 spinlock() noexcept { lock_flag.clear(); }
27 LLVM_ATTRIBUTE_ALWAYS_INLINE
28 bool try_lock() {
return !lock_flag.test_and_set(std::memory_order_acquire); }
30 LLVM_ATTRIBUTE_ALWAYS_INLINE
32 for (
unsigned int i = 1; !try_lock(); ++i)
33 if ((i & 0xff) == 0) std::this_thread::yield();
36 LLVM_ATTRIBUTE_ALWAYS_INLINE
37 void unlock() { lock_flag.clear(std::memory_order_release); }
46 std::atomic<std::thread::id> owner_thread_id{std::thread::id{}};
47 int32_t recursive_counter{0};
48 std::atomic_flag lock_flag;
53 LLVM_ATTRIBUTE_ALWAYS_INLINE
55 if (!lock_flag.test_and_set(std::memory_order_acquire)) {
56 owner_thread_id.store(std::this_thread::get_id(),
57 std::memory_order_release);
59 if (owner_thread_id.load(std::memory_order_acquire) !=
60 std::this_thread::get_id())
67 LLVM_ATTRIBUTE_ALWAYS_INLINE
69 for (
unsigned int i = 1; !try_lock(); ++i)
70 if ((i & 0xffff) == 0) std::this_thread::yield();
73 LLVM_ATTRIBUTE_ALWAYS_INLINE
75 assert(owner_thread_id.load(std::memory_order_acquire) ==
76 std::this_thread::get_id());
77 assert(recursive_counter > 0);
79 if (--recursive_counter == 0) {
80 owner_thread_id.store(std::thread::id{}, std::memory_order_release);
81 lock_flag.clear(std::memory_order_release);
92 std::atomic<std::thread::id> owner_thread_id{std::thread::id{}};
93 int32_t recursive_counter{0};
96 LLVM_ATTRIBUTE_ALWAYS_INLINE
98 auto owner = std::thread::id{};
99 auto us = std::this_thread::get_id();
100 if (!owner_thread_id.compare_exchange_weak(owner, us,
101 std::memory_order_acquire)) {
102 if (owner != us)
return false;
108 LLVM_ATTRIBUTE_ALWAYS_INLINE
110 for (
unsigned int i = 1; !try_lock(); ++i)
111 if ((i & 0xffff) == 0) std::this_thread::yield();
114 LLVM_ATTRIBUTE_ALWAYS_INLINE
116 assert(owner_thread_id.load(std::memory_order_acquire) ==
117 std::this_thread::get_id());
118 assert(recursive_counter > 0);
120 if (--recursive_counter == 0)
121 owner_thread_id.store(std::thread::id{}, std::memory_order_release);
A spinlock mutex.
Definition: spinlock.h:21
A recursive spinlock mutex.
Definition: spinlock.h:45
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
A recursive spinlock mutex.
Definition: spinlock.h:91