X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fthread.cpp;h=6c542e6a6712d4e19323fbd779c401abd7ea4215;hp=a8c80b664ce47aa126e2089dd691abbaff941696;hb=a23176e6d3399bbeb75cf6a173543bf5d5f85187;hpb=967785734be5c3fc6f75da122c2d93ebbb338271 diff --git a/source/core/thread.cpp b/source/core/thread.cpp index a8c80b6..6c542e6 100644 --- a/source/core/thread.cpp +++ b/source/core/thread.cpp @@ -4,8 +4,11 @@ #include #include #endif +#include #include "thread.h" +using namespace std; + namespace Msp { struct Thread::Private @@ -19,37 +22,35 @@ struct Thread::Private Private(): handle(0) { } #ifdef WIN32 - static DWORD WINAPI main_wrapper(void *t) - { reinterpret_cast(t)->main(); return 0; } + static DWORD WINAPI #else - static void *main_wrapper(void *t) - { reinterpret_cast(t)->main(); return 0; } + static void * #endif + main_wrapper(void *a) + { + Thread *t = reinterpret_cast(a); + t->main(); + t->state_ = FINISHED; + return 0; + } }; Thread::Thread(): priv_(new Private), - launched_(false) + state_(PENDING) { } Thread::~Thread() { - if(launched_) - { - kill(); - join(); - } + kill(); + join(); delete priv_; } -/** -Waits for the thread to exit. Calling this from the thread will cause a -deadlock. -*/ void Thread::join() { - if(!launched_) + if(state_>=JOINED) return; #ifdef WIN32 @@ -57,25 +58,26 @@ void Thread::join() #else pthread_join(priv_->handle, 0); #endif - launched_ = false; + state_ = JOINED; } -/** -Violently terminates the thread. -*/ void Thread::kill() { + if(state_!=RUNNING) + return; + #ifdef WIN32 TerminateThread(priv_->handle, 0); #else pthread_kill(priv_->handle, SIGKILL); #endif + state_ = KILLED; } void Thread::launch() { - if(launched_) - return; + if(state_>=RUNNING) + throw logic_error("already launched"); #ifdef WIN32 DWORD dummy; // Win9x needs the lpTthreadId parameter @@ -83,7 +85,7 @@ void Thread::launch() #else pthread_create(&priv_->handle, 0, &Private::main_wrapper, this); #endif - launched_ = true; + state_ = RUNNING; } } // namespace Msp