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