| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/typedefs.h" |
| 34 |
|
| 35 |
#ifdef DEV_ENABLED |
| 36 |
#include "core/error/error_macros.h" |
| 37 |
#endif |
| 38 |
|
| 39 |
#include <atomic> |
| 40 |
#include <type_traits> |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
#define SAFE_NUMERIC_TYPE_PUN_GUARANTEES(m_type) \ |
| 54 |
static_assert(sizeof(SafeNumeric<m_type>) == sizeof(m_type)); \ |
| 55 |
static_assert(alignof(SafeNumeric<m_type>) == alignof(m_type)); \ |
| 56 |
static_assert(std::is_trivially_destructible_v<std::atomic<m_type>>); |
| 57 |
#define SAFE_FLAG_TYPE_PUN_GUARANTEES \ |
| 58 |
static_assert(sizeof(SafeFlag) == sizeof(bool)); \ |
| 59 |
static_assert(alignof(SafeFlag) == alignof(bool)); |
| 60 |
|
| 61 |
template <typename T> |
| 62 |
class SafeNumeric { |
| 63 |
std::atomic<T> value; |
| 64 |
|
| 65 |
static_assert(std::atomic<T>::is_always_lock_free); |
| 66 |
|
| 67 |
public: |
| 68 |
_ALWAYS_INLINE_ void set(T p_value) { |
| 69 |
value.store(p_value, std::memory_order_release); |
| 70 |
} |
| 71 |
|
| 72 |
_ALWAYS_INLINE_ T get() const { |
| 73 |
return value.load(std::memory_order_acquire); |
| 74 |
} |
| 75 |
|
| 76 |
_ALWAYS_INLINE_ T increment() { |
| 77 |
return value.fetch_add(1, std::memory_order_acq_rel) + 1; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
_ALWAYS_INLINE_ T postincrement() { |
| 82 |
return value.fetch_add(1, std::memory_order_acq_rel); |
| 83 |
} |
| 84 |
|
| 85 |
_ALWAYS_INLINE_ T decrement() { |
| 86 |
return value.fetch_sub(1, std::memory_order_acq_rel) - 1; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
_ALWAYS_INLINE_ T postdecrement() { |
| 91 |
return value.fetch_sub(1, std::memory_order_acq_rel); |
| 92 |
} |
| 93 |
|
| 94 |
_ALWAYS_INLINE_ T add(T p_value) { |
| 95 |
return value.fetch_add(p_value, std::memory_order_acq_rel) + p_value; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
_ALWAYS_INLINE_ T postadd(T p_value) { |
| 100 |
return value.fetch_add(p_value, std::memory_order_acq_rel); |
| 101 |
} |
| 102 |
|
| 103 |
_ALWAYS_INLINE_ T sub(T p_value) { |
| 104 |
return value.fetch_sub(p_value, std::memory_order_acq_rel) - p_value; |
| 105 |
} |
| 106 |
|
| 107 |
_ALWAYS_INLINE_ T bit_or(T p_value) { |
| 108 |
return value.fetch_or(p_value, std::memory_order_acq_rel); |
| 109 |
} |
| 110 |
_ALWAYS_INLINE_ T bit_and(T p_value) { |
| 111 |
return value.fetch_and(p_value, std::memory_order_acq_rel); |
| 112 |
} |
| 113 |
|
| 114 |
_ALWAYS_INLINE_ T bit_xor(T p_value) { |
| 115 |
return value.fetch_xor(p_value, std::memory_order_acq_rel); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
_ALWAYS_INLINE_ T postsub(T p_value) { |
| 120 |
return value.fetch_sub(p_value, std::memory_order_acq_rel); |
| 121 |
} |
| 122 |
|
| 123 |
_ALWAYS_INLINE_ T exchange_if_greater(T p_value) { |
| 124 |
while (true) { |
| 125 |
T tmp = value.load(std::memory_order_acquire); |
| 126 |
if (tmp >= p_value) { |
| 127 |
return tmp; |
| 128 |
} |
| 129 |
|
| 130 |
if (value.compare_exchange_weak(tmp, p_value, std::memory_order_acq_rel)) { |
| 131 |
return p_value; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
_ALWAYS_INLINE_ T conditional_increment() { |
| 137 |
while (true) { |
| 138 |
T c = value.load(std::memory_order_acquire); |
| 139 |
if (c == 0) { |
| 140 |
return 0; |
| 141 |
} |
| 142 |
if (value.compare_exchange_weak(c, c + 1, std::memory_order_acq_rel)) { |
| 143 |
return c + 1; |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
_ALWAYS_INLINE_ explicit SafeNumeric(T p_value = static_cast<T>(0)) { |
| 149 |
set(p_value); |
| 150 |
} |
| 151 |
}; |
| 152 |
|
| 153 |
class SafeFlag { |
| 154 |
std::atomic_bool flag; |
| 155 |
|
| 156 |
static_assert(std::atomic_bool::is_always_lock_free); |
| 157 |
|
| 158 |
public: |
| 159 |
_ALWAYS_INLINE_ bool is_set() const { |
| 160 |
return flag.load(std::memory_order_acquire); |
| 161 |
} |
| 162 |
|
| 163 |
_ALWAYS_INLINE_ void set() { |
| 164 |
flag.store(true, std::memory_order_release); |
| 165 |
} |
| 166 |
|
| 167 |
_ALWAYS_INLINE_ void clear() { |
| 168 |
flag.store(false, std::memory_order_release); |
| 169 |
} |
| 170 |
|
| 171 |
_ALWAYS_INLINE_ void set_to(bool p_value) { |
| 172 |
flag.store(p_value, std::memory_order_release); |
| 173 |
} |
| 174 |
|
| 175 |
_ALWAYS_INLINE_ explicit SafeFlag(bool p_value = false) { |
| 176 |
set_to(p_value); |
| 177 |
} |
| 178 |
}; |
| 179 |
|
| 180 |
class SafeRefCount { |
| 181 |
SafeNumeric<uint32_t> count; |
| 182 |
|
| 183 |
#ifdef DEV_ENABLED |
| 184 |
_ALWAYS_INLINE_ void _check_unref_safety() { |
| 185 |
|
| 186 |
CRASH_COND_MSG(count.get() == 0, |
| 187 |
"Trying to unreference a SafeRefCount which is already zero is wrong and a symptom of it being misused.\n" |
| 188 |
"Upon a SafeRefCount reaching zero any object whose lifetime is tied to it, as well as the ref count itself, must be destroyed.\n" |
| 189 |
"Moreover, to guarantee that, no multiple threads should be racing to do the final unreferencing to zero."); |
| 190 |
} |
| 191 |
#endif |
| 192 |
|
| 193 |
public: |
| 194 |
_ALWAYS_INLINE_ bool ref() { |
| 195 |
return count.conditional_increment() != 0; |
| 196 |
} |
| 197 |
|
| 198 |
_ALWAYS_INLINE_ uint32_t refval() { |
| 199 |
return count.conditional_increment(); |
| 200 |
} |
| 201 |
|
| 202 |
_ALWAYS_INLINE_ bool unref() { |
| 203 |
#ifdef DEV_ENABLED |
| 204 |
_check_unref_safety(); |
| 205 |
#endif |
| 206 |
return count.decrement() == 0; |
| 207 |
} |
| 208 |
|
| 209 |
_ALWAYS_INLINE_ uint32_t unrefval() { |
| 210 |
#ifdef DEV_ENABLED |
| 211 |
_check_unref_safety(); |
| 212 |
#endif |
| 213 |
return count.decrement(); |
| 214 |
} |
| 215 |
|
| 216 |
_ALWAYS_INLINE_ uint32_t get() const { |
| 217 |
return count.get(); |
| 218 |
} |
| 219 |
|
| 220 |
_ALWAYS_INLINE_ void init(uint32_t p_value = 1) { |
| 221 |
count.set(p_value); |
| 222 |
} |
| 223 |
}; |