1 /* 2 ****************************************************************************** 3 * Copyright (C) 2014, International Business Machines 4 * Corporation and others. All Rights Reserved. 5 ****************************************************************************** 6 * sharedobject.cpp 7 */ 8 #include "sharedobject.h" 9 10 U_NAMESPACE_BEGIN ~SharedObject()11SharedObject::~SharedObject() {} 12 13 void addRef() const14SharedObject::addRef() const { 15 umtx_atomic_inc(&totalRefCount); 16 } 17 18 void removeRef() const19SharedObject::removeRef() const { 20 if(umtx_atomic_dec(&totalRefCount) == 0) { 21 delete this; 22 } 23 } 24 25 void addSoftRef() const26SharedObject::addSoftRef() const { 27 addRef(); 28 umtx_atomic_inc(&softRefCount); 29 } 30 31 void removeSoftRef() const32SharedObject::removeSoftRef() const { 33 umtx_atomic_dec(&softRefCount); 34 removeRef(); 35 } 36 37 UBool allSoftReferences() const38SharedObject::allSoftReferences() const { 39 return umtx_loadAcquire(totalRefCount) == umtx_loadAcquire(softRefCount); 40 } 41 42 int32_t getRefCount() const43SharedObject::getRefCount() const { 44 return umtx_loadAcquire(totalRefCount); 45 } 46 47 int32_t getSoftRefCount() const48SharedObject::getSoftRefCount() const { 49 return umtx_loadAcquire(softRefCount); 50 } 51 52 void deleteIfZeroRefCount() const53SharedObject::deleteIfZeroRefCount() const { 54 if(getRefCount() == 0) { 55 delete this; 56 } 57 } 58 59 U_NAMESPACE_END 60