Przeglądaj źródła

Rewrite POSIX Thread class to be more user-friendly and to allow more control on its execution

Daniele Bartolini 12 lat temu
rodzic
commit
6da0a96528
2 zmienionych plików z 107 dodań i 20 usunięć
  1. 59 13
      engine/os/posix/Thread.cpp
  2. 48 7
      engine/os/posix/Thread.h

+ 59 - 13
engine/os/posix/Thread.cpp

@@ -28,17 +28,36 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include <string.h>
 
 #include "Thread.h"
+#include "Assert.h"
 
 namespace crown
 {
-namespace os
-{
 
 //-----------------------------------------------------------------------------
-Thread::Thread(os::ThreadFunction f, void* params, const char* name) :
-	m_name(name)
+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;
@@ -46,21 +65,49 @@ Thread::Thread(os::ThreadFunction f, void* params, const char* name) :
 	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
 
 	// Create thread
-	int rc = pthread_create(&m_thread, &attr, f, (void*)params);
-
-	if (rc != 0)
-	{
-		os::printf("Unable to create the thread '%s' Error code: %d\n", name, rc);
-		exit(-1);
-	}
+	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;
 }
 
 //-----------------------------------------------------------------------------
-Thread::~Thread()
+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;
 }
 
 //-----------------------------------------------------------------------------
@@ -75,5 +122,4 @@ void Thread::detach()
 	pthread_detach(m_thread);
 }
 
-} // namespace os
 } // namespace crown

+ 48 - 7
engine/os/posix/Thread.h

@@ -29,12 +29,9 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include <pthread.h>
 
 #include "Types.h"
-#include "OS.h"
 
 namespace crown
 {
-namespace os
-{
 
 typedef void* (*ThreadFunction)(void*);
 
@@ -42,17 +39,61 @@ class Thread
 {
 public:
 
-					Thread(os::ThreadFunction f, void* params, const char* name);
-					~Thread();
+	/// 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);
+
+	/// Does not stop the thread. The user must call
+	/// Thread::stop() to effectively stop the thread. 
+	virtual			~Thread();
+
+	/// Returns the name of the thread.
+	const char*		name() const;
 
 	void			join();
 	void			detach();
 
+	/// Returns whether the thread is currently running.
+	bool			is_running() const;
+
+	/// 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;
+
+	/// Starts the execution of the thread.
+	/// The function creates the OS thread and starts
+	/// its execution.
+	void			start();
+
+	/// Stops the execution of the thread if it is running.
+	/// The function releases the OS thread causing its
+	/// termination.
+	void			stop();
+
+	/// Executes in background when the thead is running.
+	/// The thread has to be started with Thread::start()
+	virtual int32_t	run();
+
+private:
+
+	static void*	background_proc(void* thiz);
+
 private:
 
-	pthread_t		m_thread;
 	const char*		m_name;
+	bool			m_is_running;
+	bool			m_is_terminating;
+	pthread_t		m_thread;
+
+private:
+
+	// Disable copying
+					Thread(const Thread&);
+	Thread&			operator=(const Thread&);
 };
 
-} // namespace os
 } // namespace crown