]> Dogcows Code - chaz/yoink/blobdiff - src/moof/thread.hh
build system enhancements
[chaz/yoink] / src / moof / thread.hh
index 1e710d8c0e66ef81e913a0a072d0646fa29099a0..8151c3297d3d4dc181f1b8e700181ad0461a77d2 100644 (file)
  * Light C++ wrapper around the SDL threads API.
  */
 
-#include "config.h"
-
 #include <boost/bind.hpp>
 #include <boost/function.hpp>
+#include <boost/shared_ptr.hpp>
 #include <SDL/SDL.h>
 
 #include <moof/math.hh>
+#include <moof/timer.hh>
 
 
 namespace moof {
 
 
+class runloop;
+typedef boost::shared_ptr<runloop> 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<int(void)> function;
+       typedef boost::function<int(thread&)> 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_;
 };
 
 
This page took 0.021733 seconds and 4 git commands to generate.