1 /*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "../../SDL_internal.h"
22
23 #ifdef SDL_TIMER_UNIX
24
25 #include <stdio.h>
26 #include <sys/time.h>
27 #include <unistd.h>
28 #include <errno.h>
29
30 #include "SDL_timer.h"
31 #include "SDL_assert.h"
32
33 /* The clock_gettime provides monotonous time, so we should use it if
34 it's available. The clock_gettime function is behind ifdef
35 for __USE_POSIX199309
36 Tommi Kyntola (tommi.kyntola@ray.fi) 27/09/2005
37 */
38 /* Reworked monotonic clock to not assume the current system has one
39 as not all linux kernels provide a monotonic clock (yeah recent ones
40 probably do)
41 Also added OS X Monotonic clock support
42 Based on work in https://github.com/ThomasHabets/monotonic_clock
43 */
44 #if HAVE_NANOSLEEP || HAVE_CLOCK_GETTIME
45 #include <time.h>
46 #endif
47 #ifdef __APPLE__
48 #include <mach/mach_time.h>
49 #endif
50
51 /* Use CLOCK_MONOTONIC_RAW, if available, which is not subject to adjustment by NTP */
52 #if HAVE_CLOCK_GETTIME
53 #ifdef CLOCK_MONOTONIC_RAW
54 #define SDL_MONOTONIC_CLOCK CLOCK_MONOTONIC_RAW
55 #else
56 #define SDL_MONOTONIC_CLOCK CLOCK_MONOTONIC
57 #endif
58 #endif
59
60 /* The first ticks value of the application */
61 #if HAVE_CLOCK_GETTIME
62 static struct timespec start_ts;
63 #elif defined(__APPLE__)
64 static uint64_t start_mach;
65 mach_timebase_info_data_t mach_base_info;
66 #endif
67 static SDL_bool has_monotonic_time = SDL_FALSE;
68 static struct timeval start_tv;
69 static SDL_bool ticks_started = SDL_FALSE;
70
71 void
SDL_TicksInit(void)72 SDL_TicksInit(void)
73 {
74 if (ticks_started) {
75 return;
76 }
77 ticks_started = SDL_TRUE;
78
79 /* Set first ticks value */
80 #if HAVE_CLOCK_GETTIME
81 if (clock_gettime(SDL_MONOTONIC_CLOCK, &start_ts) == 0) {
82 has_monotonic_time = SDL_TRUE;
83 } else
84 #elif defined(__APPLE__)
85 kern_return_t ret = mach_timebase_info(&mach_base_info);
86 if (ret == 0) {
87 has_monotonic_time = SDL_TRUE;
88 start_mach = mach_absolute_time();
89 } else
90 #endif
91 {
92 gettimeofday(&start_tv, NULL);
93 }
94 }
95
96 void
SDL_TicksQuit(void)97 SDL_TicksQuit(void)
98 {
99 ticks_started = SDL_FALSE;
100 }
101
102 Uint32
SDL_GetTicks(void)103 SDL_GetTicks(void)
104 {
105 Uint32 ticks;
106 if (!ticks_started) {
107 SDL_TicksInit();
108 }
109
110 if (has_monotonic_time) {
111 #if HAVE_CLOCK_GETTIME
112 struct timespec now;
113 clock_gettime(SDL_MONOTONIC_CLOCK, &now);
114 ticks = (now.tv_sec - start_ts.tv_sec) * 1000 + (now.tv_nsec -
115 start_ts.tv_nsec) / 1000000;
116 #elif defined(__APPLE__)
117 uint64_t now = mach_absolute_time();
118 ticks = (Uint32)((((now - start_mach) * mach_base_info.numer) / mach_base_info.denom) / 1000000);
119 #else
120 SDL_assert(SDL_FALSE);
121 ticks = 0;
122 #endif
123 } else {
124 struct timeval now;
125
126 gettimeofday(&now, NULL);
127 ticks = (Uint32)((now.tv_sec - start_tv.tv_sec) * 1000 + (now.tv_usec - start_tv.tv_usec) / 1000);
128 }
129 return (ticks);
130 }
131
132 Uint64
SDL_GetPerformanceCounter(void)133 SDL_GetPerformanceCounter(void)
134 {
135 Uint64 ticks;
136 if (!ticks_started) {
137 SDL_TicksInit();
138 }
139
140 if (has_monotonic_time) {
141 #if HAVE_CLOCK_GETTIME
142 struct timespec now;
143
144 clock_gettime(SDL_MONOTONIC_CLOCK, &now);
145 ticks = now.tv_sec;
146 ticks *= 1000000000;
147 ticks += now.tv_nsec;
148 #elif defined(__APPLE__)
149 ticks = mach_absolute_time();
150 #else
151 SDL_assert(SDL_FALSE);
152 ticks = 0;
153 #endif
154 } else {
155 struct timeval now;
156
157 gettimeofday(&now, NULL);
158 ticks = now.tv_sec;
159 ticks *= 1000000;
160 ticks += now.tv_usec;
161 }
162 return (ticks);
163 }
164
165 Uint64
SDL_GetPerformanceFrequency(void)166 SDL_GetPerformanceFrequency(void)
167 {
168 if (!ticks_started) {
169 SDL_TicksInit();
170 }
171
172 if (has_monotonic_time) {
173 #if HAVE_CLOCK_GETTIME
174 return 1000000000;
175 #elif defined(__APPLE__)
176 Uint64 freq = mach_base_info.denom;
177 freq *= 1000000000;
178 freq /= mach_base_info.numer;
179 return freq;
180 #endif
181 }
182
183 return 1000000;
184 }
185
186 void
SDL_Delay(Uint32 ms)187 SDL_Delay(Uint32 ms)
188 {
189 int was_error;
190
191 #if HAVE_NANOSLEEP
192 struct timespec elapsed, tv;
193 #else
194 struct timeval tv;
195 Uint32 then, now, elapsed;
196 #endif
197
198 /* Set the timeout interval */
199 #if HAVE_NANOSLEEP
200 elapsed.tv_sec = ms / 1000;
201 elapsed.tv_nsec = (ms % 1000) * 1000000;
202 #else
203 then = SDL_GetTicks();
204 #endif
205 do {
206 errno = 0;
207
208 #if HAVE_NANOSLEEP
209 tv.tv_sec = elapsed.tv_sec;
210 tv.tv_nsec = elapsed.tv_nsec;
211 was_error = nanosleep(&tv, &elapsed);
212 #else
213 /* Calculate the time interval left (in case of interrupt) */
214 now = SDL_GetTicks();
215 elapsed = (now - then);
216 then = now;
217 if (elapsed >= ms) {
218 break;
219 }
220 ms -= elapsed;
221 tv.tv_sec = ms / 1000;
222 tv.tv_usec = (ms % 1000) * 1000;
223
224 was_error = select(0, NULL, NULL, NULL, &tv);
225 #endif /* HAVE_NANOSLEEP */
226 } while (was_error && (errno == EINTR));
227 }
228
229 #endif /* SDL_TIMER_UNIX */
230
231 /* vi: set ts=4 sw=4 expandtab: */
232