1 /* $NetBSD: mutex.h,v 1.8 2007/11/21 10:19:07 yamt Exp $ */
4 * Copyright (c) 2002, 2007 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe and Andrew Doran.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #ifndef _HPPA_MUTEX_H_
33 #define _HPPA_MUTEX_H_
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
45 #include <machine/lock.h>
50 * Only the 16 bytes aligned word of __cpu_simple_lock_t will
51 * be used. It's 16 bytes to simplify the allocation.
54 #ifdef __MUTEX_PRIVATE
56 __cpu_simple_lock_t mtxu_lock
; /* 0-15 */
57 volatile uint32_t mtxs_owner
; /* 16-19 */
58 ipl_cookie_t mtxs_ipl
; /* 20-23 */
59 volatile uint8_t mtxs_waiters
; /* 24 */
62 uint8_t mtxs_dodebug
; /* 25 */
65 uint8_t mtxu_pad
[32]; /* 0 - 32 */
70 #ifdef __MUTEX_PRIVATE
72 #define __HAVE_MUTEX_STUBS 1
74 #define mtx_lock u.s.mtxu_lock
75 #define mtx_owner u.s.mtxs_owner
76 #define mtx_ipl u.s.mtxs_ipl
77 #define mtx_waiters u.s.mtxs_waiters
78 #define mtx_dodebug u.s.mtxs_dodebug
80 /* Magic constants for mtx_owner */
81 #define MUTEX_ADAPTIVE_UNOWNED 0xffffff00
82 #define MUTEX_SPIN_FLAG 0xffffff10
83 #define MUTEX_UNOWNED_OR_SPIN(x) (((x) & 0xffffffef) == 0xffffff00)
87 static inline uintptr_t
88 MUTEX_OWNER(uintptr_t owner
)
94 MUTEX_OWNED(uintptr_t owner
)
96 return owner
!= MUTEX_ADAPTIVE_UNOWNED
;
100 MUTEX_SET_WAITERS(kmutex_t
*mtx
, uintptr_t owner
)
103 mtx
->mtx_waiters
= 1;
105 return mtx
->mtx_owner
!= MUTEX_ADAPTIVE_UNOWNED
;
109 MUTEX_HAS_WAITERS(volatile kmutex_t
*mtx
)
111 return mtx
->mtx_waiters
!= 0;
115 MUTEX_INITIALIZE_SPIN(kmutex_t
*mtx
, bool dodebug
, int ipl
)
117 mtx
->mtx_ipl
= makeiplcookie(ipl
);
118 mtx
->mtx_dodebug
= dodebug
;
119 mtx
->mtx_owner
= MUTEX_SPIN_FLAG
;
120 __cpu_simple_lock_init(&mtx
->mtx_lock
);
124 MUTEX_INITIALIZE_ADAPTIVE(kmutex_t
*mtx
, bool dodebug
)
126 mtx
->mtx_dodebug
= dodebug
;
127 mtx
->mtx_owner
= MUTEX_ADAPTIVE_UNOWNED
;
128 __cpu_simple_lock_init(&mtx
->mtx_lock
);
132 MUTEX_DESTROY(kmutex_t
*mtx
)
134 mtx
->mtx_owner
= 0xffffffff;
138 MUTEX_DEBUG_P(kmutex_t
*mtx
)
140 return mtx
->mtx_dodebug
!= 0;
144 MUTEX_SPIN_P(volatile kmutex_t
*mtx
)
146 return mtx
->mtx_owner
== MUTEX_SPIN_FLAG
;
150 MUTEX_ADAPTIVE_P(volatile kmutex_t
*mtx
)
152 return mtx
->mtx_owner
!= MUTEX_SPIN_FLAG
;
155 /* Acquire an adaptive mutex */
157 MUTEX_ACQUIRE(kmutex_t
*mtx
, uintptr_t curthread
)
159 if (!__cpu_simple_lock_try(&mtx
->mtx_lock
))
161 mtx
->mtx_owner
= curthread
;
165 /* Release an adaptive mutex */
167 MUTEX_RELEASE(kmutex_t
*mtx
)
169 mtx
->mtx_owner
= MUTEX_ADAPTIVE_UNOWNED
;
170 __cpu_simple_unlock(&mtx
->mtx_lock
);
171 mtx
->mtx_waiters
= 0;
175 MUTEX_CLEAR_WAITERS(kmutex_t
*mtx
)
177 mtx
->mtx_waiters
= 0;
180 #endif /* __ASSEMBLER__ */
182 #endif /* __MUTEX_PRIVATE */
184 #endif /* _HPPA_MUTEX_H_ */