57#include <SimoxUtility/threads/system_thread_id.h>
74#define DEBUG_MODE_PRINT \
75 if (debugMode.load()) \
76 std::cout << '[' << simox::system_thread_id() << "] [" << this << "] "
85 template <
class... Ts>
145 uint_fast8_t flagsNow(
flags.load(std::memory_order_consume));
153 while (!
flags.compare_exchange_weak(flagsNow,
155 std::memory_order_release,
156 std::memory_order_consume))
169 template <
class U = T>
170 typename std::enable_if<std::is_copy_constructible<U>::value>::type
189 std::cout <<
'[' << simox::system_thread_id() <<
"] [0x" <<
this
190 <<
"] set debug mode to " << mode <<
'\n';
194#undef DEBUG_MODE_PRINT
229 uint_fast8_t flagsNow(
flags.load(std::memory_order_consume));
230 while (!
flags.compare_exchange_weak(flagsNow,
232 std::memory_order_release,
233 std::memory_order_consume))
311 template <
typename T>
341 return tripleBuffer.getReadBuffer();
348 return tripleBuffer.getUpToDateReadBuffer();
355 return tripleBuffer._getNonConstReadBuffer();
362 return tripleBuffer._getNonConstHiddenBuffer();
369 return tripleBuffer.getReadBuffer();
379 return tripleBuffer.updateReadBuffer();
385 tripleBuffer.getWriteBuffer() = writeBuffer;
386 tripleBuffer.commitWrite();
393 tripleBuffer.reinitAllBuffers(init);
399 tripleBuffer.setDebugMode(mode);
A simple triple buffer for lockfree comunication between a single writer and a single reader.
uint_fast8_t getWriteBufferIndex() const
static const uint_fast8_t readBufferIndexShift
void swapWriteAndHiddenBuffer()
Swap the write buffer with the hidden buffer.
static const uint_fast8_t hiddenBufferIndexMask
std::atomic_bool debugMode
static const uint_fast8_t hiddenToWriteShift
static const uint_fast8_t writeBufferIndexShift
static const uint_fast8_t initialFlags
T & _getNonConstReadBuffer()
static const uint_fast8_t dirtyBitShift
const T & getWriteBuffer() const
const T & getReadBuffer() const
static const uint_fast8_t hiddenBufferIndexShift
const T & getUpToDateReadBuffer() const
static const uint_fast8_t writeBufferIndexMask
uint_fast8_t getReadBufferIndex() const
uint_fast8_t getHiddenBufferIndex() const
std::enable_if< std::is_copy_constructible< U >::value >::type reinitAllBuffers(const T &init)
T & _getNonConstHiddenBuffer()
bool updateReadBuffer() const
Swaps in the hidden buffer if it contains new data.
TripleBuffer(T &&initR, T &&initH, T &&initW)
bool hasNewWrite(uint_fast8_t flags) const
std::atomic_uint_fast8_t flags
static const uint_fast8_t hiddenToReadShift
TripleBuffer & operator=(const TripleBuffer &)=delete
static const uint_fast8_t dirtyBitMask
void reinitAllBuffers(T &&writeBuff, T &&hiddenBuff, T &&readBuff)
static uint_fast8_t flagSwapReadWithHidden(uint_fast8_t flags)
swap read and hidden indexes
TripleBuffer(const Ts &... ts)
static uint_fast8_t flagSwapWriteWithHidden(uint_fast8_t flags)
swap write and hidden indexes of given flags (set dirty to 1)
static const uint_fast8_t readBufferIndexMask
void setDebugMode(bool mode)
TripleBuffer(const TripleBuffer &)=delete
void reinitAllBuffers(const T &init)
T & _getNonConstReadBuffer()
WriteBufferedTripleBuffer()
const T & getWriteBuffer() const
const T & getReadBuffer() const
const T & getUpToDateReadBuffer() const
T & _getNonConstHiddenBuffer()
bool updateReadBuffer() const
Swaps in the hidden buffer if it contains new data.
T & _getNonConstHiddenWriteBuffer()
WriteBufferedTripleBuffer(const T &init)
void setDebugMode(bool mode)
This file offers overloads of toIce() and fromIce() functions for STL container types.