Add thread-safety primitives
[platform/upstream/fontconfig.git] / src / fcatomic.h
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  Google, Inc.
7  *
8  *  This is part of HarfBuzz, a text shaping library.
9  *
10  * Permission is hereby granted, without written agreement and without
11  * license or royalty fees, to use, copy, modify, and distribute this
12  * software and its documentation for any purpose, provided that the
13  * above copyright notice and the following two paragraphs appear in
14  * all copies of this software.
15  *
16  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
17  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
18  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
19  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
20  * DAMAGE.
21  *
22  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
23  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
25  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
26  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
27  *
28  * Contributor(s):
29  *      Chris Wilson <chris@chris-wilson.co.uk>
30  * Red Hat Author(s): Behdad Esfahbod
31  * Google Author(s): Behdad Esfahbod
32  */
33
34 #ifndef _FCATOMIC_H_
35 #define _FCATOMIC_H_
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41
42 /* atomic_int */
43
44 /* We need external help for these */
45
46 #if 0
47
48
49 #elif !defined(FC_NO_MT) && defined(_MSC_VER) || defined(__MINGW32__)
50
51 #define WIN32_LEAN_AND_MEAN
52 #include <windows.h>
53
54 /* mingw32 does not have MemoryBarrier.
55  * MemoryBarrier may be defined as a macro or a function.
56  * Just make a failsafe version for ourselves. */
57 #ifdef MemoryBarrier
58 #define HBMemoryBarrier MemoryBarrier
59 #else
60 static inline void HBMemoryBarrier (void) {
61   long dummy = 0;
62   InterlockedExchange (&dummy, 1);
63 }
64 #endif
65
66 typedef long fc_atomic_int_t;
67 #define fc_atomic_int_add(AI, V)        InterlockedExchangeAdd (&(AI), (V))
68
69 #define fc_atomic_ptr_get(P)            (HBMemoryBarrier (), (void *) *(P))
70 #define fc_atomic_ptr_cmpexch(P,O,N)    (InterlockedCompareExchangePointer ((void **) (P), (void *) (N), (void *) (O)) == (void *) (O))
71
72
73 #elif !defined(FC_NO_MT) && defined(__APPLE__)
74
75 #include <libkern/OSAtomic.h>
76
77 typedef int32_t fc_atomic_int_t;
78 #define fc_atomic_int_add(AI, V)        (OSAtomicAdd32Barrier ((V), &(AI)) - (V))
79
80 #define fc_atomic_ptr_get(P)            (OSMemoryBarrier (), (void *) *(P))
81 #define fc_atomic_ptr_cmpexch(P,O,N)    OSAtomicCompareAndSwapPtrBarrier ((void *) (O), (void *) (N), (void **) (P))
82
83
84 #elif !defined(FC_NO_MT) && defined(HAVE_INTEL_ATOMIC_PRIMITIVES)
85
86 typedef int fc_atomic_int_t;
87 #define fc_atomic_int_add(AI, V)        __sync_fetch_and_add (&(AI), (V))
88
89 #define fc_atomic_ptr_get(P)            (void *) (__sync_synchronize (), *(P))
90 #define fc_atomic_ptr_cmpexch(P,O,N)    __sync_bool_compare_and_swap ((P), (O), (N))
91
92
93 #elif !defined(FC_NO_MT)
94
95 #define FC_ATOMIC_INT_NIL 1 /* Warn that fallback implementation is in use. */
96 typedef volatile int fc_atomic_int_t;
97 #define fc_atomic_int_add(AI, V)        (((AI) += (V)) - (V))
98
99 #define fc_atomic_ptr_get(P)            ((void *) *(P))
100 #define fc_atomic_ptr_cmpexch(P,O,N)    (* (void * volatile *) (P) == (void *) (O) ? (* (void * volatile *) (P) = (void *) (N), true) : false)
101
102
103 #else /* FC_NO_MT */
104
105 typedef int fc_atomic_int_t;
106 #define fc_atomic_int_add(AI, V)        (((AI) += (V)) - (V))
107
108 #define fc_atomic_ptr_get(P)            ((void *) *(P))
109 #define fc_atomic_ptr_cmpexch(P,O,N)    (* (void **) (P) == (void *) (O) ? (* (void **) (P) = (void *) (N), true) : false)
110
111 #endif
112
113 /* reference count */
114 #define FC_REF_CONSTANT ((fc_atomic_int_t) -1)
115 #define FC_REF_CONSTANT_INIT {FC_REF_CONSTANT}
116 typedef struct _FcRef { fc_atomic_int_t count; } FcRef;
117 static inline void   FcRefInit    (FcRef *r, int v) { r->count = v; }
118 static inline int    FcRefInc     (FcRef *r) { return fc_atomic_int_add (r->count, +1); }
119 static inline int    FcRefDec     (FcRef *r) { return fc_atomic_int_add (r->count, -1); }
120 static inline void   FcRefFinish  (FcRef *r) { r->count = FC_REF_CONSTANT; }
121 static inline FcBool FcRefIsConst (FcRef *r) { return r->count == FC_REF_CONSTANT; }
122
123 #endif /* _FCATOMIC_H_ */