2 * Copyright © 2007 Chris Wilson
3 * Copyright © 2009,2010 Red Hat, Inc.
4 * Copyright © 2011,2012 Google, Inc.
6 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
27 * Chris Wilson <chris@chris-wilson.co.uk>
28 * Red Hat Author(s): Behdad Esfahbod
29 * Google Author(s): Behdad Esfahbod
40 * Atomic integers and pointers.
44 /* We need external help for these */
46 #if defined(hb_atomic_int_impl_add) \
47 && defined(hb_atomic_ptr_impl_get) \
48 && defined(hb_atomic_ptr_impl_cmpexch)
50 /* Defined externally, i.e. in config.h. */
53 #elif !defined(HB_NO_MT) && defined(__ATOMIC_ACQUIRE)
55 /* C++11-style GCC primitives. We prefer these as they don't require linking to libstdc++ / libc++. */
57 #define _hb_memory_barrier() __sync_synchronize ()
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)
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)
69 _hb_atomic_ptr_impl_cmplexch (const void **P, const void *O_, const void *N)
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);
74 #define hb_atomic_ptr_impl_cmpexch(P,O,N) _hb_atomic_ptr_impl_cmplexch ((const void **) (P), (O), (N))
77 #elif !defined(HB_NO_MT)
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)
87 #define hb_atomic_int_impl_add(AI, V) (reinterpret_cast<std::atomic<std::decay<decltype (*(AI))>::type> *> (AI)->fetch_add ((V), std::memory_order_acq_rel))
88 #define hb_atomic_int_impl_set_relaxed(AI, V) (reinterpret_cast<std::atomic<std::decay<decltype (*(AI))>::type> *> (AI)->store ((V), std::memory_order_relaxed))
89 #define hb_atomic_int_impl_set(AI, V) (reinterpret_cast<std::atomic<std::decay<decltype (*(AI))>::type> *> (AI)->store ((V), std::memory_order_release))
90 #define hb_atomic_int_impl_get_relaxed(AI) (reinterpret_cast<std::atomic<std::decay<decltype (*(AI))>::type> const *> (AI)->load (std::memory_order_relaxed))
91 #define hb_atomic_int_impl_get(AI) (reinterpret_cast<std::atomic<std::decay<decltype (*(AI))>::type> const *> (AI)->load (std::memory_order_acquire))
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))
97 _hb_atomic_ptr_impl_cmplexch (const void **P, const void *O_, const void *N)
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);
102 #define hb_atomic_ptr_impl_cmpexch(P,O,N) _hb_atomic_ptr_impl_cmplexch ((const void **) (P), (O), (N))
105 #else /* defined(HB_NO_MT) */
107 #define hb_atomic_int_impl_add(AI, V) ((*(AI) += (V)) - (V))
108 #define _hb_memory_barrier() do {} while (0)
109 #define hb_atomic_ptr_impl_cmpexch(P,O,N) (* (void **) (P) == (void *) (O) ? (* (void **) (P) = (void *) (N), true) : false)
114 /* This should never be disabled, even under HB_NO_MT.
115 * except that MSVC gives me an internal compiler error, so disabled there.
117 * https://github.com/harfbuzz/harfbuzz/pull/4119
119 #ifndef _hb_compiler_memory_r_barrier
120 #if defined(__ATOMIC_ACQUIRE) // gcc-like
121 #define _hb_compiler_memory_r_barrier() asm volatile("": : :"memory")
122 #elif !defined(_MSC_VER)
124 #define _hb_compiler_memory_r_barrier() std::atomic_signal_fence (std::memory_order_acquire)
126 #define _hb_compiler_memory_r_barrier() do {} while (0)
132 #ifndef _hb_memory_r_barrier
133 #define _hb_memory_r_barrier() _hb_memory_barrier ()
135 #ifndef _hb_memory_w_barrier
136 #define _hb_memory_w_barrier() _hb_memory_barrier ()
138 #ifndef hb_atomic_int_impl_set_relaxed
139 #define hb_atomic_int_impl_set_relaxed(AI, V) (*(AI) = (V))
141 #ifndef hb_atomic_int_impl_get_relaxed
142 #define hb_atomic_int_impl_get_relaxed(AI) (*(AI))
145 #ifndef hb_atomic_ptr_impl_set_relaxed
146 #define hb_atomic_ptr_impl_set_relaxed(P, V) (*(P) = (V))
148 #ifndef hb_atomic_ptr_impl_get_relaxed
149 #define hb_atomic_ptr_impl_get_relaxed(P) (*(P))
151 #ifndef hb_atomic_int_impl_set
152 inline void hb_atomic_int_impl_set (int *AI, int v) { _hb_memory_w_barrier (); *AI = v; }
153 inline void hb_atomic_int_impl_set (short *AI, short v) { _hb_memory_w_barrier (); *AI = v; }
155 #ifndef hb_atomic_int_impl_get
156 inline int hb_atomic_int_impl_get (const int *AI) { int v = *AI; _hb_memory_r_barrier (); return v; }
157 inline short hb_atomic_int_impl_get (const short *AI) { short v = *AI; _hb_memory_r_barrier (); return v; }
159 #ifndef hb_atomic_ptr_impl_get
160 inline void *hb_atomic_ptr_impl_get (void ** const P) { void *v = *P; _hb_memory_r_barrier (); return v; }
164 struct hb_atomic_short_t
166 hb_atomic_short_t () = default;
167 constexpr hb_atomic_short_t (short v) : v (v) {}
169 hb_atomic_short_t& operator = (short v_) { set_relaxed (v_); return *this; }
170 operator short () const { return get_relaxed (); }
172 void set_relaxed (short v_) { hb_atomic_int_impl_set_relaxed (&v, v_); }
173 void set_release (short v_) { hb_atomic_int_impl_set (&v, v_); }
174 short get_relaxed () const { return hb_atomic_int_impl_get_relaxed (&v); }
175 short get_acquire () const { return hb_atomic_int_impl_get (&v); }
176 short inc () { return hb_atomic_int_impl_add (&v, 1); }
177 short dec () { return hb_atomic_int_impl_add (&v, -1); }
182 struct hb_atomic_int_t
184 hb_atomic_int_t () = default;
185 constexpr hb_atomic_int_t (int v) : v (v) {}
187 hb_atomic_int_t& operator = (int v_) { set_relaxed (v_); return *this; }
188 operator int () const { return get_relaxed (); }
190 void set_relaxed (int v_) { hb_atomic_int_impl_set_relaxed (&v, v_); }
191 void set_release (int v_) { hb_atomic_int_impl_set (&v, v_); }
192 int get_relaxed () const { return hb_atomic_int_impl_get_relaxed (&v); }
193 int get_acquire () const { return hb_atomic_int_impl_get (&v); }
194 int inc () { return hb_atomic_int_impl_add (&v, 1); }
195 int dec () { return hb_atomic_int_impl_add (&v, -1); }
200 template <typename P>
201 struct hb_atomic_ptr_t
203 typedef hb_remove_pointer<P> T;
205 hb_atomic_ptr_t () = default;
206 constexpr hb_atomic_ptr_t (T* v) : v (v) {}
207 hb_atomic_ptr_t (const hb_atomic_ptr_t &other) = delete;
209 void init (T* v_ = nullptr) { set_relaxed (v_); }
210 void set_relaxed (T* v_) { hb_atomic_ptr_impl_set_relaxed (&v, v_); }
211 T *get_relaxed () const { return (T *) hb_atomic_ptr_impl_get_relaxed (&v); }
212 T *get_acquire () const { return (T *) hb_atomic_ptr_impl_get ((void **) &v); }
213 bool cmpexch (const T *old, T *new_) const { return hb_atomic_ptr_impl_cmpexch ((void **) &v, (void *) old, (void *) new_); }
215 T * operator -> () const { return get_acquire (); }
216 template <typename C> operator C * () const { return get_acquire (); }
222 #endif /* HB_ATOMIC_HH */