1 /*
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * by Oracle in the LICENSE file that accompanied this code.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 // This file is available under and governed by the GNU General Public
  26 // License version 2 only, as published by the Free Software Foundation.
  27 // However, the following notice accompanied the original version of this
  28 // file:
  29 //
  30 /*
  31  * Copyright © 2007  Chris Wilson
  32  * Copyright © 2009,2010  Red Hat, Inc.
  33  * Copyright © 2011,2012  Google, Inc.
  34  *
  35  *  This is part of HarfBuzz, a text shaping library.
  36  *
  37  * Permission is hereby granted, without written agreement and without
  38  * license or royalty fees, to use, copy, modify, and distribute this
  39  * software and its documentation for any purpose, provided that the
  40  * above copyright notice and the following two paragraphs appear in
  41  * all copies of this software.
  42  *
  43  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  44  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  45  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  46  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  47  * DAMAGE.
  48  *
  49  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  50  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  51  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  52  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  53  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  54  *
  55  * Contributor(s):
  56  *      Chris Wilson <chris@chris-wilson.co.uk>
  57  * Red Hat Author(s): Behdad Esfahbod
  58  * Google Author(s): Behdad Esfahbod
  59  */
  60 
  61 #ifndef HB_ATOMIC_PRIVATE_HH
  62 #define HB_ATOMIC_PRIVATE_HH
  63 
  64 #include "hb-private.hh"
  65 
  66 
  67 /* atomic_int */
  68 
  69 /* We need external help for these */
  70 
  71 #if defined(hb_atomic_int_impl_add) \
  72  && defined(hb_atomic_ptr_impl_get) \
  73  && defined(hb_atomic_ptr_impl_cmpexch)
  74 
  75 /* Defined externally, i.e. in config.h; must have typedef'ed hb_atomic_int_impl_t as well. */
  76 
  77 
  78 #elif !defined(HB_NO_MT) && (defined(_WIN32) || defined(__CYGWIN__))
  79 
  80 #include <windows.h>
  81 
  82 /* MinGW has a convoluted history of supporting MemoryBarrier
  83  * properly.  As such, define a function to wrap the whole
  84  * thing. */
  85 static inline void _HBMemoryBarrier (void) {
  86 #if !defined(MemoryBarrier)
  87   long dummy = 0;
  88   InterlockedExchange (&dummy, 1);
  89 #else
  90   MemoryBarrier ();
  91 #endif
  92 }
  93 
  94 typedef LONG hb_atomic_int_impl_t;
  95 #define HB_ATOMIC_INT_IMPL_INIT(V) (V)
  96 #define hb_atomic_int_impl_add(AI, V)           InterlockedExchangeAdd (&(AI), (V))
  97 
  98 #define hb_atomic_ptr_impl_get(P)               (_HBMemoryBarrier (), (void *) *(P))
  99 #define hb_atomic_ptr_impl_cmpexch(P,O,N)       (InterlockedCompareExchangePointer ((void **) (P), (void *) (N), (void *) (O)) == (void *) (O))
 100 
 101 
 102 #elif !defined(HB_NO_MT) && defined(__APPLE__)
 103 
 104 #include <libkern/OSAtomic.h>
 105 #ifdef __MAC_OS_X_MIN_REQUIRED
 106 #include <AvailabilityMacros.h>
 107 #elif defined(__IPHONE_OS_MIN_REQUIRED)
 108 #include <Availability.h>
 109 #endif
 110 
 111 
 112 typedef int32_t hb_atomic_int_impl_t;
 113 #define HB_ATOMIC_INT_IMPL_INIT(V) (V)
 114 #define hb_atomic_int_impl_add(AI, V)           (OSAtomicAdd32Barrier ((V), &(AI)) - (V))
 115 
 116 #define hb_atomic_ptr_impl_get(P)               (OSMemoryBarrier (), (void *) *(P))
 117 #if (MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4 || __IPHONE_VERSION_MIN_REQUIRED >= 20100)
 118 #define hb_atomic_ptr_impl_cmpexch(P,O,N)       OSAtomicCompareAndSwapPtrBarrier ((void *) (O), (void *) (N), (void **) (P))
 119 #else
 120 #if __ppc64__ || __x86_64__ || __aarch64__
 121 #define hb_atomic_ptr_impl_cmpexch(P,O,N)       OSAtomicCompareAndSwap64Barrier ((int64_t) (O), (int64_t) (N), (int64_t*) (P))
 122 #else
 123 #define hb_atomic_ptr_impl_cmpexch(P,O,N)       OSAtomicCompareAndSwap32Barrier ((int32_t) (O), (int32_t) (N), (int32_t*) (P))
 124 #endif
 125 #endif
 126 
 127 
 128 #elif !defined(HB_NO_MT) && defined(HAVE_INTEL_ATOMIC_PRIMITIVES)
 129 
 130 typedef int hb_atomic_int_impl_t;
 131 #define HB_ATOMIC_INT_IMPL_INIT(V) (V)
 132 #define hb_atomic_int_impl_add(AI, V)           __sync_fetch_and_add (&(AI), (V))
 133 
 134 #define hb_atomic_ptr_impl_get(P)               (void *) (__sync_synchronize (), *(P))
 135 #define hb_atomic_ptr_impl_cmpexch(P,O,N)       __sync_bool_compare_and_swap ((P), (O), (N))
 136 
 137 
 138 #elif !defined(HB_NO_MT) && defined(HAVE_SOLARIS_ATOMIC_OPS)
 139 
 140 #include <atomic.h>
 141 #include <mbarrier.h>
 142 
 143 typedef unsigned int hb_atomic_int_impl_t;
 144 #define HB_ATOMIC_INT_IMPL_INIT(V) (V)
 145 #define hb_atomic_int_impl_add(AI, V)           ( ({__machine_rw_barrier ();}), atomic_add_int_nv (&(AI), (V)) - (V))
 146 
 147 #define hb_atomic_ptr_impl_get(P)               ( ({__machine_rw_barrier ();}), (void *) *(P))
 148 #define hb_atomic_ptr_impl_cmpexch(P,O,N)       ( ({__machine_rw_barrier ();}), atomic_cas_ptr ((void **) (P), (void *) (O), (void *) (N)) == (void *) (O) ? true : false)
 149 
 150 
 151 #elif !defined(HB_NO_MT)
 152 
 153 #define HB_ATOMIC_INT_NIL 1 /* Warn that fallback implementation is in use. */
 154 
 155 typedef volatile int hb_atomic_int_impl_t;
 156 #define HB_ATOMIC_INT_IMPL_INIT(V) (V)
 157 #define hb_atomic_int_impl_add(AI, V)           (((AI) += (V)) - (V))
 158 
 159 #define hb_atomic_ptr_impl_get(P)               ((void *) *(P))
 160 #define hb_atomic_ptr_impl_cmpexch(P,O,N)       (* (void * volatile *) (P) == (void *) (O) ? (* (void * volatile *) (P) = (void *) (N), true) : false)
 161 
 162 
 163 #else /* HB_NO_MT */
 164 
 165 typedef int hb_atomic_int_impl_t;
 166 #define HB_ATOMIC_INT_IMPL_INIT(V)              (V)
 167 #define hb_atomic_int_impl_add(AI, V)           (((AI) += (V)) - (V))
 168 
 169 #define hb_atomic_ptr_impl_get(P)               ((void *) *(P))
 170 #define hb_atomic_ptr_impl_cmpexch(P,O,N)       (* (void **) (P) == (void *) (O) ? (* (void **) (P) = (void *) (N), true) : false)
 171 
 172 
 173 #endif
 174 
 175 
 176 #define HB_ATOMIC_INT_INIT(V)           {HB_ATOMIC_INT_IMPL_INIT(V)}
 177 
 178 struct hb_atomic_int_t
 179 {
 180   hb_atomic_int_impl_t v;
 181 
 182   inline void set_unsafe (int v_) { v = v_; }
 183   inline int get_unsafe (void) const { return v; }
 184   inline int inc (void) { return hb_atomic_int_impl_add (const_cast<hb_atomic_int_impl_t &> (v),  1); }
 185   inline int dec (void) { return hb_atomic_int_impl_add (const_cast<hb_atomic_int_impl_t &> (v), -1); }
 186 };
 187 
 188 
 189 #define hb_atomic_ptr_get(P) hb_atomic_ptr_impl_get(P)
 190 #define hb_atomic_ptr_cmpexch(P,O,N) hb_atomic_ptr_impl_cmpexch((P),(O),(N))
 191 
 192 
 193 #endif /* HB_ATOMIC_PRIVATE_HH */