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 TgetReadBuffer () const
 
const TgetUpToDateReadBuffer () const
 
TgetWriteBuffer ()
 
const TgetWriteBuffer () 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. More...
 

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
 
TripleBufferoperator= (const TripleBuffer &)=delete
 
void swapWriteAndHiddenBuffer ()
 Swap the write buffer with the hidden buffer. More...
 
 TripleBuffer (const TripleBuffer &)=delete
 

Static Protected Member Functions

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

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)

Definition at line 71 of file TripleBuffer.h.

Constructor & Destructor Documentation

◆ TripleBuffer() [1/3]

TripleBuffer ( T &&  initR,
T &&  initH,
T &&  initW 
)
inline

Definition at line 78 of file TripleBuffer.h.

◆ TripleBuffer() [2/3]

TripleBuffer ( const Ts &...  ts)
inline

Definition at line 86 of file TripleBuffer.h.

◆ TripleBuffer() [3/3]

TripleBuffer ( const TripleBuffer< T > &  )
protecteddelete

Member Function Documentation

◆ _getNonConstHiddenBuffer()

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

Definition at line 132 of file TripleBuffer.h.

◆ _getNonConstReadBuffer()

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

Definition at line 124 of file TripleBuffer.h.

+ Here is the caller graph for this function:

◆ commitWrite()

void commitWrite ( )
inline

Definition at line 163 of file TripleBuffer.h.

◆ flagSwapReadWithHidden()

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

Definition at line 255 of file TripleBuffer.h.

+ Here is the caller graph for this function:

◆ flagSwapWriteWithHidden()

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

Definition at line 271 of file TripleBuffer.h.

+ Here is the caller graph for this function:

◆ getHiddenBufferIndex()

uint_fast8_t getHiddenBufferIndex ( ) const
inlineprotected

Definition at line 217 of file TripleBuffer.h.

+ Here is the caller graph for this function:

◆ getReadBuffer()

const T& getReadBuffer ( ) const
inline
Returns
the current read buffer

Definition at line 108 of file TripleBuffer.h.

+ Here is the caller graph for this function:

◆ getReadBufferIndex()

uint_fast8_t getReadBufferIndex ( ) const
inlineprotected

Definition at line 203 of file TripleBuffer.h.

+ Here is the caller graph for this function:

◆ getUpToDateReadBuffer()

const T& getUpToDateReadBuffer ( ) const
inline
Returns
the most up to date read buffer

Definition at line 116 of file TripleBuffer.h.

◆ getWriteBuffer() [1/2]

T& getWriteBuffer ( )
inline
Returns
the write buffer

Definition at line 92 of file TripleBuffer.h.

◆ getWriteBuffer() [2/2]

const T& getWriteBuffer ( ) const
inline
Returns
the write buffer

Definition at line 100 of file TripleBuffer.h.

◆ getWriteBufferIndex()

uint_fast8_t getWriteBufferIndex ( ) const
inlineprotected

Definition at line 210 of file TripleBuffer.h.

+ Here is the caller graph for this function:

◆ hasNewWrite()

bool hasNewWrite ( uint_fast8_t  flags) const
inlineprotected

Definition at line 243 of file TripleBuffer.h.

+ Here is the caller graph for this function:

◆ operator=()

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

◆ reinitAllBuffers() [1/2]

std::enable_if<std::is_copy_constructible<U>::value>::type reinitAllBuffers ( const T init)
inline

Definition at line 171 of file TripleBuffer.h.

+ Here is the caller graph for this function:

◆ reinitAllBuffers() [2/2]

void reinitAllBuffers ( T &&  writeBuff,
T &&  hiddenBuff,
T &&  readBuff 
)
inline

Definition at line 179 of file TripleBuffer.h.

◆ setDebugMode()

void setDebugMode ( bool  mode)
inline

Definition at line 187 of file TripleBuffer.h.

◆ swapWriteAndHiddenBuffer()

void swapWriteAndHiddenBuffer ( )
inlineprotected

Swap the write buffer with the hidden buffer.

Definition at line 227 of file TripleBuffer.h.

+ Here is the caller graph for this function:

◆ updateReadBuffer()

bool updateReadBuffer ( ) const
inline

Swaps in the hidden buffer if it contains new data.

Returns
True if new data is available

Definition at line 143 of file TripleBuffer.h.

+ Here is the caller graph for this function:

Member Data Documentation

◆ buffer

T buffer[3]
protected

Definition at line 302 of file TripleBuffer.h.

◆ debugMode

std::atomic_bool debugMode {false}
protected

Definition at line 303 of file TripleBuffer.h.

◆ dirtyBitMask

const uint_fast8_t dirtyBitMask = 1 << dirtyBitShift
staticprotected

Definition at line 295 of file TripleBuffer.h.

◆ dirtyBitShift

const uint_fast8_t dirtyBitShift = 7
staticprotected

Definition at line 286 of file TripleBuffer.h.

◆ flags

std::atomic_uint_fast8_t flags {initialFlags}
mutableprotected

Definition at line 301 of file TripleBuffer.h.

◆ hiddenBufferIndexMask

const uint_fast8_t hiddenBufferIndexMask = 3 << hiddenBufferIndexShift
staticprotected

Definition at line 293 of file TripleBuffer.h.

◆ hiddenBufferIndexShift

const uint_fast8_t hiddenBufferIndexShift = 2
staticprotected

Definition at line 284 of file TripleBuffer.h.

◆ hiddenToReadShift

const uint_fast8_t hiddenToReadShift = hiddenBufferIndexShift - readBufferIndexShift
staticprotected

Definition at line 288 of file TripleBuffer.h.

◆ hiddenToWriteShift

const uint_fast8_t hiddenToWriteShift
staticprotected
Initial value:

Definition at line 289 of file TripleBuffer.h.

◆ initialFlags

const uint_fast8_t initialFlags = 2 + (1 << 2)
staticprotected

Definition at line 297 of file TripleBuffer.h.

◆ readBufferIndexMask

const uint_fast8_t readBufferIndexMask = 3 << readBufferIndexShift
staticprotected

Definition at line 292 of file TripleBuffer.h.

◆ readBufferIndexShift

const uint_fast8_t readBufferIndexShift = 0
staticprotected

Definition at line 283 of file TripleBuffer.h.

◆ writeBufferIndexMask

const uint_fast8_t writeBufferIndexMask = 3 << writeBufferIndexShift
staticprotected

Definition at line 294 of file TripleBuffer.h.

◆ writeBufferIndexShift

const uint_fast8_t writeBufferIndexShift = 4
staticprotected

Definition at line 285 of file TripleBuffer.h.


The documentation for this class was generated from the following file:
armarx::TripleBuffer::writeBufferIndexShift
static const uint_fast8_t writeBufferIndexShift
Definition: TripleBuffer.h:285
armarx::TripleBuffer::hiddenBufferIndexShift
static const uint_fast8_t hiddenBufferIndexShift
Definition: TripleBuffer.h:284