X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fthread.cpp;h=a8c80b664ce47aa126e2089dd691abbaff941696;hp=c7981cc4bcff9e24110d241768bf8e790d1c18a8;hb=967785734be5c3fc6f75da122c2d93ebbb338271;hpb=fe77fc6b869a71bf94d501a0762579f4ddbc5094 diff --git a/source/core/thread.cpp b/source/core/thread.cpp index c7981cc..a8c80b6 100644 --- a/source/core/thread.cpp +++ b/source/core/thread.cpp @@ -1,15 +1,48 @@ -/* -This file is part of libmspcore -Copyright © 2006 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ -#ifndef WIN32 +#ifdef WIN32 +#include +#else +#include #include #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(t)->main(); return 0; } +#else + static void *main_wrapper(void *t) + { reinterpret_cast(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. @@ -20,21 +53,11 @@ void Thread::join() return; #ifdef WIN32 - WaitForSingleObject(thread_, INFINITE); + WaitForSingleObject(priv_->handle, INFINITE); #else - pthread_join(thread_, 0); -#endif - launched_=false; -} - -/** -Requests the thread to terminate gracefully. Currently unimplemented on win32. -*/ -void Thread::cancel() -{ -#ifndef WIN32 //XXX - pthread_cancel(thread_); + pthread_join(priv_->handle, 0); #endif + launched_ = false; } /** @@ -43,18 +66,12 @@ 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(); -} - void Thread::launch() { if(launched_) @@ -62,11 +79,11 @@ 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; + launched_ = true; } } // namespace Msp