aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/ports/SkAtomics_sync.h
diff options
context:
space:
mode:
authorGravatar bungeman@google.com <bungeman@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-12-18 15:27:39 +0000
committerGravatar bungeman@google.com <bungeman@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-12-18 15:27:39 +0000
commitd9947f605a335363b0a0541d6d8cb7a7113ed788 (patch)
treeadfece12609a5ca87c8e2ea387c0dba31699f2b6 /src/ports/SkAtomics_sync.h
parente2380570de11226a93362d017e93c4790641c027 (diff)
Split atomic and mutex implementations and make inlinable.
Skia cannot use Chromium's implementation of mutex (Lock) due to static initializers. However, we would like to be able to use Chromium's implementation of atomics. This motivates the split of implementation. Skia's atomic and mutex calls should be inlinable, especially the atomics. These calls often compile down to very few instructions, and we currently have the overhead of a function call. This motivates the header implementation. There is still a desire for the build system to select the implementation, so the SK_XXX_PLATFORM_H pattern for header files is introduced. This allows the build system to control which platform specific header files are chosen. The Chromium side changes (most of which will need to go in before this change can be found at https://codereview.chromium.org/19477005/ . The Chromium side changes after this lands can be seen at https://codereview.chromium.org/98073013 . Review URL: https://codereview.chromium.org/19808007 git-svn-id: http://skia.googlecode.com/svn/trunk@12738 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/ports/SkAtomics_sync.h')
-rw-r--r--src/ports/SkAtomics_sync.h49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/ports/SkAtomics_sync.h b/src/ports/SkAtomics_sync.h
new file mode 100644
index 0000000000..fccd4f5ed3
--- /dev/null
+++ b/src/ports/SkAtomics_sync.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkAtomics_sync_DEFINED
+#define SkAtomics_sync_DEFINED
+
+/** GCC/Clang __sync based atomics. */
+
+#include <stdint.h>
+
+static inline __attribute__((always_inline)) int32_t sk_atomic_inc(int32_t* addr) {
+ return __sync_fetch_and_add(addr, 1);
+}
+
+static inline __attribute__((always_inline)) int32_t sk_atomic_add(int32_t* addr, int32_t inc) {
+ return __sync_fetch_and_add(addr, inc);
+}
+
+static inline __attribute__((always_inline)) int32_t sk_atomic_dec(int32_t* addr) {
+ return __sync_fetch_and_add(addr, -1);
+}
+
+static inline __attribute__((always_inline)) void sk_membar_acquire__after_atomic_dec() { }
+
+static inline __attribute__((always_inline)) int32_t sk_atomic_conditional_inc(int32_t* addr) {
+ int32_t value = *addr;
+
+ while (true) {
+ if (value == 0) {
+ return 0;
+ }
+
+ int32_t before = __sync_val_compare_and_swap(addr, value, value + 1);
+
+ if (before == value) {
+ return value;
+ } else {
+ value = before;
+ }
+ }
+}
+
+static inline __attribute__((always_inline)) void sk_membar_acquire__after_atomic_conditional_inc() { }
+
+#endif