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() = default;
14 SpinLock(const SpinLock&) = delete;
15 SpinLock& operator=(const SpinLock&) = delete;
16
17 bool try_lock() {
18 // Attempt to acquire the lock without waiting
19 return 0 == m_value.exchange(1, std::memory_order_acquire);
20 }
21
22 void lock() {
23 while (true) {
24 // The value has been changed, we successfully entered the lock
25 if (0 == m_value.exchange(1, std::memory_order_acquire))
26 break;
27
28 // Yield until unlocked
29 while (m_value.load(std::memory_order_relaxed) != 0)
30 GAIA_YIELD_CPU;
31 }
32 }
33
34 void unlock() {
35 // Release the lock
36 m_value.store(0, std::memory_order_release);
37 }
38 };
39 } // namespace mt
40} // namespace gaia
Definition spinlock.h:8
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9