]> git.tdb.fi Git - libs/core.git/blob - source/core/thread.cpp
Move non-oneliner functions out of RefPtr class declaration
[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(const string &name):
10         priv_(new Private),
11         name_(name),
12         state_(PENDING)
13 { }
14
15 Thread::~Thread()
16 {
17         kill();
18         join();
19         delete priv_;
20 }
21
22 void Thread::join()
23 {
24         if(state_>=JOINED)
25                 return;
26
27         platform_join();
28         state_ = JOINED;
29 }
30
31 void Thread::kill()
32 {
33         if(state_!=RUNNING)
34                 return;
35
36         platform_kill();
37         state_ = KILLED;
38 }
39
40 void Thread::launch()
41 {
42         if(state_>=RUNNING)
43                 throw logic_error("already launched");
44
45         platform_launch();
46         state_ = RUNNING;
47 }
48
49 ThreadReturn THREAD_CALL Thread::Private::main_wrapper(void *arg)
50 {
51         Thread *thread = reinterpret_cast<Thread *>(arg);
52         thread->platform_setname();
53         thread->main();
54         thread->state_ = FINISHED;
55         return 0;
56 }
57
58 } // namespace Msp