X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fmoof%2Ftimer.cc;h=c45e1b8909eb6413e79b9b14a372fca4b0da744c;hp=f16a962dfa688b032ad92da9506a92c6b24ab416;hb=af88821a172c4dfd138b91b2a5148ae50b502fa2;hpb=e9a16d767785b1a903a4466ff26265a5f7dae9e5 diff --git a/src/moof/timer.cc b/src/moof/timer.cc index f16a962..c45e1b8 100644 --- a/src/moof/timer.cc +++ b/src/moof/timer.cc @@ -18,30 +18,22 @@ #include #include "debug.hh" +#include "runloop.hh" #include "timer.hh" namespace moof { -scalar timer::next_event_ = std::numeric_limits::max(); -hash timer::timers_; - - -unsigned timer::new_identifier() +void timer::init(const function& function, + scalar seconds, + enum mode mode, + timer_source& source) { - static unsigned id = 1; - return id++; -} + source_ = &source; - -void timer::init(const function& function, scalar seconds, mode mode) -{ invalidate(); - - mode_ = mode; - - if (mode_ != invalid) + if ((mode_ = mode) != invalid) { function_ = function; @@ -51,150 +43,108 @@ void timer::init(const function& function, scalar seconds, mode mode) } else { - absolute_ = seconds - ticks(); + absolute_ = seconds + source_->ticks(); interval_ = seconds; } - - id_ = new_identifier(); - timers_.insert(std::pair(id_, this)); - - if (absolute_ < next_event_) next_event_ = absolute_; } } - -bool timer::is_valid() const +timer::~timer() { - return mode_ != invalid; + detach_from_runloop(); } void timer::invalidate() { - if (mode_ != invalid) - { - timers_.erase(id_); - mode_ = invalid; - - if (is_equal(absolute_, next_event_)) - { - next_event_ = find_next_event(); - } - } + mode_ = invalid; + absolute_ = SCALAR(0.0); } -void timer::fire() +void timer::added_to_runloop(runloop& runloop) { - scalar t = ticks(); - - if (function_) function_(*this, t); - - if (is_repeating()) - { - scalar absolute = absolute_; - - if (is_equal(absolute_, t, 1.0)) absolute_ += interval_; - else absolute_ = interval_ + t; + detach_from_runloop(); + runloop_ = &runloop; +} - if (is_equal(absolute, next_event_)) - { - next_event_ = find_next_event(); - } - } - else +void timer::detach_from_runloop() +{ + if (runloop_) { - invalidate(); + runloop_->remove_timer(*this); + runloop_ = 0; } } -scalar timer::find_next_event() +void timer::fire(scalar t) { - scalar next_fire = std::numeric_limits::max(); + if (function_) function_(*this, t); - hash::iterator it; - for (it = timers_.begin(); it.valid(); ++it) + if (mode_ == repeat) { - scalar absolute = (*it).second->absolute_; - if (absolute < next_fire) next_fire = absolute; + if (is_equal(absolute_, t, 1.0)) absolute_ += interval_; + else absolute_ = interval_ + t; } - - return next_fire; + else invalidate(); } -scalar timer::seconds_remaining() const -{ - return absolute_ - ticks(); -} - -scalar timer::next_expiration() const +scalar timer::ticks() { - return absolute_; + return default_source().ticks(); } -bool timer::is_expired() const -{ - return seconds_remaining() < 0.0; -} -bool timer::is_repeating() const -{ - return mode_ == repeat; -} +#if ENABLE_CLOCK_GETTIME -void timer::fire_expired_timers(scalar t) +class real_time : public timer_source { - if (t < next_event_) return; +public: - hash::iterator it; - for (it = timers_.begin(); it.valid(); ++it) + real_time() : + scale_(SCALAR(1.0)) { - timer* timer = (*it).second; - if (timer->is_expired()) timer->fire(); - - if (it.end()) break; + reset(); } -} - - -#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 -// become so large that it cannot be accurately stored in a float (even -// with as little two days uptime), therefore we need to start from a more -// recent reference (when the program starts). Of course this isn't much -// of an issue if scalar is a double-precision number. - -static time_t set_reference() -{ - struct timespec ts; - if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) + scalar ticks() const { - return 0; + struct timespec ts; + int result = clock_gettime(CLOCK_MONOTONIC, &ts); + ASSERT(result == 0 && "monotonic clock not available"); + + return reference_ + + (scalar(ts.tv_sec - start_.tv_sec) + + scalar(ts.tv_nsec - start_.tv_nsec) * + SCALAR(0.000000001)) * scale_; } - return ts.tv_sec; -} + void reset() + { + reference_ = SCALAR(0.0); + clock_gettime(CLOCK_MONOTONIC, &start_); + } -static const time_t reference_ = set_reference(); + void scale(scalar factor) + { + reference_ = ticks(); + clock_gettime(CLOCK_MONOTONIC, &start_); + scale_ = factor; + } -scalar timer::ticks() -{ - struct timespec ts; +private: - int result = clock_gettime(CLOCK_MONOTONIC, &ts); - ASSERT(result == 0 && "cannot access clock"); + scalar reference_; + struct timespec start_; + scalar scale_; +}; - return scalar(ts.tv_sec - reference_) + - scalar(ts.tv_nsec) * SCALAR(0.000000001); -} -void timer::sleep(scalar seconds, mode mode) +void timer::sleep(scalar seconds, enum mode mode) { if (mode == absolute) seconds -= ticks(); if (seconds < SCALAR(0.0)) return; @@ -208,17 +158,46 @@ void timer::sleep(scalar seconds, mode mode) } -#else // ! USE_CLOCK_GETTIME - +#else // ! ENABLE_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. It could end up being just as good anyway. -scalar timer::ticks() +class real_time : public timer_source { - return scalar(SDL_GetTicks()) * SCALAR(0.001); -} +public: + + real_time() : + scale_(SCALAR(1.0)) + { + reset(); + } + + + scalar ticks() const + { + return reference_ + scalar(SDL_GetTicks() - start_) * scale_; + } + + void reset() + { + reference_ = SCALAR(0.0); + start_ = SDL_GetTicks(); + } + + void scale(scalar factor) + { + reference_ = ticks(); + start_ = SDL_GetTicks(); + scale_ = factor * SCALAR(0.001); + } + + +private: + + scalar reference_; + Uint32 start_; + scalar scale_; +}; + void timer::sleep(scalar seconds, mode mode) { @@ -227,7 +206,14 @@ void timer::sleep(scalar seconds, mode mode) SDL_Delay(seconds * SCALAR(1000.0)); } -#endif // USE_CLOCK_GETTIME +#endif // ENABLE_CLOCK_GETTIME + + +timer_source& timer::default_source() +{ + static real_time t; + return t; +} } // namespace moof