Merge pull request #1731 from marauder2k9-torque/RefBase-changes

Update refBase.h
This commit is contained in:
Brian Roberts 2026-05-11 16:13:04 -05:00 committed by GitHub
commit a416573d69
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -32,6 +32,7 @@
#include <memory> #include <memory>
#include <type_traits> #include <type_traits>
#include <atomic>
class WeakRefBase; class WeakRefBase;
@ -137,37 +138,38 @@ private:
/// when all strong references go away, object is destroyed). /// when all strong references go away, object is destroyed).
class StrongRefBase : public WeakRefBase class StrongRefBase : public WeakRefBase
{ {
friend class StrongObjectRef; friend class StrongObjectRef;
public: public:
StrongRefBase() StrongRefBase() = default;
{ virtual ~StrongRefBase() = default;
mRefCount = 0;
}
virtual ~StrongRefBase() = default; U32 getRefCount() const
{
return mRefCount.load(std::memory_order_relaxed);
}
U32 getRefCount() const { return mRefCount; } virtual void destroySelf()
{
delete this;
}
/// object destroy self call (from StrongRefPtr). Override if this class has specially allocated memory. void incRefCount()
virtual void destroySelf() { delete this; } {
mRefCount.fetch_add(1, std::memory_order_relaxed);
}
/// Increments the reference count. void decRefCount()
void incRefCount() {
{ if (mRefCount.fetch_sub(1, std::memory_order_acq_rel) == 1)
mRefCount++; {
} std::atomic_thread_fence(std::memory_order_acquire);
destroySelf();
/// Decrements the reference count. }
void decRefCount() }
{
AssertFatal(mRefCount, "Decrementing a reference with refcount 0!");
if (!--mRefCount)
destroySelf();
}
protected: protected:
U32 mRefCount; ///< reference counter for StrongRefPtr objects std::atomic<U32> mRefCount{ 0 };
}; };
@ -308,8 +310,14 @@ private:
release(); release();
if (!ptr) return; if (!ptr) return;
// Always hold the identity if constexpr (std::is_base_of_v<StrongRefBase, T>)
mControl = ptr->getWeakControl().lock(); {
mControl = ptr->getSharedControl();
}
else
{
mControl = ptr->getWeakControl().lock();
}
if (!mControl) return; if (!mControl) return;
mPtr = ptr; mPtr = ptr;
@ -318,7 +326,9 @@ private:
// runtime check: only strong ref if T inherits StrongRefBase // runtime check: only strong ref if T inherits StrongRefBase
if constexpr (std::is_base_of_v<StrongRefBase, T>) if constexpr (std::is_base_of_v<StrongRefBase, T>)
{ {
mPtr->incRefCount(); T* live = getPointer();
if (live)
live->incRefCount();
} }
} }
@ -328,7 +338,9 @@ private:
{ {
if constexpr (std::is_base_of_v<StrongRefBase, T>) if constexpr (std::is_base_of_v<StrongRefBase, T>)
{ {
mPtr->decRefCount(); T* live = getPointer();
if (live)
live->decRefCount();
} }
} }