-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathprimitives.h
More file actions
149 lines (125 loc) · 3.69 KB
/
primitives.h
File metadata and controls
149 lines (125 loc) · 3.69 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/** @file */
#ifndef PRIMITIVES_H
#define PRIMITIVES_H
#if defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ > 7
/**
* An atomic fetch-and-add.
*/
#define FAA(ptr, val) __atomic_fetch_add(ptr, val, __ATOMIC_RELAXED)
/**
* An atomic fetch-and-add that also ensures sequential consistency.
*/
#define FAAcs(ptr, val) __atomic_fetch_add(ptr, val, __ATOMIC_SEQ_CST)
/**
* An atomic compare-and-swap.
*/
#define CAS(ptr, cmp, val) __atomic_compare_exchange_n(ptr, cmp, val, 0, \
__ATOMIC_RELAXED, __ATOMIC_RELAXED)
/**
* An atomic compare-and-swap that also ensures sequential consistency.
*/
#define CAScs(ptr, cmp, val) __atomic_compare_exchange_n(ptr, cmp, val, 0, \
__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
/**
* An atomic compare-and-swap that ensures release semantic when succeed
* or acquire semantic when failed.
*/
#define CASra(ptr, cmp, val) __atomic_compare_exchange_n(ptr, cmp, val, 0, \
__ATOMIC_RELEASE, __ATOMIC_ACQUIRE)
/**
* An atomic compare-and-swap that ensures acquire semantic when succeed
* or relaxed semantic when failed.
*/
#define CASa(ptr, cmp, val) __atomic_compare_exchange_n(ptr, cmp, val, 0, \
__ATOMIC_ACQUIRE, __ATOMIC_RELAXED)
/**
* An atomic swap.
*/
#define SWAP(ptr, val) __atomic_exchange_n(ptr, val, __ATOMIC_RELAXED)
/**
* An atomic swap that ensures acquire release semantics.
*/
#define SWAPra(ptr, val) __atomic_exchange_n(ptr, val, __ATOMIC_ACQ_REL)
/**
* A memory fence to ensure sequential consistency.
*/
#define FENCE() __atomic_thread_fence(__ATOMIC_SEQ_CST)
/**
* An atomic store.
*/
#define STORE(ptr, val) __atomic_store_n(ptr, val, __ATOMIC_RELAXED)
/**
* A store with a preceding release fence to ensure all previous load
* and stores completes before the current store is visiable.
*/
#define RELEASE(ptr, val) __atomic_store_n(ptr, val, __ATOMIC_RELEASE)
/**
* A load with a following acquire fence to ensure no following load and
* stores can start before the current load completes.
*/
#define ACQUIRE(ptr) __atomic_load_n(ptr, __ATOMIC_ACQUIRE)
#else /** Non-GCC or old GCC. */
#if defined(__x86_64__) || defined(_M_X64_)
#define FAA __sync_fetch_and_add
#define FAAcs __sync_fetch_and_add
static inline int
_compare_and_swap(void ** ptr, void ** expected, void * desired) {
void * oldval = *expected;
void * newval = __sync_val_compare_and_swap(ptr, oldval, desired);
if (newval == oldval) {
return 1;
} else {
*expected = newval;
return 0;
}
}
#define CAS(ptr, expected, desired) \
_compare_and_swap((void **) (ptr), (void **) (expected), (void *) (desired))
#define CAScs CAS
#define CASra CAS
#define CASa CAS
#define SWAP __sync_lock_test_and_set
#define SWAPra SWAP
#define ACQUIRE(p) ({ \
__typeof__(*(p)) __ret = *p; \
__asm__("":::"memory"); \
__ret; \
})
#define RELEASE(p, v) do {\
__asm__("":::"memory"); \
*p = v; \
} while (0)
#define FENCE() __sync_synchronize()
#endif
#endif
#if defined(__x86_64__) || defined(_M_X64_)
#define PAUSE() __asm__ ("pause")
static inline
int _CAS2(volatile long * ptr, long * cmp1, long * cmp2, long val1, long val2)
{
char success;
long tmp1 = *cmp1;
long tmp2 = *cmp2;
__asm__ __volatile__(
"lock cmpxchg16b %1\n"
"setz %0"
: "=q" (success), "+m" (*ptr), "+a" (tmp1), "+d" (tmp2)
: "b" (val1), "c" (val2)
: "cc" );
*cmp1 = tmp1;
*cmp2 = tmp2;
return success;
}
#define CAS2(p, o1, o2, n1, n2) \
_CAS2((volatile long *) p, (long *) o1, (long *) o2, (long) n1, (long) n2)
#define BTAS(ptr, bit) ({ \
char __ret; \
__asm__ __volatile__( \
"lock btsq %2, %0; setnc %1" \
: "+m" (*ptr), "=r" (__ret) : "ri" (bit) : "cc" ); \
__ret; \
})
#else
#define PAUSE()
#endif
#endif /* end of include guard: PRIMITIVES_H */