2 * Copyright © 2007 Chris Wilson
3 * Copyright © 2009,2010 Red Hat, Inc.
4 * Copyright © 2011,2012 Google, Inc.
6 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
27 * Chris Wilson <chris@chris-wilson.co.uk>
28 * Red Hat Author(s): Behdad Esfahbod
29 * Google Author(s): Behdad Esfahbod
32 #ifndef HB_MUTEX_PRIVATE_HH
33 #define HB_MUTEX_PRIVATE_HH
35 #include "hb-private.hh"
40 /* We need external help for these */
45 #elif !defined(HB_NO_MT) && (defined(_WIN32) || defined(__CYGWIN__))
48 typedef CRITICAL_SECTION hb_mutex_impl_t
;
49 #define HB_MUTEX_IMPL_INIT {0}
50 #define hb_mutex_impl_init(M) InitializeCriticalSection (M)
51 #define hb_mutex_impl_lock(M) EnterCriticalSection (M)
52 #define hb_mutex_impl_unlock(M) LeaveCriticalSection (M)
53 #define hb_mutex_impl_finish(M) DeleteCriticalSection (M)
56 #elif !defined(HB_NO_MT) && (defined(HAVE_PTHREAD) || defined(__APPLE__))
59 typedef pthread_mutex_t hb_mutex_impl_t
;
60 #define HB_MUTEX_IMPL_INIT PTHREAD_MUTEX_INITIALIZER
61 #define hb_mutex_impl_init(M) pthread_mutex_init (M, NULL)
62 #define hb_mutex_impl_lock(M) pthread_mutex_lock (M)
63 #define hb_mutex_impl_unlock(M) pthread_mutex_unlock (M)
64 #define hb_mutex_impl_finish(M) pthread_mutex_destroy (M)
67 #elif !defined(HB_NO_MT) && defined(HAVE_INTEL_ATOMIC_PRIMITIVES)
69 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_YIELD)
71 # define HB_SCHED_YIELD() sched_yield ()
73 # define HB_SCHED_YIELD() HB_STMT_START {} HB_STMT_END
76 /* This actually is not a totally awful implementation. */
77 typedef volatile int hb_mutex_impl_t
;
78 #define HB_MUTEX_IMPL_INIT 0
79 #define hb_mutex_impl_init(M) *(M) = 0
80 #define hb_mutex_impl_lock(M) HB_STMT_START { while (__sync_lock_test_and_set((M), 1)) HB_SCHED_YIELD (); } HB_STMT_END
81 #define hb_mutex_impl_unlock(M) __sync_lock_release (M)
82 #define hb_mutex_impl_finish(M) HB_STMT_START {} HB_STMT_END
85 #elif !defined(HB_NO_MT)
87 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_YIELD)
89 # define HB_SCHED_YIELD() sched_yield ()
91 # define HB_SCHED_YIELD() HB_STMT_START {} HB_STMT_END
94 #define HB_MUTEX_INT_NIL 1 /* Warn that fallback implementation is in use. */
95 typedef volatile int hb_mutex_impl_t
;
96 #define HB_MUTEX_IMPL_INIT 0
97 #define hb_mutex_impl_init(M) *(M) = 0
98 #define hb_mutex_impl_lock(M) HB_STMT_START { while (*(M)) HB_SCHED_YIELD (); (*(M))++; } HB_STMT_END
99 #define hb_mutex_impl_unlock(M) (*(M))--;
100 #define hb_mutex_impl_finish(M) HB_STMT_START {} HB_STMT_END
105 typedef int hb_mutex_impl_t
;
106 #define HB_MUTEX_IMPL_INIT 0
107 #define hb_mutex_impl_init(M) HB_STMT_START {} HB_STMT_END
108 #define hb_mutex_impl_lock(M) HB_STMT_START {} HB_STMT_END
109 #define hb_mutex_impl_unlock(M) HB_STMT_START {} HB_STMT_END
110 #define hb_mutex_impl_finish(M) HB_STMT_START {} HB_STMT_END
115 #define HB_MUTEX_INIT {HB_MUTEX_IMPL_INIT}
118 /* TODO Add tracing. */
122 inline void init (void) { hb_mutex_impl_init (&m
); }
123 inline void lock (void) { hb_mutex_impl_lock (&m
); }
124 inline void unlock (void) { hb_mutex_impl_unlock (&m
); }
125 inline void finish (void) { hb_mutex_impl_finish (&m
); }
129 #endif /* HB_MUTEX_PRIVATE_HH */