]> Dogcows Code - chaz/yoink/blobdiff - src/moof/thread.cc
build system enhancements
[chaz/yoink] / src / moof / thread.cc
diff --git a/src/moof/thread.cc b/src/moof/thread.cc
new file mode 100644 (file)
index 0000000..c1e501c
--- /dev/null
@@ -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
+
This page took 0.019247 seconds and 4 git commands to generate.