OSDN Git Service

ldso: initialize stack_chk_guard after TLS is initialized
[uclinux-h8/uClibc.git] / include / atomic.h
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.
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, newval, oldval)             \
75   atomic_compare_and_exchange_val_acq (mem, newval, oldval)
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, newval, oldval) \
99   atomic_compare_and_exchange_bool_acq (mem, newval, oldval)
100 #endif
101
102
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);                                  \
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 #ifndef atomic_exchange_rel
121 # define atomic_exchange_rel(mem, newvalue) atomic_exchange_acq (mem, newvalue)
122 #endif
123
124
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);                                     \
131                                                                               \
132      do                                                                       \
133        __oldval = (*__memp);                                                  \
134      while (__builtin_expect (atomic_compare_and_exchange_bool_acq (__memp,   \
135                                                                     __oldval  \
136                                                                     + __value,\
137                                                                     __oldval),\
138                               0));                                            \
139                                                                               \
140      __oldval; })
141 #endif
142
143
144 #ifndef atomic_add
145 # define atomic_add(mem, value) (void) atomic_exchange_and_add ((mem), (value))
146 #endif
147
148
149 #ifndef atomic_increment
150 # define atomic_increment(mem) atomic_add ((mem), 1)
151 #endif
152
153
154 #ifndef atomic_increment_val
155 # define atomic_increment_val(mem) (atomic_exchange_and_add ((mem), 1) + 1)
156 #endif
157
158
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)
163 #endif
164
165
166 #ifndef atomic_decrement
167 # define atomic_decrement(mem) atomic_add ((mem), -1)
168 #endif
169
170
171 #ifndef atomic_decrement_val
172 # define atomic_decrement_val(mem) (atomic_exchange_and_add ((mem), -1) - 1)
173 #endif
174
175
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)
180 #endif
181
182
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);                                           \
188                                                                               \
189      do                                                                       \
190        {                                                                      \
191          __oldval = *__memp;                                                  \
192          if (__builtin_expect (__oldval <= 0, 0))                             \
193            break;                                                             \
194        }                                                                      \
195      while (__builtin_expect (atomic_compare_and_exchange_bool_acq (__memp,   \
196                                                                     __oldval  \
197                                                                     - 1,      \
198                                                                     __oldval),\
199                               0));\
200      __oldval; })
201 #endif
202
203
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; })
208 #endif
209
210
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; })
215 #endif
216
217
218 #ifndef atomic_bit_set
219 # define atomic_bit_set(mem, bit) \
220   (void) atomic_bit_test_set(mem, bit)
221 #endif
222
223
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));             \
229                                                                               \
230      do                                                                       \
231        __oldval = (*__memp);                                                  \
232      while (__builtin_expect (atomic_compare_and_exchange_bool_acq (__memp,   \
233                                                                     __oldval  \
234                                                                     | __mask, \
235                                                                     __oldval),\
236                               0));                                            \
237                                                                               \
238      __oldval & __mask; })
239 #endif
240
241
242 #ifndef atomic_full_barrier
243 # define atomic_full_barrier() __asm__ ("" ::: "memory")
244 #endif
245
246
247 #ifndef atomic_read_barrier
248 # define atomic_read_barrier() atomic_full_barrier ()
249 #endif
250
251
252 #ifndef atomic_write_barrier
253 # define atomic_write_barrier() atomic_full_barrier ()
254 #endif
255
256
257 #ifndef atomic_delay
258 # define atomic_delay() do { /* nothing */ } while (0)
259 #endif
260
261 #endif  /* atomic.h */