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_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 #define g_atomic_int_get(atomic) \
88 (G_GNUC_EXTENSION ({ \
89 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
90 (void) (0 ? *(atomic) ^ *(atomic) : 0); \
91 __sync_synchronize (); \
94 #define g_atomic_int_set(atomic, newval) \
95 (G_GNUC_EXTENSION ({ \
96 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
97 (void) (0 ? *(atomic) ^ (newval) : 0); \
98 *(atomic) = (newval); \
99 __sync_synchronize (); \
101 #define g_atomic_int_inc(atomic) \
102 (G_GNUC_EXTENSION ({ \
103 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
104 (void) (0 ? *(atomic) ^ *(atomic) : 0); \
105 (void) __sync_fetch_and_add ((atomic), 1); \
107 #define g_atomic_int_dec_and_test(atomic) \
108 (G_GNUC_EXTENSION ({ \
109 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
110 (void) (0 ? *(atomic) ^ *(atomic) : 0); \
111 __sync_fetch_and_sub ((atomic), 1) == 1; \
113 #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
114 (G_GNUC_EXTENSION ({ \
115 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
116 (void) (0 ? *(atomic) ^ (newval) ^ (oldval) : 0); \
117 (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval)); \
119 #define g_atomic_int_add(atomic, val) \
120 (G_GNUC_EXTENSION ({ \
121 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
122 (void) (0 ? *(atomic) ^ (val) : 0); \
123 (gint) __sync_fetch_and_add ((atomic), (val)); \
125 #define g_atomic_int_and(atomic, val) \
126 (G_GNUC_EXTENSION ({ \
127 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
128 (void) (0 ? *(atomic) ^ (val) : 0); \
129 (guint) __sync_fetch_and_and ((atomic), (val)); \
131 #define g_atomic_int_or(atomic, val) \
132 (G_GNUC_EXTENSION ({ \
133 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
134 (void) (0 ? *(atomic) ^ (val) : 0); \
135 (guint) __sync_fetch_and_or ((atomic), (val)); \
137 #define g_atomic_int_xor(atomic, val) \
138 (G_GNUC_EXTENSION ({ \
139 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
140 (void) (0 ? *(atomic) ^ (val) : 0); \
141 (guint) __sync_fetch_and_xor ((atomic), (val)); \
144 #define g_atomic_pointer_get(atomic) \
145 (G_GNUC_EXTENSION ({ \
146 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
147 __sync_synchronize (); \
148 (gpointer) *(atomic); \
150 #define g_atomic_pointer_set(atomic, newval) \
151 (G_GNUC_EXTENSION ({ \
152 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
153 (void) (0 ? (gpointer) *(atomic) : 0); \
154 *(atomic) = (__typeof__ (*(atomic))) (gsize) (newval); \
155 __sync_synchronize (); \
157 #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
158 (G_GNUC_EXTENSION ({ \
159 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
160 (void) (0 ? (gpointer) *(atomic) : 0); \
161 (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval)); \
163 #define g_atomic_pointer_add(atomic, val) \
164 (G_GNUC_EXTENSION ({ \
165 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
166 (void) (0 ? (gpointer) *(atomic) : 0); \
167 (void) (0 ? (val) ^ (val) : 0); \
168 (gssize) __sync_fetch_and_add ((atomic), (val)); \
170 #define g_atomic_pointer_and(atomic, val) \
171 (G_GNUC_EXTENSION ({ \
172 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
173 (void) (0 ? (gpointer) *(atomic) : 0); \
174 (void) (0 ? (val) ^ (val) : 0); \
175 (gsize) __sync_fetch_and_and ((atomic), (val)); \
177 #define g_atomic_pointer_or(atomic, val) \
178 (G_GNUC_EXTENSION ({ \
179 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
180 (void) (0 ? (gpointer) *(atomic) : 0); \
181 (void) (0 ? (val) ^ (val) : 0); \
182 (gsize) __sync_fetch_and_or ((atomic), (val)); \
184 #define g_atomic_pointer_xor(atomic, val) \
185 (G_GNUC_EXTENSION ({ \
186 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
187 (void) (0 ? (gpointer) *(atomic) : 0); \
188 (void) (0 ? (val) ^ (val) : 0); \
189 (gsize) __sync_fetch_and_xor ((atomic), (val)); \
192 #else /* defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) */
194 #define g_atomic_int_get(atomic) \
195 (g_atomic_int_get ((gint *) (atomic)))
196 #define g_atomic_int_set(atomic, newval) \
197 (g_atomic_int_set ((gint *) (atomic), (gint) (newval)))
198 #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
199 (g_atomic_int_compare_and_exchange ((gint *) (atomic), (oldval), (newval)))
200 #define g_atomic_int_add(atomic, val) \
201 (g_atomic_int_add ((gint *) (atomic), (val)))
202 #define g_atomic_int_and(atomic, val) \
203 (g_atomic_int_and ((guint *) (atomic), (val)))
204 #define g_atomic_int_or(atomic, val) \
205 (g_atomic_int_or ((guint *) (atomic), (val)))
206 #define g_atomic_int_xor(atomic, val) \
207 (g_atomic_int_xor ((guint *) (atomic), (val)))
208 #define g_atomic_int_inc(atomic) \
209 (g_atomic_int_inc ((gint *) (atomic)))
210 #define g_atomic_int_dec_and_test(atomic) \
211 (g_atomic_int_dec_and_test ((gint *) (atomic)))
213 #define g_atomic_pointer_get(atomic) \
214 (g_atomic_pointer_get (atomic))
215 #define g_atomic_pointer_set(atomic, newval) \
216 (g_atomic_pointer_set ((atomic), (gpointer) (newval)))
217 #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
218 (g_atomic_pointer_compare_and_exchange ((atomic), (gpointer) (oldval), (gpointer) (newval)))
219 #define g_atomic_pointer_add(atomic, val) \
220 (g_atomic_pointer_add ((atomic), (gssize) (val)))
221 #define g_atomic_pointer_and(atomic, val) \
222 (g_atomic_pointer_and ((atomic), (gsize) (val)))
223 #define g_atomic_pointer_or(atomic, val) \
224 (g_atomic_pointer_or ((atomic), (gsize) (val)))
225 #define g_atomic_pointer_xor(atomic, val) \
226 (g_atomic_pointer_xor ((atomic), (gsize) (val)))
228 #endif /* defined(__GNUC__) && defined(G_ATOMIC_OP_USE_GCC_BUILTINS) */
230 #endif /* __G_ATOMIC_H__ */