1 /* Internal macros for atomic operations for GNU C Library.
2 Copyright (C) 2002, 2003, 2004 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, \
145 # define atomic_add(mem, value) (void) atomic_exchange_and_add ((mem), (value))
149 #ifndef atomic_increment
150 # define atomic_increment(mem) atomic_add ((mem), 1)
154 #ifndef atomic_increment_val
155 # define atomic_increment_val(mem) (atomic_exchange_and_add ((mem), 1) + 1)
159 /* Add one to *MEM and return true iff it's now zero. */
160 #ifndef atomic_increment_and_test
161 # define atomic_increment_and_test(mem) \
162 (atomic_exchange_and_add ((mem), 1) + 1 == 0)
166 #ifndef atomic_decrement
167 # define atomic_decrement(mem) atomic_add ((mem), -1)
171 #ifndef atomic_decrement_val
172 # define atomic_decrement_val(mem) (atomic_exchange_and_add ((mem), -1) - 1)
176 /* Subtract 1 from *MEM and return true iff it's now zero. */
177 #ifndef atomic_decrement_and_test
178 # define atomic_decrement_and_test(mem) \
179 (atomic_exchange_and_add ((mem), -1) == 1)
183 /* Decrement *MEM if it is > 0, and return the old value. */
184 #ifndef atomic_decrement_if_positive
185 # define atomic_decrement_if_positive(mem) \
186 ({ __typeof (*(mem)) __oldval; \
187 __typeof (mem) __memp = (mem); \
191 __oldval = *__memp; \
192 if (__builtin_expect (__oldval <= 0, 0)) \
195 while (__builtin_expect (atomic_compare_and_exchange_bool_acq (__memp, \
204 #ifndef atomic_add_negative
205 # define atomic_add_negative(mem, value) \
206 ({ __typeof (value) __aan_value = (value); \
207 atomic_exchange_and_add (mem, __aan_value) < -__aan_value; })
211 #ifndef atomic_add_zero
212 # define atomic_add_zero(mem, value) \
213 ({ __typeof (value) __aaz_value = (value); \
214 atomic_exchange_and_add (mem, __aaz_value) == -__aaz_value; })
218 #ifndef atomic_bit_set
219 # define atomic_bit_set(mem, bit) \
220 (void) atomic_bit_test_set(mem, bit)
224 #ifndef atomic_bit_test_set
225 # define atomic_bit_test_set(mem, bit) \
226 ({ __typeof (*(mem)) __oldval; \
227 __typeof (mem) __memp = (mem); \
228 __typeof (*(mem)) __mask = ((__typeof (*(mem))) 1 << (bit)); \
231 __oldval = (*__memp); \
232 while (__builtin_expect (atomic_compare_and_exchange_bool_acq (__memp, \
238 __oldval & __mask; })
242 #ifndef atomic_full_barrier
243 # define atomic_full_barrier() __asm__ ("" ::: "memory")
247 #ifndef atomic_read_barrier
248 # define atomic_read_barrier() atomic_full_barrier ()
252 #ifndef atomic_write_barrier
253 # define atomic_write_barrier() atomic_full_barrier ()
258 # define atomic_delay() do { /* nothing */ } while (0)
261 #endif /* atomic.h */