summaryrefslogtreecommitdiffstats
path: root/src/contrib/libbpf/include/linux/compiler.h
blob: 26336dc7027e85962cf85a551e298647cfede9cf (plain)
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
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */

#ifndef __LINUX_COMPILER_H
#define __LINUX_COMPILER_H

#define likely(x)		__builtin_expect(!!(x), 1)
#define unlikely(x)		__builtin_expect(!!(x), 0)

#define READ_ONCE(x)		(*(volatile typeof(x) *)&x)
#define WRITE_ONCE(x, v)	(*(volatile typeof(x) *)&x) = (v)

#define barrier()		asm volatile("" ::: "memory")

#if defined(__x86_64__)

# define smp_rmb()		barrier()
# define smp_wmb()		barrier()
# define smp_mb()		asm volatile("lock; addl $0,-132(%%rsp)" ::: "memory", "cc")

# define smp_store_release(p, v)		\
do {						\
	barrier();				\
	WRITE_ONCE(*p, v);			\
} while (0)

# define smp_load_acquire(p)			\
({						\
	typeof(*p) ___p = READ_ONCE(*p);	\
	barrier();				\
	___p;					\
})

#elif defined(__aarch64__)

# define smp_rmb()		asm volatile("dmb ishld" ::: "memory")
# define smp_wmb()		asm volatile("dmb ishst" ::: "memory")
# define smp_mb()		asm volatile("dmb ish" ::: "memory")

#endif

#ifndef smp_mb
# define smp_mb()		__sync_synchronize()
#endif

#ifndef smp_rmb
# define smp_rmb()		smp_mb()
#endif

#ifndef smp_wmb
# define smp_wmb()		smp_mb()
#endif

#ifndef smp_store_release
# define smp_store_release(p, v)		\
do {						\
	smp_mb();				\
	WRITE_ONCE(*p, v);			\
} while (0)
#endif

#ifndef smp_load_acquire
# define smp_load_acquire(p)			\
({						\
	typeof(*p) ___p = READ_ONCE(*p);	\
	smp_mb();				\
	___p;					\
})
#endif

#endif /* __LINUX_COMPILER_H */