From 1935b6a9fa190395bd81ac8164be9cd714dadcc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Thu, 16 Feb 2017 21:32:40 -0800 Subject: [PATCH] Cleanup. --- include/bx/mpscqueue.h | 66 ++++++++++++++++----------------------- include/bx/mpscqueue.inl | 67 ++++++++++++++++++++++++++++++++++++++++ tests/queue_test.cpp | 34 ++++++++++++++++++++ 3 files changed, 128 insertions(+), 39 deletions(-) create mode 100644 include/bx/mpscqueue.inl create mode 100644 tests/queue_test.cpp diff --git a/include/bx/mpscqueue.h b/include/bx/mpscqueue.h index a3071cf..15220d9 100644 --- a/include/bx/mpscqueue.h +++ b/include/bx/mpscqueue.h @@ -6,48 +6,42 @@ #ifndef BX_MPSCQUEUE_H_HEADER_GUARD #define BX_MPSCQUEUE_H_HEADER_GUARD +#include "mutex.h" #include "spscqueue.h" namespace bx { + /// template - class MpScUnboundedQueue + class MpScUnboundedQueueT { - BX_CLASS(MpScUnboundedQueue + BX_CLASS(MpScUnboundedQueueT , NO_COPY , NO_ASSIGNMENT ); public: - MpScUnboundedQueue() - { - } + /// + MpScUnboundedQueueT(); - ~MpScUnboundedQueue() - { - } + /// + ~MpScUnboundedQueueT(); - void push(Ty* _ptr) // producer only - { - MutexScope lock(m_write); - m_queue.push(_ptr); - } + /// + void push(Ty* _ptr); // producer only - Ty* peek() // consumer only - { - return m_queue.peek(); - } + /// + Ty* peek(); // consumer only - Ty* pop() // consumer only - { - return m_queue.pop(); - } + /// + Ty* pop(); // consumer only private: Mutex m_write; - SpScUnboundedQueue m_queue; + SpScUnboundedQueueT m_queue; }; + /// template class MpScUnboundedBlockingQueue { @@ -57,31 +51,25 @@ namespace bx ); public: - MpScUnboundedBlockingQueue() - { - } + /// + MpScUnboundedBlockingQueue(); - ~MpScUnboundedBlockingQueue() - { - } + /// + ~MpScUnboundedBlockingQueue(); - void push(Ty* _ptr) // producer only - { - m_queue.push(_ptr); - m_sem.post(); - } + /// + void push(Ty* _ptr); // producer only - Ty* pop() // consumer only - { - m_sem.wait(); - return m_queue.pop(); - } + /// + Ty* pop(); // consumer only private: - MpScUnboundedQueue m_queue; + MpScUnboundedQueueT m_queue; Semaphore m_sem; }; } // namespace bx +#include "mpscqueue.inl" + #endif // BX_MPSCQUEUE_H_HEADER_GUARD diff --git a/include/bx/mpscqueue.inl b/include/bx/mpscqueue.inl new file mode 100644 index 0000000..cf06152 --- /dev/null +++ b/include/bx/mpscqueue.inl @@ -0,0 +1,67 @@ +/* + * Copyright 2010-2017 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bx#license-bsd-2-clause + */ + +#ifndef BX_MPSCQUEUE_H_HEADER_GUARD +# error "Must be included from bx/mpscqueue.h!" +#endif // BX_MPSCQUEUE_H_HEADER_GUARD + +#include "spscqueue.h" + +namespace bx +{ + template + inline MpScUnboundedQueueT::MpScUnboundedQueueT() + { + } + + template + inline MpScUnboundedQueueT::~MpScUnboundedQueueT() + { + } + + template + inline void MpScUnboundedQueueT::push(Ty* _ptr) + { + MutexScope lock(m_write); + m_queue.push(_ptr); + } + + template + inline Ty* MpScUnboundedQueueT::peek() + { + return m_queue.peek(); + } + + template + inline Ty* MpScUnboundedQueueT::pop() + { + return m_queue.pop(); + } + + template + inline MpScUnboundedBlockingQueue::MpScUnboundedBlockingQueue() + { + } + + template + inline MpScUnboundedBlockingQueue::~MpScUnboundedBlockingQueue() + { + } + + template + inline void MpScUnboundedBlockingQueue::push(Ty* _ptr) + { + m_queue.push(_ptr); + m_sem.post(); + } + + template + inline Ty* MpScUnboundedBlockingQueue::pop() + { + m_sem.wait(); + return m_queue.pop(); + } + +} // namespace bx diff --git a/tests/queue_test.cpp b/tests/queue_test.cpp new file mode 100644 index 0000000..48fd270 --- /dev/null +++ b/tests/queue_test.cpp @@ -0,0 +1,34 @@ +/* + * Copyright 2010-2017 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bx#license-bsd-2-clause + */ + +#include "test.h" +#include +#include + +void* bitsToPtr(uintptr_t _ui) +{ + union { uintptr_t ui; void* ptr; } cast = { _ui }; + return cast.ptr; +} + +uintptr_t ptrToBits(void* _ptr) +{ + union { void* ptr; uintptr_t ui; } cast = { _ptr }; + return cast.ui; +} + +TEST_CASE("SpSc", "") +{ + bx::SpScUnboundedQueue queue; + queue.push(bitsToPtr(0xdeadbeef) ); + REQUIRE(0xdeadbeef == ptrToBits(queue.pop() ) ); +} + +TEST_CASE("MpSc", "") +{ + bx::MpScUnboundedQueueT queue; + queue.push(bitsToPtr(0xdeadbeef) ); + REQUIRE(0xdeadbeef == ptrToBits(queue.pop() ) ); +}