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