X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fthread.cpp;h=cfc0c37573d02a100df148a433a0bf365b76111f;hb=933356a36607f5d4480172e285071d4d7dfc5a7b;hp=810b1f80485197eeb66552707897c89e42356215;hpb=b56eb5ec1da675da0c66abc53c1e4f6c4e4cccbd;p=libs%2Fcore.git diff --git a/source/core/thread.cpp b/source/core/thread.cpp index 810b1f8..cfc0c37 100644 --- a/source/core/thread.cpp +++ b/source/core/thread.cpp @@ -1,55 +1,37 @@ -/* $Id$ - -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 { -/** -Waits for the thread to exit. Calling this from the thread will cause a -deadlock. -*/ -void Thread::join() +struct Thread::Private { - if(!launched_) - return; - #ifdef WIN32 - WaitForSingleObject(thread_, INFINITE); + HANDLE handle; #else - pthread_join(thread_, 0); + pthread_t handle; #endif - launched_ = false; -} -/** -Requests the thread to terminate gracefully. Currently unimplemented on win32. -*/ -void Thread::cancel() -{ -#ifndef WIN32 //XXX - pthread_cancel(thread_); -#endif -} + Private(): handle(0) { } -/** -Violently terminates the thread. -*/ -void Thread::kill() -{ #ifdef WIN32 - TerminateThread(thread_, 0); + static DWORD WINAPI main_wrapper(void *t) + { reinterpret_cast(t)->main(); return 0; } #else - pthread_kill(thread_, SIGKILL); + static void *main_wrapper(void *t) + { reinterpret_cast(t)->main(); return 0; } #endif -} +}; + + +Thread::Thread(): + priv_(new Private), + launched_(false) +{ } Thread::~Thread() { @@ -58,6 +40,29 @@ Thread::~Thread() kill(); join(); } + delete priv_; +} + +void Thread::join() +{ + if(!launched_) + return; + +#ifdef WIN32 + WaitForSingleObject(priv_->handle, INFINITE); +#else + pthread_join(priv_->handle, 0); +#endif + launched_ = false; +} + +void Thread::kill() +{ +#ifdef WIN32 + TerminateThread(priv_->handle, 0); +#else + pthread_kill(priv_->handle, SIGKILL); +#endif } void Thread::launch() @@ -67,9 +72,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; }