xref: /titanic_41/usr/src/uts/intel/sys/mutex_impl.h (revision 2449e17f82f6097fd2c665b64723e31ceecbeca6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _SYS_MUTEX_IMPL_H
28 #define	_SYS_MUTEX_IMPL_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #ifndef	_ASM
33 #include <sys/types.h>
34 #include <sys/machlock.h>
35 #endif
36 
37 #ifdef	__cplusplus
38 extern "C" {
39 #endif
40 
41 #ifndef	_ASM
42 
43 /*
44  * mutex_enter() assumes that the mutex is adaptive and tries to grab the
45  * lock by doing a atomic compare and exchange on the first word of the mutex.
46  * If the compare and exchange fails, it means that either (1) the lock is a
47  * spin lock, or (2) the lock is adaptive but already held.
48  * mutex_vector_enter() distinguishes these cases by looking at the mutex
49  * type, which is encoded in the low-order bits of the owner field.
50  */
51 typedef union mutex_impl {
52 	/*
53 	 * Adaptive mutex.
54 	 */
55 	struct adaptive_mutex {
56 		uintptr_t _m_owner;	/* 0-3/0-7 owner and waiters bit */
57 #ifndef _LP64
58 		uintptr_t _m_filler;	/* 4-7 unused */
59 #endif
60 	} m_adaptive;
61 
62 	/*
63 	 * Spin Mutex.
64 	 */
65 	struct spin_mutex {
66 		lock_t	m_dummylock;	/* 0	dummy lock (always set) */
67 		lock_t	m_spinlock;	/* 1	real lock */
68 		ushort_t m_filler;	/* 2-3	unused */
69 		ushort_t m_oldspl;	/* 4-5	old pil value */
70 		ushort_t m_minspl;	/* 6-7	min pil val if lock held */
71 	} m_spin;
72 
73 } mutex_impl_t;
74 
75 #define	m_owner	m_adaptive._m_owner
76 
77 #define	MUTEX_WAITERS		0x1
78 #define	MUTEX_DEAD		0x6
79 #define	MUTEX_THREAD		(-0x8)
80 
81 #define	MUTEX_OWNER(lp)		((kthread_id_t)((lp)->m_owner & MUTEX_THREAD))
82 #define	MUTEX_NO_OWNER		((kthread_id_t)NULL)
83 
84 #define	MUTEX_SET_WAITERS(lp)						\
85 {									\
86 	uintptr_t old;							\
87 	while ((old = (lp)->m_owner) != 0 &&				\
88 	    casip(&(lp)->m_owner, old, old | MUTEX_WAITERS) != old)	\
89 		continue;						\
90 }
91 
92 #define	MUTEX_HAS_WAITERS(lp)			((lp)->m_owner & MUTEX_WAITERS)
93 #define	MUTEX_CLEAR_LOCK_AND_WAITERS(lp)	(lp)->m_owner = 0
94 
95 #define	MUTEX_SET_TYPE(lp, type)
96 #define	MUTEX_TYPE_ADAPTIVE(lp)	(((lp)->m_owner & MUTEX_DEAD) == 0)
97 #define	MUTEX_TYPE_SPIN(lp)	((lp)->m_spin.m_dummylock == LOCK_HELD_VALUE)
98 
99 #define	MUTEX_DESTROY(lp)	\
100 	(lp)->m_owner = ((uintptr_t)curthread | MUTEX_DEAD)
101 
102 extern int mutex_adaptive_tryenter(mutex_impl_t *);
103 
104 #endif	/* _ASM */
105 
106 #ifdef	__cplusplus
107 }
108 #endif
109 
110 #endif	/* _SYS_MUTEX_IMPL_H */
111