-
Notifications
You must be signed in to change notification settings - Fork 0
/
atomic_ops.h
63 lines (52 loc) · 1.72 KB
/
atomic_ops.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#ifndef ATOMIC_OPS_H_
#define ATOMIC_OPS_H_
#include <stdint.h>
/*
* atomic instruction that is equivalent to the following:
* if (a == oldval) {
* a = newval;
* return true;
* } else {
* return false;
* }
*/
template <typename T>
bool atomic_compare_and_swap(T& a, T oldval, T newval) {
return __sync_bool_compare_and_swap(&a, oldval, newval);
}
template <typename T>
bool atomic_compare_and_swap(volatile T& a, T oldval, T newval) {
return __sync_bool_compare_and_swap(&a, oldval, newval);
}
template <>
inline bool atomic_compare_and_swap(volatile double& a, double oldval,
double newval) {
volatile uint64_t* a_ptr = reinterpret_cast<volatile uint64_t*>(&a);
const uint64_t* oldval_ptr = reinterpret_cast<const uint64_t*>(&oldval);
const uint64_t* newval_ptr = reinterpret_cast<const uint64_t*>(&newval);
return __sync_bool_compare_and_swap(a_ptr, *oldval_ptr, *newval_ptr);
}
template <>
inline bool atomic_compare_and_swap(volatile float& a, float oldval,
float newval) {
volatile uint32_t* a_ptr = reinterpret_cast<volatile uint32_t*>(&a);
const uint32_t* oldval_ptr = reinterpret_cast<const uint32_t*>(&oldval);
const uint32_t* newval_ptr = reinterpret_cast<const uint32_t*>(&newval);
return __sync_bool_compare_and_swap(a_ptr, *oldval_ptr, *newval_ptr);
}
template <typename T>
void atomic_exchange(T& a, T& b) {
b = __sync_lock_test_and_set(&a, b);
}
template <typename T>
void atomic_exchange(volatile T& a, T& b) {
b = __sync_lock_test_and_set(&a, b);
}
/*
* Set a to the newval, returning the old value
*/
template <typename T>
T fetch_and_store(T& a, const T& newval) {
return __sync_lock_test_and_set(&a, newval);
}
#endif