/
Timer.cc
294 lines (212 loc) · 7.05 KB
/
Timer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
////////////////////////////////////////////////////////////////////////////////
// -------------------------------------------------------------------------- //
// //
// (C) 2010-2016 Robot Developers //
// See LICENSE for licensing info //
// //
// -------------------------------------------------------------------------- //
////////////////////////////////////////////////////////////////////////////////
//----------------------------------------------------------------------------//
// Prefaces //
//----------------------------------------------------------------------------//
#include "Timer.h"
#ifdef ROBOT_OS_LINUX
#include <time.h>
#include <unistd.h>
#endif
#ifdef ROBOT_OS_MAC
#include <unistd.h>
#include <mach/mach_time.h>
#endif
#ifdef ROBOT_OS_WIN
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif
ROBOT_NS_BEGIN
//----------------------------------------------------------------------------//
// Locals //
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////
static const uint64 gInvalid
= 0x8000000000000000;
//----------------------------------------------------------------------------//
// Constructors Timer //
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////
Timer::Timer (void)
{
mStarted = gInvalid;
}
//----------------------------------------------------------------------------//
// Functions Timer //
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////
void Timer::Start (void)
{
mStarted = GetCpuTime();
}
////////////////////////////////////////////////////////////////////////////////
uint64 Timer::Reset (void)
{
// Check if timer started
if (mStarted == gInvalid)
return 0;
else
{
auto old = mStarted;
mStarted = gInvalid;
return GetCpuTime() - old;
}
}
////////////////////////////////////////////////////////////////////////////////
uint64 Timer::Restart (void)
{
// Check if timer started
if (mStarted == gInvalid)
{
mStarted = GetCpuTime();
return 0;
}
else
{
auto old = mStarted;
mStarted = GetCpuTime();
return mStarted - old;
}
}
////////////////////////////////////////////////////////////////////////////////
uint64 Timer::GetElapsed (void) const
{
// Check if timer started
if (mStarted == gInvalid)
return 0;
return GetCpuTime() - mStarted;
}
////////////////////////////////////////////////////////////////////////////////
bool Timer::HasStarted (void) const
{
return mStarted != gInvalid;
}
////////////////////////////////////////////////////////////////////////////////
bool Timer::HasExpired (uint64 time) const
{
// Check if timer started
if (mStarted == gInvalid)
return true;
return GetElapsed() > time;
}
////////////////////////////////////////////////////////////////////////////////
void Timer::Sleep (const Range& time)
{
// Generate a random range
int32 d = time.GetRandom();
// Skip negatives
if (d < 0) return;
#ifdef ROBOT_OS_LINUX
usleep (d * 1000);
#endif
#ifdef ROBOT_OS_MAC
usleep (d * 1000);
#endif
#ifdef ROBOT_OS_WIN
::Sleep (d);
#endif
}
////////////////////////////////////////////////////////////////////////////////
void Timer::Sleep
(uint32 minimum, uint32 maximum)
{
Sleep (Range (minimum, maximum));
}
////////////////////////////////////////////////////////////////////////////////
uint64 Timer::GetCpuTime (void)
{
#ifdef ROBOT_OS_LINUX
// Gets the current time and returns it in milliseconds
timespec time; clock_gettime (CLOCK_MONOTONIC, &time);
return (time.tv_sec * 1000) + (time.tv_nsec / 1000000);
#endif
#ifdef ROBOT_OS_MAC
// Initialize the mach timebase info one time
static mach_timebase_info_data_t info = { 0 };
if (info.denom == 0) mach_timebase_info (&info);
// Retrieve current absolute time
uint64 time = mach_absolute_time();
// Return the current absolute time in milliseconds
return (time * info.numer) / (info.denom * 1000000);
#endif
#ifdef ROBOT_OS_WIN
// Initialize timer info once
static uint64 frequency = 0;
static bool initialized = false;
if (!initialized)
{
// Get performance counter frequency
initialized = true; LARGE_INTEGER f;
if (QueryPerformanceFrequency (&f))
frequency = (uint64) f.QuadPart;
}
if (frequency > 0)
{
// Retrieve performance counter value
uint64 time = 0; LARGE_INTEGER counter;
if (QueryPerformanceCounter (&counter))
time = (uint64) counter.QuadPart;
// Return result in milliseconds
return (time * 1000) / frequency;
}
// Should never happen
return GetTickCount64();
#endif
}
//----------------------------------------------------------------------------//
// Operators Timer //
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////
uint64 Timer::operator () (void) const
{
// Check if timer started
if (mStarted == gInvalid)
return 0;
return GetCpuTime() - mStarted;
}
////////////////////////////////////////////////////////////////////////////////
bool Timer::operator < (const Timer& timer) const
{
if (timer.mStarted == gInvalid) return false;
if (this->mStarted == gInvalid) return true;
return mStarted > timer.mStarted;
}
////////////////////////////////////////////////////////////////////////////////
bool Timer::operator > (const Timer& timer) const
{
if (this->mStarted == gInvalid) return false;
if (timer.mStarted == gInvalid) return true;
return mStarted < timer.mStarted;
}
////////////////////////////////////////////////////////////////////////////////
bool Timer::operator <= (const Timer& timer) const
{
if (this->mStarted == gInvalid) return true;
if (timer.mStarted == gInvalid) return false;
return mStarted >= timer.mStarted;
}
////////////////////////////////////////////////////////////////////////////////
bool Timer::operator >= (const Timer& timer) const
{
if (timer.mStarted == gInvalid) return true;
if (this->mStarted == gInvalid) return false;
return mStarted <= timer.mStarted;
}
////////////////////////////////////////////////////////////////////////////////
bool Timer::operator == (const Timer& timer) const
{
return mStarted == timer.mStarted;
}
////////////////////////////////////////////////////////////////////////////////
bool Timer::operator != (const Timer& timer) const
{
return mStarted != timer.mStarted;
}
ROBOT_NS_END