mutex.h revision 1.1
11.1Smatt/*	$NetBSD: mutex.h,v 1.1 2007/02/18 07:24:52 matt Exp $	*/
21.1Smatt
31.1Smatt/*-
41.1Smatt * Copyright (c) 2002, 2007 The NetBSD Foundation, Inc.
51.1Smatt * All rights reserved.
61.1Smatt *
71.1Smatt * This code is derived from software contributed to The NetBSD Foundation
81.1Smatt * by Jason R. Thorpe and Andrew Doran.
91.1Smatt *
101.1Smatt * Redistribution and use in source and binary forms, with or without
111.1Smatt * modification, are permitted provided that the following conditions
121.1Smatt * are met:
131.1Smatt * 1. Redistributions of source code must retain the above copyright
141.1Smatt *    notice, this list of conditions and the following disclaimer.
151.1Smatt * 2. Redistributions in binary form must reproduce the above copyright
161.1Smatt *    notice, this list of conditions and the following disclaimer in the
171.1Smatt *    documentation and/or other materials provided with the distribution.
181.1Smatt * 3. All advertising materials mentioning features or use of this software
191.1Smatt *    must display the following acknowledgement:
201.1Smatt *	This product includes software developed by the NetBSD
211.1Smatt *	Foundation, Inc. and its contributors.
221.1Smatt * 4. Neither the name of The NetBSD Foundation nor the names of its
231.1Smatt *    contributors may be used to endorse or promote products derived
241.1Smatt *    from this software without specific prior written permission.
251.1Smatt *
261.1Smatt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
271.1Smatt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
281.1Smatt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
291.1Smatt * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
301.1Smatt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
311.1Smatt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
321.1Smatt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
331.1Smatt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
341.1Smatt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
351.1Smatt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
361.1Smatt * POSSIBILITY OF SUCH DAMAGE.
371.1Smatt */
381.1Smatt
391.1Smatt#ifndef _ARM_MUTEX_H_
401.1Smatt#define	_ARM_MUTEX_H_
411.1Smatt
421.1Smatt/*
431.1Smatt * The arm mutex implementation is troublesome, because arm lacks a
441.1Smatt * compare-and-set operation.  However, there aren't MP arms.
451.1Smatt * SMP for spin mutexes is easy - we don't need
461.1Smatt * to know who owns the lock.  For adaptive mutexes, we need an aditional
471.1Smatt * interlock.
481.1Smatt *
491.1Smatt * The locked byte set by the sparc 'ldstub' instruction is 0xff.  sparc
501.1Smatt * kernels are always loaded above 0xe0000000, and the low 5 bits of any
511.1Smatt * "struct lwp *" are always zero.  So, to record the lock owner, we only
521.1Smatt * need 23 bits of space.  mtxa_owner contains the mutex owner's address
531.1Smatt * shifted right by 5: the top three bits of which will always be 0xe,
541.1Smatt * overlapping with the interlock at the top byte, which is always 0xff
551.1Smatt * when the mutex is held.
561.1Smatt *
571.1Smatt * For a mutex acquisition, the owner field is set in two steps: first,
581.1Smatt * acquire the interlock (top byte), and then test the owner's address.
591.1Smatt * Once the owner field is non zero, it will appear that the mutex is held,
601.1Smatt * by which LWP it does not matter: other LWPs competing for the lock will
611.1Smatt * fall through to mutex_vector_enter(), and either spin or sleep.
621.1Smatt *
631.1Smatt * As a result there is no space for a waiters bit in the owner field.  No
641.1Smatt * problem, because it would be hard to synchronise using one without a CAS
651.1Smatt * operation.  Note that in order to do unlocked release of adaptive
661.1Smatt * mutexes, we need the effect of MUTEX_SET_WAITERS() to be immediatley
671.1Smatt * visible on the bus.  So, adaptive mutexes share the spin lock byte with
681.1Smatt * spin mutexes (set with ldstub), but it is not treated as a lock in its
691.1Smatt * own right, rather as a flag that can be atomically set or cleared.
701.1Smatt *
711.1Smatt * When releasing an adaptive mutex, we first clear the owners field, and
721.1Smatt * then check to see if the waiters byte is set.  This ensures that there
731.1Smatt * will always be someone to wake any sleeping waiters up (even it the mutex
741.1Smatt * is acquired immediately after we release it, or if we are preempted
751.1Smatt * immediatley after clearing the owners field).  The setting or clearing of
761.1Smatt * the waiters byte is serialized by the turnstile chain lock associated
771.1Smatt * with the mutex.
781.1Smatt *
791.1Smatt * See comments in kern_mutex.c about releasing adaptive mutexes without
801.1Smatt * an interlocking step.
811.1Smatt */
821.1Smatt
831.1Smatt#ifndef __MUTEX_PRIVATE
841.1Smatt
851.1Smattstruct kmutex {
861.1Smatt	uintptr_t	mtx_pad1;
871.1Smatt	uint32_t	mtx_pad2[4];
881.1Smatt};
891.1Smatt
901.1Smatt#else	/* __MUTEX_PRIVATE */
911.1Smatt
921.1Smattstruct kmutex {
931.1Smatt	volatile uintptr_t	mtx_owner;		/* 0-3 */
941.1Smatt	__cpu_simple_lock_t	mtx_interlock;		/* 4-7 */
951.1Smatt	ipl_cookie_t		mtx_ipl;		/* 8-11 */
961.1Smatt	__cpu_simple_lock_t	mtx_lock;		/* 12-15 */
971.1Smatt	uint32_t		mtx_id;			/* 16-19 */
981.1Smatt};
991.1Smatt
1001.1Smatt#if 0
1011.1Smatt#define	__HAVE_MUTEX_STUBS	1
1021.1Smatt#define	__HAVE_SPIN_MUTEX_STUBS	1
1031.1Smatt#endif
1041.1Smatt
1051.1Smattstatic inline uintptr_t
1061.1SmattMUTEX_OWNER(uintptr_t owner)
1071.1Smatt{
1081.1Smatt	return owner;
1091.1Smatt}
1101.1Smatt
1111.1Smattstatic inline int
1121.1SmattMUTEX_OWNED(uintptr_t owner)
1131.1Smatt{
1141.1Smatt	return owner != 0;
1151.1Smatt}
1161.1Smatt
1171.1Smattstatic inline int
1181.1SmattMUTEX_SET_WAITERS(kmutex_t *mtx, uintptr_t owner)
1191.1Smatt{
1201.1Smatt	(void)__cpu_simple_lock_try(&mtx->mtx_lock);
1211.1Smatt 	return mtx->mtx_owner != 0;
1221.1Smatt}
1231.1Smatt
1241.1Smattstatic inline void
1251.1SmattMUTEX_CLEAR_WAITERS(kmutex_t *mtx)
1261.1Smatt{
1271.1Smatt	__cpu_simple_unlock(&mtx->mtx_lock);
1281.1Smatt}
1291.1Smatt
1301.1Smattstatic inline int
1311.1SmattMUTEX_HAS_WAITERS(volatile kmutex_t *mtx)
1321.1Smatt{
1331.1Smatt	if (mtx->mtx_owner == 0)
1341.1Smatt		return 0;
1351.1Smatt	return mtx->mtx_lock == __SIMPLELOCK_LOCKED;
1361.1Smatt}
1371.1Smatt
1381.1Smattstatic inline void
1391.1SmattMUTEX_INITIALIZE_SPIN(kmutex_t *mtx, u_int id, int ipl)
1401.1Smatt{
1411.1Smatt	mtx->mtx_id = (id << 1) | 1;
1421.1Smatt	mtx->mtx_ipl = makeiplcookie(ipl);
1431.1Smatt	mtx->mtx_interlock = __SIMPLELOCK_LOCKED;
1441.1Smatt	__cpu_simple_lock_init(&mtx->mtx_lock);
1451.1Smatt}
1461.1Smatt
1471.1Smattstatic inline void
1481.1SmattMUTEX_INITIALIZE_ADAPTIVE(kmutex_t *mtx, u_int id)
1491.1Smatt{
1501.1Smatt	mtx->mtx_id = (id << 1) | 0;
1511.1Smatt	__cpu_simple_lock_init(&mtx->mtx_interlock);
1521.1Smatt	__cpu_simple_lock_init(&mtx->mtx_lock);
1531.1Smatt}
1541.1Smatt
1551.1Smattstatic inline void
1561.1SmattMUTEX_DESTROY(kmutex_t *mtx)
1571.1Smatt{
1581.1Smatt	mtx->mtx_owner = (uintptr_t)-1L;
1591.1Smatt	mtx->mtx_id = ~0;
1601.1Smatt}
1611.1Smatt
1621.1Smattstatic inline u_int
1631.1SmattMUTEX_GETID(kmutex_t *mtx)
1641.1Smatt{
1651.1Smatt	return mtx->mtx_id >> 1;
1661.1Smatt}
1671.1Smatt
1681.1Smattstatic inline bool
1691.1SmattMUTEX_SPIN_P(volatile kmutex_t *mtx)
1701.1Smatt{
1711.1Smatt	return (mtx->mtx_id & 1) == 1;
1721.1Smatt}
1731.1Smatt
1741.1Smattstatic inline bool
1751.1SmattMUTEX_ADAPTIVE_P(volatile kmutex_t *mtx)
1761.1Smatt{
1771.1Smatt	return (mtx->mtx_id & 1) == 0;
1781.1Smatt}
1791.1Smatt
1801.1Smattstatic inline int
1811.1SmattMUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t curthread)
1821.1Smatt{
1831.1Smatt	if (!__cpu_simple_lock_try(&mtx->mtx_interlock))
1841.1Smatt		return 0;
1851.1Smatt	mtx->mtx_owner = curthread;
1861.1Smatt	return 1;
1871.1Smatt}
1881.1Smatt
1891.1Smattstatic inline void
1901.1SmattMUTEX_RELEASE(kmutex_t *mtx)
1911.1Smatt{
1921.1Smatt	mtx->mtx_owner = 0;
1931.1Smatt	__cpu_simple_unlock(&mtx->mtx_lock);
1941.1Smatt	__cpu_simple_unlock(&mtx->mtx_interlock);
1951.1Smatt}
1961.1Smatt
1971.1Smatt#endif	/* __MUTEX_PRIVATE */
1981.1Smatt
1991.1Smatt#endif /* _ARM_MUTEX_H_ */
200