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