diff options
author | 2015-03-30 08:13:33 -0700 | |
---|---|---|
committer | 2015-03-30 08:13:34 -0700 | |
commit | 50ffd9921e6dc82cae8ef143e8cecfd8bcf900fb (patch) | |
tree | 56876ee2f54e95478ad143b177abc8f34843e3dd /include/ports | |
parent | ff420217394e6e35e831b11e98b46202e37de23f (diff) |
Extract the spinlock from SkOnce as SkSpinlock.
This uses slightly newer APIs from SkAtomics.h to make it a bit more efficient.
No public API changes.
TBR=reed@google.com
BUG=skia:
Review URL: https://codereview.chromium.org/1039323002
Diffstat (limited to 'include/ports')
-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 |
3 files changed, 44 insertions, 0 deletions
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 |