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_atomic_DEFINED
9 #define SkAtomics_atomic_DEFINED
10
11 template <typename T>
sk_atomic_load(const T * ptr,sk_memory_order mo)12 T sk_atomic_load(const T* ptr, sk_memory_order mo) {
13 SkASSERT(mo == sk_memory_order_relaxed ||
14 mo == sk_memory_order_seq_cst ||
15 mo == sk_memory_order_acquire ||
16 mo == sk_memory_order_consume);
17 return __atomic_load_n(ptr, mo);
18 }
19
20 template <typename T>
sk_atomic_store(T * ptr,T val,sk_memory_order mo)21 void sk_atomic_store(T* ptr, T val, sk_memory_order mo) {
22 SkASSERT(mo == sk_memory_order_relaxed ||
23 mo == sk_memory_order_seq_cst ||
24 mo == sk_memory_order_release);
25 __atomic_store_n(ptr, val, mo);
26 }
27
28 template <typename T>
sk_atomic_fetch_add(T * ptr,T val,sk_memory_order mo)29 T sk_atomic_fetch_add(T* ptr, T val, sk_memory_order mo) {
30 // All values of mo are valid.
31 return __atomic_fetch_add(ptr, val, mo);
32 }
33
34 template <typename T>
sk_atomic_compare_exchange(T * ptr,T * expected,T desired,sk_memory_order success,sk_memory_order failure)35 bool sk_atomic_compare_exchange(T* ptr, T* expected, T desired,
36 sk_memory_order success,
37 sk_memory_order failure) {
38 // All values of success are valid.
39 SkASSERT(failure == sk_memory_order_relaxed ||
40 failure == sk_memory_order_seq_cst ||
41 failure == sk_memory_order_acquire ||
42 failure == sk_memory_order_consume);
43 SkASSERT(failure <= success);
44 return __atomic_compare_exchange_n(ptr, expected, desired, false/*weak?*/, success, failure);
45 }
46
47 template <typename T>
sk_atomic_exchange(T * ptr,T val,sk_memory_order mo)48 T sk_atomic_exchange(T* ptr, T val, sk_memory_order mo) {
49 // All values of mo are valid.
50 return __atomic_exchange_n(ptr, val, mo);
51 }
52
53 #endif//SkAtomics_atomic_DEFINED
54