kern_rwlock.c revision 155162
1154941Sjhb/*-
2154941Sjhb * Copyright (c) 2006 John Baldwin <jhb@FreeBSD.org>
3154941Sjhb * All rights reserved.
4154941Sjhb *
5154941Sjhb * Redistribution and use in source and binary forms, with or without
6154941Sjhb * modification, are permitted provided that the following conditions
7154941Sjhb * are met:
8154941Sjhb * 1. Redistributions of source code must retain the above copyright
9154941Sjhb *    notice, this list of conditions and the following disclaimer.
10154941Sjhb * 2. Redistributions in binary form must reproduce the above copyright
11154941Sjhb *    notice, this list of conditions and the following disclaimer in the
12154941Sjhb *    documentation and/or other materials provided with the distribution.
13154941Sjhb * 3. Neither the name of the author nor the names of any co-contributors
14154941Sjhb *    may be used to endorse or promote products derived from this software
15154941Sjhb *    without specific prior written permission.
16154941Sjhb *
17154941Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18154941Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19154941Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20154941Sjhb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21154941Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22154941Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23154941Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24154941Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25154941Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26154941Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27154941Sjhb * SUCH DAMAGE.
28154941Sjhb */
29154941Sjhb
30154941Sjhb/*
31154941Sjhb * Machine independent bits of reader/writer lock implementation.
32154941Sjhb */
33154941Sjhb
34154941Sjhb#include <sys/cdefs.h>
35154941Sjhb__FBSDID("$FreeBSD: head/sys/kern/kern_rwlock.c 155162 2006-02-01 04:18:07Z scottl $");
36154941Sjhb
37154941Sjhb#include "opt_ddb.h"
38167801Sjhb
39154941Sjhb#include <sys/param.h>
40154941Sjhb#include <sys/ktr.h>
41154941Sjhb#include <sys/lock.h>
42154941Sjhb#include <sys/mutex.h>
43154941Sjhb#include <sys/proc.h>
44154941Sjhb#include <sys/rwlock.h>
45154941Sjhb#include <sys/systm.h>
46154941Sjhb#include <sys/turnstile.h>
47154941Sjhb
48164159Skmacy#include <machine/cpu.h>
49154941Sjhb
50154941Sjhb#ifdef DDB
51167801Sjhb#include <ddb/ddb.h>
52167801Sjhb
53167801Sjhbstatic void	db_show_rwlock(struct lock_object *lock);
54167801Sjhb#endif
55154941Sjhb
56154941Sjhbstruct lock_class lock_class_rw = {
57154941Sjhb	"rw",
58154941Sjhb	LC_SLEEPLOCK | LC_RECURSABLE /* | LC_UPGRADABLE */,
59154941Sjhb#ifdef DDB
60167368Sjhb	db_show_rwlock
61167368Sjhb#endif
62154941Sjhb};
63154941Sjhb
64167365Sjhb#define	rw_owner(rw)							\
65167365Sjhb	((rw)->rw_lock & RW_LOCK_READ ? NULL :				\
66154941Sjhb	    (struct thread *)RW_OWNER((rw)->rw_lock))
67167365Sjhb
68154941Sjhb#ifndef INVARIANTS
69167368Sjhb#define	_rw_assert(rw, what, file, line)
70167368Sjhb#endif
71154941Sjhb
72154941Sjhbvoid
73157826Sjhbrw_init(struct rwlock *rw, const char *name)
74157826Sjhb{
75157826Sjhb
76157826Sjhb	rw->rw_lock = RW_UNLOCKED;
77157826Sjhb
78154941Sjhb	lock_init(&rw->rw_object, &lock_class_rw, name, NULL, LO_WITNESS |
79154941Sjhb	    LO_RECURSABLE /* | LO_UPGRADABLE */);
80154941Sjhb}
81157826Sjhb
82157826Sjhbvoid
83157826Sjhbrw_destroy(struct rwlock *rw)
84157826Sjhb{
85157826Sjhb
86157826Sjhb	KASSERT(rw->rw_lock == RW_UNLOCKED, ("rw lock not unlocked"));
87157826Sjhb	lock_destroy(&rw->rw_object);
88154941Sjhb}
89154941Sjhb
90154941Sjhbvoid
91154941Sjhbrw_sysinit(void *arg)
92154941Sjhb{
93167368Sjhb	struct rw_args *args = arg;
94167368Sjhb
95167368Sjhb	rw_init(args->ra_rw, args->ra_desc);
96167368Sjhb}
97167368Sjhb
98167368Sjhbvoid
99167368Sjhb_rw_wlock(struct rwlock *rw, const char *file, int line)
100167368Sjhb{
101167368Sjhb
102167368Sjhb	MPASS(curthread != NULL);
103167368Sjhb	KASSERT(rw_owner(rw) != curthread,
104167368Sjhb	    ("%s (%s): wlock already held @ %s:%d", __func__,
105167368Sjhb	    rw->rw_object.lo_name, file, line));
106167368Sjhb	WITNESS_CHECKORDER(&rw->rw_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
107167368Sjhb	    line);
108167368Sjhb	__rw_wlock(rw, curthread, file, line);
109167368Sjhb	LOCK_LOG_LOCK("WLOCK", &rw->rw_object, 0, 0, file, line);
110167368Sjhb	WITNESS_LOCK(&rw->rw_object, LOP_EXCLUSIVE, file, line);
111167368Sjhb}
112167368Sjhb
113167368Sjhbvoid
114167368Sjhb_rw_wunlock(struct rwlock *rw, const char *file, int line)
115167368Sjhb{
116167368Sjhb
117167368Sjhb	MPASS(curthread != NULL);
118167368Sjhb	_rw_assert(rw, RA_WLOCKED, file, line);
119167368Sjhb	WITNESS_UNLOCK(&rw->rw_object, LOP_EXCLUSIVE, file, line);
120167368Sjhb	LOCK_LOG_LOCK("WUNLOCK", &rw->rw_object, 0, 0, file, line);
121154941Sjhb	__rw_wunlock(rw, curthread, file, line);
122154941Sjhb}
123154941Sjhb
124154941Sjhbvoid
125154941Sjhb_rw_rlock(struct rwlock *rw, const char *file, int line)
126167787Sjhb{
127167787Sjhb	uintptr_t x;
128157882Sjhb
129154941Sjhb	KASSERT(rw_owner(rw) != curthread,
130154941Sjhb	    ("%s (%s): wlock already held @ %s:%d", __func__,
131154941Sjhb	    rw->rw_object.lo_name, file, line));
132154941Sjhb	WITNESS_CHECKORDER(&rw->rw_object, LOP_NEWORDER, file, line);
133154941Sjhb
134154941Sjhb	/*
135154941Sjhb	 * Note that we don't make any attempt to try to block read
136169394Sjhb	 * locks once a writer has blocked on the lock.  The reason is
137167787Sjhb	 * that we currently allow for read locks to recurse and we
138167787Sjhb	 * don't keep track of all the holders of read locks.  Thus, if
139154941Sjhb	 * we were to block readers once a writer blocked and a reader
140154941Sjhb	 * tried to recurse on their reader lock after a writer had
141154941Sjhb	 * blocked we would end up in a deadlock since the reader would
142154941Sjhb	 * be blocked on the writer, and the writer would be blocked
143154941Sjhb	 * waiting for the reader to release its original read lock.
144154941Sjhb	 */
145154941Sjhb	for (;;) {
146154941Sjhb		/*
147154941Sjhb		 * Handle the easy case.  If no other thread has a write
148154941Sjhb		 * lock, then try to bump up the count of read locks.  Note
149167024Srwatson		 * that we have to preserve the current state of the
150167024Srwatson		 * RW_LOCK_WRITE_WAITERS flag.  If we fail to acquire a
151167024Srwatson		 * read lock, then rw_lock must have changed, so restart
152167024Srwatson		 * the loop.  Note that this handles the case of a
153167024Srwatson		 * completely unlocked rwlock since such a lock is encoded
154167024Srwatson		 * as a read lock with no waiters.
155167024Srwatson		 */
156154941Sjhb		x = rw->rw_lock;
157154941Sjhb		if (x & RW_LOCK_READ) {
158154941Sjhb
159154941Sjhb			/*
160154941Sjhb			 * The RW_LOCK_READ_WAITERS flag should only be set
161169394Sjhb			 * if another thread currently holds a write lock,
162169394Sjhb			 * and in that case RW_LOCK_READ should be clear.
163157826Sjhb			 */
164154941Sjhb			MPASS((x & RW_LOCK_READ_WAITERS) == 0);
165167787Sjhb			if (atomic_cmpset_acq_ptr(&rw->rw_lock, x,
166167787Sjhb			    x + RW_ONE_READER)) {
167154941Sjhb				if (LOCK_LOG_TEST(&rw->rw_object, 0))
168154941Sjhb					CTR4(KTR_LOCK,
169167787Sjhb					    "%s: %p succeed %p -> %p", __func__,
170167787Sjhb					    rw, (void *)x,
171160771Sjhb					    (void *)(x + RW_ONE_READER));
172154941Sjhb				break;
173154941Sjhb			}
174154941Sjhb			continue;
175154941Sjhb		}
176154941Sjhb
177154941Sjhb		/*
178154941Sjhb		 * Okay, now it's the hard case.  Some other thread already
179169394Sjhb		 * has a write lock, so acquire the turnstile lock so we can
180169394Sjhb		 * begin the process of blocking.
181154941Sjhb		 */
182160771Sjhb		turnstile_lock(&rw->rw_object);
183167787Sjhb
184167787Sjhb		/*
185167787Sjhb		 * The lock might have been released while we spun, so
186154941Sjhb		 * recheck its state and restart the loop if there is no
187154941Sjhb		 * longer a write lock.
188154941Sjhb		 */
189154941Sjhb		x = rw->rw_lock;
190154941Sjhb		if (x & RW_LOCK_READ) {
191154941Sjhb			turnstile_release(&rw->rw_object);
192167801Sjhb			continue;
193157846Sjhb		}
194157851Swkoszek
195167307Sjhb		/*
196167054Skmacy		 * Ok, it's still a write lock.  If the RW_LOCK_READ_WAITERS
197154941Sjhb		 * flag is already set, then we can go ahead and block.  If
198154941Sjhb		 * it is not set then try to set it.  If we fail to set it
199169394Sjhb		 * drop the turnstile lock and restart the loop.
200169394Sjhb		 */
201157826Sjhb		if (!(x & RW_LOCK_READ_WAITERS) &&
202154941Sjhb		    !atomic_cmpset_ptr(&rw->rw_lock, x,
203167787Sjhb		    x | RW_LOCK_READ_WAITERS)) {
204167787Sjhb			turnstile_release(&rw->rw_object);
205154941Sjhb			continue;
206154941Sjhb		}
207154941Sjhb		if (!(x & RW_LOCK_READ_WAITERS) &&
208154941Sjhb		    LOCK_LOG_TEST(&rw->rw_object, 0))
209154941Sjhb			CTR2(KTR_LOCK, "%s: %p set read waiters flag", __func__,
210154941Sjhb				rw);
211154941Sjhb
212154941Sjhb		/*
213154941Sjhb		 * We were unable to acquire the lock and the read waiters
214154941Sjhb		 * flag is set, so we must block on the turnstile.
215154941Sjhb		 */
216154941Sjhb		if (LOCK_LOG_TEST(&rw->rw_object, 0))
217154941Sjhb			CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
218154941Sjhb			    rw);
219154941Sjhb		turnstile_wait(&rw->rw_object, rw_owner(rw), TS_SHARED_QUEUE);
220154941Sjhb		if (LOCK_LOG_TEST(&rw->rw_object, 0))
221154941Sjhb			CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
222154941Sjhb			    __func__, rw);
223154941Sjhb	}
224154941Sjhb
225154941Sjhb	/*
226154941Sjhb	 * TODO: acquire "owner of record" here.  Here be turnstile dragons
227154941Sjhb	 * however.  turnstiles don't like owners changing between calls to
228154941Sjhb	 * turnstile_wait() currently.
229154941Sjhb	 */
230154941Sjhb
231154941Sjhb	LOCK_LOG_LOCK("RLOCK", &rw->rw_object, 0, 0, file, line);
232154941Sjhb	WITNESS_LOCK(&rw->rw_object, 0, file, line);
233154941Sjhb}
234154941Sjhb
235154941Sjhbvoid
236154941Sjhb_rw_runlock(struct rwlock *rw, const char *file, int line)
237154941Sjhb{
238154941Sjhb	struct turnstile *ts;
239167787Sjhb	uintptr_t x;
240154941Sjhb
241154941Sjhb	_rw_assert(rw, RA_RLOCKED, file, line);
242154941Sjhb	WITNESS_UNLOCK(&rw->rw_object, 0, file, line);
243154941Sjhb	LOCK_LOG_LOCK("RUNLOCK", &rw->rw_object, 0, 0, file, line);
244167307Sjhb
245167307Sjhb	/* TODO: drop "owner of record" here. */
246167787Sjhb
247167307Sjhb	for (;;) {
248154941Sjhb		/*
249154941Sjhb		 * See if there is more than one read lock held.  If so,
250157846Sjhb		 * just drop one and return.
251154941Sjhb		 */
252154941Sjhb		x = rw->rw_lock;
253167787Sjhb		if (RW_READERS(x) > 1) {
254167307Sjhb			if (atomic_cmpset_ptr(&rw->rw_lock, x,
255154941Sjhb			    x - RW_ONE_READER)) {
256154941Sjhb				if (LOCK_LOG_TEST(&rw->rw_object, 0))
257154941Sjhb					CTR4(KTR_LOCK,
258154941Sjhb					    "%s: %p succeeded %p -> %p",
259154941Sjhb					    __func__, rw, (void *)x,
260154941Sjhb					    (void *)(x - RW_ONE_READER));
261167787Sjhb				break;
262154941Sjhb			}
263154941Sjhb			continue;
264154941Sjhb		}
265154941Sjhb
266154941Sjhb		/*
267154941Sjhb		 * We should never have read waiters while at least one
268154941Sjhb		 * thread holds a read lock.  (See note above)
269154941Sjhb		 */
270167787Sjhb		KASSERT(!(x & RW_LOCK_READ_WAITERS),
271157846Sjhb		    ("%s: waiting readers", __func__));
272154941Sjhb
273154941Sjhb		/*
274154941Sjhb		 * If there aren't any waiters for a write lock, then try
275154941Sjhb		 * to drop it quickly.
276154941Sjhb		 */
277154941Sjhb		if (!(x & RW_LOCK_WRITE_WAITERS)) {
278154941Sjhb
279154941Sjhb			/*
280154941Sjhb			 * There shouldn't be any flags set and we should
281157826Sjhb			 * be the only read lock.  If we fail to release
282157826Sjhb			 * the single read lock, then another thread might
283157826Sjhb			 * have just acquired a read lock, so go back up
284167787Sjhb			 * to the multiple read locks case.
285157826Sjhb			 */
286157826Sjhb			MPASS(x == RW_READERS_LOCK(1));
287157826Sjhb			if (atomic_cmpset_ptr(&rw->rw_lock, RW_READERS_LOCK(1),
288167787Sjhb			    RW_UNLOCKED)) {
289157826Sjhb				if (LOCK_LOG_TEST(&rw->rw_object, 0))
290157826Sjhb					CTR2(KTR_LOCK, "%s: %p last succeeded",
291154941Sjhb					    __func__, rw);
292154941Sjhb				break;
293167801Sjhb			}
294154941Sjhb			continue;
295157846Sjhb		}
296157846Sjhb
297157846Sjhb		/*
298157846Sjhb		 * There should just be one reader with one or more
299157846Sjhb		 * writers waiting.
300157846Sjhb		 */
301167787Sjhb		MPASS(x == (RW_READERS_LOCK(1) | RW_LOCK_WRITE_WAITERS));
302167787Sjhb
303157846Sjhb		/*
304157846Sjhb		 * Ok, we know we have a waiting writer and we think we
305157846Sjhb		 * are the last reader, so grab the turnstile lock.
306157846Sjhb		 */
307157846Sjhb		turnstile_lock(&rw->rw_object);
308157846Sjhb
309157846Sjhb		/*
310157846Sjhb		 * Try to drop our lock leaving the lock in a unlocked
311157846Sjhb		 * state.
312157846Sjhb		 *
313154941Sjhb		 * If you wanted to do explicit lock handoff you'd have to
314154941Sjhb		 * do it here.  You'd also want to use turnstile_signal()
315154941Sjhb		 * and you'd have to handle the race where a higher
316167787Sjhb		 * priority thread blocks on the write lock before the
317154941Sjhb		 * thread you wakeup actually runs and have the new thread
318154941Sjhb		 * "steal" the lock.  For now it's a lot simpler to just
319167787Sjhb		 * wakeup all of the waiters.
320167787Sjhb		 *
321154941Sjhb		 * As above, if we fail, then another thread might have
322154941Sjhb		 * acquired a read lock, so drop the turnstile lock and
323154941Sjhb		 * restart.
324154941Sjhb		 */
325154941Sjhb		if (!atomic_cmpset_ptr(&rw->rw_lock,
326154941Sjhb		    RW_READERS_LOCK(1) | RW_LOCK_WRITE_WAITERS, RW_UNLOCKED)) {
327154941Sjhb			turnstile_release(&rw->rw_object);
328154941Sjhb			continue;
329154941Sjhb		}
330154941Sjhb		if (LOCK_LOG_TEST(&rw->rw_object, 0))
331167787Sjhb			CTR2(KTR_LOCK, "%s: %p last succeeded with waiters",
332167787Sjhb			    __func__, rw);
333160771Sjhb
334154941Sjhb		/*
335154941Sjhb		 * Ok.  The lock is released and all that's left is to
336154941Sjhb		 * wake up the waiters.  Note that the lock might not be
337154941Sjhb		 * free anymore, but in that case the writers will just
338154941Sjhb		 * block again if they run before the new lock holder(s)
339154941Sjhb		 * release the lock.
340154941Sjhb		 */
341154941Sjhb		ts = turnstile_lookup(&rw->rw_object);
342169394Sjhb		turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
343169394Sjhb		turnstile_unpend(ts, TS_SHARED_LOCK);
344154941Sjhb		break;
345160771Sjhb	}
346167787Sjhb}
347167787Sjhb
348154941Sjhb/*
349154941Sjhb * This function is called when we are unable to obtain a write lock on the
350154941Sjhb * first try.  This means that at least one other thread holds either a
351154941Sjhb * read or write lock.
352154941Sjhb */
353154941Sjhbvoid
354154941Sjhb_rw_wlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
355154941Sjhb{
356154941Sjhb	uintptr_t v;
357154941Sjhb
358154941Sjhb	if (LOCK_LOG_TEST(&rw->rw_object, 0))
359154941Sjhb		CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
360167787Sjhb		    rw->rw_object.lo_name, (void *)rw->rw_lock, file, line);
361154941Sjhb
362154941Sjhb	while (!_rw_write_lock(rw, tid)) {
363154941Sjhb		turnstile_lock(&rw->rw_object);
364154941Sjhb		v = rw->rw_lock;
365154941Sjhb
366154941Sjhb		/*
367154941Sjhb		 * If the lock was released while spinning on the
368167307Sjhb		 * turnstile chain lock, try again.
369154941Sjhb		 */
370164159Skmacy		if (v == RW_UNLOCKED) {
371154941Sjhb			turnstile_release(&rw->rw_object);
372154941Sjhb			cpu_spinwait();
373154941Sjhb			continue;
374154941Sjhb		}
375154941Sjhb
376154941Sjhb		/*
377154941Sjhb		 * If the lock was released by a writer with both readers
378154941Sjhb		 * and writers waiting and a reader hasn't woken up and
379154941Sjhb		 * acquired the lock yet, rw_lock will be set to the
380154941Sjhb		 * value RW_UNLOCKED | RW_LOCK_WRITE_WAITERS.  If we see
381154941Sjhb		 * that value, try to acquire it once.  Note that we have
382154941Sjhb		 * to preserve the RW_LOCK_WRITE_WAITERS flag as there are
383154941Sjhb		 * other writers waiting still. If we fail, restart the
384154941Sjhb		 * loop.
385154941Sjhb		 */
386154941Sjhb		if (v == (RW_UNLOCKED | RW_LOCK_WRITE_WAITERS)) {
387154941Sjhb			if (atomic_cmpset_acq_ptr(&rw->rw_lock,
388154941Sjhb			    RW_UNLOCKED | RW_LOCK_WRITE_WAITERS,
389154941Sjhb			    tid | RW_LOCK_WRITE_WAITERS)) {
390154941Sjhb				turnstile_claim(&rw->rw_object);
391154941Sjhb				CTR2(KTR_LOCK, "%s: %p claimed by new writer",
392154941Sjhb				    __func__, rw);
393154941Sjhb				break;
394167787Sjhb			}
395154941Sjhb			turnstile_release(&rw->rw_object);
396154941Sjhb			cpu_spinwait();
397154941Sjhb			continue;
398154941Sjhb		}
399154941Sjhb
400154941Sjhb		/*
401154941Sjhb		 * If the RW_LOCK_WRITE_WAITERS flag isn't set, then try to
402154941Sjhb		 * set it.  If we fail to set it, then loop back and try
403154941Sjhb		 * again.
404154941Sjhb		 */
405154941Sjhb		if (!(v & RW_LOCK_WRITE_WAITERS) &&
406154941Sjhb		    !atomic_cmpset_ptr(&rw->rw_lock, v,
407154941Sjhb		    v | RW_LOCK_WRITE_WAITERS)) {
408154941Sjhb			turnstile_release(&rw->rw_object);
409154941Sjhb			cpu_spinwait();
410154941Sjhb			continue;
411154941Sjhb		}
412167787Sjhb		if (!(v & RW_LOCK_WRITE_WAITERS) &&
413154941Sjhb		    LOCK_LOG_TEST(&rw->rw_object, 0))
414154941Sjhb			CTR2(KTR_LOCK, "%s: %p set write waiters flag",
415154941Sjhb			    __func__, rw);
416154941Sjhb
417154941Sjhb		/* XXX: Adaptively spin if current wlock owner on another CPU? */
418154941Sjhb
419154941Sjhb		/*
420154941Sjhb		 * We were unable to acquire the lock and the write waiters
421154941Sjhb		 * flag is set, so we must block on the turnstile.
422154941Sjhb		 */
423154941Sjhb		if (LOCK_LOG_TEST(&rw->rw_object, 0))
424154941Sjhb			CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
425154941Sjhb			    rw);
426154941Sjhb		turnstile_wait(&rw->rw_object, rw_owner(rw),
427154941Sjhb		    TS_EXCLUSIVE_QUEUE);
428154941Sjhb		if (LOCK_LOG_TEST(&rw->rw_object, 0))
429154941Sjhb			CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
430154941Sjhb			    __func__, rw);
431154941Sjhb	}
432167787Sjhb}
433154941Sjhb
434154941Sjhb/*
435167787Sjhb * This function is called if the first try at releasing a write lock failed.
436154941Sjhb * This means that one of the 2 waiter bits must be set indicating that at
437154941Sjhb * least one thread is waiting on this lock.
438154941Sjhb */
439154941Sjhbvoid
440154941Sjhb_rw_wunlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
441154941Sjhb{
442154941Sjhb	struct turnstile *ts;
443154941Sjhb	uintptr_t v;
444154941Sjhb	int queue;
445154941Sjhb
446167787Sjhb	KASSERT(rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS),
447157846Sjhb	    ("%s: neither of the waiter flags are set", __func__));
448154941Sjhb
449154941Sjhb	if (LOCK_LOG_TEST(&rw->rw_object, 0))
450154941Sjhb		CTR2(KTR_LOCK, "%s: %p contested", __func__, rw);
451154941Sjhb
452167787Sjhb	turnstile_lock(&rw->rw_object);
453154941Sjhb	ts = turnstile_lookup(&rw->rw_object);
454154941Sjhb
455154941Sjhb	/* XXX: Adaptive fixup would be required here. */
456154941Sjhb	MPASS(ts != NULL);
457154941Sjhb
458154941Sjhb	/*
459154941Sjhb	 * Use the same algo as sx locks for now.  Prefer waking up shared
460154941Sjhb	 * waiters if we have any over writers.  This is probably not ideal.
461154941Sjhb	 *
462154941Sjhb	 * 'v' is the value we are going to write back to rw_lock.  If we
463167801Sjhb	 * have waiters on both queues, we need to preserve the state of
464157846Sjhb	 * the waiter flag for the queue we don't wake up.  For now this is
465157851Swkoszek	 * hardcoded for the algorithm mentioned above.
466154941Sjhb	 *
467154941Sjhb	 * In the case of both readers and writers waiting we wakeup the
468167787Sjhb	 * readers but leave the RW_LOCK_WRITE_WAITERS flag set.  If a
469154941Sjhb	 * new writer comes in before a reader it will claim the lock up
470167787Sjhb	 * above.  There is probably a potential priority inversion in
471154941Sjhb	 * there that could be worked around either by waking both queues
472154941Sjhb	 * of waiters or doing some complicated lock handoff gymnastics.
473167787Sjhb	 */
474154941Sjhb	if (rw->rw_lock & RW_LOCK_READ_WAITERS) {
475154941Sjhb		queue = TS_SHARED_QUEUE;
476154941Sjhb		v = RW_UNLOCKED | (rw->rw_lock & RW_LOCK_WRITE_WAITERS);
477154941Sjhb	} else {
478154941Sjhb		queue = TS_EXCLUSIVE_QUEUE;
479154941Sjhb		v = RW_UNLOCKED;
480154941Sjhb	}
481167787Sjhb	if (LOCK_LOG_TEST(&rw->rw_object, 0))
482154941Sjhb		CTR3(KTR_LOCK, "%s: %p waking up %s waiters", __func__, rw,
483154941Sjhb		    queue == TS_SHARED_QUEUE ? "read" : "write");
484154941Sjhb
485154941Sjhb	/* Wake up all waiters for the specific queue. */
486154941Sjhb	turnstile_broadcast(ts, queue);
487154941Sjhb	atomic_store_rel_ptr(&rw->rw_lock, v);
488154941Sjhb	turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
489154941Sjhb}
490154941Sjhb
491154941Sjhb#ifdef INVARIANT_SUPPORT
492154941Sjhb#ifndef INVARIANTS
493168073Sjhb#undef _rw_assert
494154941Sjhb#endif
495154941Sjhb
496154941Sjhb/*
497154941Sjhb * In the non-WITNESS case, rw_assert() can only detect that at least
498154941Sjhb * *some* thread owns an rlock, but it cannot guarantee that *this*
499154941Sjhb * thread owns an rlock.
500167787Sjhb */
501154941Sjhbvoid
502154941Sjhb_rw_assert(struct rwlock *rw, int what, const char *file, int line)
503154941Sjhb{
504154941Sjhb
505167787Sjhb	if (panicstr != NULL)
506154941Sjhb		return;
507154941Sjhb	switch (what) {
508154941Sjhb	case RA_LOCKED:
509154941Sjhb	case RA_RLOCKED:
510154941Sjhb#ifdef WITNESS
511154941Sjhb		witness_assert(&rw->rw_object, what, file, line);
512154941Sjhb#else
513154941Sjhb		/*
514154941Sjhb		 * If some other thread has a write lock or we have one
515157826Sjhb		 * and are asserting a read lock, fail.  Also, if no one
516157826Sjhb		 * has a lock at all, fail.
517157826Sjhb		 */
518167787Sjhb		if (rw->rw_lock == RW_UNLOCKED ||
519157826Sjhb		    (!(rw->rw_lock & RW_LOCK_READ) && (what == RA_RLOCKED ||
520157826Sjhb		    rw_owner(rw) != curthread)))
521157826Sjhb			panic("Lock %s not %slocked @ %s:%d\n",
522167787Sjhb			    rw->rw_object.lo_name, (what == RA_RLOCKED) ?
523157826Sjhb			    "read " : "", file, line);
524157826Sjhb#endif
525154941Sjhb		break;
526154941Sjhb	case RA_WLOCKED:
527167801Sjhb		if (rw_owner(rw) != curthread)
528157846Sjhb			panic("Lock %s not exclusively locked @ %s:%d\n",
529157846Sjhb			    rw->rw_object.lo_name, file, line);
530157846Sjhb		break;
531157846Sjhb	case RA_UNLOCKED:
532157846Sjhb#ifdef WITNESS
533157846Sjhb		witness_assert(&rw->rw_object, what, file, line);
534157846Sjhb#else
535167787Sjhb		/*
536167787Sjhb		 * If we hold a write lock fail.  We can't reliably check
537157846Sjhb		 * to see if we hold a read lock or not.
538157846Sjhb		 */
539157846Sjhb		if (rw_owner(rw) == curthread)
540157846Sjhb			panic("Lock %s exclusively locked @ %s:%d\n",
541157846Sjhb			    rw->rw_object.lo_name, file, line);
542157846Sjhb#endif
543157846Sjhb		break;
544157846Sjhb	default:
545154941Sjhb		panic("Unknown rw lock assertion: %d @ %s:%d", what, file,
546154941Sjhb		    line);
547154941Sjhb	}
548154941Sjhb}
549154941Sjhb#endif /* INVARIANT_SUPPORT */
550167787Sjhb
551154941Sjhb#ifdef DDB
552154941Sjhbvoid
553167787Sjhbdb_show_rwlock(struct lock_object *lock)
554154941Sjhb{
555167787Sjhb	struct rwlock *rw;
556154941Sjhb	struct thread *td;
557154941Sjhb
558154941Sjhb	rw = (struct rwlock *)lock;
559154941Sjhb
560154941Sjhb	db_printf(" state: ");
561154941Sjhb	if (rw->rw_lock == RW_UNLOCKED)
562154941Sjhb		db_printf("UNLOCKED\n");
563154941Sjhb	else if (rw->rw_lock & RW_LOCK_READ)
564154941Sjhb		db_printf("RLOCK: %jd locks\n",
565154941Sjhb		    (intmax_t)(RW_READERS(rw->rw_lock)));
566154941Sjhb	else {
567154941Sjhb		td = rw_owner(rw);
568154941Sjhb		db_printf("WLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
569154941Sjhb		    td->td_tid, td->td_proc->p_pid, td->td_proc->p_comm);
570154941Sjhb	}
571154941Sjhb	db_printf(" waiters: ");
572154941Sjhb	switch (rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)) {
573154941Sjhb	case RW_LOCK_READ_WAITERS:
574154941Sjhb		db_printf("readers\n");
575154941Sjhb		break;
576167787Sjhb	case RW_LOCK_WRITE_WAITERS:
577154941Sjhb		db_printf("writers\n");
578154941Sjhb		break;
579167787Sjhb	case RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS:
580167787Sjhb		db_printf("readers and waiters\n");
581154941Sjhb		break;
582167801Sjhb	default:
583157846Sjhb		db_printf("none\n");
584157846Sjhb		break;
585157846Sjhb	}
586157846Sjhb}
587157846Sjhb
588157846Sjhb#endif
589157846Sjhb