Line data Source code
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 "uassert.h"
12 :
13 : U_NAMESPACE_BEGIN
14 :
15 0 : SharedObject::~SharedObject() {}
16 :
17 0 : UnifiedCacheBase::~UnifiedCacheBase() {}
18 :
19 : void
20 0 : SharedObject::addRef(UBool fromWithinCache) const {
21 0 : umtx_atomic_inc(&totalRefCount);
22 :
23 : // Although items in use may not be correct immediately, it
24 : // will be correct eventually.
25 0 : if (umtx_atomic_inc(&hardRefCount) == 1 && cachePtr != NULL) {
26 : // If this object is cached, and the hardRefCount goes from 0 to 1,
27 : // then the increment must happen from within the cache while the
28 : // cache global mutex is locked. In this way, we can be rest assured
29 : // that data races can't happen if the cache performs some task if
30 : // the hardRefCount is zero while the global cache mutex is locked.
31 : (void)fromWithinCache; // Suppress unused variable warning in non-debug builds.
32 0 : U_ASSERT(fromWithinCache);
33 0 : cachePtr->incrementItemsInUse();
34 : }
35 0 : }
36 :
37 : void
38 0 : SharedObject::removeRef(UBool fromWithinCache) const {
39 0 : UBool decrementItemsInUse = (umtx_atomic_dec(&hardRefCount) == 0);
40 0 : UBool allReferencesGone = (umtx_atomic_dec(&totalRefCount) == 0);
41 :
42 : // Although items in use may not be correct immediately, it
43 : // will be correct eventually.
44 0 : if (decrementItemsInUse && cachePtr != NULL) {
45 0 : if (fromWithinCache) {
46 0 : cachePtr->decrementItemsInUse();
47 : } else {
48 0 : cachePtr->decrementItemsInUseWithLockingAndEviction();
49 : }
50 : }
51 0 : if (allReferencesGone) {
52 0 : delete this;
53 : }
54 0 : }
55 :
56 : void
57 0 : SharedObject::addSoftRef() const {
58 0 : umtx_atomic_inc(&totalRefCount);
59 0 : ++softRefCount;
60 0 : }
61 :
62 : void
63 0 : SharedObject::removeSoftRef() const {
64 0 : --softRefCount;
65 0 : if (umtx_atomic_dec(&totalRefCount) == 0) {
66 0 : delete this;
67 : }
68 0 : }
69 :
70 : int32_t
71 0 : SharedObject::getRefCount() const {
72 0 : return umtx_loadAcquire(totalRefCount);
73 : }
74 :
75 : int32_t
76 0 : SharedObject::getHardRefCount() const {
77 0 : return umtx_loadAcquire(hardRefCount);
78 : }
79 :
80 : void
81 0 : SharedObject::deleteIfZeroRefCount() const {
82 0 : if(getRefCount() == 0) {
83 0 : delete this;
84 : }
85 0 : }
86 :
87 : U_NAMESPACE_END
|