blob: 125683f988aa7ebca3f6595774ed8fd18deba1df [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2011 The Chromium Authors
[email protected]b2e97292008-09-02 18:20:342// 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
[email protected]3b63f8f42011-03-28 01:54:156// counting. Please use base/memory/ref_counted.h directly instead.
[email protected]b2e97292008-09-02 18:20:347
8#ifndef BASE_ATOMIC_REF_COUNT_H_
9#define BASE_ATOMIC_REF_COUNT_H_
10
Jeremy Roman031d45a2017-06-28 18:22:4711#include <atomic>
[email protected]b2e97292008-09-02 18:20:3412
13namespace base {
14
Daniel Cheng5c8cdc92022-11-15 08:14:1115namespace subtle {
16class RefCountedOverflowTest;
17} // namespace subtle
18
Jeremy Roman031d45a2017-06-28 18:22:4719class AtomicRefCount {
20 public:
21 constexpr AtomicRefCount() : ref_count_(0) {}
22 explicit constexpr AtomicRefCount(int initial_value)
23 : ref_count_(initial_value) {}
24
25 // Increment a reference count.
tzik8da8a642018-08-14 05:17:5026 // Returns the previous value of the count.
27 int Increment() { return Increment(1); }
Jeremy Roman031d45a2017-06-28 18:22:4728
29 // Increment a reference count by "increment", which must exceed 0.
tzik8da8a642018-08-14 05:17:5030 // Returns the previous value of the count.
31 int Increment(int increment) {
32 return ref_count_.fetch_add(increment, std::memory_order_relaxed);
Jeremy Roman031d45a2017-06-28 18:22:4733 }
34
35 // Decrement a reference count, and return whether the result is non-zero.
36 // Insert barriers to ensure that state written before the reference count
37 // became zero will be visible to a thread that has just made the count zero.
38 bool Decrement() {
39 // TODO(jbroman): Technically this doesn't need to be an acquire operation
40 // unless the result is 1 (i.e., the ref count did indeed reach zero).
41 // However, there are toolchain issues that make that not work as well at
42 // present (notably TSAN doesn't like it).
43 return ref_count_.fetch_sub(1, std::memory_order_acq_rel) != 1;
44 }
45
46 // Return whether the reference count is one. If the reference count is used
47 // in the conventional way, a refrerence count of 1 implies that the current
48 // thread owns the reference and no other thread shares it. This call
49 // performs the test for a reference count of one, and performs the memory
50 // barrier needed for the owning thread to act on the object, knowing that it
51 // has exclusive access to the object.
52 bool IsOne() const { return ref_count_.load(std::memory_order_acquire) == 1; }
53
54 // Return whether the reference count is zero. With conventional object
55 // referencing counting, the object will be destroyed, so the reference count
56 // should never be zero. Hence this is generally used for a debug check.
57 bool IsZero() const {
58 return ref_count_.load(std::memory_order_acquire) == 0;
59 }
60
61 // Returns the current reference count (with no barriers). This is subtle, and
62 // should be used only for debugging.
63 int SubtleRefCountForDebug() const {
64 return ref_count_.load(std::memory_order_relaxed);
65 }
66
67 private:
Daniel Cheng5c8cdc92022-11-15 08:14:1168 friend subtle::RefCountedOverflowTest;
69
Jeremy Roman031d45a2017-06-28 18:22:4770 std::atomic_int ref_count_;
71};
72
[email protected]b2e97292008-09-02 18:20:3473} // namespace base
74
75#endif // BASE_ATOMIC_REF_COUNT_H_