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 #define g_atomic_int_get(atomic) \
91 (G_GNUC_EXTENSION ({ \
92 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
93 (void) (0 ? *(atomic) ^ *(atomic) : 0); \
94 (gint) __atomic_load_4 ((atomic), __ATOMIC_SEQ_CST); \
96 #define g_atomic_int_set(atomic, newval) \
97 (G_GNUC_EXTENSION ({ \
98 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
99 (void) (0 ? *(atomic) ^ (newval) : 0); \
100 __atomic_store_4 ((atomic), (newval), __ATOMIC_SEQ_CST); \
103 #if GLIB_SIZEOF_VOID_P == 8
105 #define g_atomic_pointer_get(atomic) \
106 (G_GNUC_EXTENSION ({ \
107 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
108 (gpointer) __atomic_load_8 ((atomic), __ATOMIC_SEQ_CST); \
110 #define g_atomic_pointer_set(atomic, newval) \
111 (G_GNUC_EXTENSION ({ \
112 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
113 (void) (0 ? (gpointer) *(atomic) : 0); \
114 __atomic_store_8 ((atomic), (gsize) (newval), __ATOMIC_SEQ_CST); \
117 #else /* GLIB_SIZEOF_VOID_P == 8 */
119 #define g_atomic_pointer_get(atomic) \
120 (G_GNUC_EXTENSION ({ \
121 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
122 (gpointer) __atomic_load_4 ((atomic), __ATOMIC_SEQ_CST); \
124 #define g_atomic_pointer_set(atomic, newval) \
125 (G_GNUC_EXTENSION ({ \
126 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
127 (void) (0 ? (gpointer) *(atomic) : 0); \
128 __atomic_store_4 ((atomic), (gsize) (newval), __ATOMIC_SEQ_CST); \
131 #endif /* GLIB_SIZEOF_VOID_P == 8 */
133 #else /* defined(__ATOMIC_SEQ_CST) */
135 #define g_atomic_int_get(atomic) \
136 (G_GNUC_EXTENSION ({ \
137 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
138 (void) (0 ? *(atomic) ^ *(atomic) : 0); \
139 __sync_synchronize (); \
142 #define g_atomic_int_set(atomic, newval) \
143 (G_GNUC_EXTENSION ({ \
144 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
145 (void) (0 ? *(atomic) ^ (newval) : 0); \
146 *(atomic) = (newval); \
147 __sync_synchronize (); \
149 #define g_atomic_pointer_get(atomic) \
150 (G_GNUC_EXTENSION ({ \
151 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
152 __sync_synchronize (); \
153 (gpointer) *(atomic); \
155 #define g_atomic_pointer_set(atomic, newval) \
156 (G_GNUC_EXTENSION ({ \
157 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
158 (void) (0 ? (gpointer) *(atomic) : 0); \
159 *(atomic) = (__typeof__ (*(atomic))) (gsize) (newval); \
160 __sync_synchronize (); \
163 #endif /* !defined(__ATOMIC_SEQ_CST) */
165 #define g_atomic_int_inc(atomic) \
166 (G_GNUC_EXTENSION ({ \
167 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
168 (void) (0 ? *(atomic) ^ *(atomic) : 0); \
169 (void) __sync_fetch_and_add ((atomic), 1); \
171 #define g_atomic_int_dec_and_test(atomic) \
172 (G_GNUC_EXTENSION ({ \
173 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
174 (void) (0 ? *(atomic) ^ *(atomic) : 0); \
175 __sync_fetch_and_sub ((atomic), 1) == 1; \
177 #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
178 (G_GNUC_EXTENSION ({ \
179 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
180 (void) (0 ? *(atomic) ^ (newval) ^ (oldval) : 0); \
181 (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval)); \
183 #define g_atomic_int_add(atomic, val) \
184 (G_GNUC_EXTENSION ({ \
185 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
186 (void) (0 ? *(atomic) ^ (val) : 0); \
187 (gint) __sync_fetch_and_add ((atomic), (val)); \
189 #define g_atomic_int_and(atomic, val) \
190 (G_GNUC_EXTENSION ({ \
191 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
192 (void) (0 ? *(atomic) ^ (val) : 0); \
193 (guint) __sync_fetch_and_and ((atomic), (val)); \
195 #define g_atomic_int_or(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_or ((atomic), (val)); \
201 #define g_atomic_int_xor(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_xor ((atomic), (val)); \
208 #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
209 (G_GNUC_EXTENSION ({ \
210 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
211 (void) (0 ? (gpointer) *(atomic) : 0); \
212 (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval)); \
214 #define g_atomic_pointer_add(atomic, val) \
215 (G_GNUC_EXTENSION ({ \
216 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
217 (void) (0 ? (gpointer) *(atomic) : 0); \
218 (void) (0 ? (val) ^ (val) : 0); \
219 (gssize) __sync_fetch_and_add ((atomic), (val)); \
221 #define g_atomic_pointer_and(atomic, val) \
222 (G_GNUC_EXTENSION ({ \
223 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
224 (void) (0 ? (gpointer) *(atomic) : 0); \
225 (void) (0 ? (val) ^ (val) : 0); \
226 (gsize) __sync_fetch_and_and ((atomic), (val)); \
228 #define g_atomic_pointer_or(atomic, val) \
229 (G_GNUC_EXTENSION ({ \
230 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
231 (void) (0 ? (gpointer) *(atomic) : 0); \
232 (void) (0 ? (val) ^ (val) : 0); \
233 (gsize) __sync_fetch_and_or ((atomic), (val)); \
235 #define g_atomic_pointer_xor(atomic, val) \
236 (G_GNUC_EXTENSION ({ \
237 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
238 (void) (0 ? (gpointer) *(atomic) : 0); \
239 (void) (0 ? (val) ^ (val) : 0); \
240 (gsize) __sync_fetch_and_xor ((atomic), (val)); \
243 #else /* defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) */
245 #define g_atomic_int_get(atomic) \
246 (g_atomic_int_get ((gint *) (atomic)))
247 #define g_atomic_int_set(atomic, newval) \
248 (g_atomic_int_set ((gint *) (atomic), (gint) (newval)))
249 #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
250 (g_atomic_int_compare_and_exchange ((gint *) (atomic), (oldval), (newval)))
251 #define g_atomic_int_add(atomic, val) \
252 (g_atomic_int_add ((gint *) (atomic), (val)))
253 #define g_atomic_int_and(atomic, val) \
254 (g_atomic_int_and ((guint *) (atomic), (val)))
255 #define g_atomic_int_or(atomic, val) \
256 (g_atomic_int_or ((guint *) (atomic), (val)))
257 #define g_atomic_int_xor(atomic, val) \
258 (g_atomic_int_xor ((guint *) (atomic), (val)))
259 #define g_atomic_int_inc(atomic) \
260 (g_atomic_int_inc ((gint *) (atomic)))
261 #define g_atomic_int_dec_and_test(atomic) \
262 (g_atomic_int_dec_and_test ((gint *) (atomic)))
264 #define g_atomic_pointer_get(atomic) \
265 (g_atomic_pointer_get (atomic))
266 #define g_atomic_pointer_set(atomic, newval) \
267 (g_atomic_pointer_set ((atomic), (gpointer) (newval)))
268 #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
269 (g_atomic_pointer_compare_and_exchange ((atomic), (gpointer) (oldval), (gpointer) (newval)))
270 #define g_atomic_pointer_add(atomic, val) \
271 (g_atomic_pointer_add ((atomic), (gssize) (val)))
272 #define g_atomic_pointer_and(atomic, val) \
273 (g_atomic_pointer_and ((atomic), (gsize) (val)))
274 #define g_atomic_pointer_or(atomic, val) \
275 (g_atomic_pointer_or ((atomic), (gsize) (val)))
276 #define g_atomic_pointer_xor(atomic, val) \
277 (g_atomic_pointer_xor ((atomic), (gsize) (val)))
279 #endif /* defined(__GNUC__) && defined(G_ATOMIC_OP_USE_GCC_BUILTINS) */
281 #endif /* __G_ATOMIC_H__ */