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