1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim: set ts=8 sts=4 et sw=4 tw=99:
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 /* For documentation, see jit/AtomicOperations.h */
8 
9 #ifndef jit_mips_shared_AtomicOperations_mips_shared_h
10 #define jit_mips_shared_AtomicOperations_mips_shared_h
11 
12 #include "mozilla/Assertions.h"
13 #include "mozilla/Types.h"
14 
15 #if defined(__clang__) || defined(__GNUC__)
16 
17 // The default implementation tactic for gcc/clang is to use the newer
18 // __atomic intrinsics added for use in C++11 <atomic>.  Where that
19 // isn't available, we use GCC's older __sync functions instead.
20 //
21 // ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS is kept as a backward
22 // compatible option for older compilers: enable this to use GCC's old
23 // __sync functions instead of the newer __atomic functions.  This
24 // will be required for GCC 4.6.x and earlier, and probably for Clang
25 // 3.1, should we need to use those versions.
26 
27 //#define ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
28 
29 inline bool
isLockfree8()30 js::jit::AtomicOperations::isLockfree8()
31 {
32 # ifndef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
33     MOZ_ASSERT(__atomic_always_lock_free(sizeof(int8_t), 0));
34     MOZ_ASSERT(__atomic_always_lock_free(sizeof(int16_t), 0));
35     MOZ_ASSERT(__atomic_always_lock_free(sizeof(int32_t), 0));
36 #  if _MIPS_SIM == _ABI64
37     MOZ_ASSERT(__atomic_always_lock_free(sizeof(int64_t), 0));
38 #  endif
39     return true;
40 # else
41     return false;
42 # endif
43 }
44 
45 inline void
fenceSeqCst()46 js::jit::AtomicOperations::fenceSeqCst()
47 {
48 # ifdef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
49     __sync_synchronize();
50 # else
51     __atomic_thread_fence(__ATOMIC_SEQ_CST);
52 # endif
53 }
54 
55 template<typename T>
56 inline T
loadSeqCst(T * addr)57 js::jit::AtomicOperations::loadSeqCst(T* addr)
58 {
59     MOZ_ASSERT(sizeof(T) < 8 || isLockfree8());
60 # ifdef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
61     __sync_synchronize();
62     T v = *addr;
63     __sync_synchronize();
64 # else
65     T v;
66     __atomic_load(addr, &v, __ATOMIC_SEQ_CST);
67 # endif
68     return v;
69 }
70 
71 template<typename T>
72 inline void
storeSeqCst(T * addr,T val)73 js::jit::AtomicOperations::storeSeqCst(T* addr, T val)
74 {
75     MOZ_ASSERT(sizeof(T) < 8 || isLockfree8());
76 # ifdef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
77     __sync_synchronize();
78     *addr = val;
79     __sync_synchronize();
80 # else
81     __atomic_store(addr, &val, __ATOMIC_SEQ_CST);
82 # endif
83 }
84 
85 template<typename T>
86 inline T
compareExchangeSeqCst(T * addr,T oldval,T newval)87 js::jit::AtomicOperations::compareExchangeSeqCst(T* addr, T oldval, T newval)
88 {
89     MOZ_ASSERT(sizeof(T) < 8 || isLockfree8());
90 # ifdef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
91     return __sync_val_compare_and_swap(addr, oldval, newval);
92 # else
93     __atomic_compare_exchange(addr, &oldval, &newval, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
94     return oldval;
95 # endif
96 }
97 
98 template<typename T>
99 inline T
fetchAddSeqCst(T * addr,T val)100 js::jit::AtomicOperations::fetchAddSeqCst(T* addr, T val)
101 {
102     static_assert(sizeof(T) <= 4, "not available for 8-byte values yet");
103 # ifdef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
104     return __sync_fetch_and_add(addr, val);
105 # else
106     return __atomic_fetch_add(addr, val, __ATOMIC_SEQ_CST);
107 # endif
108 }
109 
110 template<typename T>
111 inline T
fetchSubSeqCst(T * addr,T val)112 js::jit::AtomicOperations::fetchSubSeqCst(T* addr, T val)
113 {
114     static_assert(sizeof(T) <= 4, "not available for 8-byte values yet");
115 # ifdef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
116     return __sync_fetch_and_sub(addr, val);
117 # else
118     return __atomic_fetch_sub(addr, val, __ATOMIC_SEQ_CST);
119 # endif
120 }
121 
122 template<typename T>
123 inline T
fetchAndSeqCst(T * addr,T val)124 js::jit::AtomicOperations::fetchAndSeqCst(T* addr, T val)
125 {
126     static_assert(sizeof(T) <= 4, "not available for 8-byte values yet");
127 # ifdef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
128     return __sync_fetch_and_and(addr, val);
129 # else
130     return __atomic_fetch_and(addr, val, __ATOMIC_SEQ_CST);
131 # endif
132 }
133 
134 template<typename T>
135 inline T
fetchOrSeqCst(T * addr,T val)136 js::jit::AtomicOperations::fetchOrSeqCst(T* addr, T val)
137 {
138     static_assert(sizeof(T) <= 4, "not available for 8-byte values yet");
139 # ifdef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
140     return __sync_fetch_and_or(addr, val);
141 # else
142     return __atomic_fetch_or(addr, val, __ATOMIC_SEQ_CST);
143 # endif
144 }
145 
146 template<typename T>
147 inline T
fetchXorSeqCst(T * addr,T val)148 js::jit::AtomicOperations::fetchXorSeqCst(T* addr, T val)
149 {
150     static_assert(sizeof(T) <= 4, "not available for 8-byte values yet");
151 # ifdef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
152     return __sync_fetch_and_xor(addr, val);
153 # else
154     return __atomic_fetch_xor(addr, val, __ATOMIC_SEQ_CST);
155 # endif
156 }
157 
158 template<typename T>
159 inline T
loadSafeWhenRacy(T * addr)160 js::jit::AtomicOperations::loadSafeWhenRacy(T* addr)
161 {
162     return *addr;               // FIXME (1208663): not yet safe
163 }
164 
165 template<typename T>
166 inline void
storeSafeWhenRacy(T * addr,T val)167 js::jit::AtomicOperations::storeSafeWhenRacy(T* addr, T val)
168 {
169     *addr = val;                // FIXME (1208663): not yet safe
170 }
171 
172 inline void
memcpySafeWhenRacy(void * dest,const void * src,size_t nbytes)173 js::jit::AtomicOperations::memcpySafeWhenRacy(void* dest, const void* src, size_t nbytes)
174 {
175     ::memcpy(dest, src, nbytes); // FIXME (1208663): not yet safe
176 }
177 
178 inline void
memmoveSafeWhenRacy(void * dest,const void * src,size_t nbytes)179 js::jit::AtomicOperations::memmoveSafeWhenRacy(void* dest, const void* src, size_t nbytes)
180 {
181     ::memmove(dest, src, nbytes); // FIXME (1208663): not yet safe
182 }
183 
184 template<typename T>
185 inline T
exchangeSeqCst(T * addr,T val)186 js::jit::AtomicOperations::exchangeSeqCst(T* addr, T val)
187 {
188     MOZ_ASSERT(sizeof(T) < 8 || isLockfree8());
189 # ifdef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
190     T v;
191     __sync_synchronize();
192     do {
193 	v = *addr;
194     } while (__sync_val_compare_and_swap(addr, v, val) != v);
195     return v;
196 # else
197     T v;
198     __atomic_exchange(addr, &val, &v, __ATOMIC_SEQ_CST);
199     return v;
200 # endif
201 }
202 
203 template<size_t nbytes>
204 inline void
acquire(void * addr)205 js::jit::RegionLock::acquire(void* addr)
206 {
207 # ifdef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
208     while (!__sync_bool_compare_and_swap(&spinlock, 0, 1))
209         ;
210 # else
211     uint32_t zero = 0;
212     uint32_t one = 1;
213     while (!__atomic_compare_exchange(&spinlock, &zero, &one, false, __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE)) {
214         zero = 0;
215         continue;
216     }
217 # endif
218 }
219 
220 template<size_t nbytes>
221 inline void
release(void * addr)222 js::jit::RegionLock::release(void* addr)
223 {
224     MOZ_ASSERT(AtomicOperations::loadSeqCst(&spinlock) == 1, "releasing unlocked region lock");
225 # ifdef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
226     __sync_sub_and_fetch(&spinlock, 1);
227 # else
228     uint32_t zero = 0;
229     __atomic_store(&spinlock, &zero, __ATOMIC_SEQ_CST);
230 # endif
231 }
232 
233 # undef ATOMICS_IMPLEMENTED_WITH_SYNC_INTRINSICS
234 
235 #elif defined(ENABLE_SHARED_ARRAY_BUFFER)
236 
237 # error "Either disable JS shared memory, use GCC or Clang, or add code here"
238 
239 #endif
240 
241 #endif // jit_mips_shared_AtomicOperations_mips_shared_h
242