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