Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
193 lines (171 loc) · 4.95 KB

SDL_systimer.c

File metadata and controls

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