aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSebastian Wick <sebastian.wick@redhat.com>2024-02-07 18:46:58 +0100
committerDerek Foreman <derek.foreman@collabora.com>2025-02-04 14:09:51 +0000
commitff8b88552358b6977043db0d6ee0cac7d7abfa0c (patch)
tree7ff61593625e50267cba4a50c4474f6b7e995865 /src
parenttimespec: Implement saturating timespec substraction (diff)
downloadwayland-ff8b88552358b6977043db0d6ee0cac7d7abfa0c.tar
wayland-ff8b88552358b6977043db0d6ee0cac7d7abfa0c.tar.gz
wayland-ff8b88552358b6977043db0d6ee0cac7d7abfa0c.tar.bz2
wayland-ff8b88552358b6977043db0d6ee0cac7d7abfa0c.tar.lz
wayland-ff8b88552358b6977043db0d6ee0cac7d7abfa0c.tar.xz
wayland-ff8b88552358b6977043db0d6ee0cac7d7abfa0c.tar.zst
wayland-ff8b88552358b6977043db0d6ee0cac7d7abfa0c.zip
event-loop: Use timespec utils instead of hand-rolling our own
Signed-off-by: Sebastian Wick <sebastian.wick@redhat.com>
Diffstat (limited to 'src')
-rw-r--r--src/event-loop.c61
1 files changed, 7 insertions, 54 deletions
diff --git a/src/event-loop.c b/src/event-loop.c
index 51c9b9d..89294fd 100644
--- a/src/event-loop.c
+++ b/src/event-loop.c
@@ -38,6 +38,7 @@
#include <sys/signalfd.h>
#include <sys/timerfd.h>
#include <unistd.h>
+#include "timespec-util.h"
#include "wayland-util.h"
#include "wayland-private.h"
#include "wayland-server-core.h"
@@ -973,57 +974,6 @@ wl_event_loop_dispatch_idle(struct wl_event_loop *loop)
}
}
-static int
-timespec_to_ms(struct timespec value)
-{
- return (value.tv_sec * 1000) + (value.tv_nsec / 1000000);
-}
-
-static struct timespec
-ms_to_timespec(int ms)
-{
- struct timespec val;
- val.tv_sec = ms / 1000;
- val.tv_nsec = (ms % 1000) * 1000000;
- return val;
-}
-
-static struct timespec
-timespec_normalize(struct timespec value)
-{
- struct timespec result = value;
-
- while (result.tv_nsec >= 1000000000) {
- result.tv_nsec -= 1000000000;
- result.tv_sec++;
- }
-
- while (result.tv_nsec < 0) {
- result.tv_nsec += 1000000000;
- result.tv_sec--;
- }
-
- return result;
-}
-
-static struct timespec
-timespec_add(struct timespec a, struct timespec b)
-{
- struct timespec result;
- result.tv_sec = a.tv_sec + b.tv_sec;
- result.tv_nsec = a.tv_nsec + b.tv_nsec;
- return timespec_normalize(result);
-}
-
-static struct timespec
-timespec_sub(struct timespec a, struct timespec b)
-{
- struct timespec result;
- result.tv_sec = a.tv_sec - b.tv_sec;
- result.tv_nsec = a.tv_nsec - b.tv_nsec;
- return timespec_normalize(result);
-}
-
/** Wait for events and dispatch them
*
* \param loop The event loop whose sources to wait for.
@@ -1052,13 +1002,15 @@ wl_event_loop_dispatch(struct wl_event_loop *loop, int timeout)
int i, count;
bool has_timers = false;
bool use_timeout = timeout > 0;
- struct timespec now, end;
+ struct timespec now;
+ struct timespec deadline = {0};
+ struct timespec result;
wl_event_loop_dispatch_idle(loop);
if (use_timeout) {
clock_gettime(CLOCK_MONOTONIC, &now);
- end = timespec_add(now, ms_to_timespec(timeout));
+ timespec_add_msec(&deadline, &now, timeout);
}
while (true) {
@@ -1070,7 +1022,8 @@ wl_event_loop_dispatch(struct wl_event_loop *loop, int timeout)
if (use_timeout) {
clock_gettime(CLOCK_MONOTONIC, &now);
- timeout = timespec_to_ms(timespec_sub(end, now));
+ timespec_sub(&result, &deadline, &now);
+ timeout = timespec_to_msec(&result);
if (timeout <= 0) {
/* too late */
count = 0;