Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright © 2007 Chris Wilson |
michael@0 | 3 | * Copyright © 2009,2010 Red Hat, Inc. |
michael@0 | 4 | * Copyright © 2011,2012 Google, Inc. |
michael@0 | 5 | * |
michael@0 | 6 | * This is part of HarfBuzz, a text shaping library. |
michael@0 | 7 | * |
michael@0 | 8 | * Permission is hereby granted, without written agreement and without |
michael@0 | 9 | * license or royalty fees, to use, copy, modify, and distribute this |
michael@0 | 10 | * software and its documentation for any purpose, provided that the |
michael@0 | 11 | * above copyright notice and the following two paragraphs appear in |
michael@0 | 12 | * all copies of this software. |
michael@0 | 13 | * |
michael@0 | 14 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
michael@0 | 15 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
michael@0 | 16 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
michael@0 | 17 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
michael@0 | 18 | * DAMAGE. |
michael@0 | 19 | * |
michael@0 | 20 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
michael@0 | 21 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
michael@0 | 22 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
michael@0 | 23 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
michael@0 | 24 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
michael@0 | 25 | * |
michael@0 | 26 | * Contributor(s): |
michael@0 | 27 | * Chris Wilson <chris@chris-wilson.co.uk> |
michael@0 | 28 | * Red Hat Author(s): Behdad Esfahbod |
michael@0 | 29 | * Google Author(s): Behdad Esfahbod |
michael@0 | 30 | */ |
michael@0 | 31 | |
michael@0 | 32 | #ifndef HB_MUTEX_PRIVATE_HH |
michael@0 | 33 | #define HB_MUTEX_PRIVATE_HH |
michael@0 | 34 | |
michael@0 | 35 | #include "hb-private.hh" |
michael@0 | 36 | |
michael@0 | 37 | |
michael@0 | 38 | /* mutex */ |
michael@0 | 39 | |
michael@0 | 40 | /* We need external help for these */ |
michael@0 | 41 | |
michael@0 | 42 | #if 0 |
michael@0 | 43 | |
michael@0 | 44 | |
michael@0 | 45 | #elif !defined(HB_NO_MT) && (defined(_WIN32) || defined(__CYGWIN__)) |
michael@0 | 46 | |
michael@0 | 47 | #define WIN32_LEAN_AND_MEAN |
michael@0 | 48 | #include <windows.h> |
michael@0 | 49 | typedef CRITICAL_SECTION hb_mutex_impl_t; |
michael@0 | 50 | #define HB_MUTEX_IMPL_INIT { NULL, 0, 0, NULL, NULL, 0 } |
michael@0 | 51 | #define hb_mutex_impl_init(M) InitializeCriticalSection (M) |
michael@0 | 52 | #define hb_mutex_impl_lock(M) EnterCriticalSection (M) |
michael@0 | 53 | #define hb_mutex_impl_unlock(M) LeaveCriticalSection (M) |
michael@0 | 54 | #define hb_mutex_impl_finish(M) DeleteCriticalSection (M) |
michael@0 | 55 | |
michael@0 | 56 | |
michael@0 | 57 | #elif !defined(HB_NO_MT) && (defined(HAVE_PTHREAD) || defined(__APPLE__)) |
michael@0 | 58 | |
michael@0 | 59 | #include <pthread.h> |
michael@0 | 60 | typedef pthread_mutex_t hb_mutex_impl_t; |
michael@0 | 61 | #define HB_MUTEX_IMPL_INIT PTHREAD_MUTEX_INITIALIZER |
michael@0 | 62 | #define hb_mutex_impl_init(M) pthread_mutex_init (M, NULL) |
michael@0 | 63 | #define hb_mutex_impl_lock(M) pthread_mutex_lock (M) |
michael@0 | 64 | #define hb_mutex_impl_unlock(M) pthread_mutex_unlock (M) |
michael@0 | 65 | #define hb_mutex_impl_finish(M) pthread_mutex_destroy (M) |
michael@0 | 66 | |
michael@0 | 67 | |
michael@0 | 68 | #elif !defined(HB_NO_MT) && defined(HAVE_INTEL_ATOMIC_PRIMITIVES) |
michael@0 | 69 | |
michael@0 | 70 | #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_YIELD) |
michael@0 | 71 | # include <sched.h> |
michael@0 | 72 | # define HB_SCHED_YIELD() sched_yield () |
michael@0 | 73 | #else |
michael@0 | 74 | # define HB_SCHED_YIELD() HB_STMT_START {} HB_STMT_END |
michael@0 | 75 | #endif |
michael@0 | 76 | |
michael@0 | 77 | /* This actually is not a totally awful implementation. */ |
michael@0 | 78 | typedef volatile int hb_mutex_impl_t; |
michael@0 | 79 | #define HB_MUTEX_IMPL_INIT 0 |
michael@0 | 80 | #define hb_mutex_impl_init(M) *(M) = 0 |
michael@0 | 81 | #define hb_mutex_impl_lock(M) HB_STMT_START { while (__sync_lock_test_and_set((M), 1)) HB_SCHED_YIELD (); } HB_STMT_END |
michael@0 | 82 | #define hb_mutex_impl_unlock(M) __sync_lock_release (M) |
michael@0 | 83 | #define hb_mutex_impl_finish(M) HB_STMT_START {} HB_STMT_END |
michael@0 | 84 | |
michael@0 | 85 | |
michael@0 | 86 | #elif !defined(HB_NO_MT) |
michael@0 | 87 | |
michael@0 | 88 | #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_YIELD) |
michael@0 | 89 | # include <sched.h> |
michael@0 | 90 | # define HB_SCHED_YIELD() sched_yield () |
michael@0 | 91 | #else |
michael@0 | 92 | # define HB_SCHED_YIELD() HB_STMT_START {} HB_STMT_END |
michael@0 | 93 | #endif |
michael@0 | 94 | |
michael@0 | 95 | #define HB_MUTEX_INT_NIL 1 /* Warn that fallback implementation is in use. */ |
michael@0 | 96 | typedef volatile int hb_mutex_impl_t; |
michael@0 | 97 | #define HB_MUTEX_IMPL_INIT 0 |
michael@0 | 98 | #define hb_mutex_impl_init(M) *(M) = 0 |
michael@0 | 99 | #define hb_mutex_impl_lock(M) HB_STMT_START { while (*(M)) HB_SCHED_YIELD (); (*(M))++; } HB_STMT_END |
michael@0 | 100 | #define hb_mutex_impl_unlock(M) (*(M))--; |
michael@0 | 101 | #define hb_mutex_impl_finish(M) HB_STMT_START {} HB_STMT_END |
michael@0 | 102 | |
michael@0 | 103 | |
michael@0 | 104 | #else /* HB_NO_MT */ |
michael@0 | 105 | |
michael@0 | 106 | typedef int hb_mutex_impl_t; |
michael@0 | 107 | #define HB_MUTEX_IMPL_INIT 0 |
michael@0 | 108 | #define hb_mutex_impl_init(M) HB_STMT_START {} HB_STMT_END |
michael@0 | 109 | #define hb_mutex_impl_lock(M) HB_STMT_START {} HB_STMT_END |
michael@0 | 110 | #define hb_mutex_impl_unlock(M) HB_STMT_START {} HB_STMT_END |
michael@0 | 111 | #define hb_mutex_impl_finish(M) HB_STMT_START {} HB_STMT_END |
michael@0 | 112 | |
michael@0 | 113 | #endif |
michael@0 | 114 | |
michael@0 | 115 | |
michael@0 | 116 | #define HB_MUTEX_INIT {HB_MUTEX_IMPL_INIT} |
michael@0 | 117 | struct hb_mutex_t |
michael@0 | 118 | { |
michael@0 | 119 | /* TODO Add tracing. */ |
michael@0 | 120 | |
michael@0 | 121 | hb_mutex_impl_t m; |
michael@0 | 122 | |
michael@0 | 123 | inline void init (void) { hb_mutex_impl_init (&m); } |
michael@0 | 124 | inline void lock (void) { hb_mutex_impl_lock (&m); } |
michael@0 | 125 | inline void unlock (void) { hb_mutex_impl_unlock (&m); } |
michael@0 | 126 | inline void finish (void) { hb_mutex_impl_finish (&m); } |
michael@0 | 127 | }; |
michael@0 | 128 | |
michael@0 | 129 | |
michael@0 | 130 | #endif /* HB_MUTEX_PRIVATE_HH */ |