#include "sharedobject.h"
#include "uassert.h"
U_NAMESPACE_BEGIN
SharedObject::~SharedObject() {}
UnifiedCacheBase::~UnifiedCacheBase() {}
void
SharedObject::addRef(UBool fromWithinCache) const {
umtx_atomic_inc(&totalRefCount);
if (umtx_atomic_inc(&hardRefCount) == 1 && cachePtr != NULL) {
(void)fromWithinCache; U_ASSERT(fromWithinCache);
cachePtr->incrementItemsInUse();
}
}
void
SharedObject::removeRef(UBool fromWithinCache) const {
UBool decrementItemsInUse = (umtx_atomic_dec(&hardRefCount) == 0);
UBool allReferencesGone = (umtx_atomic_dec(&totalRefCount) == 0);
if (decrementItemsInUse && cachePtr != NULL) {
if (fromWithinCache) {
cachePtr->decrementItemsInUse();
} else {
cachePtr->decrementItemsInUseWithLockingAndEviction();
}
}
if (allReferencesGone) {
delete this;
}
}
void
SharedObject::addSoftRef() const {
umtx_atomic_inc(&totalRefCount);
++softRefCount;
}
void
SharedObject::removeSoftRef() const {
--softRefCount;
if (umtx_atomic_dec(&totalRefCount) == 0) {
delete this;
}
}
int32_t
SharedObject::getRefCount() const {
return umtx_loadAcquire(totalRefCount);
}
int32_t
SharedObject::getHardRefCount() const {
return umtx_loadAcquire(hardRefCount);
}
void
SharedObject::deleteIfZeroRefCount() const {
if(getRefCount() == 0) {
delete this;
}
}
U_NAMESPACE_END