mutex.h revision 1.6
11.6Syamt/* $NetBSD: mutex.h,v 1.6 2007/11/21 10:19:08 yamt Exp $ */ 21.2Sad 31.2Sad/*- 41.2Sad * Copyright (c) 2002, 2007 The NetBSD Foundation, Inc. 51.2Sad * All rights reserved. 61.2Sad * 71.2Sad * This code is derived from software contributed to The NetBSD Foundation 81.2Sad * by Jason R. Thorpe and Andrew Doran. 91.2Sad * 101.2Sad * Redistribution and use in source and binary forms, with or without 111.2Sad * modification, are permitted provided that the following conditions 121.2Sad * are met: 131.2Sad * 1. Redistributions of source code must retain the above copyright 141.2Sad * notice, this list of conditions and the following disclaimer. 151.2Sad * 2. Redistributions in binary form must reproduce the above copyright 161.2Sad * notice, this list of conditions and the following disclaimer in the 171.2Sad * documentation and/or other materials provided with the distribution. 181.2Sad * 3. All advertising materials mentioning features or use of this software 191.2Sad * must display the following acknowledgement: 201.2Sad * This product includes software developed by the NetBSD 211.2Sad * Foundation, Inc. and its contributors. 221.2Sad * 4. Neither the name of The NetBSD Foundation nor the names of its 231.2Sad * contributors may be used to endorse or promote products derived 241.2Sad * from this software without specific prior written permission. 251.2Sad * 261.2Sad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 271.2Sad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 281.2Sad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 291.2Sad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 301.2Sad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 311.2Sad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 321.2Sad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 331.2Sad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 341.2Sad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 351.2Sad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 361.2Sad * POSSIBILITY OF SUCH DAMAGE. 371.2Sad */ 381.2Sad 391.2Sad#ifndef _SPARC_MUTEX_H_ 401.2Sad#define _SPARC_MUTEX_H_ 411.2Sad 421.2Sad/* 431.2Sad * There sparc mutex implementation is troublesome, because sparc (v7 and 441.2Sad * v8) lacks a compare-and-set operation, yet there are many SMP sparc 451.2Sad * machines in circulation. SMP for spin mutexes is easy - we don't need 461.2Sad * to know who owns the lock. For adaptive mutexes, we need an aditional 471.2Sad * interlock. 481.2Sad * 491.2Sad * The locked byte set by the sparc 'ldstub' instruction is 0xff. sparc 501.2Sad * kernels are always loaded above 0xe0000000, and the low 5 bits of any 511.2Sad * "struct lwp *" are always zero. So, to record the lock owner, we only 521.2Sad * need 23 bits of space. mtxa_owner contains the mutex owner's address 531.2Sad * shifted right by 5: the top three bits of which will always be 0xe, 541.2Sad * overlapping with the interlock at the top byte, which is always 0xff 551.2Sad * when the mutex is held. 561.2Sad * 571.2Sad * For a mutex acquisition, the owner field is set in two steps: first, 581.2Sad * acquire the interlock (top byte), and second OR in the owner's address. 591.2Sad * Once the owner field is non zero, it will appear that the mutex is held, 601.2Sad * by which LWP it does not matter: other LWPs competing for the lock will 611.2Sad * fall through to mutex_vector_enter(), and either spin or sleep. 621.2Sad * 631.2Sad * As a result there is no space for a waiters bit in the owner field. No 641.2Sad * problem, because it would be hard to synchronise using one without a CAS 651.2Sad * operation. Note that in order to do unlocked release of adaptive 661.2Sad * mutexes, we need the effect of MUTEX_SET_WAITERS() to be immediatley 671.2Sad * visible on the bus. So, adaptive mutexes share the spin lock byte with 681.2Sad * spin mutexes (set with ldstub), but it is not treated as a lock in its 691.2Sad * own right, rather as a flag that can be atomically set or cleared. 701.2Sad * 711.2Sad * When releasing an adaptive mutex, we first clear the owners field, and 721.2Sad * then check to see if the waiters byte is set. This ensures that there 731.2Sad * will always be someone to wake any sleeping waiters up (even it the mutex 741.2Sad * is acquired immediately after we release it, or if we are preempted 751.2Sad * immediatley after clearing the owners field). The setting or clearing of 761.2Sad * the waiters byte is serialized by the turnstile chain lock associated 771.2Sad * with the mutex. 781.2Sad * 791.2Sad * See comments in kern_mutex.c about releasing adaptive mutexes without 801.2Sad * an interlocking step. 811.2Sad */ 821.2Sad 831.2Sad#ifndef __MUTEX_PRIVATE 841.2Sad 851.2Sadstruct kmutex { 861.2Sad uintptr_t mtx_pad1; 871.2Sad uint32_t mtx_pad2; 881.2Sad}; 891.2Sad 901.2Sad#else /* __MUTEX_PRIVATE */ 911.2Sad 921.2Sadstruct kmutex { 931.2Sad union { 941.2Sad /* Adaptive mutex */ 951.2Sad volatile uintptr_t mtxu_owner; /* 0-3 */ 961.2Sad __cpu_simple_lock_t mtxu_interlock; /* 0 */ 971.2Sad 981.2Sad /* Spin mutex. */ 991.2Sad struct { 1001.2Sad uint8_t mtxs_dummy; /* 0 */ 1011.2Sad uint8_t mtxs_unused1; /* 1 */ 1021.2Sad ipl_cookie_t mtxs_ipl; /* 2 */ 1031.2Sad uint8_t mtxs_unused2; /* 3 */ 1041.2Sad } s; 1051.2Sad } u; 1061.2Sad __cpu_simple_lock_t mtx_lock; /* 4 */ 1071.6Syamt uint8_t mtx_dodebug; /* 5 */ 1081.6Syamt uint8_t mtx_isspin; /* 6 */ 1091.6Syamt uint8_t mtx_pad[1]; /* 7 */ 1101.2Sad}; 1111.2Sad 1121.2Sad#define __HAVE_MUTEX_STUBS 1 1131.4Smrg#if 0 /* does not work for MP yet */ 1141.2Sad#define __HAVE_SPIN_MUTEX_STUBS 1 1151.4Smrg#endif 1161.2Sad 1171.2Sad#define mtx_owner u.mtxu_owner 1181.2Sad#define mtx_interlock u.mtxu_interlock 1191.2Sad#define mtx_dummy u.s.mtxs_dummy 1201.2Sad#define mtx_ipl u.s.mtxs_ipl 1211.2Sad 1221.2Sadstatic inline uintptr_t 1231.2SadMUTEX_OWNER(uintptr_t owner) 1241.2Sad{ 1251.2Sad return owner << 5; 1261.2Sad} 1271.2Sad 1281.2Sadstatic inline int 1291.2SadMUTEX_OWNED(uintptr_t owner) 1301.2Sad{ 1311.2Sad return owner != 0; 1321.2Sad} 1331.2Sad 1341.2Sadstatic inline int 1351.2SadMUTEX_SET_WAITERS(kmutex_t *mtx, uintptr_t owner) 1361.2Sad{ 1371.2Sad (void)__cpu_simple_lock_try(&mtx->mtx_lock); 1381.2Sad return mtx->mtx_owner != 0; 1391.2Sad} 1401.2Sad 1411.2Sadstatic inline int 1421.2SadMUTEX_HAS_WAITERS(volatile kmutex_t *mtx) 1431.2Sad{ 1441.2Sad if (mtx->mtx_owner == 0) 1451.2Sad return 0; 1461.2Sad return mtx->mtx_lock == __SIMPLELOCK_LOCKED; 1471.2Sad} 1481.2Sad 1491.2Sadstatic inline void 1501.6SyamtMUTEX_INITIALIZE_SPIN(kmutex_t *mtx, bool dodebug, int ipl) 1511.2Sad{ 1521.6Syamt mtx->mtx_dodebug = dodebug; 1531.6Syamt mtx->mtx_isspin = 1; 1541.2Sad mtx->mtx_ipl = makeiplcookie(ipl); 1551.2Sad mtx->mtx_interlock = __SIMPLELOCK_LOCKED; 1561.2Sad __cpu_simple_lock_init(&mtx->mtx_lock); 1571.2Sad} 1581.2Sad 1591.2Sadstatic inline void 1601.6SyamtMUTEX_INITIALIZE_ADAPTIVE(kmutex_t *mtx, bool dodebug) 1611.2Sad{ 1621.6Syamt mtx->mtx_dodebug = dodebug; 1631.6Syamt mtx->mtx_isspin = 0; 1641.2Sad __cpu_simple_lock_init(&mtx->mtx_lock); 1651.2Sad} 1661.2Sad 1671.2Sadstatic inline void 1681.2SadMUTEX_DESTROY(kmutex_t *mtx) 1691.2Sad{ 1701.2Sad mtx->mtx_owner = (uintptr_t)-1L; 1711.2Sad} 1721.2Sad 1731.6Syamtstatic inline bool 1741.6SyamtMUTEX_DEBUG_P(kmutex_t *mtx) 1751.2Sad{ 1761.6Syamt return mtx->mtx_dodebug != 0; 1771.2Sad} 1781.2Sad 1791.2Sadstatic inline int 1801.2SadMUTEX_SPIN_P(volatile kmutex_t *mtx) 1811.2Sad{ 1821.6Syamt return mtx->mtx_isspin != 0; 1831.2Sad} 1841.2Sad 1851.2Sadstatic inline int 1861.2SadMUTEX_ADAPTIVE_P(volatile kmutex_t *mtx) 1871.2Sad{ 1881.6Syamt return mtx->mtx_isspin == 0; 1891.2Sad} 1901.2Sad 1911.2Sadstatic inline int 1921.2SadMUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t curthread) 1931.2Sad{ 1941.2Sad if (!__cpu_simple_lock_try(&mtx->mtx_interlock)) 1951.2Sad return 0; 1961.2Sad mtx->mtx_owner = (curthread >> 5) | 0xf8000000; 1971.2Sad return 1; 1981.2Sad} 1991.2Sad 2001.2Sadstatic inline void 2011.2SadMUTEX_RELEASE(kmutex_t *mtx) 2021.2Sad{ 2031.2Sad mtx->mtx_owner = 0; 2041.2Sad __cpu_simple_unlock(&mtx->mtx_lock); 2051.2Sad} 2061.2Sad 2071.3Sadstatic inline void 2081.3SadMUTEX_CLEAR_WAITERS(kmutex_t *mtx) 2091.3Sad{ 2101.3Sad __cpu_simple_unlock(&mtx->mtx_lock); 2111.3Sad} 2121.3Sad 2131.2Sad#endif /* __MUTEX_PRIVATE */ 2141.2Sad 2151.2Sad#endif /* _SPARC_MUTEX_H_ */ 216