From 8026262b84d9f6fe48056f1a766296a08ff96ca9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Sat, 4 Nov 2017 15:17:00 -0700 Subject: [PATCH] Added thread queue. --- include/bx/thread.h | 12 +++++++++--- src/thread.cpp | 16 +++++++++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/include/bx/thread.h b/include/bx/thread.h index 72a072e..6de798c 100644 --- a/include/bx/thread.h +++ b/include/bx/thread.h @@ -7,12 +7,12 @@ #define BX_THREAD_H_HEADER_GUARD #include "bx.h" -#include "semaphore.h" +#include "mpscqueue.h" namespace bx { /// - typedef int32_t (*ThreadFn)(void* _userData); + typedef int32_t (*ThreadFn)(class Thread* _self, void* _userData); /// class Thread @@ -44,6 +44,12 @@ namespace bx /// void setThreadName(const char* _name); + /// + void push(void* _ptr); + + /// + void* pop(); + private: friend struct ThreadInternal; int32_t entry(); @@ -52,7 +58,7 @@ namespace bx ThreadFn m_fn; void* m_userData; - Semaphore m_sem; + MpScUnboundedBlockingQueue m_queue; uint32_t m_stackSize; int32_t m_exitCode; bool m_running; diff --git a/src/thread.cpp b/src/thread.cpp index e99a240..863cf37 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -150,7 +150,7 @@ namespace bx # error "Not implemented!" #endif // BX_PLATFORM_ - m_sem.wait(); + m_queue.pop(); if (NULL != _name) { @@ -243,6 +243,16 @@ namespace bx #endif // BX_PLATFORM_ } + void Thread::push(void* _ptr) + { + m_queue.push(_ptr); + } + + void* Thread::pop() + { + return m_queue.pop(); + } + int32_t Thread::entry() { #if BX_PLATFORM_WINDOWS @@ -250,8 +260,8 @@ namespace bx ti->m_threadId = ::GetCurrentThreadId(); #endif // BX_PLATFORM_WINDOWS - m_sem.post(); - return m_fn(m_userData); + m_queue.push(NULL); + return m_fn(this, m_userData); } struct TlsDataInternal