3 #ifndef __CDS_REFCOUNTER_H
4 #define __CDS_REFCOUNTER_H
7 Refernce counting primitives
12 #include <cds/cxx11_atomic.h>
16 /// Simple reference counter
18 Thread-safe reference counter build on atomic primitives.
19 \p T is one of integer types
24 CDS_ATOMIC::atomic<T> m_nRefCount ; ///< The reference counter
27 typedef T ref_counter_type ; ///< The reference counter type
30 /// Construct reference counter with specified value \p initValue
31 ref_counter( T initValue = 0 ) CDS_NOEXCEPT
32 : m_nRefCount(initValue)
35 /// Get current value of reference counter.
36 T value() const CDS_NOEXCEPT
38 return m_nRefCount.load( CDS_ATOMIC::memory_order_relaxed );
41 /// Current value of reference counter
42 operator T() const CDS_NOEXCEPT
48 void inc() CDS_NOEXCEPT
50 m_nRefCount.fetch_add( 1, CDS_ATOMIC::memory_order_relaxed );
53 /// Atomic decrement. Return \p true if reference counter is 0, otherwise \p false
54 bool dec() CDS_NOEXCEPT
56 if ( m_nRefCount.fetch_sub( 1, CDS_ATOMIC::memory_order_relaxed ) == 1 ) {
57 CDS_ATOMIC::atomic_thread_fence( CDS_ATOMIC::memory_order_release );
64 void operator ++() CDS_NOEXCEPT
70 bool operator--() CDS_NOEXCEPT
76 /// Signed 32bit reference counter
77 typedef ref_counter<cds::int32_t> signed_ref_counter;
79 /// Unsigned 32bit reference counter
80 typedef ref_counter<cds::uint32_t> unsigned_ref_counter;
84 #endif // #ifndef __CDS_REFCOUNTER_H