spscqueue.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright 2010-2018 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #ifndef BX_SPSCQUEUE_H_HEADER_GUARD
  6. #define BX_SPSCQUEUE_H_HEADER_GUARD
  7. #include "allocator.h"
  8. #include "cpu.h"
  9. #include "semaphore.h"
  10. namespace bx
  11. {
  12. ///
  13. class SpScUnboundedQueue
  14. {
  15. BX_CLASS(SpScUnboundedQueue
  16. , NO_COPY
  17. , NO_ASSIGNMENT
  18. );
  19. public:
  20. ///
  21. SpScUnboundedQueue(AllocatorI* _allocator);
  22. ///
  23. ~SpScUnboundedQueue();
  24. ///
  25. void push(void* _ptr);
  26. ///
  27. void* peek();
  28. ///
  29. void* pop();
  30. private:
  31. struct Node
  32. {
  33. ///
  34. Node(void* _ptr);
  35. void* m_ptr;
  36. Node* m_next;
  37. };
  38. AllocatorI* m_allocator;
  39. Node* m_first;
  40. Node* m_divider;
  41. Node* m_last;
  42. };
  43. ///
  44. template<typename Ty>
  45. class SpScUnboundedQueueT
  46. {
  47. BX_CLASS(SpScUnboundedQueueT
  48. , NO_COPY
  49. , NO_ASSIGNMENT
  50. );
  51. public:
  52. ///
  53. SpScUnboundedQueueT(AllocatorI* _allocator);
  54. ///
  55. ~SpScUnboundedQueueT();
  56. ///
  57. void push(Ty* _ptr);
  58. ///
  59. Ty* peek();
  60. ///
  61. Ty* pop();
  62. private:
  63. SpScUnboundedQueue m_queue;
  64. };
  65. #if BX_CONFIG_SUPPORTS_THREADING
  66. ///
  67. class SpScBlockingUnboundedQueue
  68. {
  69. BX_CLASS(SpScBlockingUnboundedQueue
  70. , NO_COPY
  71. , NO_ASSIGNMENT
  72. );
  73. public:
  74. ///
  75. SpScBlockingUnboundedQueue(AllocatorI* _allocator);
  76. ///
  77. ~SpScBlockingUnboundedQueue();
  78. ///
  79. void push(void* _ptr); // producer only
  80. ///
  81. void* peek(); // consumer only
  82. ///
  83. void* pop(int32_t _msecs = -1); // consumer only
  84. private:
  85. Semaphore m_count;
  86. SpScUnboundedQueue m_queue;
  87. };
  88. ///
  89. template<typename Ty>
  90. class SpScBlockingUnboundedQueueT
  91. {
  92. BX_CLASS(SpScBlockingUnboundedQueueT
  93. , NO_COPY
  94. , NO_ASSIGNMENT
  95. );
  96. public:
  97. ///
  98. SpScBlockingUnboundedQueueT(AllocatorI* _allocator);
  99. ///
  100. ~SpScBlockingUnboundedQueueT();
  101. ///
  102. void push(Ty* _ptr); // producer only
  103. ///
  104. Ty* peek(); // consumer only
  105. ///
  106. Ty* pop(int32_t _msecs = -1); // consumer only
  107. private:
  108. SpScBlockingUnboundedQueue m_queue;
  109. };
  110. #endif // BX_CONFIG_SUPPORTS_THREADING
  111. } // namespace bx
  112. #include "inline/spscqueue.inl"
  113. #endif // BX_SPSCQUEUE_H_HEADER_GUARD