Bladeren bron

Use new Thread implementation by default

Daniele Bartolini 12 jaren geleden
bovenliggende
commit
ead8b5632a
4 gewijzigde bestanden met toevoegingen van 114 en 339 verwijderingen
  1. 0 1
      engine/os/linux/Thread.h
  2. 0 125
      engine/os/posix/Thread.cpp
  3. 114 46
      engine/os/posix/Thread.h
  4. 0 167
      engine/os/posix/Thread2.h

+ 0 - 1
engine/os/linux/Thread.h

@@ -27,4 +27,3 @@ OTHER DEALINGS IN THE SOFTWARE.
 #pragma once
 
 #include "../posix/Thread.h"
-#include "../posix/Thread2.h"

+ 0 - 125
engine/os/posix/Thread.cpp

@@ -1,125 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#include <stdlib.h>
-#include <string.h>
-
-#include "Thread.h"
-#include "Assert.h"
-
-namespace crown
-{
-
-//-----------------------------------------------------------------------------
-Thread::Thread(const char* name) :
-	m_name(name),
-	m_is_running(false),
-	m_is_terminating(false),
-	m_thread(0)
-{
-	memset(&m_thread, 0, sizeof(pthread_t));
-}
-
-//-----------------------------------------------------------------------------
-Thread::~Thread()
-{
-}
-
-//-----------------------------------------------------------------------------
-const char* Thread::name() const
-{
-	return m_name;
-}
-
-//-----------------------------------------------------------------------------
-void Thread::start()
-{
-	m_is_terminating = false;
-
-	// Make thread joinable
-	pthread_attr_t attr;
-	pthread_attr_init(&attr);
-	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
-
-	// Create thread
-	int rc = pthread_create(&m_thread, &attr, Thread::background_proc, (void*) this);
-	CE_ASSERT(rc == 0, "Failed to create the thread '%s': errno: %d", m_name, rc);
-
-	// Free attr memory
-	pthread_attr_destroy(&attr);
-
-	m_is_running = true;
-}
-
-//-----------------------------------------------------------------------------
-bool Thread::is_running() const
-{
-	return m_is_running;
-}
-
-//-----------------------------------------------------------------------------
-bool Thread::is_terminating() const
-{
-	return m_is_terminating;
-}
-
-//-----------------------------------------------------------------------------
-void Thread::stop()
-{
-	m_is_terminating = true;
-}
-
-//-----------------------------------------------------------------------------
-int32_t Thread::run()
-{
-	return 0;
-}
-
-//-----------------------------------------------------------------------------
-void* Thread::background_proc(void* thiz)
-{
-	Thread* thread = ((Thread*) thiz);
-
-	thread->run();
-
-	thread->m_is_running = false;
-
-	return NULL;
-}
-
-//-----------------------------------------------------------------------------
-void Thread::join()
-{
-	pthread_join(m_thread, NULL);
-}
-
-//-----------------------------------------------------------------------------
-void Thread::detach()
-{
-	pthread_detach(m_thread);
-}
-
-} // namespace crown

+ 114 - 46
engine/os/posix/Thread.h

@@ -26,74 +26,142 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
-#include <pthread.h>
+#include <cstring>
 
+#include "Assert.h"
 #include "Types.h"
