xref: /illumos-gate/usr/src/uts/common/sys/mutex.h (revision 93a18d6d401e844455263f926578e9d2aa6b47ec)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _SYS_MUTEX_H
27 #define	_SYS_MUTEX_H
28 
29 #ifndef	_ASM
30 #include <sys/types.h>
31 #endif
32 
33 #ifdef	__cplusplus
34 extern "C" {
35 #endif
36 
37 #ifndef	_ASM
38 
39 /*
40  * Public interface to mutual exclusion locks.  See mutex(9F) for details.
41  *
42  * The basic mutex type is MUTEX_ADAPTIVE, which is expected to be used
43  * in almost all of the kernel.  MUTEX_SPIN provides interrupt blocking
44  * and must be used in interrupt handlers above LOCK_LEVEL.  The iblock
45  * cookie argument to mutex_init() encodes the interrupt level to block.
46  * The iblock cookie must be NULL for adaptive locks.
47  *
48  * MUTEX_DEFAULT is the type usually specified (except in drivers) to
49  * mutex_init().  It is identical to MUTEX_ADAPTIVE.
50  *
51  * MUTEX_DRIVER is always used by drivers.  mutex_init() converts this to
52  * either MUTEX_ADAPTIVE or MUTEX_SPIN depending on the iblock cookie.
53  *
54  * Mutex statistics can be gathered on the fly, without rebooting or
55  * recompiling the kernel, via the lockstat driver (lockstat(7D)).
56  */
57 typedef enum {
58 	MUTEX_ADAPTIVE = 0,	/* spin if owner is running, otherwise block */
59 	MUTEX_SPIN = 1,		/* block interrupts and spin */
60 	MUTEX_DRIVER = 4,	/* driver (DDI) mutex */
61 	MUTEX_DEFAULT = 6	/* kernel default mutex */
62 } kmutex_type_t;
63 
64 typedef struct mutex {
65 #ifdef _LP64
66 	void	*_opaque[1];
67 #else
68 	void	*_opaque[2];
69 #endif
70 } kmutex_t;
71 
72 #ifdef _KERNEL
73 
74 #define	MUTEX_HELD(x)		(mutex_owned(x))
75 #define	MUTEX_NOT_HELD(x)	(!mutex_owned(x) || panicstr || quiesce_active)
76 
77 extern	void	mutex_init(kmutex_t *, char *, kmutex_type_t, void *);
78 extern	void	mutex_destroy(kmutex_t *);
79 extern	void	mutex_enter(kmutex_t *);
80 extern	int	mutex_tryenter(kmutex_t *);
81 extern	void	mutex_exit(kmutex_t *);
82 extern	int	mutex_owned(const kmutex_t *);
83 extern	struct _kthread *mutex_owner(const kmutex_t *);
84 
85 extern  ushort_t mutex_backoff_base;
86 extern  uint_t mutex_backoff_cap;
87 extern  ushort_t mutex_cap_factor;
88 extern  uchar_t mutex_backoff_shift;
89 extern  void (*mutex_lock_delay)(uint_t);
90 extern  uint_t (*mutex_lock_backoff)(uint_t);
91 extern  void (*mutex_delay)(void);
92 extern  void mutex_delay_default(void);
93 extern  void mutex_sync(void);
94 
95 extern	void default_lock_delay(uint_t);
96 extern	uint_t default_lock_backoff(uint_t);
97 
98 #endif	/* _KERNEL */
99 
100 #endif	/* _ASM */
101 
102 #ifdef	__cplusplus
103 }
104 #endif
105 
106 #endif	/* _SYS_MUTEX_H */
107