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 /* 225834Spt157919 * 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 76*6617Sck142721 #define MUTEX_ALIGN _LONG_ALIGNMENT 77*6617Sck142721 #define MUTEX_ALIGN_WARNINGS 10 /* num of warnings to issue */ 78*6617Sck142721 790Sstevel@tonic-gate #define MUTEX_WAITERS 0x1 800Sstevel@tonic-gate #define MUTEX_DEAD 0x6 810Sstevel@tonic-gate #define MUTEX_THREAD (-0x8) 820Sstevel@tonic-gate 830Sstevel@tonic-gate #define MUTEX_OWNER(lp) ((kthread_id_t)((lp)->m_owner & MUTEX_THREAD)) 840Sstevel@tonic-gate #define MUTEX_NO_OWNER ((kthread_id_t)NULL) 850Sstevel@tonic-gate 860Sstevel@tonic-gate #define MUTEX_SET_WAITERS(lp) \ 870Sstevel@tonic-gate { \ 880Sstevel@tonic-gate uintptr_t old; \ 890Sstevel@tonic-gate while ((old = (lp)->m_owner) != 0 && \ 900Sstevel@tonic-gate casip(&(lp)->m_owner, old, old | MUTEX_WAITERS) != old) \ 910Sstevel@tonic-gate continue; \ 920Sstevel@tonic-gate } 930Sstevel@tonic-gate 940Sstevel@tonic-gate #define MUTEX_HAS_WAITERS(lp) ((lp)->m_owner & MUTEX_WAITERS) 950Sstevel@tonic-gate #define MUTEX_CLEAR_LOCK_AND_WAITERS(lp) (lp)->m_owner = 0 960Sstevel@tonic-gate 970Sstevel@tonic-gate #define MUTEX_SET_TYPE(lp, type) 980Sstevel@tonic-gate #define MUTEX_TYPE_ADAPTIVE(lp) (((lp)->m_owner & MUTEX_DEAD) == 0) 990Sstevel@tonic-gate #define MUTEX_TYPE_SPIN(lp) ((lp)->m_spin.m_dummylock == LOCK_HELD_VALUE) 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate #define MUTEX_DESTROY(lp) \ 1020Sstevel@tonic-gate (lp)->m_owner = ((uintptr_t)curthread | MUTEX_DEAD) 1035834Spt157919 /* mutex backoff delay macro and constants */ 1045834Spt157919 #define MUTEX_BACKOFF_BASE 1 1055834Spt157919 #define MUTEX_BACKOFF_SHIFT 2 1065834Spt157919 #define MUTEX_CAP_FACTOR 64 1075834Spt157919 #define MUTEX_DELAY() { \ 1085834Spt157919 mutex_delay(); \ 1095834Spt157919 SMT_PAUSE(); \ 1105834Spt157919 } 1115834Spt157919 1125834Spt157919 /* low overhead clock read */ 1135834Spt157919 #define MUTEX_GETTICK() tsc_read() 1145834Spt157919 extern void null_xcall(void); 1155834Spt157919 #define MUTEX_SYNC() { \ 1165834Spt157919 cpuset_t set; \ 1175834Spt157919 CPUSET_ALL(set); \ 1185834Spt157919 xc_call(0, 0, 0, X_CALL_HIPRI, set, \ 1195834Spt157919 (xc_func_t)null_xcall); \ 1205834Spt157919 } 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate extern int mutex_adaptive_tryenter(mutex_impl_t *); 1235834Spt157919 extern void *mutex_owner_running(mutex_impl_t *); 1245834Spt157919 1255834Spt157919 #else /* _ASM */ 1265834Spt157919 1275834Spt157919 #define MUTEX_THREAD -0x8 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate #endif /* _ASM */ 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate #ifdef __cplusplus 1320Sstevel@tonic-gate } 1330Sstevel@tonic-gate #endif 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate #endif /* _SYS_MUTEX_IMPL_H */ 136