SkAtomics: add asserts for valid memory orders.

Taken from http://en.cppreference.com/w/cpp/atomic/atomic and https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html, which agree.

No public API changes.
TBR=reed@google.com

BUG=skia:

Review URL: https://codereview.chromium.org/868843003
This commit is contained in:
mtklein 2015-02-09 14:13:44 -08:00 committed by Commit bot
parent d5d27d9b14
commit 01f797fcb0
2 changed files with 28 additions and 0 deletions

View File

@ -3,16 +3,24 @@
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);
}
@ -20,6 +28,12 @@ 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);
}

View File

@ -7,18 +7,26 @@
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);
}
@ -27,6 +35,12 @@ 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,