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 5575a7426Spt157919 * Common Development and Distribution License (the "License"). 6575a7426Spt157919 * 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*f34a7178SJoe Bonasera * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #ifndef _SYS_MUTEX_IMPL_H 277c478bd9Sstevel@tonic-gate #define _SYS_MUTEX_IMPL_H 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #ifndef _ASM 307c478bd9Sstevel@tonic-gate #include <sys/types.h> 317c478bd9Sstevel@tonic-gate #include <sys/machlock.h> 327c478bd9Sstevel@tonic-gate #endif 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate #ifdef __cplusplus 357c478bd9Sstevel@tonic-gate extern "C" { 367c478bd9Sstevel@tonic-gate #endif 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate #ifndef _ASM 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate /* 417c478bd9Sstevel@tonic-gate * mutex_enter() assumes that the mutex is adaptive and tries to grab the 427c478bd9Sstevel@tonic-gate * lock by doing a atomic compare and exchange on the first word of the mutex. 437c478bd9Sstevel@tonic-gate * If the compare and exchange fails, it means that either (1) the lock is a 447c478bd9Sstevel@tonic-gate * spin lock, or (2) the lock is adaptive but already held. 457c478bd9Sstevel@tonic-gate * mutex_vector_enter() distinguishes these cases by looking at the mutex 467c478bd9Sstevel@tonic-gate * type, which is encoded in the low-order bits of the owner field. 477c478bd9Sstevel@tonic-gate */ 487c478bd9Sstevel@tonic-gate typedef union mutex_impl { 497c478bd9Sstevel@tonic-gate /* 507c478bd9Sstevel@tonic-gate * Adaptive mutex. 517c478bd9Sstevel@tonic-gate */ 527c478bd9Sstevel@tonic-gate struct adaptive_mutex { 537c478bd9Sstevel@tonic-gate uintptr_t _m_owner; /* 0-3/0-7 owner and waiters bit */ 547c478bd9Sstevel@tonic-gate #ifndef _LP64 557c478bd9Sstevel@tonic-gate uintptr_t _m_filler; /* 4-7 unused */ 567c478bd9Sstevel@tonic-gate #endif 577c478bd9Sstevel@tonic-gate } m_adaptive; 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate /* 607c478bd9Sstevel@tonic-gate * Spin Mutex. 617c478bd9Sstevel@tonic-gate */ 627c478bd9Sstevel@tonic-gate struct spin_mutex { 637c478bd9Sstevel@tonic-gate lock_t m_dummylock; /* 0 dummy lock (always set) */ 647c478bd9Sstevel@tonic-gate lock_t m_spinlock; /* 1 real lock */ 657c478bd9Sstevel@tonic-gate ushort_t m_filler; /* 2-3 unused */ 667c478bd9Sstevel@tonic-gate ushort_t m_oldspl; /* 4-5 old pil value */ 677c478bd9Sstevel@tonic-gate ushort_t m_minspl; /* 6-7 min pil val if lock held */ 687c478bd9Sstevel@tonic-gate } m_spin; 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate } mutex_impl_t; 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate #define m_owner m_adaptive._m_owner 737c478bd9Sstevel@tonic-gate 747f30f491Sck142721 #define MUTEX_ALIGN _LONG_ALIGNMENT 757f30f491Sck142721 #define MUTEX_ALIGN_WARNINGS 10 /* num of warnings to issue */ 767f30f491Sck142721 777c478bd9Sstevel@tonic-gate #define MUTEX_WAITERS 0x1 787c478bd9Sstevel@tonic-gate #define MUTEX_DEAD 0x6 797c478bd9Sstevel@tonic-gate #define MUTEX_THREAD (-0x8) 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate #define MUTEX_OWNER(lp) ((kthread_id_t)((lp)->m_owner & MUTEX_THREAD)) 827c478bd9Sstevel@tonic-gate #define MUTEX_NO_OWNER ((kthread_id_t)NULL) 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate #define MUTEX_SET_WAITERS(lp) \ 857c478bd9Sstevel@tonic-gate { \ 867c478bd9Sstevel@tonic-gate uintptr_t old; \ 877c478bd9Sstevel@tonic-gate while ((old = (lp)->m_owner) != 0 && \ 887c478bd9Sstevel@tonic-gate casip(&(lp)->m_owner, old, old | MUTEX_WAITERS) != old) \ 897c478bd9Sstevel@tonic-gate continue; \ 907c478bd9Sstevel@tonic-gate } 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate #define MUTEX_HAS_WAITERS(lp) ((lp)->m_owner & MUTEX_WAITERS) 937c478bd9Sstevel@tonic-gate #define MUTEX_CLEAR_LOCK_AND_WAITERS(lp) (lp)->m_owner = 0 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate #define MUTEX_SET_TYPE(lp, type) 967c478bd9Sstevel@tonic-gate #define MUTEX_TYPE_ADAPTIVE(lp) (((lp)->m_owner & MUTEX_DEAD) == 0) 977c478bd9Sstevel@tonic-gate #define MUTEX_TYPE_SPIN(lp) ((lp)->m_spin.m_dummylock == LOCK_HELD_VALUE) 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate #define MUTEX_DESTROY(lp) \ 1007c478bd9Sstevel@tonic-gate (lp)->m_owner = ((uintptr_t)curthread | MUTEX_DEAD) 101575a7426Spt157919 /* mutex backoff delay macro and constants */ 102575a7426Spt157919 #define MUTEX_BACKOFF_BASE 1 103575a7426Spt157919 #define MUTEX_BACKOFF_SHIFT 2 104575a7426Spt157919 #define MUTEX_CAP_FACTOR 64 105575a7426Spt157919 #define MUTEX_DELAY() { \ 106575a7426Spt157919 mutex_delay(); \ 107575a7426Spt157919 SMT_PAUSE(); \ 108575a7426Spt157919 } 109575a7426Spt157919 110575a7426Spt157919 /* low overhead clock read */ 111575a7426Spt157919 #define MUTEX_GETTICK() tsc_read() 112575a7426Spt157919 extern void null_xcall(void); 113575a7426Spt157919 #define MUTEX_SYNC() { \ 114575a7426Spt157919 cpuset_t set; \ 115575a7426Spt157919 CPUSET_ALL(set); \ 116*f34a7178SJoe Bonasera xc_call(0, 0, 0, CPUSET2BV(set), \ 117575a7426Spt157919 (xc_func_t)null_xcall); \ 118575a7426Spt157919 } 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate extern int mutex_adaptive_tryenter(mutex_impl_t *); 121575a7426Spt157919 extern void *mutex_owner_running(mutex_impl_t *); 122575a7426Spt157919 123575a7426Spt157919 #else /* _ASM */ 124575a7426Spt157919 125575a7426Spt157919 #define MUTEX_THREAD -0x8 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate #endif /* _ASM */ 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate #ifdef __cplusplus 1307c478bd9Sstevel@tonic-gate } 1317c478bd9Sstevel@tonic-gate #endif 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate #endif /* _SYS_MUTEX_IMPL_H */ 134