xref: /onnv-gate/usr/src/uts/common/sys/mutex.h (revision 12230:12df01648ca5)
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
53914Spm145316  * Common Development and Distribution License (the "License").
63914Spm145316  * 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*12230SFrank.Rival@oracle.com  * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #ifndef _SYS_MUTEX_H
260Sstevel@tonic-gate #define	_SYS_MUTEX_H
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #ifndef	_ASM
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #endif
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #ifdef	__cplusplus
330Sstevel@tonic-gate extern "C" {
340Sstevel@tonic-gate #endif
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #ifndef	_ASM
370Sstevel@tonic-gate 
380Sstevel@tonic-gate /*
390Sstevel@tonic-gate  * Public interface to mutual exclusion locks.  See mutex(9F) for details.
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * The basic mutex type is MUTEX_ADAPTIVE, which is expected to be used
420Sstevel@tonic-gate  * in almost all of the kernel.  MUTEX_SPIN provides interrupt blocking
430Sstevel@tonic-gate  * and must be used in interrupt handlers above LOCK_LEVEL.  The iblock
440Sstevel@tonic-gate  * cookie argument to mutex_init() encodes the interrupt level to block.
450Sstevel@tonic-gate  * The iblock cookie must be NULL for adaptive locks.
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  * MUTEX_DEFAULT is the type usually specified (except in drivers) to
480Sstevel@tonic-gate  * mutex_init().  It is identical to MUTEX_ADAPTIVE.
490Sstevel@tonic-gate  *
500Sstevel@tonic-gate  * MUTEX_DRIVER is always used by drivers.  mutex_init() converts this to
510Sstevel@tonic-gate  * either MUTEX_ADAPTIVE or MUTEX_SPIN depending on the iblock cookie.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * Mutex statistics can be gathered on the fly, without rebooting or
540Sstevel@tonic-gate  * recompiling the kernel, via the lockstat driver (lockstat(7D)).
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate typedef enum {
570Sstevel@tonic-gate 	MUTEX_ADAPTIVE = 0,	/* spin if owner is running, otherwise block */
580Sstevel@tonic-gate 	MUTEX_SPIN = 1,		/* block interrupts and spin */
590Sstevel@tonic-gate 	MUTEX_DRIVER = 4,	/* driver (DDI) mutex */
600Sstevel@tonic-gate 	MUTEX_DEFAULT = 6	/* kernel default mutex */
610Sstevel@tonic-gate } kmutex_type_t;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate typedef struct mutex {
640Sstevel@tonic-gate #ifdef _LP64
650Sstevel@tonic-gate 	void	*_opaque[1];
660Sstevel@tonic-gate #else
670Sstevel@tonic-gate 	void	*_opaque[2];
680Sstevel@tonic-gate #endif
690Sstevel@tonic-gate } kmutex_t;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate #ifdef _KERNEL
720Sstevel@tonic-gate 
73*12230SFrank.Rival@oracle.com /*
74*12230SFrank.Rival@oracle.com  * A padded mutex, one per 64 byte cache line.  Use when false sharing is
75*12230SFrank.Rival@oracle.com  * an issue but beware of the extra memory it uses.  Consumers may want to
76*12230SFrank.Rival@oracle.com  * consider aligning their pad_mutex_t's to a cache line boundary as well.
77*12230SFrank.Rival@oracle.com  */
78*12230SFrank.Rival@oracle.com typedef struct pad_mutex {
79*12230SFrank.Rival@oracle.com 	kmutex_t	pad_mutex;
80*12230SFrank.Rival@oracle.com #ifdef _LP64
81*12230SFrank.Rival@oracle.com 	char		pad_pad[64 - sizeof (kmutex_t)];
82*12230SFrank.Rival@oracle.com #endif
83*12230SFrank.Rival@oracle.com } pad_mutex_t;
84*12230SFrank.Rival@oracle.com 
850Sstevel@tonic-gate #define	MUTEX_HELD(x)		(mutex_owned(x))
869160SSherry.Moore@Sun.COM #define	MUTEX_NOT_HELD(x)	(!mutex_owned(x) || panicstr || quiesce_active)
870Sstevel@tonic-gate 
880Sstevel@tonic-gate extern	void	mutex_init(kmutex_t *, char *, kmutex_type_t, void *);
890Sstevel@tonic-gate extern	void	mutex_destroy(kmutex_t *);
900Sstevel@tonic-gate extern	void	mutex_enter(kmutex_t *);
910Sstevel@tonic-gate extern	int	mutex_tryenter(kmutex_t *);
920Sstevel@tonic-gate extern	void	mutex_exit(kmutex_t *);
936712Stomee extern	int	mutex_owned(const kmutex_t *);
946712Stomee extern	struct _kthread *mutex_owner(const kmutex_t *);
955834Spt157919 
965834Spt157919 extern  ushort_t mutex_backoff_base;
975834Spt157919 extern  uint_t mutex_backoff_cap;
985834Spt157919 extern  ushort_t mutex_cap_factor;
995834Spt157919 extern  uchar_t mutex_backoff_shift;
1005834Spt157919 extern  void (*mutex_lock_delay)(uint_t);
1015834Spt157919 extern  uint_t (*mutex_lock_backoff)(uint_t);
1025834Spt157919 extern  void (*mutex_delay)(void);
1035834Spt157919 extern  void mutex_delay_default(void);
1045834Spt157919 extern  void mutex_sync(void);
1050Sstevel@tonic-gate 
1066138Ssvemuri extern	void default_lock_delay(uint_t);
1076138Ssvemuri extern	uint_t default_lock_backoff(uint_t);
1086138Ssvemuri 
1090Sstevel@tonic-gate #endif	/* _KERNEL */
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate #endif	/* _ASM */
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate #ifdef	__cplusplus
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate #endif
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate #endif	/* _SYS_MUTEX_H */
118