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 55834Spt157919 * Common Development and Distribution License (the "License"). 65834Spt157919 * 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*9489SJoe.Bonasera@sun.com * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #ifndef _SYS_MUTEX_IMPL_H 270Sstevel@tonic-gate #define _SYS_MUTEX_IMPL_H 280Sstevel@tonic-gate 290Sstevel@tonic-gate #ifndef _ASM 300Sstevel@tonic-gate #include <sys/types.h> 310Sstevel@tonic-gate #include <sys/machlock.h> 320Sstevel@tonic-gate #endif 330Sstevel@tonic-gate 340Sstevel@tonic-gate #ifdef __cplusplus 350Sstevel@tonic-gate extern "C" { 360Sstevel@tonic-gate #endif 370Sstevel@tonic-gate 380Sstevel@tonic-gate #ifndef _ASM 390Sstevel@tonic-gate 400Sstevel@tonic-gate /* 410Sstevel@tonic-gate * mutex_enter() assumes that the mutex is adaptive and tries to grab the 420Sstevel@tonic-gate * lock by doing a atomic compare and exchange on the first word of the mutex. 430Sstevel@tonic-gate * If the compare and exchange fails, it means that either (1) the lock is a 440Sstevel@tonic-gate * spin lock, or (2) the lock is adaptive but already held. 450Sstevel@tonic-gate * mutex_vector_enter() distinguishes these cases by looking at the mutex 460Sstevel@tonic-gate * type, which is encoded in the low-order bits of the owner field. 470Sstevel@tonic-gate */ 480Sstevel@tonic-gate typedef union mutex_impl { 490Sstevel@tonic-gate /* 500Sstevel@tonic-gate * Adaptive mutex. 510Sstevel@tonic-gate */ 520Sstevel@tonic-gate struct adaptive_mutex { 530Sstevel@tonic-gate uintptr_t _m_owner; /* 0-3/0-7 owner and waiters bit */ 540Sstevel@tonic-gate #ifndef _LP64 550Sstevel@tonic-gate uintptr_t _m_filler; /* 4-7 unused */ 560Sstevel@tonic-gate #endif 570Sstevel@tonic-gate } m_adaptive; 580Sstevel@tonic-gate 590Sstevel@tonic-gate /* 600Sstevel@tonic-gate * Spin Mutex. 610Sstevel@tonic-gate */ 620Sstevel@tonic-gate struct spin_mutex { 630Sstevel@tonic-gate lock_t m_dummylock; /* 0 dummy lock (always set) */ 640Sstevel@tonic-gate lock_t m_spinlock; /* 1 real lock */ 650Sstevel@tonic-gate ushort_t m_filler; /* 2-3 unused */ 660Sstevel@tonic-gate ushort_t m_oldspl; /* 4-5 old pil value */ 670Sstevel@tonic-gate ushort_t m_minspl; /* 6-7 min pil val if lock held */ 680Sstevel@tonic-gate } m_spin; 690Sstevel@tonic-gate 700Sstevel@tonic-gate } mutex_impl_t; 710Sstevel@tonic-gate 720Sstevel@tonic-gate #define m_owner m_adaptive._m_owner 730Sstevel@tonic-gate 746617Sck142721 #define MUTEX_ALIGN _LONG_ALIGNMENT 756617Sck142721 #define MUTEX_ALIGN_WARNINGS 10 /* num of warnings to issue */ 766617Sck142721 770Sstevel@tonic-gate #define MUTEX_WAITERS 0x1 780Sstevel@tonic-gate #define MUTEX_DEAD 0x6 790Sstevel@tonic-gate #define MUTEX_THREAD (-0x8) 800Sstevel@tonic-gate 810Sstevel@tonic-gate #define MUTEX_OWNER(lp) ((kthread_id_t)((lp)->m_owner & MUTEX_THREAD)) 820Sstevel@tonic-gate #define MUTEX_NO_OWNER ((kthread_id_t)NULL) 830Sstevel@tonic-gate 840Sstevel@tonic-gate #define MUTEX_SET_WAITERS(lp) \ 850Sstevel@tonic-gate { \ 860Sstevel@tonic-gate uintptr_t old; \ 870Sstevel@tonic-gate while ((old = (lp)->m_owner) != 0 && \ 880Sstevel@tonic-gate casip(&(lp)->m_owner, old, old | MUTEX_WAITERS) != old) \ 890Sstevel@tonic-gate continue; \ 900Sstevel@tonic-gate } 910Sstevel@tonic-gate 920Sstevel@tonic-gate #define MUTEX_HAS_WAITERS(lp) ((lp)->m_owner & MUTEX_WAITERS) 930Sstevel@tonic-gate #define MUTEX_CLEAR_LOCK_AND_WAITERS(lp) (lp)->m_owner = 0 940Sstevel@tonic-gate 950Sstevel@tonic-gate #define MUTEX_SET_TYPE(lp, type) 960Sstevel@tonic-gate #define MUTEX_TYPE_ADAPTIVE(lp) (((lp)->m_owner & MUTEX_DEAD) == 0) 970Sstevel@tonic-gate #define MUTEX_TYPE_SPIN(lp) ((lp)->m_spin.m_dummylock == LOCK_HELD_VALUE) 980Sstevel@tonic-gate 990Sstevel@tonic-gate #define MUTEX_DESTROY(lp) \ 1000Sstevel@tonic-gate (lp)->m_owner = ((uintptr_t)curthread | MUTEX_DEAD) 1015834Spt157919 /* mutex backoff delay macro and constants */ 1025834Spt157919 #define MUTEX_BACKOFF_BASE 1 1035834Spt157919 #define MUTEX_BACKOFF_SHIFT 2 1045834Spt157919 #define MUTEX_CAP_FACTOR 64 1055834Spt157919 #define MUTEX_DELAY() { \ 1065834Spt157919 mutex_delay(); \ 1075834Spt157919 SMT_PAUSE(); \ 1085834Spt157919 } 1095834Spt157919 1105834Spt157919 /* low overhead clock read */ 1115834Spt157919 #define MUTEX_GETTICK() tsc_read() 1125834Spt157919 extern void null_xcall(void); 1135834Spt157919 #define MUTEX_SYNC() { \ 1145834Spt157919 cpuset_t set; \ 1155834Spt157919 CPUSET_ALL(set); \ 116*9489SJoe.Bonasera@sun.com xc_call(0, 0, 0, CPUSET2BV(set), \ 1175834Spt157919 (xc_func_t)null_xcall); \ 1185834Spt157919 } 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate extern int mutex_adaptive_tryenter(mutex_impl_t *); 1215834Spt157919 extern void *mutex_owner_running(mutex_impl_t *); 1225834Spt157919 1235834Spt157919 #else /* _ASM */ 1245834Spt157919 1255834Spt157919 #define MUTEX_THREAD -0x8 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate #endif /* _ASM */ 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate #ifdef __cplusplus 1300Sstevel@tonic-gate } 1310Sstevel@tonic-gate #endif 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate #endif /* _SYS_MUTEX_IMPL_H */ 134