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 5*5834Spt157919 * Common Development and Distribution License (the "License"). 6*5834Spt157919 * 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*5834Spt157919 * Copyright 2008 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 #pragma ident "%Z%%M% %I% %E% SMI" 300Sstevel@tonic-gate 310Sstevel@tonic-gate #ifndef _ASM 320Sstevel@tonic-gate #include <sys/types.h> 330Sstevel@tonic-gate #include <sys/machlock.h> 340Sstevel@tonic-gate #endif 350Sstevel@tonic-gate 360Sstevel@tonic-gate #ifdef __cplusplus 370Sstevel@tonic-gate extern "C" { 380Sstevel@tonic-gate #endif 390Sstevel@tonic-gate 400Sstevel@tonic-gate #ifndef _ASM 410Sstevel@tonic-gate 420Sstevel@tonic-gate /* 430Sstevel@tonic-gate * mutex_enter() assumes that the mutex is adaptive and tries to grab the 440Sstevel@tonic-gate * lock by doing a atomic compare and exchange on the first word of the mutex. 450Sstevel@tonic-gate * If the compare and exchange fails, it means that either (1) the lock is a 460Sstevel@tonic-gate * spin lock, or (2) the lock is adaptive but already held. 470Sstevel@tonic-gate * mutex_vector_enter() distinguishes these cases by looking at the mutex 480Sstevel@tonic-gate * type, which is encoded in the low-order bits of the owner field. 490Sstevel@tonic-gate */ 500Sstevel@tonic-gate typedef union mutex_impl { 510Sstevel@tonic-gate /* 520Sstevel@tonic-gate * Adaptive mutex. 530Sstevel@tonic-gate */ 540Sstevel@tonic-gate struct adaptive_mutex { 550Sstevel@tonic-gate uintptr_t _m_owner; /* 0-3/0-7 owner and waiters bit */ 560Sstevel@tonic-gate #ifndef _LP64 570Sstevel@tonic-gate uintptr_t _m_filler; /* 4-7 unused */ 580Sstevel@tonic-gate #endif 590Sstevel@tonic-gate } m_adaptive; 600Sstevel@tonic-gate 610Sstevel@tonic-gate /* 620Sstevel@tonic-gate * Spin Mutex. 630Sstevel@tonic-gate */ 640Sstevel@tonic-gate struct spin_mutex { 650Sstevel@tonic-gate lock_t m_dummylock; /* 0 dummy lock (always set) */ 660Sstevel@tonic-gate lock_t m_spinlock; /* 1 real lock */ 670Sstevel@tonic-gate ushort_t m_filler; /* 2-3 unused */ 680Sstevel@tonic-gate ushort_t m_oldspl; /* 4-5 old pil value */ 690Sstevel@tonic-gate ushort_t m_minspl; /* 6-7 min pil val if lock held */ 700Sstevel@tonic-gate } m_spin; 710Sstevel@tonic-gate 720Sstevel@tonic-gate } mutex_impl_t; 730Sstevel@tonic-gate 740Sstevel@tonic-gate #define m_owner m_adaptive._m_owner 750Sstevel@tonic-gate 760Sstevel@tonic-gate #define MUTEX_WAITERS 0x1 770Sstevel@tonic-gate #define MUTEX_DEAD 0x6 780Sstevel@tonic-gate #define MUTEX_THREAD (-0x8) 790Sstevel@tonic-gate 800Sstevel@tonic-gate #define MUTEX_OWNER(lp) ((kthread_id_t)((lp)->m_owner & MUTEX_THREAD)) 810Sstevel@tonic-gate #define MUTEX_NO_OWNER ((kthread_id_t)NULL) 820Sstevel@tonic-gate 830Sstevel@tonic-gate #define MUTEX_SET_WAITERS(lp) \ 840Sstevel@tonic-gate { \ 850Sstevel@tonic-gate uintptr_t old; \ 860Sstevel@tonic-gate while ((old = (lp)->m_owner) != 0 && \ 870Sstevel@tonic-gate casip(&(lp)->m_owner, old, old | MUTEX_WAITERS) != old) \ 880Sstevel@tonic-gate continue; \ 890Sstevel@tonic-gate } 900Sstevel@tonic-gate 910Sstevel@tonic-gate #define MUTEX_HAS_WAITERS(lp) ((lp)->m_owner & MUTEX_WAITERS) 920Sstevel@tonic-gate #define MUTEX_CLEAR_LOCK_AND_WAITERS(lp) (lp)->m_owner = 0 930Sstevel@tonic-gate 940Sstevel@tonic-gate #define MUTEX_SET_TYPE(lp, type) 950Sstevel@tonic-gate #define MUTEX_TYPE_ADAPTIVE(lp) (((lp)->m_owner & MUTEX_DEAD) == 0) 960Sstevel@tonic-gate #define MUTEX_TYPE_SPIN(lp) ((lp)->m_spin.m_dummylock == LOCK_HELD_VALUE) 970Sstevel@tonic-gate 980Sstevel@tonic-gate #define MUTEX_DESTROY(lp) \ 990Sstevel@tonic-gate (lp)->m_owner = ((uintptr_t)curthread | MUTEX_DEAD) 100*5834Spt157919 /* mutex backoff delay macro and constants */ 101*5834Spt157919 #define MUTEX_BACKOFF_BASE 1 102*5834Spt157919 #define MUTEX_BACKOFF_SHIFT 2 103*5834Spt157919 #define MUTEX_CAP_FACTOR 64 104*5834Spt157919 #define MUTEX_DELAY() { \ 105*5834Spt157919 mutex_delay(); \ 106*5834Spt157919 SMT_PAUSE(); \ 107*5834Spt157919 } 108*5834Spt157919 109*5834Spt157919 /* low overhead clock read */ 110*5834Spt157919 #define MUTEX_GETTICK() tsc_read() 111*5834Spt157919 extern void null_xcall(void); 112*5834Spt157919 #define MUTEX_SYNC() { \ 113*5834Spt157919 cpuset_t set; \ 114*5834Spt157919 CPUSET_ALL(set); \ 115*5834Spt157919 xc_call(0, 0, 0, X_CALL_HIPRI, set, \ 116*5834Spt157919 (xc_func_t)null_xcall); \ 117*5834Spt157919 } 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate extern int mutex_adaptive_tryenter(mutex_impl_t *); 120*5834Spt157919 extern void *mutex_owner_running(mutex_impl_t *); 121*5834Spt157919 122*5834Spt157919 #else /* _ASM */ 123*5834Spt157919 124*5834Spt157919 #define MUTEX_THREAD -0x8 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate #endif /* _ASM */ 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate #ifdef __cplusplus 1290Sstevel@tonic-gate } 1300Sstevel@tonic-gate #endif 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate #endif /* _SYS_MUTEX_IMPL_H */ 133