1 /*
2  * Distributed under the Boost Software License, Version 1.0.
3  *    (See accompanying file LICENSE_1_0.txt or copy at
4  *          http://www.boost.org/LICENSE_1_0.txt)
5  */
6 module glib.gatomic;
7 
8 import glib.gtypes;
9 
10 extern(C) {
11 
12     gint g_atomic_int_get (const(gint)*atomic);
13 
14     void g_atomic_int_set (gint *atomic, gint newval);
15 
16     void g_atomic_int_inc (gint *atomic);
17 
18     gboolean g_atomic_int_dec_and_test (gint *atomic);
19 
20     gboolean g_atomic_int_compare_and_exchange (gint *atomic,
21                                                 gint oldval, gint newval);
22 
23     gint g_atomic_int_add (gint  *atomic, gint val);
24 
25     guint g_atomic_int_and (guint *atomic, guint val);
26 
27     guint g_atomic_int_or (guint *atomic, guint val);
28 
29     guint g_atomic_int_xor (guint *atomic, guint val);
30 
31 
32     gpointer g_atomic_pointer_get (const(void)*atomic);
33 
34     void g_atomic_pointer_set (void  *atomic, gpointer newval);
35 
36     gboolean g_atomic_pointer_compare_and_exchange (void *atomic,
37                                                     gpointer oldval,
38                                                     gpointer newval);
39 
40     gssize g_atomic_pointer_add (void *atomic, gssize val);
41 
42     gsize g_atomic_pointer_and (void *atomic, gsize val);
43 
44     gsize g_atomic_pointer_or (void *atomic, gsize val);
45 
46     gsize g_atomic_pointer_xor (void *atomic, gsize val);
47 
48     deprecated gint g_atomic_int_exchange_and_add (gint *atomic, gint val);
49 
50 }
51 
52 // #if defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
53 //
54 // /* We prefer the new C11-style atomic extension of GCC if available */
55 // #if defined(__ATOMIC_SEQ_CST) && !defined(__clang__)
56 //
57 // #define g_atomic_int_get(atomic) \
58 //   (G_GNUC_EXTENSION ({                                                       \
59 //     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
60 //     (void) (0 ? *(atomic) ^ *(atomic) : 0);                                  \
61 //     (gint) __atomic_load_4 ((atomic), __ATOMIC_SEQ_CST);                     \
62 //   }))
63 // #define g_atomic_int_set(atomic, newval) \
64 //   (G_GNUC_EXTENSION ({                                                       \
65 //     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
66 //     (void) (0 ? *(atomic) ^ (newval) : 0);                                   \
67 //     __atomic_store_4 ((atomic), (newval), __ATOMIC_SEQ_CST);                 \
68 //   }))
69 //
70 // #if GLIB_SIZEOF_VOID_P == 8
71 //
72 // #define g_atomic_pointer_get(atomic) \
73 //   (G_GNUC_EXTENSION ({                                                       \
74 //     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
75 //     (gpointer) __atomic_load_8 ((atomic), __ATOMIC_SEQ_CST);                 \
76 //   }))
77 // #define g_atomic_pointer_set(atomic, newval) \
78 //   (G_GNUC_EXTENSION ({                                                       \
79 //     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
80 //     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
81 //     __atomic_store_8 ((atomic), (gsize) (newval), __ATOMIC_SEQ_CST);         \
82 //   }))
83 //
84 // #else /* GLIB_SIZEOF_VOID_P == 8 */
85 //
86 // #define g_atomic_pointer_get(atomic) \
87 //   (G_GNUC_EXTENSION ({                                                       \
88 //     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
89 //     (gpointer) __atomic_load_4 ((atomic), __ATOMIC_SEQ_CST);                 \
90 //   }))
91 // #define g_atomic_pointer_set(atomic, newval) \
92 //   (G_GNUC_EXTENSION ({                                                       \
93 //     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
94 //     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
95 //     __atomic_store_4 ((atomic), (gsize) (newval), __ATOMIC_SEQ_CST);         \
96 //   }))
97 //
98 // #endif /* GLIB_SIZEOF_VOID_P == 8 */
99 //
100 // #else /* defined(__ATOMIC_SEQ_CST) */
101 //
102 // #define g_atomic_int_get(atomic) \
103 //   (G_GNUC_EXTENSION ({                                                       \
104 //     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
105 //     (void) (0 ? *(atomic) ^ *(atomic) : 0);                                  \
106 //     __sync_synchronize ();                                                   \
107 //     (gint) *(atomic);                                                        \
108 //   }))
109 // #define g_atomic_int_set(atomic, newval) \
110 //   (G_GNUC_EXTENSION ({                                                       \
111 //     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
112 //     (void) (0 ? *(atomic) ^ (newval) : 0);                                   \
113 //     *(atomic) = (newval);                                                    \
114 //     __sync_synchronize ();                                                   \
115 //   }))
116 // #define g_atomic_pointer_get(atomic) \
117 //   (G_GNUC_EXTENSION ({                                                       \
118 //     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
119 //     __sync_synchronize ();                                                   \
120 //     (gpointer) *(atomic);                                                    \
121 //   }))
122 // #define g_atomic_pointer_set(atomic, newval) \
123 //   (G_GNUC_EXTENSION ({                                                       \
124 //     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
125 //     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
126 //     *(atomic) = (__typeof__ (*(atomic))) (gsize) (newval);                   \
127 //     __sync_synchronize ();                                                   \
128 //   }))
129 //
130 // #endif /* !defined(__ATOMIC_SEQ_CST) */
131 //
132 // #define g_atomic_int_inc(atomic) \
133 //   (G_GNUC_EXTENSION ({                                                       \
134 //     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
135 //     (void) (0 ? *(atomic) ^ *(atomic) : 0);                                  \
136 //     (void) __sync_fetch_and_add ((atomic), 1);                               \
137 //   }))
138 // #define g_atomic_int_dec_and_test(atomic) \
139 //   (G_GNUC_EXTENSION ({                                                       \
140 //     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
141 //     (void) (0 ? *(atomic) ^ *(atomic) : 0);                                  \
142 //     __sync_fetch_and_sub ((atomic), 1) == 1;                                 \
143 //   }))
144 // #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
145 //   (G_GNUC_EXTENSION ({                                                       \
146 //     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
147 //     (void) (0 ? *(atomic) ^ (newval) ^ (oldval) : 0);                        \
148 //     (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval));  \
149 //   }))
150 // #define g_atomic_int_add(atomic, val) \
151 //   (G_GNUC_EXTENSION ({                                                       \
152 //     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
153 //     (void) (0 ? *(atomic) ^ (val) : 0);                                      \
154 //     (gint) __sync_fetch_and_add ((atomic), (val));                           \
155 //   }))
156 // #define g_atomic_int_and(atomic, val) \
157 //   (G_GNUC_EXTENSION ({                                                       \
158 //     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
159 //     (void) (0 ? *(atomic) ^ (val) : 0);                                      \
160 //     (guint) __sync_fetch_and_and ((atomic), (val));                          \
161 //   }))
162 // #define g_atomic_int_or(atomic, val) \
163 //   (G_GNUC_EXTENSION ({                                                       \
164 //     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
165 //     (void) (0 ? *(atomic) ^ (val) : 0);                                      \
166 //     (guint) __sync_fetch_and_or ((atomic), (val));                           \
167 //   }))
168 // #define g_atomic_int_xor(atomic, val) \
169 //   (G_GNUC_EXTENSION ({                                                       \
170 //     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
171 //     (void) (0 ? *(atomic) ^ (val) : 0);                                      \
172 //     (guint) __sync_fetch_and_xor ((atomic), (val));                          \
173 //   }))
174 //
175 // #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
176 //   (G_GNUC_EXTENSION ({                                                       \
177 //     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
178 //     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
179 //     (gboolean) __sync_bool_compare_and_swap ((atomic), (oldval), (newval));  \
180 //   }))
181 // #define g_atomic_pointer_add(atomic, val) \
182 //   (G_GNUC_EXTENSION ({                                                       \
183 //     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
184 //     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
185 //     (void) (0 ? (val) ^ (val) : 0);                                          \
186 //     (gssize) __sync_fetch_and_add ((atomic), (val));                         \
187 //   }))
188 // #define g_atomic_pointer_and(atomic, val) \
189 //   (G_GNUC_EXTENSION ({                                                       \
190 //     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
191 //     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
192 //     (void) (0 ? (val) ^ (val) : 0);                                          \
193 //     (gsize) __sync_fetch_and_and ((atomic), (val));                          \
194 //   }))
195 // #define g_atomic_pointer_or(atomic, val) \
196 //   (G_GNUC_EXTENSION ({                                                       \
197 //     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
198 //     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
199 //     (void) (0 ? (val) ^ (val) : 0);                                          \
200 //     (gsize) __sync_fetch_and_or ((atomic), (val));                           \
201 //   }))
202 // #define g_atomic_pointer_xor(atomic, val) \
203 //   (G_GNUC_EXTENSION ({                                                       \
204 //     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \
205 //     (void) (0 ? (gpointer) *(atomic) : 0);                                   \
206 //     (void) (0 ? (val) ^ (val) : 0);                                          \
207 //     (gsize) __sync_fetch_and_xor ((atomic), (val));                          \
208 //   }))
209 //
210 // #else /* defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) */
211 //
212 // #define g_atomic_int_get(atomic) \
213 //   (g_atomic_int_get ((gint *) (atomic)))
214 // #define g_atomic_int_set(atomic, newval) \
215 //   (g_atomic_int_set ((gint *) (atomic), (gint) (newval)))
216 // #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
217 //   (g_atomic_int_compare_and_exchange ((gint *) (atomic), (oldval), (newval)))
218 // #define g_atomic_int_add(atomic, val) \
219 //   (g_atomic_int_add ((gint *) (atomic), (val)))
220 // #define g_atomic_int_and(atomic, val) \
221 //   (g_atomic_int_and ((guint *) (atomic), (val)))
222 // #define g_atomic_int_or(atomic, val) \
223 //   (g_atomic_int_or ((guint *) (atomic), (val)))
224 // #define g_atomic_int_xor(atomic, val) \
225 //   (g_atomic_int_xor ((guint *) (atomic), (val)))
226 // #define g_atomic_int_inc(atomic) \
227 //   (g_atomic_int_inc ((gint *) (atomic)))
228 // #define g_atomic_int_dec_and_test(atomic) \
229 //   (g_atomic_int_dec_and_test ((gint *) (atomic)))
230 //
231 // #define g_atomic_pointer_get(atomic) \
232 //   (g_atomic_pointer_get (atomic))
233 // #define g_atomic_pointer_set(atomic, newval) \
234 //   (g_atomic_pointer_set ((atomic), (gpointer) (newval)))
235 // #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
236 //   (g_atomic_pointer_compare_and_exchange ((atomic), (gpointer) (oldval), (gpointer) (newval)))
237 // #define g_atomic_pointer_add(atomic, val) \
238 //   (g_atomic_pointer_add ((atomic), (gssize) (val)))
239 // #define g_atomic_pointer_and(atomic, val) \
240 //   (g_atomic_pointer_and ((atomic), (gsize) (val)))
241 // #define g_atomic_pointer_or(atomic, val) \
242 //   (g_atomic_pointer_or ((atomic), (gsize) (val)))
243 // #define g_atomic_pointer_xor(atomic, val) \
244 //   (g_atomic_pointer_xor ((atomic), (gsize) (val)))
245 //
246 // #endif /* defined(__GNUC__) && defined(G_ATOMIC_OP_USE_GCC_BUILTINS) */
247 //