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 GStaticMutex hb_mutex_impl_t;
49 #define HB_MUTEX_IMPL_INIT G_STATIC_MUTEX_INIT
50 #define hb_mutex_impl_init(M) g_static_mutex_init (M)
51 #define hb_mutex_impl_lock(M) g_static_mutex_lock (M)
52 #define hb_mutex_impl_unlock(M) g_static_mutex_unlock (M)
53 #define hb_mutex_impl_free(M) g_static_mutex_free (M)
56 #elif defined(_MSC_VER)
60 typedef CRITICAL_SECTION hb_mutex_impl_t;
61 #define HB_MUTEX_IMPL_INIT { NULL, 0, 0, NULL, NULL, 0 }
62 #define hb_mutex_impl_init(M) InitializeCriticalSection (M)
63 #define hb_mutex_impl_lock(M) EnterCriticalSection (M)
64 #define hb_mutex_impl_unlock(M) LeaveCriticalSection (M)
65 #define hb_mutex_impl_free(M) DeleteCriticalSection (M)
70 #warning "Could not find any system to define platform macros, library will NOT be thread-safe"
72 typedef volatile int hb_mutex_impl_t;
73 #define HB_MUTEX_IMPL_INIT 0
74 #define hb_mutex_impl_init(M) ((void) (*(M) = 0))
75 #define hb_mutex_impl_lock(M) ((void) (*(M) = 1))
76 #define hb_mutex_impl_unlock(M) ((void) (*(M) = 0))
77 #define hb_mutex_impl_free(M) ((void) (*(M) = 2))
87 inline void init (void) { hb_mutex_impl_init (&m); }
88 inline void lock (void) { hb_mutex_impl_lock (&m); }
89 inline void unlock (void) { hb_mutex_impl_unlock (&m); }
90 inline void free (void) { hb_mutex_impl_free (&m); }
93 #define HB_MUTEX_INIT {HB_MUTEX_IMPL_INIT}
94 #define hb_mutex_init(M) (M)->init ()
95 #define hb_mutex_lock(M) (M)->lock ()
96 #define hb_mutex_unlock(M) (M)->unlock ()
97 #define hb_mutex_free(M) (M)->free ()
100 struct hb_static_mutex_t : hb_mutex_t
102 hb_static_mutex_t (void) { this->init (); }
103 ~hb_static_mutex_t (void) { this->free (); }
106 NO_COPY (hb_static_mutex_t);
113 template <typename item_t>
114 struct hb_static_threadsafe_set_t
116 hb_lockable_set_t <item_t, hb_static_mutex_t> set;
117 hb_static_mutex_t lock;
119 template <typename T>
120 inline item_t *replace_or_insert (T v)
122 return set.replace_or_insert (v, lock);
125 template <typename T>
126 inline void remove (T v)
128 set.remove (v, lock);
131 template <typename T>
132 inline bool find (T v, item_t *i)
134 return set.find (v, i, lock);
137 template <typename T>
138 inline item_t *find_or_insert (T v)
140 return set.find_or_insert (v, lock);
154 #endif /* HB_MUTEX_PRIVATE_HH */