2 * Copyright (C) 2001 Jason Evans <jasone@freebsd.org>. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice(s), this list of conditions and the following disclaimer as
9 * the first lines of this file unmodified other than the possible
10 * addition of one or more copyright notices.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice(s), this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29 * Shared/exclusive locks. This implementation assures deterministic lock
30 * granting behavior, so that slocks and xlocks are interleaved.
32 * Priority propagation will not generally raise the priority of lock holders,
33 * so should not be relied upon in combination with sx locks.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/linker_set.h>
45 #include <sys/condvar.h>
47 #include <sys/mutex.h>
54 static void db_show_sx(struct lock_object *lock);
57 struct lock_class lock_class_sx = {
59 LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
66 #define _sx_assert(sx, what, file, line)
72 struct sx_args *sargs = arg;
74 sx_init(sargs->sa_sx, sargs->sa_desc);
78 sx_init(struct sx *sx, const char *description)
80 struct lock_object *lock;
82 lock = &sx->sx_object;
83 KASSERT((lock->lo_flags & LO_INITIALIZED) == 0,
84 ("sx lock %s %p already initialized", description, sx));
85 bzero(sx, sizeof(*sx));
86 lock->lo_flags = LOCK_CLASS_SX << LO_CLASSSHIFT;
87 lock->lo_type = lock->lo_name = description;
88 lock->lo_flags |= LO_WITNESS | LO_RECURSABLE | LO_SLEEPABLE |
90 sx->sx_lock = mtx_pool_find(mtxpool_lockbuilder, sx);
92 cv_init(&sx->sx_shrd_cv, description);
94 cv_init(&sx->sx_excl_cv, description);
96 sx->sx_xholder = NULL;
98 LOCK_LOG_INIT(lock, 0);
104 sx_destroy(struct sx *sx)
107 LOCK_LOG_DESTROY(&sx->sx_object, 0);
109 KASSERT((sx->sx_cnt == 0 && sx->sx_shrd_wcnt == 0 && sx->sx_excl_wcnt ==
110 0), ("%s (%s): holders or waiters\n", __func__,
111 sx->sx_object.lo_name));
114 cv_destroy(&sx->sx_shrd_cv);
115 cv_destroy(&sx->sx_excl_cv);
117 WITNESS_DESTROY(&sx->sx_object);
121 _sx_slock(struct sx *sx, const char *file, int line)
124 mtx_lock(sx->sx_lock);
125 KASSERT(sx->sx_xholder != curthread,
126 ("%s (%s): slock while xlock is held @ %s:%d\n", __func__,
127 sx->sx_object.lo_name, file, line));
128 WITNESS_CHECKORDER(&sx->sx_object, LOP_NEWORDER, file, line);
131 * Loop in case we lose the race for lock acquisition.
133 while (sx->sx_cnt < 0) {
135 cv_wait(&sx->sx_shrd_cv, sx->sx_lock);
139 /* Acquire a shared lock. */
142 LOCK_LOG_LOCK("SLOCK", &sx->sx_object, 0, 0, file, line);
143 WITNESS_LOCK(&sx->sx_object, 0, file, line);
145 mtx_unlock(sx->sx_lock);
149 _sx_try_slock(struct sx *sx, const char *file, int line)
152 mtx_lock(sx->sx_lock);
153 if (sx->sx_cnt >= 0) {
155 LOCK_LOG_TRY("SLOCK", &sx->sx_object, 0, 1, file, line);
156 WITNESS_LOCK(&sx->sx_object, LOP_TRYLOCK, file, line);
157 mtx_unlock(sx->sx_lock);
160 LOCK_LOG_TRY("SLOCK", &sx->sx_object, 0, 0, file, line);
161 mtx_unlock(sx->sx_lock);
167 _sx_xlock(struct sx *sx, const char *file, int line)
170 mtx_lock(sx->sx_lock);
173 * With sx locks, we're absolutely not permitted to recurse on
174 * xlocks, as it is fatal (deadlock). Normally, recursion is handled
175 * by WITNESS, but as it is not semantically correct to hold the
176 * xlock while in here, we consider it API abuse and put it under
179 KASSERT(sx->sx_xholder != curthread,
180 ("%s (%s): xlock already held @ %s:%d", __func__,
181 sx->sx_object.lo_name, file, line));
182 WITNESS_CHECKORDER(&sx->sx_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
185 /* Loop in case we lose the race for lock acquisition. */
186 while (sx->sx_cnt != 0) {
188 cv_wait(&sx->sx_excl_cv, sx->sx_lock);
192 MPASS(sx->sx_cnt == 0);
194 /* Acquire an exclusive lock. */
196 sx->sx_xholder = curthread;
198 LOCK_LOG_LOCK("XLOCK", &sx->sx_object, 0, 0, file, line);
199 WITNESS_LOCK(&sx->sx_object, LOP_EXCLUSIVE, file, line);
201 mtx_unlock(sx->sx_lock);
205 _sx_try_xlock(struct sx *sx, const char *file, int line)
208 mtx_lock(sx->sx_lock);
209 if (sx->sx_cnt == 0) {
211 sx->sx_xholder = curthread;
212 LOCK_LOG_TRY("XLOCK", &sx->sx_object, 0, 1, file, line);
213 WITNESS_LOCK(&sx->sx_object, LOP_EXCLUSIVE | LOP_TRYLOCK, file,
215 mtx_unlock(sx->sx_lock);
218 LOCK_LOG_TRY("XLOCK", &sx->sx_object, 0, 0, file, line);
219 mtx_unlock(sx->sx_lock);
225 _sx_sunlock(struct sx *sx, const char *file, int line)
228 _sx_assert(sx, SX_SLOCKED, file, line);
229 mtx_lock(sx->sx_lock);
231 WITNESS_UNLOCK(&sx->sx_object, 0, file, line);
237 * If we just released the last shared lock, wake any waiters up, giving
238 * exclusive lockers precedence. In order to make sure that exclusive
239 * lockers won't be blocked forever, don't wake shared lock waiters if
240 * there are exclusive lock waiters.
242 if (sx->sx_excl_wcnt > 0) {
244 cv_signal(&sx->sx_excl_cv);
245 } else if (sx->sx_shrd_wcnt > 0)
246 cv_broadcast(&sx->sx_shrd_cv);
248 LOCK_LOG_LOCK("SUNLOCK", &sx->sx_object, 0, 0, file, line);
250 mtx_unlock(sx->sx_lock);
254 _sx_xunlock(struct sx *sx, const char *file, int line)
257 _sx_assert(sx, SX_XLOCKED, file, line);
258 mtx_lock(sx->sx_lock);
259 MPASS(sx->sx_cnt == -1);
261 WITNESS_UNLOCK(&sx->sx_object, LOP_EXCLUSIVE, file, line);
265 sx->sx_xholder = NULL;
268 * Wake up waiters if there are any. Give precedence to slock waiters.
270 if (sx->sx_shrd_wcnt > 0)
271 cv_broadcast(&sx->sx_shrd_cv);
272 else if (sx->sx_excl_wcnt > 0)
273 cv_signal(&sx->sx_excl_cv);
275 LOCK_LOG_LOCK("XUNLOCK", &sx->sx_object, 0, 0, file, line);
277 mtx_unlock(sx->sx_lock);
281 _sx_try_upgrade(struct sx *sx, const char *file, int line)
284 _sx_assert(sx, SX_SLOCKED, file, line);
285 mtx_lock(sx->sx_lock);
287 if (sx->sx_cnt == 1) {
289 sx->sx_xholder = curthread;
291 LOCK_LOG_TRY("XUPGRADE", &sx->sx_object, 0, 1, file, line);
292 WITNESS_UPGRADE(&sx->sx_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
295 mtx_unlock(sx->sx_lock);
298 LOCK_LOG_TRY("XUPGRADE", &sx->sx_object, 0, 0, file, line);
299 mtx_unlock(sx->sx_lock);
305 _sx_downgrade(struct sx *sx, const char *file, int line)
308 _sx_assert(sx, SX_XLOCKED, file, line);
309 mtx_lock(sx->sx_lock);
310 MPASS(sx->sx_cnt == -1);
312 WITNESS_DOWNGRADE(&sx->sx_object, 0, file, line);
315 sx->sx_xholder = NULL;
316 if (sx->sx_shrd_wcnt > 0)
317 cv_broadcast(&sx->sx_shrd_cv);
319 LOCK_LOG_LOCK("XDOWNGRADE", &sx->sx_object, 0, 0, file, line);
321 mtx_unlock(sx->sx_lock);
324 #ifdef INVARIANT_SUPPORT
330 * In the non-WITNESS case, sx_assert() can only detect that at least
331 * *some* thread owns an slock, but it cannot guarantee that *this*
332 * thread owns an slock.
335 _sx_assert(struct sx *sx, int what, const char *file, int line)
338 if (panicstr != NULL)
344 witness_assert(&sx->sx_object, what, file, line);
346 mtx_lock(sx->sx_lock);
347 if (sx->sx_cnt <= 0 &&
348 (what == SX_SLOCKED || sx->sx_xholder != curthread))
349 panic("Lock %s not %slocked @ %s:%d\n",
350 sx->sx_object.lo_name, (what == SX_SLOCKED) ?
351 "share " : "", file, line);
352 mtx_unlock(sx->sx_lock);
356 mtx_lock(sx->sx_lock);
357 if (sx->sx_xholder != curthread)
358 panic("Lock %s not exclusively locked @ %s:%d\n",
359 sx->sx_object.lo_name, file, line);
360 mtx_unlock(sx->sx_lock);
364 witness_assert(&sx->sx_object, what, file, line);
367 * We are able to check only exclusive lock here,
368 * we cannot assert that *this* thread owns slock.
370 mtx_lock(sx->sx_lock);
371 if (sx->sx_xholder == curthread)
372 panic("Lock %s exclusively locked @ %s:%d\n",
373 sx->sx_object.lo_name, file, line);
374 mtx_unlock(sx->sx_lock);
378 panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
382 #endif /* INVARIANT_SUPPORT */
386 db_show_sx(struct lock_object *lock)
391 sx = (struct sx *)lock;
393 db_printf(" state: ");
394 if (sx->sx_cnt < 0) {
396 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
397 td->td_tid, td->td_proc->p_pid, td->td_proc->p_comm);
398 } else if (sx->sx_cnt > 0)
399 db_printf("SLOCK: %d locks\n", sx->sx_cnt);
401 db_printf("UNLOCKED\n");
402 db_printf(" waiters: %d shared, %d exclusive\n", sx->sx_shrd_wcnt,