2 * Copyright © 2011 Ryan Lortie
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
20 #ifndef __G_ATOMIC_H__
21 #define __G_ATOMIC_H__
23 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
24 #error "Only <glib.h> can be included directly."
27 #include <glib/gtypes.h>
32 gint g_atomic_int_get (const volatile gint *atomic);
34 void g_atomic_int_set (volatile gint *atomic,
37 void g_atomic_int_inc (volatile gint *atomic);
39 gboolean g_atomic_int_dec_and_test (volatile gint *atomic);
41 gboolean g_atomic_int_compare_and_exchange (volatile gint *atomic,
45 gint g_atomic_int_add (volatile gint *atomic,
47 GLIB_AVAILABLE_IN_2_30
48 guint g_atomic_int_and (volatile guint *atomic,
50 GLIB_AVAILABLE_IN_2_30
51 guint g_atomic_int_or (volatile guint *atomic,
54 guint g_atomic_int_xor (volatile guint *atomic,
58 gpointer g_atomic_pointer_get (const volatile void *atomic);
60 void g_atomic_pointer_set (volatile void *atomic,
63 gboolean g_atomic_pointer_compare_and_exchange (volatile void *atomic,
67 gssize g_atomic_pointer_add (volatile void *atomic,
69 GLIB_AVAILABLE_IN_2_30
70 gsize g_atomic_pointer_and (volatile void *atomic,
72 GLIB_AVAILABLE_IN_2_30
73 gsize g_atomic_pointer_or (volatile void *atomic,
76 gsize g_atomic_pointer_xor (volatile void *atomic,
79 GLIB_DEPRECATED_IN_2_30_FOR(g_atomic_int_add)
80 gint g_atomic_int_exchange_and_add (volatile gint *atomic,
85 #if defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
87 /* We prefer the new C11-style atomic extension of GCC if available */
88 #if defined(__ATOMIC_SEQ_CST) && !defined(__clang__)
90 /* We use __atomic_load_4, so we rely on this being true */
91 G_STATIC_ASSERT (sizeof (gint) == 4);
93 #define g_atomic_int_get(atomic) \
94 (G_GNUC_EXTENSION ({ \
95 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
96 (void) (0 ? *(atomic) ^ *(atomic) : 0); \
97 (gint) __atomic_load_4 ((atomic), __ATOMIC_SEQ_CST); \
99 #define g_atomic_int_set(atomic, newval) \
100 (G_GNUC_EXTENSION ({ \
101 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
102 (void) (0 ? *(atomic) ^ (newval) : 0); \
103 __atomic_store_4 ((atomic), (newval), __ATOMIC_SEQ_CST); \
106 #if GLIB_SIZEOF_VOID_P == 8
108 #define g_atomic_pointer_get(atomic) \
109 (G_GNUC_EXTENSION ({ \
110 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
111 (gpointer) __atomic_load_8 ((atomic), __ATOMIC_SEQ_CST); \
113 #define g_atomic_pointer_set(atomic, newval) \
114 (G_GNUC_EXTENSION ({ \
115 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
116 (void) (0 ? (gpointer) *(atomic) : 0); \
117 __atomic_store_8 ((atomic), (gsize) (newval), __ATOMIC_SEQ_CST); \
120 #else /* GLIB_SIZEOF_VOID_P == 8 */
122 /* Assume that if GLIB_SIZEOF_VOID_P is not 8, it must be 4 */
123 G_STATIC_ASSERT (sizeof (gpointer) == 4);
125 #define g_atomic_pointer_get(atomic) \
126 (G_GNUC_EXTENSION ({ \
127 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
128 (gpointer) __atomic_load_4 ((atomic), __ATOMIC_SEQ_CST); \
130 #define g_atomic_pointer_set(atomic, newval) \
131 (G_GNUC_EXTENSION ({ \
132 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
133 (void) (0 ? (gpointer) *(atomic) : 0); \
134 __atomic_store_4 ((atomic), (gsize) (newval), __ATOMIC_SEQ_CST); \
137 #endif /* GLIB_SIZEOF_VOID_P == 8 */
139 #else /* defined(__ATOMIC_SEQ_CST) */
141 #define g_atomic_int_get(atomic) \
142 (G_GNUC_EXTENSION ({ \
143 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
144 (void) (0 ? *(atomic) ^ *(atomic) : 0); \
145 __sync_synchronize (); \
148 #define g_atomic_int_set(atomic, newval) \
149 (G_GNUC_EXTENSION ({ \
150 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
151 (void) (0 ? *(atomic) ^ (newval) : 0); \
152 *(atomic) = (newval); \
153 __sync_synchronize (); \
155 #define g_atomic_pointer_get(atomic) \
156 (G_GNUC_EXTENSION ({ \
157 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
158 __sync_synchronize (); \
159 (gpointer) *(atomic); \
161 #define g_atomic_pointer_set(atomic, newval) \
162 (G_GNUC_EXTENSION ({ \
163 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
164 (void) (0 ? (gpointer) *(atomic) : 0); \
165 *(atomic) = (__typeof__ (*(atomic))) (gsize) (newval); \
166 __sync_synchronize (); \
169 #endif /* !defined(__ATOMIC_SEQ_CST) */
171 #define g_atomic_int_inc(atomic) \
172 (G_GNUC_EXTENSION ({ \
173 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
174 (void) (0 ? *(atomic) ^ *(atomic) : 0); \
175 (void) __sync_fetch_and_add ((atomic), 1); \
177 #define g_atomic_int_dec_and_test(atomic) \
178 (G_GNUC_EXTENSION ({ \
179 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
180 (void) (0 ? *(atomic) ^ *(atomic) : 0); \
181 __sync_fetch_and_sub ((atomic), 1) == 1; \
183 #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
184 (G_GNUC_EXTENSION ({ \
185 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
186 (void) (0 ? *(atomic) ^ (newval) ^ (oldval) : 0); \
187 (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval)); \
189 #define g_atomic_int_add(atomic, val) \
190 (G_GNUC_EXTENSION ({ \
191 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
192 (void) (0 ? *(atomic) ^ (val) : 0); \
193 (gint) __sync_fetch_and_add ((atomic), (val)); \
195 #define g_atomic_int_and(atomic, val) \
196 (G_GNUC_EXTENSION ({ \
197 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
198 (void) (0 ? *(atomic) ^ (val) : 0); \
199 (guint) __sync_fetch_and_and ((atomic), (val)); \
201 #define g_atomic_int_or(atomic, val) \
202 (G_GNUC_EXTENSION ({ \
203 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
204 (void) (0 ? *(atomic) ^ (val) : 0); \
205 (guint) __sync_fetch_and_or ((atomic), (val)); \
207 #define g_atomic_int_xor(atomic, val) \
208 (G_GNUC_EXTENSION ({ \
209 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
210 (void) (0 ? *(atomic) ^ (val) : 0); \
211 (guint) __sync_fetch_and_xor ((atomic), (val)); \
214 #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
215 (G_GNUC_EXTENSION ({ \
216 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
217 (void) (0 ? (gpointer) *(atomic) : 0); \
218 (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval)); \
220 #define g_atomic_pointer_add(atomic, val) \
221 (G_GNUC_EXTENSION ({ \
222 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
223 (void) (0 ? (gpointer) *(atomic) : 0); \
224 (void) (0 ? (val) ^ (val) : 0); \
225 (gssize) __sync_fetch_and_add ((atomic), (val)); \
227 #define g_atomic_pointer_and(atomic, val) \
228 (G_GNUC_EXTENSION ({ \
229 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
230 (void) (0 ? (gpointer) *(atomic) : 0); \
231 (void) (0 ? (val) ^ (val) : 0); \
232 (gsize) __sync_fetch_and_and ((atomic), (val)); \
234 #define g_atomic_pointer_or(atomic, val) \
235 (G_GNUC_EXTENSION ({ \
236 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
237 (void) (0 ? (gpointer) *(atomic) : 0); \
238 (void) (0 ? (val) ^ (val) : 0); \
239 (gsize) __sync_fetch_and_or ((atomic), (val)); \
241 #define g_atomic_pointer_xor(atomic, val) \
242 (G_GNUC_EXTENSION ({ \
243 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
244 (void) (0 ? (gpointer) *(atomic) : 0); \
245 (void) (0 ? (val) ^ (val) : 0); \
246 (gsize) __sync_fetch_and_xor ((atomic), (val)); \
249 #else /* defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) */
251 #define g_atomic_int_get(atomic) \
252 (g_atomic_int_get ((gint *) (atomic)))
253 #define g_atomic_int_set(atomic, newval) \
254 (g_atomic_int_set ((gint *) (atomic), (gint) (newval)))
255 #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
256 (g_atomic_int_compare_and_exchange ((gint *) (atomic), (oldval), (newval)))
257 #define g_atomic_int_add(atomic, val) \
258 (g_atomic_int_add ((gint *) (atomic), (val)))
259 #define g_atomic_int_and(atomic, val) \
260 (g_atomic_int_and ((guint *) (atomic), (val)))
261 #define g_atomic_int_or(atomic, val) \
262 (g_atomic_int_or ((guint *) (atomic), (val)))
263 #define g_atomic_int_xor(atomic, val) \
264 (g_atomic_int_xor ((guint *) (atomic), (val)))
265 #define g_atomic_int_inc(atomic) \
266 (g_atomic_int_inc ((gint *) (atomic)))
267 #define g_atomic_int_dec_and_test(atomic) \
268 (g_atomic_int_dec_and_test ((gint *) (atomic)))
270 #define g_atomic_pointer_get(atomic) \
271 (g_atomic_pointer_get (atomic))
272 #define g_atomic_pointer_set(atomic, newval) \
273 (g_atomic_pointer_set ((atomic), (gpointer) (newval)))
274 #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
275 (g_atomic_pointer_compare_and_exchange ((atomic), (gpointer) (oldval), (gpointer) (newval)))
276 #define g_atomic_pointer_add(atomic, val) \
277 (g_atomic_pointer_add ((atomic), (gssize) (val)))
278 #define g_atomic_pointer_and(atomic, val) \
279 (g_atomic_pointer_and ((atomic), (gsize) (val)))
280 #define g_atomic_pointer_or(atomic, val) \
281 (g_atomic_pointer_or ((atomic), (gsize) (val)))
282 #define g_atomic_pointer_xor(atomic, val) \
283 (g_atomic_pointer_xor ((atomic), (gsize) (val)))
285 #endif /* defined(__GNUC__) && defined(G_ATOMIC_OP_USE_GCC_BUILTINS) */
287 #endif /* __G_ATOMIC_H__ */