]> Dogcows Code - chaz/yoink/blobdiff - src/moof/timer.cc
testing improved runloop scheduling
[chaz/yoink] / src / moof / timer.cc
index 74acc1c203a9bc45b7cb894835e55b9e760162fc..f16a962dfa688b032ad92da9506a92c6b24ab416 100644 (file)
@@ -24,7 +24,7 @@
 namespace moof {
 
 
-scalar timer::next_expiration_ = std::numeric_limits<scalar>::max();
+scalar timer::next_event_ = std::numeric_limits<scalar>::max();
 hash<unsigned,timer*,hash_function> timer::timers_;
 
 
@@ -58,7 +58,7 @@ void timer::init(const function& function, scalar seconds, mode mode)
                id_ = new_identifier();
                timers_.insert(std::pair<unsigned,timer*>(id_, this));
 
-               if (absolute_ < next_expiration_) next_expiration_ = absolute_;
+               if (absolute_ < next_event_) next_event_ = absolute_;
        }
 }
 
@@ -75,9 +75,9 @@ void timer::invalidate()
                timers_.erase(id_);
                mode_ = invalid;
 
-               if (is_equal(absolute_, next_expiration_))
+               if (is_equal(absolute_, next_event_))
                {
-                       next_expiration_ = find_next_expiration();
+                       next_event_ = find_next_event();
                }
        }
 }
@@ -96,9 +96,9 @@ void timer::fire()
                if (is_equal(absolute_, t, 1.0)) absolute_ += interval_;
                else absolute_ = interval_ + t;
 
-               if (is_equal(absolute, next_expiration_))
+               if (is_equal(absolute, next_event_))
                {
-                       next_expiration_ = find_next_expiration();
+                       next_event_ = find_next_event();
                }
        }
        else
@@ -108,7 +108,7 @@ void timer::fire()
 }
 
 
-scalar timer::find_next_expiration()
+scalar timer::find_next_event()
 {
        scalar next_fire = std::numeric_limits<scalar>::max();
 
@@ -128,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;
@@ -141,7 +146,7 @@ bool timer::is_repeating() const
 
 void timer::fire_expired_timers(scalar t)
 {
-       if (next_expiration_ > t) return;
+       if (t < next_event_) return;
 
        hash<unsigned,timer*,hash_function>::iterator it;
        for (it = timers_.begin(); it.valid(); ++it)
@@ -191,18 +196,15 @@ scalar timer::ticks()
 
 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)) * SCALAR(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);
 }
 
 
@@ -211,18 +213,18 @@ void timer::sleep(scalar seconds, mode mode)
 
 // 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) * SCALAR(0.001);
+       return scalar(SDL_GetTicks()) * SCALAR(0.001);
 }
 
 void timer::sleep(scalar seconds, mode mode)
 {
        if (mode == absolute) seconds -= ticks();
-       SDL_Delay(Uint32(clamp(Uint32(seconds * SCALAR(1000.0)), 0, 1000)));
+       if (seconds < SCALAR(0.0)) return;
+       SDL_Delay(seconds * SCALAR(1000.0));
 }
 
 #endif // USE_CLOCK_GETTIME
This page took 0.021121 seconds and 4 git commands to generate.