X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fmoof%2Fthread.hh;h=8151c3297d3d4dc181f1b8e700181ad0461a77d2;hp=1e710d8c0e66ef81e913a0a072d0646fa29099a0;hb=6c9943707d4f33035830eba0587a61a34eaecbc2;hpb=af88821a172c4dfd138b91b2a5148ae50b502fa2 diff --git a/src/moof/thread.hh b/src/moof/thread.hh index 1e710d8..8151c32 100644 --- a/src/moof/thread.hh +++ b/src/moof/thread.hh @@ -17,18 +17,22 @@ * Light C++ wrapper around the SDL threads API. */ -#include "config.h" - #include #include +#include #include #include +#include namespace moof { +class runloop; +typedef boost::shared_ptr runloop_ptr; + + /** * Represents a thread which may be running. You cannot instantiate a * thread object directly; new threads are created by detaching functions @@ -40,28 +44,29 @@ class thread { public: - typedef boost::function function; + typedef boost::function function; /** * Construct an invalid thread object which has no association with any * real thread. */ - thread() : - thread_(0) {} + thread(); + /** * Execute a function in a new thread. * \param function The function to execute. * \return The new thread, or an invalid thread if an error occurred. */ - static thread detach(const function& function) - { - thread::function* fcopy = new thread::function(function); - SDL_Thread* thread = SDL_CreateThread(&thread::run, (void*)fcopy); - if (thread == 0) delete fcopy; - return moof::thread(thread); - } + static thread detach(const function& function); + + /** + * Detach a new thread and run its runloop with an initial timer. + * \param timer The timer to schedule on the thread. + * \return The new thread, or an invalid thread if an error occurred. + */ + static thread detach(timer& timer); /** @@ -71,21 +76,21 @@ public: */ int wait() { - int i; - SDL_WaitThread(thread_, &i); - thread_ = 0; - return i; + if (thread_) + { + int i; + SDL_WaitThread(thread_, &i); + thread_ = 0; + return i; + } + return 1; } /** * Forcefully kill the thread without giving it a chance to clean up * after itself. The thread will be invalidated. Don't use this. */ - void kill() - { - SDL_KillThread(thread_); - thread_ = 0; - } + void kill(); /** * Get whether or not the thread object is associated with a real @@ -117,20 +122,28 @@ public: } -private: + /** + * Get the thread's runloop. + * \return The thread's runloop. + */ + moof::runloop& runloop() const; + + /** + * Get the runloop for the main thread. + * \return The runloop. + */ + static moof::runloop& main_runloop(); - thread(SDL_Thread* thread) : - thread_(thread) {} - static int run(void* arg) - { - int code = (*(function*)arg)(); - delete (function*)arg; - return code; - } +private: + + static void spawn(thread* thread); + static int run(void* arg); - SDL_Thread* thread_; + function function_; + SDL_Thread* thread_; + runloop_ptr runloop_; };