]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libsanitizer/sanitizer_common/sanitizer_atomic_clang.h
1 //===-- sanitizer_atomic_clang.h --------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
10 // Not intended for direct inclusion. Include sanitizer_atomic.h.
12 //===----------------------------------------------------------------------===//
14 #ifndef SANITIZER_ATOMIC_CLANG_H
15 #define SANITIZER_ATOMIC_CLANG_H
17 #if defined(__i386__) || defined(__x86_64__)
18 # include "sanitizer_atomic_clang_x86.h"
20 # include "sanitizer_atomic_clang_other.h"
23 namespace __sanitizer
{
25 // We would like to just use compiler builtin atomic operations
26 // for loads and stores, but they are mostly broken in clang:
27 // - they lead to vastly inefficient code generation
28 // (http://llvm.org/bugs/show_bug.cgi?id=17281)
29 // - 64-bit atomic operations are not implemented on x86_32
30 // (http://llvm.org/bugs/show_bug.cgi?id=15034)
31 // - they are not implemented on ARM
32 // error: undefined reference to '__atomic_load_4'
34 // See http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
35 // for mappings of the memory model to different processors.
37 INLINE
void atomic_signal_fence(memory_order
) {
38 __asm__
__volatile__("" ::: "memory");
41 INLINE
void atomic_thread_fence(memory_order
) {
46 INLINE typename
T::Type
atomic_fetch_add(volatile T
*a
,
47 typename
T::Type v
, memory_order mo
) {
49 DCHECK(!((uptr
)a
% sizeof(*a
)));
50 return __sync_fetch_and_add(&a
->val_dont_use
, v
);
54 INLINE typename
T::Type
atomic_fetch_sub(volatile T
*a
,
55 typename
T::Type v
, memory_order mo
) {
57 DCHECK(!((uptr
)a
% sizeof(*a
)));
58 return __sync_fetch_and_add(&a
->val_dont_use
, -v
);
62 INLINE typename
T::Type
atomic_exchange(volatile T
*a
,
63 typename
T::Type v
, memory_order mo
) {
64 DCHECK(!((uptr
)a
% sizeof(*a
)));
65 if (mo
& (memory_order_release
| memory_order_acq_rel
| memory_order_seq_cst
))
67 v
= __sync_lock_test_and_set(&a
->val_dont_use
, v
);
68 if (mo
== memory_order_seq_cst
)
74 INLINE
bool atomic_compare_exchange_strong(volatile T
*a
, typename
T::Type
*cmp
,
75 typename
T::Type xchg
,
77 typedef typename
T::Type Type
;
80 prev
= __sync_val_compare_and_swap(&a
->val_dont_use
, cmpv
, xchg
);
81 if (prev
== cmpv
) return true;
87 INLINE
bool atomic_compare_exchange_weak(volatile T
*a
,
88 typename
T::Type
*cmp
,
89 typename
T::Type xchg
,
91 return atomic_compare_exchange_strong(a
, cmp
, xchg
, mo
);
94 } // namespace __sanitizer
96 // This include provides explicit template instantiations for atomic_uint64_t
97 // on MIPS32, which does not directly support 8 byte atomics. It has to
98 // proceed the template definitions above.
99 #if defined(_MIPS_SIM) && defined(_ABIO32)
100 #include "sanitizer_atomic_clang_mips.h"
105 #endif // SANITIZER_ATOMIC_CLANG_H