]> git.tdb.fi Git - libs/core.git/blob - source/core/thread.cpp
Move most platform-specific code into overlay directories
[libs/core.git] / source / core / thread.cpp
1 #include <stdexcept>
2 #include "thread.h"
3 #include "thread_private.h"
4
5 using namespace std;
6
7 namespace Msp {
8
9 Thread::Thread():
10         priv_(new Private),
11         state_(PENDING)
12 { }
13
14 Thread::~Thread()
15 {
16         kill();
17         join();
18         delete priv_;
19 }
20
21 void Thread::join()
22 {
23         if(state_>=JOINED)
24                 return;
25
26         platform_join();
27         state_ = JOINED;
28 }
29
30 void Thread::kill()
31 {
32         if(state_!=RUNNING)
33                 return;
34
35         platform_kill();
36         state_ = KILLED;
37 }
38
39 void Thread::launch()
40 {
41         if(state_>=RUNNING)
42                 throw logic_error("already launched");
43
44         platform_launch();
45         state_ = RUNNING;
46 }
47
48 ThreadReturn THREAD_CALL Thread::Private::main_wrapper(void *arg)
49 {
50         Thread *thread = reinterpret_cast<Thread *>(arg);
51         thread->main();
52         thread->state_ = FINISHED;
53         return 0;
54 }
55
56 } // namespace Msp