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 944B

10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #ifndef SAFEQUEUE_H
  2. #define SAFEQUEUE_H
  3. #include <cstdlib>
  4. #include "ZynSema.h"
  5. #include <pthread.h>
  6. /**
  7. * C++ thread safe lockless queue
  8. * Based off of jack's ringbuffer*/
  9. template<class T>
  10. class SafeQueue
  11. {
  12. public:
  13. SafeQueue(size_t maxlen);
  14. ~SafeQueue();
  15. /**Return read size*/
  16. unsigned int size() const;
  17. /**Returns 0 for normal
  18. * Returns -1 on error*/
  19. int push(const T &in);
  20. int peak(T &out) const;
  21. int pop(T &out);
  22. //clears reading space
  23. void clear();
  24. private:
  25. unsigned int wSpace() const;
  26. unsigned int rSpace() const;
  27. //write space
  28. mutable ZynSema w_space;
  29. //read space
  30. mutable ZynSema r_space;
  31. //next writing spot
  32. size_t writePtr;
  33. //next reading spot
  34. size_t readPtr;
  35. const size_t bufSize;
  36. T *buffer;
  37. };
  38. #include "SafeQueue.cpp"
  39. #endif