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