Home | History | Annotate | Line # | Download | only in include
mutex.h revision 1.18
      1 /*	$NetBSD: mutex.h,v 1.18 2023/07/12 12:50:12 riastradh 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef _HPPA_MUTEX_H_
     33 #define	_HPPA_MUTEX_H_
     34 
     35 /*
     36  * The HPPA mutex implementation is troublesome, because HPPA lacks
     37  * a compare-and-set operation, yet there are many SMP HPPA machines
     38  * in circulation.  SMP for spin mutexes is easy - we don't need to
     39  * know who owns the lock.  For adaptive mutexes, we need an owner
     40  * field and additional interlock
     41  */
     42 
     43 #ifndef __ASSEMBLER__
     44 
     45 #include <sys/types.h>
     46 
     47 #ifdef __MUTEX_PRIVATE
     48 #include <machine/intr.h>
     49 #endif
     50 
     51 struct kmutex {
     52 	union {
     53 		/*
     54 		 * Only the 16 bytes aligned word of __cpu_simple_lock_t will
     55 		 * be used. It's 16 bytes to simplify the allocation.
     56 		 * See hppa/lock.h
     57 		 */
     58 #ifdef __MUTEX_PRIVATE
     59 		struct {
     60 			__cpu_simple_lock_t	mtxu_lock;	/* 0-15 */
     61 			volatile uint32_t	mtxs_owner;	/* 16-19 */
     62 			ipl_cookie_t		mtxs_ipl;	/* 20-23 */
     63 			volatile uint8_t	mtxs_waiters;	/* 24 */
     64 
     65 			/* For LOCKDEBUG */
     66 			uint8_t			mtxs_dodebug;	/* 25 */
     67 		} s;
     68 #endif
     69 		uint8_t			mtxu_pad[32];	/* 0 - 32 */
     70 	} u;
     71 } __aligned (16);
     72 #endif
     73 
     74 #ifdef __MUTEX_PRIVATE
     75 
     76 #define	__HAVE_MUTEX_STUBS	1
     77 
     78 #define	mtx_lock	u.s.mtxu_lock
     79 #define	mtx_owner	u.s.mtxs_owner
     80 #define	mtx_ipl		u.s.mtxs_ipl
     81 #define	mtx_waiters	u.s.mtxs_waiters
     82 #define	mtx_dodebug	u.s.mtxs_dodebug
     83 
     84 /* Magic constants for mtx_owner */
     85 #define	MUTEX_ADAPTIVE_UNOWNED		0xffffff00
     86 #define	MUTEX_SPIN_FLAG			0xffffff10
     87 #define	MUTEX_UNOWNED_OR_SPIN(x)	(((x) & 0xffffffef) == 0xffffff00)
     88 
     89 #if !defined(__ASSEMBLER__) && defined(_KERNEL)
     90 
     91 static inline uintptr_t
     92 MUTEX_OWNER(uintptr_t owner)
     93 {
     94 	return owner;
     95 }
     96 
     97 static inline int
     98 MUTEX_OWNED(uintptr_t owner)
     99 {
    100 	return owner != MUTEX_ADAPTIVE_UNOWNED;
    101 }
    102 
    103 static inline int
    104 MUTEX_SET_WAITERS(struct kmutex *mtx, uintptr_t owner)
    105 {
    106 	__sync();		/* formerly mb_read */
    107 	mtx->mtx_waiters = 1;
    108 	__sync();		/* formerly mb_memory */
    109 	return mtx->mtx_owner != MUTEX_ADAPTIVE_UNOWNED;
    110 }
    111 
    112 static inline int
    113 MUTEX_HAS_WAITERS(const volatile struct kmutex *mtx)
    114 {
    115 	return mtx->mtx_waiters != 0;
    116 }
    117 
    118 static inline void
    119 MUTEX_INITIALIZE_SPIN(struct kmutex *mtx, bool dodebug, int ipl)
    120 {
    121 	mtx->mtx_ipl = makeiplcookie(ipl);
    122 	mtx->mtx_dodebug = dodebug;
    123 	mtx->mtx_owner = MUTEX_SPIN_FLAG;
    124 	__cpu_simple_lock_init(&mtx->mtx_lock);
    125 }
    126 
    127 static inline void
    128 MUTEX_INITIALIZE_ADAPTIVE(struct kmutex *mtx, bool dodebug)
    129 {
    130 	mtx->mtx_dodebug = dodebug;
    131 	mtx->mtx_owner = MUTEX_ADAPTIVE_UNOWNED;
    132 	__cpu_simple_lock_init(&mtx->mtx_lock);
    133 }
    134 
    135 static inline void
    136 MUTEX_DESTROY(struct kmutex *mtx)
    137 {
    138 	mtx->mtx_owner = 0xffffffff;
    139 }
    140 
    141 static inline bool
    142 MUTEX_DEBUG_P(const volatile struct kmutex *mtx)
    143 {
    144 	return mtx->mtx_dodebug != 0;
    145 }
    146 
    147 static inline int
    148 MUTEX_SPIN_P(const uintptr_t owner)
    149 {
    150 	return owner == MUTEX_SPIN_FLAG;
    151 }
    152 
    153 static inline int
    154 MUTEX_ADAPTIVE_P(const uintptr_t owner)
    155 {
    156 	return owner != MUTEX_SPIN_FLAG;
    157 }
    158 
    159 /* Acquire an adaptive mutex */
    160 static inline int
    161 MUTEX_ACQUIRE(struct kmutex *mtx, uintptr_t curthread)
    162 {
    163 	if (!__cpu_simple_lock_try(&mtx->mtx_lock))
    164 		return 0;
    165 	mtx->mtx_owner = curthread;
    166 	return 1;
    167 }
    168 
    169 /* Release an adaptive mutex */
    170 static inline void
    171 MUTEX_RELEASE(struct kmutex *mtx)
    172 {
    173 	mtx->mtx_owner = MUTEX_ADAPTIVE_UNOWNED;
    174 	__cpu_simple_unlock(&mtx->mtx_lock);
    175 	mtx->mtx_waiters = 0;
    176 }
    177 
    178 static inline void
    179 MUTEX_CLEAR_WAITERS(struct kmutex *mtx)
    180 {
    181 	mtx->mtx_waiters = 0;
    182 }
    183 
    184 #endif	/* !__ASSEMBLER__ && _KERNEL */
    185 
    186 #endif	/* __MUTEX_PRIVATE */
    187 
    188 #endif /* _HPPA_MUTEX_H_ */
    189