From 111c02d74e0792d946d054924dba793bb3593333 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Mon, 21 Jun 2021 22:14:40 +0200 Subject: [PATCH] POSIX: Remove use of deprecated gettimeofday POSIX.1-2008 deprecated gettimeofday, which we used as a fallback if the monotonic clock was unavailable. This replaces that fallback with the non-monotonic real-time clock. Because of the Gordian knot of feature test macros across Unices, this also includes the shift from some platform source files defining _POSIX_C_SOURCE to various values to _DEFAULT_SOURCE being defined for all source files on Linux. This is because -std=c99 on Linux disables _DEFAULT_SOURCE (POSIX 2008 and extensions). Once runtime platform selection comes in, this kind of platform-specific preprocessor logic can be moved into the platform glue files and won't need to be replicated by third-party build setups, but for now, sorry. --- README.md | 1 + src/CMakeLists.txt | 7 +++++++ src/posix_time.c | 36 ++++++++---------------------------- src/posix_time.h | 3 ++- src/wl_init.c | 8 ++------ src/x11_init.c | 6 ++---- 6 files changed, 22 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index ec375401..0c1600b0 100644 --- a/README.md +++ b/README.md @@ -236,6 +236,7 @@ information on what to include when reporting a bug. - [Wayland] Bugfix: Client-Side Decorations were destroyed in the wrong worder (#1798) - [Wayland] Bugfix: Monitors physical size could report zero (#1784,#1792) + - [POSIX] Removed use of deprecated function `gettimeofday` - [POSIX] Bugfix: `CLOCK_MONOTONIC` was not correctly tested for or enabled - [NSGL] Removed enforcement of forward-compatible flag for core contexts - [NSGL] Bugfix: `GLFW_COCOA_RETINA_FRAMEBUFFER` had no effect on newer diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fab20ddb..14e9fbf7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -190,6 +190,13 @@ if (MSVC90) endif() endif() +# Workaround for -std=c99 on Linux disabling _DEFAULT_SOURCE (POSIX 2008 and more) +if (_GLFW_X11 OR _GLFW_WAYLAND OR _GLFW_OSMESA) + if (CMAKE_SYSTEM_NAME STREQUAL "Linux") + target_compile_definitions(glfw PRIVATE _DEFAULT_SOURCE) + endif() +endif() + if (BUILD_SHARED_LIBS) if (WIN32) if (MINGW) diff --git a/src/posix_time.c b/src/posix_time.c index ae3d5c78..9fe61d9f 100644 --- a/src/posix_time.c +++ b/src/posix_time.c @@ -27,13 +27,10 @@ // It is fine to use C99 in this file because it will not be built with VS //======================================================================== -#define _POSIX_C_SOURCE 199309L - #include "internal.h" #include #include -#include ////////////////////////////////////////////////////////////////////////// @@ -44,20 +41,14 @@ // void _glfwInitTimerPOSIX(void) { -#if defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK) - struct timespec ts; + _glfw.timer.posix.clock = CLOCK_REALTIME; + _glfw.timer.posix.frequency = 1000000000; +#if defined(_POSIX_MONOTONIC_CLOCK) + struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) - { - _glfw.timer.posix.monotonic = GLFW_TRUE; - _glfw.timer.posix.frequency = 1000000000; - } - else + _glfw.timer.posix.clock = CLOCK_MONOTONIC; #endif - { - _glfw.timer.posix.monotonic = GLFW_FALSE; - _glfw.timer.posix.frequency = 1000000; - } } @@ -67,20 +58,9 @@ void _glfwInitTimerPOSIX(void) uint64_t _glfwPlatformGetTimerValue(void) { -#if defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK) - if (_glfw.timer.posix.monotonic) - { - struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - return (uint64_t) ts.tv_sec * (uint64_t) 1000000000 + (uint64_t) ts.tv_nsec; - } - else -#endif - { - struct timeval tv; - gettimeofday(&tv, NULL); - return (uint64_t) tv.tv_sec * (uint64_t) 1000000 + (uint64_t) tv.tv_usec; - } + struct timespec ts; + clock_gettime(_glfw.timer.posix.clock, &ts); + return (uint64_t) ts.tv_sec * (uint64_t) 1000000000 + (uint64_t) ts.tv_nsec; } uint64_t _glfwPlatformGetTimerFrequency(void) diff --git a/src/posix_time.h b/src/posix_time.h index 9b59a187..17805cf6 100644 --- a/src/posix_time.h +++ b/src/posix_time.h @@ -28,13 +28,14 @@ #define _GLFW_PLATFORM_LIBRARY_TIMER_STATE _GLFWtimerPOSIX posix #include +#include // POSIX-specific global timer data // typedef struct _GLFWtimerPOSIX { - GLFWbool monotonic; + clockid_t clock; uint64_t frequency; } _GLFWtimerPOSIX; diff --git a/src/wl_init.c b/src/wl_init.c index 1ba497b7..5e20daa6 100644 --- a/src/wl_init.c +++ b/src/wl_init.c @@ -26,8 +26,6 @@ // It is fine to use C99 in this file because it will not be built with VS //======================================================================== -#define _POSIX_C_SOURCE 200809L - #include "internal.h" #include @@ -1297,10 +1295,8 @@ void _glfwPlatformTerminate(void) const char* _glfwPlatformGetVersionString(void) { return _GLFW_VERSION_NUMBER " Wayland EGL OSMesa" -#if defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK) - " clock_gettime" -#else - " gettimeofday" +#if defined(_POSIX_MONOTONIC_CLOCK) + " monotonic" #endif " evdev" #if defined(_GLFW_BUILD_DLL) diff --git a/src/x11_init.c b/src/x11_init.c index fc9ac427..6287514b 100644 --- a/src/x11_init.c +++ b/src/x11_init.c @@ -1484,10 +1484,8 @@ void _glfwPlatformTerminate(void) const char* _glfwPlatformGetVersionString(void) { return _GLFW_VERSION_NUMBER " X11 GLX EGL OSMesa" -#if defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK) - " clock_gettime" -#else - " gettimeofday" +#if defined(_POSIX_MONOTONIC_CLOCK) + " monotonic" #endif #if defined(__linux__) " evdev"