OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkBarriers_tsan_DEFINED | 8 #ifndef SkBarriers_tsan_DEFINED |
9 #define SkBarriers_tsan_DEFINED | 9 #define SkBarriers_tsan_DEFINED |
10 | 10 |
11 #include <sanitizer/tsan_interface_atomic.h> | |
12 | |
13 static inline void sk_compiler_barrier() { asm volatile("" : : : "memory"); } | 11 static inline void sk_compiler_barrier() { asm volatile("" : : : "memory"); } |
14 | 12 |
15 // We'd do this as separate functions, but you can't partially specialize functi
ons... | 13 template <typename T> |
16 template <typename T, size_t bits> | 14 T sk_acquire_load(T* ptr) { |
17 struct SkBarriers { | 15 SkASSERT(__atomic_always_lock_free(sizeof(T), ptr)); |
18 static T AcquireLoad(T*); | 16 return __atomic_load_n(ptr, __ATOMIC_ACQUIRE); |
19 static void ReleaseStore(T*, T); | 17 } |
20 }; | |
21 | |
22 #define SK_BARRIERS(BITS)
\ | |
23 template <typename T>
\ | |
24 struct SkBarriers<T, BITS> {
\ | |
25 static T AcquireLoad(T* ptr) {
\ | |
26 return (T)__tsan_atomic ## BITS ## _load((__tsan_atomic ## BITS*)ptr
, \ | |
27 __tsan_memory_order_acquire
); \ | |
28 }
\ | |
29 static void ReleaseStore(T* ptr, T val) {
\ | |
30 __tsan_atomic ## BITS ## _store((__tsan_atomic ## BITS*)ptr,
\ | |
31 val,
\ | |
32 __tsan_memory_order_release);
\ | |
33 }
\ | |
34 } | |
35 SK_BARRIERS(8); | |
36 SK_BARRIERS(16); | |
37 SK_BARRIERS(32); | |
38 SK_BARRIERS(64); | |
39 #undef SK_BARRIERS | |
40 | 18 |
41 template <typename T> | 19 template <typename T> |
42 T sk_acquire_load(T* ptr) { return SkBarriers<T, 8*sizeof(T)>::AcquireLoad(ptr);
} | 20 void sk_release_store(T* ptr, T val) { |
43 | 21 SkASSERT(__atomic_always_lock_free(sizeof(T), ptr)); |
44 template <typename T> | 22 return __atomic_store_n(ptr, val, __ATOMIC_RELEASE); |
45 void sk_release_store(T* ptr, T val) { SkBarriers<T, 8*sizeof(T)>::ReleaseStore(
ptr, val); } | 23 } |
46 | |
47 | 24 |
48 #endif//SkBarriers_tsan_DEFINED | 25 #endif//SkBarriers_tsan_DEFINED |
OLD | NEW |