1 //===-- sanitizer_atomic_clang_other.h --------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
9 // Not intended for direct inclusion. Include sanitizer_atomic.h.
11 //===----------------------------------------------------------------------===//
13 #ifndef SANITIZER_ATOMIC_CLANG_OTHER_H
14 #define SANITIZER_ATOMIC_CLANG_OTHER_H
16 namespace __sanitizer
{
18 INLINE
void proc_yield(int cnt
) {
19 __asm__
__volatile__("" ::: "memory");
23 INLINE typename
T::Type
atomic_load(
24 const volatile T
*a
, memory_order mo
) {
25 DCHECK(mo
& (memory_order_relaxed
| memory_order_consume
26 | memory_order_acquire
| memory_order_seq_cst
));
27 DCHECK(!((uptr
)a
% sizeof(*a
)));
30 if (sizeof(*a
) < 8 || sizeof(void*) == 8) {
31 // Assume that aligned loads are atomic.
32 if (mo
== memory_order_relaxed
) {
34 } else if (mo
== memory_order_consume
) {
35 // Assume that processor respects data dependencies
36 // (and that compiler won't break them).
37 __asm__
__volatile__("" ::: "memory");
39 __asm__
__volatile__("" ::: "memory");
40 } else if (mo
== memory_order_acquire
) {
41 __asm__
__volatile__("" ::: "memory");
45 // E.g. on POWER we need a hw fence even before the store.
51 // 64-bit load on 32-bit platform.
52 // Gross, but simple and reliable.
53 // Assume that it is not in read-only memory.
54 v
= __sync_fetch_and_add(
55 const_cast<typename
T::Type
volatile *>(&a
->val_dont_use
), 0);
61 INLINE
void atomic_store(volatile T
*a
, typename
T::Type v
, memory_order mo
) {
62 DCHECK(mo
& (memory_order_relaxed
| memory_order_release
63 | memory_order_seq_cst
));
64 DCHECK(!((uptr
)a
% sizeof(*a
)));
66 if (sizeof(*a
) < 8 || sizeof(void*) == 8) {
67 // Assume that aligned loads are atomic.
68 if (mo
== memory_order_relaxed
) {
70 } else if (mo
== memory_order_release
) {
73 __asm__
__volatile__("" ::: "memory");
80 // 64-bit store on 32-bit platform.
81 // Gross, but simple and reliable.
82 typename
T::Type cmp
= a
->val_dont_use
;
85 cur
= __sync_val_compare_and_swap(&a
->val_dont_use
, cmp
, v
);
93 } // namespace __sanitizer
95 #endif // #ifndef SANITIZER_ATOMIC_CLANG_OTHER_H