Embedded Multicore Building Blocks V1.0.0
Public Member Functions | List of all members
embb::base::Atomic< BaseType > Class Template Reference

Class representing atomic variables. More...

#include <atomic.h>

Public Member Functions

 Atomic ()
 Default constructor. More...
 
 Atomic (BaseType val)
 Valued-based constructor. More...
 
BaseType operator= (BaseType val)
 Assignment operator. More...
 
 operator BaseType () const
 Type conversion. More...
 
bool IsArithmetic () const
 Predicate representing support for arithmetic operations. More...
 
bool IsInteger () const
 Predicate representing integers. More...
 
bool IsPointer () const
 Predicate representing pointers. More...
 
void Store (BaseType val)
 Store operation. More...
 
BaseType Load () const
 Load operation. More...
 
BaseType Swap (BaseType val)
 Swap operation. More...
 
bool CompareAndSwap (BaseType &expected, BaseType desired)
 Compare-and-Swap operation (CAS). More...
 
Arithmetic members

The following members are only available if BaseType supports arithmetic operations (integer and non-void pointer types).

BaseType FetchAndAdd (BaseType val)
 Fetch-and-Add operation. More...
 
BaseType FetchAndSub (BaseType val)
 Fetch-and-Sub operation. More...
 
BaseType operator++ (int)
 Post-increment operation. More...
 
BaseType operator-- (int)
 Post-decrement operation. More...
 
BaseType operator++ ()
 Pre-increment operation. More...
 
BaseType operator-- ()
 Pre-decrement operation. More...
 
BaseType operator+= (BaseType val)
 Assignment by sum operation. More...
 
BaseType operator-= (BaseType val)
 Assignment by difference operation. More...
 
Integer members

The following members are only available if BaseType is an integer type.

void operator&= (BaseType val)
 Assignment by bitwise AND. More...
 
void operator|= (BaseType val)
 Assignment by bitwise OR. More...
 
void operator^= (BaseType val)
 Assignment by bitwise XOR. More...
 
Pointer members

The following members are only available if BaseType is a non-void pointer type.

BaseType * operator-> ()
 Structure dereference operation. More...
 
BaseType & operator* ()
 Dereference operation. More...
 

Detailed Description

template<typename BaseType>
class embb::base::Atomic< BaseType >

Class representing atomic variables.

The current implementation guarantees sequential consistency (full fences) for all atomic operations. Relaxed memory models may be added in the future.

Template Parameters
BaseTypeUnderlying type

Constructor & Destructor Documentation

template<typename BaseType>
embb::base::Atomic< BaseType >::Atomic ( )

Default constructor.

Constructs an atomic variable holding zero.

Concurrency
Thread-safe and wait-free
See also
Atomic(BaseType)
template<typename BaseType>
embb::base::Atomic< BaseType >::Atomic ( BaseType  val)
explicit

Valued-based constructor.

Constructs an atomic variable holding the passed value.

Parameters
valInitial value
Concurrency
Thread-safe and wait-free
Note
There is intentionally no copy constructor, since two different memory locations cannot be manipulated atomically.
See also
Atomic()

Member Function Documentation

template<typename BaseType>
BaseType embb::base::Atomic< BaseType >::operator= ( BaseType  val)

Assignment operator.

Assigns the passed value to the object.

Concurrency
Thread-safe and wait-free
Parameters
valThe value to assign
Returns
A shallow copy of this object
template<typename BaseType>
embb::base::Atomic< BaseType >::operator BaseType ( ) const

Type conversion.

Returns the value of the object. Equivalent to Load().

Concurrency
Thread-safe and wait-free
Returns
Stored value
See also
Load()
template<typename BaseType>
bool embb::base::Atomic< BaseType >::IsArithmetic ( ) const

Predicate representing support for arithmetic operations.

Returns true if type BaseType supports arithmetic operations, otherwise false. Only integers and non-void pointers support arithmetic operations.

Concurrency
Thread-safe and wait-free
Returns
Boolean value indicating support for arithmetic operations
See also
IsInteger(), IsPointer()
template<typename BaseType>
bool embb::base::Atomic< BaseType >::IsInteger ( ) const

Predicate representing integers.

Returns true if BaseType is an integer type, otherwise false.

Concurrency
Thread-safe and wait-free
Returns
Boolean value indicating whether BaseType is an integer
See also
IsArithmetic(), IsPointer()
template<typename BaseType>
bool embb::base::Atomic< BaseType >::IsPointer ( ) const

Predicate representing pointers.

Returns true if BaseType is a non-void pointer type, otherwise false.

Concurrency
Thread-safe and wait-free
Returns
Boolean value indicating whether BaseType is a non-void pointer type
See also
IsArithmetic(), IsInteger()
template<typename BaseType>
void embb::base::Atomic< BaseType >::Store ( BaseType  val)

Store operation.

Stores the passed value in the object. Equivalent to assignment operator, except that Store does not return anything.

