Home | History | Annotate | Download | only in common
      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
     11 SharedObject::~SharedObject() {}
     12 
     13 void
     14 SharedObject::addRef() const {
     15     umtx_atomic_inc(&totalRefCount);
     16 }
     17 
     18 void
     19 SharedObject::removeRef() const {
     20     if(umtx_atomic_dec(&totalRefCount) == 0) {
     21         delete this;
     22     }
     23 }
     24 
     25 void
     26 SharedObject::addSoftRef() const {
     27     addRef();
     28     umtx_atomic_inc(&softRefCount);
     29 }
     30 
     31 void
     32 SharedObject::removeSoftRef() const {
     33     umtx_atomic_dec(&softRefCount);
     34     removeRef();
     35 }
     36 
     37 UBool
     38 SharedObject::allSoftReferences() const {
     39     return umtx_loadAcquire(totalRefCount) == umtx_loadAcquire(softRefCount);
     40 }
     41 
     42 int32_t
     43 SharedObject::getRefCount() const {
     44     return umtx_loadAcquire(totalRefCount);
     45 }
     46 
     47 int32_t
     48 SharedObject::getSoftRefCount() const {
     49     return umtx_loadAcquire(softRefCount);
     50 }
     51 
     52 void
     53 SharedObject::deleteIfZeroRefCount() const {
     54     if(getRefCount() == 0) {
     55         delete this;
     56     }
     57 }
     58 
     59 U_NAMESPACE_END
     60