]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/sharedobject.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
5 * Copyright (C) 2015, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
10 #include "sharedobject.h"
14 #include "unifiedcache.h"
18 SharedObject::~SharedObject() {}
20 UnifiedCacheBase::~UnifiedCacheBase() {}
23 SharedObject::addRef() const {
24 umtx_atomic_inc(&hardRefCount
);
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.
36 SharedObject::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) {
42 cache
->handleUnreferencedObject();
51 SharedObject::getRefCount() const {
52 return umtx_loadAcquire(hardRefCount
);
56 SharedObject::deleteIfZeroRefCount() const {
57 if (this->cachePtr
== nullptr && getRefCount() == 0) {