mutex.h revision 1.1
1/* $NetBSD: mutex.h,v 1.1 2007/02/16 01:34:03 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 109static inline uintptr_t 110MUTEX_OWNER(uintptr_t owner) 111{ 112 return owner & ~1; 113} 114 115static inline bool 116MUTEX_OWNED(uintptr_t owner) 117{ 118 return owner != 0; 119} 120 121static inline bool 122MUTEX_SET_WAITERS(kmutex_t *mtx, uintptr_t owner) 123{ 124 mtx->mtx_owner |= 1; 125 return mtx->mtx_owner != 0; 126} 127 128static inline bool 129MUTEX_HAS_WAITERS(volatile kmutex_t *mtx) 130{ 131 return (mtx->mtx_owner & 1) != 0; 132} 133 134static inline void 135MUTEX_CLEAR_WAITERS(volatile kmutex_t *mtx) 136{ 137 mtx->mtx_owner &= ~1; 138} 139 140static inline void 141MUTEX_INITIALIZE_SPIN(kmutex_t *mtx, u_int id, int ipl) 142{ 143 mtx->mtx_id = (id << 1) | 1; 144 mtx->mtx_ipl = makeiplcookie(ipl); 145 mtx->mtx_lock = 0; 146} 147 148static inline void 149MUTEX_INITIALIZE_ADAPTIVE(kmutex_t *mtx, u_int id) 150{ 151 mtx->mtx_id = id << 1; 152 mtx->mtx_ipl = makeiplcookie(-1); 153 mtx->mtx_owner = 0; 154} 155 156static inline void 157MUTEX_DESTROY(kmutex_t *mtx) 158{ 159 mtx->mtx_owner = (uintptr_t)-1L; 160 mtx->mtx_id = 0xdeadface << 1; 161} 162 163static inline u_int 164MUTEX_GETID(volatile kmutex_t *mtx) 165{ 166 return (mtx)->mtx_id >> 1; 167} 168 169static inline bool 170MUTEX_SPIN_P(volatile kmutex_t *mtx) 171{ 172 return (mtx->mtx_id & 1) != 0; 173} 174 175static inline bool 176MUTEX_ADAPTIVE_P(volatile kmutex_t *mtx) 177{ 178 return (mtx->mtx_id & 1) == 0; 179} 180 181static inline bool 182MUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t curthread) 183{ 184 int rv; 185 __asm __volatile( 186 "clrl %1;" 187 "bbssi $31,%0,1f;" 188 "incl %1;" 189 "insv %2,%0,$31,%0;" 190 "1:" 191 : "=m"(mtx->mtx_owner), "=r"(rv) 192 : "g"(curthread)); 193 return 1; 194} 195 196static inline void 197MUTEX_RELEASE(kmutex_t *mtx) 198{ 199 __asm __volatile( 200 "insv $0,$0,$31,%0;" 201 "bbcci $31,%0,1f;" 202 "1:" 203 : "=m" (mtx->mtx_owner)); 204} 205 206#endif /* __MUTEX_PRIVATE */ 207 208#endif /* _VAX_MUTEX_H_ */ 209