mutex.h revision 1.5
1/* $NetBSD: mutex.h,v 1.5 2007/02/19 03:06:46 matt Exp $ */ 2 3/*- 4 * Copyright (c) 2002, 2007 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe and Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39#ifndef _VAX_MUTEX_H_ 40#define _VAX_MUTEX_H_ 41 42/* 43 * The VAX mutex implementation is troublesome, because the VAX architecture 44 * lacks a compare-and-set operation, yet there are many SMP VAX 45 * machines in circulation. SMP for spin mutexes is easy - we don't need 46 * to know who owns the lock. For adaptive mutexes, we need an aditional 47 * interlock. However, since we know that owners will be kernel addresses 48 * and all kernel addresses have the high bit set, we can use the high bit 49 * as an interlock. 50 * 51 * So we test the high bit with BBSSI and if clear 52 * kernels are always loaded above 0xe0000000, and the low 5 bits of any 53 * "struct lwp *" are always zero. So, to record the lock owner, we only 54 * need 23 bits of space. mtxa_owner contains the mutex owner's address 55 * shifted right by 5: the top three bits of which will always be 0xe, 56 * overlapping with the interlock at the top byte, which is always 0xff 57 * when the mutex is held. 58 * 59 * For a mutex acquisition, the owner field is set in two steps: first, 60 * acquire the interlock (top bit), and second OR in the owner's address. 61 * Once the owner field is non zero, it will appear that the mutex is held, 62 * by which LWP it does not matter: other LWPs competing for the lock will 63 * fall through to mutex_vector_enter(), and either spin or sleep. 64 * 65 * As a result there is no space for a waiters bit in the owner field. No 66 * problem, because it would be hard to synchronise using one without a CAS 67 * operation. Note that in order to do unlocked release of adaptive 68 * mutexes, we need the effect of MUTEX_SET_WAITERS() to be immediatley 69 * visible on the bus. So, adaptive mutexes share the spin lock byte with 70 * spin mutexes (set with bb{cc,ss}i), but it is not treated as a lock in its 71 * own right, rather as a flag that can be atomically set or cleared. 72 * 73 * When releasing an adaptive mutex, we first clear the owners field, and 74 * then check to see if the waiters byte is set. This ensures that there 75 * will always be someone to wake any sleeping waiters up (even it the mutex 76 * is acquired immediately after we release it, or if we are preempted 77 * immediatley after clearing the owners field). The setting or clearing of 78 * the waiters byte is serialized by the turnstile chain lock associated 79 * with the mutex. 80 * 81 * See comments in kern_mutex.c about releasing adaptive mutexes without 82 * an interlocking step. 83 */ 84 85#ifndef __MUTEX_PRIVATE 86 87struct kmutex { 88 uintptr_t mtx_pad1; 89 uint32_t mtx_pad2[2]; 90}; 91 92#else /* __MUTEX_PRIVATE */ 93 94struct kmutex { 95 /* Adaptive mutex */ 96 union { 97 volatile uintptr_t mtxu_owner; /* 0-3 */ 98 __cpu_simple_lock_t mtxu_lock; /* 0 */ 99 } mtx_u; 100 ipl_cookie_t mtx_ipl; /* 4-7 */ 101 uint32_t mtx_id; /* 8-11 */ 102}; 103#define mtx_owner mtx_u.mtxu_owner 104#define mtx_lock mtx_u.mtxu_lock 105 106#define __HAVE_MUTEX_STUBS 1 107#define __HAVE_SPIN_MUTEX_STUBS 1 108 109#define MUTEX_NO_SPIN_ACTIVE_P(ci) ((ci)->ci_mtx_count == 1) 110 111static inline uintptr_t 112MUTEX_OWNER(uintptr_t owner) 113{ 114 return owner & ~1; 115} 116 117static inline bool 118MUTEX_OWNED(uintptr_t owner) 119{ 120 return owner != 0; 121} 122 123static inline bool 124MUTEX_SET_WAITERS(kmutex_t *mtx, uintptr_t owner) 125{ 126 mtx->mtx_owner |= 1; 127 return (mtx->mtx_owner & ~1) != 0; 128} 129 130static inline bool 131MUTEX_HAS_WAITERS(volatile kmutex_t *mtx) 132{ 133 return (mtx->mtx_owner & 1) != 0; 134} 135 136static inline void 137MUTEX_CLEAR_WAITERS(volatile kmutex_t *mtx) 138{ 139 mtx->mtx_owner &= ~1; 140} 141 142static inline void 143MUTEX_INITIALIZE_SPIN(kmutex_t *mtx, u_int id, int ipl) 144{ 145 mtx->mtx_id = (id << 1) | 1; 146 mtx->mtx_ipl = makeiplcookie(ipl); 147 mtx->mtx_lock = 0; 148} 149 150static inline void 151MUTEX_INITIALIZE_ADAPTIVE(kmutex_t *mtx, u_int id) 152{ 153 mtx->mtx_id = id << 1; 154 mtx->mtx_ipl = makeiplcookie(-1); 155 mtx->mtx_owner = 0; 156} 157 158static inline void 159MUTEX_DESTROY(kmutex_t *mtx) 160{ 161 mtx->mtx_owner = (uintptr_t)-1L; 162 mtx->mtx_id = 0xdeadface << 1; 163} 164 165static inline u_int 166MUTEX_GETID(volatile kmutex_t *mtx) 167{ 168 return (mtx)->mtx_id >> 1; 169} 170 171static inline bool 172MUTEX_SPIN_P(volatile kmutex_t *mtx) 173{ 174 return (mtx->mtx_id & 1) != 0; 175} 176 177static inline bool 178MUTEX_ADAPTIVE_P(volatile kmutex_t *mtx) 179{ 180 return (mtx->mtx_id & 1) == 0; 181} 182 183static inline bool 184MUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t curthread) 185{ 186 int rv; 187 __asm __volatile( 188 "clrl %1;" 189 "bbssi $31,%0,1f;" 190 "incl %1;" 191 "insv %2,%0,$31,%0;" 192 "1:" 193 : "=m"(mtx->mtx_owner), "=r"(rv) 194 : "g"(curthread)); 195 return rv; 196} 197 198static inline void 199MUTEX_RELEASE(kmutex_t *mtx) 200{ 201 __asm __volatile( 202 "insv $0,$0,$31,%0;" 203 "bbcci $31,%0,1f;" 204 "1:" 205 : "=m" (mtx->mtx_owner)); 206} 207 208#endif /* __MUTEX_PRIVATE */ 209 210#endif /* _VAX_MUTEX_H_ */ 211