Audio plugin host https://kx.studio/carla
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.

64 lines
1.3KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. SafeQueue.h - Yet Another Lockless Ringbuffer
  4. Copyright (C) 2016 Mark McCurry
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. */
  10. #ifndef SAFEQUEUE_H
  11. #define SAFEQUEUE_H
  12. #include <cstdlib>
  13. #include "ZynSema.h"
  14. #include <pthread.h>
  15. namespace zyncarla {
  16. /**
  17. * C++ thread safe lockless queue
  18. * Based off of jack's ringbuffer*/
  19. template<class T>
  20. class SafeQueue
  21. {
  22. public:
  23. SafeQueue(size_t maxlen);
  24. ~SafeQueue();
  25. /**Return read size*/
  26. unsigned int size() const;
  27. /**Returns 0 for normal
  28. * Returns -1 on error*/
  29. int push(const T &in);
  30. int peak(T &out) const;
  31. int pop(T &out);
  32. //clears reading space
  33. void clear();
  34. private:
  35. unsigned int wSpace() const;
  36. unsigned int rSpace() const;
  37. //write space
  38. mutable ZynSema w_space;
  39. //read space
  40. mutable ZynSema r_space;
  41. //next writing spot
  42. size_t writePtr;
  43. //next reading spot
  44. size_t readPtr;
  45. const size_t bufSize;
  46. T *buffer;
  47. };
  48. }
  49. #include "SafeQueue.cpp"
  50. #endif