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
This commit is contained in:
Camilla Löwy 2022-02-25 15:05:59 +01:00
parent cd22e28495
commit 84b0923fe6

View File

@ -27,12 +27,18 @@
// It is fine to use C99 in this file because it will not be built with VS // 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 "internal.h"
#include <X11/cursorfont.h> #include <X11/cursorfont.h>
#include <X11/Xmd.h> #include <X11/Xmd.h>
#include <poll.h> #include <poll.h>
#include <signal.h>
#include <time.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
@ -64,10 +70,22 @@ static GLFWbool waitForData(struct pollfd* fds, nfds_t count, double* timeout)
{ {
if (timeout) if (timeout)
{ {
const int milliseconds = (int) (*timeout * 1e3);
const uint64_t base = _glfwPlatformGetTimerValue(); 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); const int result = poll(fds, count, milliseconds);
#endif
const int error = errno; // clock_gettime may overwrite our error const int error = errno; // clock_gettime may overwrite our error
*timeout -= (_glfwPlatformGetTimerValue() - base) / *timeout -= (_glfwPlatformGetTimerValue() - base) /