Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomShaper committed Oct 22, 2024
1 parent 3da53a3 commit baccfdb
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion core/templates/rid_owner.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@
#include <stdio.h>
#include <typeinfo>

#ifdef SANITIZERS_ENABLED
#ifdef __has_feature
#if __has_feature(thread_sanitizer)
#define TSAN_ENABLED
#endif
#elif defined(__SANITIZE_THREAD__)
#define TSAN_ENABLED
#endif
#endif

#ifdef TSAN_ENABLED
#include <sanitizer/tsan_interface.h>
#endif

class RID_AllocBase {
static SafeNumeric<uint64_t> base_id;

Expand Down Expand Up @@ -185,6 +199,9 @@ class RID_Alloc : public RID_AllocBase {
uint32_t validator = uint32_t(id >> 32);

Chunk &c = chunks[idx_chunk][idx_element];
#ifdef TSAN_ENABLED
__tsan_acquire(&c.validator); // We know not a race condition in practice.
#endif
if (unlikely(p_initialize)) {
if (unlikely(!(c.validator & 0x80000000))) {
ERR_FAIL_V_MSG(nullptr, "Initializing already initialized RID");
Expand All @@ -194,14 +211,18 @@ class RID_Alloc : public RID_AllocBase {
ERR_FAIL_V_MSG(nullptr, "Attempting to initialize the wrong RID");
}

c.validator &= 0x7FFFFFFF; //initialized
// Mark initialized.
c.validator &= 0x7FFFFFFF;

} else if (unlikely(c.validator != validator)) {
if ((c.validator & 0x80000000) && c.validator != 0xFFFFFFFF) {
ERR_FAIL_V_MSG(nullptr, "Attempting to use an uninitialized RID");
}
return nullptr;
}
#ifdef TSAN_ENABLED
__tsan_release(&c.validator);
#endif

T *ptr = &c.data;

Expand All @@ -210,12 +231,27 @@ class RID_Alloc : public RID_AllocBase {
void initialize_rid(RID p_rid) {
T *mem = get_or_null(p_rid, true);
ERR_FAIL_NULL(mem);
#ifdef TSAN_ENABLED
__tsan_acquire(mem); // We know not a race condition in practice.
#endif
memnew_placement(mem, T);
#ifdef TSAN_ENABLED
__tsan_release(mem);
#endif
spin.release();
}

void initialize_rid(RID p_rid, const T &p_value) {
T *mem = get_or_null(p_rid, true);
ERR_FAIL_NULL(mem);
#ifdef TSAN_ENABLED
__tsan_acquire(mem); // We know not a race condition in practice.
#endif
memnew_placement(mem, T(p_value));
#ifdef TSAN_ENABLED
__tsan_release(mem);
#endif
spin.release();
}

_FORCE_INLINE_ bool owns(const RID &p_rid) const {
Expand Down

0 comments on commit baccfdb

Please sign in to comment.