Home | History | Annotate | Line # | Download | only in include
mutex.h revision 1.1.2.2
      1 /*	$NetBSD: mutex.h,v 1.1.2.2 2007/02/01 05:04:25 ad 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 struct kmutex {
     50 	union {
     51 		/*
     52 		 * Only the low 4 bytes of the lock will be used by
     53 		 * __cpu_simple_lock(), but it must be aligned on a
     54 		 * 16-byte boundary.  See hppa/lock.h
     55 		 */
     56 #ifdef __MUTEX_PRIVATE
     57 		__cpu_simple_lock_t	mtxu_lock;		/* 0-15 */
     58 		struct {
     59 			volatile uint32_t	mtxs_lockword;	/* 0-3 */
     60 			volatile uint32_t	mtxs_owner;	/* 4-7 */
     61 			ipl_cookie_t		mtxs_ipl;	/* 8-11 */
     62 			volatile uint8_t	mtxs_waiters;	/* 12 */
     63 
     64 			/* For LOCKDEBUG */
     65 			uint8_t			mtxs_id[3];	/* 13-15 */
     66 		} s;
     67 #endif
     68 		uint8_t			mtxu_pad[16];		/* 0-15 */
     69 	} u;
     70 } __aligned (16);
     71 
     72 #ifdef __MUTEX_PRIVATE
     73 
     74 #define	__HAVE_MUTEX_STUBS	1
     75 
     76 #define	mtx_lock	u.mtxu_lock
     77 #define	mtx_owner	u.s.mtxs_owner
     78 #define	mtx_ipl		u.s.mtxs_ipl
     79 #define	mtx_waiters	u.s.mtxs_waiters
     80 #define	mtx_id		u.s.mtxs_id
     81 
     82 /* Magic constants for mtx_owner */
     83 #define	MUTEX_ADAPTIVE_UNOWNED		0xfffffff0
     84 #define	MUTEX_SPIN_FLAG			0xffffffff
     85 #define	MUTEX_UNOWNED_OR_SPIN(x)	(((x) & 0xfffffff0) == 0xfffffff0)
     86 
     87 static uintptr_t __attribute((unused))
     88 MUTEX_OWNER(uintptr_t owner)
     89 {
     90 	return owner;
     91 }
     92 
     93 static inline int
     94 MUTEX_SET_WAITERS(kmutex_t *mtx, uintptr_t owner)
     95 {
     96 	mb_write();
     97 	mtx->mtx_waiters = 1;
     98 	mb_memory();
     99 	return mtx->mtx_owner != MUTEX_ADAPTIVE_UNOWNED;
    100 }
    101 
    102 static inline int
    103 MUTEX_HAS_WAITERS(volatile kmutex_t *mtx)
    104 {
    105 	return mtx->mtx_waiters != 0;
    106 }
    107 
    108 static inline void
    109 MUTEX_INITIALIZE_SPIN(kmutex_t *mtx, u_int id, int ipl)
    110 {
    111 	mtx->mtx_ipl = makeiplcookie(ipl);
    112 	mtx->mtx_id[0] = (uint8_t)id;
    113 	mtx->mtx_id[1] = (uint8_t)(id >> 8);
    114 	mtx->mtx_id[2] = (uint8_t)(id >> 16);
    115 	mtx->mtx_owner = MUTEX_SPIN_FLAG;
    116 	__cpu_simple_lock_init(&mtx->mtx_lock);
    117 }
    118 
    119 static inline void
    120 MUTEX_INITIALIZE_ADAPTIVE(kmutex_t *mtx, u_int id)
    121 {
    122 	mtx->mtx_id[0] = (uint8_t)id;
    123 	mtx->mtx_id[1] = (uint8_t)(id >> 8);
    124 	mtx->mtx_id[2] = (uint8_t)(id >> 16);
    125 	mtx->mtx_owner = MUTEX_ADAPTIVE_UNOWNED;
    126 	__cpu_simple_lock_init(&mtx->mtx_lock);
    127 }
    128 
    129 static inline void
    130 MUTEX_DESTROY(kmutex_t *mtx)
    131 {
    132 	mtx->mtx_waiters = 1;
    133 	mtx->mtx_id[0] = 0xff;
    134 	mtx->mtx_id[1] = 0xff;
    135 	mtx->mtx_id[2] = 0xff;
    136 }
    137 
    138 static inline u_int
    139 MUTEX_GETID(kmutex_t *mtx)
    140 {
    141 	return (u_int)mtx->mtx_id[0] |
    142 	    ((u_int)mtx->mtx_id[1] << 8) |
    143 	    ((u_int)mtx->mtx_id[2] << 16);
    144 }
    145 
    146 static inline int
    147 MUTEX_SPIN_P(volatile kmutex_t *mtx)
    148 {
    149 	return mtx->mtx_owner == MUTEX_SPIN_FLAG;
    150 }
    151 
    152 static inline int
    153 MUTEX_ADAPTIVE_P(volatile kmutex_t *mtx)
    154 {
    155 	return mtx->mtx_owner != MUTEX_SPIN_FLAG;
    156 }
    157 
    158 /* Acquire an adaptive mutex */
    159 static inline int
    160 MUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t curthread)
    161 {
    162 	if (!__cpu_simple_lock_try(&mtx->mtx_lock))
    163 		return 0;
    164 	mtx->mtx_owner = curthread;
    165 	return 1;
    166 }
    167 
    168 /* Release an adaptive mutex */
    169 static inline void
    170 MUTEX_RELEASE(kmutex_t *mtx)
    171 {
    172 	mtx->mtx_owner = MUTEX_ADAPTIVE_UNOWNED;
    173 	__cpu_simple_unlock(&mtx->mtx_lock);
    174 	mtx->mtx_waiters = 0;
    175 }
    176 
    177 #endif	/* __MUTEX_PRIVATE */
    178 
    179 #endif /* _HPPA_MUTEX_H_ */
    180