Gaia-ECS v0.9.3
A simple and powerful entity component system
Loading...
Searching...
No Matches
spinlock.h
1#pragma once
2#include "gaia/config/config.h"
3
4#include <atomic>
5
6namespace gaia {
7 namespace mt {
8 class GAIA_API SpinLock final {
9 std::atomic_int32_t m_value{};
10
11 public:
12 SpinLock() = default;
13 SpinLock(const SpinLock&) = delete;
14 SpinLock& operator=(const SpinLock&) = delete;
15
16 bool try_lock() {
17 // Attempt to acquire the lock without waiting
18 return 0 == m_value.exchange(1, std::memory_order_acquire);
19 }
20
21 void lock() {
22 while (true) {
23 // The value has been changed, we successfully entered the lock
24 if (0 == m_value.exchange(1, std::memory_order_acquire))
25 break;
26
27 // Yield until unlocked
28 while (m_value.load(std::memory_order_relaxed) != 0)
29 GAIA_YIELD_CPU;
30 }
31 }
32
33 void unlock() {
34 // Release the lock
35 m_value.store(0, std::memory_order_release);
36 }
37 };
38 } // namespace mt
39} // namespace gaia
Definition spinlock.h:8
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9