Move Win32 thread-safety stuff to hb-object-private.h
[apps/core/preloaded/video-player.git] / src / hb-object-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_OBJECT_PRIVATE_HH
33 #define HB_OBJECT_PRIVATE_HH
34
35 #include "hb-private.hh"
36
37 HB_BEGIN_DECLS
38
39
40 /* Debug */
41
42 #ifndef HB_DEBUG_OBJECT
43 #define HB_DEBUG_OBJECT (HB_DEBUG+0)
44 #endif
45
46
47 /* atomic_int and mutex */
48
49 /* We need external help for these */
50
51 #ifdef HAVE_GLIB
52
53 #include <glib.h>
54
55 typedef volatile int hb_atomic_int_t;
56 #define hb_atomic_int_fetch_and_add(AI, V)      g_atomic_int_exchange_and_add (&(AI), V)
57 #define hb_atomic_int_get(AI)                   g_atomic_int_get (&(AI))
58 #define hb_atomic_int_set(AI, V)                g_atomic_int_set (&(AI), V)
59
60 typedef GStaticMutex hb_mutex_t;
61 #define HB_MUTEX_INIT                   G_STATIC_MUTEX_INIT
62 #define hb_mutex_init(M)                g_static_mutex_init (&(M))
63 #define hb_mutex_lock(M)                g_static_mutex_lock (&(M))
64 #define hb_mutex_trylock(M)             g_static_mutex_trylock (&(M))
65 #define hb_mutex_unlock(M)              g_static_mutex_unlock (&(M))
66 #define hb_mutex_free(M)                g_static_mutex_free (&(M))
67
68 #else
69
70 #ifdef _MSC_VER
71
72 #include <intrin.h>
73
74 typedef long hb_atomic_int_t;
75 #define hb_atomic_int_fetch_and_add(AI, V)      _InterlockedExchangeAdd (&(AI), V)
76 #define hb_atomic_int_get(AI)                   (_ReadBarrier (), (AI))
77 #define hb_atomic_int_set(AI, V)                ((void) _InterlockedExchange (&(AI), (V)))
78
79 #include <Windows.h>
80
81 typedef CRITICAL_SECTION hb_mutex_t;
82 #define HB_MUTEX_INIT                           { NULL, 0, 0, NULL, NULL, 0 }
83 #define hb_mutex_init(M)                        InitializeCriticalSection (&(M))
84 #define hb_mutex_lock(M)                        EnterCriticalSection (&(M))
85 #define hb_mutex_trylock(M)                     TryEnterCriticalSection (&(M))
86 #define hb_mutex_unlock(M)                      LeaveCriticalSection (&(M))
87 #define hb_mutex_free(M)                        DeleteCriticalSection (&(M))
88
89 #else
90
91 #warning "Could not find any system to define platform macros, library will NOT be thread-safe"
92
93 typedef volatile int hb_atomic_int_t;
94 #define hb_atomic_int_fetch_and_add(AI, V)      ((AI) += (V), (AI) - (V))
95 #define hb_atomic_int_get(AI)                   (AI)
96 #define hb_atomic_int_set(AI, V)                ((void) ((AI) = (V)))
97
98 typedef volatile int hb_mutex_t;
99 #define HB_MUTEX_INIT                           0
100 #define hb_mutex_init(M)                        ((void) ((M) = 0))
101 #define hb_mutex_lock(M)                        ((void) ((M) = 1))
102 #define hb_mutex_trylock(M)                     ((M) = 1, 1)
103 #define hb_mutex_unlock(M)                      ((void) ((M) = 0))
104 #define hb_mutex_free(M)                        ((void) ((M) = 2))
105
106 #endif
107
108 #endif
109
110
111
112
113 /* reference_count */
114
115 typedef struct {
116   hb_atomic_int_t ref_count;
117
118 #define HB_REFERENCE_COUNT_INVALID_VALUE ((hb_atomic_int_t) -1)
119 #define HB_REFERENCE_COUNT_INVALID {HB_REFERENCE_COUNT_INVALID_VALUE}
120
121   inline void init (int v) { ref_count = v; /* non-atomic is fine */ }
122   inline int inc (void) { return hb_atomic_int_fetch_and_add (ref_count,  1); }
123   inline int dec (void) { return hb_atomic_int_fetch_and_add (ref_count, -1); }
124   inline void set (int v) { hb_atomic_int_set (ref_count, v); }
125
126   inline int get (void) const { return hb_atomic_int_get (ref_count); }
127   inline bool is_invalid (void) const { return get () == HB_REFERENCE_COUNT_INVALID_VALUE; }
128
129 } hb_reference_count_t;
130
131
132 /* user_data */
133
134 /* XXX make this thread-safe, somehow! */
135
136 typedef struct {
137   void *data;
138   hb_destroy_func_t destroy;
139
140   void finish (void) { if (destroy) destroy (data); }
141 } hb_user_data_t;
142
143 struct hb_user_data_array_t {
144
145   hb_map_t<hb_user_data_key_t *, hb_user_data_t> map;
146
147   inline bool set (hb_user_data_key_t *key,
148                    void *              data,
149                    hb_destroy_func_t   destroy)
150   {
151     if (!data && !destroy) {
152       map.unset (key);
153       return true;
154     }
155     hb_user_data_t user_data = {data, destroy};
156     return map.set (key, user_data);
157   }
158
159   inline void *get (hb_user_data_key_t *key) {
160     hb_user_data_t *user_data = map.get (key);
161     return user_data ? user_data->data : NULL;
162   }
163
164   void finish (void) { map.finish (); }
165 };
166
167
168 /* object_header */
169
170 typedef struct _hb_object_header_t hb_object_header_t;
171
172 struct _hb_object_header_t {
173   hb_reference_count_t ref_count;
174   hb_user_data_array_t user_data;
175
176 #define HB_OBJECT_HEADER_STATIC {HB_REFERENCE_COUNT_INVALID}
177
178   static inline void *create (unsigned int size) {
179     hb_object_header_t *obj = (hb_object_header_t *) calloc (1, size);
180
181     if (likely (obj))
182       obj->init ();
183
184     return obj;
185   }
186
187   inline void init (void) {
188     ref_count.init (1);
189   }
190
191   inline bool is_inert (void) const {
192     return unlikely (ref_count.is_invalid ());
193   }
194
195   inline void reference (void) {
196     if (unlikely (!this || this->is_inert ()))
197       return;
198     ref_count.inc ();
199   }
200
201   inline bool destroy (void) {
202     if (unlikely (!this || this->is_inert ()))
203       return false;
204     if (ref_count.dec () != 1)
205       return false;
206
207     user_data.finish ();
208
209     return true;
210   }
211
212   inline bool set_user_data (hb_user_data_key_t *key,
213                              void *              data,
214                              hb_destroy_func_t   destroy) {
215     if (unlikely (!this || this->is_inert ()))
216       return false;
217
218     return user_data.set (key, data, destroy);
219   }
220
221   inline void *get_user_data (hb_user_data_key_t *key) {
222     return user_data.get (key);
223   }
224
225   inline void trace (const char *function) const {
226     (void) (HB_DEBUG_OBJECT &&
227             fprintf (stderr, "OBJECT(%p) refcount=%d %s\n",
228                      this,
229                      this ? ref_count.get () : 0,
230                      function));
231   }
232
233 };
234
235
236 HB_END_DECLS
237
238
239 /* object */
240
241 template <typename Type>
242 static inline void hb_object_trace (const Type *obj, const char *function)
243 {
244   obj->header.trace (function);
245 }
246 template <typename Type>
247 static inline Type *hb_object_create (void)
248 {
249   Type *obj = (Type *) hb_object_header_t::create (sizeof (Type));
250   hb_object_trace (obj, HB_FUNC);
251   return obj;
252 }
253 template <typename Type>
254 static inline bool hb_object_is_inert (const Type *obj)
255 {
256   return unlikely (obj->header.is_inert ());
257 }
258 template <typename Type>
259 static inline Type *hb_object_reference (Type *obj)
260 {
261   hb_object_trace (obj, HB_FUNC);
262   obj->header.reference ();
263   return obj;
264 }
265 template <typename Type>
266 static inline bool hb_object_destroy (Type *obj)
267 {
268   hb_object_trace (obj, HB_FUNC);
269   return obj->header.destroy ();
270 }
271 template <typename Type>
272 static inline bool hb_object_set_user_data (Type               *obj,
273                                             hb_user_data_key_t *key,
274                                             void *              data,
275                                             hb_destroy_func_t   destroy)
276 {
277   return obj->header.set_user_data (key, data, destroy);
278 }
279
280 template <typename Type>
281 static inline void *hb_object_get_user_data (Type               *obj,
282                                              hb_user_data_key_t *key)
283 {
284   return obj->header.get_user_data (key);
285 }
286
287
288 HB_BEGIN_DECLS
289
290
291 HB_END_DECLS
292
293 #endif /* HB_OBJECT_PRIVATE_HH */