]>
Dogcows Code - chaz/yoink/blob - src/moof/timer.cc
273625640ab4a78dedfda47a691981972a98d7cf
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 **************************************************************************/
29 scalar
timer::gNextFire
= std::numeric_limits
<scalar
>::max();
30 std::map
<unsigned,timer
*> timer::gTimers
;
33 unsigned timer::new_identifier()
35 static unsigned id
= 1;
40 void timer::init(const function
& function
, scalar seconds
, mode mode
)
56 absolute_
= seconds
- ticks();
60 id_
= new_identifier();
61 gTimers
.insert(std::pair
<unsigned,timer
*>(id_
, this));
63 if (absolute_
< gNextFire
) gNextFire
= absolute_
;
68 bool timer::is_valid() const
70 return mode_
!= invalid
;
73 void timer::invalidate()
80 if (is_equal(absolute_
, gNextFire
)) gNextFire
= find_next_expiration();
89 if (function_
) function_(*this, t
);
93 scalar absolute
= absolute_
;
95 if (is_equal(absolute_
, t
, 1.0)) absolute_
+= interval_
;
96 else absolute_
= interval_
+ t
;
98 if (is_equal(absolute
, gNextFire
)) gNextFire
= find_next_expiration();
107 scalar
timer::find_next_expiration()
109 std::map
<unsigned,timer
*>::iterator it
;
110 scalar nextFire
= std::numeric_limits
<scalar
>::max();
112 for (it
= gTimers
.begin(); it
!= gTimers
.end(); ++it
)
114 scalar absolute
= (*it
).second
->absolute_
;
115 if (absolute
< nextFire
) nextFire
= absolute
;
122 scalar
timer::seconds_remaining() const
124 return absolute_
- ticks();
127 bool timer::is_expired() const
129 return seconds_remaining() < 0.0;
132 bool timer::is_repeating() const
134 return mode_
== repeat
;
138 void timer::fire_expired_timers()
140 fire_expired_timers(ticks());
143 void timer::fire_expired_timers(scalar t
)
145 std::map
<unsigned,timer
*>::iterator it
;
147 if (gNextFire
> t
) return;
149 for (it
= gTimers
.begin(); it
!= gTimers
.end(); ++it
)
151 timer
* timer
= (*it
).second
;
152 if (timer
->is_expired()) timer
->fire();
157 #if HAVE_CLOCK_GETTIME
159 // Since the monotonic clock will provide us with the time since the
160 // computer started, the number of seconds since that time could easily
161 // become so large that it cannot be accurately stored in a float (even
162 // with as little two days uptime), therefore we need to start from a more
163 // recent reference (when the program starts). Of course this isn't much
164 // of an issue if scalar is a double-precision number.
166 static time_t set_reference()
170 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) != 0)
178 static const time_t reference
= set_reference();
181 scalar
timer::ticks()
185 int result
= clock_gettime(CLOCK_MONOTONIC
, &ts
);
186 ASSERT(result
== 0 && "cannot access clock");
188 return scalar(ts
.tv_sec
- reference
) +
189 scalar(ts
.tv_nsec
) / 1000000000.0;
192 void timer::sleep(scalar seconds
, mode mode
)
197 if (mode
== absolute
) seconds
-= ticks();
198 ts
.tv_sec
= time_t(seconds
);
199 ts
.tv_nsec
= long((seconds
- scalar(ts
.tv_sec
)) * 1000000000.0);
203 ret
= nanosleep(&ts
, &ts
);
205 while (ret
== -1 && errno
== EINTR
);
209 #else // ! HAVE_CLOCK_GETTIME
212 // If we don't have posix timers, we'll have to use a different timing
213 // method. SDL only promises centisecond accuracy, but that's better than
214 // a kick in the pants.
216 scalar
timer::ticks()
218 Uint32 ms
= SDL_GetTicks();
219 return scalar(ms
/ 1000) + scalar(ms
% 1000) / 1000.0;
222 void timer::sleep(scalar seconds
, mode mode
)
224 if (mode
== absolute
) seconds
-= ticks();
225 SDL_Delay(Uint32(clamp(int(seconds
* 1000.0), 0, 1000)));
228 #endif // HAVE_CLOCK_GETTIME
This page took 0.05196 seconds and 4 git commands to generate.