1/*
2 * Copyright © 2011 Ryan Lortie
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 * Author: Ryan Lortie <desrt@desrt.ca>
18 */
19
20#ifndef __G_ATOMIC_H__
21#define __G_ATOMIC_H__
22
23#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
24#error "Only <glib.h> can be included directly."
25#endif
26
27#include <glib/gtypes.h>
28
29G_BEGIN_DECLS
30
31GLIB_AVAILABLE_IN_ALL
32gint g_atomic_int_get (const volatile gint *atomic);
33GLIB_AVAILABLE_IN_ALL
34void g_atomic_int_set (volatile gint *atomic,
35 gint newval);
36GLIB_AVAILABLE_IN_ALL
37void g_atomic_int_inc (volatile gint *atomic);
38GLIB_AVAILABLE_IN_ALL
39gboolean g_atomic_int_dec_and_test (volatile gint *atomic);
40GLIB_AVAILABLE_IN_ALL
41gboolean g_atomic_int_compare_and_exchange (volatile gint *atomic,
42 gint oldval,
43 gint newval);
44GLIB_AVAILABLE_IN_ALL
45gint g_atomic_int_add (volatile gint *atomic,
46 gint val);
47GLIB_AVAILABLE_IN_2_30
48guint g_atomic_int_and (volatile guint *atomic,
49 guint val);
50GLIB_AVAILABLE_IN_2_30
51guint g_atomic_int_or (volatile guint *atomic,
52 guint val);
53GLIB_AVAILABLE_IN_ALL
54guint g_atomic_int_xor (volatile guint *atomic,
55 guint val);
56
57GLIB_AVAILABLE_IN_ALL
58gpointer g_atomic_pointer_get (const volatile void *atomic);
59GLIB_AVAILABLE_IN_ALL
60void g_atomic_pointer_set (volatile void *atomic,
61 gpointer newval);
62GLIB_AVAILABLE_IN_ALL
63gboolean g_atomic_pointer_compare_and_exchange (volatile void *atomic,
64 gpointer oldval,
65 gpointer newval);
66GLIB_AVAILABLE_IN_ALL
67gssize g_atomic_pointer_add (volatile void *atomic,
68 gssize val);
69GLIB_AVAILABLE_IN_2_30
70gsize g_atomic_pointer_and (volatile void *atomic,
71 gsize val);
72GLIB_AVAILABLE_IN_2_30
73gsize g_atomic_pointer_or (volatile void *atomic,
74 gsize val);
75GLIB_AVAILABLE_IN_ALL
76gsize g_atomic_pointer_xor (volatile void *atomic,
77 gsize val);
78
79GLIB_DEPRECATED_IN_2_30_FOR(g_atomic_int_add)
80gint g_atomic_int_exchange_and_add (volatile gint *atomic,
81 gint val);
82
83G_END_DECLS
84
85#if defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
86
87/* We prefer the new C11-style atomic extension of GCC if available */
88#if defined(__ATOMIC_SEQ_CST) && !defined(__clang__)
89
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); \
95 }))
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); \
101 }))
102
103#if GLIB_SIZEOF_VOID_P == 8
104
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); \
109 }))
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); \
115 }))
116
117#else /* GLIB_SIZEOF_VOID_P == 8 */
118
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); \
123 }))
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); \
129 }))
130
131#endif /* GLIB_SIZEOF_VOID_P == 8 */
132
133#else /* defined(__ATOMIC_SEQ_CST) */
134
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 (); \
140 (gint) *(atomic); \
141 }))
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 (); \
148 }))
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); \
154 }))
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 (); \
161 }))
162
163#endif /* !defined(__ATOMIC_SEQ_CST) */
164
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); \
170 }))
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; \
176 }))
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)); \
182 }))
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)); \
188 }))
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)); \
194 }))
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)); \
200 }))
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)); \
206 }))
207
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)); \
213 }))
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)); \
220 }))
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)); \
227 }))
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)); \
234 }))
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)); \
241 }))
242
243#else /* defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) */
244
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)))
263
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)))
278
279#endif /* defined(__GNUC__) && defined(G_ATOMIC_OP_USE_GCC_BUILTINS) */
280
281#endif /* __G_ATOMIC_H__ */
282