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.

334 lines
13KB

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