diff options
-rw-r--r-- | include/core/SkAtomics.h | 10 | ||||
-rw-r--r-- | include/core/SkOnce.h | 19 | ||||
-rw-r--r-- | include/core/SkSpinlock.h | 37 | ||||
-rw-r--r-- | include/ports/SkAtomics_atomic.h | 13 | ||||
-rw-r--r-- | include/ports/SkAtomics_std.h | 14 | ||||
-rw-r--r-- | include/ports/SkAtomics_sync.h | 17 |
6 files changed, 95 insertions, 15 deletions
diff --git a/include/core/SkAtomics.h b/include/core/SkAtomics.h index cdd39ef4b7..9866db4860 100644 --- a/include/core/SkAtomics.h +++ b/include/core/SkAtomics.h @@ -1,3 +1,10 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + #ifndef SkAtomics_DEFINED #define SkAtomics_DEFINED @@ -27,6 +34,9 @@ bool sk_atomic_compare_exchange(T*, T* expected, T desired, sk_memory_order success = sk_memory_order_seq_cst, sk_memory_order failure = sk_memory_order_seq_cst); +template <typename T> +T sk_atomic_exchange(T*, T, sk_memory_order = sk_memory_order_seq_cst); + // A little wrapper class for small T (think, builtins: int, float, void*) to // ensure they're always used atomically. This is our stand-in for std::atomic<T>. template <typename T> diff --git a/include/core/SkOnce.h b/include/core/SkOnce.h index c26c49fca5..a4188d08f0 100644 --- a/include/core/SkOnce.h +++ b/include/core/SkOnce.h @@ -28,6 +28,7 @@ // OnceTest.cpp also should serve as a few other simple examples. #include "SkAtomics.h" +#include "SkSpinlock.h" // This must be used in a global scope, not in fuction scope or as a class member. #define SK_DECLARE_STATIC_ONCE(name) namespace {} static SkOnceFlag name @@ -53,24 +54,12 @@ class SkOnceFlag { public: bool* mutableDone() { return &fDone; } - void acquire() { - // To act as a mutex, this needs an acquire barrier on success. - // sk_atomic_cas doesn't guarantee this ... - while (!sk_atomic_cas(&fSpinlock, 0, 1)) { - // spin - } - // ... so make sure to issue one of our own. - SkAssertResult(sk_acquire_load(&fSpinlock)); - } - - void release() { - // To act as a mutex, this needs a release barrier. sk_atomic_cas guarantees this. - SkAssertResult(sk_atomic_cas(&fSpinlock, 1, 0)); - } + void acquire() { fSpinlock.acquire(); } + void release() { fSpinlock.release(); } private: bool fDone; - int32_t fSpinlock; + SkPODSpinlock fSpinlock; }; // We've pulled a pretty standard double-checked locking implementation apart diff --git a/include/core/SkSpinlock.h b/include/core/SkSpinlock.h new file mode 100644 index 0000000000..68a6ff7b12 --- /dev/null +++ b/include/core/SkSpinlock.h @@ -0,0 +1,37 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +// This file is not part of the public Skia API. + +#ifndef SkSpinlock_DEFINED +#define SkSpinlock_DEFINED + +#include "SkAtomics.h" + +#define SK_DECLARE_STATIC_SPINLOCK(name) namespace {} static SkPODSpinlock name + +// This class has no constructor and must be zero-initialized (the macro above does this). +struct SkPODSpinlock { + void acquire() { + // To act as a mutex, we need an acquire barrier. + while(sk_atomic_exchange(&fLocked, true, sk_memory_order_acquire)) { /*spin*/ } + } + void release() { + // To act as a mutex, we need a release barrier. + sk_atomic_store(&fLocked, false, sk_memory_order_release); + } + + bool fLocked; +}; + +// For non-global-static use cases, this is normally what you want. +class SkSpinlock : public SkPODSpinlock { +public: + SkSpinlock() { this->release(); } +}; + +#endif//SkSpinlock_DEFINED diff --git a/include/ports/SkAtomics_atomic.h b/include/ports/SkAtomics_atomic.h index 6ae78a89a9..ddbf7c3f37 100644 --- a/include/ports/SkAtomics_atomic.h +++ b/include/ports/SkAtomics_atomic.h @@ -1,3 +1,10 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + #ifndef SkAtomics_atomic_DEFINED #define SkAtomics_atomic_DEFINED @@ -37,4 +44,10 @@ bool sk_atomic_compare_exchange(T* ptr, T* expected, T desired, return __atomic_compare_exchange_n(ptr, expected, desired, false/*weak?*/, success, failure); } +template <typename T> +T sk_atomic_exchange(T* ptr, T val, sk_memory_order mo) { + // All values of mo are valid. + return __atomic_exchange_n(ptr, val, mo); +} + #endif//SkAtomics_atomic_DEFINED diff --git a/include/ports/SkAtomics_std.h b/include/ports/SkAtomics_std.h index e89d74981c..4c26858dfd 100644 --- a/include/ports/SkAtomics_std.h +++ b/include/ports/SkAtomics_std.h @@ -1,3 +1,10 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + #ifndef SkAtomics_std_DEFINED #define SkAtomics_std_DEFINED @@ -47,4 +54,11 @@ bool sk_atomic_compare_exchange(T* ptr, T* expected, T desired, (std::memory_order)failure); } +template <typename T> +T sk_atomic_exchange(T* ptr, T val, sk_memory_order mo) { + // All values of mo are valid. + std::atomic<T>* ap = reinterpret_cast<std::atomic<T>*>(ptr); + return std::atomic_exchange_explicit(ap, val, (std::memory_order)mo); +} + #endif//SkAtomics_std_DEFINED diff --git a/include/ports/SkAtomics_sync.h b/include/ports/SkAtomics_sync.h index 66da4d35ee..7ca0b46a94 100644 --- a/include/ports/SkAtomics_sync.h +++ b/include/ports/SkAtomics_sync.h @@ -1,3 +1,10 @@ +/* + * Copyright 2015 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 @@ -48,4 +55,14 @@ bool sk_atomic_compare_exchange(T* ptr, T* expected, T desired, sk_memory_order, return false; } +template <typename T> +T sk_atomic_exchange(T* ptr, T val, sk_memory_order) { + // There is no __sync exchange. Emulate it with a CAS loop. + T prev; + do { + prev = sk_atomic_load(ptr); + } while(!sk_atomic_compare_exchange(ptr, &prev, val)); + return prev; +} + #endif//SkAtomics_sync_DEFINED |