blob: 0f70641f490e626086dcdc15738389c7503149c2 [file] [log] [blame]
Behdad Esfahbodec3ba4b2012-05-17 20:30:46 -04001/*
2 * Copyright © 2007 Chris Wilson
3 * Copyright © 2009,2010 Red Hat, Inc.
Behdad Esfahbod34961e32012-05-17 20:50:38 -04004 * Copyright © 2011,2012 Google, Inc.
Behdad Esfahbodec3ba4b2012-05-17 20:30:46 -04005 *
6 * This is part of HarfBuzz, a text shaping library.
7 *
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.
13 *
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
18 * DAMAGE.
19 *
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.
25 *
26 * Contributor(s):
27 * Chris Wilson <chris@chris-wilson.co.uk>
28 * Red Hat Author(s): Behdad Esfahbod
29 * Google Author(s): Behdad Esfahbod
30 */
31
32#ifndef HB_ATOMIC_PRIVATE_HH
33#define HB_ATOMIC_PRIVATE_HH
34
35#include "hb-private.hh"
36
37
38/* atomic_int */
39
40/* We need external help for these */
41
Behdad Esfahbod022a05a2012-05-17 21:53:24 -040042#if 0
Behdad Esfahbod34961e32012-05-17 20:50:38 -040043
Behdad Esfahbod022a05a2012-05-17 21:53:24 -040044
45#elif !defined(HB_NO_MT) && defined(_MSC_VER) && _MSC_VER >= 1600
Behdad Esfahbod34961e32012-05-17 20:50:38 -040046
47#include <intrin.h>
Behdad Esfahbod391f1ff2012-07-13 09:04:07 -040048/* On x86, _InterlockedCompareExchangePointer is a macro defined in concrt.h */
Behdad Esfahbod391f1ff2012-07-13 09:04:07 -040049#include <concrt.h>
Behdad Esfahbod6843ce02012-06-05 17:27:20 -040050
Behdad Esfahbod34961e32012-05-17 20:50:38 -040051typedef long hb_atomic_int_t;
Behdad Esfahbod22afd662012-05-17 21:23:49 -040052#define hb_atomic_int_add(AI, V) _InterlockedExchangeAdd (&(AI), (V))
Behdad Esfahbod34961e32012-05-17 20:50:38 -040053
Behdad Esfahbod6843ce02012-06-05 17:27:20 -040054#define hb_atomic_ptr_get(P) (MemoryBarrier (), (void *) *(P))
Behdad Esfahbod04bc1ee2012-06-05 20:16:56 -040055#define hb_atomic_ptr_cmpexch(P,O,N) (_InterlockedCompareExchangePointer ((void **) (P), (void *) (N), (void *) (O)) == (void *) (O))
Behdad Esfahbod6843ce02012-06-05 17:27:20 -040056
Behdad Esfahbod34961e32012-05-17 20:50:38 -040057
58#elif !defined(HB_NO_MT) && defined(__APPLE__)
59
60#include <libkern/OSAtomic.h>
Behdad Esfahbod6843ce02012-06-05 17:27:20 -040061
Behdad Esfahbod34961e32012-05-17 20:50:38 -040062typedef int32_t hb_atomic_int_t;
Behdad Esfahboda3547332012-05-27 10:20:47 -040063#define hb_atomic_int_add(AI, V) (OSAtomicAdd32Barrier ((V), &(AI)) - (V))
Behdad Esfahbod34961e32012-05-17 20:50:38 -040064
Behdad Esfahbod6843ce02012-06-05 17:27:20 -040065#define hb_atomic_ptr_get(P) (OSMemoryBarrier (), (void *) *(P))
Behdad Esfahbod04bc1ee2012-06-05 20:16:56 -040066#define hb_atomic_ptr_cmpexch(P,O,N) OSAtomicCompareAndSwapPtrBarrier ((void *) (O), (void *) (N), (void **) (P))
Behdad Esfahbod6843ce02012-06-05 17:27:20 -040067
Behdad Esfahbod34961e32012-05-17 20:50:38 -040068
Behdad Esfahbod12f5c0a2012-06-26 11:16:13 -040069#elif !defined(HB_NO_MT) && defined(HAVE_INTEL_ATOMIC_PRIMITIVES)
Behdad Esfahbodd970d282012-06-05 16:06:28 -040070
71typedef int hb_atomic_int_t;
72#define hb_atomic_int_add(AI, V) __sync_fetch_and_add (&(AI), (V))
73
Behdad Esfahbod6843ce02012-06-05 17:27:20 -040074#define hb_atomic_ptr_get(P) (void *) (__sync_synchronize (), *(P))
75#define hb_atomic_ptr_cmpexch(P,O,N) __sync_bool_compare_and_swap ((P), (O), (N))
76
Behdad Esfahbod34961e32012-05-17 20:50:38 -040077#elif !defined(HB_NO_MT) && defined(HAVE_GLIB)
Behdad Esfahbodec3ba4b2012-05-17 20:30:46 -040078
79#include <glib.h>
Behdad Esfahbodd970d282012-06-05 16:06:28 -040080typedef int hb_atomic_int_t;
Behdad Esfahbodec3ba4b2012-05-17 20:30:46 -040081#if GLIB_CHECK_VERSION(2,29,5)
Behdad Esfahbod22afd662012-05-17 21:23:49 -040082#define hb_atomic_int_add(AI, V) g_atomic_int_add (&(AI), (V))
Behdad Esfahbodec3ba4b2012-05-17 20:30:46 -040083#else
Behdad Esfahbod22afd662012-05-17 21:23:49 -040084#define hb_atomic_int_add(AI, V) g_atomic_int_exchange_and_add (&(AI), (V))
Behdad Esfahbodec3ba4b2012-05-17 20:30:46 -040085#endif
Behdad Esfahbodec3ba4b2012-05-17 20:30:46 -040086
Behdad Esfahbod6843ce02012-06-05 17:27:20 -040087#define hb_atomic_ptr_get(P) g_atomic_pointer_get (P)
Behdad Esfahbod04bc1ee2012-06-05 20:16:56 -040088#define hb_atomic_ptr_cmpexch(P,O,N) g_atomic_pointer_compare_and_exchange ((void **) (P), (void *) (O), (void *) (N))
Behdad Esfahbod6843ce02012-06-05 17:27:20 -040089
Behdad Esfahbodec3ba4b2012-05-17 20:30:46 -040090
Behdad Esfahbodcdafe3a2012-06-05 16:34:49 -040091#elif !defined(HB_NO_MT)
Behdad Esfahbodec3ba4b2012-05-17 20:30:46 -040092
Behdad Esfahbodcdafe3a2012-06-05 16:34:49 -040093#define HB_ATOMIC_INT_NIL 1 /* Warn that fallback implementation is in use. */
94typedef volatile int hb_atomic_int_t;
95#define hb_atomic_int_add(AI, V) (((AI) += (V)) - (V))
96
Behdad Esfahbod6843ce02012-06-05 17:27:20 -040097#define hb_atomic_ptr_get(P) ((void *) *(P))
Behdad Esfahbod0594a242012-06-05 20:35:40 -040098#define hb_atomic_ptr_cmpexch(P,O,N) (* (void * volatile *) (P) == (void *) (O) ? (* (void * volatile *) (P) = (void *) (N), true) : false)
Behdad Esfahbod6843ce02012-06-05 17:27:20 -040099
Behdad Esfahbodcdafe3a2012-06-05 16:34:49 -0400100
101#else /* HB_NO_MT */
102
Behdad Esfahbodd970d282012-06-05 16:06:28 -0400103typedef int hb_atomic_int_t;
Behdad Esfahbod819faa02012-05-27 10:09:18 -0400104#define hb_atomic_int_add(AI, V) (((AI) += (V)) - (V))
Behdad Esfahbodec3ba4b2012-05-17 20:30:46 -0400105
Behdad Esfahbod6843ce02012-06-05 17:27:20 -0400106#define hb_atomic_ptr_get(P) ((void *) *(P))
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400107#define hb_atomic_ptr_cmpexch(P,O,N) (* (void **) (P) == (void *) (O) ? (* (void **) (P) = (void *) (N), true) : false)
Behdad Esfahbod6843ce02012-06-05 17:27:20 -0400108
Behdad Esfahbodec3ba4b2012-05-17 20:30:46 -0400109#endif
110
Behdad Esfahbodcdafe3a2012-06-05 16:34:49 -0400111/* TODO Add tracing. */
Behdad Esfahbodec3ba4b2012-05-17 20:30:46 -0400112
113#endif /* HB_ATOMIC_PRIVATE_HH */