SkAtomic: always use std::atomic

We were doing it on Windows, now do it everywhere.
This just changes the backend.  We could think about another step to actually
replacing all our sk_atomic_... with std atomic stuff.

CQ_EXTRA_TRYBOTS=client.skia:Test-Ubuntu-GCC-GCE-CPU-AVX2-x86_64-Release-TSAN-Trybot
TBR=reed@google.com
Only deleting from include/...

Review URL: https://codereview.chromium.org/1441773002
This commit is contained in:
mtklein 2015-11-12 11:07:53 -08:00 committed by Commit bot
parent 64593525de
commit 23267db678
7 changed files with 62 additions and 221 deletions

View File

@ -60,9 +60,6 @@
'../src/ports/SkTLS_pthread.cpp',
'../src/ports/SkTLS_win.cpp',
'../include/ports/SkAtomics_atomic.h',
'../include/ports/SkAtomics_std.h',
'../include/ports/SkAtomics_sync.h',
'../include/ports/SkFontConfigInterface.h',
'../include/ports/SkFontMgr.h',
'../include/ports/SkFontMgr_android.h',

View File

@ -595,9 +595,6 @@
],
'paths_to_ignore': [
'<(skia_include_path)/gpu/gl/GrGLConfig_chrome.h',
'<(skia_include_path)/ports/SkAtomics_std.h',
'<(skia_include_path)/ports/SkAtomics_atomic.h',
'<(skia_include_path)/ports/SkAtomics_sync.h',
'<(skia_include_path)/ports/SkFontMgr_fontconfig.h',
'<(skia_include_path)/ports/SkTypeface_mac.h',
'<(skia_include_path)/ports/SkTypeface_win.h',

View File

@ -1,59 +0,0 @@
/*
* 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
template <typename T>
T sk_atomic_load(const T* ptr, sk_memory_order mo) {
SkASSERT(mo == sk_memory_order_relaxed ||
mo == sk_memory_order_seq_cst ||
mo == sk_memory_order_acquire ||
mo == sk_memory_order_consume);
return __atomic_load_n(ptr, mo);
}
template <typename T>
void sk_atomic_store(T* ptr, T val, sk_memory_order mo) {
SkASSERT(mo == sk_memory_order_relaxed ||
mo == sk_memory_order_seq_cst ||
mo == sk_memory_order_release);
__atomic_store_n(ptr, val, mo);
}
template <typename T>
T sk_atomic_fetch_add(T* ptr, T val, sk_memory_order mo) {
// All values of mo are valid.
return __atomic_fetch_add(ptr, val, mo);
}
template <typename T>
T sk_atomic_fetch_sub(T* ptr, T val, sk_memory_order mo) {
// All values of mo are valid.
return __atomic_fetch_sub(ptr, val, mo);
}
template <typename T>
bool sk_atomic_compare_exchange(T* ptr, T* expected, T desired,
sk_memory_order success,
sk_memory_order failure) {
// All values of success are valid.
SkASSERT(failure == sk_memory_order_relaxed ||
failure == sk_memory_order_seq_cst ||
failure == sk_memory_order_acquire ||
failure == sk_memory_order_consume);
SkASSERT(failure <= success);
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

View File

@ -1,71 +0,0 @@
/*
* 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
// We try not to depend on the C++ standard library,
// but these uses of <atomic> should all inline, so we don't feel to bad here.
#include <atomic>
template <typename T>
T sk_atomic_load(const T* ptr, sk_memory_order mo) {
SkASSERT(mo == sk_memory_order_relaxed ||
mo == sk_memory_order_seq_cst ||
mo == sk_memory_order_acquire ||
mo == sk_memory_order_consume);
const std::atomic<T>* ap = reinterpret_cast<const std::atomic<T>*>(ptr);
return std::atomic_load_explicit(ap, (std::memory_order)mo);
}
template <typename T>
void sk_atomic_store(T* ptr, T val, sk_memory_order mo) {
SkASSERT(mo == sk_memory_order_relaxed ||
mo == sk_memory_order_seq_cst ||
mo == sk_memory_order_release);
std::atomic<T>* ap = reinterpret_cast<std::atomic<T>*>(ptr);
return std::atomic_store_explicit(ap, val, (std::memory_order)mo);
}
template <typename T>
T sk_atomic_fetch_add(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_fetch_add_explicit(ap, val, (std::memory_order)mo);
}
template <typename T>
T sk_atomic_fetch_sub(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_fetch_sub_explicit(ap, val, (std::memory_order)mo);
}
template <typename T>
bool sk_atomic_compare_exchange(T* ptr, T* expected, T desired,
sk_memory_order success,
sk_memory_order failure) {
// All values of success are valid.
SkASSERT(failure == sk_memory_order_relaxed ||
failure == sk_memory_order_seq_cst ||
failure == sk_memory_order_acquire ||
failure == sk_memory_order_consume);
SkASSERT(failure <= success);
std::atomic<T>* ap = reinterpret_cast<std::atomic<T>*>(ptr);
return std::atomic_compare_exchange_strong_explicit(ap, expected, desired,
(std::memory_order)success,
(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

View File

@ -1,73 +0,0 @@
/*
* 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
// This file is mostly a shim. We'd like to delete it. Please don't put much
// effort into maintaining it, and if you find bugs in it, the right fix is to
// delete this file and upgrade your compiler to something that supports
// __atomic builtins or std::atomic.
static inline void barrier(sk_memory_order mo) {
asm volatile("" : : : "memory"); // Prevents the compiler from reordering code.
#if SK_CPU_X86
// On x86, we generally don't need an extra memory barrier for loads or stores.
if (sk_memory_order_seq_cst == mo) { __sync_synchronize(); }
#else
// On other platforms (e.g. ARM) we do unless the memory order is relaxed.
if (sk_memory_order_relaxed != mo) { __sync_synchronize(); }
#endif
}
// These barriers only support our majority use cases: acquire and relaxed loads, release stores.
// For anything more complicated, please consider deleting this file and upgrading your compiler.
template <typename T>
T sk_atomic_load(const T* ptr, sk_memory_order mo) {
T val = *ptr;
barrier(mo);
return val;
}
template <typename T>
void sk_atomic_store(T* ptr, T val, sk_memory_order mo) {
barrier(mo);
*ptr = val;
}
template <typename T>
T sk_atomic_fetch_add(T* ptr, T val, sk_memory_order) {
return __sync_fetch_and_add(ptr, val);
}
template <typename T>
T sk_atomic_fetch_sub(T* ptr, T val, sk_memory_order) {
return __sync_fetch_and_sub(ptr, val);
}
template <typename T>
bool sk_atomic_compare_exchange(T* ptr, T* expected, T desired, sk_memory_order, sk_memory_order) {
T prev = __sync_val_compare_and_swap(ptr, *expected, desired);
if (prev == *expected) {
return true;
}
*expected = prev;
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

View File

@ -10,6 +10,9 @@
// This file is not part of the public Skia API.
#include "SkTypes.h"
#include <atomic>
// ~~~~~~~~ APIs ~~~~~~~~~
enum sk_memory_order {
sk_memory_order_relaxed,
@ -86,15 +89,65 @@ private:
T fVal;
};
// IWYU pragma: begin_exports
#if defined(_MSC_VER)
#include "../ports/SkAtomics_std.h"
#elif !defined(SK_BUILD_FOR_IOS) && defined(__ATOMIC_RELAXED)
#include "../ports/SkAtomics_atomic.h"
#else
#include "../ports/SkAtomics_sync.h"
#endif
// IWYU pragma: end_exports
// ~~~~~~~~ Implementations ~~~~~~~~~
template <typename T>
T sk_atomic_load(const T* ptr, sk_memory_order mo) {
SkASSERT(mo == sk_memory_order_relaxed ||
mo == sk_memory_order_seq_cst ||
mo == sk_memory_order_acquire ||
mo == sk_memory_order_consume);
const std::atomic<T>* ap = reinterpret_cast<const std::atomic<T>*>(ptr);
return std::atomic_load_explicit(ap, (std::memory_order)mo);
}
template <typename T>
void sk_atomic_store(T* ptr, T val, sk_memory_order mo) {
SkASSERT(mo == sk_memory_order_relaxed ||
mo == sk_memory_order_seq_cst ||
mo == sk_memory_order_release);
std::atomic<T>* ap = reinterpret_cast<std::atomic<T>*>(ptr);
return std::atomic_store_explicit(ap, val, (std::memory_order)mo);
}
template <typename T>
T sk_atomic_fetch_add(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_fetch_add_explicit(ap, val, (std::memory_order)mo);
}
template <typename T>
T sk_atomic_fetch_sub(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_fetch_sub_explicit(ap, val, (std::memory_order)mo);
}
template <typename T>
bool sk_atomic_compare_exchange(T* ptr, T* expected, T desired,
sk_memory_order success,
sk_memory_order failure) {
// All values of success are valid.
SkASSERT(failure == sk_memory_order_relaxed ||
failure == sk_memory_order_seq_cst ||
failure == sk_memory_order_acquire ||
failure == sk_memory_order_consume);
SkASSERT(failure <= success);
std::atomic<T>* ap = reinterpret_cast<std::atomic<T>*>(ptr);
return std::atomic_compare_exchange_strong_explicit(ap, expected, desired,
(std::memory_order)success,
(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);
}
// ~~~~~~~~ Legacy APIs ~~~~~~~~~
// From here down we have shims for our old atomics API, to be weaned off of.
// We use the default sequentially-consistent memory order to make things simple

View File

@ -797,9 +797,6 @@ HDRS = ['include/c/sk_canvas.h',
'include/images/SkPageFlipper.h',
'include/pathops/SkPathOps.h',
'include/pipe/SkGPipe.h',
'include/ports/SkAtomics_atomic.h',
'include/ports/SkAtomics_std.h',
'include/ports/SkAtomics_sync.h',
'include/ports/SkFontConfigInterface.h',
'include/ports/SkFontMgr.h',
'include/ports/SkFontMgr_android.h',