From 84b0923fe61a12bd0bf9241da102053b906265bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Fri, 25 Feb 2022 15:05:59 +0100 Subject: [PATCH] X11: Use lower-latency poll where available This uses ppoll for waiting on file descriptors with a timeout, where that function has been available a while. On NetBSD, which will be getting ppoll in the next release, the equivalent pollts is used. This commit is based on work by OlivierSohn and kovidgoyal. Related to #1281 Related to #1285 --- src/x11_window.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/x11_window.c b/src/x11_window.c index 76becd6c..52cbbcf0 100644 --- a/src/x11_window.c +++ b/src/x11_window.c @@ -27,12 +27,18 @@ // It is fine to use C99 in this file because it will not be built with VS //======================================================================== +#if defined(__linux__) + #define _GNU_SOURCE +#endif + #include "internal.h" #include #include #include +#include +#include #include #include @@ -64,10 +70,22 @@ static GLFWbool waitForData(struct pollfd* fds, nfds_t count, double* timeout) { if (timeout) { - const int milliseconds = (int) (*timeout * 1e3); const uint64_t base = _glfwPlatformGetTimerValue(); +#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__CYGWIN__) + const time_t seconds = (time_t) *timeout; + const long nanoseconds = (long) ((*timeout - seconds) * 1e9); + const struct timespec ts = { seconds, nanoseconds }; + const int result = ppoll(fds, count, &ts, NULL); +#elif defined(__NetBSD__) + const time_t seconds = (time_t) *timeout; + const long nanoseconds = (long) ((*timeout - seconds) * 1e9); + const struct timespec ts = { seconds, nanoseconds }; + const int result = pollts(fds, count, &ts, NULL); +#else + const int milliseconds = (int) (*timeout * 1e3); const int result = poll(fds, count, milliseconds); +#endif const int error = errno; // clock_gettime may overwrite our error *timeout -= (_glfwPlatformGetTimerValue() - base) /