mpscqueue.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright 2010-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #ifndef BX_MPSCQUEUE_H_HEADER_GUARD
  6. #define BX_MPSCQUEUE_H_HEADER_GUARD
  7. #include "spscqueue.h"
  8. namespace bx
  9. {
  10. template <typename Ty>
  11. class MpScUnboundedQueue
  12. {
  13. BX_CLASS(MpScUnboundedQueue
  14. , NO_COPY
  15. , NO_ASSIGNMENT
  16. );
  17. public:
  18. MpScUnboundedQueue()
  19. {
  20. }
  21. ~MpScUnboundedQueue()
  22. {
  23. }
  24. void push(Ty* _ptr) // producer only
  25. {
  26. LwMutexScope $(m_write);
  27. m_queue.push(_ptr);
  28. }
  29. Ty* peek() // consumer only
  30. {
  31. return m_queue.peek();
  32. }
  33. Ty* pop() // consumer only
  34. {
  35. return m_queue.pop();
  36. }
  37. private:
  38. LwMutex m_write;
  39. SpScUnboundedQueue<Ty> m_queue;
  40. };
  41. template <typename Ty>
  42. class MpScUnboundedBlockingQueue
  43. {
  44. BX_CLASS(MpScUnboundedBlockingQueue
  45. , NO_COPY
  46. , NO_ASSIGNMENT
  47. );
  48. public:
  49. MpScUnboundedBlockingQueue()
  50. {
  51. }
  52. ~MpScUnboundedBlockingQueue()
  53. {
  54. }
  55. void push(Ty* _ptr) // producer only
  56. {
  57. m_queue.push(_ptr);
  58. m_sem.post();
  59. }
  60. Ty* pop() // consumer only
  61. {
  62. m_sem.wait();
  63. return m_queue.pop();
  64. }
  65. private:
  66. MpScUnboundedQueue<Ty> m_queue;
  67. Semaphore m_sem;
  68. };
  69. } // namespace bx
  70. #endif // BX_MPSCQUEUE_H_HEADER_GUARD