]> Dogcows Code - chaz/yoink/blob - src/moof/thread.cc
build system enhancements
[chaz/yoink] / src / moof / thread.cc
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
9 *
10 **************************************************************************/
11
12 #include "runloop.hh"
13 #include "thread.hh"
14
15
16 namespace moof {
17
18
19 thread::thread() :
20 thread_(0),
21 runloop_(new moof::runloop) {}
22
23
24 thread thread::detach(const function& function)
25 {
26 thread* thread = new moof::thread;
27 thread->function_ = function;
28 spawn(thread);
29 return *thread;
30 }
31
32 thread thread::detach(timer& timer)
33 {
34 thread* thread = new moof::thread;
35 thread->runloop().add_timer(timer);
36 thread->function_ = boost::bind(&moof::runloop::run, thread->runloop_);
37 spawn(thread);
38 return *thread;
39 }
40
41
42 void thread::spawn(thread* thread)
43 {
44 thread->thread_ = SDL_CreateThread(&thread::run, (void*)thread);
45 if (!thread->thread_) delete thread;
46 }
47
48 int thread::run(void* arg)
49 {
50 int code = ((thread*)arg)->function_(*(thread*)arg);
51 delete (thread*)arg;
52 return code;
53 }
54
55
56 void thread::kill()
57 {
58 if (thread_)
59 {
60 SDL_KillThread(thread_);
61 thread_ = 0;
62 }
63 }
64
65
66 moof::runloop& thread::runloop() const
67 {
68 return *runloop_;
69 }
70
71 moof::runloop& thread::main_runloop()
72 {
73 static moof::runloop runloop;
74 return runloop;
75 }
76
77
78 } // namepsace moof
79
This page took 0.033892 seconds and 4 git commands to generate.