Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(235)

Side by Side Diff: base/atomicops_internals_arm64_gcc.h

Issue 230963002: Fix and improve arm64 atomic operations in Base. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This file is an internal atomic implementation, use base/atomicops.h instead. 5 // This file is an internal atomic implementation, use base/atomicops.h instead.
6 6
7 // TODO(rmcilroy): Investigate whether we can use __sync__ intrinsics instead of 7 // TODO(rmcilroy): Investigate whether we can use __sync__ intrinsics instead of
8 // the hand coded assembly without introducing perf regressions. 8 // the hand coded assembly without introducing perf regressions.
9 // TODO(rmcilroy): Investigate whether we can use acquire / release versions of 9 // TODO(rmcilroy): Investigate whether we can use acquire / release versions of
10 // exclusive load / store assembly instructions and do away with 10 // exclusive load / store assembly instructions and do away with
11 // the barriers. 11 // the barriers.
12 12
13 #ifndef BASE_ATOMICOPS_INTERNALS_ARM64_GCC_H_ 13 #ifndef BASE_ATOMICOPS_INTERNALS_ARM64_GCC_H_
14 #define BASE_ATOMICOPS_INTERNALS_ARM64_GCC_H_ 14 #define BASE_ATOMICOPS_INTERNALS_ARM64_GCC_H_
15 15
16 #if defined(OS_QNX) 16 #if defined(OS_QNX)
17 #include <sys/cpuinline.h> 17 #include <sys/cpuinline.h>
18 #endif 18 #endif
19 19
20 namespace base { 20 namespace base {
21 namespace subtle { 21 namespace subtle {
22 22
23 inline void MemoryBarrier() { 23 inline void MemoryBarrier() {
24 __asm__ __volatile__ ( // NOLINT 24 __asm__ __volatile__ ("dmb ish" ::: "memory"); // NOLINT
25 "dmb ish \n\t" // Data memory barrier.
26 ::: "memory"
27 ); // NOLINT
28 } 25 }
29 26
27 // NoBarrier versions of the operation include "memory" in the clobber list.
28 // This is not required for direct usage of the NoBarrier versions of the
29 // operations. However this is required for correctness when they are used as
30 // part of the Acquire or Release versions, to ensure that nothing from outside
31 // the call is reordered between the operation and the memory barrier. This does
32 // not change the code generated, so has no or minimal impact on the
33 // NoBarrier operations.
30 34
31 inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr, 35 inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
32 Atomic32 old_value, 36 Atomic32 old_value,
33 Atomic32 new_value) { 37 Atomic32 new_value) {
34 Atomic32 prev; 38 Atomic32 prev;
35 int32_t temp; 39 int32_t temp;
36 40
37 __asm__ __volatile__ ( // NOLINT 41 __asm__ __volatile__ ( // NOLINT
38 "0: \n\t" 42 "0: \n\t"
39 "ldxr %w[prev], %[ptr] \n\t" // Load the previous value. 43 "ldxr %w[prev], %[ptr] \n\t" // Load the previous value.
40 "cmp %w[prev], %w[old_value] \n\t" 44 "cmp %w[prev], %w[old_value] \n\t"
41 "bne 1f \n\t" 45 "bne 1f \n\t"
42 "stxr %w[temp], %w[new_value], %[ptr] \n\t" // Try to store the new value. 46 "stxr %w[temp], %w[new_value], %[ptr] \n\t" // Try to store the new value.
43 "cbnz %w[temp], 0b \n\t" // Retry if it did not work. 47 "cbnz %w[temp], 0b \n\t" // Retry if it did not work.
44 "1: \n\t" 48 "1: \n\t"
45 "clrex \n\t" // In case we didn't swap.
46 : [prev]"=&r" (prev), 49 : [prev]"=&r" (prev),
47 [temp]"=&r" (temp), 50 [temp]"=&r" (temp),
48 [ptr]"+Q" (*ptr) 51 [ptr]"+Q" (*ptr)
49 : [old_value]"r" (old_value), 52 : [old_value]"IJr" (old_value),
50 [new_value]"r" (new_value) 53 [new_value]"r" (new_value)
51 : "memory", "cc" 54 : "cc", "memory"
52 ); // NOLINT 55 ); // NOLINT
53 56
54 return prev; 57 return prev;
55 } 58 }
56 59
57 inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, 60 inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr,
58 Atomic32 new_value) { 61 Atomic32 new_value) {
59 Atomic32 result; 62 Atomic32 result;
60 int32_t temp; 63 int32_t temp;
61 64
(...skipping 19 matching lines...) Expand all
81 84
82 __asm__ __volatile__ ( // NOLINT 85 __asm__ __volatile__ ( // NOLINT
83 "0: \n\t" 86 "0: \n\t"
84 "ldxr %w[result], %[ptr] \n\t" // Load the previous value. 87 "ldxr %w[result], %[ptr] \n\t" // Load the previous value.
85 "add %w[result], %w[result], %w[increment]\n\t" 88 "add %w[result], %w[result], %w[increment]\n\t"
86 "stxr %w[temp], %w[result], %[ptr] \n\t" // Try to store the result. 89 "stxr %w[temp], %w[result], %[ptr] \n\t" // Try to store the result.
87 "cbnz %w[temp], 0b \n\t" // Retry on failure. 90 "cbnz %w[temp], 0b \n\t" // Retry on failure.
88 : [result]"=&r" (result), 91 : [result]"=&r" (result),
89 [temp]"=&r" (temp), 92 [temp]"=&r" (temp),
90 [ptr]"+Q" (*ptr) 93 [ptr]"+Q" (*ptr)
91 : [increment]"r" (increment) 94 : [increment]"IJr" (increment)
92 : "memory" 95 : "memory"
93 ); // NOLINT 96 ); // NOLINT
94 97
95 return result; 98 return result;
96 } 99 }
97 100
98 inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr, 101 inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
99 Atomic32 increment) { 102 Atomic32 increment) {
103 Atomic32 result;
Mark Mentovai 2014/04/09 16:50:41 This change doesn’t seem to help anything. We usua
rmcilroy 2014/04/10 12:30:46 I think this change was to be consistent with the
104
100 MemoryBarrier(); 105 MemoryBarrier();
101 Atomic32 result = NoBarrier_AtomicIncrement(ptr, increment); 106 result = NoBarrier_AtomicIncrement(ptr, increment);
102 MemoryBarrier(); 107 MemoryBarrier();
103 108
104 return result; 109 return result;
105 } 110 }
106 111
107 inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr, 112 inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
108 Atomic32 old_value, 113 Atomic32 old_value,
109 Atomic32 new_value) { 114 Atomic32 new_value) {
110 Atomic32 prev; 115 Atomic32 prev;
111 int32_t temp;
112 116
113 __asm__ __volatile__ ( // NOLINT 117 prev = NoBarrier_CompareAndSwap(ptr, old_value, new_value);
Mark Mentovai 2014/04/09 16:50:41 Likewise, why not declare it on this line? Also i
114 "0: \n\t" 118 MemoryBarrier();
115 "ldxr %w[prev], %[ptr] \n\t" // Load the previous value.
116 "cmp %w[prev], %w[old_value] \n\t"
117 "bne 1f \n\t"
118 "stxr %w[temp], %w[new_value], %[ptr] \n\t" // Try to store the new value.
119 "cbnz %w[temp], 0b \n\t" // Retry if it did not work.
120 "dmb ish \n\t" // Data memory barrier.
121 "1: \n\t"
122 // If the compare failed the 'dmb' is unnecessary, but we still need a
123 // 'clrex'.
124 "clrex \n\t"
125 : [prev]"=&r" (prev),
126 [temp]"=&r" (temp),
127 [ptr]"+Q" (*ptr)
128 : [old_value]"r" (old_value),
129 [new_value]"r" (new_value)
130 : "memory", "cc"
131 ); // NOLINT
132 119
133 return prev; 120 return prev;
134 } 121 }
135 122
136 inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr, 123 inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
137 Atomic32 old_value, 124 Atomic32 old_value,
138 Atomic32 new_value) { 125 Atomic32 new_value) {
139 Atomic32 prev; 126 Atomic32 prev;
140 int32_t temp;
141 127
142 MemoryBarrier(); 128 MemoryBarrier();
143 129 prev = NoBarrier_CompareAndSwap(ptr, old_value, new_value);
Mark Mentovai 2014/04/09 16:50:41 And again here. In fact, can’t this function just
144 __asm__ __volatile__ ( // NOLINT
145 "0: \n\t"
146 "ldxr %w[prev], %[ptr] \n\t" // Load the previous value.
147 "cmp %w[prev], %w[old_value] \n\t"
148 "bne 1f \n\t"
149 "stxr %w[temp], %w[new_value], %[ptr] \n\t" // Try to store the new value.
150 "cbnz %w[temp], 0b \n\t" // Retry if it did not work.
151 "1: \n\t"
152 // If the compare failed the we still need a 'clrex'.
153 "clrex \n\t"
154 : [prev]"=&r" (prev),
155 [temp]"=&r" (temp),
156 [ptr]"+Q" (*ptr)
157 : [old_value]"r" (old_value),
158 [new_value]"r" (new_value)
159 : "memory", "cc"
160 ); // NOLINT
161 130
162 return prev; 131 return prev;
163 } 132 }
164 133
165 inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) { 134 inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) {
166 *ptr = value; 135 *ptr = value;
167 } 136 }
168 137
169 inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) { 138 inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) {
170 *ptr = value; 139 *ptr = value;
171 MemoryBarrier(); 140 MemoryBarrier();
172 } 141 }
173 142
174 inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) { 143 inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) {
175 MemoryBarrier(); 144 __asm__ __volatile__ ( // NOLINT
176 *ptr = value; 145 "stlr %w[value], %[ptr] \n\t"
146 : [ptr]"=Q" (*ptr)
147 : [value]"r" (value)
148 : "memory"
149 ); // NOLINT
177 } 150 }
178 151
179 inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) { 152 inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) {
180 return *ptr; 153 return *ptr;
181 } 154 }
182 155
183 inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) { 156 inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) {
184 Atomic32 value = *ptr; 157 Atomic32 value;
185 MemoryBarrier(); 158
159 __asm__ __volatile__ ( // NOLINT
160 "ldar %w[value], %[ptr] \n\t"
161 : [value]"=r" (value)
162 : [ptr]"Q" (*ptr)
163 : "memory"
164 ); // NOLINT
165
186 return value; 166 return value;
187 } 167 }
188 168
189 inline Atomic32 Release_Load(volatile const Atomic32* ptr) { 169 inline Atomic32 Release_Load(volatile const Atomic32* ptr) {
190 MemoryBarrier(); 170 MemoryBarrier();
191 return *ptr; 171 return *ptr;
192 } 172 }
193 173
194 // 64-bit versions of the operations. 174 // 64-bit versions of the operations.
195 // See the 32-bit versions for comments. 175 // See the 32-bit versions for comments.
196 176
197 inline Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr, 177 inline Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr,
198 Atomic64 old_value, 178 Atomic64 old_value,
199 Atomic64 new_value) { 179 Atomic64 new_value) {
200 Atomic64 prev; 180 Atomic64 prev;
201 int32_t temp; 181 int32_t temp;
202 182
203 __asm__ __volatile__ ( // NOLINT 183 __asm__ __volatile__ ( // NOLINT
204 "0: \n\t" 184 "0: \n\t"
205 "ldxr %[prev], %[ptr] \n\t" 185 "ldxr %[prev], %[ptr] \n\t"
206 "cmp %[prev], %[old_value] \n\t" 186 "cmp %[prev], %[old_value] \n\t"
207 "bne 1f \n\t" 187 "bne 1f \n\t"
208 "stxr %w[temp], %[new_value], %[ptr] \n\t" 188 "stxr %w[temp], %[new_value], %[ptr] \n\t"
209 "cbnz %w[temp], 0b \n\t" 189 "cbnz %w[temp], 0b \n\t"
210 "1: \n\t" 190 "1: \n\t"
211 "clrex \n\t"
212 : [prev]"=&r" (prev), 191 : [prev]"=&r" (prev),
213 [temp]"=&r" (temp), 192 [temp]"=&r" (temp),
214 [ptr]"+Q" (*ptr) 193 [ptr]"+Q" (*ptr)
215 : [old_value]"r" (old_value), 194 : [old_value]"IJr" (old_value),
216 [new_value]"r" (new_value) 195 [new_value]"r" (new_value)
217 : "memory", "cc" 196 : "cc", "memory"
218 ); // NOLINT 197 ); // NOLINT
219 198
220 return prev; 199 return prev;
221 } 200 }
222 201
223 inline Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr, 202 inline Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr,
224 Atomic64 new_value) { 203 Atomic64 new_value) {
225 Atomic64 result; 204 Atomic64 result;
226 int32_t temp; 205 int32_t temp;
227 206
(...skipping 19 matching lines...) Expand all
247 226
248 __asm__ __volatile__ ( // NOLINT 227 __asm__ __volatile__ ( // NOLINT
249 "0: \n\t" 228 "0: \n\t"
250 "ldxr %[result], %[ptr] \n\t" 229 "ldxr %[result], %[ptr] \n\t"
251 "add %[result], %[result], %[increment] \n\t" 230 "add %[result], %[result], %[increment] \n\t"
252 "stxr %w[temp], %[result], %[ptr] \n\t" 231 "stxr %w[temp], %[result], %[ptr] \n\t"
253 "cbnz %w[temp], 0b \n\t" 232 "cbnz %w[temp], 0b \n\t"
254 : [result]"=&r" (result), 233 : [result]"=&r" (result),
255 [temp]"=&r" (temp), 234 [temp]"=&r" (temp),
256 [ptr]"+Q" (*ptr) 235 [ptr]"+Q" (*ptr)
257 : [increment]"r" (increment) 236 : [increment]"IJr" (increment)
258 : "memory" 237 : "memory"
259 ); // NOLINT 238 ); // NOLINT
260 239
261 return result; 240 return result;
262 } 241 }
263 242
264 inline Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, 243 inline Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr,
265 Atomic64 increment) { 244 Atomic64 increment) {
245 Atomic64 result;
246
266 MemoryBarrier(); 247 MemoryBarrier();
267 Atomic64 result = NoBarrier_AtomicIncrement(ptr, increment); 248 result = NoBarrier_AtomicIncrement(ptr, increment);
268 MemoryBarrier(); 249 MemoryBarrier();
269 250
270 return result; 251 return result;
271 } 252 }
272 253
273 inline Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr, 254 inline Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr,
274 Atomic64 old_value, 255 Atomic64 old_value,
275 Atomic64 new_value) { 256 Atomic64 new_value) {
276 Atomic64 prev; 257 Atomic64 prev;
277 int32_t temp;
278 258
279 __asm__ __volatile__ ( // NOLINT 259 prev = NoBarrier_CompareAndSwap(ptr, old_value, new_value);
280 "0: \n\t" 260 MemoryBarrier();
281 "ldxr %[prev], %[ptr] \n\t"
282 "cmp %[prev], %[old_value] \n\t"
283 "bne 1f \n\t"
284 "stxr %w[temp], %[new_value], %[ptr] \n\t"
285 "cbnz %w[temp], 0b \n\t"
286 "dmb ish \n\t"
287 "1: \n\t"
288 "clrex \n\t"
289 : [prev]"=&r" (prev),
290 [temp]"=&r" (temp),
291 [ptr]"+Q" (*ptr)
292 : [old_value]"r" (old_value),
293 [new_value]"r" (new_value)
294 : "memory", "cc"
295 ); // NOLINT
296 261
297 return prev; 262 return prev;
298 } 263 }
299 264
300 inline Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr, 265 inline Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr,
301 Atomic64 old_value, 266 Atomic64 old_value,
302 Atomic64 new_value) { 267 Atomic64 new_value) {
303 Atomic64 prev; 268 Atomic64 prev;
304 int32_t temp;
305 269
306 MemoryBarrier(); 270 MemoryBarrier();
307 271 prev = NoBarrier_CompareAndSwap(ptr, old_value, new_value);
308 __asm__ __volatile__ ( // NOLINT
309 "0: \n\t"
310 "ldxr %[prev], %[ptr] \n\t"
311 "cmp %[prev], %[old_value] \n\t"
312 "bne 1f \n\t"
313 "stxr %w[temp], %[new_value], %[ptr] \n\t"
314 "cbnz %w[temp], 0b \n\t"
315 "1: \n\t"
316 "clrex \n\t"
317 : [prev]"=&r" (prev),
318 [temp]"=&r" (temp),
319 [ptr]"+Q" (*ptr)
320 : [old_value]"r" (old_value),
321 [new_value]"r" (new_value)
322 : "memory", "cc"
323 ); // NOLINT
324 272
325 return prev; 273 return prev;
326 } 274 }
327 275
328 inline void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value) { 276 inline void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value) {
329 *ptr = value; 277 *ptr = value;
330 } 278 }
331 279
332 inline void Acquire_Store(volatile Atomic64* ptr, Atomic64 value) { 280 inline void Acquire_Store(volatile Atomic64* ptr, Atomic64 value) {
333 *ptr = value; 281 *ptr = value;
334 MemoryBarrier(); 282 MemoryBarrier();
335 } 283 }
336 284
337 inline void Release_Store(volatile Atomic64* ptr, Atomic64 value) { 285 inline void Release_Store(volatile Atomic64* ptr, Atomic64 value) {
338 MemoryBarrier(); 286 __asm__ __volatile__ ( // NOLINT
339 *ptr = value; 287 "stlr %x[value], %[ptr] \n\t"
288 : [ptr]"=Q" (*ptr)
289 : [value]"r" (value)
290 : "memory"
291 ); // NOLINT
340 } 292 }
341 293
342 inline Atomic64 NoBarrier_Load(volatile const Atomic64* ptr) { 294 inline Atomic64 NoBarrier_Load(volatile const Atomic64* ptr) {
343 return *ptr; 295 return *ptr;
344 } 296 }
345 297
346 inline Atomic64 Acquire_Load(volatile const Atomic64* ptr) { 298 inline Atomic64 Acquire_Load(volatile const Atomic64* ptr) {
347 Atomic64 value = *ptr; 299 Atomic32 value;
348 MemoryBarrier(); 300
301 __asm__ __volatile__ ( // NOLINT
302 "ldar %x[value], %[ptr] \n\t"
303 : [value]"=r" (value)
304 : [ptr]"Q" (*ptr)
305 : "memory"
306 ); // NOLINT
307
349 return value; 308 return value;
350 } 309 }
351 310
352 inline Atomic64 Release_Load(volatile const Atomic64* ptr) { 311 inline Atomic64 Release_Load(volatile const Atomic64* ptr) {
353 MemoryBarrier(); 312 MemoryBarrier();
354 return *ptr; 313 return *ptr;
355 } 314 }
356 315
357 } // namespace base::subtle 316 } // namespace base::subtle
358 } // namespace base 317 } // namespace base
359 318
360 #endif // BASE_ATOMICOPS_INTERNALS_ARM64_GCC_H_ 319 #endif // BASE_ATOMICOPS_INTERNALS_ARM64_GCC_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698