]>
Dogcows Code - chaz/yoink/blob - src/moof/timer.cc
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
5 * Distributable under the terms and conditions of the 2-clause BSD license;
6 * see the file COPYING for a complete text of the license.
8 *****************************************************************************/
28 void timer::init(const function
& function
,
29 scalar seconds
, enum mode mode
, timer_source
& source
)
34 if ((mode_
= mode
) != invalid
)
44 absolute_
= seconds
+ source_
->ticks();
52 detach_from_runloop();
55 void timer::invalidate()
58 absolute_
= SCALAR(0.0);
61 void timer::added_to_runloop(runloop
& runloop
)
63 detach_from_runloop();
67 void timer::detach_from_runloop()
71 runloop_
->remove_timer(*this);
76 void timer::fire(scalar t
)
78 if (function_
) function_(*this, t
);
82 if (is_equal(absolute_
, t
, 1.0))
83 absolute_
+= interval_
;
85 absolute_
= interval_
+ t
;
86 // TODO error accumulates in absolute var
96 return default_source().ticks();
99 #if ENABLE_CLOCK_GETTIME
100 class real_time
: public timer_source
113 int result
= clock_gettime(CLOCK_MONOTONIC
, &ts
);
114 ASSERT(result
== 0 && "monotonic clock not available");
117 (scalar(ts
.tv_sec
- start_
.tv_sec
) +
118 scalar(ts
.tv_nsec
- start_
.tv_nsec
) *
119 SCALAR(0.000000001)) * scale_
;
124 reference_
= SCALAR(0.0);
125 clock_gettime(CLOCK_MONOTONIC
, &start_
);
128 void scale(scalar factor
)
130 reference_
= ticks();
131 clock_gettime(CLOCK_MONOTONIC
, &start_
);
138 struct timespec start_
;
142 void timer::sleep(scalar seconds
, enum mode mode
)
144 if (mode
== absolute
) seconds
-= ticks();
145 if (seconds
< SCALAR(0.0)) return;
149 ts
.tv_nsec
= (seconds
- scalar(ts
.tv_sec
)) * SCALAR(1000000000.0);
152 do ret
= nanosleep(&ts
, &ts
);
153 while (ret
== -1 && errno
== EINTR
);
156 #else // ! ENABLE_CLOCK_GETTIME
157 class real_time
: public timer_source
162 scale_(SCALAR(0.001))
169 return reference_
+ scalar(SDL_GetTicks() - start_
) * scale_
;
174 reference_
= SCALAR(0.0);
175 start_
= SDL_GetTicks();
178 void scale(scalar factor
)
180 reference_
= ticks();
181 start_
= SDL_GetTicks();
182 scale_
= factor
* SCALAR(0.001);
192 void timer::sleep(scalar seconds
, enum mode mode
)
194 if (mode
== absolute
) seconds
-= ticks();
195 if (seconds
< SCALAR(0.0)) return;
196 SDL_Delay(seconds
* SCALAR(1000.0));
198 #endif // ENABLE_CLOCK_GETTIME
200 timer_source
& timer::default_source()
This page took 0.037909 seconds and 4 git commands to generate.