Concurrency
Thread-safe and wait-free
Parameters
valValue to be stored
See also
Load()
template<typename BaseType>
BaseType embb::base::Atomic< BaseType >::Load ( ) const

Load operation.

Loads and returns the stored value. Equivalent to type conversion.

Concurrency
Thread-safe and wait-free
Returns
Stored value
See also
Store()
template<typename BaseType>
BaseType embb::base::Atomic< BaseType >::Swap ( BaseType  val)

Swap operation.

Stores the given value in the object and returns the old value.

Concurrency
Thread-safe and wait-free
Parameters
valNew value
Returns
Old value
See also
CompareAndSwap()
template<typename BaseType>
bool embb::base::Atomic< BaseType >::CompareAndSwap ( BaseType &  expected,
BaseType  desired 
)

Compare-and-Swap operation (CAS).

Stores desired if the current value is equal to expected. Otherwise, stores the current value in expected.

Concurrency
Thread-safe and wait-free
Parameters
expectedExpected value
desiredDesired value
Returns
true if CAS succeeded, otherwise false
See also
Swap()
template<typename BaseType>
BaseType embb::base::Atomic< BaseType >::FetchAndAdd ( BaseType  val)

Fetch-and-Add operation.

Adds the passed value and returns the old value.

Concurrency
Thread-safe and wait-free
Parameters
valAddend
Returns
Old value
See also
FetchAndSub()
template<typename BaseType>
BaseType embb::base::Atomic< BaseType >::FetchAndSub ( BaseType  val)

Fetch-and-Sub operation.

Subtracts the passed value and returns the old value.

Concurrency
Thread-safe and wait-free
Parameters
valSubtrahend
Returns
Old value
See also
FetchAndAdd()
template<typename BaseType>
BaseType embb::base::Atomic< BaseType >::operator++ ( int  )

Post-increment operation.

Increments the value and returns the old value.

Concurrency
Thread-safe and wait-free
Returns
Old value
See also
operator++()
template<typename BaseType>
BaseType embb::base::Atomic< BaseType >::operator-- ( int  )

Post-decrement operation.

Decrements the value and returns the old value.

Concurrency
Thread-safe and wait-free
Returns
Old value
See also
operator--()
template<typename BaseType>
BaseType embb::base::Atomic< BaseType >::operator++ ( )

Pre-increment operation.

Increments the value and returns the new value.

Concurrency
Thread-safe and wait-free
Returns
New value
See also
operator++(int)
template<typename BaseType>
BaseType embb::base::Atomic< BaseType >::operator-- ( )

Pre-decrement operation.

Decrements the value and returns the new value.

Concurrency
Thread-safe and wait-free
Returns
New value
See also
operator--(int)
template<typename BaseType>
BaseType embb::base::Atomic< BaseType >::operator+= ( BaseType  val)

Assignment by sum operation.

Adds the passed value and returns the new value.

Parameters
valAddend
Returns
New value
Concurrency
Thread-safe and wait-free
See also
operator-=()
template<typename BaseType>
BaseType embb::base::Atomic< BaseType >::operator-= ( BaseType  val)

Assignment by difference operation.

Subtracts the passed value and returns the new value.

Parameters
valSubtrahend
Returns
New value
Concurrency
Thread-safe and wait-free
See also
operator+=()
template<typename BaseType>
void embb::base::Atomic< BaseType >::operator&= ( BaseType  val)

Assignment by bitwise AND.

Stores the result of the bitwise AND in the current object. Does not return anything, since this cannot be implemented atomically on all architectures.

Concurrency
Thread-safe and wait-free
Parameters
valSecond operand of bitwise AND
See also
operator|=(), operator^=()
template<typename BaseType>
void embb::base::Atomic< BaseType >::operator|= ( BaseType  val)

Assignment by bitwise OR.

Stores the result of the bitwise OR in the current object. Does not return anything, since this cannot be implemented atomically on all architectures.

Concurrency
Thread-safe and wait-free
Parameters
valSecond operand of bitwise OR
See also
operator&=(), operator^=()
template<typename BaseType>
void embb::base::Atomic< BaseType >::operator^= ( BaseType  val)

Assignment by bitwise XOR.

Stores the result of the bitwise XOR in the current object. Does not return anything, since this cannot be implemented atomically on all architectures.

Parameters
valSecond operand of bitwise XOR
Concurrency
Thread-safe and wait-free
See also
operator&=(), operator|=()
template<typename BaseType>
BaseType* embb::base::Atomic< BaseType >::operator-> ( )

Structure dereference operation.

Used to access an element of an instance of a class or a structure pointed to by the stored pointer.

Returns
Stored pointer
Concurrency
Thread-safe and wait-free
See also
operator*()
template<typename BaseType>
BaseType& embb::base::Atomic< BaseType >::operator* ( )

Dereference operation.

Used to access the object pointed to by the stored pointer.

Returns
Reference to the object
Concurrency
Thread-safe and wait-free
See also
operator->()