Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // This is a low level implementation of atomic semantics for reference
      6 // counting.  Please use base/memory/ref_counted.h directly instead.
      7 //
      8 // The implementation includes annotations to avoid some false positives
      9 // when using data race detection tools.
     10 
     11 #ifndef BASE_ATOMIC_REF_COUNT_H_
     12 #define BASE_ATOMIC_REF_COUNT_H_
     13 #pragma once
     14 
     15 #include "base/atomicops.h"
     16 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
     17 
     18 namespace base {
     19 
     20 typedef subtle::Atomic32 AtomicRefCount;
     21 
     22 // Increment a reference count by "increment", which must exceed 0.
     23 inline void AtomicRefCountIncN(volatile AtomicRefCount *ptr,
     24                                AtomicRefCount increment) {
     25   subtle::NoBarrier_AtomicIncrement(ptr, increment);
     26 }
     27 
     28 // Decrement a reference count by "decrement", which must exceed 0,
     29 // and return whether the result is non-zero.
     30 // Insert barriers to ensure that state written before the reference count
     31 // became zero will be visible to a thread that has just made the count zero.
     32 inline bool AtomicRefCountDecN(volatile AtomicRefCount *ptr,
     33                                AtomicRefCount decrement) {
     34   ANNOTATE_HAPPENS_BEFORE(ptr);
     35   bool res = (subtle::Barrier_AtomicIncrement(ptr, -decrement) != 0);
     36   if (!res) {
     37     ANNOTATE_HAPPENS_AFTER(ptr);
     38   }
     39   return res;
     40 }
     41 
     42 // Increment a reference count by 1.
     43 inline void AtomicRefCountInc(volatile AtomicRefCount *ptr) {
     44   base::AtomicRefCountIncN(ptr, 1);
     45 }
     46 
     47 // Decrement a reference count by 1 and return whether the result is non-zero.
     48 // Insert barriers to ensure that state written before the reference count
     49 // became zero will be visible to a thread that has just made the count zero.
     50 inline bool AtomicRefCountDec(volatile AtomicRefCount *ptr) {
     51   return base::AtomicRefCountDecN(ptr, 1);
     52 }
     53 
     54 // Return whether the reference count is one.  If the reference count is used
     55 // in the conventional way, a refrerence count of 1 implies that the current
     56 // thread owns the reference and no other thread shares it.  This call performs
     57 // the test for a reference count of one, and performs the memory barrier
     58 // needed for the owning thread to act on the object, knowing that it has
     59 // exclusive access to the object.
     60 inline bool AtomicRefCountIsOne(volatile AtomicRefCount *ptr) {
     61   bool res = (subtle::Acquire_Load(ptr) == 1);
     62   if (res) {
     63     ANNOTATE_HAPPENS_AFTER(ptr);
     64   }
     65   return res;
     66 }
     67 
     68 // Return whether the reference count is zero.  With conventional object
     69 // referencing counting, the object will be destroyed, so the reference count
     70 // should never be zero.  Hence this is generally used for a debug check.
     71 inline bool AtomicRefCountIsZero(volatile AtomicRefCount *ptr) {
     72   bool res = (subtle::Acquire_Load(ptr) == 0);
     73   if (res) {
     74     ANNOTATE_HAPPENS_AFTER(ptr);
     75   }
     76   return res;
     77 }
     78 
     79 }  // namespace base
     80 
     81 #endif  // BASE_ATOMIC_REF_COUNT_H_
     82