X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fthread.cpp;h=6858ce9999b8ba2211db353802acfa2750811125;hp=ba0c479770b4c7cbcee4c975e19180565dad4ea5;hb=727f8ce40806cc2f7c295260f3c9aa156c815c70;hpb=a883560b42163f5ed0c83204469d17dd4f0134b6 diff --git a/source/thread.cpp b/source/thread.cpp index ba0c479..6858ce9 100644 --- a/source/thread.cpp +++ b/source/thread.cpp @@ -3,26 +3,70 @@ This file is part of libmspframework Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ +#ifndef WIN32 #include +#endif #include "thread.h" namespace Msp { -void *Thread::join() +/** +Waits for the thread to exit. Calling this from the thread will cause a +deadlock. +*/ +void Thread::join() { - if(!valid_) - return 0; + if(!launched_) + return; - void *result; - pthread_join(thread_, &result); - valid_=false; - return result; +#ifdef WIN32 + WaitForSingleObject(thread_, 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_); +#endif +} + +/** +Violently terminates the thread. +*/ +void Thread::kill() +{ +#ifdef WIN32 + TerminateThread(thread_, 0); +#else + pthread_kill(thread_, SIGKILL); +#endif } Thread::~Thread() { - if(valid_) - kill(SIGKILL); + if(launched_) + kill(); +} + +void Thread::launch() +{ + if(launched_) + return; + +#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; } } // namespace Msp