libgcc/ChangeLog:
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_atomic_clang_other.h
blobc66c0992e1b0b13c71139796655cd44c7eaa3eb5
1 //===-- sanitizer_atomic_clang_other.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.
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");
22 template<typename T>
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)));
28 typename T::Type v;
30 if (sizeof(*a) < 8 || sizeof(void*) == 8) {
31 // Assume that aligned loads are atomic.
32 if (mo == memory_order_relaxed) {
33 v = a->val_dont_use;
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");
38 v = a->val_dont_use;
39 __asm__ __volatile__("" ::: "memory");
40 } else if (mo == memory_order_acquire) {
41 __asm__ __volatile__("" ::: "memory");
42 v = a->val_dont_use;
43 __sync_synchronize();
44 } else { // seq_cst
45 // E.g. on POWER we need a hw fence even before the store.
46 __sync_synchronize();
47 v = a->val_dont_use;
48 __sync_synchronize();
50 } else {
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);
57 return v;
60 template<typename T>
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) {
69 a->val_dont_use = v;
70 } else if (mo == memory_order_release) {
71 __sync_synchronize();
72 a->val_dont_use = v;
73 __asm__ __volatile__("" ::: "memory");
74 } else { // seq_cst
75 __sync_synchronize();
76 a->val_dont_use = v;
77 __sync_synchronize();
79 } else {
80 // 64-bit store on 32-bit platform.
81 // Gross, but simple and reliable.
82 typename T::Type cmp = a->val_dont_use;
83 typename T::Type cur;
84 for (;;) {
85 cur = __sync_val_compare_and_swap(&a->val_dont_use, cmp, v);
86 if (cmp == v)
87 break;
88 cmp = cur;
93 } // namespace __sanitizer
95 #endif // #ifndef SANITIZER_ATOMIC_CLANG_OTHER_H