Move code around
[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 volatile int hb_atomic_int_t;
49 #define hb_atomic_int_fetch_and_add(AI, V)      g_atomic_int_exchange_and_add (&(AI), V)
50 #define hb_atomic_int_get(AI)                   g_atomic_int_get (&(AI))
51 #define hb_atomic_int_set(AI, V)                g_atomic_int_set (&(AI), V)
52
53 typedef GStaticMutex hb_mutex_t;
54 #define HB_MUTEX_INIT                   G_STATIC_MUTEX_INIT
55 #define hb_mutex_init(M)                g_static_mutex_init (&(M))
56 #define hb_mutex_lock(M)                g_static_mutex_lock (&(M))
57 #define hb_mutex_trylock(M)             g_static_mutex_trylock (&(M))
58 #define hb_mutex_unlock(M)              g_static_mutex_unlock (&(M))
59 #define hb_mutex_free(M)                g_static_mutex_free (&(M))
60
61
62 #elif defined(_MSC_VER)
63
64 #include <intrin.h>
65
66 typedef long hb_atomic_int_t;
67 #define hb_atomic_int_fetch_and_add(AI, V)      _InterlockedExchangeAdd (&(AI), V)
68 #define hb_atomic_int_get(AI)                   (_ReadBarrier (), (AI))
69 #define hb_atomic_int_set(AI, V)                ((void) _InterlockedExchange (&(AI), (V)))
70
71 #include <Windows.h>
72
73 typedef CRITICAL_SECTION hb_mutex_t;
74 #define HB_MUTEX_INIT                           { NULL, 0, 0, NULL, NULL, 0 }
75 #define hb_mutex_init(M)                        InitializeCriticalSection (&(M))
76 #define hb_mutex_lock(M)                        EnterCriticalSection (&(M))
77 #define hb_mutex_trylock(M)                     TryEnterCriticalSection (&(M))
78 #define hb_mutex_unlock(M)                      LeaveCriticalSection (&(M))
79 #define hb_mutex_free(M)                        DeleteCriticalSection (&(M))
80
81
82 #else
83
84 #warning "Could not find any system to define platform macros, library will NOT be thread-safe"
85
86 typedef volatile int hb_atomic_int_t;
87 #define hb_atomic_int_fetch_and_add(AI, V)      ((AI) += (V), (AI) - (V))
88 #define hb_atomic_int_get(AI)                   (AI)
89 #define hb_atomic_int_set(AI, V)                ((void) ((AI) = (V)))
90
91 typedef volatile int hb_mutex_t;
92 #define HB_MUTEX_INIT                           0
93 #define hb_mutex_init(M)                        ((void) ((M) = 0))
94 #define hb_mutex_lock(M)                        ((void) ((M) = 1))
95 #define hb_mutex_trylock(M)                     ((M) = 1, 1)
96 #define hb_mutex_unlock(M)                      ((void) ((M) = 0))
97 #define hb_mutex_free(M)                        ((void) ((M) = 2))
98
99
100 #endif
101
102
103 HB_END_DECLS
104
105 #endif /* HB_MUTEX_PRIVATE_HH */