• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2014 Marshall A. Greenblatt. Portions copyright (c) 2011
2 // Google Inc. All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //    * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //    * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //    * Neither the name of Google Inc. nor the name Chromium Embedded
15 // Framework nor the names of its contributors may be used to endorse
16 // or promote products derived from this software without specific prior
17 // written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // This is a low level implementation of atomic semantics for reference
32 // counting.  Please use cef_ref_counted.h directly instead.
33 //
34 // The Chromium implementation includes annotations to avoid some false
35 // positives when using data race detection tools. Annotations are not
36 // currently supported by the CEF implementation.
37 
38 #ifndef CEF_INCLUDE_BASE_CEF_ATOMIC_REF_COUNT_H_
39 #define CEF_INCLUDE_BASE_CEF_ATOMIC_REF_COUNT_H_
40 #pragma once
41 
42 #if defined(USING_CHROMIUM_INCLUDES)
43 // When building CEF include the Chromium header directly.
44 #include "base/atomic_ref_count.h"
45 
46 #else  // !USING_CHROMIUM_INCLUDES
47 // The following is substantially similar to the Chromium implementation.
48 // If the Chromium implementation diverges the below implementation should be
49 // updated to match.
50 
51 #include <atomic>
52 
53 namespace base {
54 
55 class AtomicRefCount {
56  public:
AtomicRefCount()57   constexpr AtomicRefCount() : ref_count_(0) {}
AtomicRefCount(int initial_value)58   explicit constexpr AtomicRefCount(int initial_value)
59       : ref_count_(initial_value) {}
60 
61   // Increment a reference count.
62   // Returns the previous value of the count.
Increment()63   int Increment() { return Increment(1); }
64 
65   // Increment a reference count by "increment", which must exceed 0.
66   // Returns the previous value of the count.
Increment(int increment)67   int Increment(int increment) {
68     return ref_count_.fetch_add(increment, std::memory_order_relaxed);
69   }
70 
71   // Decrement a reference count, and return whether the result is non-zero.
72   // Insert barriers to ensure that state written before the reference count
73   // became zero will be visible to a thread that has just made the count zero.
Decrement()74   bool Decrement() {
75     // TODO(jbroman): Technically this doesn't need to be an acquire operation
76     // unless the result is 1 (i.e., the ref count did indeed reach zero).
77     // However, there are toolchain issues that make that not work as well at
78     // present (notably TSAN doesn't like it).
79     return ref_count_.fetch_sub(1, std::memory_order_acq_rel) != 1;
80   }
81 
82   // Return whether the reference count is one.  If the reference count is used
83   // in the conventional way, a refrerence count of 1 implies that the current
84   // thread owns the reference and no other thread shares it.  This call
85   // performs the test for a reference count of one, and performs the memory
86   // barrier needed for the owning thread to act on the object, knowing that it
87   // has exclusive access to the object.
IsOne()88   bool IsOne() const { return ref_count_.load(std::memory_order_acquire) == 1; }
89 
90   // Return whether the reference count is zero.  With conventional object
91   // referencing counting, the object will be destroyed, so the reference count
92   // should never be zero.  Hence this is generally used for a debug check.
IsZero()93   bool IsZero() const {
94     return ref_count_.load(std::memory_order_acquire) == 0;
95   }
96 
97   // Returns the current reference count (with no barriers). This is subtle, and
98   // should be used only for debugging.
SubtleRefCountForDebug()99   int SubtleRefCountForDebug() const {
100     return ref_count_.load(std::memory_order_relaxed);
101   }
102 
103  private:
104   std::atomic_int ref_count_;
105 };
106 
107 }  // namespace base
108 
109 #endif  // !USING_CHROMIUM_INCLUDES
110 
111 #endif  // CEF_INCLUDE_BASE_CEF_ATOMIC_REF_COUNT_H_
112