]> git.tdb.fi Git - libs/core.git/blob - source/core/thread.cpp
Assimilate exceptions and RefPtr from mspmisc
[libs/core.git] / source / core / thread.cpp
1 /* $Id$
2
3 This file is part of libmspcore
4 Copyright © 2006 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7 #ifndef WIN32
8 #include <signal.h>
9 #endif
10 #include "thread.h"
11
12 namespace Msp {
13
14 /**
15 Waits for the thread to exit.  Calling this from the thread will cause a
16 deadlock.
17 */
18 void Thread::join()
19 {
20         if(!launched_)
21                 return;
22
23 #ifdef WIN32
24         WaitForSingleObject(thread_, INFINITE);
25 #else
26         pthread_join(thread_, 0);
27 #endif
28         launched_=false;
29 }
30
31 /**
32 Requests the thread to terminate gracefully.  Currently unimplemented on win32.
33 */
34 void Thread::cancel()
35 {
36 #ifndef WIN32 //XXX
37         pthread_cancel(thread_);
38 #endif
39 }
40
41 /**
42 Violently terminates the thread.
43 */
44 void Thread::kill()
45 {
46 #ifdef WIN32
47         TerminateThread(thread_, 0);
48 #else
49         pthread_kill(thread_, SIGKILL);
50 #endif
51 }
52
53 Thread::~Thread()
54 {
55         if(launched_)
56                 kill();
57 }
58
59 void Thread::launch()
60 {
61         if(launched_)
62                 return;
63
64 #ifdef WIN32
65         DWORD dummy;  // Win9x needs the lpTthreadId parameter
66         thread_=CreateThread(0, 0, &main_, this, 0, &dummy);
67 #else
68         pthread_create(&thread_, 0, &main_, this);
69 #endif
70         launched_=true;
71 }
72
73 } // namespace Msp