]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
sys: work around incomplete POSIX support in MacOS
authorbones_was_here <bones_was_here@xonotic.au>
Fri, 17 May 2024 08:47:33 +0000 (18:47 +1000)
committerbones_was_here <bones_was_here@xonotic.au>
Fri, 17 May 2024 08:47:33 +0000 (18:47 +1000)
Closes https://gitlab.com/xonotic/darkplaces/-/issues/417

Signed-off-by: bones_was_here <bones_was_here@xonotic.au>
sys_shared.c

index e8220707383bacd7ae173c3a1c762044d54dc5a4..46cec64247666a080dd3ea44f17c09ea6acd5e23 100644 (file)
@@ -280,7 +280,13 @@ void* Sys_GetProcAddress (dllhandle_t handle, const char* name)
 #  define HAVE_CLOCKGETTIME 1
 # endif
 # if _POSIX_VERSION >= 200112L
-#  define HAVE_CLOCK_NANOSLEEP 1
+// MacOS advertises POSIX support but doesn't implement clock_nanosleep().
+// POSIX deprecated and removed usleep() so select() seems like a safer choice.
+#  if defined(MACOSX)
+#   define HAVE_SELECT_POSIX 1
+#  else
+#   define HAVE_CLOCK_NANOSLEEP 1
+#  endif
 # endif
 #endif
 
@@ -496,7 +502,7 @@ double Sys_Sleep(double time)
        if (time < 1.0/1000000.0 || host.restless)
                return 0; // not sleeping this frame
        if (time >= 1)
-               time = 0.999999; // ensure passed values are in range
+               time = 0.999999; // simpler, also ensures values are in range for all platform APIs
        msec = time * 1000;
        usec = time * 1000000;
        nsec = time * 1000000000;
@@ -558,6 +564,14 @@ double Sys_Sleep(double time)
                ts.tv_nsec = nsec;
                clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL);
        }
+#elif HAVE_SELECT_POSIX
+       else
+       {
+               struct timeval tv;
+               tv.tv_sec = 0;
+               tv.tv_usec = usec;
+               select(0, NULL, NULL, NULL, &tv);
+       }
 #elif HAVE_WIN32_USLEEP // Windows XP/2003 minimum
        else
        {