Commit 96cfb81232e3e7679e68665c05b84249a069cd75

Sam Lantinga 2021-01-23T09:50:43

Fixed bug 5467 - SDL sys timer Mac OS update proposal David Carlier Change of api from 2016 which reduce code complexity a bit.

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 662658b..1b2e74c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -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)
diff --git a/configure b/configure
index 1570e01..4c3cdea 100755
--- a/configure
+++ b/configure
@@ -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"
@@ -26010,6 +26010,7 @@ fi
 
 
 
+
 cat >Makefile.rules <<__EOF__
 
 # Build rules for objects
diff --git a/configure.ac b/configure.ac
index cd3ce72..3418f0a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -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)
diff --git a/include/SDL_config.h.cmake b/include/SDL_config.h.cmake
index 977050d..ede9bba 100644
--- a/include/SDL_config.h.cmake
+++ b/include/SDL_config.h.cmake
@@ -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
diff --git a/include/SDL_config.h.in b/include/SDL_config.h.in
index eb2b417..9480eb4 100644
--- a/include/SDL_config.h.in
+++ b/include/SDL_config.h.in
@@ -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
diff --git a/src/timer/unix/SDL_systimer.c b/src/timer/unix/SDL_systimer.c
index 05db3a9..5af307b 100644
--- a/src/timer/unix/SDL_systimer.c
+++ b/src/timer/unix/SDL_systimer.c
@@ -46,7 +46,7 @@
    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__
@@ -54,7 +54,7 @@
 #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
@@ -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
@@ -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);
@@ -118,9 +124,13 @@ 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;
 #endif
@@ -150,8 +160,12 @@ 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;
 #endif
@@ -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;
 }