• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2007  Chris Wilson
3  * Copyright © 2009,2010  Red Hat, Inc.
4  * Copyright © 2011,2012  Google, Inc.
5  *
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_MUTEX_HH
33 #define HB_MUTEX_HH
34 
35 #include "hb.hh"
36 
37 
38 /* mutex */
39 
40 /* We need external help for these */
41 
42 #if defined(HB_MUTEX_IMPL_INIT) \
43  && defined(hb_mutex_impl_init) \
44  && defined(hb_mutex_impl_lock) \
45  && defined(hb_mutex_impl_unlock) \
46  && defined(hb_mutex_impl_finish)
47 
48 /* Defined externally, i.e. in config.h; must have typedef'ed hb_mutex_impl_t as well. */
49 
50 
51 #elif !defined(HB_NO_MT) && (defined(HAVE_PTHREAD) || defined(__APPLE__))
52 
53 #include <pthread.h>
54 typedef pthread_mutex_t hb_mutex_impl_t;
55 #define HB_MUTEX_IMPL_INIT	PTHREAD_MUTEX_INITIALIZER
56 #define hb_mutex_impl_init(M)	pthread_mutex_init (M, nullptr)
57 #define hb_mutex_impl_lock(M)	pthread_mutex_lock (M)
58 #define hb_mutex_impl_unlock(M)	pthread_mutex_unlock (M)
59 #define hb_mutex_impl_finish(M)	pthread_mutex_destroy (M)
60 
61 
62 #elif !defined(HB_NO_MT) && defined(_WIN32)
63 
64 typedef CRITICAL_SECTION hb_mutex_impl_t;
65 #define HB_MUTEX_IMPL_INIT	{0}
66 #if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
67 #define hb_mutex_impl_init(M)	InitializeCriticalSectionEx (M, 0, 0)
68 #else
69 #define hb_mutex_impl_init(M)	InitializeCriticalSection (M)
70 #endif
71 #define hb_mutex_impl_lock(M)	EnterCriticalSection (M)
72 #define hb_mutex_impl_unlock(M)	LeaveCriticalSection (M)
73 #define hb_mutex_impl_finish(M)	DeleteCriticalSection (M)
74 
75 
76 #elif defined(HB_NO_MT)
77 
78 typedef int hb_mutex_impl_t;
79 #define HB_MUTEX_IMPL_INIT	0
80 #define hb_mutex_impl_init(M)	HB_STMT_START {} HB_STMT_END
81 #define hb_mutex_impl_lock(M)	HB_STMT_START {} HB_STMT_END
82 #define hb_mutex_impl_unlock(M)	HB_STMT_START {} HB_STMT_END
83 #define hb_mutex_impl_finish(M)	HB_STMT_START {} HB_STMT_END
84 
85 
86 #else
87 
88 #error "Could not find any system to define mutex macros."
89 #error "Check hb-mutex.hh for possible resolutions."
90 
91 #endif
92 
93 
94 #define HB_MUTEX_INIT		{HB_MUTEX_IMPL_INIT}
95 
96 struct hb_mutex_t
97 {
98   hb_mutex_impl_t m;
99 
inithb_mutex_t100   void init   () { hb_mutex_impl_init   (&m); }
lockhb_mutex_t101   void lock   () { hb_mutex_impl_lock   (&m); }
unlockhb_mutex_t102   void unlock () { hb_mutex_impl_unlock (&m); }
finihb_mutex_t103   void fini ()   { hb_mutex_impl_finish (&m); }
104 };
105 
106 struct hb_lock_t
107 {
hb_lock_thb_lock_t108   hb_lock_t (hb_mutex_t &mutex_) : mutex (mutex_) { mutex.lock (); }
~hb_lock_thb_lock_t109   ~hb_lock_t () { mutex.unlock (); }
110   private:
111   hb_mutex_t &mutex;
112 };
113 
114 
115 #endif /* HB_MUTEX_HH */
116