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