1 /*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkAtomics_std_DEFINED
9 #define SkAtomics_std_DEFINED
10
11 // We try not to depend on the C++ standard library,
12 // but these uses of <atomic> should all inline, so we don't feel to bad here.
13 #include <atomic>
14
15 template <typename T>
sk_atomic_load(const T * ptr,sk_memory_order mo)16 T sk_atomic_load(const T* ptr, sk_memory_order mo) {
17 SkASSERT(mo == sk_memory_order_relaxed ||
18 mo == sk_memory_order_seq_cst ||
19 mo == sk_memory_order_acquire ||
20 mo == sk_memory_order_consume);
21 const std::atomic<T>* ap = reinterpret_cast<const std::atomic<T>*>(ptr);
22 return std::atomic_load_explicit(ap, (std::memory_order)mo);
23 }
24
25 template <typename T>
sk_atomic_store(T * ptr,T val,sk_memory_order mo)26 void sk_atomic_store(T* ptr, T val, sk_memory_order mo) {
27 SkASSERT(mo == sk_memory_order_relaxed ||
28 mo == sk_memory_order_seq_cst ||
29 mo == sk_memory_order_release);
30 std::atomic<T>* ap = reinterpret_cast<std::atomic<T>*>(ptr);
31 return std::atomic_store_explicit(ap, val, (std::memory_order)mo);
32 }
33
34 template <typename T>
sk_atomic_fetch_add(T * ptr,T val,sk_memory_order mo)35 T sk_atomic_fetch_add(T* ptr, T val, sk_memory_order mo) {
36 // All values of mo are valid.
37 std::atomic<T>* ap = reinterpret_cast<std::atomic<T>*>(ptr);
38 return std::atomic_fetch_add_explicit(ap, val, (std::memory_order)mo);
39 }
40
41 template <typename T>
sk_atomic_compare_exchange(T * ptr,T * expected,T desired,sk_memory_order success,sk_memory_order failure)42 bool sk_atomic_compare_exchange(T* ptr, T* expected, T desired,
43 sk_memory_order success,
44 sk_memory_order failure) {
45 // All values of success are valid.
46 SkASSERT(failure == sk_memory_order_relaxed ||
47 failure == sk_memory_order_seq_cst ||
48 failure == sk_memory_order_acquire ||
49 failure == sk_memory_order_consume);
50 SkASSERT(failure <= success);
51 std::atomic<T>* ap = reinterpret_cast<std::atomic<T>*>(ptr);
52 return std::atomic_compare_exchange_strong_explicit(ap, expected, desired,
53 (std::memory_order)success,
54 (std::memory_order)failure);
55 }
56
57 template <typename T>
sk_atomic_exchange(T * ptr,T val,sk_memory_order mo)58 T sk_atomic_exchange(T* ptr, T val, sk_memory_order mo) {
59 // All values of mo are valid.
60 std::atomic<T>* ap = reinterpret_cast<std::atomic<T>*>(ptr);
61 return std::atomic_exchange_explicit(ap, val, (std::memory_order)mo);
62 }
63
64 #endif//SkAtomics_std_DEFINED
65