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