14 #ifndef ATOMIC_PRV_X86_H_
15 #define ATOMIC_PRV_X86_H_
17 #include <type_traits>
20 #include "atomic_prv_mfence_x86.h"
24 typedef intptr_t int_cas2;
25 typedef uintptr_t uint_cas2;
26 typedef int_cas2 int_cas_max;
27 typedef uint_cas2 uint_cas_max;
29 #if !defined __LP64__ && !defined __LLP64__
31 #define HAVE_ATOMIC_RW64
35 #ifdef HAVE_ATOMIC_RW64
38 inline void atomicWrite64(
const T &x, T *target) noexcept {
39 static_assert(
sizeof(T) == 8,
"");
44 :
"m" (x),
"m" (*target)
50 inline void atomicRead64(T *x,
const T &target) noexcept {
51 static_assert(__alignof__(T) >= 8,
"");
52 static_assert(
sizeof(T) == 8,
"");
57 :
"m" (target),
"m" (*x)
68 inline bool atomicCompareAndSet2(
70 T newv0, T newv1, T *target ) noexcept {
72 #if defined __LP64__ || defined __LLP64__
74 " lock; cmpxchg16b (%%rsi);"
76 :
"=q" (ret),
"=d" (oldv1),
"=a" (oldv0)
77 :
"1" (oldv1),
"2" (oldv0),
78 "c" (newv1),
"b" (newv0),
86 " lock; cmpxchg8b (%%esi);"
89 :
"=q" (ret),
"=d" (oldv1),
"=a" (oldv0)
90 :
"1" (oldv1),
"2" (oldv0),
91 "c" (newv1),
"D" (newv0),
97 template <
typename T,
typename X>
98 inline typename std::enable_if<
99 std::is_pod<T>::value && (
sizeof(T) ==
sizeof(int_cas2) * 2) && (
sizeof(X) ==
sizeof(int_cas2) * 2),
bool>::type
102 T newv, X *target ) noexcept {
110 return atomicCompareAndSet2(oldv_.w[0], oldv_.w[1], newv_.w[0], newv_.w[1], (uint_cas2*)(target));
114 template <
typename T>
115 inline typename std::enable_if<
116 std::is_pod<T>::value && (
sizeof(T) <=
sizeof(int_cas_max)),
bool>::type
117 atomicCompareAndSet(T oldv, T newv, T *target ) noexcept {
120 " lock; cmpxchg %2,%3;"
122 :
"=q" (ret),
"=a" (oldv)
123 :
"q" (newv),
"m" ( *target),
"1" (oldv)
127 template <
typename T>
128 inline T atomicSwap(T v, T *target ) noexcept {
132 :
"m" ( *target),
"0" (v)
136 template <
typename T>
137 inline void atomicAdd(T *target, T x ) noexcept {
141 :
"m" ( *target),
"q" (x)
145 template <
typename T>
146 inline bool atomicAddAndTest(T *target, T x ) noexcept {
152 :
"m" ( *target),
"q" (x)
156 template <
typename T>
158 typename std::enable_if<(4 >
sizeof(T)),
void>::type
159 atomicInc(T *target ) noexcept {
166 template <
typename T>
168 typename std::enable_if<(4 == sizeof(T)), void>::type
169 atomicInc(T *target ) noexcept {
176 template <
typename T>
178 typename std::enable_if<(8 == sizeof(T)), void>::type
179 atomicInc(T *target ) noexcept {
187 template <
typename T>
189 typename std::enable_if<(4 >
sizeof(T)),
void>::type
190 atomicDec(T *target ) noexcept {
197 template <
typename T>
199 typename std::enable_if<(4 == sizeof(T)), void>::type
200 atomicDec(T *target ) noexcept {
207 template <
typename T>
209 typename std::enable_if<(8 == sizeof(T)), void>::type
210 atomicDec(T *target ) noexcept {
218 template <
typename T>
220 typename std::enable_if<(4 >
sizeof(T)),
bool>::type
221 atomicDecAndTest(T *target ) noexcept {
231 template <
typename T>
233 typename std::enable_if<(4 == sizeof(T)), bool>::type
234 atomicDecAndTest(T *target ) noexcept {
244 template <
typename T>
246 typename std::enable_if<(8 == sizeof(T)), bool>::type
247 atomicDecAndTest(T *target ) noexcept {