X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fthread.cpp;h=e860d6758f9e048f070b67855e7d7810456e7c48;hp=6c542e6a6712d4e19323fbd779c401abd7ea4215;hb=41363aed34382386f915f17c1a961750b4fdcb14;hpb=a23176e6d3399bbeb75cf6a173543bf5d5f85187 diff --git a/source/core/thread.cpp b/source/core/thread.cpp index 6c542e6..e860d67 100644 --- a/source/core/thread.cpp +++ b/source/core/thread.cpp @@ -1,91 +1,57 @@ -#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(): - priv_(new Private), - state_(PENDING) +Thread::Thread(const string &name): + _priv(new Private), + _name(name) { } Thread::~Thread() { kill(); join(); - delete priv_; + delete _priv; } void Thread::join() { - if(state_>=JOINED) + if(_state>=JOINED) return; -#ifdef WIN32 - WaitForSingleObject(priv_->handle, INFINITE); -#else - pthread_join(priv_->handle, 0); -#endif - state_ = JOINED; + platform_join(); + _state = JOINED; } void Thread::kill() { - if(state_!=RUNNING) + if(_state!=RUNNING) return; -#ifdef WIN32 - TerminateThread(priv_->handle, 0); -#else - pthread_kill(priv_->handle, SIGKILL); -#endif - state_ = KILLED; + platform_kill(); + _state = KILLED; } void Thread::launch() { - if(state_>=RUNNING) + 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 - state_ = RUNNING; + 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 nullptr; } } // namespace Msp