]> Dogcows Code - chaz/yoink/blobdiff - src/moof/runloop.cc
further implementing runloop support
[chaz/yoink] / src / moof / runloop.cc
index db598145f17fbb7f14e40d6b02713109cd67cd94..f6f47dd6f87d1f7029970e3802be6eddd6bd1aed 100644 (file)
@@ -9,7 +9,12 @@
 *
 **************************************************************************/
 
+#include "config.h"
+
+#include <algorithm>
+
 #include "hash.hh"
+#include "log.hh"
 #include "runloop.hh"
 #include "timer.hh"
 
@@ -25,18 +30,20 @@ enum registry_action
 
 static uint32_t call_registry(runloop*& runloop, registry_action action)
 {
+#if ENABLE_THREADS
        typedef stlplus::hash<uint32_t,moof::runloop*,hash_function> table_t;
        static table_t table;
 
        uint32_t thread_id = thread::current_identifier();
 
-       static MOOF_DECLARE_MUTEX(table_mutex);
+       MOOF_DECLARE_STATIC_MUTEX(table_mutex);
        MOOF_MUTEX_LOCK(table_mutex);
 
        switch (action)
        {
                case set:
                {
+                       log_info("registering runloop", runloop, "for thread", thread_id);
                        if (runloop) table[thread_id] = runloop;
                        else         table.erase(thread_id);
                        break;
@@ -46,13 +53,21 @@ static uint32_t call_registry(runloop*& runloop, registry_action action)
                {
                        table_t::iterator it = table.find(thread_id);
                        if (it != table.end()) runloop = (*it).second;
+                       else log_warning("runloop is not in registry for thread", thread_id);
                        break;
                }
        }
 
        return thread_id;
+#else
+       return thread::current_identifier();
+#endif
 }
 
+bool comp(timer* a, timer* b)
+{
+       return a->expiration() < b->expiration();
+}
 
 int runloop::run()
 {
@@ -64,6 +79,7 @@ int runloop::run()
        stop_ = false;
        while (!stop_)
        {
+               log_debug("------------------------------------");
                scalar next_event = SCALAR(0.0);
                {
                        MOOF_MUTEX_LOCK(timers_mutex_);
@@ -72,13 +88,23 @@ int runloop::run()
                                 timers_it_ != timers_.end();
                                 ++timers_it_)
                        {
-                               scalar absolute = (*timers_it_)->fire_if_expired();
-                               if (next_event == SCALAR(0.0) ||
-                                       (absolute != SCALAR(0.0) && absolute < next_event))
-                               {
-                                       next_event = absolute;
-                               }
+                               (*timers_it_)->fire_if_expired();
                        }
+
+                       std::sort(timers_.begin(), timers_.end(), comp);
+                       next_event = timers_[0]->expiration();
+
+                       //for (timers_it_ = timers_.begin();
+                                //timers_it_ != timers_.end();
+                                //++timers_it_)
+                       //{
+                               //scalar absolute = (*timers_it_)->fire_if_expired();
+                               //if (next_event == SCALAR(0.0) ||
+                                       //(absolute != SCALAR(0.0) && absolute < next_event))
+                               //{
+                                       //next_event = absolute;
+                               //}
+                       //}
                }
                timer::sleep(next_event, timer::absolute);
        }
@@ -109,36 +135,36 @@ runloop* runloop::current()
 }
 
 
-void runloop::add_timer(timer* timer)
+void runloop::add_timer(timer& timer)
 {
 #if ENABLE_THREADS
        if (thread_id_ != thread::current_identifier())
        {
                MOOF_MUTEX_LOCK(timers_mutex_);
-               timers_.insert(timer);
+               timers_.push_back(&timer);
                timers_it_ = timers_.end();
        }
        else
 #endif
        {
-               timers_.insert(timer);
+               timers_.push_back(&timer);
                timers_it_ = timers_.end();
        }
 }
 
-void runloop::remove_timer(timer* timer)
+void runloop::remove_timer(timer& timer)
 {
 #if ENABLE_THREADS
        if (thread_id_ != thread::current_identifier())
        {
                MOOF_MUTEX_LOCK(timers_mutex_);
-               timers_.erase(timer);
+               timers_.erase(std::find(timers_.begin(), timers_.end(), &timer));
                timers_it_ = timers_.end();
        }
        else
 #endif
        {
-               timers_.erase(timer);
+               timers_.erase(std::find(timers_.begin(), timers_.end(), &timer));
                timers_it_ = timers_.end();
        }
 }
This page took 0.01983 seconds and 4 git commands to generate.