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