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.

343 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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. struct MyFifo
  31. {
  32. void addToFifo (const int* someData, int numItems)
  33. {
  34. int start1, size1, start2, size2;
  35. abstractFifo.prepareToWrite (numItems, start1, size1, start2, size2);
  36. if (size1 > 0)
  37. copySomeData (myBuffer + start1, someData, size1);
  38. if (size2 > 0)
  39. copySomeData (myBuffer + start2, someData + size1, size2);
  40. abstractFifo.finishedWrite (size1 + size2);
  41. }
  42. void readFromFifo (int* someData, int numItems)
  43. {
  44. int start1, size1, start2, size2;
  45. abstractFifo.prepareToRead (numItems, start1, size1, start2, size2);
  46. if (size1 > 0)
  47. copySomeData (someData, myBuffer + start1, size1);
  48. if (size2 > 0)
  49. copySomeData (someData + size1, myBuffer + start2, size2);
  50. abstractFifo.finishedRead (size1 + size2);
  51. }
  52. AbstractFifo abstractFifo { 1024 };
  53. int myBuffer[1024];
  54. };
  55. @endcode
  56. @tags{Core}
  57. */
  58. class JUCE_API AbstractFifo
  59. {
  60. public:
  61. //==============================================================================
  62. /** Creates a FIFO to manage a buffer with the specified capacity. */
  63. AbstractFifo (int capacity) noexcept;
  64. /** Destructor */
  65. ~AbstractFifo();
  66. //==============================================================================
  67. /** Returns the total size of the buffer being managed. */
  68. int getTotalSize() const noexcept;
  69. /** Returns the number of items that can currently be added to the buffer without it overflowing. */
  70. int getFreeSpace() const noexcept;
  71. /** Returns the number of items that can currently be read from the buffer. */
  72. int getNumReady() const noexcept;
  73. /** Clears the buffer positions, so that it appears empty. */
  74. void reset() noexcept;
  75. /** Changes the buffer's total size.
  76. Note that this isn't thread-safe, so don't call it if there's any danger that it
  77. might overlap with a call to any other method in this class!
  78. */
  79. void setTotalSize (int newSize) noexcept;
  80. //==============================================================================
  81. /** Returns the location within the buffer at which an incoming block of data should be written.
  82. Because the section of data that you want to add to the buffer may overlap the end
  83. and wrap around to the start, two blocks within your buffer are returned, and you
  84. should copy your data into the first one, with any remaining data spilling over into
  85. the second.
  86. If the number of items you ask for is too large to fit within the buffer's free space, then
  87. blockSize1 + blockSize2 may add up to a lower value than numToWrite. If this happens, you
  88. may decide to keep waiting and re-trying the method until there's enough space available.
  89. After calling this method, if you choose to write your data into the blocks returned, you
  90. must call finishedWrite() to tell the FIFO how much data you actually added.
  91. e.g.
  92. @code
  93. void addToFifo (const int* someData, int numItems)
  94. {
  95. int start1, size1, start2, size2;
  96. prepareToWrite (numItems, start1, size1, start2, size2);
  97. if (size1 > 0)
  98. copySomeData (myBuffer + start1, someData, size1);
  99. if (size2 > 0)
  100. copySomeData (myBuffer + start2, someData + size1, size2);
  101. finishedWrite (size1 + size2);
  102. }
  103. @endcode
  104. @param numToWrite indicates how many items you'd like to add to the buffer
  105. @param startIndex1 on exit, this will contain the start index in your buffer at which your data should be written
  106. @param blockSize1 on exit, this indicates how many items can be written to the block starting at startIndex1
  107. @param startIndex2 on exit, this will contain the start index in your buffer at which any data that didn't fit into
  108. the first block should be written
  109. @param blockSize2 on exit, this indicates how many items can be written to the block starting at startIndex2
  110. @see finishedWrite
  111. */
  112. void prepareToWrite (int numToWrite, int& startIndex1, int& blockSize1, int& startIndex2, int& blockSize2) const noexcept;
  113. /** Called after writing from the FIFO, to indicate that this many items have been added.
  114. @see prepareToWrite
  115. */
  116. void finishedWrite (int numWritten) noexcept;
  117. /** Returns the location within the buffer from which the next block of data should be read.
  118. Because the section of data that you want to read from the buffer may overlap the end
  119. and wrap around to the start, two blocks within your buffer are returned, and you
  120. should read from both of them.
  121. If the number of items you ask for is greater than the amount of data available, then
  122. blockSize1 + blockSize2 may add up to a lower value than numWanted. If this happens, you
  123. may decide to keep waiting and re-trying the method until there's enough data available.
  124. After calling this method, if you choose to read the data, you must call finishedRead() to
  125. tell the FIFO how much data you have consumed.
  126. e.g.
  127. @code
  128. void readFromFifo (int* someData, int numItems)
  129. {
  130. int start1, size1, start2, size2;
  131. prepareToRead (numSamples, start1, size1, start2, size2);
  132. if (size1 > 0)
  133. copySomeData (someData, myBuffer + start1, size1);
  134. if (size2 > 0)
  135. copySomeData (someData + size1, myBuffer + start2, size2);
  136. finishedRead (size1 + size2);
  137. }
  138. @endcode
  139. @param numWanted indicates how many items you'd like to add to the buffer
  140. @param startIndex1 on exit, this will contain the start index in your buffer at which your data should be written
  141. @param blockSize1 on exit, this indicates how many items can be written to the block starting at startIndex1
  142. @param startIndex2 on exit, this will contain the start index in your buffer at which any data that didn't fit into
  143. the first block should be written
  144. @param blockSize2 on exit, this indicates how many items can be written to the block starting at startIndex2
  145. @see finishedRead
  146. */
  147. void prepareToRead (int numWanted, int& startIndex1, int& blockSize1, int& startIndex2, int& blockSize2) const noexcept;
  148. /** Called after reading from the FIFO, to indicate that this many items have now been consumed.
  149. @see prepareToRead
  150. */
  151. void finishedRead (int numRead) noexcept;
  152. //==============================================================================
  153. private:
  154. enum class ReadOrWrite
  155. {
  156. read,
  157. write
  158. };
  159. public:
  160. /** Class for a scoped reader/writer */
  161. template <ReadOrWrite mode>
  162. class ScopedReadWrite final
  163. {
  164. public:
  165. /** Construct an unassigned reader/writer. Doesn't do anything upon destruction. */
  166. ScopedReadWrite() = default;
  167. /** Construct a reader/writer and immediately call prepareRead/prepareWrite
  168. on the abstractFifo which was passed in.
  169. This object will hold a pointer back to the fifo, so make sure that
  170. the fifo outlives this object.
  171. */
  172. ScopedReadWrite (AbstractFifo& f, int num) noexcept : fifo (&f)
  173. {
  174. prepare (*fifo, num);
  175. }
  176. ScopedReadWrite (const ScopedReadWrite&) = delete;
  177. ScopedReadWrite (ScopedReadWrite&&) noexcept;
  178. ScopedReadWrite& operator= (const ScopedReadWrite&) = delete;
  179. ScopedReadWrite& operator= (ScopedReadWrite&&) noexcept;
  180. /** Calls finishedRead or finishedWrite if this is a non-null scoped
  181. reader/writer.
  182. */
  183. ~ScopedReadWrite() noexcept
  184. {
  185. if (fifo != nullptr)
  186. finish (*fifo, blockSize1 + blockSize2);
  187. }
  188. /** Calls the passed function with each index that was deemed valid
  189. for the current read/write operation.
  190. */
  191. template <typename FunctionToApply>
  192. void forEach (FunctionToApply&& func) const
  193. {
  194. for (auto i = startIndex1, e = startIndex1 + blockSize1; i != e; ++i) func (i);
  195. for (auto i = startIndex2, e = startIndex2 + blockSize2; i != e; ++i) func (i);
  196. }
  197. int startIndex1, blockSize1, startIndex2, blockSize2;
  198. private:
  199. void prepare (AbstractFifo&, int) noexcept;
  200. static void finish (AbstractFifo&, int) noexcept;
  201. void swap (ScopedReadWrite&) noexcept;
  202. AbstractFifo* fifo = nullptr;
  203. };
  204. using ScopedRead = ScopedReadWrite<ReadOrWrite::read>;
  205. using ScopedWrite = ScopedReadWrite<ReadOrWrite::write>;
  206. /** Replaces prepareToRead/finishedRead with a single function.
  207. This function returns an object which contains the start indices and
  208. block sizes, and also automatically finishes the read operation when
  209. it goes out of scope.
  210. @code
  211. {
  212. auto readHandle = fifo.read (4);
  213. for (auto i = 0; i != readHandle.blockSize1; ++i)
  214. {
  215. // read the item at index readHandle.startIndex1 + i
  216. }
  217. for (auto i = 0; i != readHandle.blockSize2; ++i)
  218. {
  219. // read the item at index readHandle.startIndex2 + i
  220. }
  221. } // readHandle goes out of scope here, finishing the read operation
  222. @endcode
  223. */
  224. ScopedRead read (int numToRead) noexcept;
  225. /** Replaces prepareToWrite/finishedWrite with a single function.
  226. This function returns an object which contains the start indices and
  227. block sizes, and also automatically finishes the write operation when
  228. it goes out of scope.
  229. @code
  230. {
  231. auto writeHandle = fifo.write (5);
  232. for (auto i = 0; i != writeHandle.blockSize1; ++i)
  233. {
  234. // write the item at index writeHandle.startIndex1 + i
  235. }
  236. for (auto i = 0; i != writeHandle.blockSize2; ++i)
  237. {
  238. // write the item at index writeHandle.startIndex2 + i
  239. }
  240. } // writeHandle goes out of scope here, finishing the write operation
  241. @endcode
  242. */
  243. ScopedWrite write (int numToWrite) noexcept;
  244. private:
  245. //==============================================================================
  246. int bufferSize;
  247. Atomic<int> validStart, validEnd;
  248. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AbstractFifo)
  249. };
  250. template <>
  251. inline void AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::read>::finish (AbstractFifo& f, int num) noexcept
  252. {
  253. f.finishedRead (num);
  254. }
  255. template <>
  256. inline void AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::write>::finish (AbstractFifo& f, int num) noexcept
  257. {
  258. f.finishedWrite (num);
  259. }
  260. template <>
  261. inline void AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::read>::prepare (AbstractFifo& f, int num) noexcept
  262. {
  263. f.prepareToRead (num, startIndex1, blockSize1, startIndex2, blockSize2);
  264. }
  265. template <>
  266. inline void AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::write>::prepare (AbstractFifo& f, int num) noexcept
  267. {
  268. f.prepareToWrite (num, startIndex1, blockSize1, startIndex2, blockSize2);
  269. }
  270. } // namespace juce