(root)/
fontconfig-2.14.2/
src/
fcmutex.h
       1  /*
       2   * Atomic int and pointer operations.  Originally copied from HarfBuzz.
       3   *
       4   * Copyright © 2007  Chris Wilson
       5   * Copyright © 2009,2010  Red Hat, Inc.
       6   * Copyright © 2011,2012,2013  Google, Inc.
       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 _FCMUTEX_H_
      33  #define _FCMUTEX_H_
      34  
      35  #ifdef HAVE_CONFIG_H
      36  #include <config.h>
      37  #endif
      38  
      39  #define FC_STMT_START do
      40  #define FC_STMT_END while (0)
      41  
      42  /* mutex */
      43  
      44  /* We need external help for these */
      45  
      46  #if 0
      47  
      48  
      49  #elif !defined(FC_NO_MT) && defined(_MSC_VER) || defined(__MINGW32__)
      50  
      51  #include "fcwindows.h"
      52  typedef CRITICAL_SECTION fc_mutex_impl_t;
      53  #define FC_MUTEX_IMPL_INIT	{ NULL, 0, 0, NULL, NULL, 0 }
      54  #define fc_mutex_impl_init(M)	InitializeCriticalSection (M)
      55  #define fc_mutex_impl_lock(M)	EnterCriticalSection (M)
      56  #define fc_mutex_impl_unlock(M)	LeaveCriticalSection (M)
      57  #define fc_mutex_impl_finish(M)	DeleteCriticalSection (M)
      58  
      59  
      60  #elif !defined(FC_NO_MT) && (defined(HAVE_PTHREAD) || defined(__APPLE__))
      61  
      62  #include <pthread.h>
      63  typedef pthread_mutex_t fc_mutex_impl_t;
      64  #define FC_MUTEX_IMPL_INIT	PTHREAD_MUTEX_INITIALIZER
      65  #define fc_mutex_impl_init(M)	pthread_mutex_init (M, NULL)
      66  #define fc_mutex_impl_lock(M)	pthread_mutex_lock (M)
      67  #define fc_mutex_impl_unlock(M)	pthread_mutex_unlock (M)
      68  #define fc_mutex_impl_finish(M)	pthread_mutex_destroy (M)
      69  
      70  
      71  #elif !defined(FC_NO_MT) && defined(HAVE_INTEL_ATOMIC_PRIMITIVES)
      72  
      73  #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_YIELD)
      74  # include <sched.h>
      75  # define FC_SCHED_YIELD() sched_yield ()
      76  #else
      77  # define FC_SCHED_YIELD() FC_STMT_START {} FC_STMT_END
      78  #endif
      79  
      80  /* This actually is not a totally awful implementation. */
      81  typedef volatile int fc_mutex_impl_t;
      82  #define FC_MUTEX_IMPL_INIT	0
      83  #define fc_mutex_impl_init(M)	*(M) = 0
      84  #define fc_mutex_impl_lock(M)	FC_STMT_START { while (__sync_lock_test_and_set((M), 1)) FC_SCHED_YIELD (); } FC_STMT_END
      85  #define fc_mutex_impl_unlock(M)	__sync_lock_release (M)
      86  #define fc_mutex_impl_finish(M)	FC_STMT_START {} FC_STMT_END
      87  
      88  
      89  #elif !defined(FC_NO_MT)
      90  
      91  #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_YIELD)
      92  # include <sched.h>
      93  # define FC_SCHED_YIELD() sched_yield ()
      94  #else
      95  # define FC_SCHED_YIELD() FC_STMT_START {} FC_STMT_END
      96  #endif
      97  
      98  #define FC_MUTEX_INT_NIL 1 /* Warn that fallback implementation is in use. */
      99  typedef volatile int fc_mutex_impl_t;
     100  #define FC_MUTEX_IMPL_INIT	0
     101  #define fc_mutex_impl_init(M)	*(M) = 0
     102  #define fc_mutex_impl_lock(M)	FC_STMT_START { while (*(M)) FC_SCHED_YIELD (); (*(M))++; } FC_STMT_END
     103  #define fc_mutex_impl_unlock(M)	(*(M))--;
     104  #define fc_mutex_impl_finish(M)	FC_STMT_START {} FC_STMT_END
     105  
     106  
     107  #else /* FC_NO_MT */
     108  
     109  typedef int fc_mutex_impl_t;
     110  #define FC_MUTEX_IMPL_INIT	0
     111  #define fc_mutex_impl_init(M)	FC_STMT_START {} FC_STMT_END
     112  #define fc_mutex_impl_lock(M)	FC_STMT_START {} FC_STMT_END
     113  #define fc_mutex_impl_unlock(M)	FC_STMT_START {} FC_STMT_END
     114  #define fc_mutex_impl_finish(M)	FC_STMT_START {} FC_STMT_END
     115  
     116  #endif
     117  
     118  
     119  #define FC_MUTEX_INIT		{FC_MUTEX_IMPL_INIT}
     120  typedef fc_mutex_impl_t FcMutex;
     121  static inline void FcMutexInit   (FcMutex *m) { fc_mutex_impl_init (m);   }
     122  static inline void FcMutexLock   (FcMutex *m) { fc_mutex_impl_lock (m);   }
     123  static inline void FcMutexUnlock (FcMutex *m) { fc_mutex_impl_unlock (m); }
     124  static inline void FcMutexFinish (FcMutex *m) { fc_mutex_impl_finish (m); }
     125  
     126  
     127  #endif /* _FCMUTEX_H_ */