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 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
23 #error "Only <glib.h> can be included directly."
26 #ifndef __G_ATOMIC_H__
27 #define __G_ATOMIC_H__
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 guint g_atomic_int_and (volatile guint *atomic,
45 guint g_atomic_int_or (volatile guint *atomic,
47 guint g_atomic_int_xor (volatile guint *atomic,
50 gpointer g_atomic_pointer_get (volatile void *atomic);
51 void g_atomic_pointer_set (volatile void *atomic,
53 gboolean g_atomic_pointer_compare_and_exchange (volatile void *atomic,
56 gssize g_atomic_pointer_add (volatile void *atomic,
58 gsize g_atomic_pointer_and (volatile void *atomic,
60 gsize g_atomic_pointer_or (volatile void *atomic,
62 gsize g_atomic_pointer_xor (volatile void *atomic,
65 #ifndef G_DISABLE_DEPRECATED
66 GLIB_DEPRECATED_FOR(g_atomic_add)
67 gint g_atomic_int_exchange_and_add (volatile gint *atomic,
73 #if defined(__GNUC__) && defined(G_ATOMIC_OP_USE_GCC_BUILTINS)
75 #define g_atomic_int_get(atomic) \
76 (G_GNUC_EXTENSION ({ \
77 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
78 (void) (0 ? *(atomic) ^ *(atomic) : 0); \
79 __sync_synchronize (); \
82 #define g_atomic_int_set(atomic, newval) \
83 (G_GNUC_EXTENSION ({ \
84 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
85 (void) (0 ? *(atomic) ^ (newval) : 0); \
86 *(atomic) = (newval); \
87 __sync_synchronize (); \
89 #define g_atomic_int_inc(atomic) \
90 (G_GNUC_EXTENSION ({ \
91 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
92 (void) (0 ? *(atomic) ^ *(atomic) : 0); \
93 (void) __sync_fetch_and_add ((atomic), 1); \
95 #define g_atomic_int_dec_and_test(atomic) \
96 (G_GNUC_EXTENSION ({ \
97 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
98 (void) (0 ? *(atomic) ^ *(atomic) : 0); \
99 __sync_fetch_and_sub ((atomic), 1) == 1; \
101 #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
102 (G_GNUC_EXTENSION ({ \
103 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
104 (void) (0 ? *(atomic) ^ (newval) ^ (oldval) : 0); \
105 (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval)); \
107 #define g_atomic_int_add(atomic, val) \
108 (G_GNUC_EXTENSION ({ \
109 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
110 (void) (0 ? *(atomic) ^ (val) : 0); \
111 (gint) __sync_fetch_and_add ((atomic), (val)); \
113 #define g_atomic_int_and(atomic, val) \
114 (G_GNUC_EXTENSION ({ \
115 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
116 (void) (0 ? *(atomic) ^ (val) : 0); \
117 (guint) __sync_fetch_and_and ((atomic), (val)); \
119 #define g_atomic_int_or(atomic, val) \
120 (G_GNUC_EXTENSION ({ \
121 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
122 (void) (0 ? *(atomic) ^ (val) : 0); \
123 (guint) __sync_fetch_and_or ((atomic), (val)); \
125 #define g_atomic_int_xor(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_xor ((atomic), (val)); \
132 #define g_atomic_pointer_get(atomic) \
133 (G_GNUC_EXTENSION ({ \
134 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
135 __sync_synchronize (); \
136 (gpointer) *(atomic); \
138 #define g_atomic_pointer_set(atomic, newval) \
139 (G_GNUC_EXTENSION ({ \
140 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
141 (void) (0 ? (gpointer) *(atomic) : 0); \
142 *(atomic) = (__typeof__ (*(atomic))) (gsize) (newval); \
143 __sync_synchronize (); \
145 #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
146 (G_GNUC_EXTENSION ({ \
147 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
148 (void) (0 ? (gpointer) *(atomic) : 0); \
149 (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval)); \
151 #define g_atomic_pointer_add(atomic, val) \
152 (G_GNUC_EXTENSION ({ \
153 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
154 (void) (0 ? (gpointer) *(atomic) : 0); \
155 (void) (0 ? (val) ^ (val) : 0); \
156 (gssize) __sync_fetch_and_add ((atomic), (val)); \
158 #define g_atomic_pointer_and(atomic, val) \
159 (G_GNUC_EXTENSION ({ \
160 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
161 (void) (0 ? (gpointer) *(atomic) : 0); \
162 (void) (0 ? (val) ^ (val) : 0); \
163 (gsize) __sync_fetch_and_and ((atomic), (val)); \
165 #define g_atomic_pointer_or(atomic, val) \
166 (G_GNUC_EXTENSION ({ \
167 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
168 (void) (0 ? (gpointer) *(atomic) : 0); \
169 (void) (0 ? (val) ^ (val) : 0); \
170 (gsize) __sync_fetch_and_or ((atomic), (val)); \
172 #define g_atomic_pointer_xor(atomic, val) \
173 (G_GNUC_EXTENSION ({ \
174 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
175 (void) (0 ? (gpointer) *(atomic) : 0); \
176 (void) (0 ? (val) ^ (val) : 0); \
177 (gsize) __sync_fetch_and_xor ((atomic), (val)); \
180 #else /* defined(__GNUC__) && defined(G_ATOMIC_OP_USE_GCC_BUILTINS) */
182 #define g_atomic_int_get(atomic) \
183 (g_atomic_int_get ((gint *) (atomic)))
184 #define g_atomic_int_set(atomic, newval) \
185 (g_atomic_int_set ((gint *) (atomic), (gint) (newval)))
186 #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
187 (g_atomic_int_compare_and_exchange ((gint *) (atomic), (oldval), (newval)))
188 #define g_atomic_int_add(atomic, val) \
189 (g_atomic_int_add ((gint *) (atomic), (val)))
190 #define g_atomic_int_and(atomic, val) \
191 (g_atomic_int_and ((gint *) (atomic), (val)))
192 #define g_atomic_int_or(atomic, val) \
193 (g_atomic_int_or ((gint *) (atomic), (val)))
194 #define g_atomic_int_xor(atomic, val) \
195 (g_atomic_int_xor ((gint *) (atomic), (val)))
196 #define g_atomic_int_inc(atomic) \
197 (g_atomic_int_inc ((gint *) (atomic)))
198 #define g_atomic_int_dec_and_test(atomic) \
199 (g_atomic_int_dec_and_test ((gint *) (atomic)))
201 #define g_atomic_pointer_get(atomic) \
202 (g_atomic_pointer_get (atomic))
203 #define g_atomic_pointer_set(atomic, newval) \
204 (g_atomic_pointer_set ((atomic), (gpointer) (newval)))
205 #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
206 (g_atomic_pointer_compare_and_exchange ((atomic), (gpointer) (oldval), (gpointer) (newval)))
207 #define g_atomic_pointer_add(atomic, val) \
208 (g_atomic_pointer_add ((atomic), (gssize) (val)))
209 #define g_atomic_pointer_and(atomic, val) \
210 (g_atomic_pointer_and ((atomic), (gsize) (val)))
211 #define g_atomic_pointer_or(atomic, val) \
212 (g_atomic_pointer_or ((atomic), (gsize) (val)))
213 #define g_atomic_pointer_xor(atomic, val) \
214 (g_atomic_pointer_xor ((atomic), (gsize) (val)))
216 #endif /* defined(__GNUC__) && defined(G_ATOMIC_OP_USE_GCC_BUILTINS) */
218 #endif /* __G_ATOMIC_H__ */