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