2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
35 * Implement fast persistent locks based on atomic_cmpset_int() with
36 * semantics similar to lockmgr locks but faster and taking up much less
37 * space. Taken from HAMMER's lock implementation.
39 * These are meant to complement our LWKT tokens. Tokens are only held
40 * while the thread is running. Mutexes can be held across blocking
43 * Most of the support is in sys/mutex[2].h. We mostly provide backoff
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/sysctl.h>
51 #include <sys/thread.h>
52 #include <sys/mutex.h>
54 #include <machine/cpufunc.h>
56 #include <sys/thread2.h>
57 #include <sys/mutex2.h>
59 static __int64_t mtx_contention_count;
60 static __int64_t mtx_collision_count;
61 static __int64_t mtx_wakeup_count;
63 SYSCTL_QUAD(_kern, OID_AUTO, mtx_contention_count, CTLFLAG_RW,
64 &mtx_contention_count, 0, "");
65 SYSCTL_QUAD(_kern, OID_AUTO, mtx_collision_count, CTLFLAG_RW,
66 &mtx_collision_count, 0, "");
67 SYSCTL_QUAD(_kern, OID_AUTO, mtx_wakeup_count, CTLFLAG_RW,
68 &mtx_wakeup_count, 0, "");
71 * Exclusive-lock a mutex, block until acquired. Recursion is allowed.
73 * Returns 0 on success, or the tsleep() return code on failure.
74 * An error can only be returned if PCATCH is specified in the flags.
77 __mtx_lock_ex(mtx_t mtx, const char *ident, int flags, int to)
86 nlock = MTX_EXCLUSIVE | 1;
87 if (atomic_cmpset_int(&mtx->mtx_lock, 0, nlock)) {
88 mtx->mtx_owner = curthread;
92 } else if ((lock & MTX_EXCLUSIVE) &&
93 mtx->mtx_owner == curthread) {
94 KKASSERT((lock & MTX_MASK) != MTX_MASK);
96 if (atomic_cmpset_int(&mtx->mtx_lock, 0, nlock)) {
101 nlock = lock | MTX_EXWANTED;
102 tsleep_interlock(&mtx->mtx_owner, 0);
103 if (atomic_cmpset_int(&mtx->mtx_lock, 0, nlock)) {
104 error = tsleep(&mtx->mtx_owner, flags,
106 ++mtx_contention_count;
109 wakeup_one(&mtx->mtx_owner);
113 tsleep_remove(curthread);
116 ++mtx_collision_count;
122 _mtx_lock_ex(mtx_t mtx, const char *ident, int flags, int to)
124 return(__mtx_lock_ex(mtx, ident, flags, to));
128 _mtx_lock_ex_quick(mtx_t mtx, const char *ident)
130 return(__mtx_lock_ex(mtx, ident, 0, 0));
134 * Share-lock a mutex, block until acquired. Recursion is allowed.
136 * Returns 0 on success, or the tsleep() return code on failure.
137 * An error can only be returned if PCATCH is specified in the flags.
140 __mtx_lock_sh(mtx_t mtx, const char *ident, int flags, int to)
147 lock = mtx->mtx_lock;
148 if ((lock & MTX_EXCLUSIVE) == 0) {
149 KKASSERT((lock & MTX_MASK) != MTX_MASK);
151 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock)) {
156 nlock = lock | MTX_SHWANTED;
157 tsleep_interlock(mtx, 0);
158 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock)) {
159 error = tsleep(mtx, flags, ident, to);
162 ++mtx_contention_count;
164 tsleep_remove(curthread);
167 ++mtx_collision_count;
173 _mtx_lock_sh(mtx_t mtx, const char *ident, int flags, int to)
175 return (__mtx_lock_sh(mtx, ident, flags, to));
179 _mtx_lock_sh_quick(mtx_t mtx, const char *ident)
181 return (__mtx_lock_sh(mtx, ident, 0, 0));
185 _mtx_spinlock_ex(mtx_t mtx)
193 lock = mtx->mtx_lock;
195 nlock = MTX_EXCLUSIVE | 1;
196 if (atomic_cmpset_int(&mtx->mtx_lock, 0, nlock)) {
197 /* mtx_owner set by caller */
200 } else if ((lock & MTX_EXCLUSIVE) &&
201 mtx->mtx_owner == curthread) {
202 KKASSERT((lock & MTX_MASK) != MTX_MASK);
204 if (atomic_cmpset_int(&mtx->mtx_lock, 0, nlock))
211 for (bo = 0; bo < bb; ++bo)
213 ++mtx_contention_count;
215 ++mtx_collision_count;
220 _mtx_spinlock_sh(mtx_t mtx)
228 lock = mtx->mtx_lock;
229 if ((lock & MTX_EXCLUSIVE) == 0) {
230 KKASSERT((lock & MTX_MASK) != MTX_MASK);
232 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock))
239 for (bo = 0; bo < bb; ++bo)
241 ++mtx_contention_count;
243 ++mtx_collision_count;
248 _mtx_lock_ex_try(mtx_t mtx)
255 lock = mtx->mtx_lock;
257 nlock = MTX_EXCLUSIVE | 1;
258 if (atomic_cmpset_int(&mtx->mtx_lock, 0, nlock)) {
259 /* mtx_owner set by caller */
262 } else if ((lock & MTX_EXCLUSIVE) &&
263 mtx->mtx_owner == curthread) {
264 KKASSERT((lock & MTX_MASK) != MTX_MASK);
266 if (atomic_cmpset_int(&mtx->mtx_lock, 0, nlock))
272 ++mtx_collision_count;
278 _mtx_lock_sh_try(mtx_t mtx)
285 lock = mtx->mtx_lock;
286 if ((lock & MTX_EXCLUSIVE) == 0) {
287 KKASSERT((lock & MTX_MASK) != MTX_MASK);
289 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock))
295 ++mtx_collision_count;
301 * If the lock is held exclusively it must be owned by the caller. If the
302 * lock is already a shared lock this operation is a NOP. A panic will
303 * occur if the lock is not held either shared or exclusive.
305 * The exclusive count is converted to a shared count.
308 _mtx_downgrade(mtx_t mtx)
314 lock = mtx->mtx_lock;
315 if ((lock & MTX_EXCLUSIVE) == 0) {
316 KKASSERT((lock & MTX_MASK) > 0);
319 KKASSERT(mtx->mtx_owner == curthread);
320 nlock = lock & ~(MTX_EXCLUSIVE | MTX_SHWANTED);
321 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock)) {
322 if (lock & MTX_SHWANTED) {
328 ++mtx_collision_count;
333 * Upgrade a shared lock to an exclusive lock. The upgrade will fail if
334 * the shared lock has a count other then 1. Optimize the most likely case
335 * but note that a single cmpset can fail due to WANTED races.
337 * If the lock is held exclusively it must be owned by the caller and
338 * this function will simply return without doing anything. A panic will
339 * occur if the lock is held exclusively by someone other then the caller.
341 * Returns 0 on success, EDEADLK on failure.
344 _mtx_upgrade_try(mtx_t mtx)
351 lock = mtx->mtx_lock;
353 if ((lock & ~MTX_EXWANTED) == 1) {
354 nlock = lock | MTX_EXCLUSIVE;
355 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock)) {
356 mtx->mtx_owner = curthread;
359 } else if (lock & MTX_EXCLUSIVE) {
360 KKASSERT(mtx->mtx_owner == curthread);
366 ++mtx_collision_count;
372 * Unlock a lock. The caller must hold the lock either shared or exclusive.
375 _mtx_unlock(mtx_t mtx)
381 lock = mtx->mtx_lock;
382 nlock = (lock & (MTX_EXCLUSIVE | MTX_MASK)) - 1;
384 if (atomic_cmpset_int(&mtx->mtx_lock, lock, 0)) {
385 if (lock & MTX_SHWANTED) {
389 if (lock & MTX_EXWANTED) {
390 wakeup_one(&mtx->mtx_owner);
394 } else if (nlock == MTX_EXCLUSIVE) {
395 mtx->mtx_owner = NULL;
396 if (atomic_cmpset_int(&mtx->mtx_lock, lock, 0)) {
397 if (lock & MTX_SHWANTED) {
401 if (lock & MTX_EXWANTED) {
402 wakeup_one(&mtx->mtx_owner);
409 KKASSERT((nlock & MTX_MASK) != MTX_MASK);
410 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock))
413 ++mtx_collision_count;