mutex.h revision 1.1
11.1Smatt/* $NetBSD: mutex.h,v 1.1 2007/02/16 01:34:03 matt Exp $ */ 21.1Smatt 31.1Smatt/*- 41.1Smatt * Copyright (c) 2002, 2007 The NetBSD Foundation, Inc. 51.1Smatt * All rights reserved. 61.1Smatt * 71.1Smatt * This code is derived from software contributed to The NetBSD Foundation 81.1Smatt * by Jason R. Thorpe and Andrew Doran. 91.1Smatt * 101.1Smatt * Redistribution and use in source and binary forms, with or without 111.1Smatt * modification, are permitted provided that the following conditions 121.1Smatt * are met: 131.1Smatt * 1. Redistributions of source code must retain the above copyright 141.1Smatt * notice, this list of conditions and the following disclaimer. 151.1Smatt * 2. Redistributions in binary form must reproduce the above copyright 161.1Smatt * notice, this list of conditions and the following disclaimer in the 171.1Smatt * documentation and/or other materials provided with the distribution. 181.1Smatt * 3. All advertising materials mentioning features or use of this software 191.1Smatt * must display the following acknowledgement: 201.1Smatt * This product includes software developed by the NetBSD 211.1Smatt * Foundation, Inc. and its contributors. 221.1Smatt * 4. Neither the name of The NetBSD Foundation nor the names of its 231.1Smatt * contributors may be used to endorse or promote products derived 241.1Smatt * from this software without specific prior written permission. 251.1Smatt * 261.1Smatt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 271.1Smatt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 281.1Smatt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 291.1Smatt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 301.1Smatt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 311.1Smatt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 321.1Smatt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 331.1Smatt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 341.1Smatt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 351.1Smatt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 361.1Smatt * POSSIBILITY OF SUCH DAMAGE. 371.1Smatt */ 381.1Smatt 391.1Smatt#ifndef _VAX_MUTEX_H_ 401.1Smatt#define _VAX_MUTEX_H_ 411.1Smatt 421.1Smatt/* 431.1Smatt * The VAX mutex implementation is troublesome, because the VAX architecture 441.1Smatt * lacks a compare-and-set operation, yet there are many SMP VAX 451.1Smatt * machines in circulation. SMP for spin mutexes is easy - we don't need 461.1Smatt * to know who owns the lock. For adaptive mutexes, we need an aditional 471.1Smatt * interlock. However, since we know that owners will be kernel addresses 481.1Smatt * and all kernel addresses have the high bit set, we can use the high bit 491.1Smatt * as an interlock. 501.1Smatt * 511.1Smatt * So we test the high bit with BBSSI and if clear 521.1Smatt * kernels are always loaded above 0xe0000000, and the low 5 bits of any 531.1Smatt * "struct lwp *" are always zero. So, to record the lock owner, we only 541.1Smatt * need 23 bits of space. mtxa_owner contains the mutex owner's address 551.1Smatt * shifted right by 5: the top three bits of which will always be 0xe, 561.1Smatt * overlapping with the interlock at the top byte, which is always 0xff 571.1Smatt * when the mutex is held. 581.1Smatt * 591.1Smatt * For a mutex acquisition, the owner field is set in two steps: first, 601.1Smatt * acquire the interlock (top bit), and second OR in the owner's address. 611.1Smatt * Once the owner field is non zero, it will appear that the mutex is held, 621.1Smatt * by which LWP it does not matter: other LWPs competing for the lock will 631.1Smatt * fall through to mutex_vector_enter(), and either spin or sleep. 641.1Smatt * 651.1Smatt * As a result there is no space for a waiters bit in the owner field. No 661.1Smatt * problem, because it would be hard to synchronise using one without a CAS 671.1Smatt * operation. Note that in order to do unlocked release of adaptive 681.1Smatt * mutexes, we need the effect of MUTEX_SET_WAITERS() to be immediatley 691.1Smatt * visible on the bus. So, adaptive mutexes share the spin lock byte with 701.1Smatt * spin mutexes (set with bb{cc,ss}i), but it is not treated as a lock in its 711.1Smatt * own right, rather as a flag that can be atomically set or cleared. 721.1Smatt * 731.1Smatt * When releasing an adaptive mutex, we first clear the owners field, and 741.1Smatt * then check to see if the waiters byte is set. This ensures that there 751.1Smatt * will always be someone to wake any sleeping waiters up (even it the mutex 761.1Smatt * is acquired immediately after we release it, or if we are preempted 771.1Smatt * immediatley after clearing the owners field). The setting or clearing of 781.1Smatt * the waiters byte is serialized by the turnstile chain lock associated 791.1Smatt * with the mutex. 801.1Smatt * 811.1Smatt * See comments in kern_mutex.c about releasing adaptive mutexes without 821.1Smatt * an interlocking step. 831.1Smatt */ 841.1Smatt 851.1Smatt#ifndef __MUTEX_PRIVATE 861.1Smatt 871.1Smattstruct kmutex { 881.1Smatt uintptr_t mtx_pad1; 891.1Smatt uint32_t mtx_pad2[2]; 901.1Smatt}; 911.1Smatt 921.1Smatt#else /* __MUTEX_PRIVATE */ 931.1Smatt 941.1Smattstruct kmutex { 951.1Smatt /* Adaptive mutex */ 961.1Smatt union { 971.1Smatt volatile uintptr_t mtxu_owner; /* 0-3 */ 981.1Smatt __cpu_simple_lock_t mtxu_lock; /* 0 */ 991.1Smatt } mtx_u; 1001.1Smatt ipl_cookie_t mtx_ipl; /* 4-7 */ 1011.1Smatt uint32_t mtx_id; /* 8-11 */ 1021.1Smatt}; 1031.1Smatt#define mtx_owner mtx_u.mtxu_owner 1041.1Smatt#define mtx_lock mtx_u.mtxu_lock 1051.1Smatt 1061.1Smatt#define __HAVE_MUTEX_STUBS 1 1071.1Smatt#define __HAVE_SPIN_MUTEX_STUBS 1 1081.1Smatt 1091.1Smattstatic inline uintptr_t 1101.1SmattMUTEX_OWNER(uintptr_t owner) 1111.1Smatt{ 1121.1Smatt return owner & ~1; 1131.1Smatt} 1141.1Smatt 1151.1Smattstatic inline bool 1161.1SmattMUTEX_OWNED(uintptr_t owner) 1171.1Smatt{ 1181.1Smatt return owner != 0; 1191.1Smatt} 1201.1Smatt 1211.1Smattstatic inline bool 1221.1SmattMUTEX_SET_WAITERS(kmutex_t *mtx, uintptr_t owner) 1231.1Smatt{ 1241.1Smatt mtx->mtx_owner |= 1; 1251.1Smatt return mtx->mtx_owner != 0; 1261.1Smatt} 1271.1Smatt 1281.1Smattstatic inline bool 1291.1SmattMUTEX_HAS_WAITERS(volatile kmutex_t *mtx) 1301.1Smatt{ 1311.1Smatt return (mtx->mtx_owner & 1) != 0; 1321.1Smatt} 1331.1Smatt 1341.1Smattstatic inline void 1351.1SmattMUTEX_CLEAR_WAITERS(volatile kmutex_t *mtx) 1361.1Smatt{ 1371.1Smatt mtx->mtx_owner &= ~1; 1381.1Smatt} 1391.1Smatt 1401.1Smattstatic inline void 1411.1SmattMUTEX_INITIALIZE_SPIN(kmutex_t *mtx, u_int id, int ipl) 1421.1Smatt{ 1431.1Smatt mtx->mtx_id = (id << 1) | 1; 1441.1Smatt mtx->mtx_ipl = makeiplcookie(ipl); 1451.1Smatt mtx->mtx_lock = 0; 1461.1Smatt} 1471.1Smatt 1481.1Smattstatic inline void 1491.1SmattMUTEX_INITIALIZE_ADAPTIVE(kmutex_t *mtx, u_int id) 1501.1Smatt{ 1511.1Smatt mtx->mtx_id = id << 1; 1521.1Smatt mtx->mtx_ipl = makeiplcookie(-1); 1531.1Smatt mtx->mtx_owner = 0; 1541.1Smatt} 1551.1Smatt 1561.1Smattstatic inline void 1571.1SmattMUTEX_DESTROY(kmutex_t *mtx) 1581.1Smatt{ 1591.1Smatt mtx->mtx_owner = (uintptr_t)-1L; 1601.1Smatt mtx->mtx_id = 0xdeadface << 1; 1611.1Smatt} 1621.1Smatt 1631.1Smattstatic inline u_int 1641.1SmattMUTEX_GETID(volatile kmutex_t *mtx) 1651.1Smatt{ 1661.1Smatt return (mtx)->mtx_id >> 1; 1671.1Smatt} 1681.1Smatt 1691.1Smattstatic inline bool 1701.1SmattMUTEX_SPIN_P(volatile kmutex_t *mtx) 1711.1Smatt{ 1721.1Smatt return (mtx->mtx_id & 1) != 0; 1731.1Smatt} 1741.1Smatt 1751.1Smattstatic inline bool 1761.1SmattMUTEX_ADAPTIVE_P(volatile kmutex_t *mtx) 1771.1Smatt{ 1781.1Smatt return (mtx->mtx_id & 1) == 0; 1791.1Smatt} 1801.1Smatt 1811.1Smattstatic inline bool 1821.1SmattMUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t curthread) 1831.1Smatt{ 1841.1Smatt int rv; 1851.1Smatt __asm __volatile( 1861.1Smatt "clrl %1;" 1871.1Smatt "bbssi $31,%0,1f;" 1881.1Smatt "incl %1;" 1891.1Smatt "insv %2,%0,$31,%0;" 1901.1Smatt "1:" 1911.1Smatt : "=m"(mtx->mtx_owner), "=r"(rv) 1921.1Smatt : "g"(curthread)); 1931.1Smatt return 1; 1941.1Smatt} 1951.1Smatt 1961.1Smattstatic inline void 1971.1SmattMUTEX_RELEASE(kmutex_t *mtx) 1981.1Smatt{ 1991.1Smatt __asm __volatile( 2001.1Smatt "insv $0,$0,$31,%0;" 2011.1Smatt "bbcci $31,%0,1f;" 2021.1Smatt "1:" 2031.1Smatt : "=m" (mtx->mtx_owner)); 2041.1Smatt} 2051.1Smatt 2061.1Smatt#endif /* __MUTEX_PRIVATE */ 2071.1Smatt 2081.1Smatt#endif /* _VAX_MUTEX_H_ */ 209