]> git.sesse.net Git - casparcg/blob - dependencies64/cef/linux/include/base/internal/cef_atomicops_x86_gcc.h
0e2139d95cdf105439acbd0fad041f623ecddf0a
[casparcg] / dependencies64 / cef / linux / include / base / internal / cef_atomicops_x86_gcc.h
1 // Copyright (c) 2011 Google Inc. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 //    * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //    * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 //    * Neither the name of Google Inc. nor the name Chromium Embedded
14 // Framework nor the names of its contributors may be used to endorse
15 // or promote products derived from this software without specific prior
16 // written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 // Do not include this header file directly. Use base/cef_atomicops.h
31 // instead.
32
33 #ifndef CEF_INCLUDE_BASE_INTERNAL_CEF_ATOMICOPS_X86_GCC_H_
34 #define CEF_INCLUDE_BASE_INTERNAL_CEF_ATOMICOPS_X86_GCC_H_
35
36 // This struct is not part of the public API of this module; clients may not
37 // use it.
38 // Features of this x86.  Values may not be correct before main() is run,
39 // but are set conservatively.
40 struct AtomicOps_x86CPUFeatureStruct {
41   bool has_amd_lock_mb_bug; // Processor has AMD memory-barrier bug; do lfence
42                             // after acquire compare-and-swap.
43 };
44 extern struct AtomicOps_x86CPUFeatureStruct
45     AtomicOps_Internalx86CPUFeatures;
46
47 #define ATOMICOPS_COMPILER_BARRIER() __asm__ __volatile__("" : : : "memory")
48
49 namespace base {
50 namespace subtle {
51
52 // 32-bit low-level operations on any platform.
53
54 inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
55                                          Atomic32 old_value,
56                                          Atomic32 new_value) {
57   Atomic32 prev;
58   __asm__ __volatile__("lock; cmpxchgl %1,%2"
59                        : "=a" (prev)
60                        : "q" (new_value), "m" (*ptr), "0" (old_value)
61                        : "memory");
62   return prev;
63 }
64
65 inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr,
66                                          Atomic32 new_value) {
67   __asm__ __volatile__("xchgl %1,%0"  // The lock prefix is implicit for xchg.
68                        : "=r" (new_value)
69                        : "m" (*ptr), "0" (new_value)
70                        : "memory");
71   return new_value;  // Now it's the previous value.
72 }
73
74 inline Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr,
75                                           Atomic32 increment) {
76   Atomic32 temp = increment;
77   __asm__ __volatile__("lock; xaddl %0,%1"
78                        : "+r" (temp), "+m" (*ptr)
79                        : : "memory");
80   // temp now holds the old value of *ptr
81   return temp + increment;
82 }
83
84 inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
85                                         Atomic32 increment) {
86   Atomic32 temp = increment;
87   __asm__ __volatile__("lock; xaddl %0,%1"
88                        : "+r" (temp), "+m" (*ptr)
89                        : : "memory");
90   // temp now holds the old value of *ptr
91   if (AtomicOps_Internalx86CPUFeatures.has_amd_lock_mb_bug) {
92     __asm__ __volatile__("lfence" : : : "memory");
93   }
94   return temp + increment;
95 }
96
97 inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
98                                        Atomic32 old_value,
99                                        Atomic32 new_value) {
100   Atomic32 x = NoBarrier_CompareAndSwap(ptr, old_value, new_value);
101   if (AtomicOps_Internalx86CPUFeatures.has_amd_lock_mb_bug) {
102     __asm__ __volatile__("lfence" : : : "memory");
103   }
104   return x;
105 }
106
107 inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
108                                        Atomic32 old_value,
109                                        Atomic32 new_value) {
110   return NoBarrier_CompareAndSwap(ptr, old_value, new_value);
111 }
112
113 inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) {
114   *ptr = value;
115 }
116
117 inline void MemoryBarrier() {
118   __asm__ __volatile__("mfence" : : : "memory");
119 }
120
121 inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) {
122   *ptr = value;
123   MemoryBarrier();
124 }
125
126 inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) {
127   ATOMICOPS_COMPILER_BARRIER();
128   *ptr = value; // An x86 store acts as a release barrier.
129   // See comments in Atomic64 version of Release_Store(), below.
130 }
131
132 inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) {
133   return *ptr;
134 }
135
136 inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) {
137   Atomic32 value = *ptr; // An x86 load acts as a acquire barrier.
138   // See comments in Atomic64 version of Release_Store(), below.
139   ATOMICOPS_COMPILER_BARRIER();
140   return value;
141 }
142
143 inline Atomic32 Release_Load(volatile const Atomic32* ptr) {
144   MemoryBarrier();
145   return *ptr;
146 }
147
148 #if defined(__x86_64__)
149
150 // 64-bit low-level operations on 64-bit platform.
151
152 inline Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr,
153                                          Atomic64 old_value,
154                                          Atomic64 new_value) {
155   Atomic64 prev;
156   __asm__ __volatile__("lock; cmpxchgq %1,%2"
157                        : "=a" (prev)
158                        : "q" (new_value), "m" (*ptr), "0" (old_value)
159                        : "memory");
160   return prev;
161 }
162
163 inline Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr,
164                                          Atomic64 new_value) {
165   __asm__ __volatile__("xchgq %1,%0"  // The lock prefix is implicit for xchg.
166                        : "=r" (new_value)
167                        : "m" (*ptr), "0" (new_value)
168                        : "memory");
169   return new_value;  // Now it's the previous value.
170 }
171
172 inline Atomic64 NoBarrier_AtomicIncrement(volatile Atomic64* ptr,
173                                           Atomic64 increment) {
174   Atomic64 temp = increment;
175   __asm__ __volatile__("lock; xaddq %0,%1"
176                        : "+r" (temp), "+m" (*ptr)
177                        : : "memory");
178   // temp now contains the previous value of *ptr
179   return temp + increment;
180 }
181
182 inline Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr,
183                                         Atomic64 increment) {
184   Atomic64 temp = increment;
185   __asm__ __volatile__("lock; xaddq %0,%1"
186                        : "+r" (temp), "+m" (*ptr)
187                        : : "memory");
188   // temp now contains the previous value of *ptr
189   if (AtomicOps_Internalx86CPUFeatures.has_amd_lock_mb_bug) {
190     __asm__ __volatile__("lfence" : : : "memory");
191   }
192   return temp + increment;
193 }
194
195 inline void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value) {
196   *ptr = value;
197 }
198
199 inline void Acquire_Store(volatile Atomic64* ptr, Atomic64 value) {
200   *ptr = value;
201   MemoryBarrier();
202 }
203
204 inline void Release_Store(volatile Atomic64* ptr, Atomic64 value) {
205   ATOMICOPS_COMPILER_BARRIER();
206
207   *ptr = value; // An x86 store acts as a release barrier
208                 // for current AMD/Intel chips as of Jan 2008.
209                 // See also Acquire_Load(), below.
210
211   // When new chips come out, check:
212   //  IA-32 Intel Architecture Software Developer's Manual, Volume 3:
213   //  System Programming Guide, Chatper 7: Multiple-processor management,
214   //  Section 7.2, Memory Ordering.
215   // Last seen at:
216   //   http://developer.intel.com/design/pentium4/manuals/index_new.htm
217   //
218   // x86 stores/loads fail to act as barriers for a few instructions (clflush
219   // maskmovdqu maskmovq movntdq movnti movntpd movntps movntq) but these are
220   // not generated by the compiler, and are rare.  Users of these instructions
221   // need to know about cache behaviour in any case since all of these involve
222   // either flushing cache lines or non-temporal cache hints.
223 }
224
225 inline Atomic64 NoBarrier_Load(volatile const Atomic64* ptr) {
226   return *ptr;
227 }
228
229 inline Atomic64 Acquire_Load(volatile const Atomic64* ptr) {
230   Atomic64 value = *ptr; // An x86 load acts as a acquire barrier,
231                          // for current AMD/Intel chips as of Jan 2008.
232                          // See also Release_Store(), above.
233   ATOMICOPS_COMPILER_BARRIER();
234   return value;
235 }
236
237 inline Atomic64 Release_Load(volatile const Atomic64* ptr) {
238   MemoryBarrier();
239   return *ptr;
240 }
241
242 inline Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr,
243                                        Atomic64 old_value,
244                                        Atomic64 new_value) {
245   Atomic64 x = NoBarrier_CompareAndSwap(ptr, old_value, new_value);
246   if (AtomicOps_Internalx86CPUFeatures.has_amd_lock_mb_bug) {
247     __asm__ __volatile__("lfence" : : : "memory");
248   }
249   return x;
250 }
251
252 inline Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr,
253                                        Atomic64 old_value,
254                                        Atomic64 new_value) {
255   return NoBarrier_CompareAndSwap(ptr, old_value, new_value);
256 }
257
258 #endif  // defined(__x86_64__)
259
260 } // namespace base::subtle
261 } // namespace base
262
263 #undef ATOMICOPS_COMPILER_BARRIER
264
265 #endif  // CEF_INCLUDE_BASE_INTERNAL_CEF_ATOMICOPS_X86_GCC_H_