LocklessRingBuffer.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * LocklessRingBuffer.h - LMMS wrapper for a lockless ringbuffer library
  3. *
  4. * Copyright (c) 2019 Martin Pavelek <he29/dot/HS/at/gmail/dot/com>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #ifndef LOCKLESSRINGBUFFER_H
  25. #define LOCKLESSRINGBUFFER_H
  26. #include <QMutex>
  27. #include <QWaitCondition>
  28. #include "lmms_basics.h"
  29. #include "../src/3rdparty/ringbuffer/include/ringbuffer/ringbuffer.h"
  30. //! A convenience layer for a realtime-safe and thread-safe multi-reader ringbuffer
  31. template <class T>
  32. class LocklessRingBuffer
  33. {
  34. template<class _T>
  35. friend class LocklessRingBufferReader;
  36. public:
  37. LocklessRingBuffer(std::size_t sz) : m_buffer(sz)
  38. {
  39. m_buffer.touch(); // reserve storage space before realtime operation starts
  40. }
  41. ~LocklessRingBuffer() {};
  42. std::size_t capacity() const {return m_buffer.maximum_eventual_write_space();}
  43. std::size_t free() const {return m_buffer.write_space();}
  44. void wakeAll() {m_notifier.wakeAll();}
  45. std::size_t write(const sampleFrame *src, std::size_t cnt, bool notify = false)
  46. {
  47. std::size_t written = LocklessRingBuffer<T>::m_buffer.write(src, cnt);
  48. // Let all waiting readers know new data are available.
  49. if (notify) {LocklessRingBuffer<T>::m_notifier.wakeAll();}
  50. return written;
  51. }
  52. protected:
  53. ringbuffer_t<T> m_buffer;
  54. QWaitCondition m_notifier;
  55. };
  56. //! Wrapper for lockless ringbuffer reader
  57. template <class T>
  58. class LocklessRingBufferReader : public ringbuffer_reader_t<T>
  59. {
  60. public:
  61. LocklessRingBufferReader(LocklessRingBuffer<T> &rb) :
  62. ringbuffer_reader_t<T>(rb.m_buffer),
  63. m_notifier(&rb.m_notifier) {};
  64. bool empty() const {return !this->read_space();}
  65. void waitForData()
  66. {
  67. QMutex useless_lock;
  68. useless_lock.lock();
  69. m_notifier->wait(&useless_lock);
  70. useless_lock.unlock();
  71. }
  72. private:
  73. QWaitCondition *m_notifier;
  74. };
  75. #endif //LOCKLESSRINGBUFFER_H