]> git.tdb.fi Git - libs/core.git/commitdiff
Refactor Thread class with pimpl to avoid exposing platform-specific parts
authorMikko Rasa <tdb@tdb.fi>
Sat, 28 May 2011 09:52:16 +0000 (12:52 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 28 May 2011 09:52:16 +0000 (12:52 +0300)
source/core/thread.cpp
source/core/thread.h

index 810b1f80485197eeb66552707897c89e42356215..b382f067a3588868e4a0638dc577eb3059593b42 100644 (file)
@@ -5,13 +5,51 @@ Copyright © 2006 Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
-#ifndef WIN32
+#ifdef WIN32
+#include <windows.h>
+#else
+#include <pthread.h>
 #include <signal.h>
 #endif
 #include "thread.h"
 
 namespace Msp {
 
+struct Thread::Private
+{
+#ifdef WIN32
+       HANDLE handle;
+#else
+       pthread_t handle;
+#endif
+
+       Private(): handle(0) { }
+
+#ifdef WIN32
+       static DWORD WINAPI main_wrapper(void *t)
+       { reinterpret_cast<Thread *>(t)->main(); return 0; }
+#else
+       static void *main_wrapper(void *t)
+       { reinterpret_cast<Thread *>(t)->main(); return 0; }
+#endif
+};
+
+
+Thread::Thread():
+       priv_(new Private),
+       launched_(false)
+{ }
+
+Thread::~Thread()
+{
+       if(launched_)
+       {
+               kill();
+               join();
+       }
+       delete priv_;
+}
+
 /**
 Waits for the thread to exit.  Calling this from the thread will cause a
 deadlock.
@@ -22,44 +60,25 @@ void Thread::join()
                return;
 
 #ifdef WIN32
-       WaitForSingleObject(thread_, INFINITE);
+       WaitForSingleObject(priv_->handle, INFINITE);
 #else
-       pthread_join(thread_, 0);
+       pthread_join(priv_->handle, 0);
 #endif
        launched_ = false;
 }
 
-/**
-Requests the thread to terminate gracefully.  Currently unimplemented on win32.
-*/
-void Thread::cancel()
-{
-#ifndef WIN32 //XXX
-       pthread_cancel(thread_);
-#endif
-}
-
 /**
 Violently terminates the thread.
 */
 void Thread::kill()
 {
 #ifdef WIN32
-       TerminateThread(thread_, 0);
+       TerminateThread(priv_->handle, 0);
 #else
-       pthread_kill(thread_, SIGKILL);
+       pthread_kill(priv_->handle, SIGKILL);
 #endif
 }
 
-Thread::~Thread()
-{
-       if(launched_)
-       {
-               kill();
-               join();
-       }
-}
-
 void Thread::launch()
 {
        if(launched_)
@@ -67,9 +86,9 @@ void Thread::launch()
 
 #ifdef WIN32
        DWORD dummy;  // Win9x needs the lpTthreadId parameter
-       thread_ = CreateThread(0, 0, &main_, this, 0, &dummy);
+       priv_->handle = CreateThread(0, 0, &Private::main_wrapper, this, 0, &dummy);
 #else
-       pthread_create(&thread_, 0, &main_, this);
+       pthread_create(&priv_->handle, 0, &Private::main_wrapper, this);
 #endif
        launched_ = true;
 }
index 92923f31bbd5718eb8816aed1e28d9e8e72dadda..59e6e060ce4b77d2e0e69b45f245e6537ca3a70b 100644 (file)
@@ -8,8 +8,6 @@ Distributed under the LGPL
 #ifndef MSP_CORE_THREAD_H_
 #define MSP_CORE_THREAD_H_
 
-#include "types.h"
-
 namespace Msp {
 
 /**
@@ -22,33 +20,24 @@ is started.
 class Thread
 {
 private:
-       ThreadHandle thread_;
-       bool         launched_;
+       struct Private;
+
+       Private *priv_;
+       bool launched_;
 
 protected:
-       Thread(): launched_(false) { }
+       Thread();
+private:
+       Thread(const Thread &);
+       Thread &operator=(const Thread &);
 public:
        virtual ~Thread();
 
        void join();
-       void cancel();
        void kill();
 protected:
        void launch();
        virtual void main() = 0;
-       void check_cancel();
-
-private:
-       static
-#ifdef WIN32
-       DWORD WINAPI
-#else
-       void *
-#endif
-       main_(void *t) { (reinterpret_cast<Thread *>(t))->main(); return 0; }
-
-       Thread(const Thread &);
-       Thread &operator=(const Thread &);
 };
 
 } // namespace Msp