]> Dogcows Code - chaz/yoink/blob - src/Moof/Timer.cc
fb17f1d66e1ac3a89c99c95a4cec94d9412c0dde
[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 <stdexcept>
32
33 #include "Timer.hh"
34
35 #if HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39
40 namespace Mf {
41
42
43 #if HAVE_CLOCK_GETTIME
44
45 // Since the monotonic clock will provide us with the timer since the computer
46 // started, the number of seconds since that time could easily become so large
47 // that it cannot be accurately stored in a float (even with as little two days
48 // uptime), therefore we need to start from a more recent reference (when the
49 // program starts). Of course this isn't much of an issue if scalar is a
50 // double-precision number.
51
52 static time_t setReference_()
53 {
54 struct timespec ts;
55
56 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
57 {
58 return 0;
59 }
60
61 return ts.tv_sec;
62 }
63
64 static const time_t reference = setReference_();
65
66
67 Scalar getTicks()
68 {
69 struct timespec ts;
70
71 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
72 {
73 throw std::runtime_error("cannot access monotonic clock");
74 }
75
76 return Scalar(ts.tv_sec - reference) + Scalar(ts.tv_nsec) / 1000000000.0;
77 }
78
79 void sleep(Scalar seconds, bool absolute)
80 {
81 struct timespec ts;
82 int ret;
83
84 if (absolute) seconds -= getTicks();
85 ts.tv_sec = time_t(seconds);
86 ts.tv_nsec = long((seconds - Scalar(ts.tv_sec)) * 1000000000.0);
87
88 do
89 {
90 ret = nanosleep(&ts, &ts);
91 }
92 while (ret == -1 && errno == EINTR);
93 }
94
95
96 #else // ! HAVE_CLOCK_GETTIME
97
98
99 // If we don't have posix timers, we'll have to use a different timing method.
100 // SDL only promises centisecond accuracy, but that's better than a kick in the
101 // butt.
102
103 #include <SDL/SDL.h>
104
105 Scalar getTicks()
106 {
107 Uint32 ms = SDL_GetTicks();
108 return Scalar(ms / 1000) + Scalar(ms % 1000) / 1000.0;
109 }
110
111 void sleep(Scalar seconds, bool absolute)
112 {
113 if (absolute) seconds -= getTicks();
114
115 SDL_Delay(Uint32(seconds * 1000.0));
116 }
117
118 #endif // HAVE_CLOCK_GETTIME
119
120
121 } // namespace Mf
122
123 /** vim: set ts=4 sw=4 tw=80: *************************************************/
124
This page took 0.034314 seconds and 3 git commands to generate.