util/u_queue: add util_queue_fence_wait_timeout
authorNicolai Hähnle <nicolai.haehnle@amd.com>
Sun, 22 Oct 2017 15:38:46 +0000 (17:38 +0200)
committerNicolai Hähnle <nicolai.haehnle@amd.com>
Thu, 9 Nov 2017 12:58:10 +0000 (13:58 +0100)
v2:
- style fixes
- fix missing timeout handling in futex path

Reviewed-by: Marek Olšák <marek.olsak@amd.com>
src/util/futex.h
src/util/simple_mtx.h
src/util/u_queue.c
src/util/u_queue.h

index 722cdd35f67dfe93422eb4aa1850b3704a4c5ee0..4402893069d5722ad7b227957b42632f2cbf23de 100644 (file)
@@ -33,7 +33,7 @@
 #include <sys/syscall.h>
 #include <sys/time.h>
 
-static inline long sys_futex(void *addr1, int op, int val1, struct timespec *timeout, void *addr2, int val3)
+static inline long sys_futex(void *addr1, int op, int val1, const struct timespec *timeout, void *addr2, int val3)
 {
    return syscall(SYS_futex, addr1, op, val1, timeout, addr2, val3);
 }
@@ -43,9 +43,12 @@ static inline int futex_wake(uint32_t *addr, int count)
    return sys_futex(addr, FUTEX_WAKE, count, NULL, NULL, 0);
 }
 
-static inline int futex_wait(uint32_t *addr, int32_t value)
+static inline int futex_wait(uint32_t *addr, int32_t value, const struct timespec *timeout)
 {
-   return sys_futex(addr, FUTEX_WAIT, value, NULL, NULL, 0);
+   /* FUTEX_WAIT_BITSET with FUTEX_BITSET_MATCH_ANY is equivalent to
+    * FUTEX_WAIT, except that it treats the timeout as absolute. */
+   return sys_futex(addr, FUTEX_WAIT_BITSET, value, timeout, NULL,
+                    FUTEX_BITSET_MATCH_ANY);
 }
 
 #endif
index 9f9e40861f9cc5e3fac64cebdd356fab9a8451ef..cd24b6f9eb970c97bb4da5caf86c994eaaddc86a 100644 (file)
@@ -83,7 +83,7 @@ simple_mtx_lock(simple_mtx_t *mtx)
       if (c != 2)
          c = __sync_lock_test_and_set(&mtx->val, 2);
       while (c != 0) {
-         futex_wait(&mtx->val, 2);
+         futex_wait(&mtx->val, 2, NULL);
          c = __sync_lock_test_and_set(&mtx->val, 2);
       }
    }
index 706ee8b04d9f3b107827cf9631c8799f4ed0d7f5..43c28ac6ef8cd7fff78630b03f4550a027fbd116 100644 (file)
@@ -26,6 +26,9 @@
 
 #include "u_queue.h"
 
+#include <time.h>
+
+#include "util/os_time.h"
 #include "util/u_string.h"
 #include "util/u_thread.h"
 
@@ -91,6 +94,50 @@ remove_from_atexit_list(struct util_queue *queue)
  * util_queue_fence
  */
 
+#ifdef UTIL_QUEUE_FENCE_FUTEX
+static bool
+do_futex_fence_wait(struct util_queue_fence *fence,
+                    bool timeout, int64_t abs_timeout)
+{
+   uint32_t v = fence->val;
+   struct timespec ts;
+   ts.tv_sec = abs_timeout / (1000*1000*1000);
+   ts.tv_nsec = abs_timeout % (1000*1000*1000);
+
+   while (v != 0) {
+      if (v != 2) {
+         v = p_atomic_cmpxchg(&fence->val, 1, 2);
+         if (v == 0)
+            return true;
+      }
+
+      int r = futex_wait(&fence->val, 2, timeout ? &ts : NULL);
+      if (timeout && r < 0) {
+         if (errno == -ETIMEDOUT)
+            return false;
+      }
+
+      v = fence->val;
+   }
+
+   return true;
+}
+
+void
+_util_queue_fence_wait(struct util_queue_fence *fence)
+{
+   do_futex_fence_wait(fence, false, 0);
+}
+
+bool
+_util_queue_fence_wait_timeout(struct util_queue_fence *fence,
+                               int64_t abs_timeout)
+{
+   return do_futex_fence_wait(fence, true, abs_timeout);
+}
+
+#endif
+
 #ifdef UTIL_QUEUE_FENCE_STANDARD
 void
 util_queue_fence_signal(struct util_queue_fence *fence)
