fesa-core  4.2.0
fesa::Atomic< T > Class Template Reference

#include <Atomic.h>

Inherits noncopyable.

Inherited by fesa::AtomicInteger< T >.

Public Member Functions

 Atomic (T initialValue=0)
 
 Atomic (char *memory, bool aligned, T initialValue=0, bool ignoreInitialValue=false)
 
load () const
 
void store (T value)
 
bool compareAndExchange (T &expected, const T desired)
 

Static Public Member Functions

static void checkAlignment (const char *pointer)
 

Static Public Attributes

static const std::size_t alignment = Aligned<T>::naturalAlignment
 
static const std::size_t unalignedSize = Aligned<T>::unalignedSize
 

Static Protected Member Functions

static boost::atomic< T > * alignAndCreate (char *memory, T initialValue, bool ignoreInitialValue=false)
 
static boost::atomic< T > * create (char *memory, T initialValue, bool ignoreInitialValue=false)
 

Protected Attributes

boost::atomic< T > * value_
 
bool mine_
 

Detailed Description

template<typename T>
class fesa::Atomic< T >

Wrapper class for atomic types. Can either use preexisting memory, or allocate it. All operations on Atomic<T> objects are guaranteed to be atomic. Additionally, all operations on Atomic<T> objects instantiated on preexisting memory are guaranteed to be lock-free. Instantiable with all integral types (bool included) from 8 to 64 bits.

Constructor & Destructor Documentation

template<typename T>
fesa::Atomic< T >::Atomic ( initialValue = 0)
explicit

Allocates and creates an atomic type.

Parameters
initialValuethe initial value
template<typename T>
fesa::Atomic< T >::Atomic ( char *  memory,
bool  aligned,
initialValue = 0,
bool  ignoreInitialValue = false 
)

Creates an atomic type from arbitrary memory. The memory buffer given must satisfy one of these conditions: A. Have a size of sizeof(T) and be aligned properly (for x86 and x64, this means aligned on sizeof(T)) B. Have a size of Atomic<T>::unalignedSize; the alignment doesn't matter then. In case A, the aligned parameter must be true; in case B, it must be false. Additionally, in case A, alignment will be checked and an exception thrown if the pointer is unaligned.

Parameters
memorypointer to the memory to use
alignedwhether the memory pointer is properly aligned
initialValuethe initial value to give to this object. If not given, object is initialized to 0.
ignoreInitialValueif true, the atomic created is left uninitialized.
Exceptions
FesaExceptionif aligned is true, but memory is not properly aligned.
FesaExceptionif current platform doesn't support lock-free atomic operations on T.

Member Function Documentation

template<typename T>
boost::atomic< T > * fesa::Atomic< T >::alignAndCreate ( char *  memory,
initialValue,
bool  ignoreInitialValue = false 
)
staticprotected

Allocates a boost::atomic<T> on the memory pointer given, ensuring proper alignment. The memory pointer must point to a memory buffer of at least Atomic<T>::unalignedSize bytes.

Parameters
memorypointer to a buffer where the atomic type will be allocated
initialValueinitial value to give to the object
ignoreInitialValueif true, the atomic created is left uninitialized.
Returns
the newly created boost::atomic<T>
template<typename T >
void fesa::Atomic< T >::checkAlignment ( const char *  pointer)
static

Checks the alignment of pointer and throws if it is not properly aligned for T.

Parameters
pointerthe pointer to check
template<typename T>
bool fesa::Atomic< T >::compareAndExchange ( T &  expected,
const T  desired 
)
inline

Stores the desired value in the object only if its current value is equal to the expected value. In all cases, writes the previous value to expected.

Parameters
expectedthe expected value of the object
desiredthe desired value to store in the object
Returns
true in case of success, false otherwise
template<typename T>
boost::atomic< T > * fesa::Atomic< T >::create ( char *  memory,
initialValue,
bool  ignoreInitialValue = false 
)
staticprotected

Allocates a boost::atomic<T> on the memory pointer given. The pointer must be properly aligned. Alignment is checked and this function will throw if it is not correct. The size of the buffer pointed to by memory must be at least sizeof(T) bytes.

Parameters
memorypointer to a buffer where the atomic type will be allocated
initialValueinitial value to give to the object
ignoreInitialValueif true, the atomic created is left uninitialized.
Returns
the newly created boost::atomic<T>
template<typename T >
T fesa::Atomic< T >::load ( ) const
inline
Returns
the current value of the object.
template<typename T>
void fesa::Atomic< T >::store ( value)
inline

Stores the given value in the object.

Parameters
valuethe value to store.

Member Data Documentation

template<typename T>
const std::size_t fesa::Atomic< T >::alignment = Aligned<T>::naturalAlignment
static

Contains the required alignment for T.

template<typename T>
bool fesa::Atomic< T >::mine_
protected

Whether memory was allocated by this object or given as a paremeter to the constructor

template<typename T>
const std::size_t fesa::Atomic< T >::unalignedSize = Aligned<T>::unalignedSize
static

Contains the required unaligned memory size to ensure an aligned T can be allocated.

template<typename T>
boost::atomic<T>* fesa::Atomic< T >::value_
protected

Pointer to the wrapped atomic type


The documentation for this class was generated from the following files: