2 * Copyright © 2007 Chris Wilson
3 * Copyright © 2009,2010 Red Hat, Inc.
4 * Copyright © 2011 Google, Inc.
6 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
27 * Chris Wilson <chris@chris-wilson.co.uk>
28 * Red Hat Author(s): Behdad Esfahbod
29 * Google Author(s): Behdad Esfahbod
32 #ifndef HB_MUTEX_PRIVATE_HH
33 #define HB_MUTEX_PRIVATE_HH
35 #include "hb-private.hh"
42 /* We need external help for these */
48 typedef volatile int hb_atomic_int_t;
49 #define hb_atomic_int_fetch_and_add(AI, V) g_atomic_int_exchange_and_add (&(AI), V)
50 #define hb_atomic_int_get(AI) g_atomic_int_get (&(AI))
51 #define hb_atomic_int_set(AI, V) g_atomic_int_set (&(AI), V)
53 typedef GStaticMutex hb_mutex_t;
54 #define HB_MUTEX_INIT G_STATIC_MUTEX_INIT
55 #define hb_mutex_init(M) g_static_mutex_init (M)
56 #define hb_mutex_lock(M) g_static_mutex_lock (M)
57 #define hb_mutex_trylock(M) g_static_mutex_trylock (M)
58 #define hb_mutex_unlock(M) g_static_mutex_unlock (M)
59 #define hb_mutex_free(M) g_static_mutex_free (M)
62 #elif defined(_MSC_VER)
66 typedef long hb_atomic_int_t;
67 #define hb_atomic_int_fetch_and_add(AI, V) _InterlockedExchangeAdd (&(AI), V)
68 #define hb_atomic_int_get(AI) (_ReadBarrier (), (AI))
69 #define hb_atomic_int_set(AI, V) ((void) _InterlockedExchange (&(AI), (V)))
73 typedef CRITICAL_SECTION hb_mutex_t;
74 #define HB_MUTEX_INIT { NULL, 0, 0, NULL, NULL, 0 }
75 #define hb_mutex_init(M) InitializeCriticalSection (M)
76 #define hb_mutex_lock(M) EnterCriticalSection (M)
77 #define hb_mutex_trylock(M) TryEnterCriticalSection (M)
78 #define hb_mutex_unlock(M) LeaveCriticalSection (M)
79 #define hb_mutex_free(M) DeleteCriticalSection (M)
84 #warning "Could not find any system to define platform macros, library will NOT be thread-safe"
86 typedef volatile int hb_atomic_int_t;
87 #define hb_atomic_int_fetch_and_add(AI, V) ((AI) += (V), (AI) - (V))
88 #define hb_atomic_int_get(AI) (AI)
89 #define hb_atomic_int_set(AI, V) ((void) ((AI) = (V)))
91 typedef volatile int hb_mutex_t;
92 #define HB_MUTEX_INIT 0
93 #define hb_mutex_init(M) ((void) (*(M) = 0))
94 #define hb_mutex_lock(M) ((void) (*(M) = 1))
95 #define hb_mutex_trylock(M) (*(M) = 1, 1)
96 #define hb_mutex_unlock(M) ((void) (*(M) = 0))
97 #define hb_mutex_free(M) ((void) (*(M) = 2))
103 struct hb_static_mutex_t : hb_mutex_t
105 hb_static_mutex_t (void) {
106 hb_mutex_init (this);
114 template <typename item_t>
115 struct hb_threadsafe_set_t
117 hb_set_t <item_t> set;
118 hb_static_mutex_t mutex;
120 template <typename T>
121 inline item_t *insert (T v)
123 hb_mutex_lock (&mutex);
124 item_t *item = set.insert (v);
125 hb_mutex_unlock (&mutex);
129 template <typename T>
130 inline void remove (T v)
132 hb_mutex_lock (&mutex);
134 hb_mutex_unlock (&mutex);
137 template <typename T>
138 inline item_t *find (T v)
140 hb_mutex_lock (&mutex);
141 item_t *item = set.find (v);
142 hb_mutex_unlock (&mutex);
146 template <typename T>
147 inline item_t *find_or_insert (T v) {
148 hb_mutex_lock (&mutex);
149 item_t *item = set.find_or_insert (v);
150 hb_mutex_unlock (&mutex);
155 hb_mutex_lock (&mutex);
157 hb_mutex_unlock (&mutex);
167 #endif /* HB_MUTEX_PRIVATE_HH */