]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_atomic_clang_other.h
MFV r321673:
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_atomic_clang_other.h
1 //===-- sanitizer_atomic_clang_other.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_OTHER_H
16 #define SANITIZER_ATOMIC_CLANG_OTHER_H
17
18 namespace __sanitizer {
19
20 // MIPS32 does not support atomic > 4 bytes. To address this lack of
21 // functionality, the sanitizer library provides helper methods which use an
22 // internal spin lock mechanism to emulate atomic oprations when the size is
23 // 8 bytes.
24 #if defined(_MIPS_SIM) && _MIPS_SIM == _ABIO32
25 static void __spin_lock(volatile int *lock) {
26   while (__sync_lock_test_and_set(lock, 1))
27     while (*lock) {
28     }
29 }
30
31 static void __spin_unlock(volatile int *lock) { __sync_lock_release(lock); }
32
33
34 // Make sure the lock is on its own cache line to prevent false sharing.
35 // Put it inside a struct that is aligned and padded to the typical MIPS
36 // cacheline which is 32 bytes.
37 static struct {
38   int lock;
39   char pad[32 - sizeof(int)];
40 } __attribute__((aligned(32))) lock = {0};
41
42 template <class T>
43 T __mips_sync_fetch_and_add(volatile T *ptr, T val) {
44   T ret;
45
46   __spin_lock(&lock.lock);
47
48   ret = *ptr;
49   *ptr = ret + val;
50
51   __spin_unlock(&lock.lock);
52
53   return ret;
54 }
55
56 template <class T>
57 T __mips_sync_val_compare_and_swap(volatile T *ptr, T oldval, T newval) {
58   T ret;
59   __spin_lock(&lock.lock);
60
61   ret = *ptr;
62   if (ret == oldval) *ptr = newval;
63
64   __spin_unlock(&lock.lock);
65
66   return ret;
67 }
68 #endif
69
70 INLINE void proc_yield(int cnt) {
71   __asm__ __volatile__("" ::: "memory");
72 }
73
74 template<typename T>
75 INLINE typename T::Type atomic_load(
76     const volatile T *a, memory_order mo) {
77   DCHECK(mo & (memory_order_relaxed | memory_order_consume
78       | memory_order_acquire | memory_order_seq_cst));
79   DCHECK(!((uptr)a % sizeof(*a)));
80   typename T::Type v;
81
82   if (sizeof(*a) < 8 || sizeof(void*) == 8) {
83     // Assume that aligned loads are atomic.
84     if (mo == memory_order_relaxed) {
85       v = a->val_dont_use;
86     } else if (mo == memory_order_consume) {
87       // Assume that processor respects data dependencies
88       // (and that compiler won't break them).
89       __asm__ __volatile__("" ::: "memory");
90       v = a->val_dont_use;
91       __asm__ __volatile__("" ::: "memory");
92     } else if (mo == memory_order_acquire) {
93       __asm__ __volatile__("" ::: "memory");
94       v = a->val_dont_use;
95       __sync_synchronize();
96     } else {  // seq_cst
97       // E.g. on POWER we need a hw fence even before the store.
98       __sync_synchronize();
99       v = a->val_dont_use;
100       __sync_synchronize();
101     }
102   } else {
103     // 64-bit load on 32-bit platform.
104     // Gross, but simple and reliable.
105     // Assume that it is not in read-only memory.
106 #if defined(_MIPS_SIM) && _MIPS_SIM == _ABIO32
107     typename T::Type volatile *val_ptr =
108         const_cast<typename T::Type volatile *>(&a->val_dont_use);
109     v = __mips_sync_fetch_and_add<u64>(
110         reinterpret_cast<u64 volatile *>(val_ptr), 0);
111 #else
112     v = __sync_fetch_and_add(
113         const_cast<typename T::Type volatile *>(&a->val_dont_use), 0);
114 #endif
115   }
116   return v;
117 }
118
119 template<typename T>
120 INLINE void atomic_store(volatile T *a, typename T::Type v, memory_order mo) {
121   DCHECK(mo & (memory_order_relaxed | memory_order_release
122       | memory_order_seq_cst));
123   DCHECK(!((uptr)a % sizeof(*a)));
124
125   if (sizeof(*a) < 8 || sizeof(void*) == 8) {
126     // Assume that aligned loads are atomic.
127     if (mo == memory_order_relaxed) {
128       a->val_dont_use = v;
129     } else if (mo == memory_order_release) {
130       __sync_synchronize();
131       a->val_dont_use = v;
132       __asm__ __volatile__("" ::: "memory");
133     } else {  // seq_cst
134       __sync_synchronize();
135       a->val_dont_use = v;
136       __sync_synchronize();
137     }
138   } else {
139     // 64-bit store on 32-bit platform.
140     // Gross, but simple and reliable.
141     typename T::Type cmp = a->val_dont_use;
142     typename T::Type cur;
143     for (;;) {
144 #if defined(_MIPS_SIM) && _MIPS_SIM == _ABIO32
145       typename T::Type volatile *val_ptr =
146           const_cast<typename T::Type volatile *>(&a->val_dont_use);
147       cur = __mips_sync_val_compare_and_swap<u64>(
148           reinterpret_cast<u64 volatile *>(val_ptr), (u64)cmp, (u64)v);
149 #else
150       cur = __sync_val_compare_and_swap(&a->val_dont_use, cmp, v);
151 #endif
152       if (cmp == v)
153         break;
154       cmp = cur;
155     }
156   }
157 }
158
159 }  // namespace __sanitizer
160
161 #endif  // #ifndef SANITIZER_ATOMIC_CLANG_OTHER_H