diff options
Diffstat (limited to '3rdparty/bx/include/bx/spscqueue.h')
-rw-r--r-- | 3rdparty/bx/include/bx/spscqueue.h | 204 |
1 files changed, 73 insertions, 131 deletions
diff --git a/3rdparty/bx/include/bx/spscqueue.h b/3rdparty/bx/include/bx/spscqueue.h index 3f75496f357..4d37d63b63f 100644 --- a/3rdparty/bx/include/bx/spscqueue.h +++ b/3rdparty/bx/include/bx/spscqueue.h @@ -8,83 +8,39 @@ #include "bx.h" #include "cpu.h" -#include "mutex.h" -#include "uint32_t.h" - -#include <list> +#include "semaphore.h" namespace bx { - // http://drdobbs.com/article/print?articleId=210604448&siteSectionName= - template <typename Ty> - class SpScUnboundedQueueLf + /// + class SpScUnboundedQueue { - BX_CLASS(SpScUnboundedQueueLf + BX_CLASS(SpScUnboundedQueue , NO_COPY , NO_ASSIGNMENT ); public: - SpScUnboundedQueueLf() - : m_first(new Node(NULL) ) - , m_divider(m_first) - , m_last(m_first) - { - } + /// + SpScUnboundedQueue(); - ~SpScUnboundedQueueLf() - { - while (NULL != m_first) - { - Node* node = m_first; - m_first = node->m_next; - delete node; - } - } - - void push(Ty* _ptr) // producer only - { - m_last->m_next = new Node( (void*)_ptr); - atomicExchangePtr( (void**)&m_last, m_last->m_next); - while (m_first != m_divider) - { - Node* node = m_first; - m_first = m_first->m_next; - delete node; - } - } - - Ty* peek() // consumer only - { - if (m_divider != m_last) - { - Ty* ptr = (Ty*)m_divider->m_next->m_ptr; - return ptr; - } + /// + ~SpScUnboundedQueue(); - return NULL; - } + /// + void push(void* _ptr); - Ty* pop() // consumer only - { - if (m_divider != m_last) - { - Ty* ptr = (Ty*)m_divider->m_next->m_ptr; - atomicExchangePtr( (void**)&m_divider, m_divider->m_next); - return ptr; - } + /// + void* peek(); - return NULL; - } + /// + void* pop(); private: struct Node { - Node(void* _ptr) - : m_ptr(_ptr) - , m_next(NULL) - { - } + /// + Node(void* _ptr); void* m_ptr; Node* m_next; @@ -95,69 +51,38 @@ namespace bx Node* m_last; }; -#if BX_CONFIG_SUPPORTS_THREADING + /// template<typename Ty> - class SpScUnboundedQueueMutex + class SpScUnboundedQueueT { - BX_CLASS(SpScUnboundedQueueMutex + BX_CLASS(SpScUnboundedQueueT , NO_COPY , NO_ASSIGNMENT ); public: - SpScUnboundedQueueMutex() - { - } + /// + SpScUnboundedQueueT(); - ~SpScUnboundedQueueMutex() - { - BX_CHECK(m_queue.empty(), "Queue is not empty!"); - } + /// + ~SpScUnboundedQueueT(); - void push(Ty* _item) - { - bx::LwMutexScope lock(m_mutex); - m_queue.push_back(_item); - } + /// + void push(Ty* _ptr); - Ty* peek() - { - bx::LwMutexScope lock(m_mutex); - if (!m_queue.empty() ) - { - return m_queue.front(); - } - - return NULL; - } + /// + Ty* peek(); - Ty* pop() - { - bx::LwMutexScope lock(m_mutex); - if (!m_queue.empty() ) - { - Ty* item = m_queue.front(); - m_queue.pop_front(); - return item; - } - - return NULL; - } + /// + Ty* pop(); private: - bx::LwMutex m_mutex; - std::list<Ty*> m_queue; + SpScUnboundedQueue m_queue; }; -#endif // BX_CONFIG_SUPPORTS_THREADING -#if BX_CONFIG_SPSCQUEUE_USE_MUTEX && BX_CONFIG_SUPPORTS_THREADING -# define SpScUnboundedQueue SpScUnboundedQueueMutex -#else -# define SpScUnboundedQueue SpScUnboundedQueueLf -#endif // BX_CONFIG_SPSCQUEUE_USE_MUTEX #if BX_CONFIG_SUPPORTS_THREADING - template <typename Ty> + /// class SpScBlockingUnboundedQueue { BX_CLASS(SpScBlockingUnboundedQueue @@ -166,41 +91,58 @@ namespace bx ); public: - SpScBlockingUnboundedQueue() - { - } + /// + SpScBlockingUnboundedQueue(); - ~SpScBlockingUnboundedQueue() - { - } + /// + ~SpScBlockingUnboundedQueue(); - void push(Ty* _ptr) // producer only - { - m_queue.push( (void*)_ptr); - m_count.post(); - } + /// + void push(void* _ptr); // producer only - Ty* peek() // consumer only - { - return (Ty*)m_queue.peek(); - } + /// + void* peek(); // consumer only - Ty* pop(int32_t _msecs = -1) // consumer only - { - if (m_count.wait(_msecs) ) - { - return (Ty*)m_queue.pop(); - } - - return NULL; - } + /// + void* pop(int32_t _msecs = -1); // consumer only private: Semaphore m_count; - SpScUnboundedQueue<void> m_queue; + SpScUnboundedQueue m_queue; + }; + + /// + template<typename Ty> + class SpScBlockingUnboundedQueueT + { + BX_CLASS(SpScBlockingUnboundedQueueT + , NO_COPY + , NO_ASSIGNMENT + ); + + public: + /// + SpScBlockingUnboundedQueueT(); + + /// + ~SpScBlockingUnboundedQueueT(); + + /// + void push(Ty* _ptr); // producer only + + /// + Ty* peek(); // consumer only + + /// + Ty* pop(int32_t _msecs = -1); // consumer only + + private: + SpScBlockingUnboundedQueue m_queue; }; #endif // BX_CONFIG_SUPPORTS_THREADING } // namespace bx +#include "inline/spscqueue.inl" + #endif // BX_SPSCQUEUE_H_HEADER_GUARD |