From: bones_was_here Date: Fri, 17 May 2024 08:47:33 +0000 (+1000) Subject: sys: work around incomplete POSIX support in MacOS X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=aeb8a36cf719774c66a1a7c0728a9a2b1a745438;p=xonotic%2Fdarkplaces.git sys: work around incomplete POSIX support in MacOS Closes https://gitlab.com/xonotic/darkplaces/-/issues/417 Signed-off-by: bones_was_here --- diff --git a/sys_shared.c b/sys_shared.c index e8220707..46cec642 100644 --- a/sys_shared.c +++ b/sys_shared.c @@ -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 {