1 // © 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 /* 4 ****************************************************************************** 5 * Copyright (C) 2015, International Business Machines 6 * Corporation and others. All Rights Reserved. 7 ****************************************************************************** 8 * sharedobject.cpp 9 */ 10 #include "sharedobject.h" 11 #include "mutex.h" 12 #include "uassert.h" 13 #include "umutex.h" 14 #include "unifiedcache.h" 15 16 U_NAMESPACE_BEGIN 17 ~SharedObject()18SharedObject::~SharedObject() {} 19 ~UnifiedCacheBase()20UnifiedCacheBase::~UnifiedCacheBase() {} 21 22 void addRef() const23SharedObject::addRef() const { 24 umtx_atomic_inc(&hardRefCount); 25 } 26 27 // removeRef Decrement the reference count and delete if it is zero. 28 // Note that SharedObjects with a non-null cachePtr are owned by the 29 // unified cache, and the cache will be responsible for the actual deletion. 30 // The deletion could be as soon as immediately following the 31 // update to the reference count, if another thread is running 32 // a cache eviction cycle concurrently. 33 // NO ACCESS TO *this PERMITTED AFTER REFERENCE COUNT == 0 for cached objects. 34 // THE OBJECT MAY ALREADY BE GONE. 35 void removeRef() const36SharedObject::removeRef() const { 37 const UnifiedCacheBase *cache = this->cachePtr; 38 int32_t updatedRefCount = umtx_atomic_dec(&hardRefCount); 39 U_ASSERT(updatedRefCount >= 0); 40 if (updatedRefCount == 0) { 41 if (cache) { 42 cache->handleUnreferencedObject(); 43 } else { 44 delete this; 45 } 46 } 47 } 48 49 50 int32_t getRefCount() const51SharedObject::getRefCount() const { 52 return umtx_loadAcquire(hardRefCount); 53 } 54 55 void deleteIfZeroRefCount() const56SharedObject::deleteIfZeroRefCount() const { 57 if (this->cachePtr == nullptr && getRefCount() == 0) { 58 delete this; 59 } 60 } 61 62 U_NAMESPACE_END 63