Home | History | Annotate | Line # | Download | only in kern
kern_rwlock.c revision 1.1.2.5
      1  1.1.2.5  thorpej /*	$NetBSD: kern_rwlock.c,v 1.1.2.5 2002/03/22 03:27:00 thorpej Exp $	*/
      2  1.1.2.1  thorpej 
      3  1.1.2.1  thorpej /*-
      4  1.1.2.1  thorpej  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  1.1.2.1  thorpej  * All rights reserved.
      6  1.1.2.1  thorpej  *
      7  1.1.2.1  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1.2.1  thorpej  * by Jason R. Thorpe.
      9  1.1.2.1  thorpej  *
     10  1.1.2.1  thorpej  * Redistribution and use in source and binary forms, with or without
     11  1.1.2.1  thorpej  * modification, are permitted provided that the following conditions
     12  1.1.2.1  thorpej  * are met:
     13  1.1.2.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     14  1.1.2.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     15  1.1.2.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1.2.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     17  1.1.2.1  thorpej  *    documentation and/or other materials provided with the distribution.
     18  1.1.2.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     19  1.1.2.1  thorpej  *    must display the following acknowledgement:
     20  1.1.2.1  thorpej  *	This product includes software developed by the NetBSD
     21  1.1.2.1  thorpej  *	Foundation, Inc. and its contributors.
     22  1.1.2.1  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1.2.1  thorpej  *    contributors may be used to endorse or promote products derived
     24  1.1.2.1  thorpej  *    from this software without specific prior written permission.
     25  1.1.2.1  thorpej  *
     26  1.1.2.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1.2.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1.2.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1.2.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1.2.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1.2.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1.2.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1.2.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1.2.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1.2.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1.2.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1.2.1  thorpej  */
     38  1.1.2.1  thorpej 
     39  1.1.2.1  thorpej /*
     40  1.1.2.1  thorpej  * Kernel reader/writer lock implementation, modeled after those found in
     41  1.1.2.1  thorpej  * Solaris, a description of which can be found in:
     42  1.1.2.1  thorpej  *
     43  1.1.2.1  thorpej  *	Solaris Internals: Core Kernel Architecture, Jim Mauro and
     44  1.1.2.1  thorpej  *	    Richard McDougall.
     45  1.1.2.1  thorpej  */
     46  1.1.2.1  thorpej 
     47  1.1.2.1  thorpej #include <sys/cdefs.h>
     48  1.1.2.5  thorpej __KERNEL_RCSID(0, "$NetBSD: kern_rwlock.c,v 1.1.2.5 2002/03/22 03:27:00 thorpej Exp $");
     49  1.1.2.1  thorpej 
     50  1.1.2.1  thorpej #include <sys/param.h>
     51  1.1.2.1  thorpej #include <sys/proc.h>
     52  1.1.2.1  thorpej #include <sys/rwlock.h>
     53  1.1.2.1  thorpej #include <sys/sched.h>
     54  1.1.2.1  thorpej #include <sys/systm.h>
     55  1.1.2.1  thorpej 
     56  1.1.2.5  thorpej /*
     57  1.1.2.5  thorpej  * note that stdarg.h and the ansi style va_start macro is used for both
     58  1.1.2.5  thorpej  * ansi and traditional c complers.
     59  1.1.2.5  thorpej  * XXX: this requires that stdarg.h define: va_alist and va_dcl
     60  1.1.2.5  thorpej  */
     61  1.1.2.5  thorpej #include <machine/stdarg.h>
     62  1.1.2.5  thorpej 
     63  1.1.2.5  thorpej #if defined(RWLOCK_DEBUG)
     64  1.1.2.5  thorpej #define	RWL_LOCKED(rwl)							\
     65  1.1.2.5  thorpej 	(rwl)->rwl_debug.rwl_locked = (vaddr_t) __builtin_return_address(0)
     66  1.1.2.5  thorpej #define	RWL_UNLOCKED(rwl)						\
     67  1.1.2.5  thorpej 	(rwl)->rwl_debug.rwl_unlocked = (vaddr_t) __builtin_return_address(0)
     68  1.1.2.5  thorpej #else
     69  1.1.2.5  thorpej #define	RWL_LOCKED(rwl)		/* nothing */
     70  1.1.2.5  thorpej #define	RWL_UNLOCKED(rwl)	/* nothing */
     71  1.1.2.5  thorpej #endif /* RWLOCK_DEBUG */
     72  1.1.2.5  thorpej 
     73  1.1.2.5  thorpej static void
     74  1.1.2.5  thorpej rwlock_abort(krwlock_t *rwl, const char *fmt, ...)
     75  1.1.2.5  thorpej {
     76  1.1.2.5  thorpej 	va_list ap;
     77  1.1.2.5  thorpej 
     78  1.1.2.5  thorpej 	va_start(ap, fmt);
     79  1.1.2.5  thorpej 	vprintf(fmt, ap);
     80  1.1.2.5  thorpej 	va_end(ap);
     81  1.1.2.5  thorpej 
     82  1.1.2.5  thorpej #if defined(RWLOCK_DEBUG)
     83  1.1.2.5  thorpej 	printf("last locked: 0x%lx  last unlocked: 0x%lx\n",
     84  1.1.2.5  thorpej 	    rwl->rwl_debug.rwl_locked, rwl->rwl_debug.rwl_unlocked);
     85  1.1.2.5  thorpej #endif
     86  1.1.2.5  thorpej 
     87  1.1.2.5  thorpej 	panic("rwlock_abort");
     88  1.1.2.5  thorpej }
     89  1.1.2.5  thorpej 
     90  1.1.2.1  thorpej /*
     91  1.1.2.1  thorpej  * rw_init:
     92  1.1.2.1  thorpej  *
     93  1.1.2.1  thorpej  *	Initialize a rwlock for use.
     94  1.1.2.1  thorpej  */
     95  1.1.2.1  thorpej void
     96  1.1.2.1  thorpej rw_init(krwlock_t *rwl)
     97  1.1.2.1  thorpej {
     98  1.1.2.1  thorpej 
     99  1.1.2.1  thorpej 	RWLOCK_INIT(rwl);
    100  1.1.2.1  thorpej }
    101  1.1.2.1  thorpej 
    102  1.1.2.1  thorpej /*
    103  1.1.2.1  thorpej  * rw_destroy:
    104  1.1.2.1  thorpej  *
    105  1.1.2.1  thorpej  *	Tear down a rwlock.
    106  1.1.2.1  thorpej  */
    107  1.1.2.1  thorpej void
    108  1.1.2.1  thorpej rw_destroy(krwlock_t *rwl)
    109  1.1.2.1  thorpej {
    110  1.1.2.1  thorpej 
    111  1.1.2.1  thorpej 	/* XXX IMPLEMENT ME XXX */
    112  1.1.2.1  thorpej }
    113  1.1.2.1  thorpej 
    114  1.1.2.1  thorpej /*
    115  1.1.2.1  thorpej  * rw_enter:
    116  1.1.2.1  thorpej  *
    117  1.1.2.1  thorpej  *	Acquire a rwlock.
    118  1.1.2.1  thorpej  */
    119  1.1.2.1  thorpej void
    120  1.1.2.1  thorpej rw_enter(krwlock_t *rwl, krw_t rw)
    121  1.1.2.1  thorpej {
    122  1.1.2.1  thorpej 	struct turnstile *ts;
    123  1.1.2.1  thorpej 	struct proc *p;
    124  1.1.2.4  thorpej 	unsigned long owner, incr, need_wait, set_wait;
    125  1.1.2.1  thorpej 
    126  1.1.2.1  thorpej 	/*
    127  1.1.2.1  thorpej 	 * Ensure RW_WRITER == 0, so that machine-dependent code can
    128  1.1.2.1  thorpej 	 * make that assumption.
    129  1.1.2.1  thorpej 	 */
    130  1.1.2.1  thorpej #if RW_WRITER != 0
    131  1.1.2.1  thorpej #error "RW_WRITER != 0"
    132  1.1.2.1  thorpej #endif
    133  1.1.2.1  thorpej 
    134  1.1.2.1  thorpej 	/*
    135  1.1.2.1  thorpej 	 * We play a slight trick here.  If we're a reader, we want
    136  1.1.2.1  thorpej 	 * increment the read count.  If we're a writer, we want to
    137  1.1.2.1  thorpej 	 * set the owner field and whe WRITE_LOCKED bit.
    138  1.1.2.1  thorpej 	 *
    139  1.1.2.1  thorpej 	 * In the latter case, we expect those bits to be zero,
    140  1.1.2.1  thorpej 	 * therefore we can use an add operation to set them, which
    141  1.1.2.1  thorpej 	 * means an add operation for both cases.
    142  1.1.2.1  thorpej 	 */
    143  1.1.2.1  thorpej 	switch (rw) {
    144  1.1.2.1  thorpej 	case RW_WRITER:
    145  1.1.2.1  thorpej 		incr = ((unsigned long) curproc) | RWLOCK_WRITE_LOCKED;
    146  1.1.2.1  thorpej 		need_wait = RWLOCK_WRITE_LOCKED;
    147  1.1.2.1  thorpej 		set_wait = RWLOCK_HAS_WAITERS | RWLOCK_WRITE_WANTED;
    148  1.1.2.1  thorpej 		break;
    149  1.1.2.1  thorpej 
    150  1.1.2.1  thorpej 	case RW_READER:
    151  1.1.2.1  thorpej 		incr = RWLOCK_READ_INCR;
    152  1.1.2.1  thorpej 		need_wait = RWLOCK_WRITE_LOCKED | RWLOCK_WRITE_WANTED;
    153  1.1.2.1  thorpej 		set_wait = RWLOCK_HAS_WAITERS;
    154  1.1.2.1  thorpej 		break;
    155  1.1.2.1  thorpej #ifdef DIAGNOSTIC
    156  1.1.2.1  thorpej 	default:
    157  1.1.2.5  thorpej 		rwlock_abort(rwl, "rw_enter: bad rw %d", rw);
    158  1.1.2.1  thorpej #endif
    159  1.1.2.1  thorpej 	}
    160  1.1.2.1  thorpej 
    161  1.1.2.1  thorpej 	for (;;) {
    162  1.1.2.1  thorpej 		/*
    163  1.1.2.1  thorpej 		 * Read the lock owner field.  If the need-to-wait
    164  1.1.2.1  thorpej 		 * indicator is clear, then try to acquire the lock.
    165  1.1.2.1  thorpej 		 */
    166  1.1.2.1  thorpej 		owner = rwl->rwl_owner;
    167  1.1.2.1  thorpej 		if ((owner & need_wait) == 0) {
    168  1.1.2.4  thorpej 			if (RWLOCK_ACQUIRE(rwl, owner, owner + incr)) {
    169  1.1.2.1  thorpej 				/* Got it! */
    170  1.1.2.1  thorpej 				break;
    171  1.1.2.1  thorpej 			}
    172  1.1.2.1  thorpej 
    173  1.1.2.1  thorpej 			/*
    174  1.1.2.1  thorpej 			 * Didn't get it -- spin around again (we'll
    175  1.1.2.1  thorpej 			 * probably sleep on the next iteration).
    176  1.1.2.1  thorpej 			 */
    177  1.1.2.1  thorpej 			continue;
    178  1.1.2.1  thorpej 		}
    179  1.1.2.1  thorpej 
    180  1.1.2.1  thorpej 		if (RWLOCK_OWNER(rwl) == curproc)
    181  1.1.2.5  thorpej 			rwlock_abort(rwl, "rw_enter: locking against myself");
    182  1.1.2.1  thorpej 
    183  1.1.2.1  thorpej 		ts = turnstile_lookup(rwl);
    184  1.1.2.1  thorpej 
    185  1.1.2.1  thorpej 		/*
    186  1.1.2.1  thorpej 		 * Mark the rwlock as having waiters.  After we do
    187  1.1.2.1  thorpej 		 * this, we need to check one more time if the lock
    188  1.1.2.1  thorpej 		 * is busy, and if not, spin around again.
    189  1.1.2.1  thorpej 		 *
    190  1.1.2.1  thorpej 		 * Note, we also need to spin again if we failed to
    191  1.1.2.1  thorpej 		 * set the has-waiters indicator (which means the
    192  1.1.2.1  thorpej 		 * lock condition changed, but more importantly, we
    193  1.1.2.1  thorpej 		 * need to try and set that indicator again).
    194  1.1.2.1  thorpej 		 */
    195  1.1.2.1  thorpej 		RWLOCK_SET_WAITERS(rwl, need_wait, set_wait);
    196  1.1.2.1  thorpej 		owner = rwl->rwl_owner;
    197  1.1.2.1  thorpej 		if ((owner & need_wait) == 0 || (owner & set_wait) == 0) {
    198  1.1.2.1  thorpej 			turnstile_exit(rwl);
    199  1.1.2.1  thorpej 			continue;
    200  1.1.2.1  thorpej 		}
    201  1.1.2.1  thorpej 		/* XXXJRT p->p_priority */
    202  1.1.2.1  thorpej 		/* XXXJRT Do not currently distinguish reader vs. writer. */
    203  1.1.2.1  thorpej 		(void) turnstile_block(ts, TS_WRITER_Q, p->p_priority, rwl);
    204  1.1.2.1  thorpej 
    205  1.1.2.1  thorpej 		/*
    206  1.1.2.1  thorpej 		 * XXX Solaris Internals says that the Solaris 7
    207  1.1.2.1  thorpej 		 * rwlock implementation does a direct-handoff.  We
    208  1.1.2.1  thorpej 		 * don't implement that yet, but if we did, then a
    209  1.1.2.1  thorpej 		 * thread wakes back up, i.e. arrives here, it would
    210  1.1.2.1  thorpej 		 * hold the lock as requested.
    211  1.1.2.1  thorpej 		 */
    212  1.1.2.1  thorpej 	}
    213  1.1.2.1  thorpej 
    214  1.1.2.1  thorpej 	KASSERT((rw == RW_WRITER && RWLOCK_OWNER(rwl) == curproc) ||
    215  1.1.2.1  thorpej 		(rw == RW_READER && RWLOCK_COUNT(rwl) != 0));
    216  1.1.2.5  thorpej #if defined(RWLOCK_DEBUG)
    217  1.1.2.5  thorpej 	if (rw == RW_WRITER)
    218  1.1.2.5  thorpej 		RWL_LOCKED(rwl);
    219  1.1.2.5  thorpej #endif
    220  1.1.2.1  thorpej }
    221  1.1.2.1  thorpej 
    222  1.1.2.1  thorpej /*
    223  1.1.2.1  thorpej  * rw_tryenter:
    224  1.1.2.1  thorpej  *
    225  1.1.2.1  thorpej  *	Try to acquire a rwlock.
    226  1.1.2.1  thorpej  */
    227  1.1.2.1  thorpej int
    228  1.1.2.1  thorpej rw_tryenter(krwlock_t *rwl, krw_t rw)
    229  1.1.2.1  thorpej {
    230  1.1.2.4  thorpej 	unsigned long owner, incr, need_wait;
    231  1.1.2.1  thorpej 
    232  1.1.2.1  thorpej 	switch (rw) {
    233  1.1.2.1  thorpej 	case RW_WRITER:
    234  1.1.2.1  thorpej 		incr = ((unsigned long) curproc) | RWLOCK_WRITE_LOCKED;
    235  1.1.2.1  thorpej 		need_wait = RWLOCK_WRITE_LOCKED;
    236  1.1.2.1  thorpej 		break;
    237  1.1.2.1  thorpej 
    238  1.1.2.1  thorpej 	case RW_READER:
    239  1.1.2.1  thorpej 		incr = RWLOCK_READ_INCR;
    240  1.1.2.1  thorpej 		need_wait = RWLOCK_WRITE_LOCKED | RWLOCK_WRITE_WANTED;
    241  1.1.2.1  thorpej 		break;
    242  1.1.2.1  thorpej #ifdef DIAGNOSTIC
    243  1.1.2.1  thorpej 	default:
    244  1.1.2.5  thorpej 		rwlock_abort(rwl, "rw_tryenter: bad rw %d", rw);
    245  1.1.2.1  thorpej #endif
    246  1.1.2.1  thorpej 	}
    247  1.1.2.1  thorpej 
    248  1.1.2.1  thorpej 	for (;;) {
    249  1.1.2.1  thorpej 		owner = rwl->rwl_owner;
    250  1.1.2.1  thorpej 		if ((owner & need_wait) == 0) {
    251  1.1.2.4  thorpej 			if (RWLOCK_ACQUIRE(rwl, owner, owner + incr)) {
    252  1.1.2.1  thorpej 				/* Got it! */
    253  1.1.2.1  thorpej 				break;
    254  1.1.2.1  thorpej 			}
    255  1.1.2.1  thorpej 			continue;
    256  1.1.2.1  thorpej 		}
    257  1.1.2.1  thorpej 		return (0);
    258  1.1.2.1  thorpej 	}
    259  1.1.2.1  thorpej 
    260  1.1.2.1  thorpej 	KASSERT((rw == RW_WRITER && RWLOCK_OWNER(rwl) == curproc) ||
    261  1.1.2.1  thorpej 		(rw == RW_READER && RWLOCK_COUNT(rwl) != 0));
    262  1.1.2.5  thorpej #if defined(RWLOCK_DEBUG)
    263  1.1.2.5  thorpej 	if (rw == RW_WRITER)
    264  1.1.2.5  thorpej 		RWL_LOCKED(rwl);
    265  1.1.2.5  thorpej #endif
    266  1.1.2.1  thorpej 	return (1);
    267  1.1.2.1  thorpej }
    268  1.1.2.1  thorpej 
    269  1.1.2.1  thorpej /*
    270  1.1.2.1  thorpej  * rw_exit:
    271  1.1.2.1  thorpej  *
    272  1.1.2.1  thorpej  *	Release a rwlock.
    273  1.1.2.1  thorpej  */
    274  1.1.2.1  thorpej void
    275  1.1.2.1  thorpej rw_exit(krwlock_t *rwl)
    276  1.1.2.1  thorpej {
    277  1.1.2.1  thorpej 	struct turnstile *ts;
    278  1.1.2.4  thorpej 	unsigned long owner, decr, new;
    279  1.1.2.1  thorpej 
    280  1.1.2.1  thorpej 	/*
    281  1.1.2.1  thorpej 	 * Again, we use a trick.  Since we used an add operation to
    282  1.1.2.1  thorpej 	 * set the required lock bits, we can use a subtract to clear
    283  1.1.2.1  thorpej 	 * them, which makes the read-release and write-release path
    284  1.1.2.1  thorpej 	 * the same.
    285  1.1.2.1  thorpej 	 */
    286  1.1.2.1  thorpej 	if (rwl->rwl_owner & RWLOCK_WRITE_LOCKED) {
    287  1.1.2.1  thorpej 		if (RWLOCK_OWNER(rwl) == NULL)
    288  1.1.2.5  thorpej 			rwlock_abort(rwl, "rw_exit: not owned");
    289  1.1.2.1  thorpej 		else
    290  1.1.2.5  thorpej 			rwlock_abort(rwl, "rw_exit: not owner, owner = %p, "
    291  1.1.2.1  thorpej 			    "current = %p", RWLOCK_OWNER(rwl), curproc);
    292  1.1.2.1  thorpej 		decr = ((unsigned long) curproc) | RWLOCK_WRITE_LOCKED;
    293  1.1.2.5  thorpej 		RWL_UNLOCKED(rwl);
    294  1.1.2.1  thorpej 	} else {
    295  1.1.2.1  thorpej 		if (RWLOCK_COUNT(rwl) == 0)
    296  1.1.2.5  thorpej 			rwlock_abort(rwl, "rw_exit: not held\n");
    297  1.1.2.1  thorpej 		decr = RWLOCK_READ_INCR;
    298  1.1.2.1  thorpej 	}
    299  1.1.2.1  thorpej 
    300  1.1.2.1  thorpej 	for (;;) {
    301  1.1.2.1  thorpej 		/*
    302  1.1.2.1  thorpej 		 * Get this lock's turnstile.  This gets the interlock on
    303  1.1.2.1  thorpej 		 * the sleep queue.  Once we have that, we can perform the
    304  1.1.2.1  thorpej 		 * lock release operation.
    305  1.1.2.1  thorpej 		 */
    306  1.1.2.1  thorpej 		ts = turnstile_lookup(rwl);
    307  1.1.2.1  thorpej 
    308  1.1.2.1  thorpej 		/*
    309  1.1.2.1  thorpej 		 * Compute what we expect the new value of the lock
    310  1.1.2.1  thorpej 		 * to be.  Skip the wakeup step if there are no
    311  1.1.2.1  thorpej 		 * appropriate waiters.
    312  1.1.2.1  thorpej 		 */
    313  1.1.2.1  thorpej 		owner = rwl->rwl_owner;
    314  1.1.2.1  thorpej 		new = owner - decr;
    315  1.1.2.1  thorpej 		if ((new & (RWLOCK_THREAD |
    316  1.1.2.1  thorpej 			    RWLOCK_HAS_WAITERS)) != RWLOCK_HAS_WAITERS) {
    317  1.1.2.4  thorpej 			if (RWLOCK_RELEASE(rwl, owner, new)) {
    318  1.1.2.1  thorpej 				/* Ding! */
    319  1.1.2.1  thorpej 				turnstile_exit(rwl);
    320  1.1.2.1  thorpej 				break;
    321  1.1.2.1  thorpej 			}
    322  1.1.2.1  thorpej 			turnstile_exit(rwl);
    323  1.1.2.1  thorpej 			continue;
    324  1.1.2.1  thorpej 		}
    325  1.1.2.1  thorpej 
    326  1.1.2.1  thorpej 		/* We're about to wake everybody up; clear waiter bits. */
    327  1.1.2.1  thorpej 		new &= ~(RWLOCK_HAS_WAITERS | RWLOCK_WRITE_WANTED);
    328  1.1.2.1  thorpej 
    329  1.1.2.4  thorpej 		if (RWLOCK_RELEASE(rwl, owner, new) == 0) {
    330  1.1.2.1  thorpej 			/* Oops, try again. */
    331  1.1.2.1  thorpej 			turnstile_exit(rwl);
    332  1.1.2.1  thorpej 			continue;
    333  1.1.2.1  thorpej 		}
    334  1.1.2.1  thorpej 
    335  1.1.2.1  thorpej 		/*
    336  1.1.2.1  thorpej 		 * Wake the thundering herd.
    337  1.1.2.1  thorpej 		 * XXX Should implement direct-handoff.
    338  1.1.2.1  thorpej 		 */
    339  1.1.2.1  thorpej 		KASSERT(ts != NULL);
    340  1.1.2.2  thorpej 		turnstile_wakeup(ts, TS_WRITER_Q,
    341  1.1.2.3  thorpej 		    ts->ts_sleepq[TS_WRITER_Q].tsq_waiters, NULL);
    342  1.1.2.1  thorpej 		break;
    343  1.1.2.1  thorpej 	}
    344  1.1.2.1  thorpej }
    345  1.1.2.1  thorpej 
    346  1.1.2.1  thorpej /*
    347  1.1.2.1  thorpej  * rw_downgrade:
    348  1.1.2.1  thorpej  *
    349  1.1.2.1  thorpej  *	Downgrade a write lock to a read lock.
    350  1.1.2.1  thorpej  */
    351  1.1.2.1  thorpej void
    352  1.1.2.1  thorpej rw_downgrade(krwlock_t *rwl)
    353  1.1.2.1  thorpej {
    354  1.1.2.1  thorpej 	struct turnstile *ts;
    355  1.1.2.4  thorpej 	unsigned long owner;
    356  1.1.2.1  thorpej 
    357  1.1.2.1  thorpej 	if (RWLOCK_OWNER(rwl) != curproc) {
    358  1.1.2.1  thorpej 		if (RWLOCK_OWNER(rwl) == NULL)
    359  1.1.2.5  thorpej 			rwlock_abort(rwl, "rw_downgrade: not owned");
    360  1.1.2.1  thorpej 		else
    361  1.1.2.5  thorpej 			rwlock_abort(rwl,
    362  1.1.2.5  thorpej 			    "rw_downgrade: not owner, owner = %p, "
    363  1.1.2.1  thorpej 			    "current = %p", RWLOCK_OWNER(rwl), curproc);
    364  1.1.2.1  thorpej 	}
    365  1.1.2.1  thorpej 
    366  1.1.2.5  thorpej 	RWL_UNLOCKED(rwl);
    367  1.1.2.5  thorpej 
    368  1.1.2.1  thorpej 	/* XXX This algorithm has to change if we do direct-handoff. */
    369  1.1.2.1  thorpej 	for (;;) {
    370  1.1.2.1  thorpej 		ts = turnstile_lookup(rwl);
    371  1.1.2.1  thorpej 
    372  1.1.2.1  thorpej 		owner = rwl->rwl_owner;
    373  1.1.2.4  thorpej 		if (RWLOCK_RELEASE(rwl, owner, RWLOCK_READ_INCR) == 0) {
    374  1.1.2.1  thorpej 			/* Oops, try again. */
    375  1.1.2.1  thorpej 			turnstile_exit(rwl);
    376  1.1.2.1  thorpej 			continue;
    377  1.1.2.1  thorpej 		}
    378  1.1.2.1  thorpej 		if (owner & RWLOCK_HAS_WAITERS) {
    379  1.1.2.1  thorpej 			KASSERT(ts != NULL);
    380  1.1.2.2  thorpej 			turnstile_wakeup(ts, TS_WRITER_Q,
    381  1.1.2.3  thorpej 			    ts->ts_sleepq[TS_WRITER_Q].tsq_waiters, NULL);
    382  1.1.2.1  thorpej 		}
    383  1.1.2.1  thorpej 		break;
    384  1.1.2.1  thorpej 	}
    385  1.1.2.1  thorpej 
    386  1.1.2.1  thorpej 	KASSERT((rwl->rwl_owner & RWLOCK_WRITE_LOCKED) == 0);
    387  1.1.2.1  thorpej 	KASSERT(RWLOCK_COUNT(rwl) != 0);
    388  1.1.2.1  thorpej }
    389  1.1.2.1  thorpej 
    390  1.1.2.1  thorpej /*
    391  1.1.2.1  thorpej  * rw_tryupgrade:
    392  1.1.2.1  thorpej  *
    393  1.1.2.1  thorpej  *	Try to upgrade an read lock to a write lock.
    394  1.1.2.1  thorpej  */
    395  1.1.2.1  thorpej int
    396  1.1.2.1  thorpej rw_tryupgrade(krwlock_t *rwl)
    397  1.1.2.1  thorpej {
    398  1.1.2.4  thorpej 	unsigned long owner;
    399  1.1.2.1  thorpej 
    400  1.1.2.1  thorpej 	KASSERT((rwl->rwl_owner & RWLOCK_WRITE_LOCKED) == 0);
    401  1.1.2.1  thorpej 	KASSERT(RWLOCK_COUNT(rwl) != 0);
    402  1.1.2.1  thorpej 
    403  1.1.2.1  thorpej 	for (;;) {
    404  1.1.2.1  thorpej 		/*
    405  1.1.2.1  thorpej 		 * Since we want to favor writers, we don't bother
    406  1.1.2.1  thorpej 		 * checking for waiting writers, we just scarf it.
    407  1.1.2.1  thorpej 		 *
    408  1.1.2.1  thorpej 		 * We must be the only reader.
    409  1.1.2.1  thorpej 		 */
    410  1.1.2.1  thorpej 		owner = rwl->rwl_owner;
    411  1.1.2.1  thorpej 		if ((owner & RWLOCK_THREAD) != RWLOCK_READ_INCR)
    412  1.1.2.1  thorpej 			return (0);
    413  1.1.2.4  thorpej 		if (RWLOCK_ACQUIRE(rwl, owner,
    414  1.1.2.1  thorpej 		    ((unsigned long) curproc) | RWLOCK_WRITE_LOCKED |
    415  1.1.2.4  thorpej 		    (owner & ~RWLOCK_THREAD))) {
    416  1.1.2.1  thorpej 			/* Ding! */
    417  1.1.2.1  thorpej 			break;
    418  1.1.2.1  thorpej 		}
    419  1.1.2.1  thorpej 	}
    420  1.1.2.1  thorpej 
    421  1.1.2.1  thorpej 	KASSERT(rwl->rwl_owner & RWLOCK_WRITE_LOCKED);
    422  1.1.2.1  thorpej 	KASSERT(RWLOCK_OWNER(rwl) == curproc);
    423  1.1.2.5  thorpej 	RWL_LOCKED(rwl);
    424  1.1.2.1  thorpej 	return (1);
    425  1.1.2.1  thorpej }
    426  1.1.2.1  thorpej 
    427  1.1.2.1  thorpej /*
    428  1.1.2.1  thorpej  * rw_read_held:
    429  1.1.2.1  thorpej  *
    430  1.1.2.1  thorpej  *	Returns true if the rwlock is held for reading.
    431  1.1.2.1  thorpej  */
    432  1.1.2.1  thorpej int
    433  1.1.2.1  thorpej rw_read_held(krwlock_t *rwl)
    434  1.1.2.1  thorpej {
    435  1.1.2.1  thorpej 	unsigned long owner = rwl->rwl_owner;
    436  1.1.2.1  thorpej 
    437  1.1.2.1  thorpej 	return ((owner & RWLOCK_WRITE_LOCKED) == 0 &&
    438  1.1.2.1  thorpej 	    (owner & RWLOCK_THREAD) != 0);
    439  1.1.2.1  thorpej }
    440  1.1.2.1  thorpej 
    441  1.1.2.1  thorpej /*
    442  1.1.2.1  thorpej  * rw_write_held:
    443  1.1.2.1  thorpej  *
    444  1.1.2.1  thorpej  *	Returns true if the rwlock is held for writing.
    445  1.1.2.1  thorpej  */
    446  1.1.2.1  thorpej int
    447  1.1.2.1  thorpej rw_write_held(krwlock_t *rwl)
    448  1.1.2.1  thorpej {
    449  1.1.2.1  thorpej 	unsigned long owner = rwl->rwl_owner;
    450  1.1.2.1  thorpej 
    451  1.1.2.1  thorpej 	return ((owner & RWLOCK_WRITE_LOCKED) != 0);
    452  1.1.2.1  thorpej }
    453  1.1.2.1  thorpej 
    454  1.1.2.1  thorpej /*
    455  1.1.2.1  thorpej  * rw_read_locked:
    456  1.1.2.1  thorpej  *
    457  1.1.2.1  thorpej  *	Like rw_read_held(), but asserts it.
    458  1.1.2.1  thorpej  */
    459  1.1.2.1  thorpej int
    460  1.1.2.1  thorpej rw_read_locked(krwlock_t *rwl)
    461  1.1.2.1  thorpej {
    462  1.1.2.1  thorpej 	int rv = rw_read_held(rwl);
    463  1.1.2.1  thorpej 
    464  1.1.2.1  thorpej #ifdef DIAGNOSTIC
    465  1.1.2.1  thorpej 	if (rv == 0)
    466  1.1.2.5  thorpej 		rwlock_abort(rwl, "rw_read_locked: not held");
    467  1.1.2.1  thorpej #endif
    468  1.1.2.1  thorpej 
    469  1.1.2.1  thorpej 	return (rv);
    470  1.1.2.1  thorpej }
    471  1.1.2.1  thorpej 
    472  1.1.2.1  thorpej /*
    473  1.1.2.1  thorpej  * rw_write_locked:
    474  1.1.2.1  thorpej  *
    475  1.1.2.1  thorpej  *	Like rw_write_held(), but asserts that we hold it.
    476  1.1.2.1  thorpej  */
    477  1.1.2.1  thorpej int
    478  1.1.2.1  thorpej rw_write_locked(krwlock_t *rwl)
    479  1.1.2.1  thorpej {
    480  1.1.2.1  thorpej 	int rv = rw_write_held(rwl);
    481  1.1.2.1  thorpej 
    482  1.1.2.1  thorpej #ifdef DIAGNOSTIC
    483  1.1.2.1  thorpej 	if (rv == 0)
    484  1.1.2.5  thorpej 		rwlock_abort(rwl, "rw_write_locked: not held");
    485  1.1.2.1  thorpej 	else if (RWLOCK_OWNER(rwl) != curproc)
    486  1.1.2.5  thorpej 		rwlock_abort(rwl, "rw_write_locked: not owner, owner = %p, "
    487  1.1.2.1  thorpej 		    "current = %p", RWLOCK_OWNER(rwl), curproc);
    488  1.1.2.1  thorpej #endif
    489  1.1.2.1  thorpej 
    490  1.1.2.1  thorpej 	return (rv);
    491  1.1.2.1  thorpej }
    492  1.1.2.1  thorpej 
    493  1.1.2.1  thorpej /*
    494  1.1.2.1  thorpej  * rw_owner:
    495  1.1.2.1  thorpej  *
    496  1.1.2.1  thorpej  *	Return the owner of the rwlock.
    497  1.1.2.1  thorpej  */
    498  1.1.2.1  thorpej struct proc *
    499  1.1.2.1  thorpej rw_owner(krwlock_t *rwl)
    500  1.1.2.1  thorpej {
    501  1.1.2.1  thorpej 	unsigned long owner = rwl->rwl_owner;
    502  1.1.2.1  thorpej 
    503  1.1.2.1  thorpej 	return ((owner & RWLOCK_WRITE_LOCKED) ?
    504  1.1.2.1  thorpej 	    ((struct proc *) (owner & RWLOCK_THREAD)) : NULL);
    505  1.1.2.1  thorpej }
    506