]>
Dogcows Code - chaz/yoink/blob - src/moof/timer.cc
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
10 **************************************************************************/
27 scalar
timer::next_expiration_
= std::numeric_limits
<scalar
>::max();
28 hash
<unsigned,timer
*,hash_function
> timer::timers_
;
31 unsigned timer::new_identifier()
33 static unsigned id
= 1;
38 void timer::init(const function
& function
, scalar seconds
, mode mode
)
54 absolute_
= seconds
- ticks();
58 id_
= new_identifier();
59 timers_
.insert(std::pair
<unsigned,timer
*>(id_
, this));
61 if (absolute_
< next_expiration_
) next_expiration_
= absolute_
;
66 bool timer::is_valid() const
68 return mode_
!= invalid
;
71 void timer::invalidate()
78 if (is_equal(absolute_
, next_expiration_
))
80 next_expiration_
= find_next_expiration();
90 if (function_
) function_(*this, t
);
94 scalar absolute
= absolute_
;
96 if (is_equal(absolute_
, t
, 1.0)) absolute_
+= interval_
;
97 else absolute_
= interval_
+ t
;
99 if (is_equal(absolute
, next_expiration_
))
101 next_expiration_
= find_next_expiration();
111 scalar
timer::find_next_expiration()
113 scalar next_fire
= std::numeric_limits
<scalar
>::max();
115 hash
<unsigned,timer
*,hash_function
>::iterator it
;
116 for (it
= timers_
.begin(); it
.valid(); ++it
)
118 scalar absolute
= (*it
).second
->absolute_
;
119 if (absolute
< next_fire
) next_fire
= absolute
;
126 scalar
timer::seconds_remaining() const
128 return absolute_
- ticks();
131 bool timer::is_expired() const
133 return seconds_remaining() < 0.0;
136 bool timer::is_repeating() const
138 return mode_
== repeat
;
142 void timer::fire_expired_timers()
144 fire_expired_timers(ticks());
147 void timer::fire_expired_timers(scalar t
)
149 if (next_expiration_
> t
) return;
151 hash
<unsigned,timer
*,hash_function
>::iterator it
;
152 for (it
= timers_
.begin(); it
.valid(); ++it
)
154 timer
* timer
= (*it
).second
;
155 if (timer
->is_expired()) timer
->fire();
162 #if USE_CLOCK_GETTIME
164 // Since the monotonic clock will provide us with the time since the
165 // computer started, the number of seconds since that time could easily
166 // become so large that it cannot be accurately stored in a float (even
167 // with as little two days uptime), therefore we need to start from a more
168 // recent reference (when the program starts). Of course this isn't much
169 // of an issue if scalar is a double-precision number.
171 static time_t set_reference()
175 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) != 0)
183 static const time_t reference_
= set_reference();
186 scalar
timer::ticks()
190 int result
= clock_gettime(CLOCK_MONOTONIC
, &ts
);
191 ASSERT(result
== 0 && "cannot access clock");
193 return scalar(ts
.tv_sec
- reference_
) +
194 scalar(ts
.tv_nsec
) / 1000000000.0;
197 void timer::sleep(scalar seconds
, mode mode
)
202 if (mode
== absolute
) seconds
-= ticks();
203 ts
.tv_sec
= time_t(seconds
);
204 ts
.tv_nsec
= long((seconds
- scalar(ts
.tv_sec
)) * 1000000000.0);
208 ret
= nanosleep(&ts
, &ts
);
210 while (ret
== -1 && errno
== EINTR
);
214 #else // ! USE_CLOCK_GETTIME
217 // If we don't have posix timers, we'll have to use a different timing
218 // method. SDL only promises centisecond accuracy, but that's better than
219 // a kick in the pants.
221 scalar
timer::ticks()
223 Uint32 ms
= SDL_GetTicks();
224 return scalar(ms
/ 1000) + scalar(ms
% 1000) / 1000.0;
227 void timer::sleep(scalar seconds
, mode mode
)
229 if (mode
== absolute
) seconds
-= ticks();
230 SDL_Delay(Uint32(clamp(int(seconds
* 1000.0), 0, 1000)));
233 #endif // USE_CLOCK_GETTIME
This page took 0.043617 seconds and 5 git commands to generate.