TripleBuffer< T > Class Template Reference

A simple triple buffer for lockfree comunication between a single writer and a single reader. More...

#include <ArmarXCore/util/CPPUtility/TripleBuffer.h>

Public Member Functions

T & _getNonConstHiddenBuffer ()
 
T & _getNonConstReadBuffer ()
 
void commitWrite ()
 
const T & getReadBuffer () const
 
const T & getUpToDateReadBuffer () const
 
T & getWriteBuffer ()
 
const T & getWriteBuffer () const
 
template<class U = T>
std::enable_if< std::is_copy_constructible< U >::value >::type reinitAllBuffers (const T &init)
 
void reinitAllBuffers (T &&writeBuff, T &&hiddenBuff, T &&readBuff)
 
void setDebugMode (bool mode)
 
template<class... Ts>
 TripleBuffer (const Ts &... ts)
 
 TripleBuffer (T &&initR, T &&initH, T &&initW)
 
bool updateReadBuffer () const
 Swaps in the hidden buffer if it contains new data.
 

Protected Member Functions

uint_fast8_t getHiddenBufferIndex () const
 
uint_fast8_t getReadBufferIndex () const
 
uint_fast8_t getWriteBufferIndex () const
 
bool hasNewWrite (uint_fast8_t flags) const
 
TripleBuffer & operator= (const TripleBuffer &)=delete
 
void swapWriteAndHiddenBuffer ()
 Swap the write buffer with the hidden buffer.
 
 TripleBuffer (const TripleBuffer &)=delete
 

Static Protected Member Functions

static uint_fast8_t flagSwapReadWithHidden (uint_fast8_t flags)
 swap read and hidden indexes
 
static uint_fast8_t flagSwapWriteWithHidden (uint_fast8_t flags)
 swap write and hidden indexes of given flags (set dirty to 1)
 

Protected Attributes

T buffer [3]
 
std::atomic_bool debugMode {false}
 
std::atomic_uint_fast8_t flags {initialFlags}
 

Static Protected Attributes

static const uint_fast8_t dirtyBitMask = 1 << dirtyBitShift
 
static const uint_fast8_t dirtyBitShift = 7
 
static const uint_fast8_t hiddenBufferIndexMask = 3 << hiddenBufferIndexShift
 
static const uint_fast8_t hiddenBufferIndexShift = 2
 
static const uint_fast8_t hiddenToReadShift = hiddenBufferIndexShift - readBufferIndexShift
 
static const uint_fast8_t hiddenToWriteShift
 
static const uint_fast8_t initialFlags = 2 + (1 << 2)
 
static const uint_fast8_t readBufferIndexMask = 3 << readBufferIndexShift
 
static const uint_fast8_t readBufferIndexShift = 0
 
static const uint_fast8_t writeBufferIndexMask = 3 << writeBufferIndexShift
 
static const uint_fast8_t writeBufferIndexShift = 4
 

Detailed Description

template<typename T>
class armarx::TripleBuffer< T >

A simple triple buffer for lockfree comunication between a single writer and a single reader.

If more than one reader/writer is required, you have to synchronize the access. Writers must not have: writes to the same data cell (except it is an atomic et al) and no writes are allowed during swapping of the buffer. Readers must not read when the buffer is swapped. If a writer only writes parts of the data structure, data loss could occure! (use a WriteBufferedTripleBuffer instead)

Constructor & Destructor Documentation

◆ TripleBuffer() [1/3]

template<typename T>
TripleBuffer ( T && initR,
T && initH,
T && initW )
inline

◆ TripleBuffer() [2/3]

template<typename T>
template<class... Ts>
TripleBuffer ( const Ts &... ts)
inline

◆ TripleBuffer() [3/3]

template<typename T>
TripleBuffer ( const TripleBuffer< T > & )
protecteddelete

Member Function Documentation

◆ _getNonConstHiddenBuffer()

template<typename T>
T & _getNonConstHiddenBuffer ( )
inline
Returns
the current read buffer (sometimes required for more complex initialization)

◆ _getNonConstReadBuffer()

template<typename T>
T & _getNonConstReadBuffer ( )
inline
Returns
the current read buffer (sometimes required for more complex initialization)

◆ commitWrite()

template<typename T>
void commitWrite ( )
inline

◆ flagSwapReadWithHidden()

template<typename T>
static uint_fast8_t flagSwapReadWithHidden ( uint_fast8_t flags)
inlinestaticprotected

swap read and hidden indexes

