Initialize Tizen 2.3
[external/chromium.git] / base / atomicops_internals_x86_gcc.h
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.
4
5 // This file is an internal atomic implementation, use base/atomicops.h instead.
6
7 #ifndef BASE_ATOMICOPS_INTERNALS_X86_GCC_H_
8 #define BASE_ATOMICOPS_INTERNALS_X86_GCC_H_
9 #pragma once
10
11 #include "base/base_export.h"
12
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.
22 };
23 BASE_EXPORT extern struct AtomicOps_x86CPUFeatureStruct
24     AtomicOps_Internalx86CPUFeatures;
25
26 #define ATOMICOPS_COMPILER_BARRIER() __asm__ __volatile__("" : : : "memory")
27
28 namespace base {
29 namespace subtle {
30
31 // 32-bit low-level operations on any platform.
32
33 inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
34                                          Atomic32 old_value,
35                                          Atomic32 new_value) {
36   Atomic32 prev;
37   __asm__ __volatile__("lock; cmpxchgl %1,%2"
38                        : "=a" (prev)
39                        : "q" (new_value), "m" (*ptr), "0" (old_value)
40                        : "memory");
41   return prev;
42 }
43
44 inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr,
45                                          Atomic32 new_value) {
46   __asm__ __volatile__("xchgl %1,%0"  // The lock prefix is implicit for xchg.
47                        : "=r" (new_value)
48                        : "m" (*ptr), "0" (new_value)
49                        : "memory");
50   return new_value;  // Now it's the previous value.
51 }
52
53 inline Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr,
54                                           Atomic32 increment) {
55   Atomic32 temp = increment;
56   __asm__ __volatile__("lock; xaddl %0,%1"
57                        : "+r" (temp), "+m" (*ptr)
58                        : : "memory");
59   // temp now holds the old value of *ptr
60   return temp + increment;
61 }
62
63 inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
64                                         Atomic32 increment) {
65   Atomic32 temp = increment;
66   __asm__ __volatile__("lock; xaddl %0,%1"
67                        : "+r" (temp), "+m" (*ptr)
68                        : : "memory");
69   // temp now holds the old value of *ptr
70   if (AtomicOps_Internalx86CPUFeatures.has_amd_lock_mb_bug) {
71     __asm__ __volatile__("lfence" : : : "memory");
72   }
73   return temp + increment;
74 }
75
76 inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
77                                        Atomic32 old_value,
78                                        Atomic32 new_value) {
79   Atomic32 x = NoBarrier_CompareAndSwap(ptr, old_value, new_value);
80   if (AtomicOps_Internalx86CPUFeatures.has_amd_lock_mb_bug) {
81     __asm__ __volatile__("lfence" : : : "memory");
82   }
83   return x;
84 }
85
86 inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
87                                        Atomic32 old_value,
88                                        Atomic32 new_value) {
89   return NoBarrier_CompareAndSwap(ptr, old_value, new_value);
90 }
91
92 inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) {
93   *ptr = value;
94 }
95
96 #if defined(__x86_64__)
97
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");
102 }
103
104 inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) {
105   *ptr = value;
106   MemoryBarrier();
107 }
108
109 #else
110
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
115     Atomic32 x = 0;
116     NoBarrier_AtomicExchange(&x, 0);  // acts as a barrier on PIII
117   }
118 }
119
120 inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) {
121   if (AtomicOps_Internalx86CPUFeatures.has_sse2) {
122     *ptr = value;
123     __asm__ __volatile__("mfence" : : : "memory");
124   } else {
125     NoBarrier_AtomicExchange(ptr, value);
126                           // acts as a barrier on PIII
127   }
128 }
129 #endif
130
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.
135 }
136
137 inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) {
138   return *ptr;
139 }
140
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();
145   return value;
146 }
147
148 inline Atomic32 Release_Load(volatile const Atomic32* ptr) {
149   MemoryBarrier();
150   return *ptr;
151 }
152
153 #if defined(__x86_64__)
154
155 // 64-bit low-level operations on 64-bit platform.
156
157 inline Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr,
158                                          Atomic64 old_value,
159                                          Atomic64 new_value) {
160   Atomic64 prev;
161   __asm__ __volatile__("lock; cmpxchgq %1,%2"
162                        : "=a" (prev)
163                        : "q" (new_value), "m" (*ptr), "0" (old_value)
164                        : "memory");
165   return prev;
166 }
167
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.
171                        : "=r" (new_value)
172                        : "m" (*ptr), "0" (new_value)
173                        : "memory");
174   return new_value;  // Now it's the previous value.
175 }
176
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)
182                        : : "memory");
183   // temp now contains the previous value of *ptr
184   return temp + increment;
185 }
186
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)
192                        : : "memory");
193   // temp now contains the previous value of *ptr
194   if (AtomicOps_Internalx86CPUFeatures.has_amd_lock_mb_bug) {
195     __asm__ __volatile__("lfence" : : : "memory");
196   }
197   return temp + increment;
198 }
199
200 inline void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value) {
201   *ptr = value;
202 }
203
204 inline void Acquire_Store(volatile Atomic64* ptr, Atomic64 value) {
205   *ptr = value;
206   MemoryBarrier();
207 }
208
209 inline void Release_Store(volatile Atomic64* ptr, Atomic64 value) {
210   ATOMICOPS_COMPILER_BARRIER();
211
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.
215
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.
220   // Last seen at:
221   //   http://developer.intel.com/design/pentium4/manuals/index_new.htm
222   //
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.
228 }
229
230 inline Atomic64 NoBarrier_Load(volatile const Atomic64* ptr) {
231   return *ptr;
232 }
233
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();
239   return value;
240 }
241
242 inline Atomic64 Release_Load(volatile const Atomic64* ptr) {
243   MemoryBarrier();
244   return *ptr;
245 }
246
247 inline Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr,
248                                        Atomic64 old_value,
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");
253   }
254   return x;
255 }
256
257 inline Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr,
258                                        Atomic64 old_value,
259                                        Atomic64 new_value) {
260   return NoBarrier_CompareAndSwap(ptr, old_value, new_value);
261 }
262
263 #endif  // defined(__x86_64__)
264
265 } // namespace base::subtle
266 } // namespace base
267
268 #undef ATOMICOPS_COMPILER_BARRIER
269
270 #endif  // BASE_ATOMICOPS_INTERNALS_X86_GCC_H_