]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
b331163b A |
3 | /* |
4 | ****************************************************************************** | |
0f5d89e8 A |
5 | * Copyright (C) 2015, International Business Machines Corporation and |
6 | * others. All Rights Reserved. | |
b331163b | 7 | ****************************************************************************** |
0f5d89e8 A |
8 | * |
9 | * File unifiedcache.cpp | |
b331163b A |
10 | ****************************************************************************** |
11 | */ | |
12 | ||
b331163b | 13 | #include "unifiedcache.h" |
0f5d89e8 A |
14 | |
15 | #include <algorithm> // For std::max() | |
3d1f044b | 16 | #include <mutex> |
0f5d89e8 | 17 | |
b331163b | 18 | #include "uassert.h" |
0f5d89e8 | 19 | #include "uhash.h" |
b331163b A |
20 | #include "ucln_cmn.h" |
21 | ||
22 | static icu::UnifiedCache *gCache = NULL; | |
340931cb A |
23 | static std::mutex *gCacheMutex = nullptr; |
24 | static std::condition_variable *gInProgressValueAddedCond; | |
b331163b | 25 | static icu::UInitOnce gCacheInitOnce = U_INITONCE_INITIALIZER; |
2ca993e8 | 26 | |
0f5d89e8 A |
27 | static const int32_t MAX_EVICT_ITERATIONS = 10; |
28 | static const int32_t DEFAULT_MAX_UNUSED = 1000; | |
29 | static const int32_t DEFAULT_PERCENTAGE_OF_IN_USE = 100; | |
2ca993e8 | 30 | |
b331163b A |
31 | |
32 | U_CDECL_BEGIN | |
33 | static UBool U_CALLCONV unifiedcache_cleanup() { | |
34 | gCacheInitOnce.reset(); | |
340931cb A |
35 | delete gCache; |
36 | gCache = nullptr; | |
37 | gCacheMutex->~mutex(); | |
38 | gCacheMutex = nullptr; | |
39 | gInProgressValueAddedCond->~condition_variable(); | |
40 | gInProgressValueAddedCond = nullptr; | |
b331163b A |
41 | return TRUE; |
42 | } | |
43 | U_CDECL_END | |
44 | ||
45 | ||
46 | U_NAMESPACE_BEGIN | |
47 | ||
48 | U_CAPI int32_t U_EXPORT2 | |
49 | ucache_hashKeys(const UHashTok key) { | |
50 | const CacheKeyBase *ckey = (const CacheKeyBase *) key.pointer; | |
51 | return ckey->hashCode(); | |
52 | } | |
53 | ||
54 | U_CAPI UBool U_EXPORT2 | |
55 | ucache_compareKeys(const UHashTok key1, const UHashTok key2) { | |
56 | const CacheKeyBase *p1 = (const CacheKeyBase *) key1.pointer; | |
57 | const CacheKeyBase *p2 = (const CacheKeyBase *) key2.pointer; | |
58 | return *p1 == *p2; | |
59 | } | |
60 | ||
61 | U_CAPI void U_EXPORT2 | |
62 | ucache_deleteKey(void *obj) { | |
63 | CacheKeyBase *p = (CacheKeyBase *) obj; | |
64 | delete p; | |
65 | } | |
66 | ||
67 | CacheKeyBase::~CacheKeyBase() { | |
68 | } | |
69 | ||
70 | static void U_CALLCONV cacheInit(UErrorCode &status) { | |
71 | U_ASSERT(gCache == NULL); | |
72 | ucln_common_registerCleanup( | |
73 | UCLN_COMMON_UNIFIED_CACHE, unifiedcache_cleanup); | |
74 | ||
340931cb A |
75 | gCacheMutex = STATIC_NEW(std::mutex); |
76 | gInProgressValueAddedCond = STATIC_NEW(std::condition_variable); | |
b331163b A |
77 | gCache = new UnifiedCache(status); |
78 | if (gCache == NULL) { | |
79 | status = U_MEMORY_ALLOCATION_ERROR; | |
80 | } | |
81 | if (U_FAILURE(status)) { | |
82 | delete gCache; | |
b331163b | 83 | gCache = NULL; |
b331163b A |
84 | return; |
85 | } | |
b331163b A |
86 | } |
87 | ||
2ca993e8 | 88 | UnifiedCache *UnifiedCache::getInstance(UErrorCode &status) { |
b331163b A |
89 | umtx_initOnce(gCacheInitOnce, &cacheInit, status); |
90 | if (U_FAILURE(status)) { | |
91 | return NULL; | |
92 | } | |
93 | U_ASSERT(gCache != NULL); | |
94 | return gCache; | |
95 | } | |
96 | ||
2ca993e8 A |
97 | UnifiedCache::UnifiedCache(UErrorCode &status) : |
98 | fHashtable(NULL), | |
99 | fEvictPos(UHASH_FIRST), | |
0f5d89e8 A |
100 | fNumValuesTotal(0), |
101 | fNumValuesInUse(0), | |
2ca993e8 A |
102 | fMaxUnused(DEFAULT_MAX_UNUSED), |
103 | fMaxPercentageOfInUse(DEFAULT_PERCENTAGE_OF_IN_USE), | |
0f5d89e8 A |
104 | fAutoEvictedCount(0), |
105 | fNoValue(nullptr) { | |
b331163b A |
106 | if (U_FAILURE(status)) { |
107 | return; | |
108 | } | |
0f5d89e8 A |
109 | fNoValue = new SharedObject(); |
110 | if (fNoValue == nullptr) { | |
111 | status = U_MEMORY_ALLOCATION_ERROR; | |
112 | return; | |
113 | } | |
114 | fNoValue->softRefCount = 1; // Add fake references to prevent fNoValue from being deleted | |
115 | fNoValue->hardRefCount = 1; // when other references to it are removed. | |
116 | fNoValue->cachePtr = this; | |
117 | ||
b331163b A |
118 | fHashtable = uhash_open( |
119 | &ucache_hashKeys, | |
120 | &ucache_compareKeys, | |
121 | NULL, | |
122 | &status); | |
123 | if (U_FAILURE(status)) { | |
124 | return; | |
125 | } | |
126 | uhash_setKeyDeleter(fHashtable, &ucache_deleteKey); | |
127 | } | |
128 | ||
2ca993e8 A |
129 | void UnifiedCache::setEvictionPolicy( |
130 | int32_t count, int32_t percentageOfInUseItems, UErrorCode &status) { | |
131 | if (U_FAILURE(status)) { | |
132 | return; | |
133 | } | |
134 | if (count < 0 || percentageOfInUseItems < 0) { | |
135 | status = U_ILLEGAL_ARGUMENT_ERROR; | |
136 | return; | |
137 | } | |
340931cb | 138 | std::lock_guard<std::mutex> lock(*gCacheMutex); |
2ca993e8 A |
139 | fMaxUnused = count; |
140 | fMaxPercentageOfInUse = percentageOfInUseItems; | |
141 | } | |
142 | ||
143 | int32_t UnifiedCache::unusedCount() const { | |
340931cb | 144 | std::lock_guard<std::mutex> lock(*gCacheMutex); |
0f5d89e8 | 145 | return uhash_count(fHashtable) - fNumValuesInUse; |
2ca993e8 A |
146 | } |
147 | ||
148 | int64_t UnifiedCache::autoEvictedCount() const { | |
340931cb | 149 | std::lock_guard<std::mutex> lock(*gCacheMutex); |
2ca993e8 A |
150 | return fAutoEvictedCount; |
151 | } | |
152 | ||
b331163b | 153 | int32_t UnifiedCache::keyCount() const { |
340931cb | 154 | std::lock_guard<std::mutex> lock(*gCacheMutex); |
b331163b A |
155 | return uhash_count(fHashtable); |
156 | } | |
157 | ||
158 | void UnifiedCache::flush() const { | |
340931cb | 159 | std::lock_guard<std::mutex> lock(*gCacheMutex); |
b331163b A |
160 | |
161 | // Use a loop in case cache items that are flushed held hard references to | |
162 | // other cache items making those additional cache items eligible for | |
163 | // flushing. | |
164 | while (_flush(FALSE)); | |
b331163b A |
165 | } |
166 | ||
0f5d89e8 | 167 | void UnifiedCache::handleUnreferencedObject() const { |
340931cb | 168 | std::lock_guard<std::mutex> lock(*gCacheMutex); |
0f5d89e8 A |
169 | --fNumValuesInUse; |
170 | _runEvictionSlice(); | |
171 | } | |
172 | ||
b331163b A |
173 | #ifdef UNIFIED_CACHE_DEBUG |
174 | #include <stdio.h> | |
175 | ||
176 | void UnifiedCache::dump() { | |
177 | UErrorCode status = U_ZERO_ERROR; | |
178 | const UnifiedCache *cache = getInstance(status); | |
179 | if (U_FAILURE(status)) { | |
180 | fprintf(stderr, "Unified Cache: Error fetching cache.\n"); | |
181 | return; | |
182 | } | |
183 | cache->dumpContents(); | |
184 | } | |
185 | ||
186 | void UnifiedCache::dumpContents() const { | |
340931cb | 187 | std::lock_guard<std::mutex> lock(*gCacheMutex); |
b331163b A |
188 | _dumpContents(); |
189 | } | |
190 | ||
191 | // Dumps content of cache. | |
192 | // On entry, gCacheMutex must be held. | |
193 | // On exit, cache contents dumped to stderr. | |
194 | void UnifiedCache::_dumpContents() const { | |
195 | int32_t pos = UHASH_FIRST; | |
196 | const UHashElement *element = uhash_nextElement(fHashtable, &pos); | |
197 | char buffer[256]; | |
198 | int32_t cnt = 0; | |
199 | for (; element != NULL; element = uhash_nextElement(fHashtable, &pos)) { | |
200 | const SharedObject *sharedObject = | |
201 | (const SharedObject *) element->value.pointer; | |
202 | const CacheKeyBase *key = | |
203 | (const CacheKeyBase *) element->key.pointer; | |
2ca993e8 | 204 | if (sharedObject->hasHardReferences()) { |
b331163b A |
205 | ++cnt; |
206 | fprintf( | |
207 | stderr, | |
0f5d89e8 | 208 | "Unified Cache: Key '%s', error %d, value %p, total refcount %d, soft refcount %d\n", |
b331163b A |
209 | key->writeDescription(buffer, 256), |
210 | key->creationStatus, | |
0f5d89e8 | 211 | sharedObject == fNoValue ? NULL :sharedObject, |
b331163b A |
212 | sharedObject->getRefCount(), |
213 | sharedObject->getSoftRefCount()); | |
214 | } | |
215 | } | |
216 | fprintf(stderr, "Unified Cache: %d out of a total of %d still have hard references\n", cnt, uhash_count(fHashtable)); | |
217 | } | |
218 | #endif | |
219 | ||
220 | UnifiedCache::~UnifiedCache() { | |
221 | // Try our best to clean up first. | |
222 | flush(); | |
223 | { | |
224 | // Now all that should be left in the cache are entries that refer to | |
0f5d89e8 | 225 | // each other and entries with hard references from outside the cache. |
b331163b | 226 | // Nothing we can do about these so proceed to wipe out the cache. |
340931cb | 227 | std::lock_guard<std::mutex> lock(*gCacheMutex); |
b331163b A |
228 | _flush(TRUE); |
229 | } | |
230 | uhash_close(fHashtable); | |
0f5d89e8 A |
231 | fHashtable = nullptr; |
232 | delete fNoValue; | |
233 | fNoValue = nullptr; | |
b331163b A |
234 | } |
235 | ||
2ca993e8 A |
236 | const UHashElement * |
237 | UnifiedCache::_nextElement() const { | |
238 | const UHashElement *element = uhash_nextElement(fHashtable, &fEvictPos); | |
239 | if (element == NULL) { | |
240 | fEvictPos = UHASH_FIRST; | |
241 | return uhash_nextElement(fHashtable, &fEvictPos); | |
242 | } | |
243 | return element; | |
244 | } | |
245 | ||
b331163b A |
246 | UBool UnifiedCache::_flush(UBool all) const { |
247 | UBool result = FALSE; | |
2ca993e8 A |
248 | int32_t origSize = uhash_count(fHashtable); |
249 | for (int32_t i = 0; i < origSize; ++i) { | |
250 | const UHashElement *element = _nextElement(); | |
0f5d89e8 A |
251 | if (element == nullptr) { |
252 | break; | |
253 | } | |
2ca993e8 A |
254 | if (all || _isEvictable(element)) { |
255 | const SharedObject *sharedObject = | |
256 | (const SharedObject *) element->value.pointer; | |
3d1f044b | 257 | U_ASSERT(sharedObject->cachePtr == this); |
b331163b | 258 | uhash_removeElement(fHashtable, element); |
0f5d89e8 | 259 | removeSoftRef(sharedObject); // Deletes the sharedObject when softRefCount goes to zero. |
b331163b A |
260 | result = TRUE; |
261 | } | |
262 | } | |
263 | return result; | |
264 | } | |
265 | ||
2ca993e8 | 266 | int32_t UnifiedCache::_computeCountOfItemsToEvict() const { |
0f5d89e8 A |
267 | int32_t totalItems = uhash_count(fHashtable); |
268 | int32_t evictableItems = totalItems - fNumValuesInUse; | |
269 | ||
270 | int32_t unusedLimitByPercentage = fNumValuesInUse * fMaxPercentageOfInUse / 100; | |
271 | int32_t unusedLimit = std::max(unusedLimitByPercentage, fMaxUnused); | |
272 | int32_t countOfItemsToEvict = std::max(0, evictableItems - unusedLimit); | |
273 | return countOfItemsToEvict; | |
2ca993e8 A |
274 | } |
275 | ||
2ca993e8 A |
276 | void UnifiedCache::_runEvictionSlice() const { |
277 | int32_t maxItemsToEvict = _computeCountOfItemsToEvict(); | |
278 | if (maxItemsToEvict <= 0) { | |
279 | return; | |
280 | } | |
281 | for (int32_t i = 0; i < MAX_EVICT_ITERATIONS; ++i) { | |
282 | const UHashElement *element = _nextElement(); | |
0f5d89e8 A |
283 | if (element == nullptr) { |
284 | break; | |
285 | } | |
2ca993e8 A |
286 | if (_isEvictable(element)) { |
287 | const SharedObject *sharedObject = | |
288 | (const SharedObject *) element->value.pointer; | |
289 | uhash_removeElement(fHashtable, element); | |
0f5d89e8 | 290 | removeSoftRef(sharedObject); // Deletes sharedObject when SoftRefCount goes to zero. |
2ca993e8 A |
291 | ++fAutoEvictedCount; |
292 | if (--maxItemsToEvict == 0) { | |
293 | break; | |
294 | } | |
295 | } | |
296 | } | |
297 | } | |
298 | ||
b331163b | 299 | void UnifiedCache::_putNew( |
0f5d89e8 | 300 | const CacheKeyBase &key, |
b331163b A |
301 | const SharedObject *value, |
302 | const UErrorCode creationStatus, | |
303 | UErrorCode &status) const { | |
304 | if (U_FAILURE(status)) { | |
305 | return; | |
306 | } | |
307 | CacheKeyBase *keyToAdopt = key.clone(); | |
308 | if (keyToAdopt == NULL) { | |
309 | status = U_MEMORY_ALLOCATION_ERROR; | |
310 | return; | |
311 | } | |
2ca993e8 | 312 | keyToAdopt->fCreationStatus = creationStatus; |
0f5d89e8 | 313 | if (value->softRefCount == 0) { |
2ca993e8 A |
314 | _registerMaster(keyToAdopt, value); |
315 | } | |
0f5d89e8 A |
316 | void *oldValue = uhash_put(fHashtable, keyToAdopt, (void *) value, &status); |
317 | U_ASSERT(oldValue == nullptr); | |
318 | (void)oldValue; | |
b331163b | 319 | if (U_SUCCESS(status)) { |
0f5d89e8 | 320 | value->softRefCount++; |
b331163b A |
321 | } |
322 | } | |
323 | ||
b331163b A |
324 | void UnifiedCache::_putIfAbsentAndGet( |
325 | const CacheKeyBase &key, | |
326 | const SharedObject *&value, | |
327 | UErrorCode &status) const { | |
340931cb | 328 | std::lock_guard<std::mutex> lock(*gCacheMutex); |
b331163b A |
329 | const UHashElement *element = uhash_find(fHashtable, &key); |
330 | if (element != NULL && !_inProgress(element)) { | |
331 | _fetch(element, value, status); | |
332 | return; | |
333 | } | |
334 | if (element == NULL) { | |
335 | UErrorCode putError = U_ZERO_ERROR; | |
336 | // best-effort basis only. | |
337 | _putNew(key, value, status, putError); | |
2ca993e8 A |
338 | } else { |
339 | _put(element, value, status); | |
b331163b | 340 | } |
2ca993e8 A |
341 | // Run an eviction slice. This will run even if we added a master entry |
342 | // which doesn't increase the unused count, but that is still o.k | |
343 | _runEvictionSlice(); | |
b331163b A |
344 | } |
345 | ||
0f5d89e8 | 346 | |
b331163b A |
347 | UBool UnifiedCache::_poll( |
348 | const CacheKeyBase &key, | |
349 | const SharedObject *&value, | |
350 | UErrorCode &status) const { | |
351 | U_ASSERT(value == NULL); | |
352 | U_ASSERT(status == U_ZERO_ERROR); | |
340931cb | 353 | std::unique_lock<std::mutex> lock(*gCacheMutex); |
b331163b | 354 | const UHashElement *element = uhash_find(fHashtable, &key); |
0f5d89e8 A |
355 | |
356 | // If the hash table contains an inProgress placeholder entry for this key, | |
357 | // this means that another thread is currently constructing the value object. | |
358 | // Loop, waiting for that construction to complete. | |
340931cb A |
359 | while (element != NULL && _inProgress(element)) { |
360 | gInProgressValueAddedCond->wait(lock); | |
361 | element = uhash_find(fHashtable, &key); | |
b331163b | 362 | } |
0f5d89e8 A |
363 | |
364 | // If the hash table contains an entry for the key, | |
365 | // fetch out the contents and return them. | |
b331163b | 366 | if (element != NULL) { |
0f5d89e8 | 367 | _fetch(element, value, status); |
b331163b A |
368 | return TRUE; |
369 | } | |
0f5d89e8 A |
370 | |
371 | // The hash table contained nothing for this key. | |
372 | // Insert an inProgress place holder value. | |
373 | // Our caller will create the final value and update the hash table. | |
374 | _putNew(key, fNoValue, U_ZERO_ERROR, status); | |
b331163b A |
375 | return FALSE; |
376 | } | |
377 | ||
b331163b A |
378 | void UnifiedCache::_get( |
379 | const CacheKeyBase &key, | |
380 | const SharedObject *&value, | |
381 | const void *creationContext, | |
382 | UErrorCode &status) const { | |
383 | U_ASSERT(value == NULL); | |
384 | U_ASSERT(status == U_ZERO_ERROR); | |
385 | if (_poll(key, value, status)) { | |
0f5d89e8 | 386 | if (value == fNoValue) { |
b331163b A |
387 | SharedObject::clearPtr(value); |
388 | } | |
389 | return; | |
390 | } | |
391 | if (U_FAILURE(status)) { | |
392 | return; | |
393 | } | |
394 | value = key.createObject(creationContext, status); | |
2ca993e8 | 395 | U_ASSERT(value == NULL || value->hasHardReferences()); |
b331163b A |
396 | U_ASSERT(value != NULL || status != U_ZERO_ERROR); |
397 | if (value == NULL) { | |
0f5d89e8 | 398 | SharedObject::copyPtr(fNoValue, value); |
b331163b A |
399 | } |
400 | _putIfAbsentAndGet(key, value, status); | |
0f5d89e8 | 401 | if (value == fNoValue) { |
b331163b A |
402 | SharedObject::clearPtr(value); |
403 | } | |
404 | } | |
405 | ||
0f5d89e8 A |
406 | void UnifiedCache::_registerMaster( |
407 | const CacheKeyBase *theKey, const SharedObject *value) const { | |
408 | theKey->fIsMaster = true; | |
409 | value->cachePtr = this; | |
410 | ++fNumValuesTotal; | |
411 | ++fNumValuesInUse; | |
2ca993e8 A |
412 | } |
413 | ||
b331163b | 414 | void UnifiedCache::_put( |
0f5d89e8 | 415 | const UHashElement *element, |
b331163b | 416 | const SharedObject *value, |
2ca993e8 | 417 | const UErrorCode status) const { |
b331163b A |
418 | U_ASSERT(_inProgress(element)); |
419 | const CacheKeyBase *theKey = (const CacheKeyBase *) element->key.pointer; | |
420 | const SharedObject *oldValue = (const SharedObject *) element->value.pointer; | |
2ca993e8 | 421 | theKey->fCreationStatus = status; |
0f5d89e8 | 422 | if (value->softRefCount == 0) { |
2ca993e8 A |
423 | _registerMaster(theKey, value); |
424 | } | |
0f5d89e8 | 425 | value->softRefCount++; |
b331163b A |
426 | UHashElement *ptr = const_cast<UHashElement *>(element); |
427 | ptr->value.pointer = (void *) value; | |
0f5d89e8 A |
428 | U_ASSERT(oldValue == fNoValue); |
429 | removeSoftRef(oldValue); | |
b331163b A |
430 | |
431 | // Tell waiting threads that we replace in-progress status with | |
432 | // an error. | |
340931cb | 433 | gInProgressValueAddedCond->notify_all(); |
b331163b A |
434 | } |
435 | ||
b331163b A |
436 | void UnifiedCache::_fetch( |
437 | const UHashElement *element, | |
438 | const SharedObject *&value, | |
0f5d89e8 | 439 | UErrorCode &status) const { |
b331163b | 440 | const CacheKeyBase *theKey = (const CacheKeyBase *) element->key.pointer; |
2ca993e8 A |
441 | status = theKey->fCreationStatus; |
442 | ||
0f5d89e8 | 443 | // Since we have the cache lock, calling regular SharedObject add/removeRef |
2ca993e8 A |
444 | // could cause us to deadlock on ourselves since they may need to lock |
445 | // the cache mutex. | |
0f5d89e8 A |
446 | removeHardRef(value); |
447 | value = static_cast<const SharedObject *>(element->value.pointer); | |
448 | addHardRef(value); | |
b331163b | 449 | } |
2ca993e8 | 450 | |
0f5d89e8 A |
451 | |
452 | UBool UnifiedCache::_inProgress(const UHashElement* element) const { | |
b331163b | 453 | UErrorCode status = U_ZERO_ERROR; |
0f5d89e8 | 454 | const SharedObject * value = NULL; |
b331163b | 455 | _fetch(element, value, status); |
2ca993e8 | 456 | UBool result = _inProgress(value, status); |
0f5d89e8 | 457 | removeHardRef(value); |
b331163b A |
458 | return result; |
459 | } | |
460 | ||
2ca993e8 | 461 | UBool UnifiedCache::_inProgress( |
0f5d89e8 A |
462 | const SharedObject* theValue, UErrorCode creationStatus) const { |
463 | return (theValue == fNoValue && creationStatus == U_ZERO_ERROR); | |
2ca993e8 A |
464 | } |
465 | ||
0f5d89e8 A |
466 | UBool UnifiedCache::_isEvictable(const UHashElement *element) const |
467 | { | |
2ca993e8 A |
468 | const CacheKeyBase *theKey = (const CacheKeyBase *) element->key.pointer; |
469 | const SharedObject *theValue = | |
470 | (const SharedObject *) element->value.pointer; | |
471 | ||
472 | // Entries that are under construction are never evictable | |
473 | if (_inProgress(theValue, theKey->fCreationStatus)) { | |
474 | return FALSE; | |
475 | } | |
476 | ||
477 | // We can evict entries that are either not a master or have just | |
478 | // one reference (The one reference being from the cache itself). | |
0f5d89e8 A |
479 | return (!theKey->fIsMaster || (theValue->softRefCount == 1 && theValue->noHardReferences())); |
480 | } | |
481 | ||
482 | void UnifiedCache::removeSoftRef(const SharedObject *value) const { | |
483 | U_ASSERT(value->cachePtr == this); | |
484 | U_ASSERT(value->softRefCount > 0); | |
485 | if (--value->softRefCount == 0) { | |
486 | --fNumValuesTotal; | |
487 | if (value->noHardReferences()) { | |
488 | delete value; | |
489 | } else { | |
490 | // This path only happens from flush(all). Which only happens from the | |
491 | // UnifiedCache destructor. Nulling out value.cacheptr changes the behavior | |
492 | // of value.removeRef(), causing the deletion to be done there. | |
493 | value->cachePtr = nullptr; | |
494 | } | |
495 | } | |
496 | } | |
497 | ||
498 | int32_t UnifiedCache::removeHardRef(const SharedObject *value) const { | |
499 | int refCount = 0; | |
500 | if (value) { | |
501 | refCount = umtx_atomic_dec(&value->hardRefCount); | |
502 | U_ASSERT(refCount >= 0); | |
503 | if (refCount == 0) { | |
504 | --fNumValuesInUse; | |
505 | } | |
506 | } | |
507 | return refCount; | |
508 | } | |
509 | ||
510 | int32_t UnifiedCache::addHardRef(const SharedObject *value) const { | |
511 | int refCount = 0; | |
512 | if (value) { | |
513 | refCount = umtx_atomic_inc(&value->hardRefCount); | |
514 | U_ASSERT(refCount >= 1); | |
515 | if (refCount == 1) { | |
516 | fNumValuesInUse++; | |
517 | } | |
518 | } | |
519 | return refCount; | |
2ca993e8 A |
520 | } |
521 | ||
b331163b | 522 | U_NAMESPACE_END |