SDL  2.0
SDL_systimer.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2018 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 #include "../SDL_timer_c.h"
33 
34 /* The clock_gettime provides monotonous time, so we should use it if
35  it's available. The clock_gettime function is behind ifdef
36  for __USE_POSIX199309
37  Tommi Kyntola (tommi.kyntola@ray.fi) 27/09/2005
38 */
39 /* Reworked monotonic clock to not assume the current system has one
40  as not all linux kernels provide a monotonic clock (yeah recent ones
41  probably do)
42  Also added OS X Monotonic clock support
43  Based on work in https://github.com/ThomasHabets/monotonic_clock
44  */
45 #if HAVE_NANOSLEEP || HAVE_CLOCK_GETTIME
46 #include <time.h>
47 #endif
48 #ifdef __APPLE__
49 #include <mach/mach_time.h>
50 #endif
51 
52 /* Use CLOCK_MONOTONIC_RAW, if available, which is not subject to adjustment by NTP */
53 #if HAVE_CLOCK_GETTIME
54 #ifdef CLOCK_MONOTONIC_RAW
55 #define SDL_MONOTONIC_CLOCK CLOCK_MONOTONIC_RAW
56 #else
57 #define SDL_MONOTONIC_CLOCK CLOCK_MONOTONIC
58 #endif
59 #endif
60 
61 /* The first ticks value of the application */
62 #if HAVE_CLOCK_GETTIME
63 static struct timespec start_ts;
64 #elif defined(__APPLE__)
65 static uint64_t start_mach;
66 mach_timebase_info_data_t mach_base_info;
67 #endif
68 static SDL_bool has_monotonic_time = SDL_FALSE;
69 static struct timeval start_tv;
70 static SDL_bool ticks_started = SDL_FALSE;
71 
72 void
73 SDL_TicksInit(void)
74 {
75  if (ticks_started) {
76  return;
77  }
78  ticks_started = SDL_TRUE;
79 
80  /* Set first ticks value */
81 #if HAVE_CLOCK_GETTIME
82  if (clock_gettime(SDL_MONOTONIC_CLOCK, &start_ts) == 0) {
83  has_monotonic_time = SDL_TRUE;
84  } else
85 #elif defined(__APPLE__)
86  kern_return_t ret = mach_timebase_info(&mach_base_info);
87  if (ret == 0) {
88  has_monotonic_time = SDL_TRUE;
89  start_mach = mach_absolute_time();
90  } else
91 #endif
92  {
93  gettimeofday(&start_tv, NULL);
94  }
95 }
96 
97 void
98 SDL_TicksQuit(void)
99 {
100  ticks_started = SDL_FALSE;
101 }
102 
103 Uint32
104 SDL_GetTicks(void)
105 {
106  Uint32 ticks;
107  if (!ticks_started) {
108  SDL_TicksInit();
109  }
110 
111  if (has_monotonic_time) {
112 #if HAVE_CLOCK_GETTIME
113  struct timespec now;
114  clock_gettime(SDL_MONOTONIC_CLOCK, &now);
115  ticks = (now.tv_sec - start_ts.tv_sec) * 1000 + (now.tv_nsec -
116  start_ts.tv_nsec) / 1000000;
117 #elif defined(__APPLE__)
118  uint64_t now = mach_absolute_time();
119  ticks = (Uint32)((((now - start_mach) * mach_base_info.numer) / mach_base_info.denom) / 1000000);
120 #else
122  ticks = 0;
123 #endif
124  } else {
125  struct timeval now;
126 
127  gettimeofday(&now, NULL);
128  ticks = (Uint32)((now.tv_sec - start_tv.tv_sec) * 1000 + (now.tv_usec - start_tv.tv_usec) / 1000);
129  }
130  return (ticks);
131 }
132 
133 Uint64
135 {
136  Uint64 ticks;
137  if (!ticks_started) {
138  SDL_TicksInit();
139  }
140 
141  if (has_monotonic_time) {
142 #if HAVE_CLOCK_GETTIME
143  struct timespec now;
144 
145  clock_gettime(SDL_MONOTONIC_CLOCK, &now);
146  ticks = now.tv_sec;
147  ticks *= 1000000000;
148  ticks += now.tv_nsec;
149 #elif defined(__APPLE__)
150  ticks = mach_absolute_time();
151 #else
153  ticks = 0;
154 #endif
155  } else {
156  struct timeval now;
157 
158  gettimeofday(&now, NULL);
159  ticks = now.tv_sec;
160  ticks *= 1000000;
161  ticks += now.tv_usec;
162  }
163  return (ticks);
164 }
165 
166 Uint64
168 {
169  if (!ticks_started) {
170  SDL_TicksInit();
171  }
172 
173  if (has_monotonic_time) {
174 #if HAVE_CLOCK_GETTIME
175  return 1000000000;
176 #elif defined(__APPLE__)
177  Uint64 freq = mach_base_info.denom;
178  freq *= 1000000000;
179  freq /= mach_base_info.numer;
180  return freq;
181 #endif
182  }
183 
184  return 1000000;
185 }
186 
187 void
188 SDL_Delay(Uint32 ms)
189 {
190  int was_error;
191 
192 #if HAVE_NANOSLEEP
193  struct timespec elapsed, tv;
194 #else
195  struct timeval tv;
196  Uint32 then, now, elapsed;
197 #endif
198 
199  /* Set the timeout interval */
200 #if HAVE_NANOSLEEP
201  elapsed.tv_sec = ms / 1000;
202  elapsed.tv_nsec = (ms % 1000) * 1000000;
203 #else
204  then = SDL_GetTicks();
205 #endif
206  do {
207  errno = 0;
208 
209 #if HAVE_NANOSLEEP
210  tv.tv_sec = elapsed.tv_sec;
211  tv.tv_nsec = elapsed.tv_nsec;
212  was_error = nanosleep(&tv, &elapsed);
213 #else
214  /* Calculate the time interval left (in case of interrupt) */
215  now = SDL_GetTicks();
216  elapsed = (now - then);
217  then = now;
218  if (elapsed >= ms) {
219  break;
220  }
221  ms -= elapsed;
222  tv.tv_sec = ms / 1000;
223  tv.tv_usec = (ms % 1000) * 1000;
224 
225  was_error = select(0, NULL, NULL, NULL, &tv);
226 #endif /* HAVE_NANOSLEEP */
227  } while (was_error && (errno == EINTR));
228 }
229 
230 #endif /* SDL_TIMER_UNIX */
231 
232 /* vi: set ts=4 sw=4 expandtab: */
static int ticks
Definition: testtimer.c:24
unsigned long long uint64_t
Uint64 SDL_GetPerformanceFrequency(void)
Get the count per second of the high resolution counter.
uint32_t Uint32
Definition: SDL_stdinc.h:181
uint64_t Uint64
Definition: SDL_stdinc.h:194
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
#define SDL_GetPerformanceCounter
#define SDL_Delay
#define SDL_assert(condition)
Definition: SDL_assert.h:169
#define NULL
Definition: begin_code.h:164
SDL_bool
Definition: SDL_stdinc.h:139
void SDL_TicksInit(void)
void SDL_TicksQuit(void)