No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / hppa / include / mutex.h
blob5244c724168c02399117972767e1c6e8c1e51552
1 /* $NetBSD: mutex.h,v 1.8 2007/11/21 10:19:07 yamt Exp $ */
3 /*-
4 * Copyright (c) 2002, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
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
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.
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
43 #ifndef __ASSEMBLER__
45 #include <machine/lock.h>
47 struct kmutex {
48 union {
50 * Only the 16 bytes aligned word of __cpu_simple_lock_t will
51 * be used. It's 16 bytes to simplify the allocation.
52 * See hppa/lock.h
54 #ifdef __MUTEX_PRIVATE
55 struct {
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 */
61 /* For LOCKDEBUG */
62 uint8_t mtxs_dodebug; /* 25 */
63 } s;
64 #endif
65 uint8_t mtxu_pad[32]; /* 0 - 32 */
66 } u;
67 } __aligned (16);
68 #endif
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)
85 #ifndef __ASSEMBLER__
87 static inline uintptr_t
88 MUTEX_OWNER(uintptr_t owner)
90 return owner;
93 static inline int
94 MUTEX_OWNED(uintptr_t owner)
96 return owner != MUTEX_ADAPTIVE_UNOWNED;
99 static inline int
100 MUTEX_SET_WAITERS(kmutex_t *mtx, uintptr_t owner)
102 mb_write();
103 mtx->mtx_waiters = 1;
104 mb_memory();
105 return mtx->mtx_owner != MUTEX_ADAPTIVE_UNOWNED;
108 static inline int
109 MUTEX_HAS_WAITERS(volatile kmutex_t *mtx)
111 return mtx->mtx_waiters != 0;
114 static inline void
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);
123 static inline void
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);
131 static inline void
132 MUTEX_DESTROY(kmutex_t *mtx)
134 mtx->mtx_owner = 0xffffffff;
137 static inline bool
138 MUTEX_DEBUG_P(kmutex_t *mtx)
140 return mtx->mtx_dodebug != 0;
143 static inline int
144 MUTEX_SPIN_P(volatile kmutex_t *mtx)
146 return mtx->mtx_owner == MUTEX_SPIN_FLAG;
149 static inline int
150 MUTEX_ADAPTIVE_P(volatile kmutex_t *mtx)
152 return mtx->mtx_owner != MUTEX_SPIN_FLAG;
155 /* Acquire an adaptive mutex */
156 static inline int
157 MUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t curthread)
159 if (!__cpu_simple_lock_try(&mtx->mtx_lock))
160 return 0;
161 mtx->mtx_owner = curthread;
162 return 1;
165 /* Release an adaptive mutex */
166 static inline void
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;
174 static inline void
175 MUTEX_CLEAR_WAITERS(kmutex_t *mtx)
177 mtx->mtx_waiters = 0;
180 #endif /* __ASSEMBLER__ */
182 #endif /* __MUTEX_PRIVATE */
184 #endif /* _HPPA_MUTEX_H_ */