util: futex fixes for OpenBSD
authorJonathan Gray <jsg@jsg.id.au>
Thu, 20 Feb 2020 02:18:01 +0000 (13:18 +1100)
committerMarge Bot <eric+marge@anholt.net>
Mon, 31 Aug 2020 09:14:57 +0000 (09:14 +0000)
Fix absolute to relative timeout computation.

Add sanity checks to futex_wait()
- handle the NULL timeout pointer case
- avoid negative cases.

From Matthieu Herrb and Scott Cheloha.

Fixes: c91997b6c43 ("util/futex: use futex syscall on OpenBSD")
Signed-off-by: Jonathan Gray <jsg@jsg.id.au>
Acked-by: Eric Engestrom <eric@engestrom.ch>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/5630>

src/util/futex.h

index 4d712e2ef2dea91fee9de9b9a125f8e21f2ea38c..43097f4cd5b4ff9afd55460d192ea7a154ef9f70 100644 (file)
@@ -100,9 +100,16 @@ static inline int futex_wake(uint32_t *addr, int count)
 
 static inline int futex_wait(uint32_t *addr, int32_t value, const struct timespec *timeout)
 {
-   struct timespec tsrel, tsnow;
-   clock_gettime(CLOCK_MONOTONIC, &tsnow); 
-   timespecsub(timeout, &tsrel, &tsrel);
+   struct timespec tsnow, tsrel;
+
+   if (timeout == NULL)
+      return futex(addr, FUTEX_WAIT, value, NULL, NULL);
+
+   clock_gettime(CLOCK_MONOTONIC, &tsnow);
+   if (timespeccmp(&tsnow, timeout, <))
+      timespecsub(timeout, &tsnow, &tsrel);
+   else
+      timespecclear(&tsrel);
    return futex(addr, FUTEX_WAIT, value, &tsrel, NULL);
 }