new beta-0.90.0
[luatex.git] / source / libs / cairo / cairo-src / src / cairo-atomic-private.h
blob11b2887a147d1d04b5891318ab2bfeaf183ffbdc
1 /* cairo - a vector graphics library with display and print output
3 * Copyright © 2007 Chris Wilson
4 * Copyright © 2010 Andrea Canciani
6 * This library is free software; you can redistribute it and/or
7 * modify it either under the terms of the GNU Lesser General Public
8 * License version 2.1 as published by the Free Software Foundation
9 * (the "LGPL") or, at your option, under the terms of the Mozilla
10 * Public License Version 1.1 (the "MPL"). If you do not alter this
11 * notice, a recipient may use your version of this file under either
12 * the MPL or the LGPL.
14 * You should have received a copy of the LGPL along with this library
15 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
17 * You should have received a copy of the MPL along with this library
18 * in the file COPYING-MPL-1.1
20 * The contents of this file are subject to the Mozilla Public License
21 * Version 1.1 (the "License"); you may not use this file except in
22 * compliance with the License. You may obtain a copy of the License at
23 * http://www.mozilla.org/MPL/
25 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27 * the specific language governing rights and limitations.
29 * The Original Code is the cairo graphics library.
31 * The Initial Developer of the Original Code is University of Southern
32 * California.
34 * Contributor(s):
35 * Chris Wilson <chris@chris-wilson.co.uk>
36 * Andrea Canciani <ranma42@gmail.com>
39 #ifndef CAIRO_ATOMIC_PRIVATE_H
40 #define CAIRO_ATOMIC_PRIVATE_H
42 # include "cairo-compiler-private.h"
44 #if HAVE_CONFIG_H
45 #include "config.h"
46 #endif
48 /* The autoconf on OpenBSD 4.5 produces the malformed constant name
49 * SIZEOF_VOID__ rather than SIZEOF_VOID_P. Work around that here. */
50 #if !defined(SIZEOF_VOID_P) && defined(SIZEOF_VOID__)
51 # define SIZEOF_VOID_P SIZEOF_VOID__
52 #endif
54 CAIRO_BEGIN_DECLS
56 /* C++11 atomic primitives were designed to be more flexible than the
57 * __sync_* family of primitives. Despite the name, they are available
58 * in C as well as C++. The motivating reason for using them is that
59 * for _cairo_atomic_{int,ptr}_get, the compiler is able to see that
60 * the load is intended to be atomic, as opposed to the __sync_*
61 * version, below, where the load looks like a plain load. Having
62 * the load appear atomic to the compiler is particular important for
63 * tools like ThreadSanitizer so they don't report false positives on
64 * memory operations that we intend to be atomic.
66 #if HAVE_CXX11_ATOMIC_PRIMITIVES
68 #define HAS_ATOMIC_OPS 1
70 typedef int cairo_atomic_int_t;
72 static cairo_always_inline cairo_atomic_int_t
73 _cairo_atomic_int_get (cairo_atomic_int_t *x)
75 return __atomic_load_n(x, __ATOMIC_SEQ_CST);
78 static cairo_always_inline void *
79 _cairo_atomic_ptr_get (void **x)
81 return __atomic_load_n(x, __ATOMIC_SEQ_CST);
84 # define _cairo_atomic_int_inc(x) ((void) __atomic_fetch_add(x, 1, __ATOMIC_SEQ_CST))
85 # define _cairo_atomic_int_dec(x) ((void) __atomic_fetch_sub(x, 1, __ATOMIC_SEQ_CST))
86 # define _cairo_atomic_int_dec_and_test(x) (__atomic_fetch_sub(x, 1, __ATOMIC_SEQ_CST) == 1)
88 #if SIZEOF_VOID_P==SIZEOF_INT
89 typedef int cairo_atomic_intptr_t;
90 #elif SIZEOF_VOID_P==SIZEOF_LONG
91 typedef long cairo_atomic_intptr_t;
92 #elif SIZEOF_VOID_P==SIZEOF_LONG_LONG
93 typedef long long cairo_atomic_intptr_t;
94 #else
95 #error No matching integer pointer type
96 #endif
98 static cairo_always_inline cairo_bool_t
99 _cairo_atomic_int_cmpxchg_impl(cairo_atomic_int_t *x,
100 cairo_atomic_int_t oldv,
101 cairo_atomic_int_t newv)
103 cairo_atomic_int_t expected = oldv;
104 return __atomic_compare_exchange_n(x, &expected, newv, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
107 #define _cairo_atomic_int_cmpxchg(x, oldv, newv) \
108 _cairo_atomic_int_cmpxchg_impl(x, oldv, newv)
110 static cairo_always_inline cairo_atomic_int_t
111 _cairo_atomic_int_cmpxchg_return_old_impl(cairo_atomic_int_t *x,
112 cairo_atomic_int_t oldv,
113 cairo_atomic_int_t newv)
115 cairo_atomic_int_t expected = oldv;
116 (void) __atomic_compare_exchange_n(x, &expected, newv, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
117 return expected;
120 #define _cairo_atomic_int_cmpxchg_return_old(x, oldv, newv) \
121 _cairo_atomic_int_cmpxchg_return_old_impl(x, oldv, newv)
123 static cairo_always_inline cairo_bool_t
124 _cairo_atomic_ptr_cmpxchg_impl(void **x, void *oldv, void *newv)
126 void *expected = oldv;
127 return __atomic_compare_exchange_n(x, &expected, newv, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
130 #define _cairo_atomic_ptr_cmpxchg(x, oldv, newv) \
131 _cairo_atomic_ptr_cmpxchg_impl(x, oldv, newv)
133 static cairo_always_inline void *
134 _cairo_atomic_ptr_cmpxchg_return_old_impl(void **x, void *oldv, void *newv)
136 void *expected = oldv;
137 (void) __atomic_compare_exchange_n(x, &expected, newv, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
138 return expected;
141 #define _cairo_atomic_ptr_cmpxchg_return_old(x, oldv, newv) \
142 _cairo_atomic_ptr_cmpxchg_return_old_impl(x, oldv, newv)
144 #endif
146 #if HAVE_INTEL_ATOMIC_PRIMITIVES
148 #define HAS_ATOMIC_OPS 1
150 typedef int cairo_atomic_int_t;
152 #ifdef ATOMIC_OP_NEEDS_MEMORY_BARRIER
153 static cairo_always_inline cairo_atomic_int_t
154 _cairo_atomic_int_get (cairo_atomic_int_t *x)
156 __sync_synchronize ();
157 return *x;
160 static cairo_always_inline void *
161 _cairo_atomic_ptr_get (void **x)
163 __sync_synchronize ();
164 return *x;
166 #else
167 # define _cairo_atomic_int_get(x) (*x)
168 # define _cairo_atomic_ptr_get(x) (*x)
169 #endif
171 # define _cairo_atomic_int_inc(x) ((void) __sync_fetch_and_add(x, 1))
172 # define _cairo_atomic_int_dec(x) ((void) __sync_fetch_and_add(x, -1))
173 # define _cairo_atomic_int_dec_and_test(x) (__sync_fetch_and_add(x, -1) == 1)
174 # define _cairo_atomic_int_cmpxchg(x, oldv, newv) __sync_bool_compare_and_swap (x, oldv, newv)
175 # define _cairo_atomic_int_cmpxchg_return_old(x, oldv, newv) __sync_val_compare_and_swap (x, oldv, newv)
177 #if SIZEOF_VOID_P==SIZEOF_INT
178 typedef int cairo_atomic_intptr_t;
179 #elif SIZEOF_VOID_P==SIZEOF_LONG
180 typedef long cairo_atomic_intptr_t;
181 #elif SIZEOF_VOID_P==SIZEOF_LONG_LONG
182 typedef long long cairo_atomic_intptr_t;
183 #else
184 #error No matching integer pointer type
185 #endif
187 # define _cairo_atomic_ptr_cmpxchg(x, oldv, newv) \
188 __sync_bool_compare_and_swap ((cairo_atomic_intptr_t*)x, (cairo_atomic_intptr_t)oldv, (cairo_atomic_intptr_t)newv)
190 # define _cairo_atomic_ptr_cmpxchg_return_old(x, oldv, newv) \
191 _cairo_atomic_intptr_to_voidptr (__sync_val_compare_and_swap ((cairo_atomic_intptr_t*)x, (cairo_atomic_intptr_t)oldv, (cairo_atomic_intptr_t)newv))
193 #endif
195 #if HAVE_LIB_ATOMIC_OPS
196 #include <atomic_ops.h>
198 #define HAS_ATOMIC_OPS 1
200 typedef AO_t cairo_atomic_int_t;
202 # define _cairo_atomic_int_get(x) (AO_load_full (x))
204 # define _cairo_atomic_int_inc(x) ((void) AO_fetch_and_add1_full(x))
205 # define _cairo_atomic_int_dec(x) ((void) AO_fetch_and_sub1_full(x))
206 # define _cairo_atomic_int_dec_and_test(x) (AO_fetch_and_sub1_full(x) == 1)
207 # define _cairo_atomic_int_cmpxchg(x, oldv, newv) AO_compare_and_swap_full(x, oldv, newv)
209 #if SIZEOF_VOID_P==SIZEOF_INT
210 typedef unsigned int cairo_atomic_intptr_t;
211 #elif SIZEOF_VOID_P==SIZEOF_LONG
212 typedef unsigned long cairo_atomic_intptr_t;
213 #elif SIZEOF_VOID_P==SIZEOF_LONG_LONG
214 typedef unsigned long long cairo_atomic_intptr_t;
215 #else
216 #error No matching integer pointer type
217 #endif
219 # define _cairo_atomic_ptr_get(x) _cairo_atomic_intptr_to_voidptr (AO_load_full (x))
220 # define _cairo_atomic_ptr_cmpxchg(x, oldv, newv) \
221 _cairo_atomic_int_cmpxchg ((cairo_atomic_intptr_t*)(x), (cairo_atomic_intptr_t)oldv, (cairo_atomic_intptr_t)newv)
223 #endif
225 #if HAVE_OS_ATOMIC_OPS
226 #include <libkern/OSAtomic.h>
228 #define HAS_ATOMIC_OPS 1
230 typedef int32_t cairo_atomic_int_t;
232 # define _cairo_atomic_int_get(x) (OSMemoryBarrier(), *(x))
234 # define _cairo_atomic_int_inc(x) ((void) OSAtomicIncrement32Barrier (x))
235 # define _cairo_atomic_int_dec(x) ((void) OSAtomicDecrement32Barrier (x))
236 # define _cairo_atomic_int_dec_and_test(x) (OSAtomicDecrement32Barrier (x) == 0)
237 # define _cairo_atomic_int_cmpxchg(x, oldv, newv) OSAtomicCompareAndSwap32Barrier(oldv, newv, x)
239 #if SIZEOF_VOID_P==4
240 typedef int32_t cairo_atomic_intptr_t;
241 # define _cairo_atomic_ptr_cmpxchg(x, oldv, newv) \
242 OSAtomicCompareAndSwap32Barrier((cairo_atomic_intptr_t)oldv, (cairo_atomic_intptr_t)newv, (cairo_atomic_intptr_t *)x)
244 #elif SIZEOF_VOID_P==8
245 typedef int64_t cairo_atomic_intptr_t;
246 # define _cairo_atomic_ptr_cmpxchg(x, oldv, newv) \
247 OSAtomicCompareAndSwap64Barrier((cairo_atomic_intptr_t)oldv, (cairo_atomic_intptr_t)newv, (cairo_atomic_intptr_t *)x)
249 #else
250 #error No matching integer pointer type
251 #endif
253 # define _cairo_atomic_ptr_get(x) (OSMemoryBarrier(), *(x))
255 #endif
257 #ifndef HAS_ATOMIC_OPS
259 #if SIZEOF_VOID_P==SIZEOF_INT
260 typedef unsigned int cairo_atomic_intptr_t;
261 #elif SIZEOF_VOID_P==SIZEOF_LONG
262 typedef unsigned long cairo_atomic_intptr_t;
263 #elif SIZEOF_VOID_P==SIZEOF_LONG_LONG
264 typedef unsigned long long cairo_atomic_intptr_t;
265 #else
266 #error No matching integer pointer type
267 #endif
269 typedef cairo_atomic_intptr_t cairo_atomic_int_t;
271 cairo_private void
272 _cairo_atomic_int_inc (cairo_atomic_int_t *x);
274 #define _cairo_atomic_int_dec(x) _cairo_atomic_int_dec_and_test(x)
276 cairo_private cairo_bool_t
277 _cairo_atomic_int_dec_and_test (cairo_atomic_int_t *x);
279 cairo_private cairo_atomic_int_t
280 _cairo_atomic_int_cmpxchg_return_old_impl (cairo_atomic_int_t *x, cairo_atomic_int_t oldv, cairo_atomic_int_t newv);
282 cairo_private void *
283 _cairo_atomic_ptr_cmpxchg_return_old_impl (void **x, void *oldv, void *newv);
285 #define _cairo_atomic_int_cmpxchg_return_old(x, oldv, newv) _cairo_atomic_int_cmpxchg_return_old_impl (x, oldv, newv)
286 #define _cairo_atomic_ptr_cmpxchg_return_old(x, oldv, newv) _cairo_atomic_ptr_cmpxchg_return_old_impl (x, oldv, newv)
288 #ifdef ATOMIC_OP_NEEDS_MEMORY_BARRIER
289 cairo_private cairo_atomic_int_t
290 _cairo_atomic_int_get (cairo_atomic_int_t *x);
291 # define _cairo_atomic_ptr_get(x) (void *) _cairo_atomic_int_get((cairo_atomic_int_t *) x)
292 #else
293 # define _cairo_atomic_int_get(x) (*x)
294 # define _cairo_atomic_ptr_get(x) (*x)
295 #endif
297 #else
299 /* Workaround GCC complaining about casts */
300 static cairo_always_inline void *
301 _cairo_atomic_intptr_to_voidptr (cairo_atomic_intptr_t x)
303 return (void *) x;
306 static cairo_always_inline cairo_atomic_int_t
307 _cairo_atomic_int_cmpxchg_return_old_fallback(cairo_atomic_int_t *x, cairo_atomic_int_t oldv, cairo_atomic_int_t newv)
309 cairo_atomic_int_t curr;
311 do {
312 curr = _cairo_atomic_int_get (x);
313 } while (curr == oldv && !_cairo_atomic_int_cmpxchg (x, oldv, newv));
315 return curr;
318 static cairo_always_inline void *
319 _cairo_atomic_ptr_cmpxchg_return_old_fallback(void **x, void *oldv, void *newv)
321 void *curr;
323 do {
324 curr = _cairo_atomic_ptr_get (x);
325 } while (curr == oldv && !_cairo_atomic_ptr_cmpxchg (x, oldv, newv));
327 return curr;
329 #endif
331 #ifndef _cairo_atomic_int_cmpxchg_return_old
332 #define _cairo_atomic_int_cmpxchg_return_old(x, oldv, newv) _cairo_atomic_int_cmpxchg_return_old_fallback (x, oldv, newv)
333 #endif
335 #ifndef _cairo_atomic_ptr_cmpxchg_return_old
336 #define _cairo_atomic_ptr_cmpxchg_return_old(x, oldv, newv) _cairo_atomic_ptr_cmpxchg_return_old_fallback (x, oldv, newv)
337 #endif
339 #ifndef _cairo_atomic_int_cmpxchg
340 #define _cairo_atomic_int_cmpxchg(x, oldv, newv) (_cairo_atomic_int_cmpxchg_return_old (x, oldv, newv) == oldv)
341 #endif
343 #ifndef _cairo_atomic_ptr_cmpxchg
344 #define _cairo_atomic_ptr_cmpxchg(x, oldv, newv) (_cairo_atomic_ptr_cmpxchg_return_old (x, oldv, newv) == oldv)
345 #endif
347 #define _cairo_atomic_uint_get(x) _cairo_atomic_int_get(x)
348 #define _cairo_atomic_uint_cmpxchg(x, oldv, newv) \
349 _cairo_atomic_int_cmpxchg((cairo_atomic_int_t *)x, oldv, newv)
351 #define _cairo_status_set_error(status, err) do { \
352 int ret__; \
353 assert (err < CAIRO_STATUS_LAST_STATUS); \
354 /* hide compiler warnings about cairo_status_t != int (gcc treats its as \
355 * an unsigned integer instead, and about ignoring the return value. */ \
356 ret__ = _cairo_atomic_int_cmpxchg ((cairo_atomic_int_t *) status, CAIRO_STATUS_SUCCESS, err); \
357 (void) ret__; \
358 } while (0)
360 CAIRO_END_DECLS
362 #endif