SDL  2.0
SDL_systimer.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2019 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 = (Uint32)((now.tv_sec - start_ts.tv_sec) * 1000 + (now.tv_nsec - 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
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
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
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
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
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: */
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.
uint64_t Uint64
Definition: SDL_stdinc.h:216
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:167
SDL_bool
Definition: SDL_stdinc.h:161
uint32_t Uint32
Definition: SDL_stdinc.h:203
void SDL_TicksInit(void)
void SDL_TicksQuit(void)