21 #include "../../SDL_internal.h" 32 #include "../SDL_timer_c.h" 45 #if HAVE_NANOSLEEP || HAVE_CLOCK_GETTIME 49 #include <mach/mach_time.h> 53 #if HAVE_CLOCK_GETTIME 54 #ifdef CLOCK_MONOTONIC_RAW 55 #define SDL_MONOTONIC_CLOCK CLOCK_MONOTONIC_RAW 57 #define SDL_MONOTONIC_CLOCK CLOCK_MONOTONIC 62 #if HAVE_CLOCK_GETTIME 63 static struct timespec start_ts;
64 #elif defined(__APPLE__) 66 mach_timebase_info_data_t mach_base_info;
69 static struct timeval start_tv;
81 #if HAVE_CLOCK_GETTIME 82 if (clock_gettime(SDL_MONOTONIC_CLOCK, &start_ts) == 0) {
85 #elif defined(__APPLE__) 86 kern_return_t ret = mach_timebase_info(&mach_base_info);
89 start_mach = mach_absolute_time();
93 gettimeofday(&start_tv,
NULL);
107 if (!ticks_started) {
111 if (has_monotonic_time) {
112 #if HAVE_CLOCK_GETTIME 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);
127 gettimeofday(&now,
NULL);
128 ticks = (
Uint32)((now.tv_sec - start_tv.tv_sec) * 1000 + (now.tv_usec - start_tv.tv_usec) / 1000);
137 if (!ticks_started) {
141 if (has_monotonic_time) {
142 #if HAVE_CLOCK_GETTIME 145 clock_gettime(SDL_MONOTONIC_CLOCK, &now);
148 ticks += now.tv_nsec;
149 #elif defined(__APPLE__) 150 ticks = mach_absolute_time();
158 gettimeofday(&now,
NULL);
161 ticks += now.tv_usec;
169 if (!ticks_started) {
173 if (has_monotonic_time) {
174 #if HAVE_CLOCK_GETTIME 176 #elif defined(__APPLE__) 177 Uint64 freq = mach_base_info.denom;
179 freq /= mach_base_info.numer;
193 struct timespec elapsed, tv;
196 Uint32 then, now, elapsed;
201 elapsed.tv_sec = ms / 1000;
202 elapsed.tv_nsec = (ms % 1000) * 1000000;
210 tv.tv_sec = elapsed.tv_sec;
211 tv.tv_nsec = elapsed.tv_nsec;
212 was_error = nanosleep(&tv, &elapsed);
216 elapsed = (now - then);
222 tv.tv_sec = ms / 1000;
223 tv.tv_usec = (ms % 1000) * 1000;
227 }
while (was_error && (errno == EINTR));
unsigned long long uint64_t
Uint64 SDL_GetPerformanceFrequency(void)
Get the count per second of the high resolution counter.
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
#define SDL_GetPerformanceCounter
#define SDL_assert(condition)