@@ -102,7 +149,7 @@ util_queue_fence_signal(struct util_queue_fence *fence)
 }
 
 void
-util_queue_fence_wait(struct util_queue_fence *fence)
+_util_queue_fence_wait(struct util_queue_fence *fence)
 {
    mtx_lock(&fence->mutex);
    while (!fence->signalled)
@@ -110,6 +157,39 @@ util_queue_fence_wait(struct util_queue_fence *fence)
    mtx_unlock(&fence->mutex);
 }
 
+bool
+_util_queue_fence_wait_timeout(struct util_queue_fence *fence,
+                               int64_t abs_timeout)
+{
+   /* This terrible hack is made necessary by the fact that we really want an
+    * internal interface consistent with os_time_*, but cnd_timedwait is spec'd
+    * to be relative to the TIME_UTC clock.
+    */
+   int64_t rel = abs_timeout - os_time_get_nano();
+
+   if (rel > 0) {
+      struct timespec ts;
+
+      timespec_get(&ts, TIME_UTC);
+
+      ts.tv_sec += abs_timeout / (1000*1000*1000);
+      ts.tv_nsec += abs_timeout % (1000*1000*1000);
+      if (ts.tv_nsec >= (1000*1000*1000)) {
+         ts.tv_sec++;
+         ts.tv_nsec -= (1000*1000*1000);
+      }
+
+      mtx_lock(&fence->mutex);
+      while (!fence->signalled) {
+         if (cnd_timedwait(&fence->cond, &fence->mutex, &ts) != thrd_success)
+            break;
+      }
+      mtx_unlock(&fence->mutex);
+   }
+
+   return fence->signalled;
+}
+
 void
 util_queue_fence_init(struct util_queue_fence *fence)
 {
index a54ec7101144756830aca6efa6eb63db757b6084..ec028157480a7e614c419eae7a5059b5e6586fe7 100644 (file)
@@ -80,26 +80,6 @@ util_queue_fence_destroy(struct util_queue_fence *fence)
    /* no-op */
 }
 
-static inline void
-util_queue_fence_wait(struct util_queue_fence *fence)
-{
-   uint32_t v = fence->val;
-
-   if (likely(v == 0))
-      return;
-
-   do {
-      if (v != 2) {
-         v = p_atomic_cmpxchg(&fence->val, 1, 2);
-         if (v == 0)
-            return;
-      }
-
-      futex_wait(&fence->val, 2);
-      v = fence->val;
-   } while(v != 0);
-}
-
 static inline void
 util_queue_fence_signal(struct util_queue_fence *fence)
 {
@@ -147,7 +127,6 @@ struct util_queue_fence {
 
 void util_queue_fence_init(struct util_queue_fence *fence);
 void util_queue_fence_destroy(struct util_queue_fence *fence);
-void util_queue_fence_wait(struct util_queue_fence *fence);
 void util_queue_fence_signal(struct util_queue_fence *fence);
 
 /**
@@ -170,6 +149,39 @@ util_queue_fence_is_signalled(struct util_queue_fence *fence)
 }
 #endif
 
+void
+_util_queue_fence_wait(struct util_queue_fence *fence);
+
+static inline void
+util_queue_fence_wait(struct util_queue_fence *fence)
+{
+   if (unlikely(!util_queue_fence_is_signalled(fence)))
+      _util_queue_fence_wait(fence);
+}
+
+bool
+_util_queue_fence_wait_timeout(struct util_queue_fence *fence,
+                               int64_t abs_timeout);
+
+/**
+ * Wait for the fence to be signaled with a timeout.
+ *
+ * \param fence the fence
+ * \param abs_timeout the absolute timeout in nanoseconds, relative to the
+ *                    clock provided by os_time_get_nano.
+ *
+ * \return true if the fence was signaled, false if the timeout occurred.
+ */
+static inline bool
+util_queue_fence_wait_timeout(struct util_queue_fence *fence,
+                              int64_t abs_timeout)
+{
+   if (util_queue_fence_is_signalled(fence))
+      return true;
+
+   return _util_queue_fence_wait_timeout(fence, abs_timeout);
+}
+
 typedef void (*util_queue_execute_func)(void *job, int thread_index);
 
 struct util_queue_job {