Gaia-ECS v1.0.0
A simple and powerful entity component system
Loading...
Searching...
No Matches
jobqueue.h
1#pragma once
2
3#include "gaia/config/config.h"
4
5#include <atomic>
6
7#include "gaia/cnt/sarray.h"
8#include "gaia/config/profiler.h"
9#include "gaia/core/utility.h"
10#include "gaia/mt/jobhandle.h"
11
12// MSVC might warn about applying additional padding around alignas usage.
13// This is perfectly fine but can cause builds with warning-as-error turned on to fail.
14GAIA_MSVC_WARNING_PUSH()
15GAIA_MSVC_WARNING_DISABLE(4324)
16
17namespace gaia {
18 namespace mt {
19
22 template <const uint32_t N = 1 << 12>
23 class JobQueue {
24 static_assert(N >= 2);
25 static_assert((N & (N - 1)) == 0, "Extent of JobQueue must be a power of 2");
26 static constexpr uint32_t MASK = N - 1;
27
28 static_assert(sizeof(std::atomic_uint32_t) == sizeof(JobHandle));
29 cnt::sarray<std::atomic_uint32_t, N> m_buffer;
30 GAIA_ALIGNAS(GAIA_CACHELINE_SIZE) std::atomic_uint32_t m_bottom;
31 GAIA_ALIGNAS(GAIA_CACHELINE_SIZE) std::atomic_uint32_t m_top;
32
33 public:
34 JobQueue() {
35 clear();
36 }
37
38 ~JobQueue() = default;
39 JobQueue(const JobQueue&) = default;
40 JobQueue& operator=(const JobQueue&) = default;
41 JobQueue(JobQueue&&) noexcept = default;
42 JobQueue& operator=(JobQueue&&) noexcept = default;
43
44 void clear() {
45 m_bottom.store(0);
46 m_top.store(0);
47 for (auto& val: m_buffer)
48 val.store(((JobHandle)JobNull_t()).value());
49 }
50
53 bool empty() const {
54 GAIA_PROF_SCOPE(JobQueue::empty);
55
56 const uint32_t b = m_bottom.load(std::memory_order_relaxed);
57 const uint32_t t = m_top.load(std::memory_order_relaxed);
58 return int32_t(b - t) <= 0; // b<=t, but handles overflows, too
59 }
60
64 GAIA_NODISCARD bool try_push(JobHandle jobHandle) {
65 GAIA_PROF_SCOPE(JobQueue::try_push);
66
67 const uint32_t b = m_bottom.load(std::memory_order_relaxed);
68 const uint32_t t = m_top.load(std::memory_order_acquire);
69 const uint32_t used = b - t;
70 if (used > MASK)
71 return false;
72
73 m_buffer[b & MASK].store(jobHandle.value(), std::memory_order_relaxed);
74 // Publish the handle together with the updated queue boundary.
75 m_bottom.store(b + 1, std::memory_order_release);
76
77 return true;
78 }
79
83 GAIA_NODISCARD uint32_t try_push(std::span<JobHandle> jobHandles) {
84 GAIA_PROF_SCOPE(JobQueue::try_push);
85
86 const uint32_t cnt = (uint32_t)jobHandles.size();
87 uint32_t b = m_bottom.load(std::memory_order_relaxed);
88 const uint32_t t = m_top.load(std::memory_order_acquire);
89 const uint32_t used = b - t;
90 const uint32_t free = (MASK + 1) - used;
91 const uint32_t freeFinal = core::get_min(cnt, free);
92
93 for (uint32_t i = 0; i < freeFinal; i++, b++)
94 m_buffer[b & MASK].store(jobHandles[i].value(), std::memory_order_relaxed);
95 // Publish all handles together with the updated queue boundary.
96 m_bottom.store(b, std::memory_order_release);
97
98 return freeFinal;
99 }
100
104 GAIA_NODISCARD bool try_pop(JobHandle& jobHandle) {
105 GAIA_PROF_SCOPE(JobQueue::try_pop);
106
107 uint32_t jobHandleValue = ((JobHandle)JobNull_t{}).value();
108
109 const uint32_t b = m_bottom.load(std::memory_order_relaxed) - 1;
110 m_bottom.store(b, std::memory_order_relaxed);
111 std::atomic_thread_fence(std::memory_order_seq_cst);
112 uint32_t t = m_top.load(std::memory_order_relaxed);
113
114 if (int(t - b) <= 0) { // t <= b, but handles overflows, too
115 // non-empty queue
116 jobHandleValue = m_buffer[b & MASK].load(std::memory_order_relaxed);
117
118 if (t == b) {
119 // last element in the queue
120 const bool ret =
121 m_top.compare_exchange_strong(t, t + 1, std::memory_order_seq_cst, std::memory_order_relaxed);
122 m_bottom.store(b + 1, std::memory_order_relaxed);
123 jobHandle = JobHandle(jobHandleValue);
124 GAIA_ASSERT(jobHandle != (JobHandle)JobNull_t{});
125 return ret; // false = failed race, don't use jobHandle; true = found a result
126 }
127
128 jobHandle = JobHandle(jobHandleValue);
129 GAIA_ASSERT(jobHandle != (JobHandle)JobNull_t{});
130 return true;
131 }
132
133 // empty queue
134 m_bottom.store(b + 1, std::memory_order_relaxed);
135 return false; // false = empty, don't use jobHandle
136 }
137
141 GAIA_NODISCARD bool try_steal(JobHandle& jobHandle) {
142 GAIA_PROF_SCOPE(JobQueue::try_steal);
143
144 uint32_t t = m_top.load(std::memory_order_acquire);
145 std::atomic_thread_fence(std::memory_order_seq_cst);
146 const uint32_t b = m_bottom.load(std::memory_order_acquire);
147
148 if (int(b - t) <= 0) { // t >= b, but handles overflows, too
149 jobHandle = (JobHandle)JobNull_t{};
150 return true; // true + JobNull = empty, don't use jobHandle
151 }
152
153 const uint32_t jobHandleValue = m_buffer[t & MASK].load(std::memory_order_relaxed);
154
155 // We fail if concurrent pop()/steal() operation changed the current top
156 const bool ret = m_top.compare_exchange_strong(t, t + 1, std::memory_order_seq_cst, std::memory_order_relaxed);
157 jobHandle = JobHandle(jobHandleValue);
158 GAIA_ASSERT(jobHandle != (JobHandle)JobNull_t{});
159 return ret; // false = failed race, don't use jobHandle; true = found a result
160 }
161 };
162
165 template <class T, const uint32_t N = 1 << 12>
166 class MpmcQueue {
167 static_assert(N >= 2);
168 static_assert((N & (N - 1)) == 0, "Extent of MpmcQueue must be a power of 2");
169 static constexpr uint32_t MASK = N - 1;
170
171 struct Node {
172 std::atomic_uint32_t sequence{};
173 T item;
174 };
175 using view_policy = mem::data_view_policy_aos<Node>;
176
177 static constexpr uint32_t extent = N;
178 static constexpr uint32_t allocated_bytes = view_policy::get_min_byte_size(0, N);
179
180 // MSVC might warn about applying additional padding to an instance of StackAllocator.
181 // This is perfectly fine, but might make builds with warning-as-error turned on to fail.
182 GAIA_MSVC_WARNING_PUSH()
183 GAIA_MSVC_WARNING_DISABLE(4324)
184
185 mem::raw_data_holder<Node, allocated_bytes> m_data;
186 GAIA_ALIGNAS(GAIA_CACHELINE_SIZE) std::atomic_uint32_t m_pushPos;
187 GAIA_ALIGNAS(GAIA_CACHELINE_SIZE) std::atomic_uint32_t m_popPos;
188
189 GAIA_MSVC_WARNING_POP()
190
191 public:
192 MpmcQueue() {
193 init();
194 }
195 ~MpmcQueue() {
196 free();
197 }
198
199 MpmcQueue(MpmcQueue&&) = delete;
200 MpmcQueue(const MpmcQueue&) = delete;
201 MpmcQueue& operator=(MpmcQueue&&) = delete;
202 MpmcQueue& operator=(const MpmcQueue&) = delete;
203
204 private:
205 GAIA_NODISCARD constexpr Node* data() noexcept {
206 return GAIA_ACC((Node*)&m_data[0]);
207 }
208
209 GAIA_NODISCARD constexpr const Node* data() const noexcept {
210 return GAIA_ACC((const Node*)&m_data[0]);
211 }
212
213 void init() {
214 Node* pNodes = data();
215
216 GAIA_FOR(extent) {
217 Node* pNode = &pNodes[i];
218 core::call_ctor(&pNode->sequence, i);
219 }
220
221 m_pushPos.store(0, std::memory_order_relaxed);
222 m_popPos.store(0, std::memory_order_relaxed);
223 }
224
225 void free() {
226 Node* pNodes = data();
227
228 uint32_t enqPos = m_pushPos.load(std::memory_order_relaxed);
229 uint32_t deqPos = m_popPos.load(std::memory_order_relaxed);
230 for (uint32_t pos = deqPos; pos != enqPos; ++pos) {
231 Node* pNode = &pNodes[pos & MASK];
232 if (pNode->sequence.load(std::memory_order_relaxed) == pos + 1)
233 pNode->item.~T();
234 }
235
236 GAIA_FOR(extent) {
237 Node* pNode = &pNodes[i];
238 core::call_dtor(&pNode->sequence);
239 }
240 }
241
242 public:
245 bool empty() const {
246 GAIA_PROF_SCOPE(MpmcQueue::empty);
247
248 const uint32_t pos = m_popPos.load(std::memory_order_relaxed);
249 const auto* pNode = &data()[pos & MASK];
250 const uint32_t seq = pNode->sequence.load(std::memory_order_acquire);
251 return pos >= seq;
252 }
253
257 template <typename TT>
258 bool try_push(TT&& item) {
259 GAIA_PROF_SCOPE(MpmcQueue::try_push);
260
261 Node* pNodes = data();
262
263 Node* pNode = nullptr;
264 uint32_t pos = m_pushPos.load(std::memory_order_relaxed);
265 while (true) {
266 pNode = &pNodes[pos & MASK];
267 uint32_t seq = pNode->sequence.load(std::memory_order_acquire);
268 int32_t diff = int32_t(seq) - int32_t(pos);
269 if (diff == 0) {
270 if (m_pushPos.compare_exchange_weak(pos, pos + 1, std::memory_order_relaxed))
271 break;
272 } else if (diff < 0) {
273 // The queue is full, we can't push
274 return false;
275 } else {
276 pos = m_pushPos.load(std::memory_order_relaxed);
277 }
278 }
279
280 core::call_ctor(&pNode->item, GAIA_FWD(item));
281 pNode->sequence.store(pos + 1, std::memory_order_release);
282 return true;
283 }
284
288 bool try_pop(T& item) {
289 GAIA_PROF_SCOPE(MpmcQueue::try_pop);
290
291 Node* pNodes = data();
292
293 Node* pNode = nullptr;
294 uint32_t pos = m_popPos.load(std::memory_order_relaxed);
295 while (true) {
296 pNode = &pNodes[pos & MASK];
297 uint32_t seq = pNode->sequence.load(std::memory_order_acquire);
298 int32_t diff = int32_t(seq) - int32_t(pos + 1);
299 if (diff == 0) {
300 if (m_popPos.compare_exchange_weak(pos, pos + 1, std::memory_order_relaxed))
301 break;
302 } else if (diff < 0) {
303 // The queue is empty, we can't pop
304 return false;
305 } else {
306 pos = m_popPos.load(std::memory_order_relaxed);
307 }
308 }
309
310 item = GAIA_MOV(pNode->item);
311 core::call_dtor(&pNode->item);
312 pNode->sequence.store(pos + MASK + 1, std::memory_order_release);
313 return true;
314 }
315 };
316 } // namespace mt
317} // namespace gaia
318
319GAIA_MSVC_WARNING_POP()