+#include "Semaphore.h"
+#include "Log.h"
 
 namespace crown
 {
 
-typedef void* (*ThreadFunction)(void*);
+typedef int32_t (*OsThreadFunction)(void*);
 
-class Thread
+class OsThread
 {
 public:
 
-	/// Constructs the thread and gives it a @a name.
-	/// @note
-	/// The actual OS thread creation and execution is
-	/// deferred to the first call to Thread::start().
-					Thread(const char* name);
+						OsThread(const char* name);
+						~OsThread();
 
-	/// Does not stop the thread. The user must call
-	/// Thread::stop() to effectively stop the thread. 
-	virtual			~Thread();
+	void				start(OsThreadFunction func, void* data = NULL, size_t stack_size = 0);
+	void				stop();
 
-	/// Returns the name of the thread.
-	const char*		name() const;
+	bool				is_running();
 
-	void			join();
-	void			detach();
+private:
 
-	/// Returns whether the thread is currently running.
-	bool			is_running() const;
+	int32_t				run();
 
-	/// Returns whether the thread is being asked to stop running.
-	/// @note
-	/// The implementer tipically polls this function to
-	/// determine whether to stop the execution or not.
-	bool			is_terminating() const;
+	static void* 		thread_proc(void* arg);
 
-	/// Starts the execution of the thread.
-	/// The function creates the OS thread and starts
-	/// its execution.
-	void			start();
+private:
 
-	/// Stops the execution of the thread if it is running.
-	/// The function releases the OS thread causing its
-	/// termination.
-	void			stop();
+	const char* 		m_name;
 
-	/// Executes in background when the thead is running.
-	/// The thread has to be started with Thread::start()
-	virtual int32_t	run();
+	pthread_t			m_handle;
+	OsThreadFunction 	m_function;
+	void*				m_data;
+	Semaphore			m_sem;
+	size_t 				m_stack_size;
 
-private:
+	bool				m_is_running :1;
+};
 
-	static void*	background_proc(void* thiz);
+//-----------------------------------------------------------------------------
+inline OsThread::OsThread(const char* name) :
+	m_name(name),
+	m_handle(0),
+	m_function(NULL),
+	m_data(NULL),
+	m_stack_size(0),
+	m_is_running(false)
+{
+	memset(&m_handle, 0, sizeof(pthread_t));
+}
 
-private:
+//-----------------------------------------------------------------------------
+inline OsThread::~OsThread()
+{
+}
 
-	const char*		m_name;
-	bool			m_is_running;
-	bool			m_is_terminating;
-	pthread_t		m_thread;
+//-----------------------------------------------------------------------------
+inline void OsThread::start(OsThreadFunction func, void* data, size_t stack_size)
+{
+	CE_ASSERT(!m_is_running, "Thread is already running");
+	CE_ASSERT(func != NULL, "Function must be != NULL");
 
-private:
+	m_function = func;
+	m_data = data;
+	m_stack_size = stack_size;
+
+	pthread_attr_t attr;
+	int32_t result = pthread_attr_init(&attr);
+	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
+
+
+	CE_ASSERT(result == 0, "pthread_attr_init failed. errno: %d", result);
+
+	if (m_stack_size != 0)
+	{
+		result = pthread_attr_setstacksize(&attr, m_stack_size);
+		CE_ASSERT(result == 0, "pthread_attr_setstacksize failed. errno: %d", result);
+	}
+
+	result = pthread_create(&m_handle, &attr, thread_proc, this);
+	CE_ASSERT(result == 0, "pthread_create failed. errno: %d", result);
+
+	// Free attr memory
+	result = pthread_attr_destroy(&attr);
+	CE_ASSERT(result == 0, "pthread_attr_destroy failed. errno: %d", result);
+
+	m_is_running = true;
+
+	m_sem.wait();
+}
+
+//-----------------------------------------------------------------------------
+inline void OsThread::stop()
+{
+	CE_ASSERT(m_is_running, "Thread is not running");
+	
+	m_is_running = false;
+
+	int32_t result = pthread_join(m_handle, NULL);
+
+	CE_ASSERT(result == 0, "Thread join failed. errno: %d", result);
+
+	m_handle = 0;
+}
+
+//-----------------------------------------------------------------------------
+inline bool OsThread::is_running()
+{
+	return m_is_running;
+}
+
+//-----------------------------------------------------------------------------
+inline int32_t OsThread::run()
+{
+	m_sem.post();
+	
+	return m_function(m_data);
+}
+
+//-----------------------------------------------------------------------------
+inline void* OsThread::thread_proc(void* arg)
+{
+	OsThread* thread = (OsThread*)arg;
+
+	int32_t result = thread->run();
+
+	CE_ASSERT(result == 0, "Function failed");
+
+	thread->m_is_running = false;
+
+	return NULL;
+}
 
-	// Disable copying
-					Thread(const Thread&);
-	Thread&			operator=(const Thread&);
-};
 
