Set up test environment properly
[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, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17  * USA.
18  *
19  * Author: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #ifndef __G_ATOMIC_H__
23 #define __G_ATOMIC_H__
24
25 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
26 #error "Only <glib.h> can be included directly."
27 #endif
28
29 #include <glib/gtypes.h>
30
31 G_BEGIN_DECLS
32
33 GLIB_AVAILABLE_IN_ALL
34 gint                    g_atomic_int_get                      (const volatile gint *atomic);
35 GLIB_AVAILABLE_IN_ALL
36 void                    g_atomic_int_set                      (volatile gint  *atomic,
37                                                                gint            newval);
38 GLIB_AVAILABLE_IN_ALL
39 void                    g_atomic_int_inc                      (volatile gint  *atomic);
40 GLIB_AVAILABLE_IN_ALL
41 gboolean                g_atomic_int_dec_and_test             (volatile gint  *atomic);
42 GLIB_AVAILABLE_IN_ALL
43 gboolean                g_atomic_int_compare_and_exchange     (volatile gint  *atomic,
44                                                                gint            oldval,
45                                                                gint            newval);
46 GLIB_AVAILABLE_IN_ALL
47 gint                    g_atomic_int_add                      (volatile gint  *atomic,
48                                                                gint            val);
49 GLIB_AVAILABLE_IN_2_30
50 guint                   g_atomic_int_and                      (volatile guint *atomic,
51                                                                guint           val);
52 GLIB_AVAILABLE_IN_2_30
53 guint                   g_atomic_int_or                       (volatile guint *atomic,
54                                                                guint           val);
55 GLIB_AVAILABLE_IN_ALL
56 guint                   g_atomic_int_xor                      (volatile guint *atomic,
57                                                                guint           val);
58
59 GLIB_AVAILABLE_IN_ALL
60 gpointer                g_atomic_pointer_get                  (const volatile void *atomic);
61 GLIB_AVAILABLE_IN_ALL
62 void                    g_atomic_pointer_set                  (volatile void  *atomic,
63                                                                gpointer        newval);
64 GLIB_AVAILABLE_IN_ALL
65 gboolean                g_atomic_pointer_compare_and_exchange (volatile void  *atomic,
66                                                                gpointer        oldval,
67                                                                gpointer        newval);
68 GLIB_AVAILABLE_IN_ALL
69 gssize                  g_atomic_pointer_add                  (volatile void  *atomic,
70                                                                gssize          val);
71 GLIB_AVAILABLE_IN_2_30
72 gsize                   g_atomic_pointer_and                  (volatile void  *atomic,
73                                                                gsize           val);
74 GLIB_AVAILABLE_IN_2_30
75 gsize                   g_atomic_pointer_or                   (volatile void  *atomic,
76                                                                gsize           val);
77 GLIB_AVAILABLE_IN_ALL
78 gsize                   g_atomic_pointer_xor                  (volatile void  *atomic,
79                                                                gsize           val);
80
81 GLIB_DEPRECATED_IN_2_30_FOR(g_atomic_add)
82 gint                    g_atomic_int_exchange_and_add         (volatile gint  *atomic,
83                                                                gint            val);
84
85 G_END_DECLS
86
87 #if defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
88
89 #define g_atomic_int_get(atomic) \
90   (G_GNUC_EXTENSION ({                                                          \
91     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
92     (void) (0 ? *(atomic) ^ *(atomic) : 0);                                  \
93     __sync_synchronize ();                                                   \
94     (gint) *(atomic);                                                        \
95   }))
96 #define g_atomic_int_set(atomic, newval) \
97   (G_GNUC_EXTENSION ({                                                          \
98     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
99     (void) (0 ? *(atomic) ^ (newval) : 0);                                   \
100     *(atomic) = (newval);                                                    \
101     __sync_synchronize ();                                                   \
102   }))
103 #define g_atomic_int_inc(atomic) \
104   (G_GNUC_EXTENSION ({                                                          \
105     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
106     (void) (0 ? *(atomic) ^ *(atomic) : 0);                                  \
107     (void) __sync_fetch_and_add ((atomic), 1);                               \
108   }))
109 #define g_atomic_int_dec_and_test(atomic) \
110   (G_GNUC_EXTENSION ({                                                          \
111     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
112     (void) (0 ? *(atomic) ^ *(atomic) : 0);                                  \
113     __sync_fetch_and_sub ((atomic), 1) == 1;                                 \
114   }))
115 #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
116   (G_GNUC_EXTENSION ({                                                          \
117     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
118     (void) (0 ? *(atomic) ^ (newval) ^ (oldval) : 0);                        \
119     (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval));  \
120   }))
121 #define g_atomic_int_add(atomic, val) \
122   (G_GNUC_EXTENSION ({                                                          \
123     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
124     (void) (0 ? *(atomic) ^ (val) : 0);                                      \
125     (gint) __sync_fetch_and_add ((atomic), (val));                           \
126   }))
127 #define g_atomic_int_and(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_and ((atomic), (val));                          \
132   }))
133 #define g_atomic_int_or(atomic, val) \
134   (G_GNUC_EXTENSION ({                                                          \
135     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
136     (void) (0 ? *(atomic) ^ (val) : 0);                                      \
137     (guint) __sync_fetch_and_or ((atomic), (val));                           \
138   }))
139 #define g_atomic_int_xor(atomic, val) \
140   (G_GNUC_EXTENSION ({                                                          \
141     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
142     (void) (0 ? *(atomic) ^ (val) : 0);                                      \
143     (guint) __sync_fetch_and_xor ((atomic), (val));                          \
144   }))
145
146 #define g_atomic_pointer_get(atomic) \
147   (G_GNUC_EXTENSION ({                                                          \
148     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
149     __sync_synchronize ();                                                   \
150     (gpointer) *(atomic);                                                    \
151   }))
152 #define g_atomic_pointer_set(atomic, newval) \
153   (G_GNUC_EXTENSION ({                                                          \
154     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
155     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
156     *(atomic) = (__typeof__ (*(atomic))) (gsize) (newval);                   \
157     __sync_synchronize ();                                                   \
158   }))
159 #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
160   (G_GNUC_EXTENSION ({                                                          \
161     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
162     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
163     (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval));  \
164   }))
165 #define g_atomic_pointer_add(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     (gssize) __sync_fetch_and_add ((atomic), (val));                         \
171   }))
172 #define g_atomic_pointer_and(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_and ((atomic), (val));                          \
178   }))
179 #define g_atomic_pointer_or(atomic, val) \
180   (G_GNUC_EXTENSION ({                                                          \
181     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
182     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
183     (void) (0 ? (val) ^ (val) : 0);                                          \
184     (gsize) __sync_fetch_and_or ((atomic), (val));                           \
185   }))
186 #define g_atomic_pointer_xor(atomic, val) \
187   (G_GNUC_EXTENSION ({                                                          \
188     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
189     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
190     (void) (0 ? (val) ^ (val) : 0);                                          \
191     (gsize) __sync_fetch_and_xor ((atomic), (val));                          \
192   }))
193
194 #else /* defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) */
195
196 #define g_atomic_int_get(atomic) \
197   (g_atomic_int_get ((gint *) (atomic)))
198 #define g_atomic_int_set(atomic, newval) \
199   (g_atomic_int_set ((gint *) (atomic), (gint) (newval)))
200 #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
201   (g_atomic_int_compare_and_exchange ((gint *) (atomic), (oldval), (newval)))
202 #define g_atomic_int_add(atomic, val) \
203   (g_atomic_int_add ((gint *) (atomic), (val)))
204 #define g_atomic_int_and(atomic, val) \
205   (g_atomic_int_and ((guint *) (atomic), (val)))
206 #define g_atomic_int_or(atomic, val) \
207   (g_atomic_int_or ((guint *) (atomic), (val)))
208 #define g_atomic_int_xor(atomic, val) \
209   (g_atomic_int_xor ((guint *) (atomic), (val)))
210 #define g_atomic_int_inc(atomic) \
211   (g_atomic_int_inc ((gint *) (atomic)))
212 #define g_atomic_int_dec_and_test(atomic) \
213   (g_atomic_int_dec_and_test ((gint *) (atomic)))
214
215 #define g_atomic_pointer_get(atomic) \
216   (g_atomic_pointer_get (atomic))
217 #define g_atomic_pointer_set(atomic, newval) \
218   (g_atomic_pointer_set ((atomic), (gpointer) (newval)))
219 #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
220   (g_atomic_pointer_compare_and_exchange ((atomic), (gpointer) (oldval), (gpointer) (newval)))
221 #define g_atomic_pointer_add(atomic, val) \
222   (g_atomic_pointer_add ((atomic), (gssize) (val)))
223 #define g_atomic_pointer_and(atomic, val) \
224   (g_atomic_pointer_and ((atomic), (gsize) (val)))
225 #define g_atomic_pointer_or(atomic, val) \
226   (g_atomic_pointer_or ((atomic), (gsize) (val)))
227 #define g_atomic_pointer_xor(atomic, val) \
228   (g_atomic_pointer_xor ((atomic), (gsize) (val)))
229
230 #endif /* defined(__GNUC__) && defined(G_ATOMIC_OP_USE_GCC_BUILTINS) */
231
232 #endif /* __G_ATOMIC_H__ */