X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fthread.cpp;h=e860d6758f9e048f070b67855e7d7810456e7c48;hp=8e1134b80fa2e68ade849c5bff6033af3b6a2c25;hb=41363aed34382386f915f17c1a961750b4fdcb14;hpb=521cf1db00f8ce2d9f9494dca503d6c17d89ac2f diff --git a/source/core/thread.cpp b/source/core/thread.cpp index 8e1134b..e860d67 100644 --- a/source/core/thread.cpp +++ b/source/core/thread.cpp @@ -1,73 +1,57 @@ -/* $Id$ - -This file is part of libmspcore -Copyright © 2006 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ -#ifndef WIN32 -#include -#endif +#include #include "thread.h" +#include "thread_private.h" + +using namespace std; namespace Msp { -/** -Waits for the thread to exit. Calling this from the thread will cause a -deadlock. -*/ -void Thread::join() -{ - if(!launched_) - return; +Thread::Thread(const string &name): + _priv(new Private), + _name(name) +{ } -#ifdef WIN32 - WaitForSingleObject(thread_, INFINITE); -#else - pthread_join(thread_, 0); -#endif - launched_=false; +Thread::~Thread() +{ + kill(); + join(); + delete _priv; } -/** -Requests the thread to terminate gracefully. Currently unimplemented on win32. -*/ -void Thread::cancel() +void Thread::join() { -#ifndef WIN32 //XXX - pthread_cancel(thread_); -#endif + if(_state>=JOINED) + return; + + platform_join(); + _state = JOINED; } -/** -Violently terminates the thread. -*/ void Thread::kill() { -#ifdef WIN32 - TerminateThread(thread_, 0); -#else - pthread_kill(thread_, SIGKILL); -#endif -} + if(_state!=RUNNING) + return; -Thread::~Thread() -{ - if(launched_) - kill(); + platform_kill(); + _state = KILLED; } void Thread::launch() { - if(launched_) - return; + if(_state>=RUNNING) + throw logic_error("already launched"); + + platform_launch(); + _state = RUNNING; +} -#ifdef WIN32 - DWORD dummy; // Win9x needs the lpTthreadId parameter - thread_=CreateThread(0, 0, &main_, this, 0, &dummy); -#else - pthread_create(&thread_, 0, &main_, this); -#endif - launched_=true; +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