GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
synchronization.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_SYSTEM_SYNCHRONIZATION_HPP
5#define GREM_CORE_SYSTEM_SYNCHRONIZATION_HPP
6
8
13#include <GREM/core/time.hpp>
14
15#ifdef GREM_USE_MULTITHREADING
16#include <atomic> // std::memory_order, std::atomic..., std::kill_dependency
17#include <mutex> // std::...mutex, std::...lock, std::...lock_t
18#include <shared_mutex> // std::shared_mutex
19#include <version> // __cpp_lib_atomic_ref
20#else
22
23#include <system_error> // std::system_error, std::errc, std::make_error_code(std::errc)
24#include <utility> // std::exchange, std::swap, std::...index_sequence
25#endif
26#include <cstddef> // std::size_t, std::ptrdiff_t
27#include <type_traits> // std::is_..._v, std::remove_..._t, std::underlying_type_t
28
29namespace grem {
30
31enum class MemoryOrder : int { // NOLINT(performance-enum-size)
32#ifdef GREM_USE_MULTITHREADING
33 RELAXED = static_cast<int>(static_cast<std::underlying_type_t<std::memory_order>>(std::memory_order::relaxed)),
34 CONSUME = static_cast<int>(static_cast<std::underlying_type_t<std::memory_order>>(std::memory_order::consume)),
35 ACQUIRE = static_cast<int>(static_cast<std::underlying_type_t<std::memory_order>>(std::memory_order::acquire)),
36 RELEASE = static_cast<int>(static_cast<std::underlying_type_t<std::memory_order>>(std::memory_order::release)),
37 ACQUIRE_RELEASE = static_cast<int>(static_cast<std::underlying_type_t<std::memory_order>>(std::memory_order::acq_rel)),
38 SEQUENTIALLY_CONSISTENT = static_cast<int>(static_cast<std::underlying_type_t<std::memory_order>>(std::memory_order::seq_cst)),
39#else
46#endif
47};
48
49namespace detail {
50
51#ifdef GREM_USE_MULTITHREADING
52[[nodiscard]] constexpr std::memory_order translateMemoryOrder(MemoryOrder order) noexcept {
53 return static_cast<std::memory_order>(static_cast<std::underlying_type_t<std::memory_order>>(static_cast<int>(order)));
54}
55#if __cpp_lib_atomic_ref < 201806L
56[[nodiscard]] constexpr int translateMemoryOrderBuiltin(MemoryOrder order) noexcept {
57 switch (order) {
58 case MemoryOrder::RELAXED: return __ATOMIC_RELAXED;
59 case MemoryOrder::CONSUME: return __ATOMIC_CONSUME;
60 case MemoryOrder::ACQUIRE: return __ATOMIC_ACQUIRE;
61 case MemoryOrder::RELEASE: return __ATOMIC_RELEASE;
62 case MemoryOrder::ACQUIRE_RELEASE: return __ATOMIC_ACQ_REL;
63 case MemoryOrder::SEQUENTIALLY_CONSISTENT: return __ATOMIC_SEQ_CST;
64 }
65 return 0;
66}
67#endif
68#endif
69
70template <typename T>
71struct AtomicBase {
72 using value_type = T;
73};
74
75template <typename T>
76struct AtomicBase<T*> {
77 using value_type = T*;
78 using difference_type = ptrdiff_t;
79};
80
81template <typename T>
82requires(std::is_integral_v<T> || std::is_floating_point_v<T>) struct AtomicBase<T> {
83 using value_type = T;
84 using difference_type = value_type;
85};
86
87} // namespace detail
88
89template <typename T>
90class Atomic : public detail::AtomicBase<T> {
91public:
92#ifdef GREM_USE_MULTITHREADING
93 static constexpr bool is_always_lock_free = std::atomic<T>::is_always_lock_free;
94#else
95 static constexpr bool is_always_lock_free = true;
96#endif
97
98 constexpr Atomic() noexcept(std::is_nothrow_default_constructible_v<T>) = default;
99
100 GREM_ALWAYS_INLINE Atomic(T value) noexcept
101 : value(value) {}
102
103 Atomic(const Atomic&) = delete;
104
105 Atomic& operator=(const Atomic&) = delete;
106 Atomic& operator=(const Atomic&) volatile = delete;
107
108 [[nodiscard]] GREM_ALWAYS_INLINE bool is_lock_free() const noexcept {
109#ifdef GREM_USE_MULTITHREADING
110 return value.is_lock_free();
111#else
112 return true;
113#endif
114 }
115
116 [[nodiscard]] GREM_ALWAYS_INLINE bool is_lock_free() const volatile noexcept {
117#ifdef GREM_USE_MULTITHREADING
118 return value.is_lock_free();
119#else
120 return true;
121#endif
122 }
123
125#ifdef GREM_USE_MULTITHREADING
126 value.store(newValue, detail::translateMemoryOrder(order));
127#else
128 (void)order;
129 value = newValue;
130#endif
131 }
132
133 GREM_ALWAYS_INLINE void store(T newValue, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) volatile noexcept {
134#ifdef GREM_USE_MULTITHREADING
135 value.store(newValue, detail::translateMemoryOrder(order));
136#else
137 (void)order;
138 value = newValue;
139#endif
140 }
141
143#ifdef GREM_USE_MULTITHREADING
144 return value.load(detail::translateMemoryOrder(order));
145#else
146 (void)order;
147 return value;
148#endif
149 }
150
151 [[nodiscard]] GREM_ALWAYS_INLINE T load(MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) const volatile noexcept {
152#ifdef GREM_USE_MULTITHREADING
153 return value.load(detail::translateMemoryOrder(order));
154#else
155 (void)order;
156 return value;
157#endif
158 }
159
160 [[nodiscard]] GREM_ALWAYS_INLINE T exchange(T newValue, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) noexcept {
161#ifdef GREM_USE_MULTITHREADING
162 return value.exchange(newValue, detail::translateMemoryOrder(order));
163#else
164 (void)order;
165 return std::exchange(value, newValue);
166#endif
167 }
168
169 [[nodiscard]] GREM_ALWAYS_INLINE T exchange(T newValue, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) volatile noexcept {
170#ifdef GREM_USE_MULTITHREADING
171 return value.exchange(newValue, detail::translateMemoryOrder(order));
172#else
173 (void)order;
174 return std::exchange(value, newValue);
175#endif
176 }
177
178 GREM_ALWAYS_INLINE bool compare_exchange_weak(T& expected, T desired, MemoryOrder success, MemoryOrder failure) noexcept {
179#ifdef GREM_USE_MULTITHREADING
180 return value.compare_exchange_weak(expected, desired, detail::translateMemoryOrder(success), detail::translateMemoryOrder(failure));
181#else
182 (void)success;
183 (void)failure;
184 if (memcmp(&value, &expected, sizeof(T)) == 0) {
185 value = desired;
186 return true;
187 }
188 expected = value;
189 return false;
190#endif
191 }
192
193 GREM_ALWAYS_INLINE bool compare_exchange_weak(T& expected, T desired, MemoryOrder success, MemoryOrder failure) volatile noexcept {
194#ifdef GREM_USE_MULTITHREADING
195 return value.compare_exchange_weak(expected, desired, detail::translateMemoryOrder(success), detail::translateMemoryOrder(failure));
196#else
197 (void)success;
198 (void)failure;
199 if (memcmp(&value, &expected, sizeof(T)) == 0) {
200 value = desired;
201 return true;
202 }
203 expected = value;
204 return false;
205#endif
206 }
207
209#ifdef GREM_USE_MULTITHREADING
210 return value.compare_exchange_weak(expected, desired, detail::translateMemoryOrder(order));
211#else
212 (void)order;
213 if (memcmp(&value, &expected, sizeof(T)) == 0) {
214 value = desired;
215 return true;
216 }
217 expected = value;
218 return false;
219#endif
220 }
221
222 GREM_ALWAYS_INLINE bool compare_exchange_weak(T& expected, T desired, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) volatile noexcept {
223#ifdef GREM_USE_MULTITHREADING
224 return value.compare_exchange_weak(expected, desired, detail::translateMemoryOrder(order));
225#else
226 (void)order;
227 if (memcmp(&value, &expected, sizeof(T)) == 0) {
228 value = desired;
229 return true;
230 }
231 expected = value;
232 return false;
233#endif
234 }
235
236 GREM_ALWAYS_INLINE bool compare_exchange_strong(T& expected, T desired, MemoryOrder success, MemoryOrder failure) noexcept {
237#ifdef GREM_USE_MULTITHREADING
238 return value.compare_exchange_strong(expected, desired, detail::translateMemoryOrder(success), detail::translateMemoryOrder(failure));
239#else
240 (void)success;
241 (void)failure;
242 if (memcmp(&value, &expected, sizeof(T)) == 0) {
243 value = desired;
244 return true;
245 }
246 expected = value;
247 return false;
248#endif
249 }
250
251 GREM_ALWAYS_INLINE bool compare_exchange_strong(T& expected, T desired, MemoryOrder success, MemoryOrder failure) volatile noexcept {
252#ifdef GREM_USE_MULTITHREADING
253 return value.compare_exchange_strong(expected, desired, detail::translateMemoryOrder(success), detail::translateMemoryOrder(failure));
254#else
255 (void)success;
256 (void)failure;
257 if (memcmp(&value, &expected, sizeof(T)) == 0) {
258 value = desired;
259 return true;
260 }
261 expected = value;
262 return false;
263#endif
264 }
265
267#ifdef GREM_USE_MULTITHREADING
268 return value.compare_exchange_strong(expected, desired, detail::translateMemoryOrder(order));
269#else
270 (void)order;
271 if (memcmp(&value, &expected, sizeof(T)) == 0) {
272 value = desired;
273 return true;
274 }
275 expected = value;
276 return false;
277#endif
278 }
279
280 GREM_ALWAYS_INLINE bool compare_exchange_strong(T& expected, T desired, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) volatile noexcept {
281#ifdef GREM_USE_MULTITHREADING
282 return value.compare_exchange_strong(expected, desired, detail::translateMemoryOrder(order));
283#else
284 (void)order;
285 if (memcmp(&value, &expected, sizeof(T)) == 0) {
286 value = desired;
287 return true;
288 }
289 expected = value;
290 return false;
291#endif
292 }
293
295#ifdef GREM_USE_MULTITHREADING
296 value.wait(old, detail::translateMemoryOrder(order));
297#else
298 (void)old;
299 (void)order;
300 GREM_ASSERT(memcmp(&value, &old, sizeof(T)) != 0);
301#endif
302 }
303
304 GREM_ALWAYS_INLINE void wait(T old, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) const volatile noexcept {
305#ifdef GREM_USE_MULTITHREADING
306 value.wait(old, detail::translateMemoryOrder(order));
307#else
308 (void)old;
309 (void)order;
310 GREM_ASSERT(memcmp(&value, &old, sizeof(T)) != 0);
311#endif
312 }
313
315#ifdef GREM_USE_MULTITHREADING
316 value.notify_one();
317#endif
318 }
319
320 GREM_ALWAYS_INLINE void notify_one() volatile noexcept {
321#ifdef GREM_USE_MULTITHREADING
322 value.notify_one();
323#endif
324 }
325
327#ifdef GREM_USE_MULTITHREADING
328 value.notify_all();
329#endif
330 }
331
332 GREM_ALWAYS_INLINE void notify_all() volatile noexcept {
333#ifdef GREM_USE_MULTITHREADING
334 value.notify_all();
335#endif
336 }
337
338 GREM_ALWAYS_INLINE T fetch_add(T arg, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) noexcept requires(std::is_integral_v<T> || std::is_floating_point_v<T>) {
339#ifdef GREM_USE_MULTITHREADING
340 return value.fetch_add(arg, detail::translateMemoryOrder(order));
341#else
342 (void)order;
343 const T old = value;
344 value += arg;
345 return old;
346#endif
347 }
348
349 GREM_ALWAYS_INLINE T fetch_add(T arg, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) volatile noexcept requires(std::is_integral_v<T> || std::is_floating_point_v<T>)
350 {
351#ifdef GREM_USE_MULTITHREADING
352 return value.fetch_add(arg, detail::translateMemoryOrder(order));
353#else
354 (void)order;
355 const T old = value;
356 value += arg;
357 return old;
358#endif
359 }
360
361 GREM_ALWAYS_INLINE T fetch_add(std::ptrdiff_t arg, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) noexcept requires(std::is_pointer_v<T>) {
362#ifdef GREM_USE_MULTITHREADING
363 return value.fetch_add(arg, detail::translateMemoryOrder(order));
364#else
365 (void)order;
366 const T old = value;
367 value += arg;
368 return old;
369#endif
370 }
371
372 GREM_ALWAYS_INLINE T fetch_add(std::ptrdiff_t arg, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) volatile noexcept requires(std::is_pointer_v<T>) {
373#ifdef GREM_USE_MULTITHREADING
374 return value.fetch_add(arg, detail::translateMemoryOrder(order));
375#else
376 (void)order;
377 const T old = value;
378 value += arg;
379 return old;
380#endif
381 }
382
383 GREM_ALWAYS_INLINE T fetch_sub(T arg, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) noexcept requires(std::is_integral_v<T> || std::is_floating_point_v<T>) {
384#ifdef GREM_USE_MULTITHREADING
385 return value.fetch_sub(arg, detail::translateMemoryOrder(order));
386#else
387 (void)order;
388 const T old = value;
389 value -= arg;
390 return old;
391#endif
392 }
393
394 GREM_ALWAYS_INLINE T fetch_sub(T arg, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) volatile noexcept requires(std::is_integral_v<T> || std::is_floating_point_v<T>)
395 {
396#ifdef GREM_USE_MULTITHREADING
397 return value.fetch_sub(arg, detail::translateMemoryOrder(order));
398#else
399 (void)order;
400 const T old = value;
401 value -= arg;
402 return old;
403#endif
404 }
405
406 GREM_ALWAYS_INLINE T fetch_sub(std::ptrdiff_t arg, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) noexcept requires(std::is_pointer_v<T>) {
407#ifdef GREM_USE_MULTITHREADING
408 return value.fetch_sub(arg, detail::translateMemoryOrder(order));
409#else
410 (void)order;
411 const T old = value;
412 value -= arg;
413 return old;
414#endif
415 }
416
417 GREM_ALWAYS_INLINE T fetch_sub(std::ptrdiff_t arg, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) volatile noexcept requires(std::is_pointer_v<T>) {
418#ifdef GREM_USE_MULTITHREADING
419 return value.fetch_sub(arg, detail::translateMemoryOrder(order));
420#else
421 (void)order;
422 const T old = value;
423 value -= arg;
424 return old;
425#endif
426 }
427
428 GREM_ALWAYS_INLINE T fetch_and(T arg, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) noexcept requires(std::is_integral_v<T>) {
429#ifdef GREM_USE_MULTITHREADING
430 return value.fetch_and(arg, detail::translateMemoryOrder(order));
431#else
432 (void)order;
433 const T old = value;
434 value &= arg;
435 return old;
436#endif
437 }
438
439 GREM_ALWAYS_INLINE T fetch_and(T arg, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) volatile noexcept requires(std::is_integral_v<T>) {
440#ifdef GREM_USE_MULTITHREADING
441 return value.fetch_and(arg, detail::translateMemoryOrder(order));
442#else
443 (void)order;
444 const T old = value;
445 value &= arg;
446 return old;
447#endif
448 }
449
450 GREM_ALWAYS_INLINE T fetch_or(T arg, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) noexcept requires(std::is_integral_v<T>) {
451#ifdef GREM_USE_MULTITHREADING
452 return value.fetch_or(arg, detail::translateMemoryOrder(order));
453#else
454 (void)order;
455 const T old = value;
456 value |= arg;
457 return old;
458#endif
459 }
460
461 GREM_ALWAYS_INLINE T fetch_or(T arg, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) volatile noexcept requires(std::is_integral_v<T>) {
462#ifdef GREM_USE_MULTITHREADING
463 return value.fetch_or(arg, detail::translateMemoryOrder(order));
464#else
465 (void)order;
466 const T old = value;
467 value |= arg;
468 return old;
469#endif
470 }
471
472 GREM_ALWAYS_INLINE T fetch_xor(T arg, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) noexcept requires(std::is_integral_v<T>) {
473#ifdef GREM_USE_MULTITHREADING
474 return value.fetch_xor(arg, detail::translateMemoryOrder(order));
475#else
476 (void)order;
477 const T old = value;
478 value ^= arg;
479 return old;
480#endif
481 }
482
483 GREM_ALWAYS_INLINE T fetch_xor(T arg, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) volatile noexcept requires(std::is_integral_v<T>) {
484#ifdef GREM_USE_MULTITHREADING
485 return value.fetch_xor(arg, detail::translateMemoryOrder(order));
486#else
487 (void)order;
488 const T old = value;
489 value ^= arg;
490 return old;
491#endif
492 }
493
494private:
495#ifdef GREM_USE_MULTITHREADING
496 std::atomic<T> value;
497#else
498 T value;
499#endif
500};
501
503public:
504 constexpr AtomicFlag() noexcept = default;
505
506 AtomicFlag(const AtomicFlag&) = delete;
507
508 AtomicFlag& operator=(const AtomicFlag&) = delete;
509
511#ifdef GREM_USE_MULTITHREADING
512 value.clear(detail::translateMemoryOrder(order));
513#else
514 (void)order;
515 value = false;
516#endif
517 }
518
520#ifdef GREM_USE_MULTITHREADING
521 return value.test_and_set(detail::translateMemoryOrder(order));
522#else
523 (void)order;
524 const bool old = value;
525 value = true;
526 return old;
527#endif
528 }
529
530 [[nodiscard]] GREM_ALWAYS_INLINE bool test(MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept {
531#ifdef GREM_USE_MULTITHREADING
532 return value.test(detail::translateMemoryOrder(order));
533#else
534 (void)order;
535 return value;
536#endif
537 }
538
540#ifdef GREM_USE_MULTITHREADING
541 value.wait(old, detail::translateMemoryOrder(order));
542#else
543 (void)old;
544 (void)order;
545 GREM_ASSERT(value != old);
546#endif
547 }
548
550#ifdef GREM_USE_MULTITHREADING
551 value.notify_one();
552#endif
553 }
554
556#ifdef GREM_USE_MULTITHREADING
557 value.notify_all();
558#endif
559 }
560
561private:
562#ifdef GREM_USE_MULTITHREADING
563 std::atomic_flag value{};
564#else
565 bool value = false;
566#endif
567};
568
570#ifdef GREM_USE_MULTITHREADING
571 std::atomic_thread_fence(detail::translateMemoryOrder(order));
572#else
573 (void)order;
574#endif
575}
576
578#ifdef GREM_USE_MULTITHREADING
579 std::atomic_signal_fence(detail::translateMemoryOrder(order));
580#else
581 (void)order;
582#endif
583}
584
585template <typename T>
587#ifdef GREM_USE_MULTITHREADING
588 std::kill_dependency(y);
589#else
590 return y;
591#endif
592}
593
594template <typename T>
595class AtomicRef : public detail::AtomicBase<std::remove_cv_t<T>> {
596public:
597 using typename detail::AtomicBase<std::remove_cv_t<T>>::value_type;
598
599#ifdef GREM_USE_MULTITHREADING
600#if __cpp_lib_atomic_ref >= 201806L
601 static constexpr bool is_always_lock_free = std::atomic_ref<T>::is_always_lock_free;
602 static constexpr std::size_t required_alignment = std::atomic_ref<T>::required_alignment;
603
604 GREM_ALWAYS_INLINE explicit AtomicRef(T& obj)
605 : ref(obj) {}
606
607 GREM_ALWAYS_INLINE AtomicRef(const AtomicRef& other) noexcept
608 : ref(other.ref) {}
609#else
610 static constexpr bool is_always_lock_free = true;
611 static constexpr std::size_t required_alignment = alignof(T);
612
613 GREM_ALWAYS_INLINE explicit AtomicRef(T& obj)
614 : ptr(&obj) {}
615
616 GREM_ALWAYS_INLINE AtomicRef(const AtomicRef& other) noexcept
617 : ptr(other.ptr) {}
618#endif
619#else
620 static constexpr bool is_always_lock_free = true;
621 static constexpr std::size_t required_alignment = alignof(T);
622
624 : ptr(&obj) {}
625
627 : ptr(other.ptr) {}
628#endif
629
630 GREM_ALWAYS_INLINE value_type operator=(value_type desired) const noexcept // NOLINT(misc-unconventional-assign-operator, cppcoreguidelines-c-copy-assignment-signature)
631 requires(!std::is_const_v<T>) {
632 store(desired);
633 return desired;
634 }
635
636 AtomicRef& operator=(const AtomicRef&) = delete;
637
638 [[nodiscard]] GREM_ALWAYS_INLINE bool is_lock_free() const noexcept {
639#ifdef GREM_USE_MULTITHREADING
640#if __cpp_lib_atomic_ref >= 201806L
641 return ref.is_lock_free();
642#else
643 return true;
644#endif
645#else
646 return true;
647#endif
648 }
649
650 GREM_ALWAYS_INLINE void store(T newValue, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept requires(!std::is_const_v<T>) {
651#ifdef GREM_USE_MULTITHREADING
652#if __cpp_lib_atomic_ref >= 201806L
653 ref.store(newValue, detail::translateMemoryOrder(order));
654#else
655 GREM_ASSERT(ptr);
656 __atomic_store(ptr, &newValue, detail::translateMemoryOrderBuiltin(order));
657#endif
658#else
659 GREM_ASSERT(ptr);
660 (void)order;
661 *ptr = newValue;
662#endif
663 }
664
666#ifdef GREM_USE_MULTITHREADING
667#if __cpp_lib_atomic_ref >= 201806L
668 return ref.load(detail::translateMemoryOrder(order));
669#else
670 GREM_ASSERT(ptr);
671 T result{};
672 __atomic_load(ptr, &result, detail::translateMemoryOrderBuiltin(order));
673 return result;
674#endif
675#else
676 (void)order;
677 return *ptr;
678#endif
679 }
680
681 [[nodiscard]] GREM_ALWAYS_INLINE T exchange(T newValue, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept requires(!std::is_const_v<T>) {
682#ifdef GREM_USE_MULTITHREADING
683#if __cpp_lib_atomic_ref >= 201806L
684 return ref.exchange(newValue, detail::translateMemoryOrder(order));
685#else
686 GREM_ASSERT(ptr);
687 T result{};
688 __atomic_exchange(ptr, &newValue, &result, detail::translateMemoryOrderBuiltin(order));
689 return result;
690#endif
691#else
692 GREM_ASSERT(ptr);
693 (void)order;
694 return std::exchange(*ptr, newValue);
695#endif
696 }
697
698 GREM_ALWAYS_INLINE bool compare_exchange_weak(T& expected, T desired, MemoryOrder success, MemoryOrder failure) const noexcept requires(!std::is_const_v<T>) {
699#ifdef GREM_USE_MULTITHREADING
700#if __cpp_lib_atomic_ref >= 201806L
701 return ref.compare_exchange_weak(expected, desired, detail::translateMemoryOrder(success), detail::translateMemoryOrder(failure));
702#else
703 GREM_ASSERT(ptr);
704 return __atomic_compare_exchange(ptr, &expected, &desired, true, detail::translateMemoryOrderBuiltin(success), detail::translateMemoryOrderBuiltin(failure));
705#endif
706#else
707 GREM_ASSERT(ptr);
708 (void)success;
709 (void)failure;
710 if (memcmp(ptr, &expected, sizeof(T)) == 0) {
711 *ptr = desired;
712 return true;
713 }
714 expected = *ptr;
715 return false;
716#endif
717 }
718
719 GREM_ALWAYS_INLINE bool compare_exchange_weak(T& expected, T desired, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept requires(!std::is_const_v<T>) {
720#ifdef GREM_USE_MULTITHREADING
721#if __cpp_lib_atomic_ref >= 201806L
722 return ref.compare_exchange_weak(expected, desired, detail::translateMemoryOrder(order));
723#else
724 GREM_ASSERT(ptr);
725 return __atomic_compare_exchange(ptr, &expected, &desired, true, detail::translateMemoryOrderBuiltin(order),
726 (order == MemoryOrder::ACQUIRE_RELEASE) ? __ATOMIC_ACQUIRE
727 : (order == MemoryOrder::RELEASE) ? __ATOMIC_RELAXED
728 : detail::translateMemoryOrderBuiltin(order));
729#endif
730#else
731 GREM_ASSERT(ptr);
732 (void)order;
733 if (memcmp(ptr, &expected, sizeof(T)) == 0) {
734 *ptr = desired;
735 return true;
736 }
737 expected = *ptr;
738 return false;
739#endif
740 }
741
742 GREM_ALWAYS_INLINE bool compare_exchange_strong(T& expected, T desired, MemoryOrder success, MemoryOrder failure) const noexcept requires(!std::is_const_v<T>) {
743#ifdef GREM_USE_MULTITHREADING
744#if __cpp_lib_atomic_ref >= 201806L
745 return ref.compare_exchange_strong(expected, desired, detail::translateMemoryOrder(success), detail::translateMemoryOrder(failure));
746#else
747 GREM_ASSERT(ptr);
748 return __atomic_compare_exchange(ptr, &expected, &desired, false, detail::translateMemoryOrderBuiltin(success), detail::translateMemoryOrderBuiltin(failure));
749#endif
750#else
751 GREM_ASSERT(ptr);
752 (void)success;
753 (void)failure;
754 if (memcmp(ptr, &expected, sizeof(T)) == 0) {
755 *ptr = desired;
756 return true;
757 }
758 expected = *ptr;
759 return false;
760#endif
761 }
762
763 GREM_ALWAYS_INLINE bool compare_exchange_strong(T& expected, T desired, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept requires(!std::is_const_v<T>) {
764#ifdef GREM_USE_MULTITHREADING
765#if __cpp_lib_atomic_ref >= 201806L
766 return ref.compare_exchange_strong(expected, desired, detail::translateMemoryOrder(order));
767#else
768 GREM_ASSERT(ptr);
769 return __atomic_compare_exchange(ptr, &expected, &desired, false, detail::translateMemoryOrderBuiltin(order),
770 (order == MemoryOrder::ACQUIRE_RELEASE) ? __ATOMIC_ACQUIRE
771 : (order == MemoryOrder::RELEASE) ? __ATOMIC_RELAXED
772 : detail::translateMemoryOrderBuiltin(order));
773#endif
774#else
775 GREM_ASSERT(ptr);
776 (void)order;
777 if (memcmp(ptr, &expected, sizeof(T)) == 0) {
778 *ptr = desired;
779 return true;
780 }
781 expected = *ptr;
782 return false;
783#endif
784 }
785
787#ifdef GREM_USE_MULTITHREADING
788#if __cpp_lib_atomic_ref >= 201806L
789 ref.wait(old, detail::translateMemoryOrder(order));
790#else
791 while (load(order) == old) {
792 }
793#endif
794#else
795 GREM_ASSERT(ptr);
796 GREM_ASSERT(memcmp(ptr, &old, sizeof(T)) != 0);
797 (void)old;
798 (void)order;
799#endif
800 }
801
803#ifdef GREM_USE_MULTITHREADING
804#if __cpp_lib_atomic_ref >= 201806L
805 ref.notify_one();
806#endif
807#endif
808 }
809
811#ifdef GREM_USE_MULTITHREADING
812#if __cpp_lib_atomic_ref >= 201806L
813 ref.notify_all();
814#endif
815#endif
816 }
817
819 requires(!std::is_const_v<T> && (std::is_integral_v<T> || std::is_floating_point_v<T>)) {
820#ifdef GREM_USE_MULTITHREADING
821#if __cpp_lib_atomic_ref >= 201806L
822 return ref.fetch_add(arg, detail::translateMemoryOrder(order));
823#else
824 GREM_ASSERT(ptr);
825 return __atomic_fetch_add(ptr, arg, detail::translateMemoryOrderBuiltin(order));
826#endif
827#else
828 GREM_ASSERT(ptr);
829 (void)order;
830 const T old = *ptr;
831 *ptr += arg;
832 return old;
833#endif
834 }
835
837 requires(!std::is_const_v<T> && std::is_pointer_v<T>) {
838#ifdef GREM_USE_MULTITHREADING
839#if __cpp_lib_atomic_ref >= 201806L
840 return ref.fetch_add(arg, detail::translateMemoryOrder(order));
841#else
842 GREM_ASSERT(ptr);
843 return __atomic_fetch_add(ptr, arg, detail::translateMemoryOrderBuiltin(order));
844#endif
845#else
846 GREM_ASSERT(ptr);
847 (void)order;
848 const T old = *ptr;
849 *ptr += arg;
850 return old;
851#endif
852 }
853
855 requires(!std::is_const_v<T> && (std::is_integral_v<T> || std::is_floating_point_v<T>)) {
856#ifdef GREM_USE_MULTITHREADING
857#if __cpp_lib_atomic_ref >= 201806L
858 return ref.fetch_sub(arg, detail::translateMemoryOrder(order));
859#else
860 GREM_ASSERT(ptr);
861 return __atomic_fetch_sub(ptr, arg, detail::translateMemoryOrderBuiltin(order));
862#endif
863#else
864 GREM_ASSERT(ptr);
865 (void)order;
866 const T old = *ptr;
867 *ptr -= arg;
868 return old;
869#endif
870 }
871
873 requires(!std::is_const_v<T> && std::is_pointer_v<T>) {
874#ifdef GREM_USE_MULTITHREADING
875#if __cpp_lib_atomic_ref >= 201806L
876 return ref.fetch_sub(arg, detail::translateMemoryOrder(order));
877#else
878 GREM_ASSERT(ptr);
879 return __atomic_fetch_sub(ptr, arg, detail::translateMemoryOrderBuiltin(order));
880#endif
881#else
882 GREM_ASSERT(ptr);
883 (void)order;
884 const T old = *ptr;
885 *ptr -= arg;
886 return old;
887#endif
888 }
889
890 GREM_ALWAYS_INLINE T fetch_and(T arg, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept requires(!std::is_const_v<T> && std::is_integral_v<T>) {
891#ifdef GREM_USE_MULTITHREADING
892#if __cpp_lib_atomic_ref >= 201806L
893 return ref.fetch_and(arg, detail::translateMemoryOrder(order));
894#else
895 GREM_ASSERT(ptr);
896 return __atomic_fetch_and(ptr, arg, detail::translateMemoryOrderBuiltin(order));
897#endif
898#else
899 GREM_ASSERT(ptr);
900 (void)order;
901 const T old = *ptr;
902 *ptr &= arg;
903 return old;
904#endif
905 }
906
907 GREM_ALWAYS_INLINE T fetch_or(T arg, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept requires(!std::is_const_v<T> && std::is_integral_v<T>) {
908#ifdef GREM_USE_MULTITHREADING
909#if __cpp_lib_atomic_ref >= 201806L
910 return ref.fetch_or(arg, detail::translateMemoryOrder(order));
911#else
912 GREM_ASSERT(ptr);
913 return __atomic_fetch_or(ptr, arg, detail::translateMemoryOrderBuiltin(order));
914#endif
915#else
916 GREM_ASSERT(ptr);
917 (void)order;
918 const T old = *ptr;
919 *ptr |= arg;
920 return old;
921#endif
922 }
923
924 GREM_ALWAYS_INLINE T fetch_xor(T arg, MemoryOrder order = MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept requires(!std::is_const_v<T> && std::is_integral_v<T>) {
925#ifdef GREM_USE_MULTITHREADING
926#if __cpp_lib_atomic_ref >= 201806L
927 return ref.fetch_xor(arg, detail::translateMemoryOrder(order));
928#else
929 GREM_ASSERT(ptr);
930 return __atomic_fetch_xor(ptr, arg, detail::translateMemoryOrderBuiltin(order));
931#endif
932#else
933 GREM_ASSERT(ptr);
934 (void)order;
935 const T old = *ptr;
936 *ptr ^= arg;
937 return old;
938#endif
939 }
940
941private:
942#ifdef GREM_USE_MULTITHREADING
943#if __cpp_lib_atomic_ref >= 201806L
944 std::atomic_ref<T> ref;
945#else
946 T* ptr;
947#endif
948#else
949 T* ptr;
950#endif
951};
952
953class Mutex {
954public:
955 constexpr Mutex() noexcept = default;
956
957 Mutex(const Mutex&) = delete;
958 Mutex& operator=(const Mutex&) = delete;
959
961#ifdef GREM_USE_MULTITHREADING
962 mutex.lock();
963#endif
964 }
965
966 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock() {
967#ifdef GREM_USE_MULTITHREADING
968 return mutex.try_lock();
969#else
970 return true;
971#endif
972 }
973
975#ifdef GREM_USE_MULTITHREADING
976 mutex.unlock();
977#endif
978 }
979
980private:
981#ifdef GREM_USE_MULTITHREADING
982 std::mutex mutex{};
983#endif
984};
985
987public:
988 constexpr RecursiveMutex() noexcept = default;
989
991 RecursiveMutex& operator=(const RecursiveMutex&) = delete;
992
994#ifdef GREM_USE_MULTITHREADING
995 mutex.lock();
996#endif
997 }
998
999 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock() {
1000#ifdef GREM_USE_MULTITHREADING
1001 return mutex.try_lock();
1002#else
1003 return true;
1004#endif
1005 }
1006
1008#ifdef GREM_USE_MULTITHREADING
1009 mutex.unlock();
1010#endif
1011 }
1012
1013private:
1014#ifdef GREM_USE_MULTITHREADING
1015 std::recursive_mutex mutex{};
1016#endif
1017};
1018
1020public:
1021 constexpr SharedMutex() noexcept = default;
1022
1023 SharedMutex(const SharedMutex&) = delete;
1024 SharedMutex& operator=(const SharedMutex&) = delete;
1025
1027#ifdef GREM_USE_MULTITHREADING
1028 mutex.lock();
1029#endif
1030 }
1031
1032 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock() {
1033#ifdef GREM_USE_MULTITHREADING
1034 return mutex.try_lock();
1035#else
1036 return true;
1037#endif
1038 }
1039
1041#ifdef GREM_USE_MULTITHREADING
1042 mutex.unlock();
1043#endif
1044 }
1045
1047#ifdef GREM_USE_MULTITHREADING
1048 mutex.lock_shared();
1049#endif
1050 }
1051
1053#ifdef GREM_USE_MULTITHREADING
1054 return mutex.try_lock_shared();
1055#else
1056 return true;
1057#endif
1058 }
1059
1061#ifdef GREM_USE_MULTITHREADING
1062 mutex.unlock_shared();
1063#endif
1064 }
1065
1066private:
1067#ifdef GREM_USE_MULTITHREADING
1068 std::shared_mutex mutex{};
1069#endif
1070};
1071
1073public:
1074 constexpr TimedMutex() noexcept = default;
1075
1076 TimedMutex(const TimedMutex&) = delete;
1077 TimedMutex& operator=(const TimedMutex&) = delete;
1078
1080#ifdef GREM_USE_MULTITHREADING
1081 mutex.lock();
1082#endif
1083 }
1084
1085 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock() {
1086#ifdef GREM_USE_MULTITHREADING
1087 return mutex.try_lock();
1088#else
1089 return true;
1090#endif
1091 }
1092
1093 template <typename Rep, typename Period>
1094 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock_for(const DurationBase<Rep, Period>& timeoutDuration) {
1095#ifdef GREM_USE_MULTITHREADING
1096 return mutex.try_lock_for(timeoutDuration);
1097#else
1098 (void)timeoutDuration;
1099 return true;
1100#endif
1101 }
1102
1103 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock_until(const TimePoint& timeoutTime) {
1104#ifdef GREM_USE_MULTITHREADING
1105 return mutex.try_lock_until(timeoutTime);
1106#else
1107 (void)timeoutTime;
1108 return true;
1109#endif
1110 }
1111
1113#ifdef GREM_USE_MULTITHREADING
1114 mutex.unlock();
1115#endif
1116 }
1117
1118private:
1119#ifdef GREM_USE_MULTITHREADING
1120 std::timed_mutex mutex{};
1121#endif
1122};
1123
1125public:
1126 constexpr RecursiveTimedMutex() noexcept = default;
1127
1129 RecursiveTimedMutex& operator=(const RecursiveTimedMutex&) = delete;
1130
1132#ifdef GREM_USE_MULTITHREADING
1133 mutex.lock();
1134#endif
1135 }
1136
1137 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock() {
1138#ifdef GREM_USE_MULTITHREADING
1139 return mutex.try_lock();
1140#else
1141 return true;
1142#endif
1143 }
1144
1145 template <typename Rep, typename Period>
1146 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock_for(const DurationBase<Rep, Period>& timeoutDuration) {
1147#ifdef GREM_USE_MULTITHREADING
1148 return mutex.try_lock_for(timeoutDuration);
1149#else
1150 (void)timeoutDuration;
1151 return true;
1152#endif
1153 }
1154
1155 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock_until(const TimePoint& timeoutTime) {
1156#ifdef GREM_USE_MULTITHREADING
1157 return mutex.try_lock_until(timeoutTime);
1158#else
1159 (void)timeoutTime;
1160 return true;
1161#endif
1162 }
1163
1165#ifdef GREM_USE_MULTITHREADING
1166 mutex.unlock();
1167#endif
1168 }
1169
1170private:
1171#ifdef GREM_USE_MULTITHREADING
1172 std::recursive_timed_mutex mutex{};
1173#endif
1174};
1175
1177public:
1178 constexpr SharedTimedMutex() noexcept = default;
1179
1181 SharedTimedMutex& operator=(const SharedTimedMutex&) = delete;
1182
1184#ifdef GREM_USE_MULTITHREADING
1185 mutex.lock();
1186#endif
1187 }
1188
1189 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock() {
1190#ifdef GREM_USE_MULTITHREADING
1191 return mutex.try_lock();
1192#else
1193 return true;
1194#endif
1195 }
1196
1197 template <typename Rep, typename Period>
1198 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock_for(const DurationBase<Rep, Period>& timeoutDuration) {
1199#ifdef GREM_USE_MULTITHREADING
1200 return mutex.try_lock_for(timeoutDuration);
1201#else
1202 (void)timeoutDuration;
1203 return true;
1204#endif
1205 }
1206
1207 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock_until(const TimePoint& timeoutTime) {
1208#ifdef GREM_USE_MULTITHREADING
1209 return mutex.try_lock_until(timeoutTime);
1210#else
1211 (void)timeoutTime;
1212 return true;
1213#endif
1214 }
1215
1217#ifdef GREM_USE_MULTITHREADING
1218 mutex.unlock();
1219#endif
1220 }
1221
1223#ifdef GREM_USE_MULTITHREADING
1224 mutex.lock_shared();
1225#endif
1226 }
1227
1229#ifdef GREM_USE_MULTITHREADING
1230 return mutex.try_lock_shared();
1231#else
1232 return true;
1233#endif
1234 }
1235
1236 template <typename Rep, typename Period>
1237 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock_shared_for(const DurationBase<Rep, Period>& timeoutDuration) {
1238#ifdef GREM_USE_MULTITHREADING
1239 return mutex.try_lock_shared_for(timeoutDuration);
1240#else
1241 (void)timeoutDuration;
1242 return true;
1243#endif
1244 }
1245
1246 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock_shared_until(const TimePoint& timeoutTime) {
1247#ifdef GREM_USE_MULTITHREADING
1248 return mutex.try_lock_shared_until(timeoutTime);
1249#else
1250 (void)timeoutTime;
1251 return true;
1252#endif
1253 }
1254
1256#ifdef GREM_USE_MULTITHREADING
1257 mutex.unlock_shared();
1258#endif
1259 }
1260
1261private:
1262#ifdef GREM_USE_MULTITHREADING
1263 std::shared_timed_mutex mutex{};
1264#endif
1265};
1266
1267template <typename Lockable1, typename Lockable2, typename... LockableN>
1268GREM_ALWAYS_INLINE void lock(Lockable1& lock1, Lockable2& lock2, LockableN&... lockN) {
1269#ifdef GREM_USE_MULTITHREADING
1270 std::lock(lock1, lock2, lockN...);
1271#else
1272 bool locked[2 + sizeof...(LockableN)]{};
1273 try {
1274 lock1.lock();
1275 locked[0] = true;
1276 lock2.lock();
1277 locked[1] = true;
1278 std::size_t i = 2;
1279 ((lockN.lock(), (locked[i++] = true)), ...);
1280 } catch (...) {
1281 if (locked[0]) {
1282 lock1.unlock();
1283 }
1284 if (locked[1]) {
1285 lock2.unlock();
1286 }
1287 std::size_t i = 2;
1288 ((locked[i++] && (lockN.unlock(), false)), ...);
1289 throw;
1290 }
1291#endif
1292}
1293
1295 explicit DeferLock() = default;
1296};
1297inline constexpr DeferLock DEFER_LOCK{};
1298
1300 explicit TryToLock() = default;
1301};
1302inline constexpr TryToLock TRY_TO_LOCK{};
1303
1305 explicit AdoptLock() = default;
1306};
1307inline constexpr AdoptLock ADOPT_LOCK{};
1308
1309template <typename... MutexTypes>
1311public:
1312#ifdef GREM_USE_MULTITHREADING
1313 GREM_ALWAYS_INLINE explicit ScopedLock(MutexTypes&... mutexes)
1314 : implementation(mutexes...) {}
1315
1316 GREM_ALWAYS_INLINE ScopedLock(AdoptLock, MutexTypes&... mutexes)
1317 : implementation(std::adopt_lock, mutexes...) {}
1318#else
1319 GREM_ALWAYS_INLINE explicit ScopedLock(MutexTypes&... mutexes)
1320 : mutexes(mutexes...) {
1321 lock(mutexes...);
1322 }
1323
1324 GREM_ALWAYS_INLINE ScopedLock(AdoptLock, MutexTypes&... mutexes)
1325 : mutexes(mutexes...) {}
1326
1328 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) {
1329 (get<Indices>(mutexes).unlock(), ...);
1330 }(std::make_index_sequence<sizeof...(MutexTypes)>{});
1331 }
1332#endif
1333
1334 ScopedLock(const ScopedLock&) = delete;
1336
1337private:
1338#ifdef GREM_USE_MULTITHREADING
1339 std::scoped_lock<MutexTypes...> implementation;
1340#else
1341 Tuple<MutexTypes&...> mutexes;
1342#endif
1343};
1344
1345template <typename MutexType>
1346class ScopedLock<MutexType> {
1347public:
1348 using mutex_type = MutexType;
1349
1350#ifdef GREM_USE_MULTITHREADING
1352 : implementation(mutex) {}
1353
1354 GREM_ALWAYS_INLINE ScopedLock(AdoptLock, mutex_type& mutex)
1355 : implementation(std::adopt_lock, mutex) {}
1356#else
1358 : mutex(mutex) {
1359 mutex.lock();
1360 }
1361
1363 : mutex(mutex) {}
1364
1366 mutex.unlock();
1367 }
1368#endif
1369
1370 ScopedLock(const ScopedLock&) = delete;
1372
1373private:
1374#ifdef GREM_USE_MULTITHREADING
1375 std::scoped_lock<MutexType> implementation;
1376#else
1377 mutex_type& mutex;
1378#endif
1379};
1380
1381template <typename MutexType>
1383public:
1384 using mutex_type = MutexType;
1385
1386 UniqueLock() noexcept = default;
1387 UniqueLock(const UniqueLock&) = delete;
1388
1389#ifdef GREM_USE_MULTITHREADING
1390 UniqueLock(UniqueLock&& other) noexcept = default;
1391
1393 : implementation(mutex) {}
1394
1396 : implementation(mutex, std::defer_lock) {}
1397
1399 : implementation(mutex, std::try_to_lock) {}
1400
1402 : implementation(mutex, std::adopt_lock) {}
1403
1404 template <typename Rep, typename Period>
1406 : implementation(mutex, timeoutDuration) {}
1407
1409 : implementation(mutex, timeoutTime) {}
1410
1411 UniqueLock& operator=(UniqueLock&& other) = default;
1412#else
1414 : associatedMutex(std::exchange(other.associatedMutex, nullptr))
1415 , locked(std::exchange(other.locked, false)) {}
1416
1418 : associatedMutex(&mutex) {
1419 mutex.lock();
1420 locked = true; // NOLINT(cppcoreguidelines-prefer-member-initializer)
1421 }
1422
1424 : associatedMutex(&mutex) {}
1425
1427 : associatedMutex(&mutex) {
1428 if constexpr (requires { mutex.try_lock(); }) {
1429 locked = mutex.try_lock();
1430 }
1431 }
1432
1434 : associatedMutex(&mutex)
1435 , locked(true) {}
1436
1437 template <typename Rep, typename Period>
1439 : associatedMutex(&mutex) {
1440 if constexpr (requires { mutex.try_lock_for(timeoutDuration); }) {
1441 locked = mutex.try_lock_for(timeoutDuration);
1442 } else if constexpr (requires { mutex.try_lock(); }) {
1443 locked = mutex.try_lock();
1444 }
1445 }
1446
1448 : associatedMutex(&mutex) {
1449 if constexpr (requires { mutex.try_lock_until(timeoutTime); }) {
1450 locked = mutex.try_lock_until(timeoutTime);
1451 } else if constexpr (requires { mutex.try_lock(); }) {
1452 locked = mutex.try_lock();
1453 }
1454 }
1455
1457 if (locked) {
1458 associatedMutex->unlock();
1459 }
1460 }
1461
1462 GREM_ALWAYS_INLINE UniqueLock& operator=(UniqueLock&& other) { // NOLINT(cppcoreguidelines-noexcept-move-operations, performance-noexcept-move-constructor)
1463 if (this == &other) {
1464 return *this;
1465 }
1466 associatedMutex = std::exchange(other.associatedMutex, nullptr);
1467 locked = std::exchange(other.locked, false);
1468 return *this;
1469 }
1470#endif
1471
1473
1475#ifdef GREM_USE_MULTITHREADING
1476 implementation.lock();
1477#else
1478 if (!associatedMutex) {
1479 throw std::system_error{make_error_code(std::errc::operation_not_permitted)};
1480 }
1481 if (locked) {
1482 throw std::system_error{make_error_code(std::errc::resource_deadlock_would_occur)};
1483 }
1484 associatedMutex->lock();
1485 locked = true;
1486#endif
1487 }
1488
1489#ifdef GREM_USE_MULTITHREADING
1490 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock() requires(requires(std::unique_lock<MutexType> l) { l.try_lock(); }) {
1491 return implementation.try_lock();
1492 }
1493
1494 template <typename Rep, typename Period>
1495 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock_for(const DurationBase<Rep, Period>& timeoutDuration)
1496 requires(requires(std::unique_lock<MutexType> l) { l.try_lock_for(timeoutDuration); }) {
1497 return implementation.try_lock_for(timeoutDuration);
1498 }
1499
1500 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock_until(TimePoint timeoutTime) requires(requires(std::unique_lock<MutexType> l) { l.try_lock_until(timeoutTime); }) {
1501 return implementation.try_lock_until(timeoutTime);
1502 }
1503#else
1504 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock() requires(requires(mutex_type m) { m.try_lock(); }) {
1505 if (!associatedMutex) {
1506 throw std::system_error{make_error_code(std::errc::operation_not_permitted)};
1507 }
1508 if (locked) {
1509 throw std::system_error{make_error_code(std::errc::resource_deadlock_would_occur)};
1510 }
1511 return locked = associatedMutex->try_lock();
1512 }
1513
1514 template <typename Rep, typename Period>
1515 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock_for(const DurationBase<Rep, Period>& timeoutDuration) requires(requires(mutex_type m) { m.try_lock_for(timeoutDuration); }) {
1516 if (!associatedMutex) {
1517 throw std::system_error{make_error_code(std::errc::operation_not_permitted)};
1518 }
1519 if (locked) {
1520 throw std::system_error{make_error_code(std::errc::resource_deadlock_would_occur)};
1521 }
1522 return locked = associatedMutex->try_lock_for(timeoutDuration);
1523 }
1524
1525 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock_until(TimePoint timeoutTime) requires(requires(mutex_type m) { m.try_lock_until(timeoutTime); }) {
1526 if (!associatedMutex) {
1527 throw std::system_error{make_error_code(std::errc::operation_not_permitted)};
1528 }
1529 if (locked) {
1530 throw std::system_error{make_error_code(std::errc::resource_deadlock_would_occur)};
1531 }
1532 return locked = associatedMutex->try_lock_until(timeoutTime);
1533 }
1534#endif
1535
1537#ifdef GREM_USE_MULTITHREADING
1538 implementation.unlock();
1539#else
1540 if (!associatedMutex || !locked) {
1541 throw std::system_error{make_error_code(std::errc::operation_not_permitted)};
1542 }
1543 associatedMutex->unlock();
1544 locked = false;
1545#endif
1546 }
1547
1548 GREM_ALWAYS_INLINE void swap(UniqueLock& other) noexcept {
1549#ifdef GREM_USE_MULTITHREADING
1550 implementation.swap(other.implementation);
1551#else
1552 std::swap(associatedMutex, other.associatedMutex);
1553 std::swap(locked, other.locked);
1554#endif
1555 }
1556
1557 GREM_ALWAYS_INLINE friend void swap(UniqueLock& a, UniqueLock& b) noexcept {
1558 a.swap(b);
1559 }
1560
1562#ifdef GREM_USE_MULTITHREADING
1563 return implementation.release();
1564#else
1565 locked = false;
1566 return std::exchange(associatedMutex, nullptr);
1567#endif
1568 }
1569
1570 [[nodiscard]] GREM_ALWAYS_INLINE mutex_type* mutex() const noexcept {
1571#ifdef GREM_USE_MULTITHREADING
1572 return implementation.mutex();
1573#else
1574 return associatedMutex;
1575#endif
1576 }
1577
1578 [[nodiscard]] GREM_ALWAYS_INLINE bool owns_lock() const noexcept {
1579#ifdef GREM_USE_MULTITHREADING
1580 return implementation.owns_lock();
1581#else
1582 return associatedMutex && locked;
1583#endif
1584 }
1585
1586 GREM_ALWAYS_INLINE explicit operator bool() const noexcept {
1587#ifdef GREM_USE_MULTITHREADING
1588 return static_cast<bool>(implementation);
1589#else
1590 return owns_lock();
1591#endif
1592 }
1593
1594private:
1595#ifdef GREM_USE_MULTITHREADING
1596 std::unique_lock<MutexType> implementation;
1597#else
1598 mutex_type* associatedMutex = nullptr;
1599 bool locked = false;
1600#endif
1601};
1602
1603template <typename MutexType>
1605public:
1606 using mutex_type = MutexType;
1607
1608 SharedLock() noexcept = default;
1609 SharedLock(const SharedLock&) = delete;
1610
1611#ifdef GREM_USE_MULTITHREADING
1612 SharedLock(SharedLock&& other) noexcept = default;
1613
1615 : implementation(mutex) {}
1616
1618 : implementation(mutex, std::defer_lock) {}
1619
1621 : implementation(mutex, std::try_to_lock) {}
1622
1624 : implementation(mutex, std::adopt_lock) {}
1625
1626 template <typename Rep, typename Period>
1628 : implementation(mutex, timeoutDuration) {}
1629
1631 : implementation(mutex, timeoutTime) {}
1632
1633 SharedLock& operator=(SharedLock&& other) = default;
1634#else
1636 : associatedMutex(std::exchange(other.associatedMutex, nullptr))
1637 , locked(std::exchange(other.locked, false)) {}
1638
1640 : associatedMutex(&mutex) {
1641 mutex.lock_shared();
1642 locked = true; // NOLINT(cppcoreguidelines-prefer-member-initializer)
1643 }
1644
1646 : associatedMutex(&mutex) {}
1647
1649 : associatedMutex(&mutex) {
1650 if constexpr (requires { mutex.try_lock_shared(); }) {
1651 locked = mutex.try_lock_shared();
1652 }
1653 }
1654
1656 : associatedMutex(&mutex)
1657 , locked(true) {}
1658
1659 template <typename Rep, typename Period>
1661 : associatedMutex(&mutex) {
1662 if constexpr (requires { mutex.try_lock_shared_for(timeoutDuration); }) {
1663 locked = mutex.try_lock_shared_for(timeoutDuration);
1664 } else if constexpr (requires { mutex.try_lock_shared(); }) {
1665 locked = mutex.try_lock_shared();
1666 }
1667 }
1668
1670 : associatedMutex(&mutex) {
1671 if constexpr (requires { mutex.try_lock_shared_until(timeoutTime); }) {
1672 locked = mutex.try_lock_shared_until(timeoutTime);
1673 } else if constexpr (requires { mutex.try_lock_shared(); }) {
1674 locked = mutex.try_lock_shared();
1675 }
1676 }
1677
1679 if (locked) {
1680 associatedMutex->unlock_shared();
1681 }
1682 }
1683
1684 GREM_ALWAYS_INLINE SharedLock& operator=(SharedLock&& other) { // NOLINT(cppcoreguidelines-noexcept-move-operations, performance-noexcept-move-constructor)
1685 if (this == &other) {
1686 return *this;
1687 }
1688 associatedMutex = std::exchange(other.associatedMutex, nullptr);
1689 locked = std::exchange(other.locked, false);
1690 return *this;
1691 }
1692#endif
1693
1695
1697#ifdef GREM_USE_MULTITHREADING
1698 implementation.lock();
1699#else
1700 if (!associatedMutex) {
1701 throw std::system_error{make_error_code(std::errc::operation_not_permitted)};
1702 }
1703 if (locked) {
1704 throw std::system_error{make_error_code(std::errc::resource_deadlock_would_occur)};
1705 }
1706 associatedMutex->lock_shared();
1707 locked = true;
1708#endif
1709 }
1710
1711#ifdef GREM_USE_MULTITHREADING
1712 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock() requires(requires(std::shared_lock<MutexType> l) { l.try_lock(); }) {
1713 return implementation.try_lock();
1714 }
1715
1716 template <typename Rep, typename Period>
1717 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock_for(const DurationBase<Rep, Period>& timeoutDuration)
1718 requires(requires(std::shared_lock<MutexType> l) { l.try_lock_for(timeoutDuration); }) {
1719 return implementation.try_lock_for(timeoutDuration);
1720 }
1721
1722 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock_until(TimePoint timeoutTime) requires(requires(std::shared_lock<MutexType> l) { l.try_lock_until(timeoutTime); }) {
1723 return implementation.try_lock_until(timeoutTime);
1724 }
1725#else
1726 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock() requires(requires(mutex_type m) { m.try_lock_shared(); }) {
1727 if (!associatedMutex) {
1728 throw std::system_error{make_error_code(std::errc::operation_not_permitted)};
1729 }
1730 if (locked) {
1731 throw std::system_error{make_error_code(std::errc::resource_deadlock_would_occur)};
1732 }
1733 return locked = associatedMutex->try_lock_shared();
1734 }
1735
1736 template <typename Rep, typename Period>
1737 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock_for(const DurationBase<Rep, Period>& timeoutDuration)
1738 requires(requires(mutex_type m) { m.try_lock_shared_for(timeoutDuration); }) {
1739 if (!associatedMutex) {
1740 throw std::system_error{make_error_code(std::errc::operation_not_permitted)};
1741 }
1742 if (locked) {
1743 throw std::system_error{make_error_code(std::errc::resource_deadlock_would_occur)};
1744 }
1745 return locked = associatedMutex->try_lock_shared_for(timeoutDuration);
1746 }
1747
1748 [[nodiscard]] GREM_ALWAYS_INLINE bool try_lock_until(TimePoint timeoutTime) requires(requires(mutex_type m) { m.try_lock_shared_until(timeoutTime); }) {
1749 if (!associatedMutex) {
1750 throw std::system_error{make_error_code(std::errc::operation_not_permitted)};
1751 }
1752 if (locked) {
1753 throw std::system_error{make_error_code(std::errc::resource_deadlock_would_occur)};
1754 }
1755 return locked = associatedMutex->try_lock_shared_until(timeoutTime);
1756 }
1757#endif
1758
1760#ifdef GREM_USE_MULTITHREADING
1761 implementation.unlock();
1762#else
1763 if (!associatedMutex || !locked) {
1764 throw std::system_error{make_error_code(std::errc::operation_not_permitted)};
1765 }
1766 associatedMutex->unlock_shared();
1767 locked = false;
1768#endif
1769 }
1770
1771 GREM_ALWAYS_INLINE void swap(SharedLock& other) noexcept {
1772#ifdef GREM_USE_MULTITHREADING
1773 implementation.swap(other.implementation);
1774#else
1775 std::swap(associatedMutex, other.associatedMutex);
1776 std::swap(locked, other.locked);
1777#endif
1778 }
1779
1780 GREM_ALWAYS_INLINE friend void swap(SharedLock& a, SharedLock& b) noexcept {
1781 a.swap(b);
1782 }
1783
1785#ifdef GREM_USE_MULTITHREADING
1786 return implementation.release();
1787#else
1788 locked = false;
1789 return std::exchange(associatedMutex, nullptr);
1790#endif
1791 }
1792
1793 [[nodiscard]] GREM_ALWAYS_INLINE mutex_type* mutex() const noexcept {
1794#ifdef GREM_USE_MULTITHREADING
1795 return implementation.mutex();
1796#else
1797 return associatedMutex;
1798#endif
1799 }
1800
1801 [[nodiscard]] GREM_ALWAYS_INLINE bool owns_lock() const noexcept {
1802#ifdef GREM_USE_MULTITHREADING
1803 return implementation.owns_lock();
1804#else
1805 return associatedMutex && locked;
1806#endif
1807 }
1808
1809 GREM_ALWAYS_INLINE explicit operator bool() const noexcept {
1810#ifdef GREM_USE_MULTITHREADING
1811 return static_cast<bool>(implementation);
1812#else
1813 return owns_lock();
1814#endif
1815 }
1816
1817private:
1818#ifdef GREM_USE_MULTITHREADING
1819 std::shared_lock<MutexType> implementation;
1820#else
1821 mutex_type* associatedMutex = nullptr;
1822 bool locked = false;
1823#endif
1824};
1825
1826} // namespace grem
1827
1828#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
#define GREM_ALWAYS_INLINE
Definition attributes.hpp:10
GREM_ALWAYS_INLINE void notify_one() noexcept
Definition synchronization.hpp:549
GREM_ALWAYS_INLINE void notify_all() noexcept
Definition synchronization.hpp:555
constexpr AtomicFlag() noexcept=default
GREM_ALWAYS_INLINE void wait(bool old, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept
Definition synchronization.hpp:539
GREM_ALWAYS_INLINE bool test(MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept
Definition synchronization.hpp:530
GREM_ALWAYS_INLINE void clear(MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) noexcept
Definition synchronization.hpp:510
GREM_ALWAYS_INLINE bool test_and_set(MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) noexcept
Definition synchronization.hpp:519
GREM_ALWAYS_INLINE value_type operator=(value_type desired) const noexcept
Definition synchronization.hpp:630
static constexpr std::size_t required_alignment
Definition synchronization.hpp:621
GREM_ALWAYS_INLINE T fetch_sub(std::ptrdiff_t arg, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept
Definition synchronization.hpp:872
GREM_ALWAYS_INLINE T fetch_add(std::ptrdiff_t arg, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept
Definition synchronization.hpp:836
GREM_ALWAYS_INLINE void wait(T old, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept
Definition synchronization.hpp:786
GREM_ALWAYS_INLINE bool compare_exchange_weak(T &expected, T desired, MemoryOrder success, MemoryOrder failure) const noexcept
Definition synchronization.hpp:698
GREM_ALWAYS_INLINE T fetch_and(T arg, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept
Definition synchronization.hpp:890
GREM_ALWAYS_INLINE T exchange(T newValue, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept
Definition synchronization.hpp:681
GREM_ALWAYS_INLINE AtomicRef(T &obj)
Definition synchronization.hpp:623
GREM_ALWAYS_INLINE T fetch_add(T arg, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept
Definition synchronization.hpp:818
GREM_ALWAYS_INLINE T fetch_xor(T arg, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept
Definition synchronization.hpp:924
GREM_ALWAYS_INLINE T fetch_sub(T arg, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept
Definition synchronization.hpp:854
GREM_ALWAYS_INLINE void notify_all() noexcept
Definition synchronization.hpp:810
GREM_ALWAYS_INLINE void notify_one() noexcept
Definition synchronization.hpp:802
GREM_ALWAYS_INLINE bool is_lock_free() const noexcept
Definition synchronization.hpp:638
GREM_ALWAYS_INLINE void store(T newValue, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept
Definition synchronization.hpp:650
GREM_ALWAYS_INLINE bool compare_exchange_strong(T &expected, T desired, MemoryOrder success, MemoryOrder failure) const noexcept
Definition synchronization.hpp:742
GREM_ALWAYS_INLINE bool compare_exchange_strong(T &expected, T desired, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept
Definition synchronization.hpp:763
static constexpr bool is_always_lock_free
Definition synchronization.hpp:620
AtomicRef & operator=(const AtomicRef &)=delete
GREM_ALWAYS_INLINE bool compare_exchange_weak(T &expected, T desired, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept
Definition synchronization.hpp:719
GREM_ALWAYS_INLINE T fetch_or(T arg, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept
Definition synchronization.hpp:907
GREM_ALWAYS_INLINE T load(MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept
Definition synchronization.hpp:665
GREM_ALWAYS_INLINE AtomicRef(const AtomicRef &other) noexcept
Definition synchronization.hpp:626
GREM_ALWAYS_INLINE bool compare_exchange_strong(T &expected, T desired, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) noexcept
Definition synchronization.hpp:266
GREM_ALWAYS_INLINE void notify_one() volatile noexcept
Definition synchronization.hpp:320
Atomic(const Atomic &)=delete
GREM_ALWAYS_INLINE T fetch_or(T arg, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) volatile noexcept
Definition synchronization.hpp:461
Atomic & operator=(const Atomic &)=delete
Atomic & operator=(const Atomic &) volatile =delete
GREM_ALWAYS_INLINE T load(MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept
Definition synchronization.hpp:142
GREM_ALWAYS_INLINE T fetch_add(T arg, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) volatile noexcept
Definition synchronization.hpp:349
GREM_ALWAYS_INLINE T fetch_sub(std::ptrdiff_t arg, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) volatile noexcept
Definition synchronization.hpp:417
GREM_ALWAYS_INLINE bool compare_exchange_strong(T &expected, T desired, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) volatile noexcept
Definition synchronization.hpp:280
GREM_ALWAYS_INLINE void store(T newValue, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) volatile noexcept
Definition synchronization.hpp:133
GREM_ALWAYS_INLINE void store(T newValue, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) noexcept
Definition synchronization.hpp:124
GREM_ALWAYS_INLINE void notify_one() noexcept
Definition synchronization.hpp:314
GREM_ALWAYS_INLINE T fetch_xor(T arg, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) noexcept
Definition synchronization.hpp:472
GREM_ALWAYS_INLINE bool compare_exchange_strong(T &expected, T desired, MemoryOrder success, MemoryOrder failure) volatile noexcept
Definition synchronization.hpp:251
GREM_ALWAYS_INLINE bool is_lock_free() const noexcept
Definition synchronization.hpp:108
GREM_ALWAYS_INLINE T fetch_and(T arg, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) volatile noexcept
Definition synchronization.hpp:439
GREM_ALWAYS_INLINE void notify_all() noexcept
Definition synchronization.hpp:326
constexpr Atomic() noexcept(std::is_nothrow_default_constructible_v< T >)=default
GREM_ALWAYS_INLINE bool compare_exchange_strong(T &expected, T desired, MemoryOrder success, MemoryOrder failure) noexcept
Definition synchronization.hpp:236
GREM_ALWAYS_INLINE T fetch_add(std::ptrdiff_t arg, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) volatile noexcept
Definition synchronization.hpp:372
GREM_ALWAYS_INLINE bool compare_exchange_weak(T &expected, T desired, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) noexcept
Definition synchronization.hpp:208
GREM_ALWAYS_INLINE bool is_lock_free() const volatile noexcept
Definition synchronization.hpp:116
GREM_ALWAYS_INLINE T exchange(T newValue, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) noexcept
Definition synchronization.hpp:160
GREM_ALWAYS_INLINE void wait(T old, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) const volatile noexcept
Definition synchronization.hpp:304
GREM_ALWAYS_INLINE T fetch_sub(T arg, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) volatile noexcept
Definition synchronization.hpp:394
GREM_ALWAYS_INLINE T fetch_or(T arg, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) noexcept
Definition synchronization.hpp:450
GREM_ALWAYS_INLINE T fetch_and(T arg, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) noexcept
Definition synchronization.hpp:428
GREM_ALWAYS_INLINE T fetch_sub(T arg, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) noexcept
Definition synchronization.hpp:383
GREM_ALWAYS_INLINE bool compare_exchange_weak(T &expected, T desired, MemoryOrder success, MemoryOrder failure) noexcept
Definition synchronization.hpp:178
GREM_ALWAYS_INLINE void notify_all() volatile noexcept
Definition synchronization.hpp:332
GREM_ALWAYS_INLINE T fetch_xor(T arg, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) volatile noexcept
Definition synchronization.hpp:483
GREM_ALWAYS_INLINE bool compare_exchange_weak(T &expected, T desired, MemoryOrder success, MemoryOrder failure) volatile noexcept
Definition synchronization.hpp:193
static constexpr bool is_always_lock_free
Definition synchronization.hpp:95
GREM_ALWAYS_INLINE bool compare_exchange_weak(T &expected, T desired, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) volatile noexcept
Definition synchronization.hpp:222
GREM_ALWAYS_INLINE T load(MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) const volatile noexcept
Definition synchronization.hpp:151
GREM_ALWAYS_INLINE T fetch_add(std::ptrdiff_t arg, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) noexcept
Definition synchronization.hpp:361
GREM_ALWAYS_INLINE T fetch_sub(std::ptrdiff_t arg, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) noexcept
Definition synchronization.hpp:406
GREM_ALWAYS_INLINE T exchange(T newValue, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) volatile noexcept
Definition synchronization.hpp:169
GREM_ALWAYS_INLINE void wait(T old, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) const noexcept
Definition synchronization.hpp:294
GREM_ALWAYS_INLINE T fetch_add(T arg, MemoryOrder order=MemoryOrder::SEQUENTIALLY_CONSISTENT) noexcept
Definition synchronization.hpp:338
GREM_ALWAYS_INLINE bool try_lock()
Definition synchronization.hpp:966
GREM_ALWAYS_INLINE void unlock()
Definition synchronization.hpp:974
constexpr Mutex() noexcept=default
GREM_ALWAYS_INLINE void lock()
Definition synchronization.hpp:960
GREM_ALWAYS_INLINE void unlock()
Definition synchronization.hpp:1007
constexpr RecursiveMutex() noexcept=default
GREM_ALWAYS_INLINE bool try_lock()
Definition synchronization.hpp:999
GREM_ALWAYS_INLINE void lock()
Definition synchronization.hpp:993
GREM_ALWAYS_INLINE void unlock()
Definition synchronization.hpp:1164
GREM_ALWAYS_INLINE bool try_lock_until(const TimePoint &timeoutTime)
Definition synchronization.hpp:1155
GREM_ALWAYS_INLINE bool try_lock_for(const DurationBase< Rep, Period > &timeoutDuration)
Definition synchronization.hpp:1146
GREM_ALWAYS_INLINE void lock()
Definition synchronization.hpp:1131
constexpr RecursiveTimedMutex() noexcept=default
GREM_ALWAYS_INLINE bool try_lock()
Definition synchronization.hpp:1137
ScopedLock(const ScopedLock &)=delete
MutexType mutex_type
Definition synchronization.hpp:1348
ScopedLock & operator=(const ScopedLock &)=delete
GREM_ALWAYS_INLINE ScopedLock(AdoptLock, mutex_type &mutex)
Definition synchronization.hpp:1362
GREM_ALWAYS_INLINE ScopedLock(mutex_type &mutex)
Definition synchronization.hpp:1357
GREM_ALWAYS_INLINE ~ScopedLock()
Definition synchronization.hpp:1365
GREM_ALWAYS_INLINE ~ScopedLock()
Definition synchronization.hpp:1327
GREM_ALWAYS_INLINE ScopedLock(MutexTypes &... mutexes)
Definition synchronization.hpp:1319
GREM_ALWAYS_INLINE ScopedLock(AdoptLock, MutexTypes &... mutexes)
Definition synchronization.hpp:1324
ScopedLock & operator=(const ScopedLock &)=delete
ScopedLock(const ScopedLock &)=delete
SharedLock & operator=(const SharedLock &)=delete
SharedLock() noexcept=default
GREM_ALWAYS_INLINE SharedLock(mutex_type &mutex, const TimePoint &timeoutTime)
Definition synchronization.hpp:1669
MutexType mutex_type
Definition synchronization.hpp:1606
GREM_ALWAYS_INLINE friend void swap(SharedLock &a, SharedLock &b) noexcept
Definition synchronization.hpp:1780
GREM_ALWAYS_INLINE mutex_type * release() noexcept
Definition synchronization.hpp:1784
GREM_ALWAYS_INLINE SharedLock(SharedLock &&other) noexcept
Definition synchronization.hpp:1635
GREM_ALWAYS_INLINE SharedLock & operator=(SharedLock &&other)
Definition synchronization.hpp:1684
GREM_ALWAYS_INLINE SharedLock(mutex_type &mutex, TryToLock)
Definition synchronization.hpp:1648
GREM_ALWAYS_INLINE bool try_lock_until(TimePoint timeoutTime)
Definition synchronization.hpp:1748
GREM_ALWAYS_INLINE SharedLock(mutex_type &mutex, DeferLock) noexcept
Definition synchronization.hpp:1645
GREM_ALWAYS_INLINE ~SharedLock()
Definition synchronization.hpp:1678
GREM_ALWAYS_INLINE bool try_lock()
Definition synchronization.hpp:1726
GREM_ALWAYS_INLINE SharedLock(mutex_type &mutex, const DurationBase< Rep, Period > &timeoutDuration)
Definition synchronization.hpp:1660
GREM_ALWAYS_INLINE bool owns_lock() const noexcept
Definition synchronization.hpp:1801
GREM_ALWAYS_INLINE bool try_lock_for(const DurationBase< Rep, Period > &timeoutDuration)
Definition synchronization.hpp:1737
GREM_ALWAYS_INLINE void swap(SharedLock &other) noexcept
Definition synchronization.hpp:1771
GREM_ALWAYS_INLINE SharedLock(mutex_type &mutex)
Definition synchronization.hpp:1639
GREM_ALWAYS_INLINE void lock()
Definition synchronization.hpp:1696
GREM_ALWAYS_INLINE SharedLock(mutex_type &mutex, AdoptLock)
Definition synchronization.hpp:1655
GREM_ALWAYS_INLINE mutex_type * mutex() const noexcept
Definition synchronization.hpp:1793
GREM_ALWAYS_INLINE void unlock()
Definition synchronization.hpp:1759
GREM_ALWAYS_INLINE void unlock_shared()
Definition synchronization.hpp:1060
GREM_ALWAYS_INLINE void unlock()
Definition synchronization.hpp:1040
GREM_ALWAYS_INLINE bool try_lock()
Definition synchronization.hpp:1032
GREM_ALWAYS_INLINE void lock_shared()
Definition synchronization.hpp:1046
GREM_ALWAYS_INLINE bool try_lock_shared()
Definition synchronization.hpp:1052
GREM_ALWAYS_INLINE void lock()
Definition synchronization.hpp:1026
constexpr SharedMutex() noexcept=default
constexpr SharedTimedMutex() noexcept=default
GREM_ALWAYS_INLINE bool try_lock()
Definition synchronization.hpp:1189
GREM_ALWAYS_INLINE bool try_lock_shared_for(const DurationBase< Rep, Period > &timeoutDuration)
Definition synchronization.hpp:1237
GREM_ALWAYS_INLINE void unlock_shared()
Definition synchronization.hpp:1255
GREM_ALWAYS_INLINE void lock_shared()
Definition synchronization.hpp:1222
GREM_ALWAYS_INLINE bool try_lock_shared()
Definition synchronization.hpp:1228
GREM_ALWAYS_INLINE bool try_lock_until(const TimePoint &timeoutTime)
Definition synchronization.hpp:1207
GREM_ALWAYS_INLINE void unlock()
Definition synchronization.hpp:1216
GREM_ALWAYS_INLINE void lock()
Definition synchronization.hpp:1183
GREM_ALWAYS_INLINE bool try_lock_shared_until(const TimePoint &timeoutTime)
Definition synchronization.hpp:1246
GREM_ALWAYS_INLINE bool try_lock_for(const DurationBase< Rep, Period > &timeoutDuration)
Definition synchronization.hpp:1198
GREM_ALWAYS_INLINE void unlock()
Definition synchronization.hpp:1112
constexpr TimedMutex() noexcept=default
GREM_ALWAYS_INLINE bool try_lock()
Definition synchronization.hpp:1085
GREM_ALWAYS_INLINE void lock()
Definition synchronization.hpp:1079
GREM_ALWAYS_INLINE bool try_lock_until(const TimePoint &timeoutTime)
Definition synchronization.hpp:1103
GREM_ALWAYS_INLINE bool try_lock_for(const DurationBase< Rep, Period > &timeoutDuration)
Definition synchronization.hpp:1094
Definition Tuple.hpp:210
GREM_ALWAYS_INLINE UniqueLock(UniqueLock &&other) noexcept
Definition synchronization.hpp:1413
UniqueLock() noexcept=default
GREM_ALWAYS_INLINE UniqueLock(mutex_type &mutex)
Definition synchronization.hpp:1417
GREM_ALWAYS_INLINE bool try_lock()
Definition synchronization.hpp:1504
GREM_ALWAYS_INLINE ~UniqueLock()
Definition synchronization.hpp:1456
GREM_ALWAYS_INLINE UniqueLock(mutex_type &mutex, const DurationBase< Rep, Period > &timeoutDuration)
Definition synchronization.hpp:1438
GREM_ALWAYS_INLINE bool try_lock_until(TimePoint timeoutTime)
Definition synchronization.hpp:1525
GREM_ALWAYS_INLINE bool owns_lock() const noexcept
Definition synchronization.hpp:1578
GREM_ALWAYS_INLINE UniqueLock(mutex_type &mutex, DeferLock) noexcept
Definition synchronization.hpp:1423
GREM_ALWAYS_INLINE void lock()
Definition synchronization.hpp:1474
GREM_ALWAYS_INLINE void swap(UniqueLock &other) noexcept
Definition synchronization.hpp:1548
GREM_ALWAYS_INLINE UniqueLock & operator=(UniqueLock &&other)
Definition synchronization.hpp:1462
GREM_ALWAYS_INLINE mutex_type * mutex() const noexcept
Definition synchronization.hpp:1570
GREM_ALWAYS_INLINE UniqueLock(mutex_type &mutex, TryToLock)
Definition synchronization.hpp:1426
GREM_ALWAYS_INLINE void unlock()
Definition synchronization.hpp:1536
GREM_ALWAYS_INLINE mutex_type * release() noexcept
Definition synchronization.hpp:1561
UniqueLock & operator=(const UniqueLock &)=delete
GREM_ALWAYS_INLINE bool try_lock_for(const DurationBase< Rep, Period > &timeoutDuration)
Definition synchronization.hpp:1515
MutexType mutex_type
Definition synchronization.hpp:1384
GREM_ALWAYS_INLINE UniqueLock(mutex_type &mutex, AdoptLock)
Definition synchronization.hpp:1433
GREM_ALWAYS_INLINE UniqueLock(mutex_type &mutex, const TimePoint &timeoutTime)
Definition synchronization.hpp:1447
GREM_ALWAYS_INLINE friend void swap(UniqueLock &a, UniqueLock &b) noexcept
Definition synchronization.hpp:1557
@ T
't'
Definition keyboard.hpp:90
std::error_code make_error_code(EndpointError error)
Create a std::error_code from an EndpointError value.
Definition Error.hpp:153
Definition aliases.hpp:18
Definition aliases.hpp:9
constexpr tuple_element_t< Index, Pair< T1, T2 > > & get(Pair< T1, T2 > &t) noexcept
Definition Pair.hpp:202
constexpr DeferLock DEFER_LOCK
Definition synchronization.hpp:1297
Clock::time_point TimePoint
Definition Clock.hpp:26
GREM_ALWAYS_INLINE void lock(Lockable1 &lock1, Lockable2 &lock2, LockableN &... lockN)
Definition synchronization.hpp:1268
MemoryOrder
Definition synchronization.hpp:31
@ CONSUME
Definition synchronization.hpp:41
@ ACQUIRE_RELEASE
Definition synchronization.hpp:44
@ ACQUIRE
Definition synchronization.hpp:42
@ RELAXED
Definition synchronization.hpp:40
@ RELEASE
Definition synchronization.hpp:43
@ SEQUENTIALLY_CONSISTENT
Definition synchronization.hpp:45
constexpr TryToLock TRY_TO_LOCK
Definition synchronization.hpp:1302
std::chrono::duration< Rep, Period > DurationBase
Definition time.hpp:39
GREM_ALWAYS_INLINE T killDependency(T y) noexcept
Definition synchronization.hpp:586
GREM_ALWAYS_INLINE void atomicThreadFence(MemoryOrder order) noexcept
Definition synchronization.hpp:569
constexpr AdoptLock ADOPT_LOCK
Definition synchronization.hpp:1307
GREM_ALWAYS_INLINE void atomicSignalFence(MemoryOrder order) noexcept
Definition synchronization.hpp:577
Definition synchronization.hpp:1304
AdoptLock()=default
Definition synchronization.hpp:1294
DeferLock()=default
Definition synchronization.hpp:1299
TryToLock()=default