Imported Upstream version 0.9.3
[platform/upstream/harfbuzz.git] / src / hb-mutex-private.hh
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_PRIVATE_HH
33 #define HB_MUTEX_PRIVATE_HH
34
35 #include "hb-private.hh"
36
37
38 /* mutex */
39
40 /* We need external help for these */
41
42 #if 0
43
44
45 #elif !defined(HB_NO_MT) && defined(_MSC_VER) || defined(__MINGW32__)
46
47 #define WIN32_LEAN_AND_MEAN
48 #include <windows.h>
49 typedef CRITICAL_SECTION hb_mutex_impl_t;
50 #define HB_MUTEX_IMPL_INIT      { NULL, 0, 0, NULL, NULL, 0 }
51 #define hb_mutex_impl_init(M)   InitializeCriticalSection (M)
52 #define hb_mutex_impl_lock(M)   EnterCriticalSection (M)
53 #define hb_mutex_impl_unlock(M) LeaveCriticalSection (M)
54 #define hb_mutex_impl_finish(M) DeleteCriticalSection (M)
55
56
57 #elif !defined(HB_NO_MT) && (defined(HAVE_PTHREAD) || defined(__APPLE__))
58
59 #include <pthread.h>
60 typedef pthread_mutex_t hb_mutex_impl_t;
61 #define HB_MUTEX_IMPL_INIT      PTHREAD_MUTEX_INITIALIZER
62 #define hb_mutex_impl_init(M)   pthread_mutex_init (M, NULL)
63 #define hb_mutex_impl_lock(M)   pthread_mutex_lock (M)
64 #define hb_mutex_impl_unlock(M) pthread_mutex_unlock (M)
65 #define hb_mutex_impl_finish(M) pthread_mutex_destroy (M)
66
67
68 #elif !defined(HB_NO_MT) && defined(HAVE_GLIB)
69
70 #include <glib.h>
71 #if !GLIB_CHECK_VERSION(2,32,0)
72 typedef GStaticMutex hb_mutex_impl_t;
73 #define HB_MUTEX_IMPL_INIT      G_STATIC_MUTEX_INIT
74 #define hb_mutex_impl_init(M)   g_static_mutex_init (M)
75 #define hb_mutex_impl_lock(M)   g_static_mutex_lock (M)
76 #define hb_mutex_impl_unlock(M) g_static_mutex_unlock (M)
77 #define hb_mutex_impl_finish(M) g_static_mutex_free (M)
78 #else
79 typedef GMutex hb_mutex_impl_t;
80 #define HB_MUTEX_IMPL_INIT      {0}
81 #define hb_mutex_impl_init(M)   g_mutex_init (M)
82 #define hb_mutex_impl_lock(M)   g_mutex_lock (M)
83 #define hb_mutex_impl_unlock(M) g_mutex_unlock (M)
84 #define hb_mutex_impl_finish(M) g_mutex_clear (M)
85 #endif
86
87
88 #elif !defined(HB_NO_MT) && defined(HAVE_INTEL_ATOMIC_PRIMITIVES)
89
90 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_YIELD)
91 # include <sched.h>
92 # define HB_SCHED_YIELD() sched_yield ()
93 #else
94 # define HB_SCHED_YIELD() HB_STMT_START {} HB_STMT_END
95 #endif
96
97 /* This actually is not a totally awful implementation. */
98 typedef volatile int hb_mutex_impl_t;
99 #define HB_MUTEX_IMPL_INIT      0
100 #define hb_mutex_impl_init(M)   *(M) = 0
101 #define hb_mutex_impl_lock(M)   HB_STMT_START { while (__sync_lock_test_and_set((M), 1)) HB_SCHED_YIELD (); } HB_STMT_END
102 #define hb_mutex_impl_unlock(M) __sync_lock_release (M)
103 #define hb_mutex_impl_finish(M) HB_STMT_START {} HB_STMT_END
104
105
106 #elif !defined(HB_NO_MT)
107
108 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_YIELD)
109 # include <sched.h>
110 # define HB_SCHED_YIELD() sched_yield ()
111 #else
112 # define HB_SCHED_YIELD() HB_STMT_START {} HB_STMT_END
113 #endif
114
115 #define HB_MUTEX_INT_NIL 1 /* Warn that fallback implementation is in use. */
116 typedef volatile int hb_mutex_impl_t;
117 #define HB_MUTEX_IMPL_INIT      0
118 #define hb_mutex_impl_init(M)   *(M) = 0
119 #define hb_mutex_impl_lock(M)   HB_STMT_START { while (*(M)) HB_SCHED_YIELD (); (*(M))++; } HB_STMT_END
120 #define hb_mutex_impl_unlock(M) (*(M))--;
121 #define hb_mutex_impl_finish(M) HB_STMT_START {} HB_STMT_END
122
123
124 #else /* HB_NO_MT */
125
126 typedef int hb_mutex_impl_t;
127 #define HB_MUTEX_IMPL_INIT      0
128 #define hb_mutex_impl_init(M)   HB_STMT_START {} HB_STMT_END
129 #define hb_mutex_impl_lock(M)   HB_STMT_START {} HB_STMT_END
130 #define hb_mutex_impl_unlock(M) HB_STMT_START {} HB_STMT_END
131 #define hb_mutex_impl_finish(M) HB_STMT_START {} HB_STMT_END
132
133 #endif
134
135
136 #define HB_MUTEX_INIT           {HB_MUTEX_IMPL_INIT}
137 struct hb_mutex_t
138 {
139   /* TODO Add tracing. */
140
141   hb_mutex_impl_t m;
142
143   inline void init   (void) { hb_mutex_impl_init   (&m); }
144   inline void lock   (void) { hb_mutex_impl_lock   (&m); }
145   inline void unlock (void) { hb_mutex_impl_unlock (&m); }
146   inline void finish (void) { hb_mutex_impl_finish (&m); }
147 };
148
149
150 #endif /* HB_MUTEX_PRIVATE_HH */