Queue.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* Copyright 2025, Ableton AG, Berlin. All rights reserved.
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * If you would like to incorporate Link into a proprietary software application,
  17. * please contact <link-devs@ableton.com>.
  18. */
  19. #pragma once
  20. #include <atomic>
  21. #include <memory>
  22. #include <vector>
  23. namespace ableton
  24. {
  25. namespace link_audio
  26. {
  27. template <typename T>
  28. struct Queue
  29. {
  30. private:
  31. struct Impl;
  32. public:
  33. struct Writer
  34. {
  35. using SlotType = T;
  36. Writer(const Writer&) = delete;
  37. Writer& operator=(const Writer&) = delete;
  38. Writer(Writer&&) = default;
  39. Writer& operator=(Writer&&) = default;
  40. bool retainSlot()
  41. {
  42. return mpQueue->retainSlot(mpQueue->mWriterEnd, mpQueue->mReaderBegin);
  43. }
  44. void releaseSlot()
  45. {
  46. mpQueue->releaseSlot(mpQueue->mWriterBegin, mpQueue->mWriterEnd);
  47. }
  48. size_t numRetainedSlots() const
  49. {
  50. return mpQueue->numRetainedSlots(mpQueue->mWriterBegin, mpQueue->mWriterEnd);
  51. }
  52. size_t numQueuedSlots() const { return mpQueue->numQueuedSlots(); }
  53. T* operator[](size_t i) const
  54. {
  55. if (i < numRetainedSlots())
  56. {
  57. i += mpQueue->mWriterBegin.load();
  58. return mpQueue->mData[i % mpQueue->mData.size()].get();
  59. }
  60. return nullptr;
  61. }
  62. size_t numSlots() const { return mpQueue->numSlots(); }
  63. private:
  64. friend Queue;
  65. Writer(std::shared_ptr<Impl> pQueue)
  66. : mpQueue(pQueue)
  67. {
  68. }
  69. std::shared_ptr<Impl> mpQueue;
  70. };
  71. struct Reader
  72. {
  73. Reader(const Reader&) = delete;
  74. Reader& operator=(const Reader&) = delete;
  75. Reader(Reader&&) = default;
  76. Reader& operator=(Reader&&) = default;
  77. bool retainSlot()
  78. {
  79. return mpQueue->retainSlot(mpQueue->mReaderEnd, mpQueue->mWriterBegin);
  80. }
  81. void releaseSlot()
  82. {
  83. mpQueue->releaseSlot(mpQueue->mReaderBegin, mpQueue->mReaderEnd);
  84. }
  85. size_t numRetainedSlots() const
  86. {
  87. return mpQueue->numRetainedSlots(mpQueue->mReaderBegin, mpQueue->mReaderEnd);
  88. }
  89. size_t numQueuedSlots() const { return mpQueue->numQueuedSlots(); }
  90. T* operator[](size_t i) const
  91. {
  92. if (i < numRetainedSlots())
  93. {
  94. i += mpQueue->mReaderBegin.load() + 1;
  95. return mpQueue->mData[i % mpQueue->mData.size()].get();
  96. }
  97. return nullptr;
  98. }
  99. size_t numSlots() const { return mpQueue->numSlots(); }
  100. private:
  101. friend Queue;
  102. Reader(std::shared_ptr<Impl> pQueue)
  103. : mpQueue(pQueue)
  104. {
  105. }
  106. std::shared_ptr<Impl> mpQueue;
  107. };
  108. Queue(size_t numRetainedSlots, const T& t)
  109. : mpImpl{std::make_shared<Impl>(numRetainedSlots, t)}
  110. {
  111. }
  112. Writer writer() const { return {mpImpl}; }
  113. Reader reader() const { return {mpImpl}; }
  114. private:
  115. struct Impl
  116. {
  117. Impl(size_t numRetainedSlots, const T& t)
  118. : mData(numRetainedSlots + 2)
  119. , mWriterBegin(1)
  120. , mWriterEnd(1)
  121. , mReaderBegin(0)
  122. , mReaderEnd(0)
  123. {
  124. for (auto& slot : mData)
  125. {
  126. slot = std::make_unique<T>(t);
  127. }
  128. }
  129. inline size_t nextIndex(const size_t index) const
  130. {
  131. return (index + 1) % mData.size();
  132. }
  133. inline bool retainSlot(std::atomic_size_t& aEnd, std::atomic_size_t& aOtherBegin)
  134. {
  135. const auto nextEnd = nextIndex(aEnd.load());
  136. const auto otherBegin = aOtherBegin.load();
  137. if (nextEnd != otherBegin)
  138. {
  139. aEnd.store(nextEnd);
  140. return true;
  141. }
  142. return false;
  143. }
  144. inline void releaseSlot(std::atomic_size_t& aBegin, std::atomic_size_t& aEnd)
  145. {
  146. const auto begin = aBegin.load();
  147. const auto end = aEnd.load();
  148. if (begin != end)
  149. {
  150. aBegin.store(nextIndex(begin));
  151. }
  152. }
  153. inline size_t numRetainedSlots(const std::atomic_size_t& aBegin,
  154. const std::atomic_size_t& aEnd) const
  155. {
  156. const auto begin = aBegin.load();
  157. const auto end = aEnd.load();
  158. return begin > end ? end + mData.size() - begin : end - begin;
  159. }
  160. inline size_t numQueuedSlots() const
  161. {
  162. const auto writerBegin = mWriterBegin.load();
  163. const auto readerEnd = mReaderEnd.load();
  164. return writerBegin > readerEnd ? writerBegin - readerEnd - 1
  165. : writerBegin + mData.size() - readerEnd - 1;
  166. }
  167. size_t numSlots() const { return mData.size() - 2; }
  168. std::vector<std::unique_ptr<T>> mData;
  169. std::atomic_size_t mWriterBegin;
  170. std::atomic_size_t mWriterEnd;
  171. std::atomic_size_t mReaderBegin;
  172. std::atomic_size_t mReaderEnd;
  173. };
  174. std::shared_ptr<Impl> mpImpl;
  175. };
  176. } // namespace link_audio
  177. } // namespace ableton