X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fmoof%2Ftimer.cc;h=f16a962dfa688b032ad92da9506a92c6b24ab416;hp=273625640ab4a78dedfda47a691981972a98d7cf;hb=e9a16d767785b1a903a4466ff26265a5f7dae9e5;hpb=831f04d4bc19a390415ac0bbac4331c7a65509bc diff --git a/src/moof/timer.cc b/src/moof/timer.cc index 2736256..f16a962 100644 --- a/src/moof/timer.cc +++ b/src/moof/timer.cc @@ -9,25 +9,23 @@ * **************************************************************************/ +#include "config.h" + #include #include #include #include -#include "log.hh" +#include "debug.hh" #include "timer.hh" -#if HAVE_CONFIG_H -#include "config.h" -#endif - namespace moof { -scalar timer::gNextFire = std::numeric_limits::max(); -std::map timer::gTimers; +scalar timer::next_event_ = std::numeric_limits::max(); +hash timer::timers_; unsigned timer::new_identifier() @@ -58,9 +56,9 @@ void timer::init(const function& function, scalar seconds, mode mode) } id_ = new_identifier(); - gTimers.insert(std::pair(id_, this)); + timers_.insert(std::pair(id_, this)); - if (absolute_ < gNextFire) gNextFire = absolute_; + if (absolute_ < next_event_) next_event_ = absolute_; } } @@ -74,10 +72,13 @@ void timer::invalidate() { if (mode_ != invalid) { - gTimers.erase(id_); + timers_.erase(id_); mode_ = invalid; - if (is_equal(absolute_, gNextFire)) gNextFire = find_next_expiration(); + if (is_equal(absolute_, next_event_)) + { + next_event_ = find_next_event(); + } } } @@ -95,7 +96,10 @@ void timer::fire() if (is_equal(absolute_, t, 1.0)) absolute_ += interval_; else absolute_ = interval_ + t; - if (is_equal(absolute, gNextFire)) gNextFire = find_next_expiration(); + if (is_equal(absolute, next_event_)) + { + next_event_ = find_next_event(); + } } else { @@ -104,18 +108,18 @@ void timer::fire() } -scalar timer::find_next_expiration() +scalar timer::find_next_event() { - std::map::iterator it; - scalar nextFire = std::numeric_limits::max(); + scalar next_fire = std::numeric_limits::max(); - for (it = gTimers.begin(); it != gTimers.end(); ++it) + hash::iterator it; + for (it = timers_.begin(); it.valid(); ++it) { scalar absolute = (*it).second->absolute_; - if (absolute < nextFire) nextFire = absolute; + if (absolute < next_fire) next_fire = absolute; } - return nextFire; + return next_fire; } @@ -124,6 +128,11 @@ scalar timer::seconds_remaining() const return absolute_ - ticks(); } +scalar timer::next_expiration() const +{ + return absolute_; +} + bool timer::is_expired() const { return seconds_remaining() < 0.0; @@ -135,26 +144,22 @@ bool timer::is_repeating() const } -void timer::fire_expired_timers() -{ - fire_expired_timers(ticks()); -} - void timer::fire_expired_timers(scalar t) { - std::map::iterator it; - - if (gNextFire > t) return; + if (t < next_event_) return; - for (it = gTimers.begin(); it != gTimers.end(); ++it) + hash::iterator it; + for (it = timers_.begin(); it.valid(); ++it) { timer* timer = (*it).second; if (timer->is_expired()) timer->fire(); + + if (it.end()) break; } } -#if HAVE_CLOCK_GETTIME +#if USE_CLOCK_GETTIME // Since the monotonic clock will provide us with the time since the // computer started, the number of seconds since that time could easily @@ -175,7 +180,7 @@ static time_t set_reference() return ts.tv_sec; } -static const time_t reference = set_reference(); +static const time_t reference_ = set_reference(); scalar timer::ticks() @@ -185,47 +190,44 @@ scalar timer::ticks() int result = clock_gettime(CLOCK_MONOTONIC, &ts); ASSERT(result == 0 && "cannot access clock"); - return scalar(ts.tv_sec - reference) + - scalar(ts.tv_nsec) / 1000000000.0; + return scalar(ts.tv_sec - reference_) + + scalar(ts.tv_nsec) * SCALAR(0.000000001); } void timer::sleep(scalar seconds, mode mode) { - struct timespec ts; - int ret; - if (mode == absolute) seconds -= ticks(); - ts.tv_sec = time_t(seconds); - ts.tv_nsec = long((seconds - scalar(ts.tv_sec)) * 1000000000.0); + if (seconds < SCALAR(0.0)) return; - do - { - ret = nanosleep(&ts, &ts); - } - while (ret == -1 && errno == EINTR); + struct timespec ts; + ts.tv_sec = seconds; + ts.tv_nsec = (seconds - scalar(ts.tv_sec)) * SCALAR(1000000000.0); + + int ret; + do ret = nanosleep(&ts, &ts); while (ret == -1 && errno == EINTR); } -#else // ! HAVE_CLOCK_GETTIME +#else // ! USE_CLOCK_GETTIME // If we don't have posix timers, we'll have to use a different timing // method. SDL only promises centisecond accuracy, but that's better than -// a kick in the pants. +// a kick in the pants. It could end up being just as good anyway. scalar timer::ticks() { - Uint32 ms = SDL_GetTicks(); - return scalar(ms / 1000) + scalar(ms % 1000) / 1000.0; + return scalar(SDL_GetTicks()) * SCALAR(0.001); } void timer::sleep(scalar seconds, mode mode) { if (mode == absolute) seconds -= ticks(); - SDL_Delay(Uint32(clamp(int(seconds * 1000.0), 0, 1000))); + if (seconds < SCALAR(0.0)) return; + SDL_Delay(seconds * SCALAR(1000.0)); } -#endif // HAVE_CLOCK_GETTIME +#endif // USE_CLOCK_GETTIME } // namespace moof