X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fmoof%2Fthread.cc;fp=src%2Fmoof%2Fthread.cc;h=c1e501c6a378ea22774283c341c2ee43269b9bd9;hp=0000000000000000000000000000000000000000;hb=6c9943707d4f33035830eba0587a61a34eaecbc2;hpb=af88821a172c4dfd138b91b2a5148ae50b502fa2 diff --git a/src/moof/thread.cc b/src/moof/thread.cc new file mode 100644 index 0000000..c1e501c --- /dev/null +++ b/src/moof/thread.cc @@ -0,0 +1,79 @@ + +/*] Copyright (c) 2009-2010, Charles McGarvey [************************** +**] All rights reserved. +* +* vi:ts=4 sw=4 tw=75 +* +* Distributable under the terms and conditions of the 2-clause BSD license; +* see the file COPYING for a complete text of the license. +* +**************************************************************************/ + +#include "runloop.hh" +#include "thread.hh" + + +namespace moof { + + +thread::thread() : + thread_(0), + runloop_(new moof::runloop) {} + + +thread thread::detach(const function& function) +{ + thread* thread = new moof::thread; + thread->function_ = function; + spawn(thread); + return *thread; +} + +thread thread::detach(timer& timer) +{ + thread* thread = new moof::thread; + thread->runloop().add_timer(timer); + thread->function_ = boost::bind(&moof::runloop::run, thread->runloop_); + spawn(thread); + return *thread; +} + + +void thread::spawn(thread* thread) +{ + thread->thread_ = SDL_CreateThread(&thread::run, (void*)thread); + if (!thread->thread_) delete thread; +} + +int thread::run(void* arg) +{ + int code = ((thread*)arg)->function_(*(thread*)arg); + delete (thread*)arg; + return code; +} + + +void thread::kill() +{ + if (thread_) + { + SDL_KillThread(thread_); + thread_ = 0; + } +} + + +moof::runloop& thread::runloop() const +{ + return *runloop_; +} + +moof::runloop& thread::main_runloop() +{ + static moof::runloop runloop; + return runloop; +} + + +} // namepsace moof +