3 #ifndef __CDS_DETAILS_MARKED_PTR_H
4 #define __CDS_DETAILS_MARKED_PTR_H
6 #include <cds/cxx11_atomic.h>
13 On the modern architectures, the default data alignment is 4 (for 32bit) or 8 byte for 64bit.
14 Therefore, the least 2 or 3 bits of the pointer is always zero and can
15 be used as a bitfield to store logical flags. This trick is widely used in
16 lock-free programming to operate with the pointer and its flags atomically.
20 - Bitmask - bitmask of least unused bits
22 template <typename T, int Bitmask>
25 T * m_ptr ; ///< pointer and its mark bits
28 typedef T value_type ; ///< type of value the class points to
29 typedef T * pointer_type ; ///< type of pointer
30 static CDS_CONSTEXPR_CONST uintptr_t bitmask = Bitmask ; ///< bitfield bitmask
31 static CDS_CONSTEXPR_CONST uintptr_t pointer_bitmask = ~bitmask ; ///< pointer bitmask
34 /// Constructs null marked pointer. The flag is cleared.
35 CDS_CONSTEXPR marked_ptr() CDS_NOEXCEPT
39 /// Constructs marked pointer with \p ptr value. The least bit(s) of \p ptr is the flag.
40 CDS_CONSTEXPR explicit marked_ptr( value_type * ptr ) CDS_NOEXCEPT
44 /// Constructs marked pointer with \p ptr value and \p nMask flag.
46 The \p nMask argument defines the OR-bits
48 marked_ptr( value_type * ptr, int nMask ) CDS_NOEXCEPT
51 assert( bits() == 0 );
56 marked_ptr( marked_ptr const& src ) CDS_NOEXCEPT_DEFAULTED = default;
57 /// Copy-assignment operator
58 marked_ptr& operator =( marked_ptr const& p ) CDS_NOEXCEPT_DEFAULTED = default;
59 # if defined(CDS_MOVE_SEMANTICS_SUPPORT) && !defined(CDS_DISABLE_DEFAULT_MOVE_CTOR)
61 marked_ptr( marked_ptr&& src ) CDS_NOEXCEPT_DEFAULTED = default;
62 marked_ptr& operator =( marked_ptr&& p ) CDS_NOEXCEPT_DEFAULTED = default;
66 //TODO: make move ctor
70 static uintptr_t to_int( value_type * p ) CDS_NOEXCEPT
72 return reinterpret_cast<uintptr_t>( p );
75 static value_type * to_ptr( uintptr_t n ) CDS_NOEXCEPT
77 return reinterpret_cast< value_type *>( n );
80 uintptr_t to_int() const CDS_NOEXCEPT
82 return to_int( m_ptr );
87 /// Returns the pointer without mark bits (real pointer) const version
88 value_type * ptr() const CDS_NOEXCEPT
90 return to_ptr( to_int() & ~bitmask );
93 /// Returns the pointer and bits together
94 value_type * all() const CDS_NOEXCEPT
99 /// Returns the least bits of pointer according to \p Bitmask template argument of the class
100 uintptr_t bits() const CDS_NOEXCEPT
102 return to_int() & bitmask;
105 /// Analogue for \ref ptr
106 value_type * operator ->() const CDS_NOEXCEPT
111 /// Assignment operator sets markup bits to zero
112 marked_ptr operator =( T * p ) CDS_NOEXCEPT
118 /// Set LSB bits as <tt>bits() | nBits</tt>
119 marked_ptr& operator |=( int nBits ) CDS_NOEXCEPT
121 assert( (nBits & pointer_bitmask) == 0 );
122 m_ptr = to_ptr( to_int() | nBits );
126 /// Set LSB bits as <tt>bits() & nBits</tt>
127 marked_ptr& operator &=( int nBits ) CDS_NOEXCEPT
129 assert( (nBits & pointer_bitmask) == 0 );
130 m_ptr = to_ptr( to_int() & (pointer_bitmask | nBits) );
134 /// Set LSB bits as <tt>bits() ^ nBits</tt>
135 marked_ptr& operator ^=( int nBits ) CDS_NOEXCEPT
137 assert( (nBits & pointer_bitmask) == 0 );
138 m_ptr = to_ptr( to_int() ^ nBits );
142 /// Returns <tt>p |= nBits</tt>
143 friend marked_ptr operator |( marked_ptr p, int nBits) CDS_NOEXCEPT
149 /// Returns <tt>p |= nBits</tt>
150 friend marked_ptr operator |( int nBits, marked_ptr p ) CDS_NOEXCEPT
156 /// Returns <tt>p &= nBits</tt>
157 friend marked_ptr operator &( marked_ptr p, int nBits) CDS_NOEXCEPT
163 /// Returns <tt>p &= nBits</tt>
164 friend marked_ptr operator &( int nBits, marked_ptr p ) CDS_NOEXCEPT
170 /// Returns <tt>p ^= nBits</tt>
171 friend marked_ptr operator ^( marked_ptr p, int nBits) CDS_NOEXCEPT
176 /// Returns <tt>p ^= nBits</tt>
177 friend marked_ptr operator ^( int nBits, marked_ptr p ) CDS_NOEXCEPT
183 /// Inverts LSBs of pointer \p p
184 friend marked_ptr operator ~( marked_ptr p ) CDS_NOEXCEPT
186 return p ^ marked_ptr::bitmask;
190 /// Comparing two marked pointer including its mark bits
191 friend bool operator ==( marked_ptr p1, marked_ptr p2 ) CDS_NOEXCEPT
193 return p1.all() == p2.all();
196 /// Comparing marked pointer and raw pointer, mark bits of \p p1 is ignored
197 friend bool operator ==( marked_ptr p1, value_type const * p2 ) CDS_NOEXCEPT
199 return p1.ptr() == p2;
202 /// Comparing marked pointer and raw pointer, mark bits of \p p2 is ignored
203 friend bool operator ==( value_type const * p1, marked_ptr p2 ) CDS_NOEXCEPT
205 return p1 == p2.ptr();
208 /// Comparing two marked pointer including its mark bits
209 friend bool operator !=( marked_ptr p1, marked_ptr p2 ) CDS_NOEXCEPT
211 return p1.all() != p2.all();
214 /// Comparing marked pointer and raw pointer, mark bits of \p p1 is ignored
215 friend bool operator !=( marked_ptr p1, value_type const * p2 ) CDS_NOEXCEPT
217 return p1.ptr() != p2;
220 /// Comparing marked pointer and raw pointer, mark bits of \p p2 is ignored
221 friend bool operator !=( value_type const * p1, marked_ptr p2 ) CDS_NOEXCEPT
223 return p1 != p2.ptr();
227 /// atomic< marked_ptr< T, Bitmask > > support
228 T *& impl_ref() CDS_NOEXCEPT
234 } // namespace details
239 CDS_CXX11_ATOMIC_BEGIN_NAMESPACE
241 template <typename T, int Bitmask >
242 class atomic< cds::details::marked_ptr<T, Bitmask> >
245 typedef cds::details::marked_ptr<T, Bitmask> marked_ptr;
246 typedef atomics::atomic<T *> atomic_impl;
248 atomic_impl m_atomic;
250 bool is_lock_free() const volatile CDS_NOEXCEPT
252 return m_atomic.is_lock_free();
254 bool is_lock_free() const CDS_NOEXCEPT
256 return m_atomic.is_lock_free();
259 void store(marked_ptr val, memory_order order = memory_order_seq_cst) volatile CDS_NOEXCEPT
261 m_atomic.store( val.all(), order );
263 void store(marked_ptr val, memory_order order = memory_order_seq_cst) CDS_NOEXCEPT
265 m_atomic.store( val.all(), order );
268 marked_ptr load(memory_order order = memory_order_seq_cst) const volatile CDS_NOEXCEPT
270 return marked_ptr( m_atomic.load( order ));
272 marked_ptr load(memory_order order = memory_order_seq_cst) const CDS_NOEXCEPT
274 return marked_ptr( m_atomic.load( order ));
277 operator marked_ptr() const volatile CDS_NOEXCEPT
281 operator marked_ptr() const CDS_NOEXCEPT
286 marked_ptr exchange(marked_ptr val, memory_order order = memory_order_seq_cst) volatile CDS_NOEXCEPT
288 return marked_ptr( m_atomic.exchange( val.all(), order ));
290 marked_ptr exchange(marked_ptr val, memory_order order = memory_order_seq_cst) CDS_NOEXCEPT
292 return marked_ptr( m_atomic.exchange( val.all(), order ));
295 bool compare_exchange_weak(marked_ptr& expected, marked_ptr desired, memory_order success_order, memory_order failure_order) volatile CDS_NOEXCEPT
297 return m_atomic.compare_exchange_weak( expected.impl_ref(), desired.all(), success_order, failure_order );
299 bool compare_exchange_weak(marked_ptr& expected, marked_ptr desired, memory_order success_order, memory_order failure_order) CDS_NOEXCEPT
301 return m_atomic.compare_exchange_weak( expected.impl_ref(), desired.all(), success_order, failure_order );
303 bool compare_exchange_strong(marked_ptr& expected, marked_ptr desired, memory_order success_order, memory_order failure_order) volatile CDS_NOEXCEPT
305 return m_atomic.compare_exchange_strong( expected.impl_ref(), desired.all(), success_order, failure_order );
307 bool compare_exchange_strong(marked_ptr& expected, marked_ptr desired, memory_order success_order, memory_order failure_order) CDS_NOEXCEPT
309 return m_atomic.compare_exchange_strong( expected.impl_ref(), desired.all(), success_order, failure_order );
311 bool compare_exchange_weak(marked_ptr& expected, marked_ptr desired, memory_order success_order = memory_order_seq_cst) volatile CDS_NOEXCEPT
313 return m_atomic.compare_exchange_weak( expected.impl_ref(), desired.all(), success_order );
315 bool compare_exchange_weak(marked_ptr& expected, marked_ptr desired, memory_order success_order = memory_order_seq_cst) CDS_NOEXCEPT
317 return m_atomic.compare_exchange_weak( expected.impl_ref(), desired.all(), success_order );
319 bool compare_exchange_strong(marked_ptr& expected, marked_ptr desired, memory_order success_order = memory_order_seq_cst) volatile CDS_NOEXCEPT
321 return m_atomic.compare_exchange_strong( expected.impl_ref(), desired.all(), success_order );
323 bool compare_exchange_strong(marked_ptr& expected, marked_ptr desired, memory_order success_order = memory_order_seq_cst) CDS_NOEXCEPT
325 return m_atomic.compare_exchange_strong( expected.impl_ref(), desired.all(), success_order );
328 CDS_CONSTEXPR atomic() CDS_NOEXCEPT
329 : m_atomic( nullptr )
332 CDS_CONSTEXPR explicit atomic(marked_ptr val) CDS_NOEXCEPT
333 : m_atomic( val.all() )
335 CDS_CONSTEXPR explicit atomic(T * p) CDS_NOEXCEPT
339 # ifdef CDS_CXX11_DELETE_DEFINITION_SUPPORT
340 atomic(const atomic&) = delete;
341 atomic& operator=(const atomic&) = delete;
342 atomic& operator=(const atomic&) volatile = delete;
345 marked_ptr operator=(marked_ptr val) volatile CDS_NOEXCEPT
350 marked_ptr operator=(marked_ptr val) CDS_NOEXCEPT
357 CDS_CXX11_ATOMIC_END_NAMESPACE
360 #endif // #ifndef __CDS_DETAILS_MARKED_PTR_H