xref: /aosp_15_r20/external/webrtc/rtc_base/ref_counter.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright 2017 The WebRTC Project Authors. All rights reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker #ifndef RTC_BASE_REF_COUNTER_H_
11*d9f75844SAndroid Build Coastguard Worker #define RTC_BASE_REF_COUNTER_H_
12*d9f75844SAndroid Build Coastguard Worker 
13*d9f75844SAndroid Build Coastguard Worker #include <atomic>
14*d9f75844SAndroid Build Coastguard Worker 
15*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/ref_count.h"
16*d9f75844SAndroid Build Coastguard Worker 
17*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
18*d9f75844SAndroid Build Coastguard Worker namespace webrtc_impl {
19*d9f75844SAndroid Build Coastguard Worker 
20*d9f75844SAndroid Build Coastguard Worker class RefCounter {
21*d9f75844SAndroid Build Coastguard Worker  public:
RefCounter(int ref_count)22*d9f75844SAndroid Build Coastguard Worker   explicit RefCounter(int ref_count) : ref_count_(ref_count) {}
23*d9f75844SAndroid Build Coastguard Worker   RefCounter() = delete;
24*d9f75844SAndroid Build Coastguard Worker 
IncRef()25*d9f75844SAndroid Build Coastguard Worker   void IncRef() {
26*d9f75844SAndroid Build Coastguard Worker     // Relaxed memory order: The current thread is allowed to act on the
27*d9f75844SAndroid Build Coastguard Worker     // resource protected by the reference counter both before and after the
28*d9f75844SAndroid Build Coastguard Worker     // atomic op, so this function doesn't prevent memory access reordering.
29*d9f75844SAndroid Build Coastguard Worker     ref_count_.fetch_add(1, std::memory_order_relaxed);
30*d9f75844SAndroid Build Coastguard Worker   }
31*d9f75844SAndroid Build Coastguard Worker 
32*d9f75844SAndroid Build Coastguard Worker   // Returns kDroppedLastRef if this call dropped the last reference; the caller
33*d9f75844SAndroid Build Coastguard Worker   // should therefore free the resource protected by the reference counter.
34*d9f75844SAndroid Build Coastguard Worker   // Otherwise, returns kOtherRefsRemained (note that in case of multithreading,
35*d9f75844SAndroid Build Coastguard Worker   // some other caller may have dropped the last reference by the time this call
36*d9f75844SAndroid Build Coastguard Worker   // returns; all we know is that we didn't do it).
DecRef()37*d9f75844SAndroid Build Coastguard Worker   rtc::RefCountReleaseStatus DecRef() {
38*d9f75844SAndroid Build Coastguard Worker     // Use release-acquire barrier to ensure all actions on the protected
39*d9f75844SAndroid Build Coastguard Worker     // resource are finished before the resource can be freed.
40*d9f75844SAndroid Build Coastguard Worker     // When ref_count_after_subtract > 0, this function require
41*d9f75844SAndroid Build Coastguard Worker     // std::memory_order_release part of the barrier.
42*d9f75844SAndroid Build Coastguard Worker     // When ref_count_after_subtract == 0, this function require
43*d9f75844SAndroid Build Coastguard Worker     // std::memory_order_acquire part of the barrier.
44*d9f75844SAndroid Build Coastguard Worker     // In addition std::memory_order_release is used for synchronization with
45*d9f75844SAndroid Build Coastguard Worker     // the HasOneRef function to make sure all actions on the protected resource
46*d9f75844SAndroid Build Coastguard Worker     // are finished before the resource is assumed to have exclusive access.
47*d9f75844SAndroid Build Coastguard Worker     int ref_count_after_subtract =
48*d9f75844SAndroid Build Coastguard Worker         ref_count_.fetch_sub(1, std::memory_order_acq_rel) - 1;
49*d9f75844SAndroid Build Coastguard Worker     return ref_count_after_subtract == 0
50*d9f75844SAndroid Build Coastguard Worker                ? rtc::RefCountReleaseStatus::kDroppedLastRef
51*d9f75844SAndroid Build Coastguard Worker                : rtc::RefCountReleaseStatus::kOtherRefsRemained;
52*d9f75844SAndroid Build Coastguard Worker   }
53*d9f75844SAndroid Build Coastguard Worker 
54*d9f75844SAndroid Build Coastguard Worker   // Return whether the reference count is one. If the reference count is used
55*d9f75844SAndroid Build Coastguard Worker   // in the conventional way, a reference count of 1 implies that the current
56*d9f75844SAndroid Build Coastguard Worker   // thread owns the reference and no other thread shares it. This call performs
57*d9f75844SAndroid Build Coastguard Worker   // the test for a reference count of one, and performs the memory barrier
58*d9f75844SAndroid Build Coastguard Worker   // needed for the owning thread to act on the resource protected by the
59*d9f75844SAndroid Build Coastguard Worker   // reference counter, knowing that it has exclusive access.
HasOneRef()60*d9f75844SAndroid Build Coastguard Worker   bool HasOneRef() const {
61*d9f75844SAndroid Build Coastguard Worker     // To ensure resource protected by the reference counter has exclusive
62*d9f75844SAndroid Build Coastguard Worker     // access, all changes to the resource before it was released by other
63*d9f75844SAndroid Build Coastguard Worker     // threads must be visible by current thread. That is provided by release
64*d9f75844SAndroid Build Coastguard Worker     // (in DecRef) and acquire (in this function) ordering.
65*d9f75844SAndroid Build Coastguard Worker     return ref_count_.load(std::memory_order_acquire) == 1;
66*d9f75844SAndroid Build Coastguard Worker   }
67*d9f75844SAndroid Build Coastguard Worker 
68*d9f75844SAndroid Build Coastguard Worker  private:
69*d9f75844SAndroid Build Coastguard Worker   std::atomic<int> ref_count_;
70*d9f75844SAndroid Build Coastguard Worker };
71*d9f75844SAndroid Build Coastguard Worker 
72*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc_impl
73*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
74*d9f75844SAndroid Build Coastguard Worker 
75*d9f75844SAndroid Build Coastguard Worker #endif  // RTC_BASE_REF_COUNTER_H_
76