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_arm64_AtomicOperations_arm64_h
10 #define jit_arm64_AtomicOperations_arm64_h
11 
12 #include "mozilla/Assertions.h"
13 #include "mozilla/Types.h"
14 
15 inline bool
isLockfree8()16 js::jit::AtomicOperations::isLockfree8()
17 {
18     MOZ_ASSERT(__atomic_always_lock_free(sizeof(int8_t), 0));
19     MOZ_ASSERT(__atomic_always_lock_free(sizeof(int16_t), 0));
20     MOZ_ASSERT(__atomic_always_lock_free(sizeof(int32_t), 0));
21     return true;
22 }
23 
24 inline void
fenceSeqCst()25 js::jit::AtomicOperations::fenceSeqCst()
26 {
27     __atomic_thread_fence(__ATOMIC_SEQ_CST);
28 }
29 
30 template<typename T>
31 inline T
loadSeqCst(T * addr)32 js::jit::AtomicOperations::loadSeqCst(T* addr)
33 {
34     MOZ_ASSERT(sizeof(T) < 8 || isLockfree8());
35     T v;
36     __atomic_load(addr, &v, __ATOMIC_SEQ_CST);
37     return v;
38 }
39 
40 template<typename T>
41 inline void
storeSeqCst(T * addr,T val)42 js::jit::AtomicOperations::storeSeqCst(T* addr, T val)
43 {
44     MOZ_ASSERT(sizeof(T) < 8 || isLockfree8());
45     __atomic_store(addr, &val, __ATOMIC_SEQ_CST);
46 }
47 
48 template<typename T>
49 inline T
exchangeSeqCst(T * addr,T val)50 js::jit::AtomicOperations::exchangeSeqCst(T* addr, T val)
51 {
52     MOZ_ASSERT(sizeof(T) < 8 || isLockfree8());
53     T v;
54     __atomic_exchange(addr, &val, &v, __ATOMIC_SEQ_CST);
55     return v;
56 }
57 
58 template<typename T>
59 inline T
compareExchangeSeqCst(T * addr,T oldval,T newval)60 js::jit::AtomicOperations::compareExchangeSeqCst(T* addr, T oldval, T newval)
61 {
62     MOZ_ASSERT(sizeof(T) < 8 || isLockfree8());
63     __atomic_compare_exchange(addr, &oldval, &newval, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
64     return oldval;
65 }
66 
67 template<typename T>
68 inline T
fetchAddSeqCst(T * addr,T val)69 js::jit::AtomicOperations::fetchAddSeqCst(T* addr, T val)
70 {
71     static_assert(sizeof(T) <= 4, "not available for 8-byte values yet");
72     return __atomic_fetch_add(addr, val, __ATOMIC_SEQ_CST);
73 }
74 
75 template<typename T>
76 inline T
fetchSubSeqCst(T * addr,T val)77 js::jit::AtomicOperations::fetchSubSeqCst(T* addr, T val)
78 {
79     static_assert(sizeof(T) <= 4, "not available for 8-byte values yet");
80     return __atomic_fetch_sub(addr, val, __ATOMIC_SEQ_CST);
81 }
82 
83 template<typename T>
84 inline T
fetchAndSeqCst(T * addr,T val)85 js::jit::AtomicOperations::fetchAndSeqCst(T* addr, T val)
86 {
87     static_assert(sizeof(T) <= 4, "not available for 8-byte values yet");
88     return __atomic_fetch_and(addr, val, __ATOMIC_SEQ_CST);
89 }
90 
91 template<typename T>
92 inline T
fetchOrSeqCst(T * addr,T val)93 js::jit::AtomicOperations::fetchOrSeqCst(T* addr, T val)
94 {
95     static_assert(sizeof(T) <= 4, "not available for 8-byte values yet");
96     return __atomic_fetch_or(addr, val, __ATOMIC_SEQ_CST);
97 }
98 
99 template<typename T>
100 inline T
fetchXorSeqCst(T * addr,T val)101 js::jit::AtomicOperations::fetchXorSeqCst(T* addr, T val)
102 {
103     static_assert(sizeof(T) <= 4, "not available for 8-byte values yet");
104     return __atomic_fetch_xor(addr, val, __ATOMIC_SEQ_CST);
105 }
106 
107 template <typename T>
108 inline T
loadSafeWhenRacy(T * addr)109 js::jit::AtomicOperations::loadSafeWhenRacy(T* addr)
110 {
111     return *addr; // FIXME (1208663): not yet safe
112 }
113 
114 template <typename T>
115 inline void
storeSafeWhenRacy(T * addr,T val)116 js::jit::AtomicOperations::storeSafeWhenRacy(T* addr, T val)
117 {
118     *addr = val; // FIXME (1208663): not yet safe
119 }
120 
121 inline void
memcpySafeWhenRacy(void * dest,const void * src,size_t nbytes)122 js::jit::AtomicOperations::memcpySafeWhenRacy(void* dest, const void* src,
123                                               size_t nbytes)
124 {
125     memcpy(dest, src, nbytes); // FIXME (1208663): not yet safe
126 }
127 
128 inline void
memmoveSafeWhenRacy(void * dest,const void * src,size_t nbytes)129 js::jit::AtomicOperations::memmoveSafeWhenRacy(void* dest, const void* src,
130                                                size_t nbytes)
131 {
132     memmove(dest, src, nbytes); // FIXME (1208663): not yet safe
133 }
134 
135 template<size_t nbytes>
136 inline void
acquire(void * addr)137 js::jit::RegionLock::acquire(void* addr)
138 {
139     uint32_t zero = 0;
140     uint32_t one = 1;
141     while (!__atomic_compare_exchange(&spinlock, &zero, &one, false, __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE)) {
142         zero = 0;
143         continue;
144     }
145 }
146 
147 template<size_t nbytes>
148 inline void
release(void * addr)149 js::jit::RegionLock::release(void* addr)
150 {
151     MOZ_ASSERT(AtomicOperations::loadSeqCst(&spinlock) == 1, "releasing unlocked region lock");
152     uint32_t zero = 0;
153     __atomic_store(&spinlock, &zero, __ATOMIC_SEQ_CST);
154 }
155 
156 #endif // jit_arm64_AtomicOperations_arm64_h
157