1ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod/*
2ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod * Copyright © 2007  Chris Wilson
3ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod * Copyright © 2009,2010  Red Hat, Inc.
434961e3198e27fa37fd4cfdad12ef86a2e9e51c2Behdad Esfahbod * Copyright © 2011,2012  Google, Inc.
5ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod *
6ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod *  This is part of HarfBuzz, a text shaping library.
7ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod *
8ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod * Permission is hereby granted, without written agreement and without
9ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod * license or royalty fees, to use, copy, modify, and distribute this
10ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod * software and its documentation for any purpose, provided that the
11ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod * above copyright notice and the following two paragraphs appear in
12ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod * all copies of this software.
13ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod *
14ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
15ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
17ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod * DAMAGE.
19ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod *
20ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
21ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
23ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
24ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod *
26ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod * Contributor(s):
27ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod *	Chris Wilson <chris@chris-wilson.co.uk>
28ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod * Red Hat Author(s): Behdad Esfahbod
29ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod * Google Author(s): Behdad Esfahbod
30ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod */
31ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod
32ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod#ifndef HB_ATOMIC_PRIVATE_HH
33ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod#define HB_ATOMIC_PRIVATE_HH
34ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod
35ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod#include "hb-private.hh"
36ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod
37ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod
38ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod/* atomic_int */
39ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod
40ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod/* We need external help for these */
41ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod
42022a05ae90f30bcddff413022e0cd801809b5390Behdad Esfahbod#if 0
43ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod
44022a05ae90f30bcddff413022e0cd801809b5390Behdad Esfahbod
45a6c1e040e594faeefd61b456ef995c7886cdea78Behdad Esfahbod#elif !defined(HB_NO_MT) && (defined(_WIN32) || defined(__CYGWIN__))
4652ff2681d88886e2165ced397966ba29c2073583Behdad Esfahbod
4752ff2681d88886e2165ced397966ba29c2073583Behdad Esfahbod#define WIN32_LEAN_AND_MEAN
4852ff2681d88886e2165ced397966ba29c2073583Behdad Esfahbod#include <windows.h>
4952ff2681d88886e2165ced397966ba29c2073583Behdad Esfahbod
5011d2956553f0d4a0086166a04ffc352fcfacf56eBehdad Esfahbodtypedef LONG hb_atomic_int_t;
5152ff2681d88886e2165ced397966ba29c2073583Behdad Esfahbod#define hb_atomic_int_add(AI, V)	InterlockedExchangeAdd (&(AI), (V))
52ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod
532761e8a632c14353f286708898be8df6ebad7407Behdad Esfahbod#define hb_atomic_ptr_get(P)		(MemoryBarrier (), (void *) *(P))
5452ff2681d88886e2165ced397966ba29c2073583Behdad Esfahbod#define hb_atomic_ptr_cmpexch(P,O,N)	(InterlockedCompareExchangePointer ((void **) (P), (void *) (N), (void *) (O)) == (void *) (O))
556843ce01be0df501ef3149a2c1c54cdfb693195dBehdad Esfahbod
5634961e3198e27fa37fd4cfdad12ef86a2e9e51c2Behdad Esfahbod
57ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod#elif !defined(HB_NO_MT) && defined(__APPLE__)
58ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod
59ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod#include <libkern/OSAtomic.h>
600e9f0f3e5f16a45951423a1229af42dc2fd798c3Behdad Esfahbod#ifdef __MAC_OS_X_MIN_REQUIRED
61071d5b831e6de5f3b24160dc77b139cb040ab886Behdad Esfahbod#include <AvailabilityMacros.h>
620e9f0f3e5f16a45951423a1229af42dc2fd798c3Behdad Esfahbod#elif defined(__IPHONE_OS_MIN_REQUIRED)
630e9f0f3e5f16a45951423a1229af42dc2fd798c3Behdad Esfahbod#include <Availability.h>
640e9f0f3e5f16a45951423a1229af42dc2fd798c3Behdad Esfahbod#endif
656843ce01be0df501ef3149a2c1c54cdfb693195dBehdad Esfahbod
66ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbodtypedef int32_t hb_atomic_int_t;
67a3547330fa88e30a138f6f17e60d9c7d1e316622Behdad Esfahbod#define hb_atomic_int_add(AI, V)	(OSAtomicAdd32Barrier ((V), &(AI)) - (V))
68ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod
696843ce01be0df501ef3149a2c1c54cdfb693195dBehdad Esfahbod#define hb_atomic_ptr_get(P)		(OSMemoryBarrier (), (void *) *(P))
700e9f0f3e5f16a45951423a1229af42dc2fd798c3Behdad Esfahbod#if (MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4 || __IPHONE_VERSION_MIN_REQUIRED >= 20100)
7104bc1eebe7a304c0e6f86ab6814c65889f152602Behdad Esfahbod#define hb_atomic_ptr_cmpexch(P,O,N)	OSAtomicCompareAndSwapPtrBarrier ((void *) (O), (void *) (N), (void **) (P))
72071d5b831e6de5f3b24160dc77b139cb040ab886Behdad Esfahbod#else
73071d5b831e6de5f3b24160dc77b139cb040ab886Behdad Esfahbod#if __ppc64__ || __x86_64__
74071d5b831e6de5f3b24160dc77b139cb040ab886Behdad Esfahbod#define hb_atomic_ptr_cmpexch(P,O,N)    OSAtomicCompareAndSwap64Barrier ((int64_t) (O), (int64_t) (N), (int64_t*) (P))
75071d5b831e6de5f3b24160dc77b139cb040ab886Behdad Esfahbod#else
76071d5b831e6de5f3b24160dc77b139cb040ab886Behdad Esfahbod#define hb_atomic_ptr_cmpexch(P,O,N)    OSAtomicCompareAndSwap32Barrier ((int32_t) (O), (int32_t) (N), (int32_t*) (P))
77071d5b831e6de5f3b24160dc77b139cb040ab886Behdad Esfahbod#endif
78071d5b831e6de5f3b24160dc77b139cb040ab886Behdad Esfahbod#endif
796843ce01be0df501ef3149a2c1c54cdfb693195dBehdad Esfahbod
8034961e3198e27fa37fd4cfdad12ef86a2e9e51c2Behdad Esfahbod
8112f5c0a222a2f0aebe63c0d367937a0ff985474aBehdad Esfahbod#elif !defined(HB_NO_MT) && defined(HAVE_INTEL_ATOMIC_PRIMITIVES)
82d970d2899b36a2fbd002b224b8bd37b0906fdd5fBehdad Esfahbod
83d970d2899b36a2fbd002b224b8bd37b0906fdd5fBehdad Esfahbodtypedef int hb_atomic_int_t;
84d970d2899b36a2fbd002b224b8bd37b0906fdd5fBehdad Esfahbod#define hb_atomic_int_add(AI, V)	__sync_fetch_and_add (&(AI), (V))
85d970d2899b36a2fbd002b224b8bd37b0906fdd5fBehdad Esfahbod
866843ce01be0df501ef3149a2c1c54cdfb693195dBehdad Esfahbod#define hb_atomic_ptr_get(P)		(void *) (__sync_synchronize (), *(P))
876843ce01be0df501ef3149a2c1c54cdfb693195dBehdad Esfahbod#define hb_atomic_ptr_cmpexch(P,O,N)	__sync_bool_compare_and_swap ((P), (O), (N))
886843ce01be0df501ef3149a2c1c54cdfb693195dBehdad Esfahbod
8934961e3198e27fa37fd4cfdad12ef86a2e9e51c2Behdad Esfahbod
902dcb333f52492018816f0d5d67a47634a612e49cBehdad Esfahbod#elif !defined(HB_NO_MT) && defined(HAVE_SOLARIS_ATOMIC_OPS)
912dcb333f52492018816f0d5d67a47634a612e49cBehdad Esfahbod
922dcb333f52492018816f0d5d67a47634a612e49cBehdad Esfahbod#include <atomic.h>
932dcb333f52492018816f0d5d67a47634a612e49cBehdad Esfahbod#include <mbarrier.h>
942dcb333f52492018816f0d5d67a47634a612e49cBehdad Esfahbod
95de649f07f1f8fdeb998a0b049759f4f7363e25d2Behdad Esfahbodtypedef unsigned int hb_atomic_int_t;
96de649f07f1f8fdeb998a0b049759f4f7363e25d2Behdad Esfahbod#define hb_atomic_int_add(AI, V)	( ({__machine_rw_barrier ();}), atomic_add_int_nv (&(AI), (V)) - (V))
972dcb333f52492018816f0d5d67a47634a612e49cBehdad Esfahbod
98de649f07f1f8fdeb998a0b049759f4f7363e25d2Behdad Esfahbod#define hb_atomic_ptr_get(P)		( ({__machine_rw_barrier ();}), (void *) *(P))
99de649f07f1f8fdeb998a0b049759f4f7363e25d2Behdad Esfahbod#define hb_atomic_ptr_cmpexch(P,O,N)	( ({__machine_rw_barrier ();}), atomic_cas_ptr ((void **) (P), (void *) (O), (void *) (N)) == (void *) (O) ? true : false)
1002dcb333f52492018816f0d5d67a47634a612e49cBehdad Esfahbod
1012dcb333f52492018816f0d5d67a47634a612e49cBehdad Esfahbod
102cdafe3a7d8483ac586e2c16487e2a09164e0f65cBehdad Esfahbod#elif !defined(HB_NO_MT)
103cdafe3a7d8483ac586e2c16487e2a09164e0f65cBehdad Esfahbod
104cdafe3a7d8483ac586e2c16487e2a09164e0f65cBehdad Esfahbod#define HB_ATOMIC_INT_NIL 1 /* Warn that fallback implementation is in use. */
105cdafe3a7d8483ac586e2c16487e2a09164e0f65cBehdad Esfahbodtypedef volatile int hb_atomic_int_t;
106cdafe3a7d8483ac586e2c16487e2a09164e0f65cBehdad Esfahbod#define hb_atomic_int_add(AI, V)	(((AI) += (V)) - (V))
107cdafe3a7d8483ac586e2c16487e2a09164e0f65cBehdad Esfahbod
1086843ce01be0df501ef3149a2c1c54cdfb693195dBehdad Esfahbod#define hb_atomic_ptr_get(P)		((void *) *(P))
1090594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod#define hb_atomic_ptr_cmpexch(P,O,N)	(* (void * volatile *) (P) == (void *) (O) ? (* (void * volatile *) (P) = (void *) (N), true) : false)
1106843ce01be0df501ef3149a2c1c54cdfb693195dBehdad Esfahbod
111cdafe3a7d8483ac586e2c16487e2a09164e0f65cBehdad Esfahbod
112cdafe3a7d8483ac586e2c16487e2a09164e0f65cBehdad Esfahbod#else /* HB_NO_MT */
113ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod
114d970d2899b36a2fbd002b224b8bd37b0906fdd5fBehdad Esfahbodtypedef int hb_atomic_int_t;
115819faa05307aa192015f4b43d8103a35e87d6cc7Behdad Esfahbod#define hb_atomic_int_add(AI, V)	(((AI) += (V)) - (V))
116ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod
1176843ce01be0df501ef3149a2c1c54cdfb693195dBehdad Esfahbod#define hb_atomic_ptr_get(P)		((void *) *(P))
1180594a2448440208efa0acac9a5d8d52d43108289Behdad Esfahbod#define hb_atomic_ptr_cmpexch(P,O,N)	(* (void **) (P) == (void *) (O) ? (* (void **) (P) = (void *) (N), true) : false)
1196843ce01be0df501ef3149a2c1c54cdfb693195dBehdad Esfahbod
120ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod#endif
121ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod
122cdafe3a7d8483ac586e2c16487e2a09164e0f65cBehdad Esfahbod/* TODO Add tracing. */
123ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod
124ec3ba4b96fc4f262db1ff9f906628c32f26c9b7dBehdad Esfahbod#endif /* HB_ATOMIC_PRIVATE_HH */
125