Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright © 2007 Chris Wilson |
michael@0 | 3 | * Copyright © 2009,2010 Red Hat, Inc. |
michael@0 | 4 | * Copyright © 2011,2012 Google, Inc. |
michael@0 | 5 | * |
michael@0 | 6 | * This is part of HarfBuzz, a text shaping library. |
michael@0 | 7 | * |
michael@0 | 8 | * Permission is hereby granted, without written agreement and without |
michael@0 | 9 | * license or royalty fees, to use, copy, modify, and distribute this |
michael@0 | 10 | * software and its documentation for any purpose, provided that the |
michael@0 | 11 | * above copyright notice and the following two paragraphs appear in |
michael@0 | 12 | * all copies of this software. |
michael@0 | 13 | * |
michael@0 | 14 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
michael@0 | 15 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
michael@0 | 16 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
michael@0 | 17 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
michael@0 | 18 | * DAMAGE. |
michael@0 | 19 | * |
michael@0 | 20 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
michael@0 | 21 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
michael@0 | 22 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
michael@0 | 23 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
michael@0 | 24 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
michael@0 | 25 | * |
michael@0 | 26 | * Contributor(s): |
michael@0 | 27 | * Chris Wilson <chris@chris-wilson.co.uk> |
michael@0 | 28 | * Red Hat Author(s): Behdad Esfahbod |
michael@0 | 29 | * Google Author(s): Behdad Esfahbod |
michael@0 | 30 | */ |
michael@0 | 31 | |
michael@0 | 32 | #ifndef HB_ATOMIC_PRIVATE_HH |
michael@0 | 33 | #define HB_ATOMIC_PRIVATE_HH |
michael@0 | 34 | |
michael@0 | 35 | #include "hb-private.hh" |
michael@0 | 36 | |
michael@0 | 37 | |
michael@0 | 38 | /* atomic_int */ |
michael@0 | 39 | |
michael@0 | 40 | /* We need external help for these */ |
michael@0 | 41 | |
michael@0 | 42 | #if 0 |
michael@0 | 43 | |
michael@0 | 44 | |
michael@0 | 45 | #elif !defined(HB_NO_MT) && (defined(_WIN32) || defined(__CYGWIN__)) |
michael@0 | 46 | |
michael@0 | 47 | #define WIN32_LEAN_AND_MEAN |
michael@0 | 48 | #include <windows.h> |
michael@0 | 49 | |
michael@0 | 50 | #if defined(__MINGW32__) && !defined(MemoryBarrier) |
michael@0 | 51 | static inline void _HBMemoryBarrier (void) { |
michael@0 | 52 | long dummy = 0; |
michael@0 | 53 | InterlockedExchange (&dummy, 1); |
michael@0 | 54 | } |
michael@0 | 55 | # define MemoryBarrier _HBMemoryBarrier |
michael@0 | 56 | #endif |
michael@0 | 57 | |
michael@0 | 58 | typedef LONG hb_atomic_int_t; |
michael@0 | 59 | #define hb_atomic_int_add(AI, V) InterlockedExchangeAdd (&(AI), (V)) |
michael@0 | 60 | |
michael@0 | 61 | #define hb_atomic_ptr_get(P) (MemoryBarrier (), (void *) *(P)) |
michael@0 | 62 | #define hb_atomic_ptr_cmpexch(P,O,N) (InterlockedCompareExchangePointer ((void **) (P), (void *) (N), (void *) (O)) == (void *) (O)) |
michael@0 | 63 | |
michael@0 | 64 | |
michael@0 | 65 | #elif !defined(HB_NO_MT) && defined(__APPLE__) |
michael@0 | 66 | |
michael@0 | 67 | #include <libkern/OSAtomic.h> |
michael@0 | 68 | #ifdef __MAC_OS_X_MIN_REQUIRED |
michael@0 | 69 | #include <AvailabilityMacros.h> |
michael@0 | 70 | #elif defined(__IPHONE_OS_MIN_REQUIRED) |
michael@0 | 71 | #include <Availability.h> |
michael@0 | 72 | #endif |
michael@0 | 73 | |
michael@0 | 74 | typedef int32_t hb_atomic_int_t; |
michael@0 | 75 | #define hb_atomic_int_add(AI, V) (OSAtomicAdd32Barrier ((V), &(AI)) - (V)) |
michael@0 | 76 | |
michael@0 | 77 | #define hb_atomic_ptr_get(P) (OSMemoryBarrier (), (void *) *(P)) |
michael@0 | 78 | #if (MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4 || __IPHONE_VERSION_MIN_REQUIRED >= 20100) |
michael@0 | 79 | #define hb_atomic_ptr_cmpexch(P,O,N) OSAtomicCompareAndSwapPtrBarrier ((void *) (O), (void *) (N), (void **) (P)) |
michael@0 | 80 | #else |
michael@0 | 81 | #if __ppc64__ || __x86_64__ || __arm64__ |
michael@0 | 82 | #define hb_atomic_ptr_cmpexch(P,O,N) OSAtomicCompareAndSwap64Barrier ((int64_t) (O), (int64_t) (N), (int64_t*) (P)) |
michael@0 | 83 | #else |
michael@0 | 84 | #define hb_atomic_ptr_cmpexch(P,O,N) OSAtomicCompareAndSwap32Barrier ((int32_t) (O), (int32_t) (N), (int32_t*) (P)) |
michael@0 | 85 | #endif |
michael@0 | 86 | #endif |
michael@0 | 87 | |
michael@0 | 88 | |
michael@0 | 89 | #elif !defined(HB_NO_MT) && defined(HAVE_INTEL_ATOMIC_PRIMITIVES) |
michael@0 | 90 | |
michael@0 | 91 | typedef int hb_atomic_int_t; |
michael@0 | 92 | #define hb_atomic_int_add(AI, V) __sync_fetch_and_add (&(AI), (V)) |
michael@0 | 93 | |
michael@0 | 94 | #define hb_atomic_ptr_get(P) (void *) (__sync_synchronize (), *(P)) |
michael@0 | 95 | #define hb_atomic_ptr_cmpexch(P,O,N) __sync_bool_compare_and_swap ((P), (O), (N)) |
michael@0 | 96 | |
michael@0 | 97 | |
michael@0 | 98 | #elif !defined(HB_NO_MT) && defined(HAVE_SOLARIS_ATOMIC_OPS) |
michael@0 | 99 | |
michael@0 | 100 | #include <atomic.h> |
michael@0 | 101 | #include <mbarrier.h> |
michael@0 | 102 | |
michael@0 | 103 | typedef unsigned int hb_atomic_int_t; |
michael@0 | 104 | #define hb_atomic_int_add(AI, V) ( ({__machine_rw_barrier ();}), atomic_add_int_nv (&(AI), (V)) - (V)) |
michael@0 | 105 | |
michael@0 | 106 | #define hb_atomic_ptr_get(P) ( ({__machine_rw_barrier ();}), (void *) *(P)) |
michael@0 | 107 | #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 | 108 | |
michael@0 | 109 | |
michael@0 | 110 | #elif !defined(HB_NO_MT) |
michael@0 | 111 | |
michael@0 | 112 | #define HB_ATOMIC_INT_NIL 1 /* Warn that fallback implementation is in use. */ |
michael@0 | 113 | typedef volatile int hb_atomic_int_t; |
michael@0 | 114 | #define hb_atomic_int_add(AI, V) (((AI) += (V)) - (V)) |
michael@0 | 115 | |
michael@0 | 116 | #define hb_atomic_ptr_get(P) ((void *) *(P)) |
michael@0 | 117 | #define hb_atomic_ptr_cmpexch(P,O,N) (* (void * volatile *) (P) == (void *) (O) ? (* (void * volatile *) (P) = (void *) (N), true) : false) |
michael@0 | 118 | |
michael@0 | 119 | |
michael@0 | 120 | #else /* HB_NO_MT */ |
michael@0 | 121 | |
michael@0 | 122 | typedef int hb_atomic_int_t; |
michael@0 | 123 | #define hb_atomic_int_add(AI, V) (((AI) += (V)) - (V)) |
michael@0 | 124 | |
michael@0 | 125 | #define hb_atomic_ptr_get(P) ((void *) *(P)) |
michael@0 | 126 | #define hb_atomic_ptr_cmpexch(P,O,N) (* (void **) (P) == (void *) (O) ? (* (void **) (P) = (void *) (N), true) : false) |
michael@0 | 127 | |
michael@0 | 128 | #endif |
michael@0 | 129 | |
michael@0 | 130 | /* TODO Add tracing. */ |
michael@0 | 131 | |
michael@0 | 132 | #endif /* HB_ATOMIC_PRIVATE_HH */ |