Fix typo.
[kopensolaris-gnu/glibc.git] / include / atomic.h
1 /* Internal macros for atomic operations for GNU C Library.
2    Copyright (C) 2002, 2003 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5
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.
10
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.
15
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
19    02111-1307 USA.  */
20
21 #ifndef _ATOMIC_H
22 #define _ATOMIC_H       1
23
24 #include <stdlib.h>
25
26 #include <bits/atomic.h>
27
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, ...)                              \
32   ({                                                                          \
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__);                          \
42     else                                                                      \
43       abort ();                                                               \
44     __result;                                                                 \
45   })
46 #define __atomic_bool_bysize(pre, post, mem, ...)                             \
47   ({                                                                          \
48     int __result;                                                             \
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__);                          \
57     else                                                                      \
58       abort ();                                                               \
59     __result;                                                                 \
60   })
61
62
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, \
69                        (mem), (newval), (oldval))
70 #endif
71
72
73 #ifndef atomic_compare_and_exchange_val_rel
74 # define atomic_compare_and_exchange_val_rel(mem, oldval, newval) \
75   atomic_compare_and_exchange_val_acq ((mem), (oldval), (newval))
76 #endif
77
78
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, \
85                         (mem), (newval), (oldval))
86 # else
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;   \
92   })
93 # endif
94 #endif
95
96
97 #ifndef atomic_compare_and_exchange_bool_rel
98 # define atomic_compare_and_exchange_bool_rel(mem, oldval, newval) \
99   atomic_compare_and_exchange_bool_acq ((mem), (oldval), (newval))
100 #endif
101
102
103 /* Store NEWVALUE in *MEM and return the old value.  */
104 #ifndef atomic_exchange
105 # define atomic_exchange(mem, newvalue) \
106   ({ __typeof (*(mem)) __oldval;                                              \
107      __typeof (mem) __memp = (mem);                                           \
108      __typeof (*(mem)) __value = (newvalue);                                  \
109                                                                               \
110      do                                                                       \
111        __oldval = (*__memp);                                                  \
112      while (__builtin_expect (atomic_compare_and_exchange_bool_acq (__memp,   \
113                                                                     __value,  \
114                                                                     __oldval),\
115                               0));                                            \
116                                                                               \
117      __oldval; })
118 #endif
119
120
121 /* Add VALUE to *MEM and return the old value of *MEM.  */
122 #ifndef atomic_exchange_and_add
123 # define atomic_exchange_and_add(mem, value) \
124   ({ __typeof (*(mem)) __oldval;                                              \
125      __typeof (mem) __memp = (mem);                                           \
126      __typeof (*(mem)) __value = (value);                                     \
127                                                                               \
128      do                                                                       \
129        __oldval = (*__memp);                                                  \
130      while (__builtin_expect (atomic_compare_and_exchange_bool_acq (__memp,   \
131                                                                     __oldval  \
132                                                                     + __value,\
133                                                                     __oldval),\
134                               0));                                            \
135                                                                               \
136      __oldval; })
137 #endif
138
139
140 #ifndef atomic_add
141 # define atomic_add(mem, value) (void) atomic_exchange_and_add ((mem), (value))
142 #endif
143
144
145 #ifndef atomic_increment
146 # define atomic_increment(mem) atomic_add ((mem), 1)
147 #endif
148
149
150 /* Add one to *MEM and return true iff it's now zero.  */
151 #ifndef atomic_increment_and_test
152 # define atomic_increment_and_test(mem) \
153   (atomic_exchange_and_add ((mem), 1) + 1 == 0)
154 #endif
155
156
157 #ifndef atomic_decrement
158 # define atomic_decrement(mem) atomic_add ((mem), -1)
159 #endif
160
161
162 /* Subtract 1 from *MEM and return true iff it's now zero.  */
163 #ifndef atomic_decrement_and_test
164 # define atomic_decrement_and_test(mem) \
165   (atomic_exchange_and_add ((mem), -1) == 1)
166 #endif
167
168
169 /* Decrement *MEM if it is > 0, and return the old value.  */
170 #ifndef atomic_decrement_if_positive
171 # define atomic_decrement_if_positive(mem) \
172   ({ __typeof (*(mem)) __oldval;                                              \
173      __typeof (mem) __memp = (mem);                                           \
174                                                                               \
175      do                                                                       \
176        {                                                                      \
177          __oldval = *__memp;                                                  \
178          if (__builtin_expect (__oldval <= 0, 0))                             \
179            break;                                                             \
180        }                                                                      \
181      while (__builtin_expect (atomic_compare_and_exchange_bool_acq (__memp,   \
182                                                                     __oldval  \
183                                                                     - 1,      \
184                                                                     __oldval),\
185                               0));\
186      __oldval; })
187 #endif
188
189
190 #ifndef atomic_add_negative
191 # define atomic_add_negative(mem, value) \
192   ({ __typeof (value) __value = (value);                                      \
193      atomic_exchange_and_add ((mem), __value) < -__value; })
194 #endif
195
196
197 #ifndef atomic_add_zero
198 # define atomic_add_zero(mem, value) \
199   ({ __typeof (value) __value = (value);                                      \
200      atomic_exchange_and_add ((mem), __value) == -__value; })
201 #endif
202
203
204 #ifndef atomic_bit_set
205 # define atomic_bit_set(mem, bit) \
206   (void) atomic_bit_test_set((mem), (bit))
207 #endif
208
209
210 #ifndef atomic_bit_test_set
211 # define atomic_bit_test_set(mem, bit) \
212   ({ __typeof (*(mem)) __oldval;                                              \
213      __typeof (mem) __memp = (mem);                                           \
214      __typeof (*(mem)) __mask = ((__typeof (*(mem))) 1 << (bit));             \
215                                                                               \
216      do                                                                       \
217        __oldval = (*__memp);                                                  \
218      while (__builtin_expect (atomic_compare_and_exchange_bool_acq (__memp,   \
219                                                                     __oldval  \
220                                                                     | __mask, \
221                                                                     __oldval),\
222                               0));                                            \
223                                                                               \
224      __oldval & __mask; })
225 #endif
226
227
228 #ifndef atomic_full_barrier
229 # define atomic_full_barrier() __asm ("" ::: "memory")
230 #endif
231
232
233 #ifndef atomic_read_barrier
234 # define atomic_read_barrier() atomic_full_barrier()
235 #endif
236
237
238 #ifndef atomic_write_barrier
239 # define atomic_write_barrier() atomic_full_barrier()
240 #endif
241
242 #endif  /* atomic.h */