gfileutils: Add missing g_free() in error path
[platform/upstream/glib.git] / glib / gatomic.h
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
29 G_BEGIN_DECLS
30
31 GLIB_AVAILABLE_IN_ALL
32 gint                    g_atomic_int_get                      (const volatile gint *atomic);
33 GLIB_AVAILABLE_IN_ALL
34 void                    g_atomic_int_set                      (volatile gint  *atomic,
35                                                                gint            newval);
36 GLIB_AVAILABLE_IN_ALL
37 void                    g_atomic_int_inc                      (volatile gint  *atomic);
38 GLIB_AVAILABLE_IN_ALL
39 gboolean                g_atomic_int_dec_and_test             (volatile gint  *atomic);
40 GLIB_AVAILABLE_IN_ALL
41 gboolean                g_atomic_int_compare_and_exchange     (volatile gint  *atomic,
42                                                                gint            oldval,
43                                                                gint            newval);
44 GLIB_AVAILABLE_IN_ALL
45 gint                    g_atomic_int_add                      (volatile gint  *atomic,
46                                                                gint            val);
47 GLIB_AVAILABLE_IN_2_30
48 guint                   g_atomic_int_and                      (volatile guint *atomic,
49                                                                guint           val);
50 GLIB_AVAILABLE_IN_2_30
51 guint                   g_atomic_int_or                       (volatile guint *atomic,
52                                                                guint           val);
53 GLIB_AVAILABLE_IN_ALL
54 guint                   g_atomic_int_xor                      (volatile guint *atomic,
55                                                                guint           val);
56
57 GLIB_AVAILABLE_IN_ALL
58 gpointer                g_atomic_pointer_get                  (const volatile void *atomic);
59 GLIB_AVAILABLE_IN_ALL
60 void                    g_atomic_pointer_set                  (volatile void  *atomic,
61                                                                gpointer        newval);
62 GLIB_AVAILABLE_IN_ALL
63 gboolean                g_atomic_pointer_compare_and_exchange (volatile void  *atomic,
64                                                                gpointer        oldval,
65                                                                gpointer        newval);
66 GLIB_AVAILABLE_IN_ALL
67 gssize                  g_atomic_pointer_add                  (volatile void  *atomic,
68                                                                gssize          val);
69 GLIB_AVAILABLE_IN_2_30
70 gsize                   g_atomic_pointer_and                  (volatile void  *atomic,
71                                                                gsize           val);
72 GLIB_AVAILABLE_IN_2_30
73 gsize                   g_atomic_pointer_or                   (volatile void  *atomic,
74                                                                gsize           val);
75 GLIB_AVAILABLE_IN_ALL
76 gsize                   g_atomic_pointer_xor                  (volatile void  *atomic,
77                                                                gsize           val);
78
79 GLIB_DEPRECATED_IN_2_30_FOR(g_atomic_int_add)
80 gint                    g_atomic_int_exchange_and_add         (volatile gint  *atomic,
81                                                                gint            val);
82
83 G_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 /* We use __atomic_load_4, so we rely on this being true */
91 G_STATIC_ASSERT (sizeof (gint) == 4);
92
93 #define g_atomic_int_get(atomic) \
94   (G_GNUC_EXTENSION ({                                                       \
95     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
96     (void) (0 ? *(atomic) ^ *(atomic) : 0);                                  \
97     (gint) __atomic_load_4 ((atomic), __ATOMIC_SEQ_CST);                     \
98   }))
99 #define g_atomic_int_set(atomic, newval) \
100   (G_GNUC_EXTENSION ({                                                       \
101     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
102     (void) (0 ? *(atomic) ^ (newval) : 0);                                   \
103     __atomic_store_4 ((atomic), (newval), __ATOMIC_SEQ_CST);                 \
104   }))
105
106 #if GLIB_SIZEOF_VOID_P == 8
107
108 #define g_atomic_pointer_get(atomic) \
109   (G_GNUC_EXTENSION ({                                                       \
110     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
111     (gpointer) __atomic_load_8 ((atomic), __ATOMIC_SEQ_CST);                 \
112   }))
113 #define g_atomic_pointer_set(atomic, newval) \
114   (G_GNUC_EXTENSION ({                                                       \
115     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
116     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
117     __atomic_store_8 ((atomic), (gsize) (newval), __ATOMIC_SEQ_CST);         \
118   }))
119
120 #else /* GLIB_SIZEOF_VOID_P == 8 */
121
122 /* Assume that if GLIB_SIZEOF_VOID_P is not 8, it must be 4 */
123 G_STATIC_ASSERT (sizeof (gpointer) == 4);
124
125 #define g_atomic_pointer_get(atomic) \
126   (G_GNUC_EXTENSION ({                                                       \
127     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
128     (gpointer) __atomic_load_4 ((atomic), __ATOMIC_SEQ_CST);                 \
129   }))
130 #define g_atomic_pointer_set(atomic, newval) \
131   (G_GNUC_EXTENSION ({                                                       \
132     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
133     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
134     __atomic_store_4 ((atomic), (gsize) (newval), __ATOMIC_SEQ_CST);         \
135   }))
136
137 #endif /* GLIB_SIZEOF_VOID_P == 8 */
138
139 #else /* defined(__ATOMIC_SEQ_CST) */
140
141 #define g_atomic_int_get(atomic) \
142   (G_GNUC_EXTENSION ({                                                       \
143     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
144     (void) (0 ? *(atomic) ^ *(atomic) : 0);                                  \
145     __sync_synchronize ();                                                   \
146     (gint) *(atomic);                                                        \
147   }))
148 #define g_atomic_int_set(atomic, newval) \
149   (G_GNUC_EXTENSION ({                                                       \
150     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
151     (void) (0 ? *(atomic) ^ (newval) : 0);                                   \
152     *(atomic) = (newval);                                                    \
153     __sync_synchronize ();                                                   \
154   }))
155 #define g_atomic_pointer_get(atomic) \
156   (G_GNUC_EXTENSION ({                                                       \
157     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
158     __sync_synchronize ();                                                   \
159     (gpointer) *(atomic);                                                    \
160   }))
161 #define g_atomic_pointer_set(atomic, newval) \
162   (G_GNUC_EXTENSION ({                                                       \
163     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
164     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
165     *(atomic) = (__typeof__ (*(atomic))) (gsize) (newval);                   \
166     __sync_synchronize ();                                                   \
167   }))
168
169 #endif /* !defined(__ATOMIC_SEQ_CST) */
170
171 #define g_atomic_int_inc(atomic) \
172   (G_GNUC_EXTENSION ({                                                       \
173     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
174     (void) (0 ? *(atomic) ^ *(atomic) : 0);                                  \
175     (void) __sync_fetch_and_add ((atomic), 1);                               \
176   }))
177 #define g_atomic_int_dec_and_test(atomic) \
178   (G_GNUC_EXTENSION ({                                                       \
179     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
180     (void) (0 ? *(atomic) ^ *(atomic) : 0);                                  \
181     __sync_fetch_and_sub ((atomic), 1) == 1;                                 \
182   }))
183 #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
184   (G_GNUC_EXTENSION ({                                                       \
185     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
186     (void) (0 ? *(atomic) ^ (newval) ^ (oldval) : 0);                        \
187     (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval));  \
188   }))
189 #define g_atomic_int_add(atomic, val) \
190   (G_GNUC_EXTENSION ({                                                       \
191     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
192     (void) (0 ? *(atomic) ^ (val) : 0);                                      \
193     (gint) __sync_fetch_and_add ((atomic), (val));                           \
194   }))
195 #define g_atomic_int_and(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_and ((atomic), (val));                          \
200   }))
201 #define g_atomic_int_or(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_or ((atomic), (val));                           \
206   }))
207 #define g_atomic_int_xor(atomic, val) \
208   (G_GNUC_EXTENSION ({                                                       \
209     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
210     (void) (0 ? *(atomic) ^ (val) : 0);                                      \
211     (guint) __sync_fetch_and_xor ((atomic), (val));                          \
212   }))
213
214 #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
215   (G_GNUC_EXTENSION ({                                                       \
216     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
217     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
218     (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval));  \
219   }))
220 #define g_atomic_pointer_add(atomic, val) \
221   (G_GNUC_EXTENSION ({                                                       \
222     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
223     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
224     (void) (0 ? (val) ^ (val) : 0);                                          \
225     (gssize) __sync_fetch_and_add ((atomic), (val));                         \
226   }))
227 #define g_atomic_pointer_and(atomic, val) \
228   (G_GNUC_EXTENSION ({                                                       \
229     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
230     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
231     (void) (0 ? (val) ^ (val) : 0);                                          \
232     (gsize) __sync_fetch_and_and ((atomic), (val));                          \
233   }))
234 #define g_atomic_pointer_or(atomic, val) \
235   (G_GNUC_EXTENSION ({                                                       \
236     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
237     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
238     (void) (0 ? (val) ^ (val) : 0);                                          \
239     (gsize) __sync_fetch_and_or ((atomic), (val));                           \
240   }))
241 #define g_atomic_pointer_xor(atomic, val) \
242   (G_GNUC_EXTENSION ({                                                       \
243     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
244     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
245     (void) (0 ? (val) ^ (val) : 0);                                          \
246     (gsize) __sync_fetch_and_xor ((atomic), (val));                          \
247   }))
248
249 #else /* defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) */
250
251 #define g_atomic_int_get(atomic) \
252   (g_atomic_int_get ((gint *) (atomic)))
253 #define g_atomic_int_set(atomic, newval) \
254   (g_atomic_int_set ((gint *) (atomic), (gint) (newval)))
255 #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
256   (g_atomic_int_compare_and_exchange ((gint *) (atomic), (oldval), (newval)))
257 #define g_atomic_int_add(atomic, val) \
258   (g_atomic_int_add ((gint *) (atomic), (val)))
259 #define g_atomic_int_and(atomic, val) \
260   (g_atomic_int_and ((guint *) (atomic), (val)))
261 #define g_atomic_int_or(atomic, val) \
262   (g_atomic_int_or ((guint *) (atomic), (val)))
263 #define g_atomic_int_xor(atomic, val) \
264   (g_atomic_int_xor ((guint *) (atomic), (val)))
265 #define g_atomic_int_inc(atomic) \
266   (g_atomic_int_inc ((gint *) (atomic)))
267 #define g_atomic_int_dec_and_test(atomic) \
268   (g_atomic_int_dec_and_test ((gint *) (atomic)))
269
270 #define g_atomic_pointer_get(atomic) \
271   (g_atomic_pointer_get (atomic))
272 #define g_atomic_pointer_set(atomic, newval) \
273   (g_atomic_pointer_set ((atomic), (gpointer) (newval)))
274 #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
275   (g_atomic_pointer_compare_and_exchange ((atomic), (gpointer) (oldval), (gpointer) (newval)))
276 #define g_atomic_pointer_add(atomic, val) \
277   (g_atomic_pointer_add ((atomic), (gssize) (val)))
278 #define g_atomic_pointer_and(atomic, val) \
279   (g_atomic_pointer_and ((atomic), (gsize) (val)))
280 #define g_atomic_pointer_or(atomic, val) \
281   (g_atomic_pointer_or ((atomic), (gsize) (val)))
282 #define g_atomic_pointer_xor(atomic, val) \
283   (g_atomic_pointer_xor ((atomic), (gsize) (val)))
284
285 #endif /* defined(__GNUC__) && defined(G_ATOMIC_OP_USE_GCC_BUILTINS) */
286
287 #endif /* __G_ATOMIC_H__ */