Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/templates/safe_refcount.h
9973 views
1
/**************************************************************************/
2
/* safe_refcount.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
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> // IWYU pragma: keep // Used in macro.
41
42
// Design goals for these classes:
43
// - No automatic conversions or arithmetic operators,
44
// to keep explicit the use of atomics everywhere.
45
// - Using acquire-release semantics, even to set the first value.
46
// The first value may be set relaxedly in many cases, but adding the distinction
47
// between relaxed and unrelaxed operation to the interface would make it needlessly
48
// flexible. There's negligible waste in having release semantics for the initial
49
// value and, as an important benefit, you can be sure the value is properly synchronized
50
// even with threads that are already running.
51
52
// These are used in very specific areas of the engine where it's critical that these guarantees are held
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
// Returns the original value instead of the new one
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
// Returns the original value instead of the new one
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
// Returns the original value instead of the new one
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
// Returns the original value instead of the new one
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; // already greater, or equal
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
// This won't catch every misuse, but it's better than nothing.
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() { // true on success
195
return count.conditional_increment() != 0;
196
}
197
198
_ALWAYS_INLINE_ uint32_t refval() { // none-zero on success
199
return count.conditional_increment();
200
}
201
202
_ALWAYS_INLINE_ bool unref() { // true if must be disposed of
203
#ifdef DEV_ENABLED
204
_check_unref_safety();
205
#endif
206
return count.decrement() == 0;
207
}
208
209
_ALWAYS_INLINE_ uint32_t unrefval() { // 0 if must be disposed of
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
};
224
225