You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
2.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. */
  10. #ifndef RTC_BASE_REF_COUNTER_H_
  11. #define RTC_BASE_REF_COUNTER_H_
  12. #include "rtc_base/atomic_ops.h"
  13. #include "rtc_base/ref_count.h"
  14. namespace webrtc {
  15. namespace webrtc_impl {
  16. class RefCounter {
  17. public:
  18. explicit RefCounter(int ref_count) : ref_count_(ref_count) {}
  19. RefCounter() = delete;
  20. void IncRef() { rtc::AtomicOps::Increment(&ref_count_); }
  21. // Returns kDroppedLastRef if this call dropped the last reference; the caller
  22. // should therefore free the resource protected by the reference counter.
  23. // Otherwise, returns kOtherRefsRemained (note that in case of multithreading,
  24. // some other caller may have dropped the last reference by the time this call
  25. // returns; all we know is that we didn't do it).
  26. rtc::RefCountReleaseStatus DecRef() {
  27. return (rtc::AtomicOps::Decrement(&ref_count_) == 0)
  28. ? rtc::RefCountReleaseStatus::kDroppedLastRef
  29. : rtc::RefCountReleaseStatus::kOtherRefsRemained;
  30. }
  31. // Return whether the reference count is one. If the reference count is used
  32. // in the conventional way, a reference count of 1 implies that the current
  33. // thread owns the reference and no other thread shares it. This call performs
  34. // the test for a reference count of one, and performs the memory barrier
  35. // needed for the owning thread to act on the resource protected by the
  36. // reference counter, knowing that it has exclusive access.
  37. bool HasOneRef() const {
  38. return rtc::AtomicOps::AcquireLoad(&ref_count_) == 1;
  39. }
  40. private:
  41. volatile int ref_count_;
  42. };
  43. } // namespace webrtc_impl
  44. } // namespace webrtc
  45. #endif // RTC_BASE_REF_COUNTER_H_