1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This file is an internal atomic implementation, use base/atomicops.h instead.
7 #ifndef BASE_ATOMICOPS_INTERNALS_X86_GCC_H_
8 #define BASE_ATOMICOPS_INTERNALS_X86_GCC_H_
11 #include "base/base_export.h"
13 // This struct is not part of the public API of this module; clients may not
14 // use it. (However, it's exported via BASE_EXPORT because clients implicitly
15 // do use it at link time by inlining these functions.)
16 // Features of this x86. Values may not be correct before main() is run,
17 // but are set conservatively.
18 struct AtomicOps_x86CPUFeatureStruct {
19 bool has_amd_lock_mb_bug; // Processor has AMD memory-barrier bug; do lfence
20 // after acquire compare-and-swap.
21 bool has_sse2; // Processor has SSE2.
23 BASE_EXPORT extern struct AtomicOps_x86CPUFeatureStruct
24 AtomicOps_Internalx86CPUFeatures;
26 #define ATOMICOPS_COMPILER_BARRIER() __asm__ __volatile__("" : : : "memory")
31 // 32-bit low-level operations on any platform.
33 inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
37 __asm__ __volatile__("lock; cmpxchgl %1,%2"
39 : "q" (new_value), "m" (*ptr), "0" (old_value)
44 inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr,
46 __asm__ __volatile__("xchgl %1,%0" // The lock prefix is implicit for xchg.
48 : "m" (*ptr), "0" (new_value)
50 return new_value; // Now it's the previous value.
53 inline Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr,
55 Atomic32 temp = increment;
56 __asm__ __volatile__("lock; xaddl %0,%1"
57 : "+r" (temp), "+m" (*ptr)
59 // temp now holds the old value of *ptr
60 return temp + increment;
63 inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
65 Atomic32 temp = increment;
66 __asm__ __volatile__("lock; xaddl %0,%1"
67 : "+r" (temp), "+m" (*ptr)
69 // temp now holds the old value of *ptr
70 if (AtomicOps_Internalx86CPUFeatures.has_amd_lock_mb_bug) {
71 __asm__ __volatile__("lfence" : : : "memory");
73 return temp + increment;
76 inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
79 Atomic32 x = NoBarrier_CompareAndSwap(ptr, old_value, new_value);
80 if (AtomicOps_Internalx86CPUFeatures.has_amd_lock_mb_bug) {
81 __asm__ __volatile__("lfence" : : : "memory");
86 inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
89 return NoBarrier_CompareAndSwap(ptr, old_value, new_value);
92 inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) {
96 #if defined(__x86_64__)
98 // 64-bit implementations of memory barrier can be simpler, because it
99 // "mfence" is guaranteed to exist.
100 inline void MemoryBarrier() {
101 __asm__ __volatile__("mfence" : : : "memory");
104 inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) {
111 inline void MemoryBarrier() {
112 if (AtomicOps_Internalx86CPUFeatures.has_sse2) {
113 __asm__ __volatile__("mfence" : : : "memory");
114 } else { // mfence is faster but not present on PIII
116 NoBarrier_AtomicExchange(&x, 0); // acts as a barrier on PIII
120 inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) {
121 if (AtomicOps_Internalx86CPUFeatures.has_sse2) {
123 __asm__ __volatile__("mfence" : : : "memory");
125 NoBarrier_AtomicExchange(ptr, value);
126 // acts as a barrier on PIII
131 inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) {
132 ATOMICOPS_COMPILER_BARRIER();
133 *ptr = value; // An x86 store acts as a release barrier.
134 // See comments in Atomic64 version of Release_Store(), below.
137 inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) {
141 inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) {
142 Atomic32 value = *ptr; // An x86 load acts as a acquire barrier.
143 // See comments in Atomic64 version of Release_Store(), below.
144 ATOMICOPS_COMPILER_BARRIER();
148 inline Atomic32 Release_Load(volatile const Atomic32* ptr) {
153 #if defined(__x86_64__)
155 // 64-bit low-level operations on 64-bit platform.
157 inline Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr,
159 Atomic64 new_value) {
161 __asm__ __volatile__("lock; cmpxchgq %1,%2"
163 : "q" (new_value), "m" (*ptr), "0" (old_value)
168 inline Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr,
169 Atomic64 new_value) {
170 __asm__ __volatile__("xchgq %1,%0" // The lock prefix is implicit for xchg.
172 : "m" (*ptr), "0" (new_value)
174 return new_value; // Now it's the previous value.
177 inline Atomic64 NoBarrier_AtomicIncrement(volatile Atomic64* ptr,
178 Atomic64 increment) {
179 Atomic64 temp = increment;
180 __asm__ __volatile__("lock; xaddq %0,%1"
181 : "+r" (temp), "+m" (*ptr)
183 // temp now contains the previous value of *ptr
184 return temp + increment;
187 inline Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr,
188 Atomic64 increment) {
189 Atomic64 temp = increment;
190 __asm__ __volatile__("lock; xaddq %0,%1"
191 : "+r" (temp), "+m" (*ptr)
193 // temp now contains the previous value of *ptr
194 if (AtomicOps_Internalx86CPUFeatures.has_amd_lock_mb_bug) {
195 __asm__ __volatile__("lfence" : : : "memory");
197 return temp + increment;
200 inline void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value) {
204 inline void Acquire_Store(volatile Atomic64* ptr, Atomic64 value) {
209 inline void Release_Store(volatile Atomic64* ptr, Atomic64 value) {
210 ATOMICOPS_COMPILER_BARRIER();
212 *ptr = value; // An x86 store acts as a release barrier
213 // for current AMD/Intel chips as of Jan 2008.
214 // See also Acquire_Load(), below.
216 // When new chips come out, check:
217 // IA-32 Intel Architecture Software Developer's Manual, Volume 3:
218 // System Programming Guide, Chatper 7: Multiple-processor management,
219 // Section 7.2, Memory Ordering.
221 // http://developer.intel.com/design/pentium4/manuals/index_new.htm
223 // x86 stores/loads fail to act as barriers for a few instructions (clflush
224 // maskmovdqu maskmovq movntdq movnti movntpd movntps movntq) but these are
225 // not generated by the compiler, and are rare. Users of these instructions
226 // need to know about cache behaviour in any case since all of these involve
227 // either flushing cache lines or non-temporal cache hints.
230 inline Atomic64 NoBarrier_Load(volatile const Atomic64* ptr) {
234 inline Atomic64 Acquire_Load(volatile const Atomic64* ptr) {
235 Atomic64 value = *ptr; // An x86 load acts as a acquire barrier,
236 // for current AMD/Intel chips as of Jan 2008.
237 // See also Release_Store(), above.
238 ATOMICOPS_COMPILER_BARRIER();
242 inline Atomic64 Release_Load(volatile const Atomic64* ptr) {
247 inline Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr,
249 Atomic64 new_value) {
250 Atomic64 x = NoBarrier_CompareAndSwap(ptr, old_value, new_value);
251 if (AtomicOps_Internalx86CPUFeatures.has_amd_lock_mb_bug) {
252 __asm__ __volatile__("lfence" : : : "memory");
257 inline Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr,
259 Atomic64 new_value) {
260 return NoBarrier_CompareAndSwap(ptr, old_value, new_value);
263 #endif // defined(__x86_64__)
265 } // namespace base::subtle
268 #undef ATOMICOPS_COMPILER_BARRIER
270 #endif // BASE_ATOMICOPS_INTERNALS_X86_GCC_H_