23 #include <boost/utility.hpp>
33 template <
typename T,
typename IsTLarge =
void>
34 class AtomicIntrinsics {
36 static T compareAndSwap(
volatile T* dest, T expected, T newValue) {
38 asm volatile(
"lock cmpxchg %[src], %[dest]"
39 : [dest]
"+m"(*dest),
"=a"(result)
40 : [src]
"r"(newValue),
"a"(expected)
45 static T swap(
volatile T* dest, T newValue) {
48 asm volatile(
"xchg %[r], %[dest]" : [dest]
"+m"(*dest), [r]
"+r"(result) : :
"memory");
52 static T load(
volatile const T* value) {
53 asm volatile(
"mfence" :::
"memory");
55 asm volatile(
"mfence" :::
"memory");
59 static T loadRelaxed(
volatile const T* value) {
63 static void store(
volatile T* dest, T newValue) {
64 asm volatile(
"mfence" :::
"memory");
66 asm volatile(
"mfence" :::
"memory");
69 static T fetchAndAdd(
volatile T* dest, T increment) {
71 asm volatile(
"lock xadd %[src], %[dest]"
72 : [dest]
"+m"(*dest), [src]
"+r"(result)
93 class AtomicIntrinsics<T, typename boost::disable_if_c<sizeof(T) <= sizeof(void*)>::type> {
95 static T compareAndSwap(
volatile T* dest, T expected, T newValue) {
102 "mov (%%edx), %%ebx\n"
103 "mov 4(%%edx), %%ecx\n"
104 "mov (%%edi), %%eax\n"
105 "mov 4(%%edi), %%edx\n"
106 "lock cmpxchg8b (%%esi)\n"
107 "mov %%eax, (%%edi)\n"
108 "mov %%edx, 4(%%edi)\n"
114 :
"S"(dest),
"D"(&result),
"d"(&newValue)
119 static T swap(
volatile T* dest, T newValue) {
124 actual = compareAndSwap(dest, expected, newValue);
125 }
while (actual != expected);
129 static T load(
volatile const T* value) {
130 return compareAndSwap(const_cast<volatile T*>(value), T(0), T(0));
133 static void store(
volatile T* dest, T newValue) {
134 swap(dest, newValue);
137 static T fetchAndAdd(
volatile T* dest, T increment) {
141 expected = load(dest);
142 actual = compareAndSwap(dest, expected, expected + increment);
143 }
while (actual != expected);
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20