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.

SafeQueue.h 1.3KB

10 years ago
10 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. /**
  16. * C++ thread safe lockless queue
  17. * Based off of jack's ringbuffer*/
  18. template<class T>
  19. class SafeQueue
  20. {
  21. public:
  22. SafeQueue(size_t maxlen);
  23. ~SafeQueue();
  24. /**Return read size*/
  25. unsigned int size() const;
  26. /**Returns 0 for normal
  27. * Returns -1 on error*/
  28. int push(const T &in);
  29. int peak(T &out) const;
  30. int pop(T &out);
  31. //clears reading space
  32. void clear();
  33. private:
  34. unsigned int wSpace() const;
  35. unsigned int rSpace() const;
  36. //write space
  37. mutable ZynSema w_space;
  38. //read space
  39. mutable ZynSema r_space;
  40. //next writing spot
  41. size_t writePtr;
  42. //next reading spot
  43. size_t readPtr;
  44. const size_t bufSize;
  45. T *buffer;
  46. };
  47. #include "SafeQueue.cpp"
  48. #endif