swap read and hidden indexes of given flags (set dirty to 0)

Parameters
flagsthe current flags
Returns
the new flags

◆ flagSwapWriteWithHidden()

template<typename T>
static uint_fast8_t flagSwapWriteWithHidden ( uint_fast8_t flags)
inlinestaticprotected

swap write and hidden indexes of given flags (set dirty to 1)

Parameters
flagsthe current flags
Returns
the new flags

◆ getHiddenBufferIndex()

template<typename T>
uint_fast8_t getHiddenBufferIndex ( ) const
inlineprotected

◆ getReadBuffer()

template<typename T>
const T & getReadBuffer ( ) const
inline
Returns
the current read buffer

◆ getReadBufferIndex()

template<typename T>
uint_fast8_t getReadBufferIndex ( ) const
inlineprotected

◆ getUpToDateReadBuffer()

template<typename T>
const T & getUpToDateReadBuffer ( ) const
inline
Returns
the most up to date read buffer

◆ getWriteBuffer() [1/2]

template<typename T>
T & getWriteBuffer ( )
inline
Returns
the write buffer

◆ getWriteBuffer() [2/2]

template<typename T>
const T & getWriteBuffer ( ) const
inline
Returns
the write buffer

◆ getWriteBufferIndex()

template<typename T>
uint_fast8_t getWriteBufferIndex ( ) const
inlineprotected

◆ hasNewWrite()

template<typename T>
bool hasNewWrite ( uint_fast8_t flags) const
inlineprotected

◆ operator=()

template<typename T>
TripleBuffer & operator= ( const TripleBuffer< T > & )
protecteddelete

◆ reinitAllBuffers() [1/2]

template<typename T>
template<class U = T>
std::enable_if< std::is_copy_constructible< U >::value >::type reinitAllBuffers ( const T & init)
inline

◆ reinitAllBuffers() [2/2]

template<typename T>
void reinitAllBuffers ( T && writeBuff,
T && hiddenBuff,
T && readBuff )
inline

◆ setDebugMode()

template<typename T>
void setDebugMode ( bool mode)
inline

◆ swapWriteAndHiddenBuffer()

template<typename T>
void swapWriteAndHiddenBuffer ( )
inlineprotected

Swap the write buffer with the hidden buffer.

◆ updateReadBuffer()

template<typename T>
bool updateReadBuffer ( ) const
inline

Swaps in the hidden buffer if it contains new data.

Returns
True if new data is available

Member Data Documentation

◆ buffer

template<typename T>
T buffer[3]
protected

◆ debugMode

template<typename T>
std::atomic_bool debugMode {false}
protected

◆ dirtyBitMask

template<typename T>
const uint_fast8_t dirtyBitMask = 1 << dirtyBitShift
staticprotected

◆ dirtyBitShift

template<typename T>
const uint_fast8_t dirtyBitShift = 7
staticprotected

◆ flags

template<typename T>
std::atomic_uint_fast8_t flags {initialFlags}
mutableprotected

◆ hiddenBufferIndexMask

template<typename T>
const uint_fast8_t hiddenBufferIndexMask = 3 << hiddenBufferIndexShift
staticprotected

◆ hiddenBufferIndexShift

template<typename T>
const uint_fast8_t hiddenBufferIndexShift = 2
staticprotected

◆ hiddenToReadShift

template<typename T>
const uint_fast8_t hiddenToReadShift = hiddenBufferIndexShift - readBufferIndexShift
staticprotected

◆ hiddenToWriteShift

template<typename T>
const uint_fast8_t hiddenToWriteShift
staticprotected
Initial value:
=
static const uint_fast8_t writeBufferIndexShift
Definition TripleBuffer.h:285
static const uint_fast8_t hiddenBufferIndexShift
Definition TripleBuffer.h:284

◆ initialFlags

template<typename T>
const uint_fast8_t initialFlags = 2 + (1 << 2)
staticprotected

◆ readBufferIndexMask

template<typename T>
const uint_fast8_t readBufferIndexMask = 3 << readBufferIndexShift
staticprotected

◆ readBufferIndexShift

template<typename T>
const uint_fast8_t readBufferIndexShift = 0
staticprotected

◆ writeBufferIndexMask

template<typename T>
const uint_fast8_t writeBufferIndexMask = 3 << writeBufferIndexShift
staticprotected

◆ writeBufferIndexShift

template<typename T>
const uint_fast8_t writeBufferIndexShift = 4
staticprotected

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