diff --git a/base/include/refbase.h b/base/include/refbase.h old mode 100755 new mode 100644 index 4c4c24b3cf2b6cac893b562ed349ac651fc880d2..ed0fc28ccd2fe0f497b74050587b3331cccdd2fb --- a/base/include/refbase.h +++ b/base/include/refbase.h @@ -70,6 +70,8 @@ public: bool AttemptIncStrongRef(const void *objectId, int &outCount); + bool AttemptIncStrong(const void *objectId); + bool IsLifeTimeExtended(); void ExtendObjectLifetime(); @@ -140,6 +142,8 @@ public: bool AttemptIncStrongRef(const void *objectId); + bool AttemptIncStrong(const void *objectId); + bool IsAttemptAcquireSet(); bool IsExtendLifeTimeSet(); diff --git a/base/src/refbase.cpp b/base/src/refbase.cpp old mode 100755 new mode 100644 index fd314d1e04b35740179288cbfc9d2c6ce6838868..abd5f427f037d14a1ce6b1e66a5dcccc5eef776a --- a/base/src/refbase.cpp +++ b/base/src/refbase.cpp @@ -245,6 +245,22 @@ ATTEMPT_SUCCESS: return true; } +bool RefCounter::AttemptIncStrong(const void *objectId) +{ + IncWeakRefCount(objectId); + int curCount = GetStrongRefCount(); + while (curCount > 0) { + if (atomicStrong_.compare_exchange_weak(curCount, curCount + 1, std::memory_order_relaxed)) { + break; + } + // curCount has been updated + } + if (curCount <= 0) { + DecWeakRefCount(objectId); + } + return curCount > 0; +} + RefBase::RefBase() : refs_(new RefCounter()) { refs_->IncRefCount(); @@ -332,8 +348,8 @@ void RefBase::IncStrongRef(const void *objectId) return; } - const int curCount = refs_->IncStrongRefCount(objectId); IncWeakRef(objectId); + const int curCount = refs_->IncStrongRefCount(objectId); if (curCount == INITIAL_PRIMARY_VALUE) { OnFirstStrongRef(objectId); } @@ -434,6 +450,18 @@ bool RefBase::AttemptIncStrongRef(const void *objectId) return false; } +bool RefBase::AttemptIncStrong(const void *objectId) +{ + if (refs_ == nullptr) { + return false; + } + if (refs_->AttemptIncStrong(objectId)) { + refs_->SetAttemptAcquire(); + return true; + } + return false; +} + bool RefBase::IsAttemptAcquireSet() { if (refs_ == nullptr) {