/src/icu/source/common/sharedobject.cpp
Line | Count | Source (jump to first uncovered line) |
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 "mutex.h" |
12 | | #include "uassert.h" |
13 | | #include "umutex.h" |
14 | | #include "unifiedcache.h" |
15 | | |
16 | | U_NAMESPACE_BEGIN |
17 | | |
18 | 0 | SharedObject::~SharedObject() {} |
19 | | |
20 | 0 | UnifiedCacheBase::~UnifiedCacheBase() {} |
21 | | |
22 | | void |
23 | 0 | SharedObject::addRef() const { |
24 | 0 | umtx_atomic_inc(&hardRefCount); |
25 | 0 | } |
26 | | |
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. |
35 | | void |
36 | 0 | SharedObject::removeRef() const { |
37 | 0 | const UnifiedCacheBase *cache = this->cachePtr; |
38 | 0 | int32_t updatedRefCount = umtx_atomic_dec(&hardRefCount); |
39 | 0 | U_ASSERT(updatedRefCount >= 0); |
40 | 0 | if (updatedRefCount == 0) { |
41 | 0 | if (cache) { |
42 | 0 | cache->handleUnreferencedObject(); |
43 | 0 | } else { |
44 | 0 | delete this; |
45 | 0 | } |
46 | 0 | } |
47 | 0 | } |
48 | | |
49 | | |
50 | | int32_t |
51 | 0 | SharedObject::getRefCount() const { |
52 | 0 | return umtx_loadAcquire(hardRefCount); |
53 | 0 | } |
54 | | |
55 | | void |
56 | 0 | SharedObject::deleteIfZeroRefCount() const { |
57 | 0 | if (this->cachePtr == nullptr && getRefCount() == 0) { |
58 | 0 | delete this; |
59 | 0 | } |
60 | 0 | } |
61 | | |
62 | | U_NAMESPACE_END |