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
16U_NAMESPACE_BEGIN
17
18SharedObject::~SharedObject() {}
19
20UnifiedCacheBase::~UnifiedCacheBase() {}
21
22void
23SharedObject::addRef() const {
24 umtx_atomic_inc(&hardRefCount);
25}
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.
35void
36SharedObject::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();
43 } else {
44 delete this;
45 }
46 }
47}
48
49
50int32_t
51SharedObject::getRefCount() const {
52 return umtx_loadAcquire(hardRefCount);
53}
54
55void
56SharedObject::deleteIfZeroRefCount() const {
57 if (this->cachePtr == nullptr && getRefCount() == 0) {
58 delete this;
59 }
60}
61
62U_NAMESPACE_END
63