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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 13 | #include "rtc_base/atomic_ops.h" |
| 14 | #include "rtc_base/ref_count.h" |
Niels Möller | 9155e49 | 2017-10-23 09:22:30 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | namespace webrtc_impl { |
| 18 | |
| 19 | class RefCounter { |
| 20 | public: |
| 21 | explicit RefCounter(int ref_count) : ref_count_(ref_count) {} |
| 22 | RefCounter() = delete; |
| 23 | |
| 24 | void IncRef() { rtc::AtomicOps::Increment(&ref_count_); } |
| 25 | |
Karl Wiberg | a8c7326 | 2019-01-14 12:18:31 | [diff] [blame] | 26 | // Returns kDroppedLastRef if this call dropped the last reference; the caller |
| 27 | // should therefore free the resource protected by the reference counter. |
| 28 | // Otherwise, returns kOtherRefsRemained (note that in case of multithreading, |
| 29 | // some other caller may have dropped the last reference by the time this call |
| 30 | // returns; all we know is that we didn't do it). |
Niels Möller | 9155e49 | 2017-10-23 09:22:30 | [diff] [blame] | 31 | rtc::RefCountReleaseStatus DecRef() { |
| 32 | return (rtc::AtomicOps::Decrement(&ref_count_) == 0) |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 33 | ? rtc::RefCountReleaseStatus::kDroppedLastRef |
| 34 | : rtc::RefCountReleaseStatus::kOtherRefsRemained; |
Niels Möller | 9155e49 | 2017-10-23 09:22:30 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // Return whether the reference count is one. If the reference count is used |
| 38 | // in the conventional way, a reference count of 1 implies that the current |
| 39 | // thread owns the reference and no other thread shares it. This call performs |
| 40 | // the test for a reference count of one, and performs the memory barrier |
| 41 | // needed for the owning thread to act on the resource protected by the |
| 42 | // reference counter, knowing that it has exclusive access. |
| 43 | bool HasOneRef() const { |
| 44 | return rtc::AtomicOps::AcquireLoad(&ref_count_) == 1; |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | volatile int ref_count_; |
| 49 | }; |
| 50 | |
| 51 | } // namespace webrtc_impl |
| 52 | } // namespace webrtc |
| 53 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 54 | #endif // RTC_BASE_REF_COUNTER_H_ |