Browse Source

Added thread queue.

Branimir Karadžić 8 years ago
parent
commit
8026262b84
2 changed files with 22 additions and 6 deletions
  1. 9 3
      include/bx/thread.h
  2. 13 3
      src/thread.cpp

+ 9 - 3
include/bx/thread.h

@@ -7,12 +7,12 @@
 #define BX_THREAD_H_HEADER_GUARD
 #define BX_THREAD_H_HEADER_GUARD
 
 
 #include "bx.h"
 #include "bx.h"
-#include "semaphore.h"
+#include "mpscqueue.h"
 
 
 namespace bx
 namespace bx
 {
 {
 	///
 	///
-	typedef int32_t (*ThreadFn)(void* _userData);
+	typedef int32_t (*ThreadFn)(class Thread* _self, void* _userData);
 
 
 	///
 	///
 	class Thread
 	class Thread
@@ -44,6 +44,12 @@ namespace bx
 		///
 		///
 		void setThreadName(const char* _name);
 		void setThreadName(const char* _name);
 
 
+		///
+		void push(void* _ptr);
+
+		///
+		void* pop();
+
 	private:
 	private:
 		friend struct ThreadInternal;
 		friend struct ThreadInternal;
 		int32_t entry();
 		int32_t entry();
@@ -52,7 +58,7 @@ namespace bx
 
 
 		ThreadFn  m_fn;
 		ThreadFn  m_fn;
 		void*     m_userData;
 		void*     m_userData;
-		Semaphore m_sem;
+		MpScUnboundedBlockingQueue<void> m_queue;
 		uint32_t  m_stackSize;
 		uint32_t  m_stackSize;
 		int32_t   m_exitCode;
 		int32_t   m_exitCode;
 		bool      m_running;
 		bool      m_running;

+ 13 - 3
src/thread.cpp

@@ -150,7 +150,7 @@ namespace bx
 #	error "Not implemented!"
 #	error "Not implemented!"
 #endif // BX_PLATFORM_
 #endif // BX_PLATFORM_
 
 
-		m_sem.wait();
+		m_queue.pop();
 
 
 		if (NULL != _name)
 		if (NULL != _name)
 		{
 		{
@@ -243,6 +243,16 @@ namespace bx
 #endif // BX_PLATFORM_
 #endif // BX_PLATFORM_
 	}
 	}
 
 
+	void Thread::push(void* _ptr)
+	{
+		m_queue.push(_ptr);
+	}
+
+	void* Thread::pop()
+	{
+		return m_queue.pop();
+	}
+
 	int32_t Thread::entry()
 	int32_t Thread::entry()
 	{
 	{
 #if BX_PLATFORM_WINDOWS
 #if BX_PLATFORM_WINDOWS
@@ -250,8 +260,8 @@ namespace bx
 		ti->m_threadId = ::GetCurrentThreadId();
 		ti->m_threadId = ::GetCurrentThreadId();
 #endif // BX_PLATFORM_WINDOWS
 #endif // BX_PLATFORM_WINDOWS
 
 
-		m_sem.post();
-		return m_fn(m_userData);
+		m_queue.push(NULL);
+		return m_fn(this, m_userData);
 	}
 	}
 
 
 	struct TlsDataInternal
 	struct TlsDataInternal