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 5e603b7d4Spm145316 * Common Development and Distribution License (the "License"). 6e603b7d4Spm145316 * 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 /* 22*753a6d45SSherry Moore * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23e603b7d4Spm145316 * 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 #ifndef _ASM 307c478bd9Sstevel@tonic-gate #include <sys/types.h> 317c478bd9Sstevel@tonic-gate #endif 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #ifdef __cplusplus 347c478bd9Sstevel@tonic-gate extern "C" { 357c478bd9Sstevel@tonic-gate #endif 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate #ifndef _ASM 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate /* 407c478bd9Sstevel@tonic-gate * Public interface to mutual exclusion locks. See mutex(9F) for details. 417c478bd9Sstevel@tonic-gate * 427c478bd9Sstevel@tonic-gate * The basic mutex type is MUTEX_ADAPTIVE, which is expected to be used 437c478bd9Sstevel@tonic-gate * in almost all of the kernel. MUTEX_SPIN provides interrupt blocking 447c478bd9Sstevel@tonic-gate * and must be used in interrupt handlers above LOCK_LEVEL. The iblock 457c478bd9Sstevel@tonic-gate * cookie argument to mutex_init() encodes the interrupt level to block. 467c478bd9Sstevel@tonic-gate * The iblock cookie must be NULL for adaptive locks. 477c478bd9Sstevel@tonic-gate * 487c478bd9Sstevel@tonic-gate * MUTEX_DEFAULT is the type usually specified (except in drivers) to 497c478bd9Sstevel@tonic-gate * mutex_init(). It is identical to MUTEX_ADAPTIVE. 507c478bd9Sstevel@tonic-gate * 517c478bd9Sstevel@tonic-gate * MUTEX_DRIVER is always used by drivers. mutex_init() converts this to 527c478bd9Sstevel@tonic-gate * either MUTEX_ADAPTIVE or MUTEX_SPIN depending on the iblock cookie. 537c478bd9Sstevel@tonic-gate * 547c478bd9Sstevel@tonic-gate * Mutex statistics can be gathered on the fly, without rebooting or 557c478bd9Sstevel@tonic-gate * recompiling the kernel, via the lockstat driver (lockstat(7D)). 567c478bd9Sstevel@tonic-gate */ 577c478bd9Sstevel@tonic-gate typedef enum { 587c478bd9Sstevel@tonic-gate MUTEX_ADAPTIVE = 0, /* spin if owner is running, otherwise block */ 597c478bd9Sstevel@tonic-gate MUTEX_SPIN = 1, /* block interrupts and spin */ 607c478bd9Sstevel@tonic-gate MUTEX_DRIVER = 4, /* driver (DDI) mutex */ 617c478bd9Sstevel@tonic-gate MUTEX_DEFAULT = 6 /* kernel default mutex */ 627c478bd9Sstevel@tonic-gate } kmutex_type_t; 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate typedef struct mutex { 657c478bd9Sstevel@tonic-gate #ifdef _LP64 667c478bd9Sstevel@tonic-gate void *_opaque[1]; 677c478bd9Sstevel@tonic-gate #else 687c478bd9Sstevel@tonic-gate void *_opaque[2]; 697c478bd9Sstevel@tonic-gate #endif 707c478bd9Sstevel@tonic-gate } kmutex_t; 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate #ifdef _KERNEL 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate #define MUTEX_HELD(x) (mutex_owned(x)) 75*753a6d45SSherry Moore #define MUTEX_NOT_HELD(x) (!mutex_owned(x) || panicstr || quiesce_active) 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate extern void mutex_init(kmutex_t *, char *, kmutex_type_t, void *); 787c478bd9Sstevel@tonic-gate extern void mutex_destroy(kmutex_t *); 797c478bd9Sstevel@tonic-gate extern void mutex_enter(kmutex_t *); 807c478bd9Sstevel@tonic-gate extern int mutex_tryenter(kmutex_t *); 817c478bd9Sstevel@tonic-gate extern void mutex_exit(kmutex_t *); 82b5fca8f8Stomee extern int mutex_owned(const kmutex_t *); 83b5fca8f8Stomee extern struct _kthread *mutex_owner(const kmutex_t *); 84575a7426Spt157919 85575a7426Spt157919 extern ushort_t mutex_backoff_base; 86575a7426Spt157919 extern uint_t mutex_backoff_cap; 87575a7426Spt157919 extern ushort_t mutex_cap_factor; 88575a7426Spt157919 extern uchar_t mutex_backoff_shift; 89575a7426Spt157919 extern void (*mutex_lock_delay)(uint_t); 90575a7426Spt157919 extern uint_t (*mutex_lock_backoff)(uint_t); 91575a7426Spt157919 extern void (*mutex_delay)(void); 92575a7426Spt157919 extern void mutex_delay_default(void); 93575a7426Spt157919 extern void mutex_sync(void); 947c478bd9Sstevel@tonic-gate 95374ae87fSsvemuri extern void default_lock_delay(uint_t); 96374ae87fSsvemuri extern uint_t default_lock_backoff(uint_t); 97374ae87fSsvemuri 987c478bd9Sstevel@tonic-gate #endif /* _KERNEL */ 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate #endif /* _ASM */ 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate #ifdef __cplusplus 1037c478bd9Sstevel@tonic-gate } 1047c478bd9Sstevel@tonic-gate #endif 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate #endif /* _SYS_MUTEX_H */ 107