ref_counter.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 <atomic>
  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() {
  21. // Relaxed memory order: The current thread is allowed to act on the
  22. // resource protected by the reference counter both before and after the
  23. // atomic op, so this function doesn't prevent memory access reordering.
  24. ref_count_.fetch_add(1, std::memory_order_relaxed);
  25. }
  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).
  31. rtc::RefCountReleaseStatus DecRef() {
  32. // Use release-acquire barrier to ensure all actions on the protected
  33. // resource are finished before the resource can be freed.
  34. // When ref_count_after_subtract > 0, this function require
  35. // std::memory_order_release part of the barrier.
  36. // When ref_count_after_subtract == 0, this function require
  37. // std::memory_order_acquire part of the barrier.
  38. // In addition std::memory_order_release is used for synchronization with
  39. // the HasOneRef function to make sure all actions on the protected resource
  40. // are finished before the resource is assumed to have exclusive access.
  41. int ref_count_after_subtract =
  42. ref_count_.fetch_sub(1, std::memory_order_acq_rel) - 1;
  43. return ref_count_after_subtract == 0
  44. ? rtc::RefCountReleaseStatus::kDroppedLastRef
  45. : rtc::RefCountReleaseStatus::kOtherRefsRemained;
  46. }
  47. // Return whether the reference count is one. If the reference count is used
  48. // in the conventional way, a reference count of 1 implies that the current
  49. // thread owns the reference and no other thread shares it. This call performs
  50. // the test for a reference count of one, and performs the memory barrier
  51. // needed for the owning thread to act on the resource protected by the
  52. // reference counter, knowing that it has exclusive access.
  53. bool HasOneRef() const {
  54. // To ensure resource protected by the reference counter has exclusive
  55. // access, all changes to the resource before it was released by other
  56. // threads must be visible by current thread. That is provided by release
  57. // (in DecRef) and acquire (in this function) ordering.
  58. return ref_count_.load(std::memory_order_acquire) == 1;
  59. }
  60. private:
  61. std::atomic<int> ref_count_;
  62. };
  63. } // namespace webrtc_impl
  64. } // namespace webrtc
  65. #endif // RTC_BASE_REF_COUNTER_H_