376ccc185050fdb5c1e73ed1bfb914234c51fb83
[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 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);
49
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);
64
65 #ifndef G_DISABLE_DEPRECATED
66 GLIB_DEPRECATED_FOR(g_atomic_add)
67 gint                    g_atomic_int_exchange_and_add         (volatile gint  *atomic,
68                                                                gint            val);
69 #endif
70
71 G_END_DECLS
72
73 #if defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
74
75 #define g_atomic_int_get(atomic) \
76   (G_GNUC_EXTENSION ({                                                          \
77     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
78     (void) (0 ? *(atomic) ^ *(atomic) : 0);                                  \
79     __sync_synchronize ();                                                   \
80     (gint) *(atomic);                                                        \
81   }))
82 #define g_atomic_int_set(atomic, newval) \
83   (G_GNUC_EXTENSION ({                                                          \
84     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
85     (void) (0 ? *(atomic) ^ (newval) : 0);                                   \
86     *(atomic) = (newval);                                                    \
87     __sync_synchronize ();                                                   \
88   }))
89 #define g_atomic_int_inc(atomic) \
90   (G_GNUC_EXTENSION ({                                                          \
91     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
92     (void) (0 ? *(atomic) ^ *(atomic) : 0);                                  \
93     (void) __sync_fetch_and_add ((atomic), 1);                               \
94   }))
95 #define g_atomic_int_dec_and_test(atomic) \
96   (G_GNUC_EXTENSION ({                                                          \
97     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
98     (void) (0 ? *(atomic) ^ *(atomic) : 0);                                  \
99     __sync_fetch_and_sub ((atomic), 1) == 1;                                 \
100   }))
101 #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
102   (G_GNUC_EXTENSION ({                                                          \
103     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
104     (void) (0 ? *(atomic) ^ (newval) ^ (oldval) : 0);                        \
105     (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval));  \
106   }))
107 #define g_atomic_int_add(atomic, val) \
108   (G_GNUC_EXTENSION ({                                                          \
109     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
110     (void) (0 ? *(atomic) ^ (val) : 0);                                      \
111     (gint) __sync_fetch_and_add ((atomic), (val));                           \
112   }))
113 #define g_atomic_int_and(atomic, val) \
114   (G_GNUC_EXTENSION ({                                                          \
115     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
116     (void) (0 ? *(atomic) ^ (val) : 0);                                      \
117     (guint) __sync_fetch_and_and ((atomic), (val));                          \
118   }))
119 #define g_atomic_int_or(atomic, val) \
120   (G_GNUC_EXTENSION ({                                                          \
121     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
122     (void) (0 ? *(atomic) ^ (val) : 0);                                      \
123     (guint) __sync_fetch_and_or ((atomic), (val));                           \
124   }))
125 #define g_atomic_int_xor(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_xor ((atomic), (val));                          \
130   }))
131
132 #define g_atomic_pointer_get(atomic) \
133   (G_GNUC_EXTENSION ({                                                          \
134     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
135     __sync_synchronize ();                                                   \
136     (gpointer) *(atomic);                                                    \
137   }))
138 #define g_atomic_pointer_set(atomic, newval) \
139   (G_GNUC_EXTENSION ({                                                          \
140     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
141     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
142     *(atomic) = (__typeof__ (*(atomic))) (gsize) (newval);                   \
143     __sync_synchronize ();                                                   \
144   }))
145 #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
146   (G_GNUC_EXTENSION ({                                                          \
147     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
148     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
149     (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval));  \
150   }))
151 #define g_atomic_pointer_add(atomic, val) \
152   (G_GNUC_EXTENSION ({                                                          \
153     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
154     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
155     (void) (0 ? (val) ^ (val) : 0);                                          \
156     (gssize) __sync_fetch_and_add ((atomic), (val));                         \
157   }))
158 #define g_atomic_pointer_and(atomic, val) \
159   (G_GNUC_EXTENSION ({                                                          \
160     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
161     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
162     (void) (0 ? (val) ^ (val) : 0);                                          \
163     (gsize) __sync_fetch_and_and ((atomic), (val));                          \
164   }))
165 #define g_atomic_pointer_or(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     (gsize) __sync_fetch_and_or ((atomic), (val));                           \
171   }))
172 #define g_atomic_pointer_xor(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_xor ((atomic), (val));                          \
178   }))
179
180 #else /* defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) */
181
182 #define g_atomic_int_get(atomic) \
183   (g_atomic_int_get ((gint *) (atomic)))
184 #define g_atomic_int_set(atomic, newval) \
185   (g_atomic_int_set ((gint *) (atomic), (gint) (newval)))
186 #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
187   (g_atomic_int_compare_and_exchange ((gint *) (atomic), (oldval), (newval)))
188 #define g_atomic_int_add(atomic, val) \
189   (g_atomic_int_add ((gint *) (atomic), (val)))
190 #define g_atomic_int_and(atomic, val) \
191   (g_atomic_int_and ((guint *) (atomic), (val)))
192 #define g_atomic_int_or(atomic, val) \
193   (g_atomic_int_or ((guint *) (atomic), (val)))
194 #define g_atomic_int_xor(atomic, val) \
195   (g_atomic_int_xor ((guint *) (atomic), (val)))
196 #define g_atomic_int_inc(atomic) \
197   (g_atomic_int_inc ((gint *) (atomic)))
198 #define g_atomic_int_dec_and_test(atomic) \
199   (g_atomic_int_dec_and_test ((gint *) (atomic)))
200
201 #define g_atomic_pointer_get(atomic) \
202   (g_atomic_pointer_get (atomic))
203 #define g_atomic_pointer_set(atomic, newval) \
204   (g_atomic_pointer_set ((atomic), (gpointer) (newval)))
205 #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
206   (g_atomic_pointer_compare_and_exchange ((atomic), (gpointer) (oldval), (gpointer) (newval)))
207 #define g_atomic_pointer_add(atomic, val) \
208   (g_atomic_pointer_add ((atomic), (gssize) (val)))
209 #define g_atomic_pointer_and(atomic, val) \
210   (g_atomic_pointer_and ((atomic), (gsize) (val)))
211 #define g_atomic_pointer_or(atomic, val) \
212   (g_atomic_pointer_or ((atomic), (gsize) (val)))
213 #define g_atomic_pointer_xor(atomic, val) \
214   (g_atomic_pointer_xor ((atomic), (gsize) (val)))
215
216 #endif /* defined(__GNUC__) && defined(G_ATOMIC_OP_USE_GCC_BUILTINS) */
217
218 #endif /* __G_ATOMIC_H__ */