util: futex fixes for OpenBSD
[mesa.git] / 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);
 }