1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
// Copyright (c) 2006-2018 Maxim Khizhinsky
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef CDSLIB_DETAILS_MARKED_PTR_H
#define CDSLIB_DETAILS_MARKED_PTR_H
#include <cds/algo/atomic.h>
namespace cds {
namespace details {
/// Marked pointer
/**
On the modern architectures, the default data alignment is 4 (for 32bit) or 8 byte for 64bit.
Therefore, the least 2 or 3 bits of the pointer is always zero and can
be used as a bitfield to store logical flags. This trick is widely used in
lock-free programming to operate with the pointer and its flags atomically.
Template parameters:
- T - type of pointer
- Bitmask - bitmask of least unused bits
*/
template <typename T, int Bitmask>
class marked_ptr
{
T * m_ptr ; ///< pointer and its mark bits
public:
typedef T value_type ; ///< type of value the class points to
typedef T * pointer_type ; ///< type of pointer
static constexpr const uintptr_t bitmask = Bitmask; ///< bitfield bitmask
static constexpr const uintptr_t pointer_bitmask = ~bitmask; ///< pointer bitmask
public:
/// Constructs null marked pointer. The flag is cleared.
constexpr marked_ptr() noexcept
: m_ptr( nullptr )
{}
/// Constructs marked pointer with \p ptr value. The least bit(s) of \p ptr is the flag.
constexpr explicit marked_ptr( value_type * ptr ) noexcept
: m_ptr( ptr )
{}
/// Constructs marked pointer with \p ptr value and \p nMask flag.
/**
The \p nMask argument defines the OR-bits
*/
marked_ptr( value_type * ptr, int nMask ) noexcept
: m_ptr( ptr )
{
assert( bits() == 0 );
*this |= nMask;
}
/// Copy constructor
marked_ptr( marked_ptr const& src ) noexcept = default;
/// Copy-assignment operator
marked_ptr& operator =( marked_ptr const& p ) noexcept = default;
# if !defined(CDS_DISABLE_DEFAULT_MOVE_CTOR)
//@cond
marked_ptr( marked_ptr&& src ) noexcept = default;
marked_ptr& operator =( marked_ptr&& p ) noexcept = default;
//@endcond
# endif
//TODO: make move ctor
private:
//@cond
union pointer_cast {
T * ptr;
uintptr_t n;
pointer_cast(T * p) : ptr(p) {}
pointer_cast(uintptr_t i) : n(i) {}
};
static uintptr_t to_int( value_type * p ) noexcept
{
return pointer_cast(p).n;
}
static value_type * to_ptr( uintptr_t n ) noexcept
{
return pointer_cast(n).ptr;
}
uintptr_t to_int() const noexcept
{
return to_int( m_ptr );
}
//@endcond
public:
/// Returns the pointer without mark bits (real pointer) const version
value_type * ptr() const noexcept
{
return to_ptr( to_int() & ~bitmask );
}
/// Returns the pointer and bits together
value_type * all() const noexcept
{
return m_ptr;
}
/// Returns the least bits of pointer according to \p Bitmask template argument of the class
uintptr_t bits() const noexcept
{
return to_int() & bitmask;
}
/// Analogue for \ref ptr
value_type * operator ->() const noexcept
{
return ptr();
}
/// Assignment operator sets markup bits to zero
marked_ptr operator =( T * p ) noexcept
{
m_ptr = p;
return *this;
}
/// Set LSB bits as <tt>bits() | nBits</tt>
marked_ptr& operator |=( int nBits ) noexcept
{
assert( (nBits & pointer_bitmask) == 0 );
m_ptr = to_ptr( to_int() | nBits );
return *this;
}
/// Set LSB bits as <tt>bits() & nBits</tt>
marked_ptr& operator &=( int nBits ) noexcept
{
assert( (nBits & pointer_bitmask) == 0 );
m_ptr = to_ptr( to_int() & (pointer_bitmask | nBits));
return *this;
}
/// Set LSB bits as <tt>bits() ^ nBits</tt>
marked_ptr& operator ^=( int nBits ) noexcept
{
assert( (nBits & pointer_bitmask) == 0 );
m_ptr = to_ptr( to_int() ^ nBits );
return *this;
}
/// Returns <tt>p |= nBits</tt>
friend marked_ptr operator |( marked_ptr p, int nBits) noexcept
{
p |= nBits;
return p;
}
/// Returns <tt>p |= nBits</tt>
friend marked_ptr operator |( int nBits, marked_ptr p ) noexcept
{
p |= nBits;
return p;
}
/// Returns <tt>p &= nBits</tt>
friend marked_ptr operator &( marked_ptr p, int nBits) noexcept
{
p &= nBits;
return p;
}
/// Returns <tt>p &= nBits</tt>
friend marked_ptr operator &( int nBits, marked_ptr p ) noexcept
{
p &= nBits;
return p;
}
/// Returns <tt>p ^= nBits</tt>
friend marked_ptr operator ^( marked_ptr p, int nBits) noexcept
{
p ^= nBits;
return p;
}
/// Returns <tt>p ^= nBits</tt>
friend marked_ptr operator ^( int nBits, marked_ptr p ) noexcept
{
p ^= nBits;
return p;
}
/// Inverts LSBs of pointer \p p
friend marked_ptr operator ~( marked_ptr p ) noexcept
{
return p ^ marked_ptr::bitmask;
}
/// Comparing two marked pointer including its mark bits
friend bool operator ==( marked_ptr p1, marked_ptr p2 ) noexcept
{
return p1.all() == p2.all();
}
/// Comparing marked pointer and raw pointer, mark bits of \p p1 is ignored
friend bool operator ==( marked_ptr p1, value_type const * p2 ) noexcept
{
return p1.ptr() == p2;
}
/// Comparing marked pointer and raw pointer, mark bits of \p p2 is ignored
friend bool operator ==( value_type const * p1, marked_ptr p2 ) noexcept
{
return p1 == p2.ptr();
}
/// Comparing two marked pointer including its mark bits
friend bool operator !=( marked_ptr p1, marked_ptr p2 ) noexcept
{
return p1.all() != p2.all();
}
/// Comparing marked pointer and raw pointer, mark bits of \p p1 is ignored
friend bool operator !=( marked_ptr p1, value_type const * p2 ) noexcept
{
return p1.ptr() != p2;
}
/// Comparing marked pointer and raw pointer, mark bits of \p p2 is ignored
friend bool operator !=( value_type const * p1, marked_ptr p2 ) noexcept
{
return p1 != p2.ptr();
}
//@cond
/// atomic< marked_ptr< T, Bitmask > > support
T *& impl_ref() noexcept
{
return m_ptr;
}
//@endcond
};
} // namespace details
} // namespace cds
//@cond
CDS_CXX11_ATOMIC_BEGIN_NAMESPACE
template <typename T, int Bitmask >
class atomic< cds::details::marked_ptr<T, Bitmask> >
{
private:
typedef cds::details::marked_ptr<T, Bitmask> marked_ptr;
typedef atomics::atomic<T *> atomic_impl;
atomic_impl m_atomic;
public:
bool is_lock_free() const volatile noexcept
{
return m_atomic.is_lock_free();
}
bool is_lock_free() const noexcept
{
return m_atomic.is_lock_free();
}
void store(marked_ptr val, memory_order order = memory_order_seq_cst) volatile noexcept
{
m_atomic.store( val.all(), order );
}
void store(marked_ptr val, memory_order order = memory_order_seq_cst) noexcept
{
m_atomic.store( val.all(), order );
}
marked_ptr load(memory_order order = memory_order_seq_cst) const volatile noexcept
{
return marked_ptr( m_atomic.load( order ));
}
marked_ptr load(memory_order order = memory_order_seq_cst) const noexcept
{
return marked_ptr( m_atomic.load( order ));
}
operator marked_ptr() const volatile noexcept
{
return load();
}
operator marked_ptr() const noexcept
{
return load();
}
marked_ptr exchange(marked_ptr val, memory_order order = memory_order_seq_cst) volatile noexcept
{
return marked_ptr( m_atomic.exchange( val.all(), order ));
}
marked_ptr exchange(marked_ptr val, memory_order order = memory_order_seq_cst) noexcept
{
return marked_ptr( m_atomic.exchange( val.all(), order ));
}
bool compare_exchange_weak(marked_ptr& expected, marked_ptr desired, memory_order success_order, memory_order failure_order) volatile noexcept
{
return m_atomic.compare_exchange_weak( expected.impl_ref(), desired.all(), success_order, failure_order );
}
bool compare_exchange_weak(marked_ptr& expected, marked_ptr desired, memory_order success_order, memory_order failure_order) noexcept
{
return m_atomic.compare_exchange_weak( expected.impl_ref(), desired.all(), success_order, failure_order );
}
bool compare_exchange_strong(marked_ptr& expected, marked_ptr desired, memory_order success_order, memory_order failure_order) volatile noexcept
{
return m_atomic.compare_exchange_strong( expected.impl_ref(), desired.all(), success_order, failure_order );
}
bool compare_exchange_strong(marked_ptr& expected, marked_ptr desired, memory_order success_order, memory_order failure_order) noexcept
{
return m_atomic.compare_exchange_strong( expected.impl_ref(), desired.all(), success_order, failure_order );
}
bool compare_exchange_weak(marked_ptr& expected, marked_ptr desired, memory_order success_order = memory_order_seq_cst) volatile noexcept
{
return m_atomic.compare_exchange_weak( expected.impl_ref(), desired.all(), success_order );
}
bool compare_exchange_weak(marked_ptr& expected, marked_ptr desired, memory_order success_order = memory_order_seq_cst) noexcept
{
return m_atomic.compare_exchange_weak( expected.impl_ref(), desired.all(), success_order );
}
bool compare_exchange_strong(marked_ptr& expected, marked_ptr desired, memory_order success_order = memory_order_seq_cst) volatile noexcept
{
return m_atomic.compare_exchange_strong( expected.impl_ref(), desired.all(), success_order );
}
bool compare_exchange_strong(marked_ptr& expected, marked_ptr desired, memory_order success_order = memory_order_seq_cst) noexcept
{
return m_atomic.compare_exchange_strong( expected.impl_ref(), desired.all(), success_order );
}
constexpr atomic() noexcept
: m_atomic( nullptr )
{}
constexpr explicit atomic(marked_ptr val) noexcept
: m_atomic( val.all())
{}
constexpr explicit atomic(T * p) noexcept
: m_atomic( p )
{}
atomic(const atomic&) = delete;
atomic& operator=(const atomic&) = delete;
#if !(CDS_COMPILER == CDS_COMPILER_MSVC && CDS_COMPILER_VERSION < CDS_COMPILER_MSVC15)
// MSVC12, MSVC14, MSVC14.1: warning C4522: multiple assignment operators specified
atomic& operator=(const atomic&) volatile = delete;
marked_ptr operator=(marked_ptr val) volatile noexcept
{
store( val );
return val;
}
#endif
marked_ptr operator=(marked_ptr val) noexcept
{
store( val );
return val;
}
};
CDS_CXX11_ATOMIC_END_NAMESPACE
//@endcond
#endif // #ifndef CDSLIB_DETAILS_MARKED_PTR_H
|