Debian Codegraph @

js/src/jit/mips-shared/AtomicOperations-mips-shared.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
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 #include "builtin/AtomicsObject.h"
16 #include "vm/Uint8Clamped.h"
17
18 #if !defined(__clang__) && !defined(__GNUC__)
19 # error "This file only for gcc-compatible compilers"
20 #endif
21
22 inline bool js::jit::AtomicOperations::hasAtomic8() { return true; }
23
24 inline bool js::jit::AtomicOperations::isLockfree8() {
25 MOZ_ASSERT(__atomic_always_lock_free(sizeof(int8_t), 0));
26 MOZ_ASSERT(__atomic_always_lock_free(sizeof(int16_t), 0));
27 MOZ_ASSERT(__atomic_always_lock_free(sizeof(int32_t), 0));
28 MOZ_ASSERT(__atomic_always_lock_free(sizeof(int64_t), 0));
29 return true;
30 }
31
32 inline void js::jit::AtomicOperations::fenceSeqCst() {
33 __atomic_thread_fence(__ATOMIC_SEQ_CST);
34 }
35
36 namespace js {
37 namespace jit {
38
39 inline void AtomicPause() { asm volatile("sync" ::: "memory"); }
40
41 } // namespace jit
42 } // namespace js
43
44 inline void js::jit::AtomicOperations::pause() { AtomicPause(); }
45
46 template <typename T>
47 inline T js::jit::AtomicOperations::loadSeqCst(T* addr) {
48 static_assert(sizeof(T) <= sizeof(void*),
49 "atomics supported up to pointer size only");
50 T v;
51 __atomic_load(addr, &v, __ATOMIC_SEQ_CST);
52 return v;
53 }
54
55 template <typename T>
56 inline void js::jit::AtomicOperations::storeSeqCst(T* addr, T val) {
57 static_assert(sizeof(T) <= sizeof(void*),
58 "atomics supported up to pointer size only");
59 __atomic_store(addr, &val, __ATOMIC_SEQ_CST);
60 }
61
62 template <typename T>
63 inline T js::jit::AtomicOperations::compareExchangeSeqCst(T* addr, T oldval,
64 T newval) {
65 static_assert(sizeof(T) <= sizeof(void*),
66 "atomics supported up to pointer size only");
67 __atomic_compare_exchange(addr, &oldval, &newval, false, __ATOMIC_SEQ_CST,
68 __ATOMIC_SEQ_CST);
69 return oldval;
70 }
71
72 template <typename T>
73 inline T js::jit::AtomicOperations::fetchAddSeqCst(T* addr, T val) {
74 static_assert(sizeof(T) <= sizeof(void*),
75 "atomics supported up to pointer size only");
76 return __atomic_fetch_add(addr, val, __ATOMIC_SEQ_CST);
77 }
78
79 template <typename T>
80 inline T js::jit::AtomicOperations::fetchSubSeqCst(T* addr, T val) {
81 static_assert(sizeof(T) <= sizeof(void*),
82 "atomics supported up to pointer size only");
83 return __atomic_fetch_sub(addr, val, __ATOMIC_SEQ_CST);
84 }
85
86 template <typename T>
87 inline T js::jit::AtomicOperations::fetchAndSeqCst(T* addr, T val) {
88 static_assert(sizeof(T) <= sizeof(void*),
89 "atomics supported up to pointer size only");
90 return __atomic_fetch_and(addr, val, __ATOMIC_SEQ_CST);
91 }
92
93 template <typename T>
94 inline T js::jit::AtomicOperations::fetchOrSeqCst(T* addr, T val) {
95 static_assert(sizeof(T) <= sizeof(void*),
96 "atomics supported up to pointer size only");
97 return __atomic_fetch_or(addr, val, __ATOMIC_SEQ_CST);
98 }
99
100 template <typename T>
101 inline T js::jit::AtomicOperations::fetchXorSeqCst(T* addr, T val) {
102 static_assert(sizeof(T) <= sizeof(void*),
103 "atomics supported up to pointer size only");
104 return __atomic_fetch_xor(addr, val, __ATOMIC_SEQ_CST);
105 }
106
107 template <typename T>
108 inline T js::jit::AtomicOperations::loadSafeWhenRacy(T* addr) {
109 static_assert(sizeof(T) <= sizeof(void*),
110 "atomics supported up to pointer size only");
111 T v;
112 __atomic_load(addr, &v, __ATOMIC_RELAXED);
113 return v;
114 }
115
116 namespace js {
117 namespace jit {
118
119 template <>
120 inline uint8_clamped js::jit::AtomicOperations::loadSafeWhenRacy(
121 uint8_clamped* addr) {
122 uint8_t v;
123 __atomic_load((uint8_t*)addr, &v, __ATOMIC_RELAXED);
124 return uint8_clamped(v);
125 }
126
127 template <>
128 inline float js::jit::AtomicOperations::loadSafeWhenRacy(float* addr) {
129 return *addr;
130 }
131
132 template <>
133 inline double js::jit::AtomicOperations::loadSafeWhenRacy(double* addr) {
134 return *addr;
135 }
136
137 } // namespace jit
138 } // namespace js
139
140 template <typename T>
141 inline void js::jit::AtomicOperations::storeSafeWhenRacy(T* addr, T val) {
142 static_assert(sizeof(T) <= sizeof(void*),
143 "atomics supported up to pointer size only");
144 __atomic_store(addr, &val, __ATOMIC_RELAXED);
145 }
146
147 namespace js {
148 namespace jit {
149
150 template <>
151 inline void js::jit::AtomicOperations::storeSafeWhenRacy(uint8_clamped* addr,
152 uint8_clamped val) {
153 __atomic_store((uint8_t*)addr, (uint8_t*)&val, __ATOMIC_RELAXED);
154 }
155
156 template <>
157 inline void js::jit::AtomicOperations::storeSafeWhenRacy(float* addr,
158 float val) {
159 *addr = val;
160 }
161
162 template <>
163 inline void js::jit::AtomicOperations::storeSafeWhenRacy(double* addr,
164 double val) {
165 *addr = val;
166 }
167
168 } // namespace jit
169 } // namespace js
170
171 inline void js::jit::AtomicOperations::memcpySafeWhenRacy(void* dest,
172 const void* src,
173 size_t nbytes) {
174 MOZ_ASSERT(!((char*)dest <= (char*)src && (char*)src < (char*)dest + nbytes));
175 MOZ_ASSERT(!((char*)src <= (char*)dest && (char*)dest < (char*)src + nbytes));
176 ::memcpy(dest, src, nbytes);
177 }
178
179 inline void js::jit::AtomicOperations::memmoveSafeWhenRacy(void* dest,
180 const void* src,
181 size_t nbytes) {
182 ::memmove(dest, src, nbytes);
183 }
184
185 template <typename T>
186 inline T js::jit::AtomicOperations::exchangeSeqCst(T* addr, T val) {
187 static_assert(sizeof(T) <= sizeof(void*),
188 "atomics supported up to pointer size only");
189 T v;
190 __atomic_exchange(addr, &val, &v, __ATOMIC_SEQ_CST);
191 return v;
192 }
193
194 #endif // jit_mips_shared_AtomicOperations_mips_shared_h