1 /* 2 ****************************************************************************** 3 * Copyright (C) 2015, International Business Machines 4 * Corporation and others. All Rights Reserved. 5 ****************************************************************************** 6 * sharedobject.cpp 7 */ 8 #include "sharedobject.h" 9 #include "uassert.h" 10 11 U_NAMESPACE_BEGIN 12 ~SharedObject()13SharedObject::~SharedObject() {} 14 ~UnifiedCacheBase()15UnifiedCacheBase::~UnifiedCacheBase() {} 16 17 void addRef(UBool fromWithinCache) const18SharedObject::addRef(UBool fromWithinCache) const { 19 umtx_atomic_inc(&totalRefCount); 20 21 // Although items in use may not be correct immediately, it 22 // will be correct eventually. 23 if (umtx_atomic_inc(&hardRefCount) == 1 && cachePtr != NULL) { 24 // If this object is cached, and the hardRefCount goes from 0 to 1, 25 // then the increment must happen from within the cache while the 26 // cache global mutex is locked. In this way, we can be rest assured 27 // that data races can't happen if the cache performs some task if 28 // the hardRefCount is zero while the global cache mutex is locked. 29 U_ASSERT(fromWithinCache); 30 cachePtr->incrementItemsInUse(); 31 } 32 } 33 34 void removeRef(UBool fromWithinCache) const35SharedObject::removeRef(UBool fromWithinCache) const { 36 UBool decrementItemsInUse = (umtx_atomic_dec(&hardRefCount) == 0); 37 UBool allReferencesGone = (umtx_atomic_dec(&totalRefCount) == 0); 38 39 // Although items in use may not be correct immediately, it 40 // will be correct eventually. 41 if (decrementItemsInUse && cachePtr != NULL) { 42 if (fromWithinCache) { 43 cachePtr->decrementItemsInUse(); 44 } else { 45 cachePtr->decrementItemsInUseWithLockingAndEviction(); 46 } 47 } 48 if (allReferencesGone) { 49 delete this; 50 } 51 } 52 53 void addSoftRef() const54SharedObject::addSoftRef() const { 55 umtx_atomic_inc(&totalRefCount); 56 ++softRefCount; 57 } 58 59 void removeSoftRef() const60SharedObject::removeSoftRef() const { 61 --softRefCount; 62 if (umtx_atomic_dec(&totalRefCount) == 0) { 63 delete this; 64 } 65 } 66 67 int32_t getRefCount() const68SharedObject::getRefCount() const { 69 return umtx_loadAcquire(totalRefCount); 70 } 71 72 int32_t getHardRefCount() const73SharedObject::getHardRefCount() const { 74 return umtx_loadAcquire(hardRefCount); 75 } 76 77 void deleteIfZeroRefCount() const78SharedObject::deleteIfZeroRefCount() const { 79 if(getRefCount() == 0) { 80 delete this; 81 } 82 } 83 84 U_NAMESPACE_END 85