Add subtract to atomics.

BUG=skia:

Review URL: https://codereview.chromium.org/1348113004
This commit is contained in:
herb 2015-09-16 07:46:17 -07:00 committed by Commit bot
parent 42bd6bfd0d
commit 3667d5bf3f
4 changed files with 25 additions and 0 deletions

View File

@ -29,6 +29,9 @@ void sk_atomic_store(T*, T, sk_memory_order = sk_memory_order_seq_cst);
template <typename T>
T sk_atomic_fetch_add(T*, T, sk_memory_order = sk_memory_order_seq_cst);
template <typename T>
T sk_atomic_fetch_sub(T*, T, sk_memory_order = sk_memory_order_seq_cst);
template <typename T>
bool sk_atomic_compare_exchange(T*, T* expected, T desired,
sk_memory_order success = sk_memory_order_seq_cst,
@ -58,6 +61,10 @@ public:
return sk_atomic_fetch_add(&fVal, val, mo);
}
T fetch_sub(const T& val, sk_memory_order mo = sk_memory_order_seq_cst) {
return sk_atomic_fetch_sub(&fVal, val, mo);
}
bool compare_exchange(T* expected, const T& desired,
sk_memory_order success = sk_memory_order_seq_cst,
sk_memory_order failure = sk_memory_order_seq_cst) {

View File

@ -31,6 +31,12 @@ T sk_atomic_fetch_add(T* ptr, T val, sk_memory_order mo) {
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,

View File

@ -38,6 +38,13 @@ T sk_atomic_fetch_add(T* ptr, T val, sk_memory_order mo) {
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,

View File

@ -45,6 +45,11 @@ 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);