Fix build without mutex
[profile/ivi/org.tizen.video-player.git] / src / hb-mutex-private.hh
1 /*
2  * Copyright © 2007  Chris Wilson
3  * Copyright © 2009,2010  Red Hat, Inc.
4  * Copyright © 2011  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 HB_BEGIN_DECLS
38
39
40 /* mutex */
41
42 /* We need external help for these */
43
44 #ifdef HAVE_GLIB
45
46 #include <glib.h>
47
48 typedef GStaticMutex hb_mutex_t;
49 #define HB_MUTEX_INIT                   G_STATIC_MUTEX_INIT
50 #define hb_mutex_init(M)                g_static_mutex_init (M)
51 #define hb_mutex_lock(M)                g_static_mutex_lock (M)
52 #define hb_mutex_trylock(M)             g_static_mutex_trylock (M)
53 #define hb_mutex_unlock(M)              g_static_mutex_unlock (M)
54 #define hb_mutex_free(M)                g_static_mutex_free (M)
55
56
57 #elif defined(_MSC_VER)
58
59 #include <Windows.h>
60
61 typedef CRITICAL_SECTION hb_mutex_t;
62 #define HB_MUTEX_INIT                           { NULL, 0, 0, NULL, NULL, 0 }
63 #define hb_mutex_init(M)                        InitializeCriticalSection (M)
64 #define hb_mutex_lock(M)                        EnterCriticalSection (M)
65 #define hb_mutex_trylock(M)                     TryEnterCriticalSection (M)
66 #define hb_mutex_unlock(M)                      LeaveCriticalSection (M)
67 #define hb_mutex_free(M)                        DeleteCriticalSection (M)
68
69
70 #else
71
72 #warning "Could not find any system to define platform macros, library will NOT be thread-safe"
73
74 typedef struct { volatile int m; } hb_mutex_t;
75 #define HB_MUTEX_INIT                           0
76 #define hb_mutex_init(M)                        ((void) ((M)->m = 0))
77 #define hb_mutex_lock(M)                        ((void) ((M)->m = 1))
78 #define hb_mutex_trylock(M)                     ((M)->m = 1, 1)
79 #define hb_mutex_unlock(M)                      ((void) ((M)->m = 0))
80 #define hb_mutex_free(M)                        ((void) ((M)-M = 2))
81
82
83 #endif
84
85
86 struct hb_static_mutex_t : hb_mutex_t
87 {
88   hb_static_mutex_t (void) {
89     hb_mutex_init (this);
90   }
91
92   inline void lock (void) { hb_mutex_lock (this); }
93   inline void unlock (void) { hb_mutex_unlock (this); }
94 };
95
96
97 HB_END_DECLS
98
99
100 template <typename item_t>
101 struct hb_static_threadsafe_set_t
102 {
103   hb_lockable_set_t <item_t, hb_static_mutex_t> set;
104   hb_static_mutex_t lock;
105
106   template <typename T>
107   inline item_t *replace_or_insert (T v)
108   {
109     return set.replace_or_insert (v, lock);
110   }
111
112   template <typename T>
113   inline void remove (T v)
114   {
115     set.remove (v, lock);
116   }
117
118   template <typename T>
119   inline bool find (T v, item_t *i)
120   {
121     return set.find (v, i, lock);
122   }
123
124   template <typename T>
125   inline item_t *find_or_insert (T v)
126   {
127     return set.find_or_insert (v, lock);
128   }
129
130   void finish (void)
131   {
132     set.finish (lock);
133   }
134 };
135
136
137 HB_BEGIN_DECLS
138
139 HB_END_DECLS
140
141 #endif /* HB_MUTEX_PRIVATE_HH */