Move futex implementation to util

Use existing implementation on Linux.

Change-Id: Idbdf8a2989c1b88e7db48f1c8b6abb0f192a84c7
diff --git a/src/BUILD.gn b/src/BUILD.gn
index b034d37..76b8ad1 100644
--- a/src/BUILD.gn
+++ b/src/BUILD.gn
@@ -71,6 +71,10 @@
     "_GNU_SOURCE=1",
   ]
 
+  if (current_os == "linux") {
+    defines += [ "HAVE_LINUX_FUTEX_H" ]
+  }
+
   if (is_debug) {
     defines += [ "DEBUG=1" ]
   }
diff --git a/src/intel/vulkan/BUILD.gn b/src/intel/vulkan/BUILD.gn
index 0117bde..c096bc2 100644
--- a/src/intel/vulkan/BUILD.gn
+++ b/src/intel/vulkan/BUILD.gn
@@ -77,7 +77,6 @@
     "$magma_build_root/include:magma_abi",
     "$magma_build_root/src/magma_util",
     "$magma_build_root/src/magma_util:allocator",
-    "$magma_build_root/src/magma_util/platform:futex",
     "$mesa_build_root/include:c_compat",
     "$mesa_build_root/src/compiler/nir",
     "$mesa_build_root/src/compiler/spirv",
@@ -121,7 +120,6 @@
     "anv_pass.c",
     "anv_pipeline.c",
     "anv_pipeline_cache.c",
-    "anv_platform.cc",
     "anv_private.h",
     "anv_queue.c",
     "anv_util.c",
diff --git a/src/intel/vulkan/anv_allocator.c b/src/intel/vulkan/anv_allocator.c
index 7192776..b8c7552 100644
--- a/src/intel/vulkan/anv_allocator.c
+++ b/src/intel/vulkan/anv_allocator.c
@@ -107,21 +107,6 @@
 
 #define ANV_MMAP_CLEANUP_INIT ((struct anv_mmap_cleanup){0})
 
-static inline int
-futex_wake(uint32_t *addr, int count)
-{
-  return anv_platform_futex_wake(addr, count);
-}
-
-/* Changed to return void because no caller is checking the return code;
- * futex_wait must always be used in a loop. */
-static inline void
-futex_wait(uint32_t *addr, int32_t value, void* timeout)
-{
-  assert(timeout == NULL);
-  (void) anv_platform_futex_wait(addr, value);
-}
-
 static inline uint32_t
 ilog2_round_up(uint32_t value)
 {
diff --git a/src/intel/vulkan/anv_platform.cc b/src/intel/vulkan/anv_platform.cc
deleted file mode 100644
index b279cf5..0000000
--- a/src/intel/vulkan/anv_platform.cc
+++ /dev/null
@@ -1,30 +0,0 @@
-// Copyright 2016 The Fuchsia Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "magma_util/dlog.h"
-#include "magma_util/macros.h"
-#include "platform_futex.h"
-#include <errno.h>
-
-extern "C" {
-
-int anv_platform_futex_wake(uint32_t* addr, int count)
-{
-   if (!magma::PlatformFutex::Wake(addr, count))
-      return DRET_MSG(-1, "Wake failed");
-   return 0;
-}
-
-int anv_platform_futex_wait(uint32_t* addr, int32_t value)
-{
-  magma::PlatformFutex::WaitResult result;
-  if (!magma::PlatformFutex::WaitForever(addr, value, &result))
-    return DRET_MSG(-EINVAL, "WaitForever failed");
-  if (result == magma::PlatformFutex::WaitResult::RETRY)
-    return -EAGAIN;
-  assert(result == magma::PlatformFutex::WaitResult::AWOKE);
-  return 0;
-}
-
-} // extern "C"
\ No newline at end of file
diff --git a/src/intel/vulkan/anv_private.h b/src/intel/vulkan/anv_private.h
index a75b005..2c4292d 100644
--- a/src/intel/vulkan/anv_private.h
+++ b/src/intel/vulkan/anv_private.h
@@ -1145,8 +1145,6 @@
                          anv_syncobj_handle_t *handles, uint32_t num_handles,
                          int64_t abs_timeout_ns, bool wait_all);
 
