Blame src/fcmutex.h

Packit 352660
/*
Packit 352660
 * Atomic int and pointer operations.  Originally copied from HarfBuzz.
Packit 352660
 *
Packit 352660
 * Copyright © 2007  Chris Wilson
Packit 352660
 * Copyright © 2009,2010  Red Hat, Inc.
Packit 352660
 * Copyright © 2011,2012,2013  Google, Inc.
Packit 352660
 *
Packit 352660
 * Permission is hereby granted, without written agreement and without
Packit 352660
 * license or royalty fees, to use, copy, modify, and distribute this
Packit 352660
 * software and its documentation for any purpose, provided that the
Packit 352660
 * above copyright notice and the following two paragraphs appear in
Packit 352660
 * all copies of this software.
Packit 352660
 *
Packit 352660
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
Packit 352660
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
Packit 352660
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
Packit 352660
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
Packit 352660
 * DAMAGE.
Packit 352660
 *
Packit 352660
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
Packit 352660
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
Packit 352660
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
Packit 352660
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
Packit 352660
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
Packit 352660
 *
Packit 352660
 * Contributor(s):
Packit 352660
 *	Chris Wilson <chris@chris-wilson.co.uk>
Packit 352660
 * Red Hat Author(s): Behdad Esfahbod
Packit 352660
 * Google Author(s): Behdad Esfahbod
Packit 352660
 */
Packit 352660
Packit 352660
#ifndef _FCMUTEX_H_
Packit 352660
#define _FCMUTEX_H_
Packit 352660
Packit 352660
#ifdef HAVE_CONFIG_H
Packit 352660
#include <config.h>
Packit 352660
#endif
Packit 352660
Packit 352660
#define FC_STMT_START do
Packit 352660
#define FC_STMT_END while (0)
Packit 352660
Packit 352660
/* mutex */
Packit 352660
Packit 352660
/* We need external help for these */
Packit 352660
Packit 352660
#if 0
Packit 352660
Packit 352660
Packit 352660
#elif !defined(FC_NO_MT) && defined(_MSC_VER) || defined(__MINGW32__)
Packit 352660
Packit 352660
#include "fcwindows.h"
Packit 352660
typedef CRITICAL_SECTION fc_mutex_impl_t;
Packit 352660
#define FC_MUTEX_IMPL_INIT	{ NULL, 0, 0, NULL, NULL, 0 }
Packit 352660
#define fc_mutex_impl_init(M)	InitializeCriticalSection (M)
Packit 352660
#define fc_mutex_impl_lock(M)	EnterCriticalSection (M)
Packit 352660
#define fc_mutex_impl_unlock(M)	LeaveCriticalSection (M)
Packit 352660
#define fc_mutex_impl_finish(M)	DeleteCriticalSection (M)
Packit 352660
Packit 352660
Packit 352660
#elif !defined(FC_NO_MT) && (defined(HAVE_PTHREAD) || defined(__APPLE__))
Packit 352660
Packit 352660
#include <pthread.h>
Packit 352660
typedef pthread_mutex_t fc_mutex_impl_t;
Packit 352660
#define FC_MUTEX_IMPL_INIT	PTHREAD_MUTEX_INITIALIZER
Packit 352660
#define fc_mutex_impl_init(M)	pthread_mutex_init (M, NULL)
Packit 352660
#define fc_mutex_impl_lock(M)	pthread_mutex_lock (M)
Packit 352660
#define fc_mutex_impl_unlock(M)	pthread_mutex_unlock (M)
Packit 352660
#define fc_mutex_impl_finish(M)	pthread_mutex_destroy (M)
Packit 352660
Packit 352660
Packit 352660
#elif !defined(FC_NO_MT) && defined(HAVE_INTEL_ATOMIC_PRIMITIVES)
Packit 352660
Packit 352660
#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_YIELD)
Packit 352660
# include <sched.h>
Packit 352660
# define FC_SCHED_YIELD() sched_yield ()
Packit 352660
#else
Packit 352660
# define FC_SCHED_YIELD() FC_STMT_START {} FC_STMT_END
Packit 352660
#endif
Packit 352660
Packit 352660
/* This actually is not a totally awful implementation. */
Packit 352660
typedef volatile int fc_mutex_impl_t;
Packit 352660
#define FC_MUTEX_IMPL_INIT	0
Packit 352660
#define fc_mutex_impl_init(M)	*(M) = 0
Packit 352660
#define fc_mutex_impl_lock(M)	FC_STMT_START { while (__sync_lock_test_and_set((M), 1)) FC_SCHED_YIELD (); } FC_STMT_END
Packit 352660
#define fc_mutex_impl_unlock(M)	__sync_lock_release (M)
Packit 352660
#define fc_mutex_impl_finish(M)	FC_STMT_START {} FC_STMT_END
Packit 352660
Packit 352660
Packit 352660
#elif !defined(FC_NO_MT)
Packit 352660
Packit 352660
#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_YIELD)
Packit 352660
# include <sched.h>
Packit 352660
# define FC_SCHED_YIELD() sched_yield ()
Packit 352660
#else
Packit 352660
# define FC_SCHED_YIELD() FC_STMT_START {} FC_STMT_END
Packit 352660
#endif
Packit 352660
Packit 352660
#define FC_MUTEX_INT_NIL 1 /* Warn that fallback implementation is in use. */
Packit 352660
typedef volatile int fc_mutex_impl_t;
Packit 352660
#define FC_MUTEX_IMPL_INIT	0
Packit 352660
#define fc_mutex_impl_init(M)	*(M) = 0
Packit 352660
#define fc_mutex_impl_lock(M)	FC_STMT_START { while (*(M)) FC_SCHED_YIELD (); (*(M))++; } FC_STMT_END
Packit 352660
#define fc_mutex_impl_unlock(M)	(*(M))--;
Packit 352660
#define fc_mutex_impl_finish(M)	FC_STMT_START {} FC_STMT_END
Packit 352660
Packit 352660
Packit 352660
#else /* FC_NO_MT */
Packit 352660
Packit 352660
typedef int fc_mutex_impl_t;
Packit 352660
#define FC_MUTEX_IMPL_INIT	0
Packit 352660
#define fc_mutex_impl_init(M)	FC_STMT_START {} FC_STMT_END
Packit 352660
#define fc_mutex_impl_lock(M)	FC_STMT_START {} FC_STMT_END
Packit 352660
#define fc_mutex_impl_unlock(M)	FC_STMT_START {} FC_STMT_END
Packit 352660
#define fc_mutex_impl_finish(M)	FC_STMT_START {} FC_STMT_END
Packit 352660
Packit 352660
#endif
Packit 352660
Packit 352660
Packit 352660
#define FC_MUTEX_INIT		{FC_MUTEX_IMPL_INIT}
Packit 352660
typedef fc_mutex_impl_t FcMutex;
Packit 352660
static inline void FcMutexInit   (FcMutex *m) { fc_mutex_impl_init (m);   }
Packit 352660
static inline void FcMutexLock   (FcMutex *m) { fc_mutex_impl_lock (m);   }
Packit 352660
static inline void FcMutexUnlock (FcMutex *m) { fc_mutex_impl_unlock (m); }
Packit 352660
static inline void FcMutexFinish (FcMutex *m) { fc_mutex_impl_finish (m); }
Packit 352660
Packit 352660
Packit 352660
#endif /* _FCMUTEX_H_ */