atomic_ref_count.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // This is a low level implementation of atomic semantics for reference
  5. // counting. Please use base/memory/ref_counted.h directly instead.
  6. #ifndef BASE_ATOMIC_REF_COUNT_H_
  7. #define BASE_ATOMIC_REF_COUNT_H_
  8. #include <atomic>
  9. namespace base {
  10. class AtomicRefCount {
  11. public:
  12. constexpr AtomicRefCount() : ref_count_(0) {}
  13. explicit constexpr AtomicRefCount(int initial_value)
  14. : ref_count_(initial_value) {}
  15. // Increment a reference count.
  16. // Returns the previous value of the count.
  17. int Increment() { return Increment(1); }
  18. // Increment a reference count by "increment", which must exceed 0.
  19. // Returns the previous value of the count.
  20. int Increment(int increment) {
  21. return ref_count_.fetch_add(increment, std::memory_order_relaxed);
  22. }
  23. // Decrement a reference count, and return whether the result is non-zero.
  24. // Insert barriers to ensure that state written before the reference count
  25. // became zero will be visible to a thread that has just made the count zero.
  26. bool Decrement() {
  27. // TODO(jbroman): Technically this doesn't need to be an acquire operation
  28. // unless the result is 1 (i.e., the ref count did indeed reach zero).
  29. // However, there are toolchain issues that make that not work as well at
  30. // present (notably TSAN doesn't like it).
  31. return ref_count_.fetch_sub(1, std::memory_order_acq_rel) != 1;
  32. }
  33. // Return whether the reference count is one. If the reference count is used
  34. // in the conventional way, a refrerence count of 1 implies that the current
  35. // thread owns the reference and no other thread shares it. This call
  36. // performs the test for a reference count of one, and performs the memory
  37. // barrier needed for the owning thread to act on the object, knowing that it
  38. // has exclusive access to the object.
  39. bool IsOne() const { return ref_count_.load(std::memory_order_acquire) == 1; }
  40. // Return whether the reference count is zero. With conventional object
  41. // referencing counting, the object will be destroyed, so the reference count
  42. // should never be zero. Hence this is generally used for a debug check.
  43. bool IsZero() const {
  44. return ref_count_.load(std::memory_order_acquire) == 0;
  45. }
  46. // Returns the current reference count (with no barriers). This is subtle, and
  47. // should be used only for debugging.
  48. int SubtleRefCountForDebug() const {
  49. return ref_count_.load(std::memory_order_relaxed);
  50. }
  51. private:
  52. std::atomic_int ref_count_;
  53. };
  54. } // namespace base
  55. #endif // BASE_ATOMIC_REF_COUNT_H_