mutex.h revision 1.5
11.5Sthorpej/* $NetBSD: mutex.h,v 1.5 2007/03/09 07:13:06 thorpej 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 _ARM_MUTEX_H_ 401.1Smatt#define _ARM_MUTEX_H_ 411.1Smatt 421.1Smatt/* 431.5Sthorpej * The ARM mutex implementation is troublesome, because pre-v6 ARM lacks a 441.4Sthorpej * compare-and-set operation. However, there aren't any MP pre-v6 ARM 451.5Sthorpej * systems to speak of. We are mostly concerned with atomicity with respect 461.5Sthorpej * to interrupts. 471.4Sthorpej * 481.4Sthorpej * SMP for spin mutexes is easy - we don't need to know who owns the lock. 491.4Sthorpej * For adaptive mutexes, we need an aditional interlock. 501.4Sthorpej * 511.4Sthorpej * Unfortunately, not all ARM kernels are linked at the same address, 521.4Sthorpej * meaning we cannot safely overlay the interlock with the MSB of the 531.4Sthorpej * owner field. 541.4Sthorpej * 551.4Sthorpej * For a mutex acquisition, we first grab the interlock and then set the 561.4Sthorpej * owner field. 571.4Sthorpej * 581.4Sthorpej * There is room in the owners field for a waiters bit, but we don't do 591.4Sthorpej * that because it would be hard to synchronize using one without a CAS 601.4Sthorpej * operation. Because the waiters bit is only needed for adaptive mutexes, 611.4Sthorpej * we instead use the lock that is normally used by spin mutexes to indicate 621.4Sthorpej * waiters. 631.4Sthorpej * 641.4Sthorpej * Spin mutexes are initialized with the interlock held to cause the 651.4Sthorpej * assembly stub to go through mutex_vector_enter(). 661.1Smatt * 671.1Smatt * When releasing an adaptive mutex, we first clear the owners field, and 681.1Smatt * then check to see if the waiters byte is set. This ensures that there 691.1Smatt * will always be someone to wake any sleeping waiters up (even it the mutex 701.1Smatt * is acquired immediately after we release it, or if we are preempted 711.1Smatt * immediatley after clearing the owners field). The setting or clearing of 721.1Smatt * the waiters byte is serialized by the turnstile chain lock associated 731.1Smatt * with the mutex. 741.1Smatt * 751.1Smatt * See comments in kern_mutex.c about releasing adaptive mutexes without 761.1Smatt * an interlocking step. 771.1Smatt */ 781.1Smatt 791.1Smatt#ifndef __MUTEX_PRIVATE 801.1Smatt 811.1Smattstruct kmutex { 821.1Smatt uintptr_t mtx_pad1; 831.3Sthorpej uint32_t mtx_pad2[2]; 841.1Smatt}; 851.1Smatt 861.1Smatt#else /* __MUTEX_PRIVATE */ 871.1Smatt 881.1Smattstruct kmutex { 891.1Smatt volatile uintptr_t mtx_owner; /* 0-3 */ 901.2Sthorpej __cpu_simple_lock_t mtx_interlock; /* 4 */ 911.2Sthorpej __cpu_simple_lock_t mtx_lock; /* 5 */ 921.3Sthorpej ipl_cookie_t mtx_ipl; /* 6 */ 931.3Sthorpej uint8_t mtx_pad; /* 7 */ 941.3Sthorpej uint32_t mtx_id; /* 8-11 */ 951.1Smatt}; 961.1Smatt 971.1Smatt#if 0 981.1Smatt#define __HAVE_MUTEX_STUBS 1 991.1Smatt#define __HAVE_SPIN_MUTEX_STUBS 1 1001.1Smatt#endif 1011.1Smatt 1021.1Smattstatic inline uintptr_t 1031.1SmattMUTEX_OWNER(uintptr_t owner) 1041.1Smatt{ 1051.1Smatt return owner; 1061.1Smatt} 1071.1Smatt 1081.1Smattstatic inline int 1091.1SmattMUTEX_OWNED(uintptr_t owner) 1101.1Smatt{ 1111.1Smatt return owner != 0; 1121.1Smatt} 1131.1Smatt 1141.1Smattstatic inline int 1151.1SmattMUTEX_SET_WAITERS(kmutex_t *mtx, uintptr_t owner) 1161.1Smatt{ 1171.1Smatt (void)__cpu_simple_lock_try(&mtx->mtx_lock); 1181.1Smatt return mtx->mtx_owner != 0; 1191.1Smatt} 1201.1Smatt 1211.1Smattstatic inline void 1221.1SmattMUTEX_CLEAR_WAITERS(kmutex_t *mtx) 1231.1Smatt{ 1241.1Smatt __cpu_simple_unlock(&mtx->mtx_lock); 1251.1Smatt} 1261.1Smatt 1271.1Smattstatic inline int 1281.1SmattMUTEX_HAS_WAITERS(volatile kmutex_t *mtx) 1291.1Smatt{ 1301.1Smatt if (mtx->mtx_owner == 0) 1311.1Smatt return 0; 1321.1Smatt return mtx->mtx_lock == __SIMPLELOCK_LOCKED; 1331.1Smatt} 1341.1Smatt 1351.1Smattstatic inline void 1361.1SmattMUTEX_INITIALIZE_SPIN(kmutex_t *mtx, u_int id, int ipl) 1371.1Smatt{ 1381.1Smatt mtx->mtx_id = (id << 1) | 1; 1391.1Smatt mtx->mtx_ipl = makeiplcookie(ipl); 1401.1Smatt mtx->mtx_interlock = __SIMPLELOCK_LOCKED; 1411.1Smatt __cpu_simple_lock_init(&mtx->mtx_lock); 1421.1Smatt} 1431.1Smatt 1441.1Smattstatic inline void 1451.1SmattMUTEX_INITIALIZE_ADAPTIVE(kmutex_t *mtx, u_int id) 1461.1Smatt{ 1471.1Smatt mtx->mtx_id = (id << 1) | 0; 1481.1Smatt __cpu_simple_lock_init(&mtx->mtx_interlock); 1491.1Smatt __cpu_simple_lock_init(&mtx->mtx_lock); 1501.1Smatt} 1511.1Smatt 1521.1Smattstatic inline void 1531.1SmattMUTEX_DESTROY(kmutex_t *mtx) 1541.1Smatt{ 1551.1Smatt mtx->mtx_owner = (uintptr_t)-1L; 1561.1Smatt mtx->mtx_id = ~0; 1571.1Smatt} 1581.1Smatt 1591.1Smattstatic inline u_int 1601.1SmattMUTEX_GETID(kmutex_t *mtx) 1611.1Smatt{ 1621.1Smatt return mtx->mtx_id >> 1; 1631.1Smatt} 1641.1Smatt 1651.1Smattstatic inline bool 1661.1SmattMUTEX_SPIN_P(volatile kmutex_t *mtx) 1671.1Smatt{ 1681.1Smatt return (mtx->mtx_id & 1) == 1; 1691.1Smatt} 1701.1Smatt 1711.1Smattstatic inline bool 1721.1SmattMUTEX_ADAPTIVE_P(volatile kmutex_t *mtx) 1731.1Smatt{ 1741.1Smatt return (mtx->mtx_id & 1) == 0; 1751.1Smatt} 1761.1Smatt 1771.1Smattstatic inline int 1781.1SmattMUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t curthread) 1791.1Smatt{ 1801.1Smatt if (!__cpu_simple_lock_try(&mtx->mtx_interlock)) 1811.1Smatt return 0; 1821.1Smatt mtx->mtx_owner = curthread; 1831.1Smatt return 1; 1841.1Smatt} 1851.1Smatt 1861.1Smattstatic inline void 1871.1SmattMUTEX_RELEASE(kmutex_t *mtx) 1881.1Smatt{ 1891.1Smatt mtx->mtx_owner = 0; 1901.1Smatt __cpu_simple_unlock(&mtx->mtx_lock); 1911.1Smatt __cpu_simple_unlock(&mtx->mtx_interlock); 1921.1Smatt} 1931.1Smatt 1941.1Smatt#endif /* __MUTEX_PRIVATE */ 1951.1Smatt 1961.1Smatt#endif /* _ARM_MUTEX_H_ */ 197