1 //===-- sanitizer_atomic_clang.h --------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
11 // Not intended for direct inclusion. Include sanitizer_atomic.h.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef SANITIZER_ATOMIC_CLANG_H
16 #define SANITIZER_ATOMIC_CLANG_H
17
18 #if defined(__i386__) || defined(__x86_64__)
19 # include "sanitizer_atomic_clang_x86.h"
20 #else
21 # include "sanitizer_atomic_clang_other.h"
22 #endif
23
24 namespace __sanitizer {
25
26 // We would like to just use compiler builtin atomic operations
27 // for loads and stores, but they are mostly broken in clang:
28 // - they lead to vastly inefficient code generation
29 // (http://llvm.org/bugs/show_bug.cgi?id=17281)
30 // - 64-bit atomic operations are not implemented on x86_32
31 // (http://llvm.org/bugs/show_bug.cgi?id=15034)
32 // - they are not implemented on ARM
33 // error: undefined reference to '__atomic_load_4'
34
35 // See http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
36 // for mappings of the memory model to different processors.
37
atomic_signal_fence(memory_order)38 INLINE void atomic_signal_fence(memory_order) {
39 __asm__ __volatile__("" ::: "memory");
40 }
41
atomic_thread_fence(memory_order)42 INLINE void atomic_thread_fence(memory_order) {
43 __sync_synchronize();
44 }
45
46 template<typename T>
atomic_fetch_add(volatile T * a,typename T::Type v,memory_order mo)47 INLINE typename T::Type atomic_fetch_add(volatile T *a,
48 typename T::Type v, memory_order mo) {
49 (void)mo;
50 DCHECK(!((uptr)a % sizeof(*a)));
51 return __sync_fetch_and_add(&a->val_dont_use, v);
52 }
53
54 template<typename T>
atomic_fetch_sub(volatile T * a,typename T::Type v,memory_order mo)55 INLINE typename T::Type atomic_fetch_sub(volatile T *a,
56 typename T::Type v, memory_order mo) {
57 (void)mo;
58 DCHECK(!((uptr)a % sizeof(*a)));
59 return __sync_fetch_and_add(&a->val_dont_use, -v);
60 }
61
62 template<typename T>
atomic_exchange(volatile T * a,typename T::Type v,memory_order mo)63 INLINE typename T::Type atomic_exchange(volatile T *a,
64 typename T::Type v, memory_order mo) {
65 DCHECK(!((uptr)a % sizeof(*a)));
66 if (mo & (memory_order_release | memory_order_acq_rel | memory_order_seq_cst))
67 __sync_synchronize();
68 v = __sync_lock_test_and_set(&a->val_dont_use, v);
69 if (mo == memory_order_seq_cst)
70 __sync_synchronize();
71 return v;
72 }
73
74 template<typename T>
atomic_compare_exchange_strong(volatile T * a,typename T::Type * cmp,typename T::Type xchg,memory_order mo)75 INLINE bool atomic_compare_exchange_strong(volatile T *a,
76 typename T::Type *cmp,
77 typename T::Type xchg,
78 memory_order mo) {
79 typedef typename T::Type Type;
80 Type cmpv = *cmp;
81 Type prev = __sync_val_compare_and_swap(&a->val_dont_use, cmpv, xchg);
82 if (prev == cmpv)
83 return true;
84 *cmp = prev;
85 return false;
86 }
87
88 template<typename T>
atomic_compare_exchange_weak(volatile T * a,typename T::Type * cmp,typename T::Type xchg,memory_order mo)89 INLINE bool atomic_compare_exchange_weak(volatile T *a,
90 typename T::Type *cmp,
91 typename T::Type xchg,
92 memory_order mo) {
93 return atomic_compare_exchange_strong(a, cmp, xchg, mo);
94 }
95
96 } // namespace __sanitizer
97
98 #undef ATOMIC_ORDER
99
100 #endif // SANITIZER_ATOMIC_CLANG_H
101