xref: /illumos-gate/usr/src/uts/common/sys/mutex.h (revision bbf21555)
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 /*
22cb15d5d9SPeter Rival  * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate #ifndef _SYS_MUTEX_H
267c478bd9Sstevel@tonic-gate #define	_SYS_MUTEX_H
287c478bd9Sstevel@tonic-gate #ifndef	_ASM
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #endif
327c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
337c478bd9Sstevel@tonic-gate extern "C" {
347c478bd9Sstevel@tonic-gate #endif
367c478bd9Sstevel@tonic-gate #ifndef	_ASM
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * Public interface to mutual exclusion locks.  See mutex(9F) for details.
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  * The basic mutex type is MUTEX_ADAPTIVE, which is expected to be used
427c478bd9Sstevel@tonic-gate  * in almost all of the kernel.  MUTEX_SPIN provides interrupt blocking
437c478bd9Sstevel@tonic-gate  * and must be used in interrupt handlers above LOCK_LEVEL.  The iblock
447c478bd9Sstevel@tonic-gate  * cookie argument to mutex_init() encodes the interrupt level to block.
457c478bd9Sstevel@tonic-gate  * The iblock cookie must be NULL for adaptive locks.
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  * MUTEX_DEFAULT is the type usually specified (except in drivers) to
487c478bd9Sstevel@tonic-gate  * mutex_init().  It is identical to MUTEX_ADAPTIVE.
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * MUTEX_DRIVER is always used by drivers.  mutex_init() converts this to
517c478bd9Sstevel@tonic-gate  * either MUTEX_ADAPTIVE or MUTEX_SPIN depending on the iblock cookie.
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  * Mutex statistics can be gathered on the fly, without rebooting or
54*bbf21555SRichard Lowe  * recompiling the kernel, via the lockstat driver (lockstat(4D)).
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate typedef enum {
577c478bd9Sstevel@tonic-gate 	MUTEX_ADAPTIVE = 0,	/* spin if owner is running, otherwise block */
587c478bd9Sstevel@tonic-gate 	MUTEX_SPIN = 1,		/* block interrupts and spin */
597c478bd9Sstevel@tonic-gate 	MUTEX_DRIVER = 4,	/* driver (DDI) mutex */
607c478bd9Sstevel@tonic-gate 	MUTEX_DEFAULT = 6	/* kernel default mutex */
617c478bd9Sstevel@tonic-gate } kmutex_type_t;
637c478bd9Sstevel@tonic-gate typedef struct mutex {
647c478bd9Sstevel@tonic-gate #ifdef _LP64
657c478bd9Sstevel@tonic-gate 	void	*_opaque[1];
667c478bd9Sstevel@tonic-gate #else
677c478bd9Sstevel@tonic-gate 	void	*_opaque[2];
687c478bd9Sstevel@tonic-gate #endif
697c478bd9Sstevel@tonic-gate } kmutex_t;
717c478bd9Sstevel@tonic-gate #ifdef _KERNEL
73cb15d5d9SPeter Rival /*
74cb15d5d9SPeter Rival  * A padded mutex, one per 64 byte cache line.  Use when false sharing is
75cb15d5d9SPeter Rival  * an issue but beware of the extra memory it uses.  Consumers may want to
76cb15d5d9SPeter Rival  * consider aligning their pad_mutex_t's to a cache line boundary as well.
77cb15d5d9SPeter Rival  */
78cb15d5d9SPeter Rival typedef struct pad_mutex {
79cb15d5d9SPeter Rival 	kmutex_t	pad_mutex;
80cb15d5d9SPeter Rival #ifdef _LP64
81cb15d5d9SPeter Rival 	char		pad_pad[64 - sizeof (kmutex_t)];
82cb15d5d9SPeter Rival #endif
83cb15d5d9SPeter Rival } pad_mutex_t;
84cb15d5d9SPeter Rival 
857c478bd9Sstevel@tonic-gate #define	MUTEX_HELD(x)		(mutex_owned(x))
86753a6d45SSherry Moore #define	MUTEX_NOT_HELD(x)	(!mutex_owned(x) || panicstr || quiesce_active)
887c478bd9Sstevel@tonic-gate extern	void	mutex_init(kmutex_t *, char *, kmutex_type_t, void *);
897c478bd9Sstevel@tonic-gate extern	void	mutex_destroy(kmutex_t *);
907c478bd9Sstevel@tonic-gate extern	void	mutex_enter(kmutex_t *);
917c478bd9Sstevel@tonic-gate extern	int	mutex_tryenter(kmutex_t *);
927c478bd9Sstevel@tonic-gate extern	void	mutex_exit(kmutex_t *);
93b5fca8f8Stomee extern	int	mutex_owned(const kmutex_t *);
94b5fca8f8Stomee extern	struct _kthread *mutex_owner(const kmutex_t *);
96575a7426Spt extern  ushort_t mutex_backoff_base;
97575a7426Spt extern  uint_t mutex_backoff_cap;
98575a7426Spt extern  ushort_t mutex_cap_factor;
99575a7426Spt extern  uchar_t mutex_backoff_shift;
100575a7426Spt extern  void (*mutex_lock_delay)(uint_t);
101575a7426Spt extern  uint_t (*mutex_lock_backoff)(uint_t);
102575a7426Spt extern  void (*mutex_delay)(void);
103575a7426Spt extern  void mutex_delay_default(void);
104575a7426Spt extern  void mutex_sync(void);
106374ae87fSsvemuri extern	void default_lock_delay(uint_t);
107374ae87fSsvemuri extern	uint_t default_lock_backoff(uint_t);
1097c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
1117c478bd9Sstevel@tonic-gate #endif	/* _ASM */
1137c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate #endif
1177c478bd9Sstevel@tonic-gate #endif	/* _SYS_MUTEX_H */