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, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * Author: Ryan Lortie <desrt@desrt.ca>
22 #ifndef __G_ATOMIC_H__
23 #define __G_ATOMIC_H__
25 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
26 #error "Only <glib.h> can be included directly."
29 #include <glib/gtypes.h>
33 gint g_atomic_int_get (volatile gint *atomic);
34 void g_atomic_int_set (volatile gint *atomic,
36 void g_atomic_int_inc (volatile gint *atomic);
37 gboolean g_atomic_int_dec_and_test (volatile gint *atomic);
38 gboolean g_atomic_int_compare_and_exchange (volatile gint *atomic,
41 gint g_atomic_int_add (volatile gint *atomic,
43 GLIB_AVAILABLE_IN_2_30
44 guint g_atomic_int_and (volatile guint *atomic,
46 GLIB_AVAILABLE_IN_2_30
47 guint g_atomic_int_or (volatile guint *atomic,
49 guint g_atomic_int_xor (volatile guint *atomic,
52 gpointer g_atomic_pointer_get (volatile void *atomic);
53 void g_atomic_pointer_set (volatile void *atomic,
55 gboolean g_atomic_pointer_compare_and_exchange (volatile void *atomic,
58 gssize g_atomic_pointer_add (volatile void *atomic,
60 GLIB_AVAILABLE_IN_2_30
61 gsize g_atomic_pointer_and (volatile void *atomic,
63 GLIB_AVAILABLE_IN_2_30
64 gsize g_atomic_pointer_or (volatile void *atomic,
66 gsize g_atomic_pointer_xor (volatile void *atomic,
69 GLIB_DEPRECATED_IN_2_30_FOR(g_atomic_add)
70 gint g_atomic_int_exchange_and_add (volatile gint *atomic,
75 #if defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
77 #define g_atomic_int_get(atomic) \
78 (G_GNUC_EXTENSION ({ \
79 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
80 (void) (0 ? *(atomic) ^ *(atomic) : 0); \
81 __sync_synchronize (); \
84 #define g_atomic_int_set(atomic, newval) \
85 (G_GNUC_EXTENSION ({ \
86 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
87 (void) (0 ? *(atomic) ^ (newval) : 0); \
88 *(atomic) = (newval); \
89 __sync_synchronize (); \
91 #define g_atomic_int_inc(atomic) \
92 (G_GNUC_EXTENSION ({ \
93 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
94 (void) (0 ? *(atomic) ^ *(atomic) : 0); \
95 (void) __sync_fetch_and_add ((atomic), 1); \
97 #define g_atomic_int_dec_and_test(atomic) \
98 (G_GNUC_EXTENSION ({ \
99 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
100 (void) (0 ? *(atomic) ^ *(atomic) : 0); \
101 __sync_fetch_and_sub ((atomic), 1) == 1; \
103 #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
104 (G_GNUC_EXTENSION ({ \
105 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
106 (void) (0 ? *(atomic) ^ (newval) ^ (oldval) : 0); \
107 (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval)); \
109 #define g_atomic_int_add(atomic, val) \
110 (G_GNUC_EXTENSION ({ \
111 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
112 (void) (0 ? *(atomic) ^ (val) : 0); \
113 (gint) __sync_fetch_and_add ((atomic), (val)); \
115 #define g_atomic_int_and(atomic, val) \
116 (G_GNUC_EXTENSION ({ \
117 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
118 (void) (0 ? *(atomic) ^ (val) : 0); \
119 (guint) __sync_fetch_and_and ((atomic), (val)); \
121 #define g_atomic_int_or(atomic, val) \
122 (G_GNUC_EXTENSION ({ \
123 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
124 (void) (0 ? *(atomic) ^ (val) : 0); \
125 (guint) __sync_fetch_and_or ((atomic), (val)); \
127 #define g_atomic_int_xor(atomic, val) \
128 (G_GNUC_EXTENSION ({ \
129 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
130 (void) (0 ? *(atomic) ^ (val) : 0); \
131 (guint) __sync_fetch_and_xor ((atomic), (val)); \
134 #define g_atomic_pointer_get(atomic) \
135 (G_GNUC_EXTENSION ({ \
136 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
137 __sync_synchronize (); \
138 (gpointer) *(atomic); \
140 #define g_atomic_pointer_set(atomic, newval) \
141 (G_GNUC_EXTENSION ({ \
142 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
143 (void) (0 ? (gpointer) *(atomic) : 0); \
144 *(atomic) = (__typeof__ (*(atomic))) (gsize) (newval); \
145 __sync_synchronize (); \
147 #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
148 (G_GNUC_EXTENSION ({ \
149 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
150 (void) (0 ? (gpointer) *(atomic) : 0); \
151 (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval)); \
153 #define g_atomic_pointer_add(atomic, val) \
154 (G_GNUC_EXTENSION ({ \
155 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
156 (void) (0 ? (gpointer) *(atomic) : 0); \
157 (void) (0 ? (val) ^ (val) : 0); \
158 (gssize) __sync_fetch_and_add ((atomic), (val)); \
160 #define g_atomic_pointer_and(atomic, val) \
161 (G_GNUC_EXTENSION ({ \
162 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
163 (void) (0 ? (gpointer) *(atomic) : 0); \
164 (void) (0 ? (val) ^ (val) : 0); \
165 (gsize) __sync_fetch_and_and ((atomic), (val)); \
167 #define g_atomic_pointer_or(atomic, val) \
168 (G_GNUC_EXTENSION ({ \
169 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
170 (void) (0 ? (gpointer) *(atomic) : 0); \
171 (void) (0 ? (val) ^ (val) : 0); \
172 (gsize) __sync_fetch_and_or ((atomic), (val)); \
174 #define g_atomic_pointer_xor(atomic, val) \
175 (G_GNUC_EXTENSION ({ \
176 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
177 (void) (0 ? (gpointer) *(atomic) : 0); \
178 (void) (0 ? (val) ^ (val) : 0); \
179 (gsize) __sync_fetch_and_xor ((atomic), (val)); \
182 #else /* defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) */
184 #define g_atomic_int_get(atomic) \
185 (g_atomic_int_get ((gint *) (atomic)))
186 #define g_atomic_int_set(atomic, newval) \
187 (g_atomic_int_set ((gint *) (atomic), (gint) (newval)))
188 #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
189 (g_atomic_int_compare_and_exchange ((gint *) (atomic), (oldval), (newval)))
190 #define g_atomic_int_add(atomic, val) \
191 (g_atomic_int_add ((gint *) (atomic), (val)))
192 #define g_atomic_int_and(atomic, val) \
193 (g_atomic_int_and ((guint *) (atomic), (val)))
194 #define g_atomic_int_or(atomic, val) \
195 (g_atomic_int_or ((guint *) (atomic), (val)))
196 #define g_atomic_int_xor(atomic, val) \
197 (g_atomic_int_xor ((guint *) (atomic), (val)))
198 #define g_atomic_int_inc(atomic) \
199 (g_atomic_int_inc ((gint *) (atomic)))
200 #define g_atomic_int_dec_and_test(atomic) \
201 (g_atomic_int_dec_and_test ((gint *) (atomic)))
203 #define g_atomic_pointer_get(atomic) \
204 (g_atomic_pointer_get (atomic))
205 #define g_atomic_pointer_set(atomic, newval) \
206 (g_atomic_pointer_set ((atomic), (gpointer) (newval)))
207 #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
208 (g_atomic_pointer_compare_and_exchange ((atomic), (gpointer) (oldval), (gpointer) (newval)))
209 #define g_atomic_pointer_add(atomic, val) \
210 (g_atomic_pointer_add ((atomic), (gssize) (val)))
211 #define g_atomic_pointer_and(atomic, val) \
212 (g_atomic_pointer_and ((atomic), (gsize) (val)))
213 #define g_atomic_pointer_or(atomic, val) \
214 (g_atomic_pointer_or ((atomic), (gsize) (val)))
215 #define g_atomic_pointer_xor(atomic, val) \
216 (g_atomic_pointer_xor ((atomic), (gsize) (val)))
218 #endif /* defined(__GNUC__) && defined(G_ATOMIC_OP_USE_GCC_BUILTINS) */
220 #endif /* __G_ATOMIC_H__ */