| 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 | */ |
| 10 | #ifndef RTC_BASE_REFCOUNTER_H_ |
| 11 | #define RTC_BASE_REFCOUNTER_H_ |
| 12 |
| 13 | #include "rtc_base/atomicops.h" |
| 14 | #include "rtc_base/refcount.h" |
| 15 |
| 16 | namespace webrtc |
| 17 | { |
| 18 | namespace webrtc_impl |
| 19 | { |
| 20 |
| 21 | class RefCounter |
| 22 | { |
| 23 | public: |
| 24 | explicit RefCounter(int ref_count) |
| 25 | : ref_count_(ref_count) |
| 26 | { |
| 27 | } |
| 28 | RefCounter() = delete; |
| 29 |
| 30 | void IncRef() { rtc::AtomicOps::Increment(&ref_count_); } |
| 31 |
| 32 | // TODO(nisse): Switch return type to RefCountReleaseStatus? |
| 33 | // Returns true if this was the last reference, and the resource protected by |
| 34 | // the reference counter can be deleted. |
| 35 | rtc::RefCountReleaseStatus DecRef() |
| 36 | { |
| 37 | return (rtc::AtomicOps::Decrement(&ref_count_) == 0) |
| 38 | ? rtc::RefCountReleaseStatus::kDroppedLastRef |
| 39 | : rtc::RefCountReleaseStatus::kOtherRefsRemained; |
| 40 | } |
| 41 |
| 42 | // Return whether the reference count is one. If the reference count is used |
| 43 | // in the conventional way, a reference count of 1 implies that the current |
| 44 | // thread owns the reference and no other thread shares it. This call performs |
| 45 | // the test for a reference count of one, and performs the memory barrier |
| 46 | // needed for the owning thread to act on the resource protected by the |
| 47 | // reference counter, knowing that it has exclusive access. |
| 48 | bool HasOneRef() const |
| 49 | { |
| 50 | return rtc::AtomicOps::AcquireLoad(&ref_count_) == 1; |
| 51 | } |
| 52 |
| 53 | private: |
| 54 | volatile int ref_count_; |
| 55 | }; |
| 56 |
| 57 | } // namespace webrtc_impl |
| 58 | } // namespace webrtc |
| 59 |
| 60 | #endif // RTC_BASE_REFCOUNTER_H_ |
| 61 |