-int anv_platform_futex_wake(uint32_t *addr, int count);
-int anv_platform_futex_wait(uint32_t *addr, int32_t value);
 int anv_gem_import_fuchsia_buffer(struct anv_device *device, uint32_t handle, anv_buffer_handle_t* buffer_out, uint64_t* size_out);
 
 bool anv_vma_alloc(struct anv_device *device, struct anv_bo *bo);
diff --git a/src/util/BUILD.gn b/src/util/BUILD.gn
index d6cc411..3194bc8 100644
--- a/src/util/BUILD.gn
+++ b/src/util/BUILD.gn
@@ -37,6 +37,7 @@
     "build_id.h",
     "debug.h",
     "format_srgb.h",
+    "futex.h",
     "half_float.h",
     "hash_table.h",
     "list.h",
@@ -47,8 +48,8 @@
     "rgtc.h",
     "rounding.h",
     "set.h",
-    "simple_list.h",
     "sha1/sha1.h",
+    "simple_list.h",
     "strndup.h",
     "strtod.h",
     "texcompress_rgtc_tmp.h",
@@ -72,9 +73,7 @@
     "$mesa_build_root/include:c_compat",
   ]
 
-  public_configs = [
-    ":util_public_config",
-  ]
+  public_configs = [ ":util_public_config" ]
 
   sources = [
     "build_id.c",
@@ -91,6 +90,15 @@
     "u_vector.c",
     "vma.c",
   ]
+
+  deps = [
+    "$magma_build_root/src/magma_util",
+  ]
+
+  if (current_os == "fuchsia") {
+    sources += [ "futex_fuchsia.cpp" ]
+    deps += [ "$magma_build_root/src/magma_util/platform:futex" ]
+  }
 }
 
 action("format_srgb") {
diff --git a/src/util/futex.h b/src/util/futex.h
index 4402893..d4b6d54 100644
--- a/src/util/futex.h
+++ b/src/util/futex.h
@@ -51,6 +51,22 @@
                     FUTEX_BITSET_MATCH_ANY);
 }
 
+#else
+
+#include <stdint.h>
+#include <sys/time.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int futex_wake(uint32_t *addr, int count);
+int futex_wait(uint32_t *addr, int32_t value, const struct timespec *timeout);
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
+
 #endif
 
 #endif /* UTIL_FUTEX_H */
diff --git a/src/util/futex_fuchsia.cpp b/src/util/futex_fuchsia.cpp
new file mode 100644
index 0000000..1448ca7
--- /dev/null
+++ b/src/util/futex_fuchsia.cpp
@@ -0,0 +1,29 @@
+// Copyright 2019 The Fuchsia Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "futex.h"
+#include "magma_util/dlog.h"
+#include "magma_util/macros.h"
+#include "platform_futex.h"
+#include <errno.h>
+
+int futex_wake(uint32_t* addr, int count)
+{
+   if (!magma::PlatformFutex::Wake(addr, count))
+      return DRET_MSG(-1, "Wake failed");
+   return 0;
+}
+
+int futex_wait(uint32_t* addr, int32_t value, const struct timespec* timeout)
+{
+   // Timeouts not implemented.
+   assert(timeout == nullptr);
+   magma::PlatformFutex::WaitResult result;
+   if (!magma::PlatformFutex::WaitForever(addr, value, &result))
+      return DRET_MSG(-EINVAL, "PlatformFutex::WaitForever failed");
+   if (result == magma::PlatformFutex::WaitResult::RETRY)
+      return -EAGAIN;
+   assert(result == magma::PlatformFutex::WaitResult::AWOKE);
+   return 0;
+}