Skip to content

Commit

Permalink
Fixed bug 5467 - SDL sys timer Mac OS update proposal
Browse files Browse the repository at this point in the history
David Carlier

Change of api from 2016 which reduce code complexity a bit.
  • Loading branch information
slouken committed Jan 24, 2021
1 parent 0f57864 commit 96cfb81
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -873,6 +873,7 @@ if(LIBC)
else()
set(HAVE_ALLOCA_H 1)
set(HAVE_ALLOCA 1)
check_symbol_exists(clock_gettime_nsec_np HAVE_CLOCK_GETTIME_NSEC_NP)
endif()

check_struct_has_member("struct sigaction" "sa_sigaction" "signal.h" HAVE_SA_SIGACTION)
Expand Down
3 changes: 2 additions & 1 deletion configure
Expand Up @@ -17068,7 +17068,7 @@ fi
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
fi

for ac_func in malloc calloc realloc free getenv setenv putenv unsetenv qsort abs bcopy memset memcpy memmove wcslen wcslcpy wcslcat wcsdup wcsstr wcscmp wcsncmp wcscasecmp _wcsicmp wcsncasecmp _wcsnicmp strlen strlcpy strlcat _strrev _strupr _strlwr strchr strrchr strstr strtok_r itoa _ltoa _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp vsscanf vsnprintf fopen64 fseeko fseeko64 sigaction setjmp nanosleep sysconf sysctlbyname getauxval elf_aux_info poll _Exit
for ac_func in malloc calloc realloc free getenv setenv putenv unsetenv qsort abs bcopy memset memcpy memmove wcslen wcslcpy wcslcat wcsdup wcsstr wcscmp wcsncmp wcscasecmp _wcsicmp wcsncasecmp _wcsnicmp strlen strlcpy strlcat _strrev _strupr _strlwr strchr strrchr strstr strtok_r itoa _ltoa _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp vsscanf vsnprintf fopen64 fseeko fseeko64 sigaction setjmp nanosleep sysconf sysctlbyname getauxval elf_aux_info clock_gettime_nsec_np poll _Exit
do :
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
Expand Down Expand Up @@ -26008,6 +26008,7 @@ fi






cat >Makefile.rules <<__EOF__
Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Expand Up @@ -345,7 +345,7 @@ if test x$enable_libc = xyes; then
AC_DEFINE(HAVE_MPROTECT, 1, [ ])
]),
)
AC_CHECK_FUNCS(malloc calloc realloc free getenv setenv putenv unsetenv qsort abs bcopy memset memcpy memmove wcslen wcslcpy wcslcat wcsdup wcsstr wcscmp wcsncmp wcscasecmp _wcsicmp wcsncasecmp _wcsnicmp strlen strlcpy strlcat _strrev _strupr _strlwr strchr strrchr strstr strtok_r itoa _ltoa _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp vsscanf vsnprintf fopen64 fseeko fseeko64 sigaction setjmp nanosleep sysconf sysctlbyname getauxval elf_aux_info poll _Exit)
AC_CHECK_FUNCS(malloc calloc realloc free getenv setenv putenv unsetenv qsort abs bcopy memset memcpy memmove wcslen wcslcpy wcslcat wcsdup wcsstr wcscmp wcsncmp wcscasecmp _wcsicmp wcsncasecmp _wcsnicmp strlen strlcpy strlcat _strrev _strupr _strlwr strchr strrchr strstr strtok_r itoa _ltoa _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp vsscanf vsnprintf fopen64 fseeko fseeko64 sigaction setjmp nanosleep sysconf sysctlbyname getauxval elf_aux_info clock_gettime_nsec_np poll _Exit)

