From 50ffd9921e6dc82cae8ef143e8cecfd8bcf900fb Mon Sep 17 00:00:00 2001 From: mtklein Date: Mon, 30 Mar 2015 08:13:33 -0700 Subject: 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 --- include/ports/SkAtomics_atomic.h | 13 +++++++++++++ include/ports/SkAtomics_std.h | 14 ++++++++++++++ include/ports/SkAtomics_sync.h | 17 +++++++++++++++++ 3 files changed, 44 insertions(+) (limited to 'include/ports') 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 +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 +T sk_atomic_exchange(T* ptr, T val, sk_memory_order mo) { + // All values of mo are valid. + std::atomic* ap = reinterpret_cast*>(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 +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 -- cgit v1.2.3