-} // namespace crown
+} // namespace crown

+ 0 - 167
engine/os/posix/Thread2.h

@@ -1,167 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#pragma once
-
-#include <cstring>
-
-#include "Assert.h"
-#include "Types.h"
-#include "Semaphore.h"
-#include "Log.h"
-
-namespace crown
-{
-
-typedef int32_t (*OsThreadFunction)(void*);
-
-class OsThread
-{
-public:
-
-						OsThread(const char* name);
-						~OsThread();
-
-	void				start(OsThreadFunction func, void* data = NULL, size_t stack_size = 0);
-	void				stop();
-
-	bool				is_running();
-
-private:
-
-	int32_t				run();
-
-	static void* 		thread_proc(void* arg);
-
-private:
-
-	const char* 		m_name;
-
-	pthread_t			m_handle;
-	OsThreadFunction 	m_function;
-	void*				m_data;
-	Semaphore			m_sem;
-	size_t 				m_stack_size;
-
-	bool				m_is_running :1;
-};
-
-//-----------------------------------------------------------------------------
-inline OsThread::OsThread(const char* name) :
-	m_name(name),
-	m_handle(0),
-	m_function(NULL),
-	m_data(NULL),
-	m_stack_size(0),
-	m_is_running(false)
-{
-	memset(&m_handle, 0, sizeof(pthread_t));
-}
-
-//-----------------------------------------------------------------------------
-inline OsThread::~OsThread()
-{
-}
-
-//-----------------------------------------------------------------------------
-inline void OsThread::start(OsThreadFunction func, void* data, size_t stack_size)
-{
-	CE_ASSERT(!m_is_running, "Thread is already running");
-	CE_ASSERT(func != NULL, "Function must be != NULL");
-
-	m_function = func;
-	m_data = data;
-	m_stack_size = stack_size;
-
-	pthread_attr_t attr;
-	int32_t result = pthread_attr_init(&attr);
-	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
-
-
-	CE_ASSERT(result == 0, "pthread_attr_init failed. errno: %d", result);
-
-	if (m_stack_size != 0)
-	{
-		result = pthread_attr_setstacksize(&attr, m_stack_size);
-		CE_ASSERT(result == 0, "pthread_attr_setstacksize failed. errno: %d", result);
-	}
-
-	result = pthread_create(&m_handle, &attr, thread_proc, this);
-	CE_ASSERT(result == 0, "pthread_create failed. errno: %d", result);
-
-	// Free attr memory
-	result = pthread_attr_destroy(&attr);
-	CE_ASSERT(result == 0, "pthread_attr_destroy failed. errno: %d", result);
-
-	m_is_running = true;
-
-	// m_sem.wait();
-}
-
-//-----------------------------------------------------------------------------
-inline void OsThread::stop()
-{
-	CE_ASSERT(m_is_running, "Thread is not running");
-	
-	m_is_running = false;
-
-	int32_t result = pthread_join(m_handle, NULL);
-
-	CE_ASSERT(result == 0, "Thread join failed. errno: %d", result);
-
-	m_handle = 0;
-}
-
-//-----------------------------------------------------------------------------
-inline bool OsThread::is_running()
-{
-	return m_is_running;
-}
-
-//-----------------------------------------------------------------------------
-inline int32_t OsThread::run()
-{
-	// m_sem.post();
-	
-	return m_function(m_data);
-}
-
-//-----------------------------------------------------------------------------
-inline void* OsThread::thread_proc(void* arg)
-{
-	OsThread* thread = (OsThread*)arg;
-
-	int32_t result = thread->run();
-
-	CE_ASSERT(result == 0, "Function failed");
-
-	thread->m_is_running = false;
-
-	return NULL;
-}
-
-
-} // namespace crown