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 = (
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);
126 gettimeofday(&now,
NULL);
127 ticks = (
Uint32)((now.tv_sec - start_tv.tv_sec) * 1000 + (now.tv_usec - start_tv.tv_usec) / 1000);
136 if (!ticks_started) {
140 if (has_monotonic_time) {
141 #if HAVE_CLOCK_GETTIME
144 clock_gettime(SDL_MONOTONIC_CLOCK, &now);
147 ticks += now.tv_nsec;
148 #elif defined(__APPLE__)
149 ticks = mach_absolute_time();
157 gettimeofday(&now,
NULL);
160 ticks += now.tv_usec;
168 if (!ticks_started) {
172 if (has_monotonic_time) {
173 #if HAVE_CLOCK_GETTIME
175 #elif defined(__APPLE__)
176 Uint64 freq = mach_base_info.denom;
178 freq /= mach_base_info.numer;
192 struct timespec elapsed, tv;
195 Uint32 then, now, elapsed;
200 elapsed.tv_sec = ms / 1000;
201 elapsed.tv_nsec = (ms % 1000) * 1000000;
209 tv.tv_sec = elapsed.tv_sec;
210 tv.tv_nsec = elapsed.tv_nsec;
211 was_error = nanosleep(&tv, &elapsed);
215 elapsed = (now - then);
221 tv.tv_sec = ms / 1000;
222 tv.tv_usec = (ms % 1000) * 1000;
226 }
while (was_error && (errno == EINTR));