/* $NetBSD: mutex.h,v 1.4 2007/03/09 07:11:10 thorpej Exp $ */ /*- * Copyright (c) 2002, 2007 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation * by Jason R. Thorpe and Andrew Doran. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the NetBSD * Foundation, Inc. and its contributors. * 4. Neither the name of The NetBSD Foundation nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef _ARM_MUTEX_H_ #define _ARM_MUTEX_H_ /* * The ARM mutex implementation is troublesome, because ARM (< v6) lacks a * compare-and-set operation. However, there aren't any MP pre-v6 ARM * systems to speak of. * * SMP for spin mutexes is easy - we don't need to know who owns the lock. * For adaptive mutexes, we need an aditional interlock. * * Unfortunately, not all ARM kernels are linked at the same address, * meaning we cannot safely overlay the interlock with the MSB of the * owner field. * * For a mutex acquisition, we first grab the interlock and then set the * owner field. * * There is room in the owners field for a waiters bit, but we don't do * that because it would be hard to synchronize using one without a CAS * operation. Because the waiters bit is only needed for adaptive mutexes, * we instead use the lock that is normally used by spin mutexes to indicate * waiters. * * Spin mutexes are initialized with the interlock held to cause the * assembly stub to go through mutex_vector_enter(). * * When releasing an adaptive mutex, we first clear the owners field, and * then check to see if the waiters byte is set. This ensures that there * will always be someone to wake any sleeping waiters up (even it the mutex * is acquired immediately after we release it, or if we are preempted * immediatley after clearing the owners field). The setting or clearing of * the waiters byte is serialized by the turnstile chain lock associated * with the mutex. * * See comments in kern_mutex.c about releasing adaptive mutexes without * an interlocking step. */ #ifndef __MUTEX_PRIVATE struct kmutex { uintptr_t mtx_pad1; uint32_t mtx_pad2[2]; }; #else /* __MUTEX_PRIVATE */ struct kmutex { volatile uintptr_t mtx_owner; /* 0-3 */ __cpu_simple_lock_t mtx_interlock; /* 4 */ __cpu_simple_lock_t mtx_lock; /* 5 */ ipl_cookie_t mtx_ipl; /* 6 */ uint8_t mtx_pad; /* 7 */ uint32_t mtx_id; /* 8-11 */ }; #if 0 #define __HAVE_MUTEX_STUBS 1 #define __HAVE_SPIN_MUTEX_STUBS 1 #endif static inline uintptr_t MUTEX_OWNER(uintptr_t owner) { return owner; } static inline int MUTEX_OWNED(uintptr_t owner) { return owner != 0; } static inline int MUTEX_SET_WAITERS(kmutex_t *mtx, uintptr_t owner) { (void)__cpu_simple_lock_try(&mtx->mtx_lock); return mtx->mtx_owner != 0; } static inline void MUTEX_CLEAR_WAITERS(kmutex_t *mtx) { __cpu_simple_unlock(&mtx->mtx_lock); } static inline int MUTEX_HAS_WAITERS(volatile kmutex_t *mtx) { if (mtx->mtx_owner == 0) return 0; return mtx->mtx_lock == __SIMPLELOCK_LOCKED; } static inline void MUTEX_INITIALIZE_SPIN(kmutex_t *mtx, u_int id, int ipl) { mtx->mtx_id = (id << 1) | 1; mtx->mtx_ipl = makeiplcookie(ipl); mtx->mtx_interlock = __SIMPLELOCK_LOCKED; __cpu_simple_lock_init(&mtx->mtx_lock); } static inline void MUTEX_INITIALIZE_ADAPTIVE(kmutex_t *mtx, u_int id) { mtx->mtx_id = (id << 1) | 0; __cpu_simple_lock_init(&mtx->mtx_interlock); __cpu_simple_lock_init(&mtx->mtx_lock); } static inline void MUTEX_DESTROY(kmutex_t *mtx) { mtx->mtx_owner = (uintptr_t)-1L; mtx->mtx_id = ~0; } static inline u_int MUTEX_GETID(kmutex_t *mtx) { return mtx->mtx_id >> 1; } static inline bool MUTEX_SPIN_P(volatile kmutex_t *mtx) { return (mtx->mtx_id & 1) == 1; } static inline bool MUTEX_ADAPTIVE_P(volatile kmutex_t *mtx) { return (mtx->mtx_id & 1) == 0; } static inline int MUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t curthread) { if (!__cpu_simple_lock_try(&mtx->mtx_interlock)) return 0; mtx->mtx_owner = curthread; return 1; } static inline void MUTEX_RELEASE(kmutex_t *mtx) { mtx->mtx_owner = 0; __cpu_simple_unlock(&mtx->mtx_lock); __cpu_simple_unlock(&mtx->mtx_interlock); } #endif /* __MUTEX_PRIVATE */ #endif /* _ARM_MUTEX_H_ */