123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- #ifndef BASE_ATOMIC_REF_COUNT_H_
- #define BASE_ATOMIC_REF_COUNT_H_
- #include <atomic>
- namespace base {
- class AtomicRefCount {
- public:
- constexpr AtomicRefCount() : ref_count_(0) {}
- explicit constexpr AtomicRefCount(int initial_value)
- : ref_count_(initial_value) {}
-
-
- int Increment() { return Increment(1); }
-
-
- int Increment(int increment) {
- return ref_count_.fetch_add(increment, std::memory_order_relaxed);
- }
-
-
-
- bool Decrement() {
-
-
-
-
- return ref_count_.fetch_sub(1, std::memory_order_acq_rel) != 1;
- }
-
-
-
-
-
-
- bool IsOne() const { return ref_count_.load(std::memory_order_acquire) == 1; }
-
-
-
- bool IsZero() const {
- return ref_count_.load(std::memory_order_acquire) == 0;
- }
-
-
- int SubtleRefCountForDebug() const {
- return ref_count_.load(std::memory_order_relaxed);
- }
- private:
- std::atomic_int ref_count_;
- };
- }
- #endif
|