]> Dogcows Code - chaz/yoink/blob - src/Moof/Timer.cc
69d2405622f7031cf134715b053304b39afb5ea2
[chaz/yoink] / src / Moof / Timer.cc
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #include <cerrno>
30 #include <ctime>
31 #include <limits>
32
33 #include <SDL/SDL.h>
34
35 #include "Log.hh"
36 #include "Timer.hh"
37
38 #if HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42
43 namespace Mf {
44
45
46 Scalar Timer::gNextFire = std::numeric_limits<Scalar>::max();
47 std::map<unsigned,Timer*> Timer::gTimers;
48
49
50 unsigned Timer::getNewID()
51 {
52 static unsigned id = 1;
53 return id++;
54 }
55
56
57 void Timer::init(const Function& function, Scalar seconds, Mode mode)
58 {
59 invalidate();
60
61 mMode = mode;
62
63 if (mMode != INVALID)
64 {
65 mFunction = function;
66
67 if (mode == ACTUAL)
68 {
69 mAbsolute = seconds;
70 }
71 else
72 {
73 mAbsolute = seconds - getTicks();
74 mInterval = seconds;
75 }
76
77 mId = getNewID();
78 gTimers.insert(std::pair<unsigned,Timer*>(mId, this));
79
80 if (mAbsolute < gNextFire) gNextFire = mAbsolute;
81 }
82 }
83
84
85 bool Timer::isValid() const
86 {
87 return mMode != INVALID;
88 }
89
90 void Timer::invalidate()
91 {
92 if (mMode != INVALID)
93 {
94 gTimers.erase(mId);
95 mMode = INVALID;
96
97 if (isEqual(mAbsolute, gNextFire)) gNextFire = findNextFire();
98 }
99 }
100
101
102 void Timer::fire()
103 {
104 Scalar t = getTicks();
105
106 if (mFunction) mFunction(*this, t);
107
108 if (isRepeating())
109 {
110 Scalar absolute = mAbsolute;
111
112 if (isEqual(mAbsolute, t, 1.0)) mAbsolute += mInterval;
113 else mAbsolute = mInterval + t;
114
115 if (isEqual(absolute, gNextFire)) gNextFire = findNextFire();
116 }
117 else
118 {
119 invalidate();
120 }
121 }
122
123
124 Scalar Timer::findNextFire()
125 {
126 std::map<unsigned,Timer*>::iterator it;
127 Scalar nextFire = std::numeric_limits<Scalar>::max();
128
129 for (it = gTimers.begin(); it != gTimers.end(); ++it)
130 {
131 Scalar absolute = (*it).second->mAbsolute;
132 if (absolute < nextFire) nextFire = absolute;
133 }
134
135 return nextFire;
136 }
137
138
139 Scalar Timer::getSecondsRemaining() const
140 {
141 return mAbsolute - getTicks();
142 }
143
144 bool Timer::isExpired() const
145 {
146 return getSecondsRemaining() < 0.0;
147 }
148
149 bool Timer::isRepeating() const
150 {
151 return mMode == REPEAT;
152 }
153
154
155 void Timer::fireIfExpired()
156 {
157 fireIfExpired(getTicks());
158 }
159
160 void Timer::fireIfExpired(Scalar t)
161 {
162 std::map<unsigned,Timer*>::iterator it;
163
164 if (gNextFire > t) return;
165
166 for (it = gTimers.begin(); it != gTimers.end(); ++it)
167 {
168 Timer* timer = (*it).second;
169 if (timer->isExpired()) timer->fire();
170 }
171 }
172
173
174 #if HAVE_CLOCK_GETTIME
175
176 // Since the monotonic clock will provide us with the time since the computer
177 // started, the number of seconds since that time could easily become so large
178 // that it cannot be accurately stored in a float (even with as little two days
179 // uptime), therefore we need to start from a more recent reference (when the
180 // program starts). Of course this isn't much of an issue if scalar is a
181 // double-precision number.
182
183 static time_t setReference_()
184 {
185 struct timespec ts;
186
187 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
188 {
189 return 0;
190 }
191
192 return ts.tv_sec;
193 }
194
195 static const time_t reference = setReference_();
196
197
198 Scalar Timer::getTicks()
199 {
200 struct timespec ts;
201
202 int result = clock_gettime(CLOCK_MONOTONIC, &ts);
203 ASSERT(result == 0 && "cannot access clock");
204
205 return Scalar(ts.tv_sec - reference) + Scalar(ts.tv_nsec) / 1000000000.0;
206 }
207
208 void Timer::sleep(Scalar seconds, Mode mode)
209 {
210 struct timespec ts;
211 int ret;
212
213 if (mode == ACTUAL) seconds -= getTicks();
214 ts.tv_sec = time_t(seconds);
215 ts.tv_nsec = long((seconds - Scalar(ts.tv_sec)) * 1000000000.0);
216
217 do
218 {
219 ret = nanosleep(&ts, &ts);
220 }
221 while (ret == -1 && errno == EINTR);
222 }
223
224
225 #else // ! HAVE_CLOCK_GETTIME
226
227
228 // If we don't have posix timers, we'll have to use a different timing method.
229 // SDL only promises centisecond accuracy, but that's better than a kick in the
230 // butt.
231
232 Scalar Timer::getTicks()
233 {
234 Uint32 ms = SDL_GetTicks();
235 return Scalar(ms / 1000) + Scalar(ms % 1000) / 1000.0;
236 }
237
238 void Timer::sleep(Scalar seconds, Mode mode)
239 {
240 if (mode == ACTUAL) seconds -= getTicks();
241 SDL_Delay(Uint32(cml::clamp(int(seconds * 1000.0), 0, 1000)));
242 }
243
244 #endif // HAVE_CLOCK_GETTIME
245
246
247 } // namespace Mf
248
249 /** vim: set ts=4 sw=4 tw=80: *************************************************/
250
This page took 0.04368 seconds and 3 git commands to generate.