uatomic/x86: Remove redundant memory barriers
[urcu.git] / include / urcu / compiler.h
CommitLineData
d3d3857f
MJ
1// SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4
ec4e58a3
MD
5#ifndef _URCU_COMPILER_H
6#define _URCU_COMPILER_H
af02d47e
MD
7
8/*
af02d47e 9 * Compiler definitions.
af02d47e
MD
10 */
11
49d7d158
MD
12#include <stddef.h> /* for offsetof */
13
06326a94
SM
14#if defined __cplusplus
15# include <type_traits> /* for std::remove_cv */
16#endif
17
865024e9
OD
18#include <urcu/config.h>
19
a0b7f7ea
MD
20#define caa_likely(x) __builtin_expect(!!(x), 1)
21#define caa_unlikely(x) __builtin_expect(!!(x), 0)
2dc5fa0f 22
865024e9
OD
23#ifdef CONFIG_RCU_USE_ATOMIC_BUILTINS
24# define cmm_barrier() __atomic_signal_fence(__ATOMIC_SEQ_CST)
25#else
26# define cmm_barrier() __asm__ __volatile__ ("" : : : "memory")
27#endif
2dc5fa0f 28
af02d47e
MD
29/*
30 * Instruct the compiler to perform only a single access to a variable
31 * (prohibits merging and refetching). The compiler is also forbidden to reorder
6cf3827c 32 * successive instances of CMM_ACCESS_ONCE(), but only when the compiler is aware of
af02d47e 33 * particular ordering. Compiler ordering can be ensured, for example, by
6cf3827c 34 * putting two CMM_ACCESS_ONCE() in separate C statements.
af02d47e
MD
35 *
36 * This macro does absolutely -nothing- to prevent the CPU from reordering,
37 * merging, or refetching absolutely anything at any time. Its main intended
38 * use is to mediate communication between process-level code and irq/NMI
39 * handlers, all running on the same CPU.
40 */
e51500ed 41#define CMM_ACCESS_ONCE(x) (*(__volatile__ __typeof__(x) *)&(x))
af02d47e 42
2f8a5ae7
MD
43#ifndef caa_max
44#define caa_max(a,b) ((a)>(b)?(a):(b))
fdee2e6d
MD
45#endif
46
2f8a5ae7
MD
47#ifndef caa_min
48#define caa_min(a,b) ((a)<(b)?(a):(b))
fdee2e6d
MD
49#endif
50
1b27a772 51#if defined(__SIZEOF_LONG__)
06f22bdb 52#define CAA_BITS_PER_LONG (__SIZEOF_LONG__ * 8)
1b27a772 53#elif defined(_LP64)
06f22bdb 54#define CAA_BITS_PER_LONG 64
1b27a772 55#else
06f22bdb 56#define CAA_BITS_PER_LONG 32
1b27a772
PB
57#endif
58
b194c06e
MD
59/*
60 * caa_container_of - Get the address of an object containing a field.
61 *
62 * @ptr: pointer to the field.
63 * @type: type of the object.
64 * @member: name of the field within the object.
65 */
66#define caa_container_of(ptr, type, member) \
1b85da85 67 __extension__ \
453629a9 68 ({ \
bdffa73a 69 const __typeof__(((type *) NULL)->member) * __ptr = (ptr); \
453629a9
MD
70 (type *)((char *)__ptr - offsetof(type, member)); \
71 })
72
4b3a7ebd
MD
73/*
74 * caa_container_of_check_null - Get the address of an object containing a field.
75 *
76 * @ptr: pointer to the field.
77 * @type: type of the object.
78 * @member: name of the field within the object.
79 *
80 * Return the address of the object containing the field. Return NULL if
81 * @ptr is NULL.
82 */
83#define caa_container_of_check_null(ptr, type, member) \
84 __extension__ \
85 ({ \
86 const __typeof__(((type *) NULL)->member) * __ptr = (ptr); \
87 (__ptr) ? (type *)((char *)__ptr - offsetof(type, member)) : NULL; \
88 })
89
78bec10c 90#define CAA_BUILD_BUG_ON_ZERO(cond) (sizeof(struct { int:-!!(cond); }))
9f59b220 91#define CAA_BUILD_BUG_ON(cond) ((void)CAA_BUILD_BUG_ON_ZERO(cond))
78bec10c 92
dc53e23e
MD
93/*
94 * __rcu is an annotation that documents RCU pointer accesses that need
95 * to be protected by a read-side critical section. Eventually, a static
96 * checker will be able to use this annotation to detect incorrect RCU
97 * usage.
98 */
99#define __rcu
100
4501f284 101#ifdef __cplusplus
06326a94 102#define URCU_FORCE_CAST(_type, arg) (reinterpret_cast<std::remove_cv<_type>::type>(arg))
4501f284
MD
103#else
104#define URCU_FORCE_CAST(type, arg) ((type) (arg))
105#endif
106
4b5f005b 107#define caa_is_signed_type(type) ((type) -1 < (type) 0)
e56d99bf 108
4b5f005b 109/*
e4749953
MD
110 * Cast to unsigned long, sign-extending if @v is signed.
111 * Note: casting to a larger type or to same type size keeps the sign of
112 * the expression being cast (see C99 6.3.1.3).
4b5f005b 113 */
e4749953 114#define caa_cast_long_keep_sign(v) ((unsigned long) (v))
e56d99bf 115
706d1165 116#if defined (__GNUC__) \
a0a0c4d5
EC
117 && ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5) \
118 || __GNUC__ >= 5)
706d1165
MD
119#define CDS_DEPRECATED(msg) \
120 __attribute__((deprecated(msg)))
121#else
122#define CDS_DEPRECATED(msg) \
123 __attribute__((deprecated))
124#endif
125
e323ceac
MD
126#define CAA_ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
127
1b4fed78
MJ
128/*
129 * URCU_GCC_VERSION is used to blacklist specific GCC versions with known
130 * bugs, clang also defines these macros to an equivalent GCC version it
131 * claims to support, so exclude it.
132 */
133#if defined(__GNUC__) && !defined(__clang__)
4b79310a
MD
134# define URCU_GCC_VERSION (__GNUC__ * 10000 \
135 + __GNUC_MINOR__ * 100 \
136 + __GNUC_PATCHLEVEL__)
4b79310a
MD
137#endif
138
d1854484
OD
139#ifdef __cplusplus
140#define caa_unqual_scalar_typeof(x) \
141 std::remove_cv<std::remove_reference<decltype(x)>::type>::type
142#else
143#define caa_scalar_type_to_expr(type) \
144 unsigned type: (unsigned type)0, \
145 signed type: (signed type)0
146
147/*
148 * Use C11 _Generic to express unqualified type from expression. This removes
149 * volatile qualifier from expression type.
150 */
151#define caa_unqual_scalar_typeof(x) \
152 __typeof__( \
153 _Generic((x), \
154 char: (char)0, \
155 caa_scalar_type_to_expr(char), \
156 caa_scalar_type_to_expr(short), \
157 caa_scalar_type_to_expr(int), \
158 caa_scalar_type_to_expr(long), \
159 caa_scalar_type_to_expr(long long), \
160 default: (x) \
161 ) \
162 )
163#endif
164
165/*
166 * Allow user to manually define CMM_SANITIZE_THREAD if their toolchain is not
167 * supported by this check.
168 */
169#ifndef CMM_SANITIZE_THREAD
170# if defined(__GNUC__) && defined(__SANITIZE_THREAD__)
171# define CMM_SANITIZE_THREAD
172# elif defined(__clang__) && defined(__has_feature)
173# if __has_feature(thread_sanitizer)
174# define CMM_SANITIZE_THREAD
175# endif
176# endif
177#endif /* !CMM_SANITIZE_THREAD */
178
179/*
180 * Helper to add the volatile qualifier to a pointer.
181 */
182#if defined __cplusplus
183template <typename T>
184volatile T cmm_cast_volatile(T t)
185{
186 return static_cast<volatile T>(t);
187}
188#else
189# define cmm_cast_volatile(ptr) \
190 __extension__ \
191 ({ \
192 (volatile __typeof__(ptr))(ptr); \
193 })
194#endif
195
455a8429
OD
196/*
197 * Compile time assertion.
198 * - predicate: boolean expression to evaluate,
199 * - msg: string to print to the user on failure when `static_assert()` is
200 * supported,
201 * - c_identifier_msg: message to be included in the typedef to emulate a
202 * static assertion. This parameter must be a valid C identifier as it will
203 * be used as a typedef name.
204 */
205#ifdef __cplusplus
206#define urcu_static_assert(predicate, msg, c_identifier_msg) \
207 static_assert(predicate, msg)
208#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
209#define urcu_static_assert(predicate, msg, c_identifier_msg) \
210 _Static_assert(predicate, msg)
211#else
212/*
213 * Evaluates the predicate and emit a compilation error on failure.
214 *
215 * If the predicate evaluates to true, this macro emits a function
216 * prototype with an argument type which is an array of size 0.
217 *
218 * If the predicate evaluates to false, this macro emits a function
219 * prototype with an argument type which is an array of negative size
220 * which is invalid in C and forces a compiler error. The
221 * c_identifier_msg parameter is used as the argument identifier so it
222 * is printed to the user when the error is reported.
223 */
224#define urcu_static_assert(predicate, msg, c_identifier_msg) \
225 void urcu_static_assert_proto(char c_identifier_msg[2*!!(predicate)-1])
226#endif
227
ec4e58a3 228#endif /* _URCU_COMPILER_H */
This page took 0.060037 seconds and 4 git commands to generate.