OLD | NEW |
1 /* | 1 /* |
2 ****************************************************************************** | 2 ****************************************************************************** |
3 * Copyright (C) 2014, International Business Machines | 3 * Copyright (C) 2015, International Business Machines |
4 * Corporation and others. All Rights Reserved. | 4 * Corporation and others. All Rights Reserved. |
5 ****************************************************************************** | 5 ****************************************************************************** |
6 * sharedobject.h | 6 * sharedobject.h |
7 */ | 7 */ |
8 | 8 |
9 #ifndef __SHAREDOBJECT_H__ | 9 #ifndef __SHAREDOBJECT_H__ |
10 #define __SHAREDOBJECT_H__ | 10 #define __SHAREDOBJECT_H__ |
11 | 11 |
12 | 12 |
13 #include "unicode/uobject.h" | 13 #include "unicode/uobject.h" |
14 #include "umutex.h" | 14 #include "umutex.h" |
15 | 15 |
16 U_NAMESPACE_BEGIN | 16 U_NAMESPACE_BEGIN |
17 | 17 |
18 /** | 18 /** |
| 19 * Base class for unified cache exposing enough methods to SharedObject |
| 20 * instances to allow their addRef() and removeRef() methods to |
| 21 * update cache metrics. No other part of ICU, except for SharedObject, |
| 22 * should directly call the methods of this base class. |
| 23 */ |
| 24 class UnifiedCacheBase : public UObject { |
| 25 public: |
| 26 UnifiedCacheBase() { } |
| 27 |
| 28 /** |
| 29 * Called by addRefWhileHoldingCacheLock() when the hard reference count |
| 30 * of its instance goes from 0 to 1. |
| 31 */ |
| 32 virtual void incrementItemsInUse() const = 0; |
| 33 |
| 34 /** |
| 35 * Called by removeRef() when the hard reference count of its instance |
| 36 * drops from 1 to 0. |
| 37 */ |
| 38 virtual void decrementItemsInUseWithLockingAndEviction() const = 0; |
| 39 |
| 40 /** |
| 41 * Called by removeRefWhileHoldingCacheLock() when the hard reference |
| 42 * count of its instance drops from 1 to 0. |
| 43 */ |
| 44 virtual void decrementItemsInUse() const = 0; |
| 45 virtual ~UnifiedCacheBase(); |
| 46 private: |
| 47 UnifiedCacheBase(const UnifiedCacheBase &); |
| 48 UnifiedCacheBase &operator=(const UnifiedCacheBase &); |
| 49 }; |
| 50 |
| 51 /** |
19 * Base class for shared, reference-counted, auto-deleted objects. | 52 * Base class for shared, reference-counted, auto-deleted objects. |
20 * Subclasses can be immutable. | 53 * Subclasses can be immutable. |
21 * If they are mutable, then they must implement their copy constructor | 54 * If they are mutable, then they must implement their copy constructor |
22 * so that copyOnWrite() works. | 55 * so that copyOnWrite() works. |
23 * | 56 * |
24 * Either stack-allocate, use LocalPointer, or use addRef()/removeRef(). | 57 * Either stack-allocate, use LocalPointer, or use addRef()/removeRef(). |
25 * Sharing requires reference-counting. | 58 * Sharing requires reference-counting. |
26 */ | 59 */ |
27 class U_COMMON_API SharedObject : public UObject { | 60 class U_COMMON_API SharedObject : public UObject { |
28 public: | 61 public: |
29 /** Initializes totalRefCount, softRefCount to 0. */ | 62 /** Initializes totalRefCount, softRefCount to 0. */ |
30 SharedObject() : totalRefCount(0), softRefCount(0) {} | 63 SharedObject() : |
| 64 totalRefCount(0), |
| 65 softRefCount(0), |
| 66 hardRefCount(0), |
| 67 cachePtr(NULL) {} |
31 | 68 |
32 /** Initializes totalRefCount, softRefCount to 0. */ | 69 /** Initializes totalRefCount, softRefCount to 0. */ |
33 SharedObject(const SharedObject &other) | 70 SharedObject(const SharedObject &other) : |
34 : UObject(other), | 71 UObject(other), |
35 totalRefCount(0), | 72 totalRefCount(0), |
36 softRefCount(0) {} | 73 softRefCount(0), |
| 74 hardRefCount(0), |
| 75 cachePtr(NULL) {} |
37 | 76 |
38 virtual ~SharedObject(); | 77 virtual ~SharedObject(); |
39 | 78 |
40 /** | 79 /** |
41 * Increments the number of references to this object. Thread-safe. | 80 * Increments the number of references to this object. Thread-safe. |
42 */ | 81 */ |
43 void addRef() const; | 82 void addRef() const { addRef(FALSE); } |
44 | 83 |
45 /** | 84 /** |
46 * Increments the number of soft references to this object. Thread-safe. | 85 * Increments the number of references to this object. |
| 86 * Must be called only from within the internals of UnifiedCache and |
| 87 * only while the cache global mutex is held. |
| 88 */ |
| 89 void addRefWhileHoldingCacheLock() const { addRef(TRUE); } |
| 90 |
| 91 /** |
| 92 * Increments the number of soft references to this object. |
| 93 * Must be called only from within the internals of UnifiedCache and |
| 94 * only while the cache global mutex is held. |
47 */ | 95 */ |
48 void addSoftRef() const; | 96 void addSoftRef() const; |
49 | 97 |
50 /** | 98 /** |
51 * Decrements the number of references to this object. Thread-safe. | 99 * Decrements the number of references to this object. Thread-safe. |
52 */ | 100 */ |
53 void removeRef() const; | 101 void removeRef() const { removeRef(FALSE); } |
54 | 102 |
55 /** | 103 /** |
56 * Decrements the number of soft references to this object. Thread-safe. | 104 * Decrements the number of references to this object. |
| 105 * Must be called only from within the internals of UnifiedCache and |
| 106 * only while the cache global mutex is held. |
| 107 */ |
| 108 void removeRefWhileHoldingCacheLock() const { removeRef(TRUE); } |
| 109 |
| 110 /** |
| 111 * Decrements the number of soft references to this object. |
| 112 * Must be called only from within the internals of UnifiedCache and |
| 113 * only while the cache global mutex is held. |
57 */ | 114 */ |
58 void removeSoftRef() const; | 115 void removeSoftRef() const; |
59 | 116 |
60 /** | 117 /** |
61 * Returns the reference counter including soft references. | 118 * Returns the reference counter including soft references. |
62 * Uses a memory barrier. | 119 * Uses a memory barrier. |
63 */ | 120 */ |
64 int32_t getRefCount() const; | 121 int32_t getRefCount() const; |
65 | 122 |
66 /** | 123 /** |
67 * Returns the count of soft references only. Uses a memory barrier. | 124 * Returns the count of soft references only. |
| 125 * Must be called only from within the internals of UnifiedCache and |
| 126 * only while the cache global mutex is held. |
| 127 */ |
| 128 int32_t getSoftRefCount() const { return softRefCount; } |
| 129 |
| 130 /** |
| 131 * Returns the count of hard references only. Uses a memory barrier. |
68 * Used for testing the cache. Regular clients won't need this. | 132 * Used for testing the cache. Regular clients won't need this. |
69 */ | 133 */ |
70 int32_t getSoftRefCount() const; | 134 int32_t getHardRefCount() const; |
71 | 135 |
72 /** | 136 /** |
73 * If allSoftReferences() == TRUE then this object has only soft | 137 * If noHardReferences() == TRUE then this object has no hard references. |
74 * references. The converse is not necessarily true. | 138 * Must be called only from within the internals of UnifiedCache. |
75 */ | 139 */ |
76 UBool allSoftReferences() const; | 140 inline UBool noHardReferences() const { return getHardRefCount() == 0; } |
| 141 |
| 142 /** |
| 143 * If hasHardReferences() == TRUE then this object has hard references. |
| 144 * Must be called only from within the internals of UnifiedCache. |
| 145 */ |
| 146 inline UBool hasHardReferences() const { return getHardRefCount() != 0; } |
| 147 |
| 148 /** |
| 149 * If noSoftReferences() == TRUE then this object has no soft references. |
| 150 * Must be called only from within the internals of UnifiedCache and |
| 151 * only while the cache global mutex is held. |
| 152 */ |
| 153 UBool noSoftReferences() const { return (softRefCount == 0); } |
77 | 154 |
78 /** | 155 /** |
79 * Deletes this object if it has no references or soft references. | 156 * Deletes this object if it has no references or soft references. |
80 */ | 157 */ |
81 void deleteIfZeroRefCount() const; | 158 void deleteIfZeroRefCount() const; |
82 | 159 |
83 /** | 160 /** |
| 161 * @internal For UnifedCache use only to register this object with itself. |
| 162 * Must be called before this object is exposed to multiple threads. |
| 163 */ |
| 164 void registerWithCache(const UnifiedCacheBase *ptr) const { |
| 165 cachePtr = ptr; |
| 166 } |
| 167 |
| 168 /** |
84 * Returns a writable version of ptr. | 169 * Returns a writable version of ptr. |
85 * If there is exactly one owner, then ptr itself is returned as a | 170 * If there is exactly one owner, then ptr itself is returned as a |
86 * non-const pointer. | 171 * non-const pointer. |
87 * If there are multiple owners, then ptr is replaced with a | 172 * If there are multiple owners, then ptr is replaced with a |
88 * copy-constructed clone, | 173 * copy-constructed clone, |
89 * and that is returned. | 174 * and that is returned. |
90 * Returns NULL if cloning failed. | 175 * Returns NULL if cloning failed. |
91 * | 176 * |
92 * T must be a subclass of SharedObject. | 177 * T must be a subclass of SharedObject. |
93 */ | 178 */ |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 template<typename T> | 211 template<typename T> |
127 static void clearPtr(const T *&ptr) { | 212 static void clearPtr(const T *&ptr) { |
128 if (ptr != NULL) { | 213 if (ptr != NULL) { |
129 ptr->removeRef(); | 214 ptr->removeRef(); |
130 ptr = NULL; | 215 ptr = NULL; |
131 } | 216 } |
132 } | 217 } |
133 | 218 |
134 private: | 219 private: |
135 mutable u_atomic_int32_t totalRefCount; | 220 mutable u_atomic_int32_t totalRefCount; |
136 mutable u_atomic_int32_t softRefCount; | 221 |
| 222 // Any thread modifying softRefCount must hold the global cache mutex |
| 223 mutable int32_t softRefCount; |
| 224 |
| 225 mutable u_atomic_int32_t hardRefCount; |
| 226 mutable const UnifiedCacheBase *cachePtr; |
| 227 void addRef(UBool withCacheLock) const; |
| 228 void removeRef(UBool withCacheLock) const; |
| 229 |
137 }; | 230 }; |
138 | 231 |
139 U_NAMESPACE_END | 232 U_NAMESPACE_END |
140 | 233 |
141 #endif | 234 #endif |
OLD | NEW |