gobject: Clean up logic in property checks
[glib.git] / glib / gatomic.h
blob41df1dbd70e932b95e6ca029db6b96e8a4c9df7f
1 /*
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,
17 * USA.
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."
24 #endif
26 #ifndef __G_ATOMIC_H__
27 #define __G_ATOMIC_H__
29 #include <glib/gtypes.h>
31 G_BEGIN_DECLS
33 gint g_atomic_int_get (volatile gint *atomic);
34 void g_atomic_int_set (volatile gint *atomic,
35 gint newval);
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,
39 gint oldval,
40 gint newval);
41 gint g_atomic_int_add (volatile gint *atomic,
42 gint val);
43 guint g_atomic_int_and (volatile guint *atomic,
44 guint val);
45 guint g_atomic_int_or (volatile guint *atomic,
46 guint val);
47 guint g_atomic_int_xor (volatile guint *atomic,
48 guint val);
50 gpointer g_atomic_pointer_get (volatile void *atomic);
51 void g_atomic_pointer_set (volatile void *atomic,
52 gpointer newval);
53 gboolean g_atomic_pointer_compare_and_exchange (volatile void *atomic,
54 gpointer oldval,
55 gpointer newval);
56 gssize g_atomic_pointer_add (volatile void *atomic,
57 gssize val);
58 gsize g_atomic_pointer_and (volatile void *atomic,
59 gsize val);
60 gsize g_atomic_pointer_or (volatile void *atomic,
61 gsize val);
62 gsize g_atomic_pointer_xor (volatile void *atomic,
63 gsize val);
65 GLIB_DEPRECATED_FOR(g_atomic_add)
66 gint g_atomic_int_exchange_and_add (volatile gint *atomic,
67 gint val);
69 G_END_DECLS
71 #if defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
73 #define g_atomic_int_get(atomic) \
74 (G_GNUC_EXTENSION ({ \
75 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
76 (void) (0 ? *(atomic) ^ *(atomic) : 0); \
77 __sync_synchronize (); \
78 (gint) *(atomic); \
79 }))
80 #define g_atomic_int_set(atomic, newval) \
81 (G_GNUC_EXTENSION ({ \
82 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
83 (void) (0 ? *(atomic) ^ (newval) : 0); \
84 *(atomic) = (newval); \
85 __sync_synchronize (); \
86 }))
87 #define g_atomic_int_inc(atomic) \
88 (G_GNUC_EXTENSION ({ \
89 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
90 (void) (0 ? *(atomic) ^ *(atomic) : 0); \
91 (void) __sync_fetch_and_add ((atomic), 1); \
92 }))
93 #define g_atomic_int_dec_and_test(atomic) \
94 (G_GNUC_EXTENSION ({ \
95 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
96 (void) (0 ? *(atomic) ^ *(atomic) : 0); \
97 __sync_fetch_and_sub ((atomic), 1) == 1; \
98 }))
99 #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
100 (G_GNUC_EXTENSION ({ \
101 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
102 (void) (0 ? *(atomic) ^ (newval) ^ (oldval) : 0); \
103 (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval)); \
105 #define g_atomic_int_add(atomic, val) \
106 (G_GNUC_EXTENSION ({ \
107 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
108 (void) (0 ? *(atomic) ^ (val) : 0); \
109 (gint) __sync_fetch_and_add ((atomic), (val)); \
111 #define g_atomic_int_and(atomic, val) \
112 (G_GNUC_EXTENSION ({ \
113 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
114 (void) (0 ? *(atomic) ^ (val) : 0); \
115 (guint) __sync_fetch_and_and ((atomic), (val)); \
117 #define g_atomic_int_or(atomic, val) \
118 (G_GNUC_EXTENSION ({ \
119 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
120 (void) (0 ? *(atomic) ^ (val) : 0); \
121 (guint) __sync_fetch_and_or ((atomic), (val)); \
123 #define g_atomic_int_xor(atomic, val) \
124 (G_GNUC_EXTENSION ({ \
125 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \
126 (void) (0 ? *(atomic) ^ (val) : 0); \
127 (guint) __sync_fetch_and_xor ((atomic), (val)); \
130 #define g_atomic_pointer_get(atomic) \
131 (G_GNUC_EXTENSION ({ \
132 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
133 __sync_synchronize (); \
134 (gpointer) *(atomic); \
136 #define g_atomic_pointer_set(atomic, newval) \
137 (G_GNUC_EXTENSION ({ \
138 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
139 (void) (0 ? (gpointer) *(atomic) : 0); \
140 *(atomic) = (__typeof__ (*(atomic))) (gsize) (newval); \
141 __sync_synchronize (); \
143 #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
144 (G_GNUC_EXTENSION ({ \
145 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
146 (void) (0 ? (gpointer) *(atomic) : 0); \
147 (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval)); \
149 #define g_atomic_pointer_add(atomic, val) \
150 (G_GNUC_EXTENSION ({ \
151 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
152 (void) (0 ? (gpointer) *(atomic) : 0); \
153 (void) (0 ? (val) ^ (val) : 0); \
154 (gssize) __sync_fetch_and_add ((atomic), (val)); \
156 #define g_atomic_pointer_and(atomic, val) \
157 (G_GNUC_EXTENSION ({ \
158 G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
159 (void) (0 ? (gpointer) *(atomic) : 0); \
160 (void) (0 ? (val) ^ (val) : 0); \
161 (gsize) __sync_fetch_and_and ((atomic), (val)); \
163 #define g_atomic_pointer_or(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 (gsize) __sync_fetch_and_or ((atomic), (val)); \
170 #define g_atomic_pointer_xor(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_xor ((atomic), (val)); \
178 #else /* defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) */
180 #define g_atomic_int_get(atomic) \
181 (g_atomic_int_get ((gint *) (atomic)))
182 #define g_atomic_int_set(atomic, newval) \
183 (g_atomic_int_set ((gint *) (atomic), (gint) (newval)))
184 #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
185 (g_atomic_int_compare_and_exchange ((gint *) (atomic), (oldval), (newval)))
186 #define g_atomic_int_add(atomic, val) \
187 (g_atomic_int_add ((gint *) (atomic), (val)))
188 #define g_atomic_int_and(atomic, val) \
189 (g_atomic_int_and ((guint *) (atomic), (val)))
190 #define g_atomic_int_or(atomic, val) \
191 (g_atomic_int_or ((guint *) (atomic), (val)))
192 #define g_atomic_int_xor(atomic, val) \
193 (g_atomic_int_xor ((guint *) (atomic), (val)))
194 #define g_atomic_int_inc(atomic) \
195 (g_atomic_int_inc ((gint *) (atomic)))
196 #define g_atomic_int_dec_and_test(atomic) \
197 (g_atomic_int_dec_and_test ((gint *) (atomic)))
199 #define g_atomic_pointer_get(atomic) \
200 (g_atomic_pointer_get (atomic))
201 #define g_atomic_pointer_set(atomic, newval) \
202 (g_atomic_pointer_set ((atomic), (gpointer) (newval)))
203 #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
204 (g_atomic_pointer_compare_and_exchange ((atomic), (gpointer) (oldval), (gpointer) (newval)))
205 #define g_atomic_pointer_add(atomic, val) \
206 (g_atomic_pointer_add ((atomic), (gssize) (val)))
207 #define g_atomic_pointer_and(atomic, val) \
208 (g_atomic_pointer_and ((atomic), (gsize) (val)))
209 #define g_atomic_pointer_or(atomic, val) \
210 (g_atomic_pointer_or ((atomic), (gsize) (val)))
211 #define g_atomic_pointer_xor(atomic, val) \
212 (g_atomic_pointer_xor ((atomic), (gsize) (val)))
214 #endif /* defined(__GNUC__) && defined(G_ATOMIC_OP_USE_GCC_BUILTINS) */
216 #endif /* __G_ATOMIC_H__ */