1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/harfbuzz/src/hb-mutex-private.hh Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,130 @@ 1.4 +/* 1.5 + * Copyright © 2007 Chris Wilson 1.6 + * Copyright © 2009,2010 Red Hat, Inc. 1.7 + * Copyright © 2011,2012 Google, Inc. 1.8 + * 1.9 + * This is part of HarfBuzz, a text shaping library. 1.10 + * 1.11 + * Permission is hereby granted, without written agreement and without 1.12 + * license or royalty fees, to use, copy, modify, and distribute this 1.13 + * software and its documentation for any purpose, provided that the 1.14 + * above copyright notice and the following two paragraphs appear in 1.15 + * all copies of this software. 1.16 + * 1.17 + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR 1.18 + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 1.19 + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN 1.20 + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 1.21 + * DAMAGE. 1.22 + * 1.23 + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, 1.24 + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 1.25 + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 1.26 + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO 1.27 + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 1.28 + * 1.29 + * Contributor(s): 1.30 + * Chris Wilson <chris@chris-wilson.co.uk> 1.31 + * Red Hat Author(s): Behdad Esfahbod 1.32 + * Google Author(s): Behdad Esfahbod 1.33 + */ 1.34 + 1.35 +#ifndef HB_MUTEX_PRIVATE_HH 1.36 +#define HB_MUTEX_PRIVATE_HH 1.37 + 1.38 +#include "hb-private.hh" 1.39 + 1.40 + 1.41 +/* mutex */ 1.42 + 1.43 +/* We need external help for these */ 1.44 + 1.45 +#if 0 1.46 + 1.47 + 1.48 +#elif !defined(HB_NO_MT) && (defined(_WIN32) || defined(__CYGWIN__)) 1.49 + 1.50 +#define WIN32_LEAN_AND_MEAN 1.51 +#include <windows.h> 1.52 +typedef CRITICAL_SECTION hb_mutex_impl_t; 1.53 +#define HB_MUTEX_IMPL_INIT { NULL, 0, 0, NULL, NULL, 0 } 1.54 +#define hb_mutex_impl_init(M) InitializeCriticalSection (M) 1.55 +#define hb_mutex_impl_lock(M) EnterCriticalSection (M) 1.56 +#define hb_mutex_impl_unlock(M) LeaveCriticalSection (M) 1.57 +#define hb_mutex_impl_finish(M) DeleteCriticalSection (M) 1.58 + 1.59 + 1.60 +#elif !defined(HB_NO_MT) && (defined(HAVE_PTHREAD) || defined(__APPLE__)) 1.61 + 1.62 +#include <pthread.h> 1.63 +typedef pthread_mutex_t hb_mutex_impl_t; 1.64 +#define HB_MUTEX_IMPL_INIT PTHREAD_MUTEX_INITIALIZER 1.65 +#define hb_mutex_impl_init(M) pthread_mutex_init (M, NULL) 1.66 +#define hb_mutex_impl_lock(M) pthread_mutex_lock (M) 1.67 +#define hb_mutex_impl_unlock(M) pthread_mutex_unlock (M) 1.68 +#define hb_mutex_impl_finish(M) pthread_mutex_destroy (M) 1.69 + 1.70 + 1.71 +#elif !defined(HB_NO_MT) && defined(HAVE_INTEL_ATOMIC_PRIMITIVES) 1.72 + 1.73 +#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_YIELD) 1.74 +# include <sched.h> 1.75 +# define HB_SCHED_YIELD() sched_yield () 1.76 +#else 1.77 +# define HB_SCHED_YIELD() HB_STMT_START {} HB_STMT_END 1.78 +#endif 1.79 + 1.80 +/* This actually is not a totally awful implementation. */ 1.81 +typedef volatile int hb_mutex_impl_t; 1.82 +#define HB_MUTEX_IMPL_INIT 0 1.83 +#define hb_mutex_impl_init(M) *(M) = 0 1.84 +#define hb_mutex_impl_lock(M) HB_STMT_START { while (__sync_lock_test_and_set((M), 1)) HB_SCHED_YIELD (); } HB_STMT_END 1.85 +#define hb_mutex_impl_unlock(M) __sync_lock_release (M) 1.86 +#define hb_mutex_impl_finish(M) HB_STMT_START {} HB_STMT_END 1.87 + 1.88 + 1.89 +#elif !defined(HB_NO_MT) 1.90 + 1.91 +#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_YIELD) 1.92 +# include <sched.h> 1.93 +# define HB_SCHED_YIELD() sched_yield () 1.94 +#else 1.95 +# define HB_SCHED_YIELD() HB_STMT_START {} HB_STMT_END 1.96 +#endif 1.97 + 1.98 +#define HB_MUTEX_INT_NIL 1 /* Warn that fallback implementation is in use. */ 1.99 +typedef volatile int hb_mutex_impl_t; 1.100 +#define HB_MUTEX_IMPL_INIT 0 1.101 +#define hb_mutex_impl_init(M) *(M) = 0 1.102 +#define hb_mutex_impl_lock(M) HB_STMT_START { while (*(M)) HB_SCHED_YIELD (); (*(M))++; } HB_STMT_END 1.103 +#define hb_mutex_impl_unlock(M) (*(M))--; 1.104 +#define hb_mutex_impl_finish(M) HB_STMT_START {} HB_STMT_END 1.105 + 1.106 + 1.107 +#else /* HB_NO_MT */ 1.108 + 1.109 +typedef int hb_mutex_impl_t; 1.110 +#define HB_MUTEX_IMPL_INIT 0 1.111 +#define hb_mutex_impl_init(M) HB_STMT_START {} HB_STMT_END 1.112 +#define hb_mutex_impl_lock(M) HB_STMT_START {} HB_STMT_END 1.113 +#define hb_mutex_impl_unlock(M) HB_STMT_START {} HB_STMT_END 1.114 +#define hb_mutex_impl_finish(M) HB_STMT_START {} HB_STMT_END 1.115 + 1.116 +#endif 1.117 + 1.118 + 1.119 +#define HB_MUTEX_INIT {HB_MUTEX_IMPL_INIT} 1.120 +struct hb_mutex_t 1.121 +{ 1.122 + /* TODO Add tracing. */ 1.123 + 1.124 + hb_mutex_impl_t m; 1.125 + 1.126 + inline void init (void) { hb_mutex_impl_init (&m); } 1.127 + inline void lock (void) { hb_mutex_impl_lock (&m); } 1.128 + inline void unlock (void) { hb_mutex_impl_unlock (&m); } 1.129 + inline void finish (void) { hb_mutex_impl_finish (&m); } 1.130 +}; 1.131 + 1.132 + 1.133 +#endif /* HB_MUTEX_PRIVATE_HH */