]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
57a6839d A |
3 | /* |
4 | ****************************************************************************** | |
2ca993e8 | 5 | * Copyright (C) 2015, International Business Machines |
57a6839d A |
6 | * Corporation and others. All Rights Reserved. |
7 | ****************************************************************************** | |
8 | * sharedobject.cpp | |
9 | */ | |
10 | #include "sharedobject.h" | |
2ca993e8 | 11 | #include "uassert.h" |
57a6839d A |
12 | |
13 | U_NAMESPACE_BEGIN | |
2ca993e8 | 14 | |
57a6839d A |
15 | SharedObject::~SharedObject() {} |
16 | ||
2ca993e8 A |
17 | UnifiedCacheBase::~UnifiedCacheBase() {} |
18 | ||
57a6839d | 19 | void |
2ca993e8 | 20 | SharedObject::addRef(UBool fromWithinCache) const { |
b331163b | 21 | umtx_atomic_inc(&totalRefCount); |
2ca993e8 A |
22 | |
23 | // Although items in use may not be correct immediately, it | |
24 | // will be correct eventually. | |
25 | 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 | U_ASSERT(fromWithinCache); | |
33 | cachePtr->incrementItemsInUse(); | |
34 | } | |
57a6839d A |
35 | } |
36 | ||
37 | void | |
2ca993e8 A |
38 | SharedObject::removeRef(UBool fromWithinCache) const { |
39 | UBool decrementItemsInUse = (umtx_atomic_dec(&hardRefCount) == 0); | |
40 | 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 | if (decrementItemsInUse && cachePtr != NULL) { | |
45 | if (fromWithinCache) { | |
46 | cachePtr->decrementItemsInUse(); | |
47 | } else { | |
48 | cachePtr->decrementItemsInUseWithLockingAndEviction(); | |
49 | } | |
50 | } | |
51 | if (allReferencesGone) { | |
57a6839d A |
52 | delete this; |
53 | } | |
54 | } | |
55 | ||
b331163b A |
56 | void |
57 | SharedObject::addSoftRef() const { | |
2ca993e8 A |
58 | umtx_atomic_inc(&totalRefCount); |
59 | ++softRefCount; | |
b331163b A |
60 | } |
61 | ||
62 | void | |
63 | SharedObject::removeSoftRef() const { | |
2ca993e8 A |
64 | --softRefCount; |
65 | if (umtx_atomic_dec(&totalRefCount) == 0) { | |
66 | delete this; | |
67 | } | |
b331163b A |
68 | } |
69 | ||
57a6839d A |
70 | int32_t |
71 | SharedObject::getRefCount() const { | |
b331163b A |
72 | return umtx_loadAcquire(totalRefCount); |
73 | } | |
74 | ||
75 | int32_t | |
2ca993e8 A |
76 | SharedObject::getHardRefCount() const { |
77 | return umtx_loadAcquire(hardRefCount); | |
57a6839d A |
78 | } |
79 | ||
80 | void | |
81 | SharedObject::deleteIfZeroRefCount() const { | |
82 | if(getRefCount() == 0) { | |
83 | delete this; | |
84 | } | |
85 | } | |
86 | ||
87 | U_NAMESPACE_END |