X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fmoof%2Frunloop.cc;h=f6f47dd6f87d1f7029970e3802be6eddd6bd1aed;hp=db598145f17fbb7f14e40d6b02713109cd67cd94;hb=af88821a172c4dfd138b91b2a5148ae50b502fa2;hpb=d6990468d297a6cbee98e4d0d33ab37e1b2352c9 diff --git a/src/moof/runloop.cc b/src/moof/runloop.cc index db59814..f6f47dd 100644 --- a/src/moof/runloop.cc +++ b/src/moof/runloop.cc @@ -9,7 +9,12 @@ * **************************************************************************/ +#include "config.h" + +#include + #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 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(); } }