xref: /onnv-gate/usr/src/uts/sparc/v9/sys/mutex_impl.h (revision 5834:66e26b3fbcc7)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*5834Spt157919  * Common Development and Distribution License (the "License").
6*5834Spt157919  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*5834Spt157919  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #ifndef _SYS_MUTEX_IMPL_H
270Sstevel@tonic-gate #define	_SYS_MUTEX_IMPL_H
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #ifndef	_ASM
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/machlock.h>
340Sstevel@tonic-gate #endif
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #ifdef	__cplusplus
370Sstevel@tonic-gate extern "C" {
380Sstevel@tonic-gate #endif
390Sstevel@tonic-gate 
40*5834Spt157919 #define	MUTEX_THREAD	(-0x8)
410Sstevel@tonic-gate #ifndef	_ASM
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate  * mutex_enter() assumes that the mutex is adaptive and tries to grab the
450Sstevel@tonic-gate  * lock by doing a cas on the first word of the mutex.  If the cas fails,
460Sstevel@tonic-gate  * it means that either (1) the lock is a spin lock, or (2) the lock is
470Sstevel@tonic-gate  * adaptive but already held.  mutex_vector_enter() distinguishes these
480Sstevel@tonic-gate  * cases by looking at the mutex type, which is encoded in the low-order
490Sstevel@tonic-gate  * bits of the owner field.
500Sstevel@tonic-gate  */
510Sstevel@tonic-gate typedef union mutex_impl {
520Sstevel@tonic-gate 	/*
530Sstevel@tonic-gate 	 * Adaptive mutex.
540Sstevel@tonic-gate 	 */
550Sstevel@tonic-gate 	struct adaptive_mutex {
560Sstevel@tonic-gate 		uintptr_t _m_owner;	/* 0-3/0-7 owner and waiters bit */
570Sstevel@tonic-gate 	} m_adaptive;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	/*
600Sstevel@tonic-gate 	 * Spin Mutex.
610Sstevel@tonic-gate 	 */
620Sstevel@tonic-gate 	struct spin_mutex {
630Sstevel@tonic-gate 		ushort_t m_oldspl;	/* 0-1	old %pil value */
640Sstevel@tonic-gate 		ushort_t m_minspl;	/* 2-3	min %pil val if lock held */
650Sstevel@tonic-gate 		ushort_t m_filler;	/* 4-5	unused */
660Sstevel@tonic-gate 		lock_t	m_spinlock;	/* 6	real lock */
670Sstevel@tonic-gate 		lock_t	m_dummylock;	/* 7	dummy lock (always set) */
680Sstevel@tonic-gate 	} m_spin;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate } mutex_impl_t;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #define	m_owner	m_adaptive._m_owner
730Sstevel@tonic-gate 
740Sstevel@tonic-gate #define	MUTEX_WAITERS		0x1
750Sstevel@tonic-gate #define	MUTEX_DEAD		0x6
760Sstevel@tonic-gate #define	MUTEX_THREAD		(-0x8)
770Sstevel@tonic-gate 
780Sstevel@tonic-gate #define	MUTEX_OWNER(lp)		((kthread_id_t)((lp)->m_owner & MUTEX_THREAD))
790Sstevel@tonic-gate #define	MUTEX_NO_OWNER		((kthread_id_t)NULL)
800Sstevel@tonic-gate 
810Sstevel@tonic-gate #define	MUTEX_SET_WAITERS(lp)						\
820Sstevel@tonic-gate {									\
830Sstevel@tonic-gate 	uintptr_t old;							\
840Sstevel@tonic-gate 	while ((old = (lp)->m_owner) != 0 &&				\
850Sstevel@tonic-gate 	    casip(&(lp)->m_owner, old, old | MUTEX_WAITERS) != old)	\
860Sstevel@tonic-gate 		continue;						\
870Sstevel@tonic-gate }
880Sstevel@tonic-gate 
890Sstevel@tonic-gate #define	MUTEX_HAS_WAITERS(lp)			((lp)->m_owner & MUTEX_WAITERS)
900Sstevel@tonic-gate #define	MUTEX_CLEAR_LOCK_AND_WAITERS(lp)	(lp)->m_owner = 0
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #define	MUTEX_SET_TYPE(lp, type)
930Sstevel@tonic-gate #define	MUTEX_TYPE_ADAPTIVE(lp)	(((lp)->m_owner & MUTEX_DEAD) == 0)
940Sstevel@tonic-gate #define	MUTEX_TYPE_SPIN(lp)	((lp)->m_spin.m_dummylock == LOCK_HELD_VALUE)
950Sstevel@tonic-gate 
960Sstevel@tonic-gate #define	MUTEX_DESTROY(lp)	\
970Sstevel@tonic-gate 	(lp)->m_owner = ((uintptr_t)curthread | MUTEX_DEAD)
980Sstevel@tonic-gate 
99*5834Spt157919 #define	MUTEX_BACKOFF_BASE	1
100*5834Spt157919 #define	MUTEX_BACKOFF_SHIFT	1
101*5834Spt157919 #define	MUTEX_CAP_FACTOR	8
102*5834Spt157919 #define	MUTEX_DELAY()	{ \
103*5834Spt157919 				mutex_delay(); \
104*5834Spt157919 			}
105*5834Spt157919 
106*5834Spt157919 /* low-overhead clock read */
107*5834Spt157919 extern u_longlong_t gettick(void);
108*5834Spt157919 #define	MUTEX_GETTICK()	gettick()
109*5834Spt157919 extern void null_xcall(void);
110*5834Spt157919 #define	MUTEX_SYNC()	xc_all((xcfunc_t *)null_xcall, 0, 0)
111*5834Spt157919 
112*5834Spt157919 extern void cas_delay(void *);
113*5834Spt157919 extern void rdccr_delay(void);
1140Sstevel@tonic-gate extern int mutex_adaptive_tryenter(mutex_impl_t *);
115*5834Spt157919 extern void *mutex_owner_running(mutex_impl_t *);
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate #endif	/* _ASM */
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate #ifdef	__cplusplus
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate #endif
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate #endif	/* _SYS_MUTEX_IMPL_H */
124