michael@0: /* michael@0: * Copyright © 2007 Chris Wilson michael@0: * Copyright © 2009,2010 Red Hat, Inc. michael@0: * Copyright © 2011,2012 Google, Inc. michael@0: * michael@0: * This is part of HarfBuzz, a text shaping library. michael@0: * michael@0: * Permission is hereby granted, without written agreement and without michael@0: * license or royalty fees, to use, copy, modify, and distribute this michael@0: * software and its documentation for any purpose, provided that the michael@0: * above copyright notice and the following two paragraphs appear in michael@0: * all copies of this software. michael@0: * michael@0: * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR michael@0: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES michael@0: * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN michael@0: * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH michael@0: * DAMAGE. michael@0: * michael@0: * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, michael@0: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND michael@0: * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS michael@0: * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO michael@0: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. michael@0: * michael@0: * Contributor(s): michael@0: * Chris Wilson michael@0: * Red Hat Author(s): Behdad Esfahbod michael@0: * Google Author(s): Behdad Esfahbod michael@0: */ michael@0: michael@0: #ifndef HB_MUTEX_PRIVATE_HH michael@0: #define HB_MUTEX_PRIVATE_HH michael@0: michael@0: #include "hb-private.hh" michael@0: michael@0: michael@0: /* mutex */ michael@0: michael@0: /* We need external help for these */ michael@0: michael@0: #if 0 michael@0: michael@0: michael@0: #elif !defined(HB_NO_MT) && (defined(_WIN32) || defined(__CYGWIN__)) michael@0: michael@0: #define WIN32_LEAN_AND_MEAN michael@0: #include michael@0: typedef CRITICAL_SECTION hb_mutex_impl_t; michael@0: #define HB_MUTEX_IMPL_INIT { NULL, 0, 0, NULL, NULL, 0 } michael@0: #define hb_mutex_impl_init(M) InitializeCriticalSection (M) michael@0: #define hb_mutex_impl_lock(M) EnterCriticalSection (M) michael@0: #define hb_mutex_impl_unlock(M) LeaveCriticalSection (M) michael@0: #define hb_mutex_impl_finish(M) DeleteCriticalSection (M) michael@0: michael@0: michael@0: #elif !defined(HB_NO_MT) && (defined(HAVE_PTHREAD) || defined(__APPLE__)) michael@0: michael@0: #include michael@0: typedef pthread_mutex_t hb_mutex_impl_t; michael@0: #define HB_MUTEX_IMPL_INIT PTHREAD_MUTEX_INITIALIZER michael@0: #define hb_mutex_impl_init(M) pthread_mutex_init (M, NULL) michael@0: #define hb_mutex_impl_lock(M) pthread_mutex_lock (M) michael@0: #define hb_mutex_impl_unlock(M) pthread_mutex_unlock (M) michael@0: #define hb_mutex_impl_finish(M) pthread_mutex_destroy (M) michael@0: michael@0: michael@0: #elif !defined(HB_NO_MT) && defined(HAVE_INTEL_ATOMIC_PRIMITIVES) michael@0: michael@0: #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_YIELD) michael@0: # include michael@0: # define HB_SCHED_YIELD() sched_yield () michael@0: #else michael@0: # define HB_SCHED_YIELD() HB_STMT_START {} HB_STMT_END michael@0: #endif michael@0: michael@0: /* This actually is not a totally awful implementation. */ michael@0: typedef volatile int hb_mutex_impl_t; michael@0: #define HB_MUTEX_IMPL_INIT 0 michael@0: #define hb_mutex_impl_init(M) *(M) = 0 michael@0: #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: #define hb_mutex_impl_unlock(M) __sync_lock_release (M) michael@0: #define hb_mutex_impl_finish(M) HB_STMT_START {} HB_STMT_END michael@0: michael@0: michael@0: #elif !defined(HB_NO_MT) michael@0: michael@0: #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_YIELD) michael@0: # include michael@0: # define HB_SCHED_YIELD() sched_yield () michael@0: #else michael@0: # define HB_SCHED_YIELD() HB_STMT_START {} HB_STMT_END michael@0: #endif michael@0: michael@0: #define HB_MUTEX_INT_NIL 1 /* Warn that fallback implementation is in use. */ michael@0: typedef volatile int hb_mutex_impl_t; michael@0: #define HB_MUTEX_IMPL_INIT 0 michael@0: #define hb_mutex_impl_init(M) *(M) = 0 michael@0: #define hb_mutex_impl_lock(M) HB_STMT_START { while (*(M)) HB_SCHED_YIELD (); (*(M))++; } HB_STMT_END michael@0: #define hb_mutex_impl_unlock(M) (*(M))--; michael@0: #define hb_mutex_impl_finish(M) HB_STMT_START {} HB_STMT_END michael@0: michael@0: michael@0: #else /* HB_NO_MT */ michael@0: michael@0: typedef int hb_mutex_impl_t; michael@0: #define HB_MUTEX_IMPL_INIT 0 michael@0: #define hb_mutex_impl_init(M) HB_STMT_START {} HB_STMT_END michael@0: #define hb_mutex_impl_lock(M) HB_STMT_START {} HB_STMT_END michael@0: #define hb_mutex_impl_unlock(M) HB_STMT_START {} HB_STMT_END michael@0: #define hb_mutex_impl_finish(M) HB_STMT_START {} HB_STMT_END michael@0: michael@0: #endif michael@0: michael@0: michael@0: #define HB_MUTEX_INIT {HB_MUTEX_IMPL_INIT} michael@0: struct hb_mutex_t michael@0: { michael@0: /* TODO Add tracing. */ michael@0: michael@0: hb_mutex_impl_t m; michael@0: michael@0: inline void init (void) { hb_mutex_impl_init (&m); } michael@0: inline void lock (void) { hb_mutex_impl_lock (&m); } michael@0: inline void unlock (void) { hb_mutex_impl_unlock (&m); } michael@0: inline void finish (void) { hb_mutex_impl_finish (&m); } michael@0: }; michael@0: michael@0: michael@0: #endif /* HB_MUTEX_PRIVATE_HH */