]>
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" | |
0f5d89e8 | 11 | #include "mutex.h" |
2ca993e8 | 12 | #include "uassert.h" |
0f5d89e8 A |
13 | #include "umutex.h" |
14 | #include "unifiedcache.h" | |
57a6839d A |
15 | |
16 | U_NAMESPACE_BEGIN | |
2ca993e8 | 17 | |
57a6839d A |
18 | SharedObject::~SharedObject() {} |
19 | ||
2ca993e8 A |
20 | UnifiedCacheBase::~UnifiedCacheBase() {} |
21 | ||
57a6839d | 22 | void |
0f5d89e8 A |
23 | SharedObject::addRef() const { |
24 | umtx_atomic_inc(&hardRefCount); | |
57a6839d A |
25 | } |
26 | ||
0f5d89e8 A |
27 | // removeRef Decrement the reference count and delete if it is zero. |
28 | // Note that SharedObjects with a non-null cachePtr are owned by the | |
29 | // unified cache, and the cache will be responsible for the actual deletion. | |
30 | // The deletion could be as soon as immediately following the | |
31 | // update to the reference count, if another thread is running | |
32 | // a cache eviction cycle concurrently. | |
33 | // NO ACCESS TO *this PERMITTED AFTER REFERENCE COUNT == 0 for cached objects. | |
34 | // THE OBJECT MAY ALREADY BE GONE. | |
57a6839d | 35 | void |
0f5d89e8 A |
36 | SharedObject::removeRef() const { |
37 | const UnifiedCacheBase *cache = this->cachePtr; | |
38 | int32_t updatedRefCount = umtx_atomic_dec(&hardRefCount); | |
39 | U_ASSERT(updatedRefCount >= 0); | |
40 | if (updatedRefCount == 0) { | |
41 | if (cache) { | |
42 | cache->handleUnreferencedObject(); | |
2ca993e8 | 43 | } else { |
0f5d89e8 | 44 | delete this; |
2ca993e8 A |
45 | } |
46 | } | |
57a6839d A |
47 | } |
48 | ||
b331163b | 49 | |
57a6839d A |
50 | int32_t |
51 | SharedObject::getRefCount() const { | |
2ca993e8 | 52 | return umtx_loadAcquire(hardRefCount); |
57a6839d A |
53 | } |
54 | ||
55 | void | |
56 | SharedObject::deleteIfZeroRefCount() const { | |
0f5d89e8 | 57 | if (this->cachePtr == nullptr && getRefCount() == 0) { |
57a6839d A |
58 | delete this; |
59 | } | |
60 | } | |
61 | ||
62 | U_NAMESPACE_END |