--- /dev/null
+
+/*] 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
+