1 /* Internal macros for atomic operations for GNU C Library.
2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 #include <bits/atomic.h>
28 /* Wrapper macros to call pre_NN_post (mem, ...) where NN is the
29 bit width of *MEM. The calling macro puts parens around MEM
30 and following args. */
31 #define __atomic_val_bysize(pre, post, mem, ...) \
33 __typeof (*mem) __result; \
34 if (sizeof (*mem) == 1) \
35 __result = pre##_8_##post (mem, __VA_ARGS__); \
36 else if (sizeof (*mem) == 2) \
37 __result = pre##_16_##post (mem, __VA_ARGS__); \
38 else if (sizeof (*mem) == 4) \
39 __result = pre##_32_##post (mem, __VA_ARGS__); \
40 else if (sizeof (*mem) == 8) \
41 __result = pre##_64_##post (mem, __VA_ARGS__); \
46 #define __atomic_bool_bysize(pre, post, mem, ...) \
49 if (sizeof (*mem) == 1) \
50 __result = pre##_8_##post (mem, __VA_ARGS__); \
51 else if (sizeof (*mem) == 2) \
52 __result = pre##_16_##post (mem, __VA_ARGS__); \
53 else if (sizeof (*mem) == 4) \
54 __result = pre##_32_##post (mem, __VA_ARGS__); \
55 else if (sizeof (*mem) == 8) \
56 __result = pre##_64_##post (mem, __VA_ARGS__); \
63 /* Atomically store NEWVAL in *MEM if *MEM is equal to OLDVAL.
64 Return the old *MEM value. */
65 #if !defined atomic_compare_and_exchange_val_acq \
66 && defined __arch_compare_and_exchange_val_32_acq
67 # define atomic_compare_and_exchange_val_acq(mem, newval, oldval) \
68 __atomic_val_bysize (__arch_compare_and_exchange_val,acq, \
73 #ifndef atomic_compare_and_exchange_val_rel
74 # define atomic_compare_and_exchange_val_rel(mem, newval, oldval) \
75 atomic_compare_and_exchange_val_acq (mem, newval, oldval)
79 /* Atomically store NEWVAL in *MEM if *MEM is equal to OLDVAL.
80 Return zero if *MEM was changed or non-zero if no exchange happened. */
81 #ifndef atomic_compare_and_exchange_bool_acq
82 # ifdef __arch_compare_and_exchange_bool_32_acq
83 # define atomic_compare_and_exchange_bool_acq(mem, newval, oldval) \
84 __atomic_bool_bysize (__arch_compare_and_exchange_bool,acq, \
87 # define atomic_compare_and_exchange_bool_acq(mem, newval, oldval) \
88 ({ /* Cannot use __oldval here, because macros later in this file might \
89 call this macro with __oldval argument. */ \
90 __typeof (oldval) __old = (oldval); \
91 atomic_compare_and_exchange_val_acq (mem, newval, __old) != __old; \
97 #ifndef atomic_compare_and_exchange_bool_rel
98 # define atomic_compare_and_exchange_bool_rel(mem, newval, oldval) \
99 atomic_compare_and_exchange_bool_acq (mem, newval, oldval)
103 /* Store NEWVALUE in *MEM and return the old value. */
104 #ifndef atomic_exchange_acq
105 # define atomic_exchange_acq(mem, newvalue) \
106 ({ __typeof (*(mem)) __oldval; \
107 __typeof (mem) __memp = (mem); \
108 __typeof (*(mem)) __value = (newvalue); \
111 __oldval = *__memp; \
112 while (__builtin_expect (atomic_compare_and_exchange_bool_acq (__memp, \
120 #ifndef atomic_exchange_rel
121 # define atomic_exchange_rel(mem, newvalue) atomic_exchange_acq (mem, newvalue)
125 /* Add VALUE to *MEM and return the old value of *MEM. */
126 #ifndef atomic_exchange_and_add
127 # define atomic_exchange_and_add(mem, value) \
128 ({ __typeof (*(mem)) __oldval; \
129 __typeof (mem) __memp = (mem); \
130 __typeof (*(mem)) __value = (value); \
133 __oldval = *__memp; \
134 while (__builtin_expect (atomic_compare_and_exchange_bool_acq (__memp, \
146 # define atomic_max(mem, value) \
148 __typeof (*(mem)) __oldval; \
149 __typeof (mem) __memp = (mem); \
150 __typeof (*(mem)) __value = (value); \
152 __oldval = *__memp; \
153 if (__oldval >= __value) \
155 } while (__builtin_expect (atomic_compare_and_exchange_bool_acq (__memp, \
163 # define atomic_min(mem, value) \
165 __typeof (*(mem)) __oldval; \
166 __typeof (mem) __memp = (mem); \
167 __typeof (*(mem)) __value = (value); \
169 __oldval = *__memp; \
170 if (__oldval <= __value) \
172 } while (__builtin_expect (atomic_compare_and_exchange_bool_acq (__memp, \
180 # define atomic_add(mem, value) (void) atomic_exchange_and_add ((mem), (value))
184 #ifndef atomic_increment
185 # define atomic_increment(mem) atomic_add ((mem), 1)
189 #ifndef atomic_increment_val
190 # define atomic_increment_val(mem) (atomic_exchange_and_add ((mem), 1) + 1)
194 /* Add one to *MEM and return true iff it's now zero. */
195 #ifndef atomic_increment_and_test
196 # define atomic_increment_and_test(mem) \
197 (atomic_exchange_and_add ((mem), 1) + 1 == 0)
201 #ifndef atomic_decrement
202 # define atomic_decrement(mem) atomic_add ((mem), -1)
206 #ifndef atomic_decrement_val
207 # define atomic_decrement_val(mem) (atomic_exchange_and_add ((mem), -1) - 1)
211 /* Subtract 1 from *MEM and return true iff it's now zero. */
212 #ifndef atomic_decrement_and_test
213 # define atomic_decrement_and_test(mem) \
214 (atomic_exchange_and_add ((mem), -1) == 1)
218 /* Decrement *MEM if it is > 0, and return the old value. */
219 #ifndef atomic_decrement_if_positive
220 # define atomic_decrement_if_positive(mem) \
221 ({ __typeof (*(mem)) __oldval; \
222 __typeof (mem) __memp = (mem); \
226 __oldval = *__memp; \
227 if (__builtin_expect (__oldval <= 0, 0)) \
230 while (__builtin_expect (atomic_compare_and_exchange_bool_acq (__memp, \
239 #ifndef atomic_add_negative
240 # define atomic_add_negative(mem, value) \
241 ({ __typeof (value) __aan_value = (value); \
242 atomic_exchange_and_add (mem, __aan_value) < -__aan_value; })
246 #ifndef atomic_add_zero
247 # define atomic_add_zero(mem, value) \
248 ({ __typeof (value) __aaz_value = (value); \
249 atomic_exchange_and_add (mem, __aaz_value) == -__aaz_value; })
253 #ifndef atomic_bit_set
254 # define atomic_bit_set(mem, bit) \
255 (void) atomic_bit_test_set(mem, bit)
259 #ifndef atomic_bit_test_set
260 # define atomic_bit_test_set(mem, bit) \
261 ({ __typeof (*(mem)) __oldval; \
262 __typeof (mem) __memp = (mem); \
263 __typeof (*(mem)) __mask = ((__typeof (*(mem))) 1 << (bit)); \
266 __oldval = (*__memp); \
267 while (__builtin_expect (atomic_compare_and_exchange_bool_acq (__memp, \
273 __oldval & __mask; })
277 #ifndef atomic_full_barrier
278 # define atomic_full_barrier() __asm ("" ::: "memory")
282 #ifndef atomic_read_barrier
283 # define atomic_read_barrier() atomic_full_barrier ()
287 #ifndef atomic_write_barrier
288 # define atomic_write_barrier() atomic_full_barrier ()
293 # define atomic_delay() do { /* nothing */ } while (0)
296 #endif /* atomic.h */