SingleProducerConsumer.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. /// \file
  11. /// \brief \b [Internal] Passes queued data between threads using a circular buffer with read and write pointers
  12. ///
  13. #ifndef __SINGLE_PRODUCER_CONSUMER_H
  14. #define __SINGLE_PRODUCER_CONSUMER_H
  15. #include "RakAssert.h"
  16. static const int MINIMUM_LIST_SIZE=8;
  17. #include "RakMemoryOverride.h"
  18. #include "Export.h"
  19. /// The namespace DataStructures was only added to avoid compiler errors for commonly named data structures
  20. /// As these data structures are stand-alone, you can use them outside of RakNet for your own projects if you wish.
  21. namespace DataStructures
  22. {
  23. /// \brief A single producer consumer implementation without critical sections.
  24. template <class SingleProducerConsumerType>
  25. class RAK_DLL_EXPORT SingleProducerConsumer
  26. {
  27. public:
  28. // Constructor
  29. SingleProducerConsumer();
  30. // Destructor
  31. ~SingleProducerConsumer();
  32. /// WriteLock must be immediately followed by WriteUnlock. These two functions must be called in the same thread.
  33. /// \return A pointer to a block of data you can write to.
  34. SingleProducerConsumerType* WriteLock(void);
  35. /// Call if you don't want to write to a block of data from WriteLock() after all.
  36. /// Cancelling locks cancels all locks back up to the data passed. So if you lock twice and cancel using the first lock, the second lock is ignored
  37. /// \param[in] cancelToLocation Which WriteLock() to cancel.
  38. void CancelWriteLock(SingleProducerConsumerType* cancelToLocation);
  39. /// Call when you are done writing to a block of memory returned by WriteLock()
  40. void WriteUnlock(void);
  41. /// ReadLock must be immediately followed by ReadUnlock. These two functions must be called in the same thread.
  42. /// \retval 0 No data is availble to read
  43. /// \retval Non-zero The data previously written to, in another thread, by WriteLock followed by WriteUnlock.
  44. SingleProducerConsumerType* ReadLock(void);
  45. // Cancelling locks cancels all locks back up to the data passed. So if you lock twice and cancel using the first lock, the second lock is ignored
  46. /// param[in] Which ReadLock() to cancel.
  47. void CancelReadLock(SingleProducerConsumerType* cancelToLocation);
  48. /// Signals that we are done reading the the data from the least recent call of ReadLock.
  49. /// At this point that pointer is no longer valid, and should no longer be read.
  50. void ReadUnlock(void);
  51. /// Clear is not thread-safe and none of the lock or unlock functions should be called while it is running.
  52. void Clear(void);
  53. /// This function will estimate how many elements are waiting to be read. It's threadsafe enough that the value returned is stable, but not threadsafe enough to give accurate results.
  54. /// \return An ESTIMATE of how many data elements are waiting to be read
  55. int Size(void) const;
  56. /// Make sure that the pointer we done reading for the call to ReadUnlock is the right pointer.
  57. /// param[in] A previous pointer returned by ReadLock()
  58. bool CheckReadUnlockOrder(const SingleProducerConsumerType* data) const;
  59. /// Returns if ReadUnlock was called before ReadLock
  60. /// \return If the read is locked
  61. bool ReadIsLocked(void) const;
  62. private:
  63. struct DataPlusPtr
  64. {
  65. DataPlusPtr () {readyToRead=false;}
  66. SingleProducerConsumerType object;
  67. // Ready to read is so we can use an equality boolean comparison, in case the writePointer var is trashed while context switching.
  68. volatile bool readyToRead;
  69. volatile DataPlusPtr *next;
  70. };
  71. volatile DataPlusPtr *readAheadPointer;
  72. volatile DataPlusPtr *writeAheadPointer;
  73. volatile DataPlusPtr *readPointer;
  74. volatile DataPlusPtr *writePointer;
  75. unsigned readCount, writeCount;
  76. };
  77. template <class SingleProducerConsumerType>
  78. SingleProducerConsumer<SingleProducerConsumerType>::SingleProducerConsumer()
  79. {
  80. // Preallocate
  81. readPointer = RakNet::OP_NEW<DataPlusPtr>( _FILE_AND_LINE_ );
  82. writePointer=readPointer;
  83. readPointer->next = RakNet::OP_NEW<DataPlusPtr>( _FILE_AND_LINE_ );
  84. int listSize;
  85. #ifdef _DEBUG
  86. RakAssert(MINIMUM_LIST_SIZE>=3);
  87. #endif
  88. for (listSize=2; listSize < MINIMUM_LIST_SIZE; listSize++)
  89. {
  90. readPointer=readPointer->next;
  91. readPointer->next = RakNet::OP_NEW<DataPlusPtr>( _FILE_AND_LINE_ );
  92. }
  93. readPointer->next->next=writePointer; // last to next = start
  94. readPointer=writePointer;
  95. readAheadPointer=readPointer;
  96. writeAheadPointer=writePointer;
  97. readCount=writeCount=0;
  98. }
  99. template <class SingleProducerConsumerType>
  100. SingleProducerConsumer<SingleProducerConsumerType>::~SingleProducerConsumer()
  101. {
  102. volatile DataPlusPtr *next;
  103. readPointer=writeAheadPointer->next;
  104. while (readPointer!=writeAheadPointer)
  105. {
  106. next=readPointer->next;
  107. RakNet::OP_DELETE((char*) readPointer, _FILE_AND_LINE_);
  108. readPointer=next;
  109. }
  110. RakNet::OP_DELETE((char*) readPointer, _FILE_AND_LINE_);
  111. }
  112. template <class SingleProducerConsumerType>
  113. SingleProducerConsumerType* SingleProducerConsumer<SingleProducerConsumerType>::WriteLock( void )
  114. {
  115. if (writeAheadPointer->next==readPointer ||
  116. writeAheadPointer->next->readyToRead==true)
  117. {
  118. volatile DataPlusPtr *originalNext=writeAheadPointer->next;
  119. writeAheadPointer->next=RakNet::OP_NEW<DataPlusPtr>(_FILE_AND_LINE_);
  120. RakAssert(writeAheadPointer->next);
  121. writeAheadPointer->next->next=originalNext;
  122. }
  123. volatile DataPlusPtr *last;
  124. last=writeAheadPointer;
  125. writeAheadPointer=writeAheadPointer->next;
  126. return (SingleProducerConsumerType*) last;
  127. }
  128. template <class SingleProducerConsumerType>
  129. void SingleProducerConsumer<SingleProducerConsumerType>::CancelWriteLock( SingleProducerConsumerType* cancelToLocation )
  130. {
  131. writeAheadPointer=(DataPlusPtr *)cancelToLocation;
  132. }
  133. template <class SingleProducerConsumerType>
  134. void SingleProducerConsumer<SingleProducerConsumerType>::WriteUnlock( void )
  135. {
  136. // DataPlusPtr *dataContainer = (DataPlusPtr *)structure;
  137. #ifdef _DEBUG
  138. RakAssert(writePointer->next!=readPointer);
  139. RakAssert(writePointer!=writeAheadPointer);
  140. #endif
  141. writeCount++;
  142. // User is done with the data, allow send by updating the write pointer
  143. writePointer->readyToRead=true;
  144. writePointer=writePointer->next;
  145. }
  146. template <class SingleProducerConsumerType>
  147. SingleProducerConsumerType* SingleProducerConsumer<SingleProducerConsumerType>::ReadLock( void )
  148. {
  149. if (readAheadPointer==writePointer ||
  150. readAheadPointer->readyToRead==false)
  151. {
  152. return 0;
  153. }
  154. volatile DataPlusPtr *last;
  155. last=readAheadPointer;
  156. readAheadPointer=readAheadPointer->next;
  157. return (SingleProducerConsumerType*)last;
  158. }
  159. template <class SingleProducerConsumerType>
  160. void SingleProducerConsumer<SingleProducerConsumerType>::CancelReadLock( SingleProducerConsumerType* cancelToLocation )
  161. {
  162. #ifdef _DEBUG
  163. RakAssert(readPointer!=writePointer);
  164. #endif
  165. readAheadPointer=(DataPlusPtr *)cancelToLocation;
  166. }
  167. template <class SingleProducerConsumerType>
  168. void SingleProducerConsumer<SingleProducerConsumerType>::ReadUnlock( void )
  169. {
  170. #ifdef _DEBUG
  171. RakAssert(readAheadPointer!=readPointer); // If hits, then called ReadUnlock before ReadLock
  172. RakAssert(readPointer!=writePointer); // If hits, then called ReadUnlock when Read returns 0
  173. #endif
  174. readCount++;
  175. // Allow writes to this memory block
  176. readPointer->readyToRead=false;
  177. readPointer=readPointer->next;
  178. }
  179. template <class SingleProducerConsumerType>
  180. void SingleProducerConsumer<SingleProducerConsumerType>::Clear( void )
  181. {
  182. // Shrink the list down to MINIMUM_LIST_SIZE elements
  183. volatile DataPlusPtr *next;
  184. writePointer=readPointer->next;
  185. int listSize=1;
  186. next=readPointer->next;
  187. while (next!=readPointer)
  188. {
  189. listSize++;
  190. next=next->next;
  191. }
  192. while (listSize-- > MINIMUM_LIST_SIZE)
  193. {
  194. next=writePointer->next;
  195. #ifdef _DEBUG
  196. RakAssert(writePointer!=readPointer);
  197. #endif
  198. RakNet::OP_DELETE((char*) writePointer, _FILE_AND_LINE_);
  199. writePointer=next;
  200. }
  201. readPointer->next=writePointer;
  202. writePointer=readPointer;
  203. readAheadPointer=readPointer;
  204. writeAheadPointer=writePointer;
  205. readCount=writeCount=0;
  206. }
  207. template <class SingleProducerConsumerType>
  208. int SingleProducerConsumer<SingleProducerConsumerType>::Size( void ) const
  209. {
  210. return writeCount-readCount;
  211. }
  212. template <class SingleProducerConsumerType>
  213. bool SingleProducerConsumer<SingleProducerConsumerType>::CheckReadUnlockOrder(const SingleProducerConsumerType* data) const
  214. {
  215. return const_cast<const SingleProducerConsumerType *>(&readPointer->object) == data;
  216. }
  217. template <class SingleProducerConsumerType>
  218. bool SingleProducerConsumer<SingleProducerConsumerType>::ReadIsLocked(void) const
  219. {
  220. return readAheadPointer!=readPointer;
  221. }
  222. }
  223. #endif
粤ICP备19079148号