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.

297 lines
8.5KB

  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. AbstractFifo::AbstractFifo (int capacity) noexcept : bufferSize (capacity)
  20. {
  21. jassert (bufferSize > 0);
  22. }
  23. AbstractFifo::~AbstractFifo() {}
  24. int AbstractFifo::getTotalSize() const noexcept { return bufferSize; }
  25. int AbstractFifo::getFreeSpace() const noexcept { return bufferSize - getNumReady() - 1; }
  26. int AbstractFifo::getNumReady() const noexcept
  27. {
  28. auto vs = validStart.get();
  29. auto ve = validEnd.get();
  30. return ve >= vs ? (ve - vs) : (bufferSize - (vs - ve));
  31. }
  32. void AbstractFifo::reset() noexcept
  33. {
  34. validEnd = 0;
  35. validStart = 0;
  36. }
  37. void AbstractFifo::setTotalSize (int newSize) noexcept
  38. {
  39. jassert (newSize > 0);
  40. reset();
  41. bufferSize = newSize;
  42. }
  43. //==============================================================================
  44. void AbstractFifo::prepareToWrite (int numToWrite, int& startIndex1, int& blockSize1, int& startIndex2, int& blockSize2) const noexcept
  45. {
  46. auto vs = validStart.get();
  47. auto ve = validEnd.get();
  48. auto freeSpace = ve >= vs ? (bufferSize - (ve - vs)) : (vs - ve);
  49. numToWrite = jmin (numToWrite, freeSpace - 1);
  50. if (numToWrite <= 0)
  51. {
  52. startIndex1 = 0;
  53. startIndex2 = 0;
  54. blockSize1 = 0;
  55. blockSize2 = 0;
  56. }
  57. else
  58. {
  59. startIndex1 = ve;
  60. startIndex2 = 0;
  61. blockSize1 = jmin (bufferSize - ve, numToWrite);
  62. numToWrite -= blockSize1;
  63. blockSize2 = numToWrite <= 0 ? 0 : jmin (numToWrite, vs);
  64. }
  65. }
  66. void AbstractFifo::finishedWrite (int numWritten) noexcept
  67. {
  68. jassert (numWritten >= 0 && numWritten < bufferSize);
  69. auto newEnd = validEnd.get() + numWritten;
  70. if (newEnd >= bufferSize)
  71. newEnd -= bufferSize;
  72. validEnd = newEnd;
  73. }
  74. void AbstractFifo::prepareToRead (int numWanted, int& startIndex1, int& blockSize1, int& startIndex2, int& blockSize2) const noexcept
  75. {
  76. auto vs = validStart.get();
  77. auto ve = validEnd.get();
  78. auto numReady = ve >= vs ? (ve - vs) : (bufferSize - (vs - ve));
  79. numWanted = jmin (numWanted, numReady);
  80. if (numWanted <= 0)
  81. {
  82. startIndex1 = 0;
  83. startIndex2 = 0;
  84. blockSize1 = 0;
  85. blockSize2 = 0;
  86. }
  87. else
  88. {
  89. startIndex1 = vs;
  90. startIndex2 = 0;
  91. blockSize1 = jmin (bufferSize - vs, numWanted);
  92. numWanted -= blockSize1;
  93. blockSize2 = numWanted <= 0 ? 0 : jmin (numWanted, ve);
  94. }
  95. }
  96. void AbstractFifo::finishedRead (int numRead) noexcept
  97. {
  98. jassert (numRead >= 0 && numRead <= bufferSize);
  99. auto newStart = validStart.get() + numRead;
  100. if (newStart >= bufferSize)
  101. newStart -= bufferSize;
  102. validStart = newStart;
  103. }
  104. //==============================================================================
  105. template <AbstractFifo::ReadOrWrite mode>
  106. AbstractFifo::ScopedReadWrite<mode>::ScopedReadWrite (AbstractFifo& f, int num) noexcept
  107. : fifo (&f)
  108. {
  109. prepare (*fifo, num);
  110. }
  111. template <AbstractFifo::ReadOrWrite mode>
  112. AbstractFifo::ScopedReadWrite<mode>::ScopedReadWrite (ScopedReadWrite&& other) noexcept
  113. : startIndex1 (other.startIndex1),
  114. blockSize1 (other.blockSize1),
  115. startIndex2 (other.startIndex2),
  116. blockSize2 (other.blockSize2)
  117. {
  118. swap (other);
  119. }
  120. template <AbstractFifo::ReadOrWrite mode>
  121. AbstractFifo::ScopedReadWrite<mode>&
  122. AbstractFifo::ScopedReadWrite<mode>::operator= (ScopedReadWrite&& other) noexcept
  123. {
  124. swap (other);
  125. return *this;
  126. }
  127. template <AbstractFifo::ReadOrWrite mode>
  128. AbstractFifo::ScopedReadWrite<mode>::~ScopedReadWrite() noexcept
  129. {
  130. if (fifo != nullptr)
  131. finish (*fifo, blockSize1 + blockSize2);
  132. }
  133. template <AbstractFifo::ReadOrWrite mode>
  134. void AbstractFifo::ScopedReadWrite<mode>::swap (ScopedReadWrite& other) noexcept
  135. {
  136. std::swap (other.fifo, fifo);
  137. std::swap (other.startIndex1, startIndex1);
  138. std::swap (other.blockSize1, blockSize1);
  139. std::swap (other.startIndex2, startIndex2);
  140. std::swap (other.blockSize2, blockSize2);
  141. }
  142. template<>
  143. void AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::read>::prepare (AbstractFifo& f, int num) noexcept
  144. {
  145. f.prepareToRead (num, startIndex1, blockSize1, startIndex2, blockSize2);
  146. }
  147. template<>
  148. void AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::write>::prepare (AbstractFifo& f, int num) noexcept
  149. {
  150. f.prepareToWrite (num, startIndex1, blockSize1, startIndex2, blockSize2);
  151. }
  152. template<>
  153. void AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::read>::finish (AbstractFifo& f, int num) noexcept
  154. {
  155. f.finishedRead (num);
  156. }
  157. template<>
  158. void AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::write>::finish (AbstractFifo& f, int num) noexcept
  159. {
  160. f.finishedWrite (num);
  161. }
  162. template class AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::read>;
  163. template class AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::write>;
  164. //==============================================================================
  165. #if JUCE_UNIT_TESTS
  166. class AbstractFifoTests : public UnitTest
  167. {
  168. public:
  169. AbstractFifoTests() : UnitTest ("Abstract Fifo", "Containers") {}
  170. struct WriteThread : public Thread
  171. {
  172. WriteThread (AbstractFifo& f, int* b, Random rng)
  173. : Thread ("fifo writer"), fifo (f), buffer (b), random (rng)
  174. {
  175. startThread();
  176. }
  177. ~WriteThread()
  178. {
  179. stopThread (5000);
  180. }
  181. void run()
  182. {
  183. int n = 0;
  184. while (! threadShouldExit())
  185. {
  186. int num = random.nextInt (2000) + 1;
  187. auto writer = fifo.write (num);
  188. jassert (writer.blockSize1 >= 0 && writer.blockSize2 >= 0);
  189. jassert (writer.blockSize1 == 0
  190. || (writer.startIndex1 >= 0 && writer.startIndex1 < fifo.getTotalSize()));
  191. jassert (writer.blockSize2 == 0
  192. || (writer.startIndex2 >= 0 && writer.startIndex2 < fifo.getTotalSize()));
  193. writer.forEach ([this, &n] (int index) { this->buffer[index] = n++; });
  194. }
  195. }
  196. AbstractFifo& fifo;
  197. int* buffer;
  198. Random random;
  199. };
  200. void runTest() override
  201. {
  202. beginTest ("AbstractFifo");
  203. int buffer[5000];
  204. AbstractFifo fifo (numElementsInArray (buffer));
  205. WriteThread writer (fifo, buffer, getRandom());
  206. int n = 0;
  207. Random r = getRandom();
  208. r.combineSeed (12345);
  209. for (int count = 100000; --count >= 0;)
  210. {
  211. int num = r.nextInt (6000) + 1;
  212. auto reader = fifo.read (num);
  213. if (! (reader.blockSize1 >= 0 && reader.blockSize2 >= 0)
  214. && (reader.blockSize1 == 0
  215. || (reader.startIndex1 >= 0 && reader.startIndex1 < fifo.getTotalSize()))
  216. && (reader.blockSize2 == 0
  217. || (reader.startIndex2 >= 0 && reader.startIndex2 < fifo.getTotalSize())))
  218. {
  219. expect (false, "prepareToRead returned -ve values");
  220. break;
  221. }
  222. bool failed = false;
  223. reader.forEach ([&failed, &buffer, &n] (int index)
  224. {
  225. failed = (buffer[index] != n++) || failed;
  226. });
  227. if (failed)
  228. {
  229. expect (false, "read values were incorrect");
  230. break;
  231. }
  232. }
  233. }
  234. };
  235. static AbstractFifoTests fifoUnitTests;
  236. #endif
  237. } // namespace juce