TripleBuffer.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Copyright 2022, 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 <array>
  21. #include <atomic>
  22. #include <cassert>
  23. #include <cstdint>
  24. #include <optional>
  25. namespace ableton
  26. {
  27. namespace util
  28. {
  29. template <typename T>
  30. struct TripleBuffer
  31. {
  32. public:
  33. TripleBuffer()
  34. : mBuffers{{{}, {}, {}}}
  35. {
  36. assert(mState.is_lock_free());
  37. }
  38. explicit TripleBuffer(const T& initial)
  39. : mBuffers{{initial, initial, initial}}
  40. {
  41. assert(mState.is_lock_free());
  42. }
  43. TripleBuffer(const TripleBuffer&) = delete;
  44. TripleBuffer& operator=(const TripleBuffer&) = delete;
  45. T read() noexcept
  46. {
  47. loadReadBuffer();
  48. return mBuffers[mReadIndex];
  49. }
  50. std::optional<T> readNew()
  51. {
  52. if (loadReadBuffer())
  53. {
  54. return std::optional<T>(mBuffers[mReadIndex]);
  55. }
  56. return std::nullopt;
  57. }
  58. template <typename U>
  59. void write(U&& value)
  60. {
  61. mBuffers[mWriteIndex] = std::forward<U>(value);
  62. const auto prevState =
  63. mState.exchange(makeState(mWriteIndex, true), std::memory_order_acq_rel);
  64. mWriteIndex = backIndex(prevState);
  65. }
  66. private:
  67. bool loadReadBuffer()
  68. {
  69. auto state = mState.load(std::memory_order_acquire);
  70. auto isNew = isNewWrite(state);
  71. if (isNew)
  72. {
  73. const auto prevState =
  74. mState.exchange(makeState(mReadIndex, false), std::memory_order_acq_rel);
  75. mReadIndex = backIndex(prevState);
  76. }
  77. return isNew;
  78. }
  79. using BackingState = uint32_t;
  80. static constexpr bool isNewWrite(const BackingState state)
  81. {
  82. return (state & 0x0000FFFFu) != 0;
  83. }
  84. static constexpr uint32_t backIndex(const BackingState state) { return state >> 16; }
  85. static constexpr BackingState makeState(const uint32_t backBufferIndex,
  86. const bool isWrite)
  87. {
  88. return (backBufferIndex << 16) | uint32_t(isWrite);
  89. }
  90. std::atomic<BackingState> mState{makeState(1u, false)}; // Reader and writer
  91. uint32_t mReadIndex = 0u; // Reader only
  92. uint32_t mWriteIndex = 2u; // Writer only
  93. std::array<T, 3> mBuffers{};
  94. };
  95. } // namespace util
  96. } // namespace ableton