X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/b331163bffd790ced0e88b73f44f86d49ccc48a5..4f1e1a09ce4daed860e35d359ce2fceccb0764e8:/icuSources/common/sharedobject.cpp diff --git a/icuSources/common/sharedobject.cpp b/icuSources/common/sharedobject.cpp index 6affcd09..6eeca860 100644 --- a/icuSources/common/sharedobject.cpp +++ b/icuSources/common/sharedobject.cpp @@ -1,57 +1,60 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /* ****************************************************************************** -* Copyright (C) 2014, International Business Machines +* Copyright (C) 2015, International Business Machines * Corporation and others. All Rights Reserved. ****************************************************************************** * sharedobject.cpp */ #include "sharedobject.h" +#include "mutex.h" +#include "uassert.h" +#include "umutex.h" +#include "unifiedcache.h" U_NAMESPACE_BEGIN + SharedObject::~SharedObject() {} +UnifiedCacheBase::~UnifiedCacheBase() {} + void SharedObject::addRef() const { - umtx_atomic_inc(&totalRefCount); + umtx_atomic_inc(&hardRefCount); } +// removeRef Decrement the reference count and delete if it is zero. +// Note that SharedObjects with a non-null cachePtr are owned by the +// unified cache, and the cache will be responsible for the actual deletion. +// The deletion could be as soon as immediately following the +// update to the reference count, if another thread is running +// a cache eviction cycle concurrently. +// NO ACCESS TO *this PERMITTED AFTER REFERENCE COUNT == 0 for cached objects. +// THE OBJECT MAY ALREADY BE GONE. void SharedObject::removeRef() const { - if(umtx_atomic_dec(&totalRefCount) == 0) { - delete this; + const UnifiedCacheBase *cache = this->cachePtr; + int32_t updatedRefCount = umtx_atomic_dec(&hardRefCount); + U_ASSERT(updatedRefCount >= 0); + if (updatedRefCount == 0) { + if (cache) { + cache->handleUnreferencedObject(); + } else { + delete this; + } } } -void -SharedObject::addSoftRef() const { - addRef(); - umtx_atomic_inc(&softRefCount); -} - -void -SharedObject::removeSoftRef() const { - umtx_atomic_dec(&softRefCount); - removeRef(); -} - -UBool -SharedObject::allSoftReferences() const { - return umtx_loadAcquire(totalRefCount) == umtx_loadAcquire(softRefCount); -} int32_t SharedObject::getRefCount() const { - return umtx_loadAcquire(totalRefCount); -} - -int32_t -SharedObject::getSoftRefCount() const { - return umtx_loadAcquire(softRefCount); + return umtx_loadAcquire(hardRefCount); } void SharedObject::deleteIfZeroRefCount() const { - if(getRefCount() == 0) { + if (this->cachePtr == nullptr && getRefCount() == 0) { delete this; } }