15 #ifndef LUNCHBOX_ATOMIC_H
16 #define LUNCHBOX_ATOMIC_H
19 #include <lunchbox/compiler.h>
23 # pragma warning (push)
24 # pragma warning (disable: 4985) // inconsistent decl of ceil
27 # pragma warning (pop)
28 # pragma intrinsic(_ReadWriteBarrier)
29 #elif defined(__xlC__)
30 # include <builtins.h>
42 #elif defined(_MSC_VER)
44 #elif defined(__xlC__)
49 # error "no memory barrier implemented for this platform"
91 LUNCHBOX_API
static T
getAndAdd( T& value,
const T increment );
94 LUNCHBOX_API
static T
getAndSub( T& value,
const T increment );
97 static T
addAndGet( T& value,
const T increment );
100 static T
subAndGet( T& value,
const T increment );
103 LUNCHBOX_API
static T
incAndGet( T& value );
106 LUNCHBOX_API
static T
decAndGet( T& value );
109 LUNCHBOX_API
static bool compareAndSwap( T* value,
const T expected,
113 explicit Atomic(
const T v = 0 );
119 operator T(
void)
const;
166 LB_ALIGN8(
mutable T _value );
174 return __sync_fetch_and_add( &value, increment );
179 return __sync_fetch_and_sub( &value, increment );
184 return __sync_add_and_fetch( &value, increment );
189 return __sync_sub_and_fetch( &value, increment );
194 return addAndGet( value, 1 );
199 return subAndGet( value, 1 );
205 return __sync_bool_compare_and_swap( value, expected, newValue );
208 #elif defined (_MSC_VER)
213 return getAndAdd( value, increment ) + increment;
218 return getAndSub( value, increment ) - increment;
226 return __compare_and_swap( value, const_cast< T* >( &expected ), newValue );
231 const int64_t newValue )
233 return __compare_and_swaplp( value, const_cast< int64_t* >( &expected ),
238 # error No compare-and-swap implementated for this platform
246 const T oldv = value;
247 const T newv = oldv + increment;
248 if( !compareAndSwap( &value, oldv, newv ))
261 const T oldv = value;
262 const T newv = oldv - increment;
263 if( !compareAndSwap( &value, oldv, newv ))
276 const T oldv = value;
277 const T newv = oldv + increment;
291 const T oldv = value;
292 const T newv = oldv - increment;
303 return addAndGet( value, 1 );
308 return subAndGet( value, 1 );
338 return addAndGet( _value, v );
343 return subAndGet( _value, v );
348 return incAndGet( _value );
353 return decAndGet( _value );
358 return getAndAdd( _value, 1 );
363 return getAndSub( _value, 1 );
369 return _value == rhs._value;
375 return _value != rhs._value;
381 return compareAndSwap( &_value, expected, newValue );
385 #endif // LUNCHBOX_ATOMIC_H
static T decAndGet(T &value)
static T getAndSub(T &value, const T increment)
A variable with atomic semantics and standalone atomic operations.
Defines export visibility macros for library Lunchbox.
Basic type definitions not provided by the operating system.
void memoryBarrierRelease()
Perform a store-with-release memory barrier.
static T getAndAdd(T &value, const T increment)
static T addAndGet(T &value, const T increment)
static T incAndGet(T &value)
bool operator!=(const Atomic< T > &rhs) const
T operator--(void)
Atomically decrement by one and return the new value.
static T subAndGet(T &value, const T increment)
static bool compareAndSwap(T *value, const T expected, const T newValue)
Perform a compare-and-swap atomic operation.
void memoryBarrier()
Perform a full memory barrier.
bool operator==(const Atomic< T > &rhs) const
void operator=(const T v)
Assign a new value.
T operator+=(T v)
Atomically add a value and return the new value.
Atomic(const T v=0)
Construct a new atomic variable with an initial value.
void memoryBarrierAcquire()
Perform a load-with-acquire memory barrier.
Abstraction layer and common utilities for multi-threaded programming.
T operator-=(T v)
Atomically substract a value and return the new value.
T operator++(void)
Atomically increment by one and return the new value.