Gaia-ECS v0.9.3
A simple and powerful entity component system
Loading...
Searching...
No Matches
futex.h
1#pragma once
2#include "gaia/config/config.h"
3#include "gaia/config/profiler.h"
4
5#include <atomic>
6#include <mutex>
7
8#include "gaia/core/utility.h"
9#include "gaia/mt/event.h"
10
11namespace gaia {
12 namespace mt {
13 namespace detail {
14 inline static constexpr uint32_t WaitMaskAll = 0x7FFFFFFF;
15 inline static constexpr uint32_t WaitMaskAny = ~0u;
16
18 FutexWaitNode* pNext = nullptr;
19 const std::atomic_uint32_t* pFutexValue = nullptr;
20 uint32_t waitMask = WaitMaskAny;
21 Event evt;
22 };
23
24 struct FutexBucket {
25 GAIA_PROF_MUTEX(std::mutex, mtx);
26 FutexWaitNode* pFirst = nullptr;
27
28 // Since there shouldn't be that many threads waiting at any one time, this seems like a good
29 // number of hash table buckets. Making it prime number for better spread.
30 static constexpr uint32_t BUCKET_SIZE = 37;
31
32 static FutexBucket& get(const std::atomic_uint32_t* pFutexValue) {
33 static FutexBucket s_buckets[BUCKET_SIZE];
34 return s_buckets[(uintptr_t(pFutexValue) >> 2) % BUCKET_SIZE];
35 }
36 };
37
40 inline thread_local FutexWaitNode t_WaitNode;
41
42 } // namespace detail
43
55 struct Futex {
56 enum class Result {
58 Change,
60 WakeUp
61 };
62
69 static Result wait(const std::atomic_uint32_t* pFutexValue, uint32_t expected, uint32_t waitMask) {
70 GAIA_PROF_SCOPE(futex::wait);
71
72 GAIA_ASSERT(waitMask != 0);
73
74 auto& bucket = detail::FutexBucket::get(pFutexValue);
75 auto& node = detail::t_WaitNode;
76 node.pFutexValue = pFutexValue;
77 node.waitMask = waitMask;
78
79 {
80 auto& mtx = GAIA_PROF_EXTRACT_MUTEX(bucket.mtx);
81 core::lock_scope lock(mtx);
82 GAIA_PROF_LOCK_MARK(bucket.mtx);
83
84 const uint32_t futexValue = pFutexValue->load(std::memory_order_relaxed);
85 if (futexValue != expected)
86 return Result::Change;
87
88 node.pNext = bucket.pFirst;
89 bucket.pFirst = &node;
90 }
91
92 node.evt.wait();
93 return Result::WakeUp;
94 }
95
100 static uint32_t
101 wake(const std::atomic_uint32_t* pFutexValue, uint32_t wakeCount, uint32_t wakeMask = detail::WaitMaskAny) {
102 GAIA_PROF_SCOPE(futex::wake);
103
104 GAIA_ASSERT(wakeMask != 0);
105
106 auto& bucket = detail::FutexBucket::get(pFutexValue);
107 auto& mtx = GAIA_PROF_EXTRACT_MUTEX(bucket.mtx);
108 core::lock_scope lock(mtx);
109 GAIA_PROF_LOCK_MARK(bucket.mtx);
110
111 uint32_t numAwoken = 0;
112 auto** ppNode = &bucket.pFirst;
113 for (auto* pNode = *ppNode; numAwoken < wakeCount && pNode != nullptr; pNode = *ppNode) {
114 if (pNode->pFutexValue == pFutexValue && (pNode->waitMask & wakeMask) != 0) {
115 ++numAwoken;
116
117 // Unlink the node
118 *ppNode = pNode->pNext;
119 pNode->pNext = nullptr;
120
121 pNode->evt.set();
122 } else {
123 ppNode = &pNode->pNext;
124 }
125 }
126
127 return numAwoken;
128 }
129 };
130 } // namespace mt
131} // namespace gaia
Definition event.h:18
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9
Definition utility.h:123
An implementation of a simple futex (fast userspace mutex). Only wait and wake are implemented.
Definition futex.h:55
static Result wait(const std::atomic_uint32_t *pFutexValue, uint32_t expected, uint32_t waitMask)
Suspends the caller on the futex while its value remains expected.
Definition futex.h:69
static uint32_t wake(const std::atomic_uint32_t *pFutexValue, uint32_t wakeCount, uint32_t wakeMask=detail::WaitMaskAny)
Wakes up to wakeCount waiters whose waitMask matches wakeMask.
Definition futex.h:101
Result
Definition futex.h:56
@ WakeUp
Futex woken up as a result of wake()
@ Change
Futex value didn't match the expected one.
Definition futex.h:24
Definition futex.h:17