Debian Codegraph @

src/fcatomic.h

raw
1 /*
2 * Mutex operations. Originally copied from HarfBuzz.
3 *
4 * Copyright © 2007 Chris Wilson
5 * Copyright © 2009,2010 Red Hat, Inc.
6 * Copyright © 2011,2012,2013 Google, Inc.
7 *
8 * Permission is hereby granted, without written agreement and without
9 * license or royalty fees, to use, copy, modify, and distribute this
10 * software and its documentation for any purpose, provided that the
11 * above copyright notice and the following two paragraphs appear in
12 * all copies of this software.
13 *
14 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
15 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
17 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * DAMAGE.
19 *
20 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
21 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
23 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
24 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 *
26 * Contributor(s):
27 * Chris Wilson <[email protected]>
28 * Red Hat Author(s): Behdad Esfahbod
29 * Google Author(s): Behdad Esfahbod
30 */
31
32 #ifndef _FCATOMIC_H_
33 #define _FCATOMIC_H_
34
35 #ifdef HAVE_CONFIG_H
36 # include <config.h>
37 #endif
38
39 /* atomic_int */
40
41 /* We need external help for these */
42
43 #if 0
44
45 typedef <type> fc_atomic_int_t;
46 # define FC_ATOMIC_INT_FORMAT "<printf format for fc_atomic_int_t>"
47 # define fc_atomic_int_add(AI, V) o = (AI), (AI) += (V), o // atomic acquire/release
48
49 # define fc_atomic_ptr_get(P) *(P) // atomic acquire
50 # define fc_atomic_ptr_cmpexch(P, O, N) *(P) == (O) ? (*(P) = (N), FcTrue) : FcFalse // atomic release
51
52 #elif !defined(FC_NO_MT) && defined(HAVE_STDATOMIC_PRIMITIVES)
53
54 # include <stdatomic.h>
55
56 typedef atomic_int fc_atomic_int_t;
57 # define FC_ATOMIC_INT_FORMAT "d"
58 # define fc_atomic_int_add(AI, V) atomic_fetch_add_explicit (&(AI), (V), memory_order_acq_rel)
59
60 # define fc_atomic_ptr_get(P) atomic_load_explicit ((_Atomic (void *) *)(P), memory_order_acquire)
61 static inline FcBool _fc_atomic_ptr_cmpexch (_Atomic (void *) *P, void *O, _Atomic (void *) N)
62 {
63 return atomic_compare_exchange_strong_explicit (P, &O, N, memory_order_release, memory_order_relaxed);
64 }
65 # define fc_atomic_ptr_cmpexch(P, O, N) _fc_atomic_ptr_cmpexch ((_Atomic (void *) *)(P), (O), (N))
66
67 /* Casting -1 to _Atomic(int) produces a compiler error with Clang (but not GCC)
68 * so we have to override FC_REF_CONSTANT_VALUE for stdatomic.h atomics.
69 * See https://bugs.llvm.org/show_bug.cgi?id=40249. */
70 # define FC_REF_CONSTANT_VALUE (-1)
71
72 #elif !defined(FC_NO_MT) && defined(_MSC_VER) || defined(__MINGW32__)
73
74 # include "fcwindows.h"
75
76 typedef LONG fc_atomic_int_t;
77 # define FC_ATOMIC_INT_FORMAT "ld"
78 # define fc_atomic_int_add(AI, V) InterlockedExchangeAdd (&(AI), (V))
79
80 # define fc_atomic_ptr_get(P) (InterlockedCompareExchangePointerAcquire ((void **)(P), NULL, NULL))
81 # define fc_atomic_ptr_cmpexch(P, O, N) (InterlockedCompareExchangePointer ((void **)(P), (void *)(N), (void *)(O)) == (void *)(O))
82
83 #elif !defined(FC_NO_MT) && defined(__APPLE__)
84
85 # include <AvailabilityMacros.h>
86 # include <libkern/OSAtomic.h>
87
88 typedef int fc_atomic_int_t;
89 # define FC_ATOMIC_INT_FORMAT "d"
90 # define fc_atomic_int_add(AI, V) (OSAtomicAdd32Barrier ((V), &(AI)) - (V))
91
92 # if SIZEOF_VOID_P == 8
93 # define fc_atomic_ptr_get(P) OSAtomicAdd64Barrier (0, (int64_t *)(P))
94 # elif SIZEOF_VOID_P == 4
95 # define fc_atomic_ptr_get(P) OSAtomicAdd32Barrier (0, (int32_t *)(P))
96 # else
97 # error "SIZEOF_VOID_P not 4 or 8 (assumes CHAR_BIT is 8)"
98 # endif
99
100 # if (MAC_OS_X_VERSION_MIN_REQUIRED >= 1050 || __IPHONE_OS_VERSION_MIN_REQUIRED >= 20100)
101 # define fc_atomic_ptr_cmpexch(P, O, N) OSAtomicCompareAndSwapPtrBarrier ((void *)(O), (void *)(N), (void **)(P))
102 # else
103 # if __LP64__
104 # define fc_atomic_ptr_cmpexch(P, O, N) OSAtomicCompareAndSwap64Barrier ((int64_t)(O), (int64_t)(N), (int64_t *)(P))
105 # else
106 # define fc_atomic_ptr_cmpexch(P, O, N) OSAtomicCompareAndSwap32Barrier ((int32_t)(O), (int32_t)(N), (int32_t *)(P))
107 # endif
108 # endif
109
110 #elif !defined(FC_NO_MT) && defined(HAVE_INTEL_ATOMIC_PRIMITIVES)
111
112 typedef int fc_atomic_int_t;
113 # define FC_ATOMIC_INT_FORMAT "d"
114 # define fc_atomic_int_add(AI, V) __sync_fetch_and_add (&(AI), (V))
115
116 # define fc_atomic_ptr_get(P) (void *)(__sync_fetch_and_add ((P), 0))
117 # define fc_atomic_ptr_cmpexch(P, O, N) __sync_bool_compare_and_swap ((P), (O), (N))
118
119 #elif !defined(FC_NO_MT) && defined(HAVE_SOLARIS_ATOMIC_OPS)
120
121 # include <atomic.h>
122 # include <mbarrier.h>
123
124 typedef unsigned int fc_atomic_int_t;
125 # define FC_ATOMIC_INT_FORMAT "u"
126 # define fc_atomic_int_add(AI, V) (({ __machine_rw_barrier(); }), atomic_add_int_nv (&(AI), (V)) - (V))
127
128 # define fc_atomic_ptr_get(P) (({ __machine_rw_barrier(); }), (void *)*(P))
129 # define fc_atomic_ptr_cmpexch(P, O, N) (({ __machine_rw_barrier(); }), atomic_cas_ptr ((P), (O), (N)) == (void *)(O) ? FcTrue : FcFalse)
130
131 #elif !defined(FC_NO_MT)
132
133 # define FC_ATOMIC_INT_NIL 1 /* Warn that fallback implementation is in use. */
134 typedef volatile int fc_atomic_int_t;
135 # define FC_ATOMIC_INT_FORMAT "d"
136 # define fc_atomic_int_add(AI, V) (((AI) += (V)) - (V))
137
138 # define fc_atomic_ptr_get(P) ((void *)*(P))
139 # define fc_atomic_ptr_cmpexch(P, O, N) (*(void *volatile *)(P) == (void *)(O) ? (*(void *volatile *)(P) = (void *)(N), FcTrue) : FcFalse)
140
141 #else /* FC_NO_MT */
142
143 typedef int fc_atomic_int_t;
144 # define FC_ATOMIC_INT_FORMAT "d"
145 # define fc_atomic_int_add(AI, V) (((AI) += (V)) - (V))
146
147 # define fc_atomic_ptr_get(P) ((void *)*(P))
148 # define fc_atomic_ptr_cmpexch(P, O, N) (*(void **)(P) == (void *)(O) ? (*(void **)(P) = (void *)(N), FcTrue) : FcFalse)
149
150 #endif
151
152 /* reference count */
153 #ifndef FC_REF_CONSTANT_VALUE
154 # define FC_REF_CONSTANT_VALUE ((fc_atomic_int_t) - 1)
155 #endif
156 #define FC_REF_CONSTANT { FC_REF_CONSTANT_VALUE }
157 typedef struct _FcRef {
158 fc_atomic_int_t count;
159 } FcRef;
160 static inline void FcRefInit (FcRef *r, int v) { r->count = v; }
161 static inline int FcRefInc (FcRef *r) { return fc_atomic_int_add (r->count, +1); }
162 static inline int FcRefDec (FcRef *r) { return fc_atomic_int_add (r->count, -1); }
163 static inline int FcRefAdd (FcRef *r, int v) { return fc_atomic_int_add (r->count, v); }
164 static inline void FcRefSetConst (FcRef *r) { r->count = FC_REF_CONSTANT_VALUE; }
165 static inline FcBool FcRefIsConst (const FcRef *r) { return r->count == FC_REF_CONSTANT_VALUE; }
166
167 #endif /* _FCATOMIC_H_ */