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.

47 lines
889B

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