Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/templates/test_rid.cpp
45991 views
1
/**************************************************************************/
2
/* test_rid.cpp */
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
#include "tests/test_macros.h"
32
33
TEST_FORCE_LINK(test_rid)
34
35
#include "core/os/os.h"
36
#include "core/os/thread.h"
37
#include "core/templates/local_vector.h"
38
#include "core/templates/rid.h"
39
#include "core/templates/rid_owner.h"
40
41
#ifdef TSAN_ENABLED
42
#include <sanitizer/tsan_interface.h>
43
#endif
44
45
namespace TestRID {
46
47
TEST_CASE("[RID] Default Constructor") {
48
RID rid;
49
50
CHECK(rid.get_id() == 0);
51
}
52
53
TEST_CASE("[RID] Factory method") {
54
RID rid = RID::from_uint64(1);
55
56
CHECK(rid.get_id() == 1);
57
}
58
59
TEST_CASE("[RID] Operators") {
60
RID rid = RID::from_uint64(1);
61
62
RID rid_zero = RID::from_uint64(0);
63
RID rid_one = RID::from_uint64(1);
64
RID rid_two = RID::from_uint64(2);
65
66
CHECK_FALSE(rid == rid_zero);
67
CHECK(rid == rid_one);
68
CHECK_FALSE(rid == rid_two);
69
70
CHECK_FALSE(rid < rid_zero);
71
CHECK_FALSE(rid < rid_one);
72
CHECK(rid < rid_two);
73
74
CHECK_FALSE(rid <= rid_zero);
75
CHECK(rid <= rid_one);
76
CHECK(rid <= rid_two);
77
78
CHECK(rid > rid_zero);
79
CHECK_FALSE(rid > rid_one);
80
CHECK_FALSE(rid > rid_two);
81
82
CHECK(rid >= rid_zero);
83
CHECK(rid >= rid_one);
84
CHECK_FALSE(rid >= rid_two);
85
86
CHECK(rid != rid_zero);
87
CHECK_FALSE(rid != rid_one);
88
CHECK(rid != rid_two);
89
}
90
91
TEST_CASE("[RID] 'is_valid' & 'is_null'") {
92
RID rid_zero = RID::from_uint64(0);
93
RID rid_one = RID::from_uint64(1);
94
95
CHECK_FALSE(rid_zero.is_valid());
96
CHECK(rid_zero.is_null());
97
98
CHECK(rid_one.is_valid());
99
CHECK_FALSE(rid_one.is_null());
100
}
101
102
TEST_CASE("[RID] 'get_local_index'") {
103
CHECK(RID::from_uint64(1).get_local_index() == 1);
104
CHECK(RID::from_uint64(4'294'967'295).get_local_index() == 4'294'967'295);
105
CHECK(RID::from_uint64(4'294'967'297).get_local_index() == 1);
106
}
107
108
#ifdef THREADS_ENABLED
109
// This case would let sanitizers realize data races.
110
// Additionally, on purely weakly ordered architectures, it would detect synchronization issues
111
// if RID_Alloc failed to impose proper memory ordering and the test's threads are distributed
112
// among multiple L1 caches.
113
TEST_CASE("[RID_Owner] Thread safety") {
114
struct DataHolder {
115
char data[Thread::CACHE_LINE_BYTES];
116
};
117
118
struct RID_OwnerTester {
119
uint32_t thread_count = 0;
120
RID_Owner<DataHolder, true> rid_owner;
121
TightLocalVector<Thread> threads;
122
SafeNumeric<uint32_t> next_thread_idx;
123
// Using std::atomic directly since SafeNumeric doesn't support relaxed ordering.
124
TightLocalVector<std::atomic<uint64_t>> rids;
125
std::atomic<uint32_t> sync[2] = {};
126
std::atomic<uint32_t> correct = 0;
127
128
// A barrier that doesn't introduce memory ordering constraints, only compiler ones.
129
// The idea is not to cause any sync traffic that would make the code we want to test
130
// seem correct as a side effect.
131
void lockstep(uint32_t p_step) {
132
uint32_t buf_idx = p_step % 2;
133
uint32_t target = (p_step / 2 + 1) * threads.size();
134
sync[buf_idx].fetch_add(1, std::memory_order_relaxed);
135
do {
136
Thread::yield();
137
} while (sync[buf_idx].load(std::memory_order_relaxed) != target);
138
}
139
140
explicit RID_OwnerTester(bool p_chunk_for_all, bool p_chunks_preallocated) :
141
thread_count(OS::get_singleton()->get_processor_count()),
142
rid_owner(sizeof(DataHolder) * (p_chunk_for_all ? thread_count : 1)) {
143
threads.resize(thread_count);
144
rids.resize(threads.size());
145
if (p_chunks_preallocated) {
146
LocalVector<RID> prealloc_rids;
147
for (uint32_t i = 0; i < (p_chunk_for_all ? 1 : threads.size()); i++) {
148
prealloc_rids.push_back(rid_owner.make_rid());
149
}
150
for (uint32_t i = 0; i < prealloc_rids.size(); i++) {
151
rid_owner.free(prealloc_rids[i]);
152
}
153
}
154
}
155
156
~RID_OwnerTester() {
157
for (uint32_t i = 0; i < threads.size(); i++) {
158
rid_owner.free(RID::from_uint64(rids[i].load(std::memory_order_relaxed)));
159
}
160
}
161
162
void test() {
163
for (uint32_t i = 0; i < threads.size(); i++) {
164
threads[i].start(
165
[](void *p_data) {
166
RID_OwnerTester *rot = (RID_OwnerTester *)p_data;
167
168
auto _compute_thread_unique_byte = [](uint32_t p_idx) -> char {
169
return ((p_idx & 0xff) ^ (0b11111110 << (p_idx % 8)));
170
};
171
172
// 1. Each thread gets a zero-based index.
173
uint32_t self_th_idx = rot->next_thread_idx.postincrement();
174
175
rot->lockstep(0);
176
177
// 2. Each thread makes a RID holding unique data.
178
DataHolder initial_data;
179
memset(&initial_data, _compute_thread_unique_byte(self_th_idx), Thread::CACHE_LINE_BYTES);
180
RID my_rid = rot->rid_owner.make_rid(initial_data);
181
rot->rids[self_th_idx].store(my_rid.get_id(), std::memory_order_relaxed);
182
183
rot->lockstep(1);
184
185
// 3. Each thread verifies all the others.
186
uint32_t local_correct = 0;
187
for (uint32_t th_idx = 0; th_idx < rot->threads.size(); th_idx++) {
188
if (th_idx == self_th_idx) {
189
continue;
190
}
191
char expected_unique_byte = _compute_thread_unique_byte(th_idx);
192
RID rid = RID::from_uint64(rot->rids[th_idx].load(std::memory_order_relaxed));
193
DataHolder *data = rot->rid_owner.get_or_null(rid);
194
#ifdef TSAN_ENABLED
195
__tsan_acquire(data); // We know not a race in practice.
196
#endif
197
bool ok = true;
198
for (uint32_t j = 0; j < Thread::CACHE_LINE_BYTES; j++) {
199
if (data->data[j] != expected_unique_byte) {
200
ok = false;
201
break;
202
}
203
}
204
if (ok) {
205
local_correct++;
206
}
207
#ifdef TSAN_ENABLED
208
__tsan_release(data);
209
#endif
210
}
211
212
rot->lockstep(2);
213
214
rot->correct.fetch_add(local_correct, std::memory_order_acq_rel);
215
},
216
this);
217
}
218
219
for (uint32_t i = 0; i < threads.size(); i++) {
220
threads[i].wait_to_finish();
221
}
222
223
CHECK_EQ(correct.load(), threads.size() * (threads.size() - 1));
224
}
225
};
226
227
SUBCASE("All items in one chunk, pre-allocated") {
228
RID_OwnerTester tester(true, true);
229
tester.test();
230
}
231
SUBCASE("All items in one chunk, NOT pre-allocated") {
232
RID_OwnerTester tester(true, false);
233
tester.test();
234
}
235
SUBCASE("One item per chunk, pre-allocated") {
236
RID_OwnerTester tester(false, true);
237
tester.test();
238
}
239
SUBCASE("One item per chunk, NOT pre-allocated") {
240
RID_OwnerTester tester(false, false);
241
tester.test();
242
}
243
}
244
#endif // THREADS_ENABLED
245
246
} // namespace TestRID
247
248