Add SkBarriers to ports.
This completes a TODO we've had to move our memory-barrier code out of
SkOnce. I also want to start using sk_acquire_load elsewhere.
BUG=skia:
R=bungeman@google.com, mtklein@google.com, reed@google.com
Author: mtklein@chromium.org
Review URL: https://codereview.chromium.org/304593003
git-svn-id: http://skia.googlecode.com/svn/trunk@14970 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/config/SkUserConfig.h b/include/config/SkUserConfig.h
index 81033f3..5f59b91 100644
--- a/include/config/SkUserConfig.h
+++ b/include/config/SkUserConfig.h
@@ -176,5 +176,6 @@
*/
//#define SK_ATOMICS_PLATFORM_H "SkAtomics_xxx.h"
//#define SK_MUTEX_PLATFORM_H "SkMutex_xxx.h"
+//#define SK_BARRIERS_PLATFORM_H "SkBarriers_xxx.h"
#endif
diff --git a/include/core/SkOnce.h b/include/core/SkOnce.h
index d5330b9..d39a05b 100644
--- a/include/core/SkOnce.h
+++ b/include/core/SkOnce.h
@@ -71,44 +71,6 @@
SkSpinlock lock;
};
-// TODO(bungeman, mtklein): move all these *barrier* functions to SkThread when refactoring lands.
-
-#ifdef SK_BUILD_FOR_WIN
-# include <intrin.h>
-inline static void compiler_barrier() {
- _ReadWriteBarrier();
-}
-#else
-inline static void compiler_barrier() {
- asm volatile("" : : : "memory");
-}
-#endif
-
-inline static void full_barrier_on_arm() {
-#if (defined(SK_CPU_ARM) && SK_ARM_ARCH >= 7) || defined(SK_CPU_ARM64)
- asm volatile("dmb ish" : : : "memory");
-#elif defined(SK_CPU_ARM)
- asm volatile("mcr p15, 0, %0, c7, c10, 5" : : "r" (0) : "memory");
-#endif
-}
-
-// On every platform, we issue a compiler barrier to prevent it from reordering
-// code. That's enough for platforms like x86 where release and acquire
-// barriers are no-ops. On other platforms we may need to be more careful;
-// ARM, in particular, needs real code for both acquire and release. We use a
-// full barrier, which acts as both, because that the finest precision ARM
-// provides.
-
-inline static void release_barrier() {
- compiler_barrier();
- full_barrier_on_arm();
-}
-
-inline static void acquire_barrier() {
- compiler_barrier();
- full_barrier_on_arm();
-}
-
// Works with SkSpinlock or SkMutex.
template <typename Lock>
class SkAutoLockAcquire {
@@ -143,8 +105,7 @@
//
// We'll use this in the fast path to make sure f(arg)'s effects are
// observable whenever we observe *done == true.
- release_barrier();
- *done = true;
+ sk_release_store(done, true);
}
}
@@ -156,15 +117,15 @@
}
// Also known as a load-load/load-store barrier, this acquire barrier makes
// sure that anything we read from memory---in particular, memory written by
- // calling f(arg)---is at least as current as the value we read from once->done.
+ // calling f(arg)---is at least as current as the value we read from done.
//
// In version control terms, this is a lot like saying "sync up to the
- // commit where we wrote once->done = true".
+ // commit where we wrote done = true".
//
- // The release barrier in sk_once_slow guaranteed that once->done = true
- // happens after f(arg), so by syncing to once->done = true here we're
+ // The release barrier in sk_once_slow guaranteed that done = true
+ // happens after f(arg), so by syncing to done = true here we're
// forcing ourselves to also wait until the effects of f(arg) are readble.
- acquire_barrier();
+ SkAssertResult(sk_acquire_load(done));
}
template <typename Func, typename Arg>
diff --git a/include/core/SkPostConfig.h b/include/core/SkPostConfig.h
index 2538522..d4d9857 100644
--- a/include/core/SkPostConfig.h
+++ b/include/core/SkPostConfig.h
@@ -399,6 +399,14 @@
# endif
#endif
+#ifndef SK_BARRIERS_PLATFORM_H
+# if defined(SK_CPU_ARM) || defined(SK_CPU_ARM64)
+# define SK_BARRIERS_PLATFORM_H "../../src/ports/SkBarriers_arm.h"
+# else
+# define SK_BARRIERS_PLATFORM_H "../../src/ports/SkBarriers_x86.h"
+# endif
+#endif
+
//////////////////////////////////////////////////////////////////////
diff --git a/include/core/SkThread.h b/include/core/SkThread.h
index c8cd4e9..3038e2d 100644
--- a/include/core/SkThread.h
+++ b/include/core/SkThread.h
@@ -60,6 +60,25 @@
return prev;
}
+// SK_BARRIERS_PLATFORM_H must provide implementations for the following declarations:
+
+/** Prevent the compiler from reordering across this barrier. */
+static void sk_compiler_barrier();
+
+/** Read T*, with at least an acquire barrier.
+ *
+ * Only needs to be implemented for T which can be atomically read.
+ */
+template <typename T> T sk_acquire_load(T*);
+
+/** Write T*, with at least a release barrier.
+ *
+ * Only needs to be implemented for T which can be atomically written.
+ */
+template <typename T> void sk_release_store(T*, T);
+
+#include SK_BARRIERS_PLATFORM_H
+
/** SK_MUTEX_PLATFORM_H must provide the following (or equivalent) declarations.
class SkBaseMutex {
diff --git a/src/ports/SkBarriers_arm.h b/src/ports/SkBarriers_arm.h
new file mode 100644
index 0000000..9161cdd
--- /dev/null
+++ b/src/ports/SkBarriers_arm.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkBarriers_arm_DEFINED
+#define SkBarriers_arm_DEFINED
+
+static inline void sk_compiler_barrier() { asm volatile("" : : : "memory"); }
+
+template <typename T>
+T sk_acquire_load(T* ptr) {
+ T val = *ptr;
+ __sync_synchronize(); // Issue a full barrier, which is an overkill acquire barrier.
+ return val;
+}
+
+template <typename T>
+void sk_release_store(T* ptr, T val) {
+ __sync_synchronize(); // Issue a full barrier, which is an overkill release barrier.
+ *ptr = val;
+}
+
+#endif//SkBarriers_x86_DEFINED
diff --git a/src/ports/SkBarriers_x86.h b/src/ports/SkBarriers_x86.h
new file mode 100644
index 0000000..fc57615
--- /dev/null
+++ b/src/ports/SkBarriers_x86.h
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkBarriers_x86_DEFINED
+#define SkBarriers_x86_DEFINED
+
+#ifdef SK_BUILD_FOR_WIN
+# include <intrin.h>
+static inline void sk_compiler_barrier() { _ReadWriteBarrier(); }
+#else
+static inline void sk_compiler_barrier() { asm volatile("" : : : "memory"); }
+#endif
+
+template <typename T>
+T sk_acquire_load(T* ptr) {
+ T val = *ptr;
+ // On x86, all loads are acquire loads, so we only need a compiler barrier.
+ sk_compiler_barrier();
+ return val;
+}
+
+template <typename T>
+void sk_release_store(T* ptr, T val) {
+ // On x86, all stores are release stores, so we only need a compiler barrier.
+ sk_compiler_barrier();
+ *ptr = val;
+}
+
+#endif//SkBarriers_x86_DEFINED