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 38 INLINE void atomic_signal_fence(memory_order) { 39 __asm__ __volatile__("" ::: "memory"); 40 } 41 42 INLINE void atomic_thread_fence(memory_order) { 43 __sync_synchronize(); 44 } 45 46 template<typename T> 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> 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> 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> 75 INLINE bool atomic_compare_exchange_strong(volatile T *a, typename T::Type *cmp, 76 typename T::Type xchg, 77 memory_order mo) { 78 typedef typename T::Type Type; 79 Type cmpv = *cmp; 80 Type prev; 81 prev = __sync_val_compare_and_swap(&a->val_dont_use, cmpv, xchg); 82 if (prev == cmpv) return true; 83 *cmp = prev; 84 return false; 85 } 86 87 template<typename T> 88 INLINE bool atomic_compare_exchange_weak(volatile T *a, 89 typename T::Type *cmp, 90 typename T::Type xchg, 91 memory_order mo) { 92 return atomic_compare_exchange_strong(a, cmp, xchg, mo); 93 } 94 95 } // namespace __sanitizer 96 97 // This include provides explicit template instantiations for atomic_uint64_t 98 // on MIPS32, which does not directly support 8 byte atomics. It has to 99 // proceed the template definitions above. 100 #if defined(_MIPS_SIM) && defined(_ABIO32) 101 #include "sanitizer_atomic_clang_mips.h" 102 #endif 103 104 #undef ATOMIC_ORDER 105 106 #endif // SANITIZER_ATOMIC_CLANG_H 107