MPSAFE - mutexes
[dragonfly.git] / sys / sys / mutex2.h
CommitLineData
33b0b87c
MD
1/*
2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#ifndef _SYS_MUTEX2_H_
36#define _SYS_MUTEX2_H_
37
38#ifndef _SYS_MUTEX_H_
39#include <sys/mutex.h>
40#endif
41#ifndef _MACHINE_ATOMIC_H_
42#include <machine/atomic.h>
43#endif
44
45/*
46 * Initialize a new mutex, placing it in an unlocked state with no refs.
47 */
48static __inline void
49mtx_init(mtx_t mtx)
50{
51 mtx->mtx_lock = 0;
52 mtx->mtx_refs = 0;
53 mtx->mtx_owner = NULL;
54}
55
56/*
57 * Deinitialize a mutex
58 */
59static __inline void
60mtx_uninit(mtx_t mtx)
61{
62 /* empty */
63}
64
65/*
66 * Exclusive-lock a mutex, block until acquired. Recursion is allowed.
7355baa5
MD
67 *
68 * Returns 0 on success, or the tsleep() return code on failure.
69 * An error can only be returned if PCATCH is specified in the flags.
33b0b87c 70 */
7355baa5
MD
71static __inline int
72mtx_lock_ex(mtx_t mtx, const char *ident, int flags, int to)
73{
74 if (atomic_cmpset_int(&mtx->mtx_lock, 0, MTX_EXCLUSIVE | 1) == 0)
75 return(_mtx_lock_ex(mtx, ident, flags, to));
76 mtx->mtx_owner = curthread;
77 return(0);
78}
79
80static __inline int
81mtx_lock_ex_quick(mtx_t mtx, const char *ident)
33b0b87c
MD
82{
83 if (atomic_cmpset_int(&mtx->mtx_lock, 0, MTX_EXCLUSIVE | 1) == 0)
7355baa5 84 return(_mtx_lock_ex_quick(mtx, ident));
33b0b87c 85 mtx->mtx_owner = curthread;
7355baa5 86 return(0);
33b0b87c
MD
87}
88
89/*
90 * Share-lock a mutex, block until acquired. Recursion is allowed.
7355baa5
MD
91 *
92 * Returns 0 on success, or the tsleep() return code on failure.
93 * An error can only be returned if PCATCH is specified in the flags.
33b0b87c 94 */
7355baa5
MD
95static __inline int
96mtx_lock_sh(mtx_t mtx, const char *ident, int flags, int to)
97{
98 if (atomic_cmpset_int(&mtx->mtx_lock, 0, 1) == 0)
99 return(_mtx_lock_sh(mtx, ident, flags, to));
100 return(0);
101}
102
103static __inline int
104mtx_lock_sh_quick(mtx_t mtx, const char *ident)
33b0b87c
MD
105{
106 if (atomic_cmpset_int(&mtx->mtx_lock, 0, 1) == 0)
7355baa5
MD
107 return(_mtx_lock_sh_quick(mtx, ident));
108 return(0);
33b0b87c
MD
109}
110
111/*
112 * Exclusive-lock a mutex, spin until acquired. Recursion is allowed.
113 */
114static __inline void
115mtx_spinlock_ex(mtx_t mtx)
116{
117 if (atomic_cmpset_int(&mtx->mtx_lock, 0, MTX_EXCLUSIVE | 1) == 0)
118 _mtx_spinlock_ex(mtx);
119}
120
121/*
122 * Share-lock a mutex, spin until acquired. Recursion is allowed.
123 */
124static __inline void
125mtx_spinlock_sh(mtx_t mtx)
126{
127 if (atomic_cmpset_int(&mtx->mtx_lock, 0, 1) == 0)
128 _mtx_spinlock_sh(mtx);
129}
130
131/*
132 * Attempt to exclusive-lock a mutex, return 0 on success and
133 * EAGAIN on failure.
134 */
135static __inline int
136mtx_lock_ex_try(mtx_t mtx)
137{
138 if (atomic_cmpset_int(&mtx->mtx_lock, 0, MTX_EXCLUSIVE | 1) == 0)
139 return (_mtx_lock_ex_try(mtx));
140 mtx->mtx_owner = curthread;
141 return (0);
142}
143
144/*
145 * Attempt to share-lock a mutex, return 0 on success and
146 * EAGAIN on failure.
147 */
148static __inline int
149mtx_lock_sh_try(mtx_t mtx)
150{
151 if (atomic_cmpset_int(&mtx->mtx_lock, 0, 1) == 0)
152 return (_mtx_lock_sh_try(mtx));
153 return (0);
154}
155
156/*
157 * If the lock is held exclusively it must be owned by the caller. If the
158 * lock is already a shared lock this operation is a NOP. A panic will
159 * occur if the lock is not held either shared or exclusive.
160 *
161 * The exclusive count is converted to a shared count.
162 */
163static __inline void
164mtx_downgrade(mtx_t mtx)
165{
166 mtx->mtx_owner = NULL;
167 if (atomic_cmpset_int(&mtx->mtx_lock, MTX_EXCLUSIVE | 1, 0) == 0)
168 _mtx_downgrade(mtx);
169}
170
171/*
172 * Upgrade a shared lock to an exclusive lock. The upgrade will fail if
173 * the shared lock has a count other then 1. Optimize the most likely case
174 * but note that a single cmpset can fail due to WANTED races.
175 *
176 * If the lock is held exclusively it must be owned by the caller and
177 * this function will simply return without doing anything. A panic will
178 * occur if the lock is held exclusively by someone other then the caller.
179 *
180 * Returns 0 on success, EDEADLK on failure.
181 */
182static __inline int
183mtx_upgrade_try(mtx_t mtx)
184{
185 if (atomic_cmpset_int(&mtx->mtx_lock, 1, MTX_EXCLUSIVE | 1))
186 return(0);
187 return (_mtx_upgrade_try(mtx));
188}
189
190/*
191 * Optimized unlock cases.
192 */
193static __inline void
194mtx_unlock(mtx_t mtx)
195{
196 u_int lock = mtx->mtx_lock;
197
198 if (lock == (MTX_EXCLUSIVE | 1)) {
199 mtx->mtx_owner = NULL;
200 if (atomic_cmpset_int(&mtx->mtx_lock, lock, 0) == 0)
201 _mtx_unlock(mtx);
202 } else if (lock == 1) {
203 if (atomic_cmpset_int(&mtx->mtx_lock, lock, 0) == 0)
204 _mtx_unlock(mtx);
205 } else {
206 _mtx_unlock(mtx);
207 }
208}
209
210static __inline void
211mtx_unlock_ex(mtx_t mtx)
212{
213 u_int lock = mtx->mtx_lock;
214
215 if (lock == (MTX_EXCLUSIVE | 1)) {
216 mtx->mtx_owner = NULL;
217 if (atomic_cmpset_int(&mtx->mtx_lock, lock, 0) == 0)
218 _mtx_unlock(mtx);
219 } else {
220 _mtx_unlock(mtx);
221 }
222}
223
224static __inline void
225mtx_unlock_sh(mtx_t mtx)
226{
227 if (atomic_cmpset_int(&mtx->mtx_lock, 1, 0) == 0)
228 _mtx_unlock(mtx);
229}
230
231/*
17386740
MD
232 * Return TRUE (non-zero) if the mutex is locked shared or exclusive by
233 * anyone, including the owner.
234 */
235static __inline int
236mtx_islocked(mtx_t mtx)
237{
238 return(mtx->mtx_lock != 0);
239}
240
241/*
242 * Return TRUE (non-zero) if the mutex is locked exclusively by anyone,
243 * including the owner.
244 *
245 * The mutex may in an unlocked or shared lock state.
246 */
247static __inline int
248mtx_islocked_ex(mtx_t mtx)
249{
250 return((mtx->mtx_lock & MTX_EXCLUSIVE) != 0);
251}
252
253/*
254 * Return TRUE (non-zero) if the mutex is not locked.
255 */
256static __inline int
257mtx_notlocked(mtx_t mtx)
258{
259 return(mtx->mtx_lock == 0);
260}
261
262/*
263 * Return TRUE (non-zero) if the mutex is not locked exclusively.
264 * The mutex may in an unlocked or shared lock state.
265 */
266static __inline int
267mtx_notlocked_ex(mtx_t mtx)
268{
269 return((mtx->mtx_lock & MTX_EXCLUSIVE) != 0);
270}
271
272/*
273 * Return TRUE (non-zero) if the mutex is exclusively locked by
274 * the caller.
275 */
276static __inline int
277mtx_owned(mtx_t mtx)
278{
279 return((mtx->mtx_lock & MTX_EXCLUSIVE) && mtx->mtx_owner == curthread);
280}
281
282/*
283 * Return TRUE (non-zero) if the mutex is not exclusively locked by
284 * the caller.
285 */
286static __inline int
287mtx_notowned(mtx_t mtx)
288{
289 return((mtx->mtx_lock & MTX_EXCLUSIVE) == 0 ||
290 mtx->mtx_owner != curthread);
291}
292
293/*
294 * Return the shared or exclusive lock count. A return value of 0
295 * indicate that the mutex is not locked.
296 *
297 * NOTE: If the mutex is held exclusively by someone other then the
298 * caller the lock count for the other owner is still returned.
299 */
300static __inline int
301mtx_lockrefs(mtx_t mtx)
302{
303 return(mtx->mtx_lock & MTX_MASK);
304}
305
306/*
33b0b87c
MD
307 * Bump the lock's ref count. This field is independent of the lock.
308 */
309static __inline void
310mtx_hold(mtx_t mtx)
311{
312 atomic_add_acq_int(&mtx->mtx_refs, 1);
313}
314
315/*
316 * Drop the lock's ref count. This field is independent of the lock.
317 *
318 * Returns the previous ref count, interlocked so testing against
319 * 1 means you won the 1->0 transition
320 */
321static __inline int
322mtx_drop(mtx_t mtx)
323{
324 return (atomic_fetchadd_int(&mtx->mtx_refs, -1));
325}
326
327#endif