-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatomic.hpp
36 lines (25 loc) · 821 Bytes
/
atomic.hpp
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
#ifndef VI_ATOMIC_HPP
#define VI_ATOMIC_HPP
//these macro taken from https://locklessinc.com/articles/locks/
#define atomic_add(P, V) __sync_add_and_fetch((P), (V))
#define atomic_xadd(P, V) __sync_fetch_and_add((P), (V))
#define atomic_read(V) (*(volatile typeof(V) *)&(V))
#define barrier() asm volatile("": : :"memory")
#define cpu_relax() asm volatile("pause\n": : :"memory")
static inline unsigned xchg_32(void *ptr, unsigned x)
{
__asm__ __volatile__("xchgl %0,%1"
:"=r" ((unsigned) x)
:"m" (*(volatile unsigned *)ptr), "0" (x)
:"memory");
return x;
}
static inline unsigned long long xchg_64(void *ptr, unsigned long long x)
{
__asm__ __volatile__("xchgq %0,%1"
:"=r" ((unsigned long long) x)
:"m" (*(volatile unsigned long long *)ptr), "0" (x)
:"memory");
return x;
}
#endif