michael@0: /* michael@0: * Copyright © 2007 Chris Wilson michael@0: * Copyright © 2009,2010 Red Hat, Inc. michael@0: * Copyright © 2011,2012 Google, Inc. michael@0: * michael@0: * This is part of HarfBuzz, a text shaping library. michael@0: * michael@0: * Permission is hereby granted, without written agreement and without michael@0: * license or royalty fees, to use, copy, modify, and distribute this michael@0: * software and its documentation for any purpose, provided that the michael@0: * above copyright notice and the following two paragraphs appear in michael@0: * all copies of this software. michael@0: * michael@0: * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR michael@0: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES michael@0: * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN michael@0: * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH michael@0: * DAMAGE. michael@0: * michael@0: * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, michael@0: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND michael@0: * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS michael@0: * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO michael@0: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. michael@0: * michael@0: * Contributor(s): michael@0: * Chris Wilson michael@0: * Red Hat Author(s): Behdad Esfahbod michael@0: * Google Author(s): Behdad Esfahbod michael@0: */ michael@0: michael@0: #ifndef HB_ATOMIC_PRIVATE_HH michael@0: #define HB_ATOMIC_PRIVATE_HH michael@0: michael@0: #include "hb-private.hh" michael@0: michael@0: michael@0: /* atomic_int */ michael@0: michael@0: /* We need external help for these */ michael@0: michael@0: #if 0 michael@0: michael@0: michael@0: #elif !defined(HB_NO_MT) && (defined(_WIN32) || defined(__CYGWIN__)) michael@0: michael@0: #define WIN32_LEAN_AND_MEAN michael@0: #include michael@0: michael@0: #if defined(__MINGW32__) && !defined(MemoryBarrier) michael@0: static inline void _HBMemoryBarrier (void) { michael@0: long dummy = 0; michael@0: InterlockedExchange (&dummy, 1); michael@0: } michael@0: # define MemoryBarrier _HBMemoryBarrier michael@0: #endif michael@0: michael@0: typedef LONG hb_atomic_int_t; michael@0: #define hb_atomic_int_add(AI, V) InterlockedExchangeAdd (&(AI), (V)) michael@0: michael@0: #define hb_atomic_ptr_get(P) (MemoryBarrier (), (void *) *(P)) michael@0: #define hb_atomic_ptr_cmpexch(P,O,N) (InterlockedCompareExchangePointer ((void **) (P), (void *) (N), (void *) (O)) == (void *) (O)) michael@0: michael@0: michael@0: #elif !defined(HB_NO_MT) && defined(__APPLE__) michael@0: michael@0: #include michael@0: #ifdef __MAC_OS_X_MIN_REQUIRED michael@0: #include michael@0: #elif defined(__IPHONE_OS_MIN_REQUIRED) michael@0: #include michael@0: #endif michael@0: michael@0: typedef int32_t hb_atomic_int_t; michael@0: #define hb_atomic_int_add(AI, V) (OSAtomicAdd32Barrier ((V), &(AI)) - (V)) michael@0: michael@0: #define hb_atomic_ptr_get(P) (OSMemoryBarrier (), (void *) *(P)) michael@0: #if (MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4 || __IPHONE_VERSION_MIN_REQUIRED >= 20100) michael@0: #define hb_atomic_ptr_cmpexch(P,O,N) OSAtomicCompareAndSwapPtrBarrier ((void *) (O), (void *) (N), (void **) (P)) michael@0: #else michael@0: #if __ppc64__ || __x86_64__ || __arm64__ michael@0: #define hb_atomic_ptr_cmpexch(P,O,N) OSAtomicCompareAndSwap64Barrier ((int64_t) (O), (int64_t) (N), (int64_t*) (P)) michael@0: #else michael@0: #define hb_atomic_ptr_cmpexch(P,O,N) OSAtomicCompareAndSwap32Barrier ((int32_t) (O), (int32_t) (N), (int32_t*) (P)) michael@0: #endif michael@0: #endif michael@0: michael@0: michael@0: #elif !defined(HB_NO_MT) && defined(HAVE_INTEL_ATOMIC_PRIMITIVES) michael@0: michael@0: typedef int hb_atomic_int_t; michael@0: #define hb_atomic_int_add(AI, V) __sync_fetch_and_add (&(AI), (V)) michael@0: michael@0: #define hb_atomic_ptr_get(P) (void *) (__sync_synchronize (), *(P)) michael@0: #define hb_atomic_ptr_cmpexch(P,O,N) __sync_bool_compare_and_swap ((P), (O), (N)) michael@0: michael@0: michael@0: #elif !defined(HB_NO_MT) && defined(HAVE_SOLARIS_ATOMIC_OPS) michael@0: michael@0: #include michael@0: #include michael@0: michael@0: typedef unsigned int hb_atomic_int_t; michael@0: #define hb_atomic_int_add(AI, V) ( ({__machine_rw_barrier ();}), atomic_add_int_nv (&(AI), (V)) - (V)) michael@0: michael@0: #define hb_atomic_ptr_get(P) ( ({__machine_rw_barrier ();}), (void *) *(P)) michael@0: #define hb_atomic_ptr_cmpexch(P,O,N) ( ({__machine_rw_barrier ();}), atomic_cas_ptr ((void **) (P), (void *) (O), (void *) (N)) == (void *) (O) ? true : false) michael@0: michael@0: michael@0: #elif !defined(HB_NO_MT) michael@0: michael@0: #define HB_ATOMIC_INT_NIL 1 /* Warn that fallback implementation is in use. */ michael@0: typedef volatile int hb_atomic_int_t; michael@0: #define hb_atomic_int_add(AI, V) (((AI) += (V)) - (V)) michael@0: michael@0: #define hb_atomic_ptr_get(P) ((void *) *(P)) michael@0: #define hb_atomic_ptr_cmpexch(P,O,N) (* (void * volatile *) (P) == (void *) (O) ? (* (void * volatile *) (P) = (void *) (N), true) : false) michael@0: michael@0: michael@0: #else /* HB_NO_MT */ michael@0: michael@0: typedef int hb_atomic_int_t; michael@0: #define hb_atomic_int_add(AI, V) (((AI) += (V)) - (V)) michael@0: michael@0: #define hb_atomic_ptr_get(P) ((void *) *(P)) michael@0: #define hb_atomic_ptr_cmpexch(P,O,N) (* (void **) (P) == (void *) (O) ? (* (void **) (P) = (void *) (N), true) : false) michael@0: michael@0: #endif michael@0: michael@0: /* TODO Add tracing. */ michael@0: michael@0: #endif /* HB_ATOMIC_PRIVATE_HH */