LocklessRingBuffer.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 "../src/3rdparty/ringbuffer/include/ringbuffer/ringbuffer.h"
  29. //! A convenience layer for a realtime-safe and thread-safe multi-reader ringbuffer
  30. template <class T>
  31. class LocklessRingBuffer
  32. {
  33. template<class _T>
  34. friend class LocklessRingBufferReader;
  35. public:
  36. LocklessRingBuffer(std::size_t sz) : m_buffer(sz)
  37. {
  38. m_buffer.touch(); // reserve storage space before realtime operation starts
  39. }
  40. ~LocklessRingBuffer() {};
  41. std::size_t capacity() const {return m_buffer.maximum_eventual_write_space();}
  42. std::size_t free() const {return m_buffer.write_space();}
  43. void wakeAll() {m_notifier.wakeAll();}
  44. std::size_t write(const sampleFrame *src, std::size_t cnt, bool notify = false)
  45. {
  46. std::size_t written = LocklessRingBuffer<T>::m_buffer.write(src, cnt);
  47. // Let all waiting readers know new data are available.
  48. if (notify) {LocklessRingBuffer<T>::m_notifier.wakeAll();}
  49. return written;
  50. }
  51. protected:
  52. ringbuffer_t<T> m_buffer;
  53. QWaitCondition m_notifier;
  54. };
  55. //! Wrapper for lockless ringbuffer reader
  56. template <class T>
  57. class LocklessRingBufferReader : public ringbuffer_reader_t<T>
  58. {
  59. public:
  60. LocklessRingBufferReader(LocklessRingBuffer<T> &rb) :
  61. ringbuffer_reader_t<T>(rb.m_buffer),
  62. m_notifier(&rb.m_notifier) {};
  63. bool empty() const {return !this->read_space();}
  64. void waitForData()
  65. {
  66. QMutex useless_lock;
  67. useless_lock.lock();
  68. m_notifier->wait(&useless_lock);
  69. useless_lock.unlock();
  70. }
  71. private:
  72. QWaitCondition *m_notifier;
  73. };
  74. #endif //LOCKLESSRINGBUFFER_H