The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

317 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. Encapsulates the logic required to implement a lock-free FIFO.
  22. This class handles the logic needed when building a single-reader, single-writer FIFO.
  23. It doesn't actually hold any data itself, but your FIFO class can use one of these to manage
  24. its position and status when reading or writing to it.
  25. To use it, you can call prepareToWrite() to determine the position within your own buffer that
  26. an incoming block of data should be stored, and prepareToRead() to find out when the next
  27. outgoing block should be read from.
  28. e.g.
  29. @code
  30. class MyFifo
  31. {
  32. public:
  33. MyFifo() : abstractFifo (1024)
  34. {
  35. }
  36. void addToFifo (const int* someData, int numItems)
  37. {
  38. int start1, size1, start2, size2;
  39. abstractFifo.prepareToWrite (numItems, start1, size1, start2, size2);
  40. if (size1 > 0)
  41. copySomeData (myBuffer + start1, someData, size1);
  42. if (size2 > 0)
  43. copySomeData (myBuffer + start2, someData + size1, size2);
  44. abstractFifo.finishedWrite (size1 + size2);
  45. }
  46. void readFromFifo (int* someData, int numItems)
  47. {
  48. int start1, size1, start2, size2;
  49. abstractFifo.prepareToRead (numItems, start1, size1, start2, size2);
  50. if (size1 > 0)
  51. copySomeData (someData, myBuffer + start1, size1);
  52. if (size2 > 0)
  53. copySomeData (someData + size1, myBuffer + start2, size2);
  54. abstractFifo.finishedRead (size1 + size2);
  55. }
  56. private:
  57. AbstractFifo abstractFifo;
  58. int myBuffer [1024];
  59. };
  60. @endcode
  61. @tags{Core}
  62. */
  63. class JUCE_API AbstractFifo
  64. {
  65. public:
  66. //==============================================================================
  67. /** Creates a FIFO to manage a buffer with the specified capacity. */
  68. AbstractFifo (int capacity) noexcept;
  69. /** Destructor */
  70. ~AbstractFifo();
  71. //==============================================================================
  72. /** Returns the total size of the buffer being managed. */
  73. int getTotalSize() const noexcept;
  74. /** Returns the number of items that can currently be added to the buffer without it overflowing. */
  75. int getFreeSpace() const noexcept;
  76. /** Returns the number of items that can currently be read from the buffer. */
  77. int getNumReady() const noexcept;
  78. /** Clears the buffer positions, so that it appears empty. */
  79. void reset() noexcept;
  80. /** Changes the buffer's total size.
  81. Note that this isn't thread-safe, so don't call it if there's any danger that it
  82. might overlap with a call to any other method in this class!
  83. */
  84. void setTotalSize (int newSize) noexcept;
  85. //==============================================================================
  86. /** Returns the location within the buffer at which an incoming block of data should be written.
  87. Because the section of data that you want to add to the buffer may overlap the end
  88. and wrap around to the start, two blocks within your buffer are returned, and you
  89. should copy your data into the first one, with any remaining data spilling over into
  90. the second.
  91. If the number of items you ask for is too large to fit within the buffer's free space, then
  92. blockSize1 + blockSize2 may add up to a lower value than numToWrite. If this happens, you
  93. may decide to keep waiting and re-trying the method until there's enough space available.
  94. After calling this method, if you choose to write your data into the blocks returned, you
  95. must call finishedWrite() to tell the FIFO how much data you actually added.
  96. e.g.
  97. @code
  98. void addToFifo (const int* someData, int numItems)
  99. {
  100. int start1, size1, start2, size2;
  101. prepareToWrite (numItems, start1, size1, start2, size2);
  102. if (size1 > 0)
  103. copySomeData (myBuffer + start1, someData, size1);
  104. if (size2 > 0)
  105. copySomeData (myBuffer + start2, someData + size1, size2);
  106. finishedWrite (size1 + size2);
  107. }
  108. @endcode
  109. @param numToWrite indicates how many items you'd like to add to the buffer
  110. @param startIndex1 on exit, this will contain the start index in your buffer at which your data should be written
  111. @param blockSize1 on exit, this indicates how many items can be written to the block starting at startIndex1
  112. @param startIndex2 on exit, this will contain the start index in your buffer at which any data that didn't fit into
  113. the first block should be written
  114. @param blockSize2 on exit, this indicates how many items can be written to the block starting at startIndex2
  115. @see finishedWrite
  116. */
  117. void prepareToWrite (int numToWrite, int& startIndex1, int& blockSize1, int& startIndex2, int& blockSize2) const noexcept;
  118. /** Called after writing from the FIFO, to indicate that this many items have been added.
  119. @see prepareToWrite
  120. */
  121. void finishedWrite (int numWritten) noexcept;
  122. /** Returns the location within the buffer from which the next block of data should be read.
  123. Because the section of data that you want to read from the buffer may overlap the end
  124. and wrap around to the start, two blocks within your buffer are returned, and you
  125. should read from both of them.
  126. If the number of items you ask for is greater than the amount of data available, then
  127. blockSize1 + blockSize2 may add up to a lower value than numWanted. If this happens, you
  128. may decide to keep waiting and re-trying the method until there's enough data available.
  129. After calling this method, if you choose to read the data, you must call finishedRead() to
  130. tell the FIFO how much data you have consumed.
  131. e.g.
  132. @code
  133. void readFromFifo (int* someData, int numItems)
  134. {
  135. int start1, size1, start2, size2;
  136. prepareToRead (numSamples, start1, size1, start2, size2);
  137. if (size1 > 0)
  138. copySomeData (someData, myBuffer + start1, size1);
  139. if (size2 > 0)
  140. copySomeData (someData + size1, myBuffer + start2, size2);
  141. finishedRead (size1 + size2);
  142. }
  143. @endcode
  144. @param numWanted indicates how many items you'd like to add to the buffer
  145. @param startIndex1 on exit, this will contain the start index in your buffer at which your data should be written
  146. @param blockSize1 on exit, this indicates how many items can be written to the block starting at startIndex1
  147. @param startIndex2 on exit, this will contain the start index in your buffer at which any data that didn't fit into
  148. the first block should be written
  149. @param blockSize2 on exit, this indicates how many items can be written to the block starting at startIndex2
  150. @see finishedRead
  151. */
  152. void prepareToRead (int numWanted, int& startIndex1, int& blockSize1, int& startIndex2, int& blockSize2) const noexcept;
  153. /** Called after reading from the FIFO, to indicate that this many items have now been consumed.
  154. @see prepareToRead
  155. */
  156. void finishedRead (int numRead) noexcept;
  157. //==============================================================================
  158. private:
  159. enum class ReadOrWrite
  160. {
  161. read,
  162. write
  163. };
  164. public:
  165. /** Class for a scoped reader/writer */
  166. template <ReadOrWrite mode>
  167. class ScopedReadWrite final
  168. {
  169. public:
  170. /** Construct an unassigned reader/writer. Doesn't do anything upon destruction. */
  171. ScopedReadWrite() = default;
  172. /** Construct a reader/writer and immediately call prepareRead/prepareWrite
  173. on the abstractFifo which was passed in.
  174. This object will hold a pointer back to the fifo, so make sure that
  175. the fifo outlives this object.
  176. */
  177. ScopedReadWrite (AbstractFifo&, int num) noexcept;
  178. ScopedReadWrite (const ScopedReadWrite&) = delete;
  179. ScopedReadWrite (ScopedReadWrite&&) noexcept;
  180. ScopedReadWrite& operator= (const ScopedReadWrite&) = delete;
  181. ScopedReadWrite& operator= (ScopedReadWrite&&) noexcept;
  182. /** Calls finishedRead or finishedWrite if this is a non-null scoped
  183. reader/writer.
  184. */
  185. ~ScopedReadWrite() noexcept;
  186. /** Calls the passed function with each index that was deemed valid
  187. for the current read/write operation.
  188. */
  189. template <typename FunctionToApply>
  190. void forEach (FunctionToApply&& func) const
  191. {
  192. for (auto i = startIndex1, e = startIndex1 + blockSize1; i != e; ++i) func (i);
  193. for (auto i = startIndex2, e = startIndex2 + blockSize2; i != e; ++i) func (i);
  194. }
  195. int startIndex1, blockSize1, startIndex2, blockSize2;
  196. private:
  197. void prepare (AbstractFifo&, int) noexcept;
  198. static void finish (AbstractFifo&, int) noexcept;
  199. void swap (ScopedReadWrite&) noexcept;
  200. AbstractFifo* fifo = nullptr;
  201. };
  202. using ScopedRead = ScopedReadWrite<ReadOrWrite::read>;
  203. using ScopedWrite = ScopedReadWrite<ReadOrWrite::write>;
  204. /** Replaces prepareToRead/finishedRead with a single function.
  205. This function returns an object which contains the start indices and
  206. block sizes, and also automatically finishes the read operation when
  207. it goes out of scope.
  208. @code
  209. {
  210. auto readHandle = fifo.read (4);
  211. for (auto i = 0; i != readHandle.blockSize1; ++i)
  212. {
  213. // read the item at index readHandle.startIndex1 + i
  214. }
  215. for (auto i = 0; i != readHandle.blockSize2; ++i)
  216. {
  217. // read the item at index readHandle.startIndex2 + i
  218. }
  219. } // readHandle goes out of scope here, finishing the read operation
  220. @endcode
  221. */
  222. ScopedRead read (int numToRead) noexcept { return { *this, numToRead }; }
  223. /** Replaces prepareToWrite/finishedWrite with a single function.
  224. This function returns an object which contains the start indices and
  225. block sizes, and also automatically finishes the write operation when
  226. it goes out of scope.
  227. @code
  228. {
  229. auto writeHandle = fifo.write (5);
  230. for (auto i = 0; i != writeHandle.blockSize1; ++i)
  231. {
  232. // write the item at index writeHandle.startIndex1 + i
  233. }
  234. for (auto i = 0; i != writeHandle.blockSize2; ++i)
  235. {
  236. // write the item at index writeHandle.startIndex2 + i
  237. }
  238. } // writeHandle goes out of scope here, finishing the write operation
  239. @endcode
  240. */
  241. ScopedWrite write (int numToWrite) noexcept { return { *this, numToWrite }; }
  242. private:
  243. //==============================================================================
  244. int bufferSize;
  245. Atomic<int> validStart, validEnd;
  246. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AbstractFifo)
  247. };
  248. } // namespace juce