2 ******************************************************************************
3 * Copyright (C) 2015, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ******************************************************************************
8 #include "sharedobject.h"
13 SharedObject::~SharedObject() {}
15 UnifiedCacheBase::~UnifiedCacheBase() {}
18 SharedObject::addRef(UBool fromWithinCache
) const {
19 umtx_atomic_inc(&totalRefCount
);
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 (void)fromWithinCache
; // Suppress unused variable warning in non-debug builds.
30 U_ASSERT(fromWithinCache
);
31 cachePtr
->incrementItemsInUse();
36 SharedObject::removeRef(UBool fromWithinCache
) const {
37 UBool decrementItemsInUse
= (umtx_atomic_dec(&hardRefCount
) == 0);
38 UBool allReferencesGone
= (umtx_atomic_dec(&totalRefCount
) == 0);
40 // Although items in use may not be correct immediately, it
41 // will be correct eventually.
42 if (decrementItemsInUse
&& cachePtr
!= NULL
) {
43 if (fromWithinCache
) {
44 cachePtr
->decrementItemsInUse();
46 cachePtr
->decrementItemsInUseWithLockingAndEviction();
49 if (allReferencesGone
) {
55 SharedObject::addSoftRef() const {
56 umtx_atomic_inc(&totalRefCount
);
61 SharedObject::removeSoftRef() const {
63 if (umtx_atomic_dec(&totalRefCount
) == 0) {
69 SharedObject::getRefCount() const {
70 return umtx_loadAcquire(totalRefCount
);
74 SharedObject::getHardRefCount() const {
75 return umtx_loadAcquire(hardRefCount
);
79 SharedObject::deleteIfZeroRefCount() const {
80 if(getRefCount() == 0) {