1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2012 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 // This file is an internal atomic implementation, use atomicops.h instead.
33 #ifndef GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_X86_GCC_H_
34 #define GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_X86_GCC_H_
40 // This struct is not part of the public API of this module; clients may not
42 // Features of this x86. Values may not be correct before main() is run,
43 // but are set conservatively.
44 struct AtomicOps_x86CPUFeatureStruct {
45 bool has_amd_lock_mb_bug; // Processor has AMD memory-barrier bug; do lfence
46 // after acquire compare-and-swap.
47 bool has_sse2; // Processor has SSE2.
49 extern struct AtomicOps_x86CPUFeatureStruct AtomicOps_Internalx86CPUFeatures;
51 #define ATOMICOPS_COMPILER_BARRIER() __asm__ __volatile__("" : : : "memory")
53 // 32-bit low-level operations on any platform.
55 inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
59 __asm__ __volatile__("lock; cmpxchgl %1,%2"
61 : "q" (new_value), "m" (*ptr), "0" (old_value)
66 inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr,
68 __asm__ __volatile__("xchgl %1,%0" // The lock prefix is implicit for xchg.
70 : "m" (*ptr), "0" (new_value)
72 return new_value; // Now it's the previous value.
75 inline Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr,
77 Atomic32 temp = increment;
78 __asm__ __volatile__("lock; xaddl %0,%1"
79 : "+r" (temp), "+m" (*ptr)
81 // temp now holds the old value of *ptr
82 return temp + increment;
85 inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
87 Atomic32 temp = increment;
88 __asm__ __volatile__("lock; xaddl %0,%1"
89 : "+r" (temp), "+m" (*ptr)
91 // temp now holds the old value of *ptr
92 if (AtomicOps_Internalx86CPUFeatures.has_amd_lock_mb_bug) {
93 __asm__ __volatile__("lfence" : : : "memory");
95 return temp + increment;
98 inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
100 Atomic32 new_value) {
101 Atomic32 x = NoBarrier_CompareAndSwap(ptr, old_value, new_value);
102 if (AtomicOps_Internalx86CPUFeatures.has_amd_lock_mb_bug) {
103 __asm__ __volatile__("lfence" : : : "memory");
108 inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
110 Atomic32 new_value) {
111 return NoBarrier_CompareAndSwap(ptr, old_value, new_value);
114 inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) {
118 #if defined(__x86_64__)
120 // 64-bit implementations of memory barrier can be simpler, because it
121 // "mfence" is guaranteed to exist.
122 inline void MemoryBarrier() {
123 __asm__ __volatile__("mfence" : : : "memory");
126 inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) {
133 inline void MemoryBarrier() {
134 if (AtomicOps_Internalx86CPUFeatures.has_sse2) {
135 __asm__ __volatile__("mfence" : : : "memory");
136 } else { // mfence is faster but not present on PIII
138 NoBarrier_AtomicExchange(&x, 0); // acts as a barrier on PIII
142 inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) {
143 if (AtomicOps_Internalx86CPUFeatures.has_sse2) {
145 __asm__ __volatile__("mfence" : : : "memory");
147 NoBarrier_AtomicExchange(ptr, value);
148 // acts as a barrier on PIII
153 inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) {
154 ATOMICOPS_COMPILER_BARRIER();
155 *ptr = value; // An x86 store acts as a release barrier.
156 // See comments in Atomic64 version of Release_Store(), below.
159 inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) {
163 inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) {
164 Atomic32 value = *ptr; // An x86 load acts as a acquire barrier.
165 // See comments in Atomic64 version of Release_Store(), below.
166 ATOMICOPS_COMPILER_BARRIER();
170 inline Atomic32 Release_Load(volatile const Atomic32* ptr) {
175 #if defined(__x86_64__)
177 // 64-bit low-level operations on 64-bit platform.
179 inline Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr,
181 Atomic64 new_value) {
183 __asm__ __volatile__("lock; cmpxchgq %1,%2"
185 : "q" (new_value), "m" (*ptr), "0" (old_value)
190 inline Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr,
191 Atomic64 new_value) {
192 __asm__ __volatile__("xchgq %1,%0" // The lock prefix is implicit for xchg.
194 : "m" (*ptr), "0" (new_value)
196 return new_value; // Now it's the previous value.
199 inline Atomic64 NoBarrier_AtomicIncrement(volatile Atomic64* ptr,
200 Atomic64 increment) {
201 Atomic64 temp = increment;
202 __asm__ __volatile__("lock; xaddq %0,%1"
203 : "+r" (temp), "+m" (*ptr)
205 // temp now contains the previous value of *ptr
206 return temp + increment;
209 inline Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr,
210 Atomic64 increment) {
211 Atomic64 temp = increment;
212 __asm__ __volatile__("lock; xaddq %0,%1"
213 : "+r" (temp), "+m" (*ptr)
215 // temp now contains the previous value of *ptr
216 if (AtomicOps_Internalx86CPUFeatures.has_amd_lock_mb_bug) {
217 __asm__ __volatile__("lfence" : : : "memory");
219 return temp + increment;
222 inline void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value) {
226 inline void Acquire_Store(volatile Atomic64* ptr, Atomic64 value) {
231 inline void Release_Store(volatile Atomic64* ptr, Atomic64 value) {
232 ATOMICOPS_COMPILER_BARRIER();
234 *ptr = value; // An x86 store acts as a release barrier
235 // for current AMD/Intel chips as of Jan 2008.
236 // See also Acquire_Load(), below.
238 // When new chips come out, check:
239 // IA-32 Intel Architecture Software Developer's Manual, Volume 3:
240 // System Programming Guide, Chatper 7: Multiple-processor management,
241 // Section 7.2, Memory Ordering.
243 // http://developer.intel.com/design/pentium4/manuals/index_new.htm
245 // x86 stores/loads fail to act as barriers for a few instructions (clflush
246 // maskmovdqu maskmovq movntdq movnti movntpd movntps movntq) but these are
247 // not generated by the compiler, and are rare. Users of these instructions
248 // need to know about cache behaviour in any case since all of these involve
249 // either flushing cache lines or non-temporal cache hints.
252 inline Atomic64 NoBarrier_Load(volatile const Atomic64* ptr) {
256 inline Atomic64 Acquire_Load(volatile const Atomic64* ptr) {
257 Atomic64 value = *ptr; // An x86 load acts as a acquire barrier,
258 // for current AMD/Intel chips as of Jan 2008.
259 // See also Release_Store(), above.
260 ATOMICOPS_COMPILER_BARRIER();
264 inline Atomic64 Release_Load(volatile const Atomic64* ptr) {
269 inline Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr,
271 Atomic64 new_value) {
272 Atomic64 x = NoBarrier_CompareAndSwap(ptr, old_value, new_value);
273 if (AtomicOps_Internalx86CPUFeatures.has_amd_lock_mb_bug) {
274 __asm__ __volatile__("lfence" : : : "memory");
279 inline Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr,
281 Atomic64 new_value) {
282 return NoBarrier_CompareAndSwap(ptr, old_value, new_value);
285 #endif // defined(__x86_64__)
287 } // namespace internal
288 } // namespace protobuf
289 } // namespace google
291 #undef ATOMICOPS_COMPILER_BARRIER
293 #endif // GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_X86_GCC_H_