mutex.h revision 1.7
11.7Sad/*	$NetBSD: mutex.h,v 1.7 2008/01/02 11:48:28 ad Exp $	*/
21.2Sad
31.2Sad/*-
41.2Sad * Copyright (c) 2002, 2007 The NetBSD Foundation, Inc.
51.2Sad * All rights reserved.
61.2Sad *
71.2Sad * This code is derived from software contributed to The NetBSD Foundation
81.2Sad * by Jason R. Thorpe and Andrew Doran.
91.2Sad *
101.2Sad * Redistribution and use in source and binary forms, with or without
111.2Sad * modification, are permitted provided that the following conditions
121.2Sad * are met:
131.2Sad * 1. Redistributions of source code must retain the above copyright
141.2Sad *    notice, this list of conditions and the following disclaimer.
151.2Sad * 2. Redistributions in binary form must reproduce the above copyright
161.2Sad *    notice, this list of conditions and the following disclaimer in the
171.2Sad *    documentation and/or other materials provided with the distribution.
181.2Sad * 3. All advertising materials mentioning features or use of this software
191.2Sad *    must display the following acknowledgement:
201.2Sad *	This product includes software developed by the NetBSD
211.2Sad *	Foundation, Inc. and its contributors.
221.2Sad * 4. Neither the name of The NetBSD Foundation nor the names of its
231.2Sad *    contributors may be used to endorse or promote products derived
241.2Sad *    from this software without specific prior written permission.
251.2Sad *
261.2Sad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
271.2Sad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
281.2Sad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
291.2Sad * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
301.2Sad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
311.2Sad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
321.2Sad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
331.2Sad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
341.2Sad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
351.2Sad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
361.2Sad * POSSIBILITY OF SUCH DAMAGE.
371.2Sad */
381.2Sad
391.2Sad#ifndef _SPARC_MUTEX_H_
401.2Sad#define	_SPARC_MUTEX_H_
411.2Sad
421.2Sad/*
431.2Sad * There sparc mutex implementation is troublesome, because sparc (v7 and
441.2Sad * v8) lacks a compare-and-set operation, yet there are many SMP sparc
451.2Sad * machines in circulation.  SMP for spin mutexes is easy - we don't need
461.2Sad * to know who owns the lock.  For adaptive mutexes, we need an aditional
471.2Sad * interlock.
481.2Sad *
491.2Sad * The locked byte set by the sparc 'ldstub' instruction is 0xff.  sparc
501.2Sad * kernels are always loaded above 0xe0000000, and the low 5 bits of any
511.2Sad * "struct lwp *" are always zero.  So, to record the lock owner, we only
521.2Sad * need 23 bits of space.  mtxa_owner contains the mutex owner's address
531.2Sad * shifted right by 5: the top three bits of which will always be 0xe,
541.2Sad * overlapping with the interlock at the top byte, which is always 0xff
551.2Sad * when the mutex is held.
561.2Sad *
571.2Sad * For a mutex acquisition, the owner field is set in two steps: first,
581.2Sad * acquire the interlock (top byte), and second OR in the owner's address.
591.2Sad * Once the owner field is non zero, it will appear that the mutex is held,
601.2Sad * by which LWP it does not matter: other LWPs competing for the lock will
611.2Sad * fall through to mutex_vector_enter(), and either spin or sleep.
621.2Sad *
631.2Sad * As a result there is no space for a waiters bit in the owner field.  No
641.2Sad * problem, because it would be hard to synchronise using one without a CAS
651.2Sad * operation.  Note that in order to do unlocked release of adaptive
661.2Sad * mutexes, we need the effect of MUTEX_SET_WAITERS() to be immediatley
671.2Sad * visible on the bus.  So, adaptive mutexes share the spin lock byte with
681.2Sad * spin mutexes (set with ldstub), but it is not treated as a lock in its
691.2Sad * own right, rather as a flag that can be atomically set or cleared.
701.2Sad *
711.2Sad * When releasing an adaptive mutex, we first clear the owners field, and
721.2Sad * then check to see if the waiters byte is set.  This ensures that there
731.2Sad * will always be someone to wake any sleeping waiters up (even it the mutex
741.2Sad * is acquired immediately after we release it, or if we are preempted
751.2Sad * immediatley after clearing the owners field).  The setting or clearing of
761.2Sad * the waiters byte is serialized by the turnstile chain lock associated
771.2Sad * with the mutex.
781.2Sad *
791.2Sad * See comments in kern_mutex.c about releasing adaptive mutexes without
801.2Sad * an interlocking step.
811.2Sad */
821.2Sad
831.2Sad#ifndef __MUTEX_PRIVATE
841.2Sad
851.2Sadstruct kmutex {
861.2Sad	uintptr_t	mtx_pad1;
871.2Sad	uint32_t	mtx_pad2;
881.2Sad};
891.2Sad
901.2Sad#else	/* __MUTEX_PRIVATE */
911.2Sad
921.7Sad#include <machine/lock.h>
931.7Sad
941.2Sadstruct kmutex {
951.2Sad	union {
961.2Sad		/* Adaptive mutex */
971.2Sad		volatile uintptr_t	mtxu_owner;		/* 0-3 */
981.2Sad		__cpu_simple_lock_t	mtxu_interlock;		/* 0 */
991.2Sad
1001.2Sad		/* Spin mutex. */
1011.2Sad		struct {
1021.2Sad			uint8_t			mtxs_dummy;	/* 0 */
1031.2Sad			uint8_t			mtxs_unused1;	/* 1 */
1041.2Sad			ipl_cookie_t		mtxs_ipl;	/* 2 */
1051.2Sad			uint8_t			mtxs_unused2;	/* 3 */
1061.2Sad		} s;
1071.2Sad	} u;
1081.2Sad	__cpu_simple_lock_t	mtx_lock;			/* 4 */
1091.6Syamt	uint8_t			mtx_dodebug;			/* 5 */
1101.6Syamt	uint8_t			mtx_isspin;			/* 6 */
1111.6Syamt	uint8_t			mtx_pad[1];			/* 7 */
1121.2Sad};
1131.2Sad
1141.2Sad#define	__HAVE_MUTEX_STUBS	1
1151.4Smrg#if 0 /* does not work for MP yet */
1161.2Sad#define	__HAVE_SPIN_MUTEX_STUBS	1
1171.4Smrg#endif
1181.2Sad
1191.2Sad#define	mtx_owner	u.mtxu_owner
1201.2Sad#define	mtx_interlock	u.mtxu_interlock
1211.2Sad#define	mtx_dummy	u.s.mtxs_dummy
1221.2Sad#define	mtx_ipl		u.s.mtxs_ipl
1231.2Sad
1241.2Sadstatic inline uintptr_t
1251.2SadMUTEX_OWNER(uintptr_t owner)
1261.2Sad{
1271.2Sad	return owner << 5;
1281.2Sad}
1291.2Sad
1301.2Sadstatic inline int
1311.2SadMUTEX_OWNED(uintptr_t owner)
1321.2Sad{
1331.2Sad	return owner != 0;
1341.2Sad}
1351.2Sad
1361.2Sadstatic inline int
1371.2SadMUTEX_SET_WAITERS(kmutex_t *mtx, uintptr_t owner)
1381.2Sad{
1391.2Sad	(void)__cpu_simple_lock_try(&mtx->mtx_lock);
1401.2Sad 	return mtx->mtx_owner != 0;
1411.2Sad}
1421.2Sad
1431.2Sadstatic inline int
1441.2SadMUTEX_HAS_WAITERS(volatile kmutex_t *mtx)
1451.2Sad{
1461.2Sad	if (mtx->mtx_owner == 0)
1471.2Sad		return 0;
1481.2Sad	return mtx->mtx_lock == __SIMPLELOCK_LOCKED;
1491.2Sad}
1501.2Sad
1511.2Sadstatic inline void
1521.6SyamtMUTEX_INITIALIZE_SPIN(kmutex_t *mtx, bool dodebug, int ipl)
1531.2Sad{
1541.6Syamt	mtx->mtx_dodebug = dodebug;
1551.6Syamt	mtx->mtx_isspin = 1;
1561.2Sad	mtx->mtx_ipl = makeiplcookie(ipl);
1571.2Sad	mtx->mtx_interlock = __SIMPLELOCK_LOCKED;
1581.2Sad	__cpu_simple_lock_init(&mtx->mtx_lock);
1591.2Sad}
1601.2Sad
1611.2Sadstatic inline void
1621.6SyamtMUTEX_INITIALIZE_ADAPTIVE(kmutex_t *mtx, bool dodebug)
1631.2Sad{
1641.6Syamt	mtx->mtx_dodebug = dodebug;
1651.6Syamt	mtx->mtx_isspin = 0;
1661.2Sad	__cpu_simple_lock_init(&mtx->mtx_lock);
1671.2Sad}
1681.2Sad
1691.2Sadstatic inline void
1701.2SadMUTEX_DESTROY(kmutex_t *mtx)
1711.2Sad{
1721.2Sad	mtx->mtx_owner = (uintptr_t)-1L;
1731.2Sad}
1741.2Sad
1751.6Syamtstatic inline bool
1761.6SyamtMUTEX_DEBUG_P(kmutex_t *mtx)
1771.2Sad{
1781.6Syamt	return mtx->mtx_dodebug != 0;
1791.2Sad}
1801.2Sad
1811.2Sadstatic inline int
1821.2SadMUTEX_SPIN_P(volatile kmutex_t *mtx)
1831.2Sad{
1841.6Syamt	return mtx->mtx_isspin != 0;
1851.2Sad}
1861.2Sad
1871.2Sadstatic inline int
1881.2SadMUTEX_ADAPTIVE_P(volatile kmutex_t *mtx)
1891.2Sad{
1901.6Syamt	return mtx->mtx_isspin == 0;
1911.2Sad}
1921.2Sad
1931.2Sadstatic inline int
1941.2SadMUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t curthread)
1951.2Sad{
1961.2Sad	if (!__cpu_simple_lock_try(&mtx->mtx_interlock))
1971.2Sad		return 0;
1981.2Sad	mtx->mtx_owner = (curthread >> 5) | 0xf8000000;
1991.2Sad	return 1;
2001.2Sad}
2011.2Sad
2021.2Sadstatic inline void
2031.2SadMUTEX_RELEASE(kmutex_t *mtx)
2041.2Sad{
2051.2Sad	mtx->mtx_owner = 0;
2061.2Sad	__cpu_simple_unlock(&mtx->mtx_lock);
2071.2Sad}
2081.2Sad
2091.3Sadstatic inline void
2101.3SadMUTEX_CLEAR_WAITERS(kmutex_t *mtx)
2111.3Sad{
2121.3Sad	__cpu_simple_unlock(&mtx->mtx_lock);
2131.3Sad}
2141.3Sad
2151.2Sad#endif	/* __MUTEX_PRIVATE */
2161.2Sad
2171.2Sad#endif /* _SPARC_MUTEX_H_ */
218