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.
74 _mtx_lock_ex(mtx_t mtx, const char *ident, int flags)
82 nlock = MTX_EXCLUSIVE | 1;
83 if (atomic_cmpset_int(&mtx->mtx_lock, 0, nlock)) {
84 /* mtx_owner set by caller */
87 } else if ((lock & MTX_EXCLUSIVE) &&
88 mtx->mtx_owner == curthread) {
89 KKASSERT((lock & MTX_MASK) != MTX_MASK);
91 if (atomic_cmpset_int(&mtx->mtx_lock, 0, nlock))
94 nlock = lock | MTX_EXWANTED;
95 tsleep_interlock(mtx, 0);
96 if (atomic_cmpset_int(&mtx->mtx_lock, 0, nlock)) {
97 ++mtx_contention_count;
98 tsleep(mtx, flags, ident, 0);
101 ++mtx_collision_count;
106 * Share-lock a mutex, block until acquired. Recursion is allowed.
109 _mtx_lock_sh(mtx_t mtx, const char *ident, int flags)
115 lock = mtx->mtx_lock;
116 if ((lock & MTX_EXCLUSIVE) == 0) {
117 KKASSERT((lock & MTX_MASK) != MTX_MASK);
119 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock))
122 nlock = lock | MTX_SHWANTED;
123 tsleep_interlock(mtx, 0);
124 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock)) {
125 ++mtx_contention_count;
126 tsleep(mtx, flags, ident, 0);
129 ++mtx_collision_count;
134 _mtx_spinlock_ex(mtx_t mtx)
142 lock = mtx->mtx_lock;
144 nlock = MTX_EXCLUSIVE | 1;
145 if (atomic_cmpset_int(&mtx->mtx_lock, 0, nlock)) {
146 /* mtx_owner set by caller */
149 } else if ((lock & MTX_EXCLUSIVE) &&
150 mtx->mtx_owner == curthread) {
151 KKASSERT((lock & MTX_MASK) != MTX_MASK);
153 if (atomic_cmpset_int(&mtx->mtx_lock, 0, nlock))
160 for (bo = 0; bo < bb; ++bo)
162 ++mtx_contention_count;
164 ++mtx_collision_count;
169 _mtx_spinlock_sh(mtx_t mtx)
177 lock = mtx->mtx_lock;
178 if ((lock & MTX_EXCLUSIVE) == 0) {
179 KKASSERT((lock & MTX_MASK) != MTX_MASK);
181 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock))
188 for (bo = 0; bo < bb; ++bo)
190 ++mtx_contention_count;
192 ++mtx_collision_count;
197 _mtx_lock_ex_try(mtx_t mtx)
204 lock = mtx->mtx_lock;
206 nlock = MTX_EXCLUSIVE | 1;
207 if (atomic_cmpset_int(&mtx->mtx_lock, 0, nlock)) {
208 /* mtx_owner set by caller */
211 } else if ((lock & MTX_EXCLUSIVE) &&
212 mtx->mtx_owner == curthread) {
213 KKASSERT((lock & MTX_MASK) != MTX_MASK);
215 if (atomic_cmpset_int(&mtx->mtx_lock, 0, nlock))
221 ++mtx_collision_count;
227 _mtx_lock_sh_try(mtx_t mtx)
234 lock = mtx->mtx_lock;
235 if ((lock & MTX_EXCLUSIVE) == 0) {
236 KKASSERT((lock & MTX_MASK) != MTX_MASK);
238 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock))
244 ++mtx_collision_count;
250 * If the lock is held exclusively it must be owned by the caller. If the
251 * lock is already a shared lock this operation is a NOP. A panic will
252 * occur if the lock is not held either shared or exclusive.
254 * The exclusive count is converted to a shared count.
257 _mtx_downgrade(mtx_t mtx)
263 lock = mtx->mtx_lock;
264 if ((lock & MTX_EXCLUSIVE) == 0) {
265 KKASSERT((lock & MTX_MASK) > 0);
268 KKASSERT(mtx->mtx_owner == curthread);
269 nlock = lock & ~(MTX_EXCLUSIVE | MTX_SHWANTED);
270 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock)) {
271 if (lock & MTX_SHWANTED) {
277 ++mtx_collision_count;
282 * Upgrade a shared lock to an exclusive lock. The upgrade will fail if
283 * the shared lock has a count other then 1. Optimize the most likely case
284 * but note that a single cmpset can fail due to WANTED races.
286 * If the lock is held exclusively it must be owned by the caller and
287 * this function will simply return without doing anything. A panic will
288 * occur if the lock is held exclusively by someone other then the caller.
290 * Returns 0 on success, EDEADLK on failure.
293 _mtx_upgrade_try(mtx_t mtx)
300 lock = mtx->mtx_lock;
302 if ((lock & ~MTX_EXWANTED) == 1) {
303 nlock = lock | MTX_EXCLUSIVE;
304 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock)) {
305 mtx->mtx_owner = curthread;
308 } else if (lock & MTX_EXCLUSIVE) {
309 KKASSERT(mtx->mtx_owner == curthread);
315 ++mtx_collision_count;
321 * Unlock a lock. The caller must hold the lock either shared or exclusive.
324 _mtx_unlock(mtx_t mtx)
330 lock = mtx->mtx_lock;
331 nlock = (lock & (MTX_EXCLUSIVE | MTX_MASK)) - 1;
333 if (atomic_cmpset_int(&mtx->mtx_lock, lock, 0)) {
334 if (lock & (MTX_SHWANTED | MTX_EXWANTED)) {
339 } else if (nlock == MTX_EXCLUSIVE) {
340 mtx->mtx_owner = NULL;
341 if (atomic_cmpset_int(&mtx->mtx_lock, lock, 0)) {
342 if (lock & (MTX_SHWANTED | MTX_EXWANTED)) {
350 KKASSERT((nlock & MTX_MASK) != MTX_MASK);
351 if (atomic_cmpset_int(&mtx->mtx_lock, lock, nlock))
354 ++mtx_collision_count;