X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fthread.cpp;h=721e8cfef51f2ff772c304ff9ece65554c3f033a;hp=6c542e6a6712d4e19323fbd779c401abd7ea4215;hb=a4596d1c2f627e4568eb6c01d81b6e45f488715a;hpb=a23176e6d3399bbeb75cf6a173543bf5d5f85187 diff --git a/source/core/thread.cpp b/source/core/thread.cpp index 6c542e6..721e8cf 100644 --- a/source/core/thread.cpp +++ b/source/core/thread.cpp @@ -1,43 +1,14 @@ -#ifdef WIN32 -#include -#else -#include -#include -#endif #include #include "thread.h" +#include "thread_private.h" using namespace std; namespace Msp { -struct Thread::Private -{ -#ifdef WIN32 - HANDLE handle; -#else - pthread_t handle; -#endif - - Private(): handle(0) { } - -#ifdef WIN32 - static DWORD WINAPI -#else - static void * -#endif - main_wrapper(void *a) - { - Thread *t = reinterpret_cast(a); - t->main(); - t->state_ = FINISHED; - return 0; - } -}; - - -Thread::Thread(): +Thread::Thread(const string &name): priv_(new Private), + name_(name), state_(PENDING) { } @@ -53,11 +24,7 @@ void Thread::join() if(state_>=JOINED) return; -#ifdef WIN32 - WaitForSingleObject(priv_->handle, INFINITE); -#else - pthread_join(priv_->handle, 0); -#endif + platform_join(); state_ = JOINED; } @@ -66,11 +33,7 @@ void Thread::kill() if(state_!=RUNNING) return; -#ifdef WIN32 - TerminateThread(priv_->handle, 0); -#else - pthread_kill(priv_->handle, SIGKILL); -#endif + platform_kill(); state_ = KILLED; } @@ -79,13 +42,17 @@ void Thread::launch() if(state_>=RUNNING) throw logic_error("already launched"); -#ifdef WIN32 - DWORD dummy; // Win9x needs the lpTthreadId parameter - priv_->handle = CreateThread(0, 0, &Private::main_wrapper, this, 0, &dummy); -#else - pthread_create(&priv_->handle, 0, &Private::main_wrapper, this); -#endif + platform_launch(); state_ = RUNNING; } +ThreadReturn THREAD_CALL Thread::Private::main_wrapper(void *arg) +{ + Thread *thread = reinterpret_cast(arg); + thread->platform_setname(); + thread->main(); + thread->state_ = FINISHED; + return 0; +} + } // namespace Msp