xref: /illumos-gate/usr/src/uts/common/sys/mutex.h (revision b5fca8f8)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5e603b7d4Spm  * Common Development and Distribution License (the "License").
6e603b7d4Spm  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22575a7426Spt  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23e603b7d4Spm  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #ifndef _SYS_MUTEX_H
277c478bd9Sstevel@tonic-gate #define	_SYS_MUTEX_H
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #ifndef	_ASM
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #endif
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
367c478bd9Sstevel@tonic-gate extern "C" {
377c478bd9Sstevel@tonic-gate #endif
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #ifndef	_ASM
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * Public interface to mutual exclusion locks.  See mutex(9F) for details.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * The basic mutex type is MUTEX_ADAPTIVE, which is expected to be used
457c478bd9Sstevel@tonic-gate  * in almost all of the kernel.  MUTEX_SPIN provides interrupt blocking
467c478bd9Sstevel@tonic-gate  * and must be used in interrupt handlers above LOCK_LEVEL.  The iblock
477c478bd9Sstevel@tonic-gate  * cookie argument to mutex_init() encodes the interrupt level to block.
487c478bd9Sstevel@tonic-gate  * The iblock cookie must be NULL for adaptive locks.
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * MUTEX_DEFAULT is the type usually specified (except in drivers) to
517c478bd9Sstevel@tonic-gate  * mutex_init().  It is identical to MUTEX_ADAPTIVE.
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  * MUTEX_DRIVER is always used by drivers.  mutex_init() converts this to
547c478bd9Sstevel@tonic-gate  * either MUTEX_ADAPTIVE or MUTEX_SPIN depending on the iblock cookie.
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  * Mutex statistics can be gathered on the fly, without rebooting or
577c478bd9Sstevel@tonic-gate  * recompiling the kernel, via the lockstat driver (lockstat(7D)).
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate typedef enum {
607c478bd9Sstevel@tonic-gate 	MUTEX_ADAPTIVE = 0,	/* spin if owner is running, otherwise block */
617c478bd9Sstevel@tonic-gate 	MUTEX_SPIN = 1,		/* block interrupts and spin */
627c478bd9Sstevel@tonic-gate 	MUTEX_DRIVER = 4,	/* driver (DDI) mutex */
637c478bd9Sstevel@tonic-gate 	MUTEX_DEFAULT = 6	/* kernel default mutex */
647c478bd9Sstevel@tonic-gate } kmutex_type_t;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate typedef struct mutex {
677c478bd9Sstevel@tonic-gate #ifdef _LP64
687c478bd9Sstevel@tonic-gate 	void	*_opaque[1];
697c478bd9Sstevel@tonic-gate #else
707c478bd9Sstevel@tonic-gate 	void	*_opaque[2];
717c478bd9Sstevel@tonic-gate #endif
727c478bd9Sstevel@tonic-gate } kmutex_t;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #ifdef _KERNEL
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #define	MUTEX_HELD(x)		(mutex_owned(x))
777c478bd9Sstevel@tonic-gate #define	MUTEX_NOT_HELD(x)	(!mutex_owned(x) || panicstr)
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate extern	void	mutex_init(kmutex_t *, char *, kmutex_type_t, void *);
807c478bd9Sstevel@tonic-gate extern	void	mutex_destroy(kmutex_t *);
817c478bd9Sstevel@tonic-gate extern	void	mutex_enter(kmutex_t *);
827c478bd9Sstevel@tonic-gate extern	int	mutex_tryenter(kmutex_t *);
837c478bd9Sstevel@tonic-gate extern	void	mutex_exit(kmutex_t *);
84*b5fca8f8Stomee extern	int	mutex_owned(const kmutex_t *);
85*b5fca8f8Stomee extern	struct _kthread *mutex_owner(const kmutex_t *);
86575a7426Spt 
87575a7426Spt extern  ushort_t mutex_backoff_base;
88575a7426Spt extern  uint_t mutex_backoff_cap;
89575a7426Spt extern  ushort_t mutex_cap_factor;
90575a7426Spt extern  uchar_t mutex_backoff_shift;
91575a7426Spt extern  void (*mutex_lock_delay)(uint_t);
92575a7426Spt extern  uint_t (*mutex_lock_backoff)(uint_t);
93575a7426Spt extern  void (*mutex_delay)(void);
94575a7426Spt extern  void mutex_delay_default(void);
95575a7426Spt extern  void mutex_sync(void);
967c478bd9Sstevel@tonic-gate 
97374ae87fSsvemuri extern	void default_lock_delay(uint_t);
98374ae87fSsvemuri extern	uint_t default_lock_backoff(uint_t);
99374ae87fSsvemuri 
1007c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate #endif	/* _ASM */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate #endif
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate #endif	/* _SYS_MUTEX_H */
109