]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_atomic_clang.h
Merge compiler-rt trunk r321017 to contrib/compiler-rt.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_atomic_clang.h
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 #if defined(_MIPS_SIM) && _MIPS_SIM == _ABIO32
82   if (sizeof(*a) == 8) {
83     Type volatile *val_ptr = const_cast<Type volatile *>(&a->val_dont_use);
84     prev = __mips_sync_val_compare_and_swap<u64>(
85         reinterpret_cast<u64 volatile *>(val_ptr), (u64)cmpv, (u64)xchg);
86   } else {
87     prev = __sync_val_compare_and_swap(&a->val_dont_use, cmpv, xchg);
88   }
89 #else
90   prev = __sync_val_compare_and_swap(&a->val_dont_use, cmpv, xchg);
91 #endif
92   if (prev == cmpv) return true;
93   *cmp = prev;
94   return false;
95 }
96
97 template<typename T>
98 INLINE bool atomic_compare_exchange_weak(volatile T *a,
99                                          typename T::Type *cmp,
100                                          typename T::Type xchg,
101                                          memory_order mo) {
102   return atomic_compare_exchange_strong(a, cmp, xchg, mo);
103 }
104
105 }  // namespace __sanitizer
106
107 #undef ATOMIC_ORDER
108
109 #endif  // SANITIZER_ATOMIC_CLANG_H