]> git.tdb.fi Git - libs/core.git/blob - source/core/thread.cpp
Style updates
[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         {
58                 kill();
59                 join();
60         }
61 }
62
63 void Thread::launch()
64 {
65         if(launched_)
66                 return;
67
68 #ifdef WIN32
69         DWORD dummy;  // Win9x needs the lpTthreadId parameter
70         thread_ = CreateThread(0, 0, &main_, this, 0, &dummy);
71 #else
72         pthread_create(&thread_, 0, &main_, this);
73 #endif
74         launched_ = true;
75 }
76
77 } // namespace Msp