• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2007  Chris Wilson
3  * Copyright © 2009,2010  Red Hat, Inc.
4  * Copyright © 2011,2012  Google, Inc.
5  *
6  *  This is part of HarfBuzz, a text shaping library.
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 HB_ATOMIC_HH
33 #define HB_ATOMIC_HH
34 
35 #include "hb.hh"
36 #include "hb-meta.hh"
37 
38 
39 /*
40  * Atomic integers and pointers.
41  */
42 
43 
44 /* We need external help for these */
45 
46 #if defined(hb_atomic_int_impl_add) \
47  && defined(hb_atomic_ptr_impl_get) \
48  && defined(hb_atomic_ptr_impl_cmpexch)
49 
50 /* Defined externally, i.e. in config.h. */
51 
52 
53 #elif !defined(HB_NO_MT) && defined(__ATOMIC_ACQUIRE)
54 
55 /* C++11-style GCC primitives. We prefer these as they don't require linking to libstdc++ / libc++. */
56 
57 #define _hb_memory_barrier()			__sync_synchronize ()
58 
59 #define hb_atomic_int_impl_add(AI, V)		__atomic_fetch_add ((AI), (V), __ATOMIC_ACQ_REL)
60 #define hb_atomic_int_impl_set_relaxed(AI, V)	__atomic_store_n ((AI), (V), __ATOMIC_RELAXED)
61 #define hb_atomic_int_impl_set(AI, V)		__atomic_store_n ((AI), (V), __ATOMIC_RELEASE)
62 #define hb_atomic_int_impl_get_relaxed(AI)	__atomic_load_n ((AI), __ATOMIC_RELAXED)
63 #define hb_atomic_int_impl_get(AI)		__atomic_load_n ((AI), __ATOMIC_ACQUIRE)
64 
65 #define hb_atomic_ptr_impl_set_relaxed(P, V)	__atomic_store_n ((P), (V), __ATOMIC_RELAXED)
66 #define hb_atomic_ptr_impl_get_relaxed(P)	__atomic_load_n ((P), __ATOMIC_RELAXED)
67 #define hb_atomic_ptr_impl_get(P)		__atomic_load_n ((P), __ATOMIC_ACQUIRE)
68 static inline bool
_hb_atomic_ptr_impl_cmplexch(const void ** P,const void * O_,const void * N)69 _hb_atomic_ptr_impl_cmplexch (const void **P, const void *O_, const void *N)
70 {
71   const void *O = O_; // Need lvalue
72   return __atomic_compare_exchange_n ((void **) P, (void **) &O, (void *) N, true, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED);
73 }
74 #define hb_atomic_ptr_impl_cmpexch(P,O,N)	_hb_atomic_ptr_impl_cmplexch ((const void **) (P), (O), (N))
75 
76 
77 #elif !defined(HB_NO_MT)
78 
79 /* C++11 atomics. */
80 
81 #include <atomic>
82 
83 #define _hb_memory_barrier()			std::atomic_thread_fence(std::memory_order_ack_rel)
84 #define _hb_memory_r_barrier()			std::atomic_thread_fence(std::memory_order_acquire)
85 #define _hb_memory_w_barrier()			std::atomic_thread_fence(std::memory_order_release)
86 
87 #define hb_atomic_int_impl_add(AI, V)		(reinterpret_cast<std::atomic<int> *> (AI)->fetch_add ((V), std::memory_order_acq_rel))
88 #define hb_atomic_int_impl_set_relaxed(AI, V)	(reinterpret_cast<std::atomic<int> *> (AI)->store ((V), std::memory_order_relaxed))
89 #define hb_atomic_int_impl_set(AI, V)		(reinterpret_cast<std::atomic<int> *> (AI)->store ((V), std::memory_order_release))
90 #define hb_atomic_int_impl_get_relaxed(AI)	(reinterpret_cast<std::atomic<int> const *> (AI)->load (std::memory_order_relaxed))
91 #define hb_atomic_int_impl_get(AI)		(reinterpret_cast<std::atomic<int> const *> (AI)->load (std::memory_order_acquire))
92 
93 #define hb_atomic_ptr_impl_set_relaxed(P, V)	(reinterpret_cast<std::atomic<void*> *> (P)->store ((V), std::memory_order_relaxed))
94 #define hb_atomic_ptr_impl_get_relaxed(P)	(reinterpret_cast<std::atomic<void*> const *> (P)->load (std::memory_order_relaxed))
95 #define hb_atomic_ptr_impl_get(P)		(reinterpret_cast<std::atomic<void*> *> (P)->load (std::memory_order_acquire))
96 static inline bool
_hb_atomic_ptr_impl_cmplexch(const void ** P,const void * O_,const void * N)97 _hb_atomic_ptr_impl_cmplexch (const void **P, const void *O_, const void *N)
98 {
99   const void *O = O_; // Need lvalue
100   return reinterpret_cast<std::atomic<const void*> *> (P)->compare_exchange_weak (O, N, std::memory_order_acq_rel, std::memory_order_relaxed);
101 }
102 #define hb_atomic_ptr_impl_cmpexch(P,O,N)	_hb_atomic_ptr_impl_cmplexch ((const void **) (P), (O), (N))
103 
104 
105 #elif defined(HB_NO_MT)
106 
107 #define hb_atomic_int_impl_add(AI, V)		((*(AI) += (V)) - (V))
108 
109 #define _hb_memory_barrier()			do {} while (0)
110 
111 #define hb_atomic_ptr_impl_cmpexch(P,O,N)	(* (void **) (P) == (void *) (O) ? (* (void **) (P) = (void *) (N), true) : false)
112 
113 
114 #else
115 
116 #error "Could not find any system to define atomic_int macros."
117 #error "Check hb-atomic.hh for possible resolutions."
118 
119 #endif
120 
121 
122 #ifndef _hb_memory_r_barrier
123 #define _hb_memory_r_barrier()			_hb_memory_barrier ()
124 #endif
125 #ifndef _hb_memory_w_barrier
126 #define _hb_memory_w_barrier()			_hb_memory_barrier ()
127 #endif
128 #ifndef hb_atomic_int_impl_set_relaxed
129 #define hb_atomic_int_impl_set_relaxed(AI, V)	(*(AI) = (V))
130 #endif
131 #ifndef hb_atomic_int_impl_get_relaxed
132 #define hb_atomic_int_impl_get_relaxed(AI)	(*(AI))
133 #endif
134 
135 #ifndef hb_atomic_ptr_impl_set_relaxed
136 #define hb_atomic_ptr_impl_set_relaxed(P, V)	(*(P) = (V))
137 #endif
138 #ifndef hb_atomic_ptr_impl_get_relaxed
139 #define hb_atomic_ptr_impl_get_relaxed(P)	(*(P))
140 #endif
141 #ifndef hb_atomic_int_impl_set
hb_atomic_int_impl_set(int * AI,int v)142 inline void hb_atomic_int_impl_set (int *AI, int v)	{ _hb_memory_w_barrier (); *AI = v; }
143 #endif
144 #ifndef hb_atomic_int_impl_get
hb_atomic_int_impl_get(const int * AI)145 inline int hb_atomic_int_impl_get (const int *AI)	{ int v = *AI; _hb_memory_r_barrier (); return v; }
146 #endif
147 #ifndef hb_atomic_ptr_impl_get
hb_atomic_ptr_impl_get(void ** const P)148 inline void *hb_atomic_ptr_impl_get (void ** const P)	{ void *v = *P; _hb_memory_r_barrier (); return v; }
149 #endif
150 
151 
152 struct hb_atomic_int_t
153 {
154   hb_atomic_int_t () = default;
hb_atomic_int_thb_atomic_int_t155   constexpr hb_atomic_int_t (int v) : v (v) {}
156 
set_relaxedhb_atomic_int_t157   void set_relaxed (int v_) { hb_atomic_int_impl_set_relaxed (&v, v_); }
sethb_atomic_int_t158   void set (int v_) { hb_atomic_int_impl_set (&v, v_); }
get_relaxedhb_atomic_int_t159   int get_relaxed () const { return hb_atomic_int_impl_get_relaxed (&v); }
gethb_atomic_int_t160   int get () const { return hb_atomic_int_impl_get (&v); }
inchb_atomic_int_t161   int inc () { return hb_atomic_int_impl_add (&v,  1); }
dechb_atomic_int_t162   int dec () { return hb_atomic_int_impl_add (&v, -1); }
163 
164   int v = 0;
165 };
166 
167 template <typename P>
168 struct hb_atomic_ptr_t
169 {
170   typedef hb_remove_pointer<P> T;
171 
172   hb_atomic_ptr_t () = default;
hb_atomic_ptr_thb_atomic_ptr_t173   constexpr hb_atomic_ptr_t (T* v) : v (v) {}
174 
inithb_atomic_ptr_t175   void init (T* v_ = nullptr) { set_relaxed (v_); }
set_relaxedhb_atomic_ptr_t176   void set_relaxed (T* v_) { hb_atomic_ptr_impl_set_relaxed (&v, v_); }
get_relaxedhb_atomic_ptr_t177   T *get_relaxed () const { return (T *) hb_atomic_ptr_impl_get_relaxed (&v); }
gethb_atomic_ptr_t178   T *get () const { return (T *) hb_atomic_ptr_impl_get ((void **) &v); }
cmpexchhb_atomic_ptr_t179   bool cmpexch (const T *old, T *new_) const { return hb_atomic_ptr_impl_cmpexch ((void **) &v, (void *) old, (void *) new_); }
180 
operator ->hb_atomic_ptr_t181   T * operator -> () const                    { return get (); }
operator C*hb_atomic_ptr_t182   template <typename C> operator C * () const { return get (); }
183 
184   T *v = nullptr;
185 };
186 
187 
188 #endif /* HB_ATOMIC_HH */
189