Home | History | Annotate | Line # | Download | only in include
mutex.h revision 1.5.10.1
      1 /*	$NetBSD: mutex.h,v 1.5.10.1 2007/11/06 23:17:07 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 _HPPA_MUTEX_H_
     40 #define	_HPPA_MUTEX_H_
     41 
     42 /*
     43  * The HPPA mutex implementation is troublesome, because HPPA lacks
     44  * a compare-and-set operation, yet there are many SMP HPPA machines
     45  * in circulation.  SMP for spin mutexes is easy - we don't need to
     46  * know who owns the lock.  For adaptive mutexes, we need an owner
     47  * field and additional interlock
     48  */
     49 
     50 #ifndef __ASSEMBLER__
     51 
     52 #include <machine/lock.h>
     53 
     54 struct kmutex {
     55 	union {
     56 		/*
     57 		 * Only the 16 bytes aligned word of __cpu_simple_lock_t will
     58 		 * be used. It's 16 bytes to simplify the allocation.
     59 		 * See hppa/lock.h
     60 		 */
     61 #ifdef __MUTEX_PRIVATE
     62 		struct {
     63 			__cpu_simple_lock_t	mtxu_lock;	/* 0-15 */
     64 			volatile uint32_t	mtxs_owner;	/* 16-19 */
     65 			ipl_cookie_t		mtxs_ipl;	/* 20-23 */
     66 			volatile uint8_t	mtxs_waiters;	/* 24 */
     67 
     68 			/* For LOCKDEBUG */
     69 			uint8_t			mtxs_id[3];	/* 25-27 */
     70 		} s;
     71 #endif
     72 		uint8_t			mtxu_pad[32];	/* 0 - 32 */
     73 	} u;
     74 } __aligned (16);
     75 #endif
     76 
     77 #ifdef __MUTEX_PRIVATE
     78 
     79 #define	__HAVE_MUTEX_STUBS	1
     80 
     81 #define	mtx_lock	u.s.mtxu_lock
     82 #define	mtx_owner	u.s.mtxs_owner
     83 #define	mtx_ipl		u.s.mtxs_ipl
     84 #define	mtx_waiters	u.s.mtxs_waiters
     85 #define	mtx_id		u.s.mtxs_id
     86 
     87 /* Magic constants for mtx_owner */
     88 #define	MUTEX_ADAPTIVE_UNOWNED		0xffffff00
     89 #define	MUTEX_SPIN_FLAG			0xffffff10
     90 #define	MUTEX_UNOWNED_OR_SPIN(x)	(((x) & 0xffffffef) == 0xffffff00)
     91 
     92 #ifndef __ASSEMBLER__
     93 
     94 static inline uintptr_t
     95 MUTEX_OWNER(uintptr_t owner)
     96 {
     97 	return owner;
     98 }
     99 
    100 static inline int
    101 MUTEX_OWNED(uintptr_t owner)
    102 {
    103 	return owner != MUTEX_ADAPTIVE_UNOWNED;
    104 }
    105 
    106 static inline int
    107 MUTEX_SET_WAITERS(kmutex_t *mtx, uintptr_t owner)
    108 {
    109 	mb_write();
    110 	mtx->mtx_waiters = 1;
    111 	mb_memory();
    112 	return mtx->mtx_owner != MUTEX_ADAPTIVE_UNOWNED;
    113 }
    114 
    115 static inline int
    116 MUTEX_HAS_WAITERS(volatile kmutex_t *mtx)
    117 {
    118 	return mtx->mtx_waiters != 0;
    119 }
    120 
    121 static inline void
    122 MUTEX_INITIALIZE_SPIN(kmutex_t *mtx, u_int id, int ipl)
    123 {
    124 	mtx->mtx_ipl = makeiplcookie(ipl);
    125 	mtx->mtx_id[0] = (uint8_t)id;
    126 	mtx->mtx_id[1] = (uint8_t)(id >> 8);
    127 	mtx->mtx_id[2] = (uint8_t)(id >> 16);
    128 	mtx->mtx_owner = MUTEX_SPIN_FLAG;
    129 	__cpu_simple_lock_init(&mtx->mtx_lock);
    130 }
    131 
    132 static inline void
    133 MUTEX_INITIALIZE_ADAPTIVE(kmutex_t *mtx, u_int id)
    134 {
    135 	mtx->mtx_id[0] = (uint8_t)id;
    136 	mtx->mtx_id[1] = (uint8_t)(id >> 8);
    137 	mtx->mtx_id[2] = (uint8_t)(id >> 16);
    138 	mtx->mtx_owner = MUTEX_ADAPTIVE_UNOWNED;
    139 	__cpu_simple_lock_init(&mtx->mtx_lock);
    140 }
    141 
    142 static inline void
    143 MUTEX_DESTROY(kmutex_t *mtx)
    144 {
    145 	mtx->mtx_owner = 0xffffffff;
    146 	mtx->mtx_id[0] = 0xff;
    147 	mtx->mtx_id[1] = 0xff;
    148 	mtx->mtx_id[2] = 0xff;
    149 }
    150 
    151 static inline u_int
    152 MUTEX_GETID(kmutex_t *mtx)
    153 {
    154 	return (u_int)mtx->mtx_id[0] |
    155 	    ((u_int)mtx->mtx_id[1] << 8) |
    156 	    ((u_int)mtx->mtx_id[2] << 16);
    157 }
    158 
    159 static inline int
    160 MUTEX_SPIN_P(volatile kmutex_t *mtx)
    161 {
    162 	return mtx->mtx_owner == MUTEX_SPIN_FLAG;
    163 }
    164 
    165 static inline int
    166 MUTEX_ADAPTIVE_P(volatile kmutex_t *mtx)
    167 {
    168 	return mtx->mtx_owner != MUTEX_SPIN_FLAG;
    169 }
    170 
    171 /* Acquire an adaptive mutex */
    172 static inline int
    173 MUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t curthread)
    174 {
    175 	if (!__cpu_simple_lock_try(&mtx->mtx_lock))
    176 		return 0;
    177 	mtx->mtx_owner = curthread;
    178 	return 1;
    179 }
    180 
    181 /* Release an adaptive mutex */
    182 static inline void
    183 MUTEX_RELEASE(kmutex_t *mtx)
    184 {
    185 	mtx->mtx_owner = MUTEX_ADAPTIVE_UNOWNED;
    186 	__cpu_simple_unlock(&mtx->mtx_lock);
    187 	mtx->mtx_waiters = 0;
    188 }
    189 
    190 static inline void
    191 MUTEX_CLEAR_WAITERS(kmutex_t *mtx)
    192 {
    193 	mtx->mtx_waiters = 0;
    194 }
    195 
    196 #endif	/* __ASSEMBLER__ */
    197 
    198 #endif	/* __MUTEX_PRIVATE */
    199 
    200 #endif /* _HPPA_MUTEX_H_ */
    201