AC_CHECK_LIB(m, pow, [LIBS="$LIBS -lm"; EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lm"])
AC_CHECK_FUNCS(acos acosf asin asinf atan atanf atan2 atan2f ceil ceilf copysign copysignf cos cosf exp expf fabs fabsf floor floorf trunc truncf fmod fmodf log logf log10 log10f lround lroundf pow powf round roundf scalbn scalbnf sin sinf sqrt sqrtf tan tanf)
Expand Down
1 change: 1 addition & 0 deletions include/SDL_config.h.cmake
Expand Up @@ -191,6 +191,7 @@
#cmakedefine HAVE_SYSCONF 1
#cmakedefine HAVE_SYSCTLBYNAME 1
#cmakedefine HAVE_CLOCK_GETTIME 1
#cmakedefine HAVE_CLOCK_GETTIME_NSEC_NP 1
#cmakedefine HAVE_GETPAGESIZE 1
#cmakedefine HAVE_MPROTECT 1
#cmakedefine HAVE_ICONV 1
Expand Down
1 change: 1 addition & 0 deletions include/SDL_config.h.in
Expand Up @@ -196,6 +196,7 @@
#undef HAVE_SYSCONF
#undef HAVE_SYSCTLBYNAME
#undef HAVE_CLOCK_GETTIME
#undef HAVE_CLOCK_GETTIME_NSEC_NP
#undef HAVE_GETPAGESIZE
#undef HAVE_MPROTECT
#undef HAVE_ICONV
Expand Down
27 changes: 22 additions & 5 deletions src/timer/unix/SDL_systimer.c
Expand Up @@ -46,15 +46,15 @@
Also added OS X Monotonic clock support
Based on work in https://github.com/ThomasHabets/monotonic_clock
*/
#if HAVE_NANOSLEEP || HAVE_CLOCK_GETTIME
#if HAVE_NANOSLEEP || HAVE_CLOCK_GETTIME || HAVE_CLOCK_GETTIME_NSEC_NP
#include <time.h>
#endif
#ifdef __APPLE__
#include <mach/mach_time.h>
#endif

/* Use CLOCK_MONOTONIC_RAW, if available, which is not subject to adjustment by NTP */
#if HAVE_CLOCK_GETTIME
#if HAVE_CLOCK_GETTIME || HAVE_CLOCK_GETTIME_NSEC_NP
#ifdef CLOCK_MONOTONIC_RAW
#define SDL_MONOTONIC_CLOCK CLOCK_MONOTONIC_RAW
#else
Expand All @@ -65,7 +65,7 @@
/* The first ticks value of the application */
#if HAVE_CLOCK_GETTIME
static struct timespec start_ts;
#elif defined(__APPLE__)
#elif defined(__APPLE__) && !HAVE_CLOCK_GETTIME_NSEC_NP
static uint64_t start_mach;
mach_timebase_info_data_t mach_base_info;
#endif
Expand All @@ -87,11 +87,17 @@ SDL_TicksInit(void)
has_monotonic_time = SDL_TRUE;
} else
#elif defined(__APPLE__)
#if !HAVE_CLOCK_GETTIME_NSEC_NP
kern_return_t ret = mach_timebase_info(&mach_base_info);
if (ret == 0) {
has_monotonic_time = SDL_TRUE;
start_mach = mach_absolute_time();
} else
#else
if (clock_gettime_nsec_np(SDL_MONOTONIC_CLOCK) > 0) {
has_monotonic_time = SDL_TRUE;
} else
#endif
#endif
{
gettimeofday(&start_tv, NULL);
Expand All @@ -118,8 +124,12 @@ SDL_GetTicks(void)
clock_gettime(SDL_MONOTONIC_CLOCK, &now);
ticks = (Uint32)((now.tv_sec - start_ts.tv_sec) * 1000 + (now.tv_nsec - start_ts.tv_nsec) / 1000000);
#elif defined(__APPLE__)
#if !HAVE_CLOCK_GETTIME_NSEC_NP
uint64_t now = mach_absolute_time();
ticks = (Uint32)((((now - start_mach) * mach_base_info.numer) / mach_base_info.denom) / 1000000);
#else
ticks = (Uint32)clock_gettime_nsec_np(SDL_MONOTONIC_CLOCK);
#endif
#else
SDL_assert(SDL_FALSE);
ticks = 0;
Expand Down Expand Up @@ -150,7 +160,11 @@ SDL_GetPerformanceCounter(void)
ticks *= 1000000000;
ticks += now.tv_nsec;
#elif defined(__APPLE__)
#if !HAVE_CLOCK_GETTIME_NSEC_NP
ticks = mach_absolute_time();
#else
ticks = (Uint32)clock_gettime_nsec_np(SDL_MONOTONIC_CLOCK);
#endif
#else
SDL_assert(SDL_FALSE);
ticks = 0;
Expand All @@ -177,13 +191,16 @@ SDL_GetPerformanceFrequency(void)
#if HAVE_CLOCK_GETTIME
return 1000000000;
#elif defined(__APPLE__)
#if !HAVE_CLOCK_GETTIME_NSEC_NP
Uint64 freq = mach_base_info.denom;
freq *= 1000000000;
freq /= mach_base_info.numer;
return freq;
#else
return 1000000000;
#endif
#endif
}

}
return 1000000;
}

Expand Down

0 comments on commit 96cfb81

Please sign in to comment.