]> git.tdb.fi Git - libs/core.git/blob - source/core/thread.cpp
Make sure all files have the correct header
[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
8 #ifndef WIN32
9 #include <signal.h>
10 #endif
11 #include "thread.h"
12
13 namespace Msp {
14
15 /**
16 Waits for the thread to exit.  Calling this from the thread will cause a
17 deadlock.
18 */
19 void Thread::join()
20 {
21         if(!launched_)
22                 return;
23
24 #ifdef WIN32
25         WaitForSingleObject(thread_, INFINITE);
26 #else
27         pthread_join(thread_, 0);
28 #endif
29         launched_=false;
30 }
31
32 /**
33 Requests the thread to terminate gracefully.  Currently unimplemented on win32.
34 */
35 void Thread::cancel()
36 {
37 #ifndef WIN32 //XXX
38         pthread_cancel(thread_);
39 #endif
40 }
41
42 /**
43 Violently terminates the thread.
44 */
45 void Thread::kill()
46 {
47 #ifdef WIN32
48         TerminateThread(thread_, 0);
49 #else
50         pthread_kill(thread_, SIGKILL);
51 #endif
52 }
53
54 Thread::~Thread()
55 {
56         if(launched_)
57                 kill();
58 }
59
60 void Thread::launch()
61 {
62         if(launched_)
63                 return;
64
65 #ifdef WIN32
66         DWORD dummy;  // Win9x needs the lpTthreadId parameter
67         thread_=CreateThread(0, 0, &main_, this, 0, &dummy);
68 #else
69         pthread_create(&thread_, 0, &main_, this);
70 #endif
71         launched_=true;
72 }
73
74 } // namespace Msp