xref: /aosp_15_r20/external/cronet/base/atomic_ref_count.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2011 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker // This is a low level implementation of atomic semantics for reference
6*6777b538SAndroid Build Coastguard Worker // counting.  Please use base/memory/ref_counted.h directly instead.
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #ifndef BASE_ATOMIC_REF_COUNT_H_
9*6777b538SAndroid Build Coastguard Worker #define BASE_ATOMIC_REF_COUNT_H_
10*6777b538SAndroid Build Coastguard Worker 
11*6777b538SAndroid Build Coastguard Worker #include <atomic>
12*6777b538SAndroid Build Coastguard Worker 
13*6777b538SAndroid Build Coastguard Worker namespace base {
14*6777b538SAndroid Build Coastguard Worker 
15*6777b538SAndroid Build Coastguard Worker namespace subtle {
16*6777b538SAndroid Build Coastguard Worker class RefCountedOverflowTest;
17*6777b538SAndroid Build Coastguard Worker }  // namespace subtle
18*6777b538SAndroid Build Coastguard Worker 
19*6777b538SAndroid Build Coastguard Worker class AtomicRefCount {
20*6777b538SAndroid Build Coastguard Worker  public:
AtomicRefCount()21*6777b538SAndroid Build Coastguard Worker   constexpr AtomicRefCount() : ref_count_(0) {}
AtomicRefCount(int initial_value)22*6777b538SAndroid Build Coastguard Worker   explicit constexpr AtomicRefCount(int initial_value)
23*6777b538SAndroid Build Coastguard Worker       : ref_count_(initial_value) {}
24*6777b538SAndroid Build Coastguard Worker 
25*6777b538SAndroid Build Coastguard Worker   // Increment a reference count.
26*6777b538SAndroid Build Coastguard Worker   // Returns the previous value of the count.
Increment()27*6777b538SAndroid Build Coastguard Worker   int Increment() { return Increment(1); }
28*6777b538SAndroid Build Coastguard Worker 
29*6777b538SAndroid Build Coastguard Worker   // Increment a reference count by "increment", which must exceed 0.
30*6777b538SAndroid Build Coastguard Worker   // Returns the previous value of the count.
Increment(int increment)31*6777b538SAndroid Build Coastguard Worker   int Increment(int increment) {
32*6777b538SAndroid Build Coastguard Worker     return ref_count_.fetch_add(increment, std::memory_order_relaxed);
33*6777b538SAndroid Build Coastguard Worker   }
34*6777b538SAndroid Build Coastguard Worker 
35*6777b538SAndroid Build Coastguard Worker   // Decrement a reference count, and return whether the result is non-zero.
36*6777b538SAndroid Build Coastguard Worker   // Insert barriers to ensure that state written before the reference count
37*6777b538SAndroid Build Coastguard Worker   // became zero will be visible to a thread that has just made the count zero.
Decrement()38*6777b538SAndroid Build Coastguard Worker   bool Decrement() {
39*6777b538SAndroid Build Coastguard Worker     // TODO(jbroman): Technically this doesn't need to be an acquire operation
40*6777b538SAndroid Build Coastguard Worker     // unless the result is 1 (i.e., the ref count did indeed reach zero).
41*6777b538SAndroid Build Coastguard Worker     // However, there are toolchain issues that make that not work as well at
42*6777b538SAndroid Build Coastguard Worker     // present (notably TSAN doesn't like it).
43*6777b538SAndroid Build Coastguard Worker     return ref_count_.fetch_sub(1, std::memory_order_acq_rel) != 1;
44*6777b538SAndroid Build Coastguard Worker   }
45*6777b538SAndroid Build Coastguard Worker 
46*6777b538SAndroid Build Coastguard Worker   // Return whether the reference count is one.  If the reference count is used
47*6777b538SAndroid Build Coastguard Worker   // in the conventional way, a refrerence count of 1 implies that the current
48*6777b538SAndroid Build Coastguard Worker   // thread owns the reference and no other thread shares it.  This call
49*6777b538SAndroid Build Coastguard Worker   // performs the test for a reference count of one, and performs the memory
50*6777b538SAndroid Build Coastguard Worker   // barrier needed for the owning thread to act on the object, knowing that it
51*6777b538SAndroid Build Coastguard Worker   // has exclusive access to the object.
IsOne()52*6777b538SAndroid Build Coastguard Worker   bool IsOne() const { return ref_count_.load(std::memory_order_acquire) == 1; }
53*6777b538SAndroid Build Coastguard Worker 
54*6777b538SAndroid Build Coastguard Worker   // Return whether the reference count is zero.  With conventional object
55*6777b538SAndroid Build Coastguard Worker   // referencing counting, the object will be destroyed, so the reference count
56*6777b538SAndroid Build Coastguard Worker   // should never be zero.  Hence this is generally used for a debug check.
IsZero()57*6777b538SAndroid Build Coastguard Worker   bool IsZero() const {
58*6777b538SAndroid Build Coastguard Worker     return ref_count_.load(std::memory_order_acquire) == 0;
59*6777b538SAndroid Build Coastguard Worker   }
60*6777b538SAndroid Build Coastguard Worker 
61*6777b538SAndroid Build Coastguard Worker   // Returns the current reference count (with no barriers). This is subtle, and
62*6777b538SAndroid Build Coastguard Worker   // should be used only for debugging.
SubtleRefCountForDebug()63*6777b538SAndroid Build Coastguard Worker   int SubtleRefCountForDebug() const {
64*6777b538SAndroid Build Coastguard Worker     return ref_count_.load(std::memory_order_relaxed);
65*6777b538SAndroid Build Coastguard Worker   }
66*6777b538SAndroid Build Coastguard Worker 
67*6777b538SAndroid Build Coastguard Worker  private:
68*6777b538SAndroid Build Coastguard Worker   friend subtle::RefCountedOverflowTest;
69*6777b538SAndroid Build Coastguard Worker 
70*6777b538SAndroid Build Coastguard Worker   std::atomic_int ref_count_;
71*6777b538SAndroid Build Coastguard Worker };
72*6777b538SAndroid Build Coastguard Worker 
73*6777b538SAndroid Build Coastguard Worker }  // namespace base
74*6777b538SAndroid Build Coastguard Worker 
75*6777b538SAndroid Build Coastguard Worker #endif  // BASE_ATOMIC_REF_COUNT_H_
76