gallium/os: add os_wait_until_zero (v2)
authorMarek Olšák <marek.olsak@amd.com>
Thu, 25 Jun 2015 18:39:34 +0000 (20:39 +0200)
committerMarek Olšák <marek.olsak@amd.com>
Sun, 5 Jul 2015 13:08:59 +0000 (15:08 +0200)
This will be used by radeon and amdgpu winsyses.
Copied from the amdgpu winsys.

v2: use volatile and p_atomic_read

Reviewed-by: Brian Paul <brianp@vmware.com>
Reviewed-by: Alex Deucher <alexander.deucher@amd.com>
src/gallium/auxiliary/os/os_time.c
src/gallium/auxiliary/os/os_time.h

index f7e4ca49c7c5901061d0dc7612b60eb610539335..bd09452df16d394ea33ab846890ba47072e2dfd3 100644 (file)
  */
 
 
-#include "pipe/p_config.h"
+#include "pipe/p_defines.h"
+#include "util/u_atomic.h"
 
 #if defined(PIPE_OS_UNIX)
 #  include <time.h> /* timeval */
 #  include <sys/time.h> /* timeval */
+#  include <sched.h> /* sched_yield */
 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
 #  include <windows.h>
 #else
@@ -92,3 +94,37 @@ os_time_sleep(int64_t usecs)
 }
 
 #endif
+
+
+bool
+os_wait_until_zero(volatile int *var, uint64_t timeout)
+{
+   if (!p_atomic_read(var))
+      return true;
+
+   if (!timeout)
+      return false;
+
+   if (timeout == PIPE_TIMEOUT_INFINITE) {
+      while (p_atomic_read(var)) {
+#if defined(PIPE_OS_UNIX)
+         sched_yield();
+#endif
+      }
+      return true;
+   }
+   else {
+      int64_t start_time = os_time_get_nano();
+      int64_t end_time = start_time + timeout;
+
+      while (p_atomic_read(var)) {
+         if (os_time_timeout(start_time, end_time, os_time_get_nano()))
+            return false;
+
+#if defined(PIPE_OS_UNIX)
+         sched_yield();
+#endif
+      }
+      return true;
+   }
+}
index 4fab03cc6712134712094d9a1e758a818a9490a7..2989af10fe2a42684449b2ebe16b15d6400ccbb4 100644 (file)
@@ -94,6 +94,17 @@ os_time_timeout(int64_t start,
 }
 
 
+/**
+ * Wait until the variable at the given memory location is zero.
+ *
+ * \param var           variable
+ * \param timeout       timeout in ns, can be anything from 0 (no wait) to
+ *                      PIPE_TIME_INFINITE (wait forever)
+ * \return     true if the variable is zero
+ */
+bool
+os_wait_until_zero(volatile int *var, uint64_t timeout);
+
 #ifdef __cplusplus
 }
 #endif