Skip to content

Commit 5ed8054

Browse files
committed
RID_Owner: Fix race conditions due to missing synchronization
1 parent 1549d51 commit 5ed8054

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

core/templates/rid_owner.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,17 @@
5555
#endif
5656

5757
// The following macros would need to be implemented somehow
58-
// for purely weakly ordered architectures. There's a test case
58+
// for weakly ordered architectures. There's a test case
5959
// ("[RID_Owner] Thread safety") with potential to catch issues
6060
// on such architectures if these primitives fail to be implemented.
6161
// For now, they will be just markers about needs that may arise.
62+
#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) // MSVC, x86.
6263
#define WEAK_MEMORY_ORDER 0
64+
#elif (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__)) // GCC/Clang, x86.
65+
#define WEAK_MEMORY_ORDER 0
66+
#else
67+
#define WEAK_MEMORY_ORDER 1
68+
#endif
6369
#if WEAK_MEMORY_ORDER
6470
// Ideally, we'd have implementations that collaborate with the
6571
// sync mechanism used (e.g., the mutex) so instead of some full
@@ -119,6 +125,7 @@ class RID_Alloc : public RID_AllocBase {
119125
_FORCE_INLINE_ RID _allocate_rid() {
120126
if constexpr (THREAD_SAFE) {
121127
mutex.lock();
128+
SYNC_ACQUIRE;
122129
}
123130

124131
if (alloc_count == max_alloc) {
@@ -175,6 +182,7 @@ class RID_Alloc : public RID_AllocBase {
175182
alloc_count++;
176183

177184
if constexpr (THREAD_SAFE) {
185+
SYNC_RELEASE;
178186
mutex.unlock();
179187
}
180188

@@ -367,6 +375,7 @@ class RID_Alloc : public RID_AllocBase {
367375
_FORCE_INLINE_ void free(const RID &p_rid) {
368376
if constexpr (THREAD_SAFE) {
369377
mutex.lock();
378+
SYNC_ACQUIRE;
370379
}
371380

372381
uint64_t id = p_rid.get_id();
@@ -401,6 +410,7 @@ class RID_Alloc : public RID_AllocBase {
401410
free_list_chunks[alloc_count / elements_in_chunk][alloc_count % elements_in_chunk] = idx;
402411

403412
if constexpr (THREAD_SAFE) {
413+
SYNC_RELEASE;
404414
mutex.unlock();
405415
}
406416
}
@@ -429,6 +439,7 @@ class RID_Alloc : public RID_AllocBase {
429439
void fill_owned_buffer(RID *p_rid_buffer) const {
430440
if constexpr (THREAD_SAFE) {
431441
mutex.lock();
442+
SYNC_ACQUIRE;
432443
}
433444
uint32_t idx = 0;
434445
for (size_t i = 0; i < max_alloc; i++) {

0 commit comments

Comments
 (0)