Annotate API introduced for 2.30 with GLIB_AVAILABLE_IN_2_30
[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 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
23 #error "Only <glib.h> can be included directly."
24 #endif
25
26 #ifndef __G_ATOMIC_H__
27 #define __G_ATOMIC_H__
28
29 #include <glib/gtypes.h>
30
31 G_BEGIN_DECLS
32
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 GLIB_AVAILABLE_IN_2_30
44 guint                   g_atomic_int_and                      (volatile guint *atomic,
45                                                                guint           val);
46 GLIB_AVAILABLE_IN_2_30
47 guint                   g_atomic_int_or                       (volatile guint *atomic,
48                                                                guint           val);
49 guint                   g_atomic_int_xor                      (volatile guint *atomic,
50                                                                guint           val);
51
52 gpointer                g_atomic_pointer_get                  (volatile void  *atomic);
53 void                    g_atomic_pointer_set                  (volatile void  *atomic,
54                                                                gpointer        newval);
55 gboolean                g_atomic_pointer_compare_and_exchange (volatile void  *atomic,
56                                                                gpointer        oldval,
57                                                                gpointer        newval);
58 gssize                  g_atomic_pointer_add                  (volatile void  *atomic,
59                                                                gssize          val);
60 GLIB_AVAILABLE_IN_2_30
61 gsize                   g_atomic_pointer_and                  (volatile void  *atomic,
62                                                                gsize           val);
63 GLIB_AVAILABLE_IN_2_30
64 gsize                   g_atomic_pointer_or                   (volatile void  *atomic,
65                                                                gsize           val);
66 gsize                   g_atomic_pointer_xor                  (volatile void  *atomic,
67                                                                gsize           val);
68
69 GLIB_DEPRECATED_IN_2_30_FOR(g_atomic_add)
70 gint                    g_atomic_int_exchange_and_add         (volatile gint  *atomic,
71                                                                gint            val);
72
73 G_END_DECLS
74
75 #if defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
76
77 #define g_atomic_int_get(atomic) \
78   (G_GNUC_EXTENSION ({                                                          \
79     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
80     (void) (0 ? *(atomic) ^ *(atomic) : 0);                                  \
81     __sync_synchronize ();                                                   \
82     (gint) *(atomic);                                                        \
83   }))
84 #define g_atomic_int_set(atomic, newval) \
85   (G_GNUC_EXTENSION ({                                                          \
86     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
87     (void) (0 ? *(atomic) ^ (newval) : 0);                                   \
88     *(atomic) = (newval);                                                    \
89     __sync_synchronize ();                                                   \
90   }))
91 #define g_atomic_int_inc(atomic) \
92   (G_GNUC_EXTENSION ({                                                          \
93     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
94     (void) (0 ? *(atomic) ^ *(atomic) : 0);                                  \
95     (void) __sync_fetch_and_add ((atomic), 1);                               \
96   }))
97 #define g_atomic_int_dec_and_test(atomic) \
98   (G_GNUC_EXTENSION ({                                                          \
99     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
100     (void) (0 ? *(atomic) ^ *(atomic) : 0);                                  \
101     __sync_fetch_and_sub ((atomic), 1) == 1;                                 \
102   }))
103 #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
104   (G_GNUC_EXTENSION ({                                                          \
105     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
106     (void) (0 ? *(atomic) ^ (newval) ^ (oldval) : 0);                        \
107     (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval));  \
108   }))
109 #define g_atomic_int_add(atomic, val) \
110   (G_GNUC_EXTENSION ({                                                          \
111     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
112     (void) (0 ? *(atomic) ^ (val) : 0);                                      \
113     (gint) __sync_fetch_and_add ((atomic), (val));                           \
114   }))
115 #define g_atomic_int_and(atomic, val) \
116   (G_GNUC_EXTENSION ({                                                          \
117     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
118     (void) (0 ? *(atomic) ^ (val) : 0);                                      \
119     (guint) __sync_fetch_and_and ((atomic), (val));                          \
120   }))
121 #define g_atomic_int_or(atomic, val) \
122   (G_GNUC_EXTENSION ({                                                          \
123     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
124     (void) (0 ? *(atomic) ^ (val) : 0);                                      \
125     (guint) __sync_fetch_and_or ((atomic), (val));                           \
126   }))
127 #define g_atomic_int_xor(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_xor ((atomic), (val));                          \
132   }))
133
134 #define g_atomic_pointer_get(atomic) \
135   (G_GNUC_EXTENSION ({                                                          \
136     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
137     __sync_synchronize ();                                                   \
138     (gpointer) *(atomic);                                                    \
139   }))
140 #define g_atomic_pointer_set(atomic, newval) \
141   (G_GNUC_EXTENSION ({                                                          \
142     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
143     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
144     *(atomic) = (__typeof__ (*(atomic))) (gsize) (newval);                   \
145     __sync_synchronize ();                                                   \
146   }))
147 #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
148   (G_GNUC_EXTENSION ({                                                          \
149     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
150     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
151     (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval));  \
152   }))
153 #define g_atomic_pointer_add(atomic, val) \
154   (G_GNUC_EXTENSION ({                                                          \
155     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
156     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
157     (void) (0 ? (val) ^ (val) : 0);                                          \
158     (gssize) __sync_fetch_and_add ((atomic), (val));                         \
159   }))
160 #define g_atomic_pointer_and(atomic, val) \
161   (G_GNUC_EXTENSION ({                                                          \
162     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
163     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
164     (void) (0 ? (val) ^ (val) : 0);                                          \
165     (gsize) __sync_fetch_and_and ((atomic), (val));                          \
166   }))
167 #define g_atomic_pointer_or(atomic, val) \
168   (G_GNUC_EXTENSION ({                                                          \
169     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
170     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
171     (void) (0 ? (val) ^ (val) : 0);                                          \
172     (gsize) __sync_fetch_and_or ((atomic), (val));                           \
173   }))
174 #define g_atomic_pointer_xor(atomic, val) \
175   (G_GNUC_EXTENSION ({                                                          \
176     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
177     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
178     (void) (0 ? (val) ^ (val) : 0);                                          \
179     (gsize) __sync_fetch_and_xor ((atomic), (val));                          \
180   }))
181
182 #else /* defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) */
183
184 #define g_atomic_int_get(atomic) \
185   (g_atomic_int_get ((gint *) (atomic)))
186 #define g_atomic_int_set(atomic, newval) \
187   (g_atomic_int_set ((gint *) (atomic), (gint) (newval)))
188 #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
189   (g_atomic_int_compare_and_exchange ((gint *) (atomic), (oldval), (newval)))
190 #define g_atomic_int_add(atomic, val) \
191   (g_atomic_int_add ((gint *) (atomic), (val)))
192 #define g_atomic_int_and(atomic, val) \
193   (g_atomic_int_and ((guint *) (atomic), (val)))
194 #define g_atomic_int_or(atomic, val) \
195   (g_atomic_int_or ((guint *) (atomic), (val)))
196 #define g_atomic_int_xor(atomic, val) \
197   (g_atomic_int_xor ((guint *) (atomic), (val)))
198 #define g_atomic_int_inc(atomic) \
199   (g_atomic_int_inc ((gint *) (atomic)))
200 #define g_atomic_int_dec_and_test(atomic) \
201   (g_atomic_int_dec_and_test ((gint *) (atomic)))
202
203 #define g_atomic_pointer_get(atomic) \
204   (g_atomic_pointer_get (atomic))
205 #define g_atomic_pointer_set(atomic, newval) \
206   (g_atomic_pointer_set ((atomic), (gpointer) (newval)))
207 #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
208   (g_atomic_pointer_compare_and_exchange ((atomic), (gpointer) (oldval), (gpointer) (newval)))
209 #define g_atomic_pointer_add(atomic, val) \
210   (g_atomic_pointer_add ((atomic), (gssize) (val)))
211 #define g_atomic_pointer_and(atomic, val) \
212   (g_atomic_pointer_and ((atomic), (gsize) (val)))
213 #define g_atomic_pointer_or(atomic, val) \
214   (g_atomic_pointer_or ((atomic), (gsize) (val)))
215 #define g_atomic_pointer_xor(atomic, val) \
216   (g_atomic_pointer_xor ((atomic), (gsize) (val)))
217
218 #endif /* defined(__GNUC__) && defined(G_ATOMIC_OP_USE_GCC_BUILTINS) */
219
220 #endif /* __G_ATOMIC_H__ */