triple_buffer.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #pragma once
  2. #include <stdint.h>
  3. /**
  4. * Template for triple buffers.
  5. *
  6. * This template implements a lock-free triple buffer that can be used to exchange data
  7. * between two threads that are producing and consuming at different rates. Instead of
  8. * atomically exchanging pointers to the buffers, we atomically update a Flags register
  9. * that holds the indices into a 3-element buffer array.
  10. *
  11. * The three buffers are named as follows:
  12. * - Read buffer: This is where Read() will read the latest value from
  13. * - Write buffer: This is where Write() will write a new value to
  14. * - Temp buffer: This is the second back-buffer currently not used for reading or writing
  15. *
  16. * Please note that reading and writing to the buffer does not automatically swap the
  17. * back-buffers. Instead, two separate methods, SwapReadBuffers() and SwapWriteBuffers()
  18. * are provided. For convenience, we also provide SwapAndRead() and WriteAndSwap() to
  19. * update and swap the buffers using a single method call.
  20. *
  21. * A dirty flag indicates whether a new value has been written and swapped into the second
  22. * back-buffer and is available for reading. It can be checked using the IsDirtyMethod().
  23. * As an optimization, SwapReadBuffers() and SwapAndRead() will not perform a back-buffer
  24. * swap if no new data is available.
  25. *
  26. * This class is thread-safe in single-producer, single-consumer scenarios.
  27. *
  28. * Based on ideas and C code in "Triple Buffering as a Concurrency Mechanism" (Reddit.com)
  29. *
  30. * @param BufferType The type of buffered items.
  31. */
  32. template<typename BufferType>
  33. class TTripleBuffer
  34. {
  35. /** Enumerates human readable bit values for accessing the Flags field. */
  36. enum EBufferFlag
  37. {
  38. /** Indicates whether a new buffer is available for reading. */
  39. Dirty = 0x40,
  40. /** Initial flags value (0dttwwrr; dirty = false, temp index = 0, write index = 1, read index = 2) */
  41. Initial = 0x06,
  42. /** Bit mask for accessing the read buffer index (bit 0-1). */
  43. ReaderMask = 0x03,
  44. /** Bit mask for the index of the unused/clean/empty buffer (bit 4-5). */
  45. TempMask = 0x30,
  46. /** Bit shift for accessing the temp buffer index. */
  47. TempShift = 4,
  48. /** Bit mask for accessing the write buffer index (bit 2-3). */
  49. WriterMask = 0x0c,
  50. /** Bit shift for accessing the write buffer index. */
  51. WriterShift = 2,
  52. };
  53. public:
  54. /** Default constructor. */
  55. TTripleBuffer()
  56. {
  57. Initialize();
  58. Buffers[0] = Buffers[1] = Buffers[2] = BufferType();
  59. }
  60. /**
  61. * Create and initialize a new instance with a given buffer value.
  62. *
  63. * @param InValue The initial value of all three buffers.
  64. */
  65. explicit TTripleBuffer(const BufferType& InValue)
  66. {
  67. Initialize();
  68. Buffers[0] = Buffers[1] = Buffers[2] = InValue;
  69. }
  70. /**
  71. * Create and initialize a new instance using provided buffers.
  72. *
  73. * The elements of the provided items array are expected to have
  74. * the following initial contents:
  75. * 0 = Temp
  76. * 1 = Write
  77. * 2 = Read
  78. *
  79. * @param InBuffers The buffer memory to use.
  80. */
  81. /*TTripleBuffer(BufferType(&InBuffers)[3])
  82. {
  83. Buffers = &InBuffers[0];
  84. Flags = EBufferFlag::Initial | EBufferFlag::Dirty;
  85. OwnsMemory = false;
  86. }*/
  87. /** Destructor. */
  88. ~TTripleBuffer()
  89. {
  90. delete[] Buffers;
  91. }
  92. public:
  93. /**
  94. * Check whether a new value is available for reading.
  95. *
  96. * @return true if a new value is available, false otherwise.
  97. */
  98. bool IsDirty() const
  99. {
  100. return ((Flags & EBufferFlag::Dirty) != 0);
  101. }
  102. /**
  103. * Read a value from the current read buffer.
  104. *
  105. * @return Reference to the read buffer's value.
  106. * @see SwapRead, Write
  107. */
  108. BufferType& Read()
  109. {
  110. return Buffers[Flags & EBufferFlag::ReaderMask];
  111. }
  112. /**
  113. * Swap the latest read buffer, if available.
  114. *
  115. * Will not perform a back-buffer swap if no new data is available (dirty flag = false).
  116. *
  117. * @see SwapWrite, Read
  118. */
  119. void SwapReadBuffers()
  120. {
  121. if(!IsDirty())
  122. {
  123. return;
  124. }
  125. Flags = SwapReadWithTempFlags(Flags);
  126. }
  127. public:
  128. /**
  129. * Get the current write buffer.
  130. *
  131. * @return Reference to write buffer.
  132. */
  133. BufferType& GetWriteBuffer()
  134. {
  135. return Buffers[(Flags & EBufferFlag::WriterMask) >> EBufferFlag::WriterShift];
  136. }
  137. /**
  138. * Swap a new write buffer (makes current write buffer available for reading).
  139. *
  140. * @see SwapRead, Write
  141. */
  142. void SwapWriteBuffers()
  143. {
  144. if(IsDirty())
  145. {
  146. return;
  147. }
  148. Flags = SwapWriteWithTempFlags(Flags);
  149. }
  150. /**
  151. * Write a value to the current write buffer.
  152. *
  153. * @param Value The value to write.
  154. * @see SwapWrite, Read
  155. */
  156. void Write(const BufferType Value)
  157. {
  158. Buffers[(Flags & EBufferFlag::WriterMask) >> EBufferFlag::WriterShift] = Value;
  159. }
  160. public:
  161. /** Reset the buffer. */
  162. void Reset()
  163. {
  164. Flags = EBufferFlag::Initial;
  165. }
  166. protected:
  167. /** Initialize the triple buffer. */
  168. void Initialize()
  169. {
  170. Buffers = new BufferType[3];
  171. Flags = EBufferFlag::Initial;
  172. }
  173. private:
  174. /** Swaps the read and temp buffer indices in the Flags field. */
  175. static int32_t SwapReadWithTempFlags(int32_t Flags)
  176. {
  177. return ((Flags & EBufferFlag::ReaderMask) << 4) | ((Flags & EBufferFlag::TempMask) >> 4) | (Flags & EBufferFlag::WriterMask);
  178. }
  179. /** Swaps the write and temp buffer indices in the Flags field, and sets the dirty bit. */
  180. static int32_t SwapWriteWithTempFlags(int32_t Flags)
  181. {
  182. return ((Flags & EBufferFlag::TempMask) >> 2) | ((Flags & EBufferFlag::WriterMask) << 2) | (Flags & EBufferFlag::ReaderMask) | EBufferFlag::Dirty;
  183. }
  184. private:
  185. /** Hidden copy constructor (triple buffers cannot be copied). */
  186. TTripleBuffer(const TTripleBuffer&);
  187. /** Hidden copy assignment (triple buffers cannot be copied). */
  188. TTripleBuffer& operator=(const TTripleBuffer&);
  189. private:
  190. /** The three buffers. */
  191. BufferType* Buffers;
  192. /** Buffer access flags. */
  193. __declspec(align(16)) int32_t volatile Flags ;
  194. };