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 <chris@chris-wilson.co.uk>
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 
40 /* atomic_int */
41 
42 /* We need external help for these */
43 
44 #if 0
45 
46 
47 #elif !defined(FC_NO_MT) && defined(_MSC_VER) || defined(__MINGW32__)
48 
49 #include "fcwindows.h"
50 
51 /* mingw32 does not have MemoryBarrier.
52  * MemoryBarrier may be defined as a macro or a function.
53  * Just make a failsafe version for ourselves. */
54 #ifdef MemoryBarrier
55 #define HBMemoryBarrier MemoryBarrier
56 #else
HBMemoryBarrier(void)57 static inline void HBMemoryBarrier (void) {
58   long dummy = 0;
59   InterlockedExchange (&dummy, 1);
60 }
61 #endif
62 
63 typedef LONG fc_atomic_int_t;
64 #define fc_atomic_int_add(AI, V)	InterlockedExchangeAdd (&(AI), (V))
65 
66 #define fc_atomic_ptr_get(P)		(HBMemoryBarrier (), (void *) *(P))
67 #define fc_atomic_ptr_cmpexch(P,O,N)	(InterlockedCompareExchangePointer ((void **) (P), (void *) (N), (void *) (O)) == (void *) (O))
68 
69 
70 #elif !defined(FC_NO_MT) && defined(__APPLE__)
71 
72 #include <libkern/OSAtomic.h>
73 #ifdef __MAC_OS_X_MIN_REQUIRED
74 #include <AvailabilityMacros.h>
75 #elif defined(__IPHONE_OS_MIN_REQUIRED)
76 #include <Availability.h>
77 #endif
78 
79 typedef int fc_atomic_int_t;
80 #define fc_atomic_int_add(AI, V)	(OSAtomicAdd32Barrier ((V), &(AI)) - (V))
81 
82 #define fc_atomic_ptr_get(P)		(OSMemoryBarrier (), (void *) *(P))
83 #if (MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4 || __IPHONE_VERSION_MIN_REQUIRED >= 20100)
84 #define fc_atomic_ptr_cmpexch(P,O,N)	OSAtomicCompareAndSwapPtrBarrier ((void *) (O), (void *) (N), (void **) (P))
85 #else
86 #if __ppc64__ || __x86_64__
87 #define fc_atomic_ptr_cmpexch(P,O,N)	OSAtomicCompareAndSwap64Barrier ((int64_t) (O), (int64_t) (N), (int64_t*) (P))
88 #else
89 #define fc_atomic_ptr_cmpexch(P,O,N)	OSAtomicCompareAndSwap32Barrier ((int32_t) (O), (int32_t) (N), (int32_t*) (P))
90 #endif
91 #endif
92 
93 #elif !defined(FC_NO_MT) && defined(HAVE_INTEL_ATOMIC_PRIMITIVES)
94 
95 typedef int fc_atomic_int_t;
96 #define fc_atomic_int_add(AI, V)	__sync_fetch_and_add (&(AI), (V))
97 
98 #define fc_atomic_ptr_get(P)		(void *) (__sync_synchronize (), *(P))
99 #define fc_atomic_ptr_cmpexch(P,O,N)	__sync_bool_compare_and_swap ((P), (O), (N))
100 
101 
102 #elif !defined(FC_NO_MT) && defined(HAVE_SOLARIS_ATOMIC_OPS)
103 
104 #include <atomic.h>
105 #include <mbarrier.h>
106 
107 typedef unsigned int fc_atomic_int_t;
108 #define fc_atomic_int_add(AI, V)	( ({__machine_rw_barrier ();}), atomic_add_int_nv (&(AI), (V)) - (V))
109 
110 #define fc_atomic_ptr_get(P)		( ({__machine_rw_barrier ();}), (void *) *(P))
111 #define fc_atomic_ptr_cmpexch(P,O,N)	( ({__machine_rw_barrier ();}), atomic_cas_ptr ((P), (O), (N)) == (void *) (O) ? FcTrue : FcFalse)
112 
113 
114 #elif !defined(FC_NO_MT)
115 
116 #define FC_ATOMIC_INT_NIL 1 /* Warn that fallback implementation is in use. */
117 typedef volatile int fc_atomic_int_t;
118 #define fc_atomic_int_add(AI, V)	(((AI) += (V)) - (V))
119 
120 #define fc_atomic_ptr_get(P)		((void *) *(P))
121 #define fc_atomic_ptr_cmpexch(P,O,N)	(* (void * volatile *) (P) == (void *) (O) ? (* (void * volatile *) (P) = (void *) (N), FcTrue) : FcFalse)
122 
123 
124 #else /* FC_NO_MT */
125 
126 typedef int fc_atomic_int_t;
127 #define fc_atomic_int_add(AI, V)	(((AI) += (V)) - (V))
128 
129 #define fc_atomic_ptr_get(P)		((void *) *(P))
130 #define fc_atomic_ptr_cmpexch(P,O,N)	(* (void **) (P) == (void *) (O) ? (* (void **) (P) = (void *) (N), FcTrue) : FcFalse)
131 
132 #endif
133 
134 /* reference count */
135 #define FC_REF_CONSTANT_VALUE ((fc_atomic_int_t) -1)
136 #define FC_REF_CONSTANT {FC_REF_CONSTANT_VALUE}
137 typedef struct _FcRef { fc_atomic_int_t count; } FcRef;
FcRefInit(FcRef * r,int v)138 static inline void   FcRefInit     (FcRef *r, int v) { r->count = v; }
FcRefInc(FcRef * r)139 static inline int    FcRefInc      (FcRef *r) { return fc_atomic_int_add (r->count, +1); }
FcRefDec(FcRef * r)140 static inline int    FcRefDec      (FcRef *r) { return fc_atomic_int_add (r->count, -1); }
FcRefAdd(FcRef * r,int v)141 static inline int    FcRefAdd      (FcRef *r, int v) { return fc_atomic_int_add (r->count, v); }
FcRefSetConst(FcRef * r)142 static inline void   FcRefSetConst (FcRef *r) { r->count = FC_REF_CONSTANT_VALUE; }
FcRefIsConst(const FcRef * r)143 static inline FcBool FcRefIsConst  (const FcRef *r) { return r->count == FC_REF_CONSTANT_VALUE; }
144 
145 #endif /* _FCATOMIC_H_ */
146