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