1 //===-- sanitizer_atomic_clang.h --------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
11 // Not intended for direct inclusion. Include sanitizer_atomic.h.
13 //===----------------------------------------------------------------------===//
15 #ifndef SANITIZER_ATOMIC_CLANG_H
16 #define SANITIZER_ATOMIC_CLANG_H
18 #if defined(__i386__) || defined(__x86_64__)
19 # include "sanitizer_atomic_clang_x86.h"
21 # include "sanitizer_atomic_clang_other.h"
24 namespace __sanitizer {
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'
35 // See http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
36 // for mappings of the memory model to different processors.
38 INLINE void atomic_signal_fence(memory_order) {
39 __asm__ __volatile__("" ::: "memory");
42 INLINE void atomic_thread_fence(memory_order) {
47 INLINE typename T::Type atomic_fetch_add(volatile T *a,
48 typename T::Type v, memory_order mo) {
50 DCHECK(!((uptr)a % sizeof(*a)));
51 return __sync_fetch_and_add(&a->val_dont_use, v);
55 INLINE typename T::Type atomic_fetch_sub(volatile T *a,
56 typename T::Type v, memory_order mo) {
58 DCHECK(!((uptr)a % sizeof(*a)));
59 return __sync_fetch_and_add(&a->val_dont_use, -v);
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))
68 v = __sync_lock_test_and_set(&a->val_dont_use, v);
69 if (mo == memory_order_seq_cst)
75 INLINE bool atomic_compare_exchange_strong(volatile T *a, typename T::Type *cmp,
76 typename T::Type xchg,
78 typedef typename T::Type Type;
81 prev = __sync_val_compare_and_swap(&a->val_dont_use, cmpv, xchg);
82 if (prev == cmpv) return true;
88 INLINE bool atomic_compare_exchange_weak(volatile T *a,
89 typename T::Type *cmp,
90 typename T::Type xchg,
92 return atomic_compare_exchange_strong(a, cmp, xchg, mo);
95 } // namespace __sanitizer
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"
106 #endif // SANITIZER_ATOMIC_CLANG_H