gatomic: fix typo in deprecation attribute
[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 #define g_atomic_int_get(atomic) \
88   (G_GNUC_EXTENSION ({                                                          \
89     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
90     (void) (0 ? *(atomic) ^ *(atomic) : 0);                                  \
91     __sync_synchronize ();                                                   \
92     (gint) *(atomic);                                                        \
93   }))
94 #define g_atomic_int_set(atomic, newval) \
95   (G_GNUC_EXTENSION ({                                                          \
96     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
97     (void) (0 ? *(atomic) ^ (newval) : 0);                                   \
98     *(atomic) = (newval);                                                    \
99     __sync_synchronize ();                                                   \
100   }))
101 #define g_atomic_int_inc(atomic) \
102   (G_GNUC_EXTENSION ({                                                          \
103     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
104     (void) (0 ? *(atomic) ^ *(atomic) : 0);                                  \
105     (void) __sync_fetch_and_add ((atomic), 1);                               \
106   }))
107 #define g_atomic_int_dec_and_test(atomic) \
108   (G_GNUC_EXTENSION ({                                                          \
109     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
110     (void) (0 ? *(atomic) ^ *(atomic) : 0);                                  \
111     __sync_fetch_and_sub ((atomic), 1) == 1;                                 \
112   }))
113 #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
114   (G_GNUC_EXTENSION ({                                                          \
115     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
116     (void) (0 ? *(atomic) ^ (newval) ^ (oldval) : 0);                        \
117     (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval));  \
118   }))
119 #define g_atomic_int_add(atomic, val) \
120   (G_GNUC_EXTENSION ({                                                          \
121     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
122     (void) (0 ? *(atomic) ^ (val) : 0);                                      \
123     (gint) __sync_fetch_and_add ((atomic), (val));                           \
124   }))
125 #define g_atomic_int_and(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_and ((atomic), (val));                          \
130   }))
131 #define g_atomic_int_or(atomic, val) \
132   (G_GNUC_EXTENSION ({                                                          \
133     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
134     (void) (0 ? *(atomic) ^ (val) : 0);                                      \
135     (guint) __sync_fetch_and_or ((atomic), (val));                           \
136   }))
137 #define g_atomic_int_xor(atomic, val) \
138   (G_GNUC_EXTENSION ({                                                          \
139     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
140     (void) (0 ? *(atomic) ^ (val) : 0);                                      \
141     (guint) __sync_fetch_and_xor ((atomic), (val));                          \
142   }))
143
144 #define g_atomic_pointer_get(atomic) \
145   (G_GNUC_EXTENSION ({                                                          \
146     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
147     __sync_synchronize ();                                                   \
148     (gpointer) *(atomic);                                                    \
149   }))
150 #define g_atomic_pointer_set(atomic, newval) \
151   (G_GNUC_EXTENSION ({                                                          \
152     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
153     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
154     *(atomic) = (__typeof__ (*(atomic))) (gsize) (newval);                   \
155     __sync_synchronize ();                                                   \
156   }))
157 #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
158   (G_GNUC_EXTENSION ({                                                          \
159     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
160     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
161     (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval));  \
162   }))
163 #define g_atomic_pointer_add(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     (gssize) __sync_fetch_and_add ((atomic), (val));                         \
169   }))
170 #define g_atomic_pointer_and(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_and ((atomic), (val));                          \
176   }))
177 #define g_atomic_pointer_or(atomic, val) \
178   (G_GNUC_EXTENSION ({                                                          \
179     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
180     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
181     (void) (0 ? (val) ^ (val) : 0);                                          \
182     (gsize) __sync_fetch_and_or ((atomic), (val));                           \
183   }))
184 #define g_atomic_pointer_xor(atomic, val) \
185   (G_GNUC_EXTENSION ({                                                          \
186     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
187     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
188     (void) (0 ? (val) ^ (val) : 0);                                          \
189     (gsize) __sync_fetch_and_xor ((atomic), (val));                          \
190   }))
191
192 #else /* defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) */
193
194 #define g_atomic_int_get(atomic) \
195   (g_atomic_int_get ((gint *) (atomic)))
196 #define g_atomic_int_set(atomic, newval) \
197   (g_atomic_int_set ((gint *) (atomic), (gint) (newval)))
198 #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
199   (g_atomic_int_compare_and_exchange ((gint *) (atomic), (oldval), (newval)))
200 #define g_atomic_int_add(atomic, val) \
201   (g_atomic_int_add ((gint *) (atomic), (val)))
202 #define g_atomic_int_and(atomic, val) \
203   (g_atomic_int_and ((guint *) (atomic), (val)))
204 #define g_atomic_int_or(atomic, val) \
205   (g_atomic_int_or ((guint *) (atomic), (val)))
206 #define g_atomic_int_xor(atomic, val) \
207   (g_atomic_int_xor ((guint *) (atomic), (val)))
208 #define g_atomic_int_inc(atomic) \
209   (g_atomic_int_inc ((gint *) (atomic)))
210 #define g_atomic_int_dec_and_test(atomic) \
211   (g_atomic_int_dec_and_test ((gint *) (atomic)))
212
213 #define g_atomic_pointer_get(atomic) \
214   (g_atomic_pointer_get (atomic))
215 #define g_atomic_pointer_set(atomic, newval) \
216   (g_atomic_pointer_set ((atomic), (gpointer) (newval)))
217 #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
218   (g_atomic_pointer_compare_and_exchange ((atomic), (gpointer) (oldval), (gpointer) (newval)))
219 #define g_atomic_pointer_add(atomic, val) \
220   (g_atomic_pointer_add ((atomic), (gssize) (val)))
221 #define g_atomic_pointer_and(atomic, val) \
222   (g_atomic_pointer_and ((atomic), (gsize) (val)))
223 #define g_atomic_pointer_or(atomic, val) \
224   (g_atomic_pointer_or ((atomic), (gsize) (val)))
225 #define g_atomic_pointer_xor(atomic, val) \
226   (g_atomic_pointer_xor ((atomic), (gsize) (val)))
227
228 #endif /* defined(__GNUC__) && defined(G_ATOMIC_OP_USE_GCC_BUILTINS) */
229
230 #endif /* __G_ATOMIC_H__ */