Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Ref assignment atomic #46302

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions core/object/reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,33 @@ class Reference : public Object {

template <class T>
class Ref {
T *reference = nullptr;
std::atomic<T *> reference = nullptr;
// Fail loudly rather than silently introduce locks at every Ref assingment. Passes on most architectures.
static_assert(std::atomic<T *>::is_always_lock_free);

void ref(const Ref &p_from) {
if (p_from.reference == reference) {
// Copy new reference to method-local variable immediately.
T *new_reference_ptr = p_from.reference.load(std::memory_order_relaxed);

// Increment the reference count before exchanging to prevent exchange of a deleted reference.
if (new_reference_ptr && !new_reference_ptr->reference()) {
// Reference count was at zero and cannot be incremented. p_from will be imminently deleted by another thread.
return;
}

unref();
T *old_reference_ptr = reference.exchange(new_reference_ptr, std::memory_order_relaxed);

reference = p_from.reference;
if (reference) {
reference->reference();
// old_reference_ptr now contains the previous value of this->reference
if (old_reference_ptr && old_reference_ptr->unreference()) {
memdelete(old_reference_ptr);
}
}

void ref_pointer(T *p_ref) {
ERR_FAIL_COND(!p_ref);

if (p_ref->init_ref()) {
reference = p_ref;
reference.store(p_ref);
}
}

Expand Down Expand Up @@ -144,20 +151,17 @@ class Ref {
void operator=(const Variant &p_variant) {
Object *object = p_variant.get_validated_object();

if (object == reference) {
return;
}

unref();

if (!object) {
unref();
return;
}

T *r = Object::cast_to<T>(object);
if (r && r->reference()) {
reference = r;
if (!r) {
unref();
return;
}
ref(r);
}

template <class T_Other>
Expand Down Expand Up @@ -205,22 +209,18 @@ class Ref {

T *r = Object::cast_to<T>(object);
if (r && r->reference()) {
reference = r;
reference.store(r, std::memory_order_relaxed);
}
}

inline bool is_valid() const { return reference != nullptr; }
inline bool is_null() const { return reference == nullptr; }

void unref() {
//TODO this should be moved to mutexes, since this engine does not really
// do a lot of referencing on references and stuff
// mutexes will avoid more crashes?

if (reference && reference->unreference()) {
memdelete(reference);
T *old_reference_ptr = reference.exchange(nullptr, std::memory_order_relaxed);
if (old_reference_ptr && old_reference_ptr->unreference()) {
memdelete(old_reference_ptr);
}
reference = nullptr;
}

void instance() {
Expand Down