Home | History | Annotate | Line # | Download | only in kern
kern_rwlock.c revision 1.1.2.2
      1  1.1.2.2  thorpej /*	$NetBSD: kern_rwlock.c,v 1.1.2.2 2002/03/16 03:46:38 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.2  thorpej __KERNEL_RCSID(0, "$NetBSD: kern_rwlock.c,v 1.1.2.2 2002/03/16 03:46:38 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.1  thorpej /*
     57  1.1.2.1  thorpej  * rw_init:
     58  1.1.2.1  thorpej  *
     59  1.1.2.1  thorpej  *	Initialize a rwlock for use.
     60  1.1.2.1  thorpej  */
     61  1.1.2.1  thorpej void
     62  1.1.2.1  thorpej rw_init(krwlock_t *rwl)
     63  1.1.2.1  thorpej {
     64  1.1.2.1  thorpej 
     65  1.1.2.1  thorpej 	RWLOCK_INIT(rwl);
     66  1.1.2.1  thorpej }
     67  1.1.2.1  thorpej 
     68  1.1.2.1  thorpej /*
     69  1.1.2.1  thorpej  * rw_destroy:
     70  1.1.2.1  thorpej  *
     71  1.1.2.1  thorpej  *	Tear down a rwlock.
     72  1.1.2.1  thorpej  */
     73  1.1.2.1  thorpej void
     74  1.1.2.1  thorpej rw_destroy(krwlock_t *rwl)
     75  1.1.2.1  thorpej {
     76  1.1.2.1  thorpej 
     77  1.1.2.1  thorpej 	/* XXX IMPLEMENT ME XXX */
     78  1.1.2.1  thorpej }
     79  1.1.2.1  thorpej 
     80  1.1.2.1  thorpej /*
     81  1.1.2.1  thorpej  * rw_enter:
     82  1.1.2.1  thorpej  *
     83  1.1.2.1  thorpej  *	Acquire a rwlock.
     84  1.1.2.1  thorpej  */
     85  1.1.2.1  thorpej void
     86  1.1.2.1  thorpej rw_enter(krwlock_t *rwl, krw_t rw)
     87  1.1.2.1  thorpej {
     88  1.1.2.1  thorpej 	struct turnstile *ts;
     89  1.1.2.1  thorpej 	struct proc *p;
     90  1.1.2.1  thorpej 	unsigned long owner, tmp, incr, need_wait, set_wait;
     91  1.1.2.1  thorpej 
     92  1.1.2.1  thorpej 	/*
     93  1.1.2.1  thorpej 	 * Ensure RW_WRITER == 0, so that machine-dependent code can
     94  1.1.2.1  thorpej 	 * make that assumption.
     95  1.1.2.1  thorpej 	 */
     96  1.1.2.1  thorpej #if RW_WRITER != 0
     97  1.1.2.1  thorpej #error "RW_WRITER != 0"
     98  1.1.2.1  thorpej #endif
     99  1.1.2.1  thorpej 
    100  1.1.2.1  thorpej 	/*
    101  1.1.2.1  thorpej 	 * We play a slight trick here.  If we're a reader, we want
    102  1.1.2.1  thorpej 	 * increment the read count.  If we're a writer, we want to
    103  1.1.2.1  thorpej 	 * set the owner field and whe WRITE_LOCKED bit.
    104  1.1.2.1  thorpej 	 *
    105  1.1.2.1  thorpej 	 * In the latter case, we expect those bits to be zero,
    106  1.1.2.1  thorpej 	 * therefore we can use an add operation to set them, which
    107  1.1.2.1  thorpej 	 * means an add operation for both cases.
    108  1.1.2.1  thorpej 	 */
    109  1.1.2.1  thorpej 	switch (rw) {
    110  1.1.2.1  thorpej 	case RW_WRITER:
    111  1.1.2.1  thorpej 		incr = ((unsigned long) curproc) | RWLOCK_WRITE_LOCKED;
    112  1.1.2.1  thorpej 		need_wait = RWLOCK_WRITE_LOCKED;
    113  1.1.2.1  thorpej 		set_wait = RWLOCK_HAS_WAITERS | RWLOCK_WRITE_WANTED;
    114  1.1.2.1  thorpej 		break;
    115  1.1.2.1  thorpej 
    116  1.1.2.1  thorpej 	case RW_READER:
    117  1.1.2.1  thorpej 		incr = RWLOCK_READ_INCR;
    118  1.1.2.1  thorpej 		need_wait = RWLOCK_WRITE_LOCKED | RWLOCK_WRITE_WANTED;
    119  1.1.2.1  thorpej 		set_wait = RWLOCK_HAS_WAITERS;
    120  1.1.2.1  thorpej 		break;
    121  1.1.2.1  thorpej #ifdef DIAGNOSTIC
    122  1.1.2.1  thorpej 	default:
    123  1.1.2.1  thorpej 		panic("rw_enter: bad rw %d", rw);
    124  1.1.2.1  thorpej #endif
    125  1.1.2.1  thorpej 	}
    126  1.1.2.1  thorpej 
    127  1.1.2.1  thorpej 	for (;;) {
    128  1.1.2.1  thorpej 		/*
    129  1.1.2.1  thorpej 		 * Read the lock owner field.  If the need-to-wait
    130  1.1.2.1  thorpej 		 * indicator is clear, then try to acquire the lock.
    131  1.1.2.1  thorpej 		 */
    132  1.1.2.1  thorpej 		owner = rwl->rwl_owner;
    133  1.1.2.1  thorpej 		if ((owner & need_wait) == 0) {
    134  1.1.2.1  thorpej 			RWLOCK_ACQUIRE(rwl, owner, owner + incr, tmp);
    135  1.1.2.1  thorpej 			if (tmp == owner) {
    136  1.1.2.1  thorpej 				/* Got it! */
    137  1.1.2.1  thorpej 				break;
    138  1.1.2.1  thorpej 			}
    139  1.1.2.1  thorpej 
    140  1.1.2.1  thorpej 			/*
    141  1.1.2.1  thorpej 			 * Didn't get it -- spin around again (we'll
    142  1.1.2.1  thorpej 			 * probably sleep on the next iteration).
    143  1.1.2.1  thorpej 			 */
    144  1.1.2.1  thorpej 			continue;
    145  1.1.2.1  thorpej 		}
    146  1.1.2.1  thorpej 
    147  1.1.2.1  thorpej 		if (RWLOCK_OWNER(rwl) == curproc)
    148  1.1.2.1  thorpej 			panic("rw_enter: locking against myself");
    149  1.1.2.1  thorpej 
    150  1.1.2.1  thorpej 		ts = turnstile_lookup(rwl);
    151  1.1.2.1  thorpej 
    152  1.1.2.1  thorpej 		/*
    153  1.1.2.1  thorpej 		 * Mark the rwlock as having waiters.  After we do
    154  1.1.2.1  thorpej 		 * this, we need to check one more time if the lock
    155  1.1.2.1  thorpej 		 * is busy, and if not, spin around again.
    156  1.1.2.1  thorpej 		 *
    157  1.1.2.1  thorpej 		 * Note, we also need to spin again if we failed to
    158  1.1.2.1  thorpej 		 * set the has-waiters indicator (which means the
    159  1.1.2.1  thorpej 		 * lock condition changed, but more importantly, we
    160  1.1.2.1  thorpej 		 * need to try and set that indicator again).
    161  1.1.2.1  thorpej 		 */
    162  1.1.2.1  thorpej 		RWLOCK_SET_WAITERS(rwl, need_wait, set_wait);
    163  1.1.2.1  thorpej 		owner = rwl->rwl_owner;
    164  1.1.2.1  thorpej 		if ((owner & need_wait) == 0 || (owner & set_wait) == 0) {
    165  1.1.2.1  thorpej 			turnstile_exit(rwl);
    166  1.1.2.1  thorpej 			continue;
    167  1.1.2.1  thorpej 		}
    168  1.1.2.1  thorpej 		/* XXXJRT p->p_priority */
    169  1.1.2.1  thorpej 		/* XXXJRT Do not currently distinguish reader vs. writer. */
    170  1.1.2.1  thorpej 		(void) turnstile_block(ts, TS_WRITER_Q, p->p_priority, rwl);
    171  1.1.2.1  thorpej 
    172  1.1.2.1  thorpej 		/*
    173  1.1.2.1  thorpej 		 * XXX Solaris Internals says that the Solaris 7
    174  1.1.2.1  thorpej 		 * rwlock implementation does a direct-handoff.  We
    175  1.1.2.1  thorpej 		 * don't implement that yet, but if we did, then a
    176  1.1.2.1  thorpej 		 * thread wakes back up, i.e. arrives here, it would
    177  1.1.2.1  thorpej 		 * hold the lock as requested.
    178  1.1.2.1  thorpej 		 */
    179  1.1.2.1  thorpej 	}
    180  1.1.2.1  thorpej 
    181  1.1.2.1  thorpej 	KASSERT((rw == RW_WRITER && RWLOCK_OWNER(rwl) == curproc) ||
    182  1.1.2.1  thorpej 		(rw == RW_READER && RWLOCK_COUNT(rwl) != 0));
    183  1.1.2.1  thorpej }
    184  1.1.2.1  thorpej 
    185  1.1.2.1  thorpej /*
    186  1.1.2.1  thorpej  * rw_tryenter:
    187  1.1.2.1  thorpej  *
    188  1.1.2.1  thorpej  *	Try to acquire a rwlock.
    189  1.1.2.1  thorpej  */
    190  1.1.2.1  thorpej int
    191  1.1.2.1  thorpej rw_tryenter(krwlock_t *rwl, krw_t rw)
    192  1.1.2.1  thorpej {
    193  1.1.2.1  thorpej 	unsigned long owner, tmp, incr, need_wait;
    194  1.1.2.1  thorpej 
    195  1.1.2.1  thorpej 	switch (rw) {
    196  1.1.2.1  thorpej 	case RW_WRITER:
    197  1.1.2.1  thorpej 		incr = ((unsigned long) curproc) | RWLOCK_WRITE_LOCKED;
    198  1.1.2.1  thorpej 		need_wait = RWLOCK_WRITE_LOCKED;
    199  1.1.2.1  thorpej 		break;
    200  1.1.2.1  thorpej 
    201  1.1.2.1  thorpej 	case RW_READER:
    202  1.1.2.1  thorpej 		incr = RWLOCK_READ_INCR;
    203  1.1.2.1  thorpej 		need_wait = RWLOCK_WRITE_LOCKED | RWLOCK_WRITE_WANTED;
    204  1.1.2.1  thorpej 		break;
    205  1.1.2.1  thorpej #ifdef DIAGNOSTIC
    206  1.1.2.1  thorpej 	default:
    207  1.1.2.1  thorpej 		panic("rw_tryenter: bad rw %d", rw);
    208  1.1.2.1  thorpej #endif
    209  1.1.2.1  thorpej 	}
    210  1.1.2.1  thorpej 
    211  1.1.2.1  thorpej 	for (;;) {
    212  1.1.2.1  thorpej 		owner = rwl->rwl_owner;
    213  1.1.2.1  thorpej 		if ((owner & need_wait) == 0) {
    214  1.1.2.1  thorpej 			RWLOCK_ACQUIRE(rwl, owner, owner + incr, tmp);
    215  1.1.2.1  thorpej 			if (tmp == owner) {
    216  1.1.2.1  thorpej 				/* Got it! */
    217  1.1.2.1  thorpej 				break;
    218  1.1.2.1  thorpej 			}
    219  1.1.2.1  thorpej 			continue;
    220  1.1.2.1  thorpej 		}
    221  1.1.2.1  thorpej 		return (0);
    222  1.1.2.1  thorpej 	}
    223  1.1.2.1  thorpej 
    224  1.1.2.1  thorpej 	KASSERT((rw == RW_WRITER && RWLOCK_OWNER(rwl) == curproc) ||
    225  1.1.2.1  thorpej 		(rw == RW_READER && RWLOCK_COUNT(rwl) != 0));
    226  1.1.2.1  thorpej 	return (1);
    227  1.1.2.1  thorpej }
    228  1.1.2.1  thorpej 
    229  1.1.2.1  thorpej /*
    230  1.1.2.1  thorpej  * rw_exit:
    231  1.1.2.1  thorpej  *
    232  1.1.2.1  thorpej  *	Release a rwlock.
    233  1.1.2.1  thorpej  */
    234  1.1.2.1  thorpej void
    235  1.1.2.1  thorpej rw_exit(krwlock_t *rwl)
    236  1.1.2.1  thorpej {
    237  1.1.2.1  thorpej 	struct turnstile *ts;
    238  1.1.2.1  thorpej 	unsigned long owner, tmp, decr, new;
    239  1.1.2.1  thorpej 
    240  1.1.2.1  thorpej 	/*
    241  1.1.2.1  thorpej 	 * Again, we use a trick.  Since we used an add operation to
    242  1.1.2.1  thorpej 	 * set the required lock bits, we can use a subtract to clear
    243  1.1.2.1  thorpej 	 * them, which makes the read-release and write-release path
    244  1.1.2.1  thorpej 	 * the same.
    245  1.1.2.1  thorpej 	 */
    246  1.1.2.1  thorpej 	if (rwl->rwl_owner & RWLOCK_WRITE_LOCKED) {
    247  1.1.2.1  thorpej 		if (RWLOCK_OWNER(rwl) == NULL)
    248  1.1.2.1  thorpej 			panic("rw_exit: not owned");
    249  1.1.2.1  thorpej 		else
    250  1.1.2.1  thorpej 			panic("rw_exit: not owner, owner = %p, "
    251  1.1.2.1  thorpej 			    "current = %p", RWLOCK_OWNER(rwl), curproc);
    252  1.1.2.1  thorpej 		decr = ((unsigned long) curproc) | RWLOCK_WRITE_LOCKED;
    253  1.1.2.1  thorpej 	} else {
    254  1.1.2.1  thorpej 		if (RWLOCK_COUNT(rwl) == 0)
    255  1.1.2.1  thorpej 			panic("rw_exit: not held\n");
    256  1.1.2.1  thorpej 		decr = RWLOCK_READ_INCR;
    257  1.1.2.1  thorpej 	}
    258  1.1.2.1  thorpej 
    259  1.1.2.1  thorpej 	for (;;) {
    260  1.1.2.1  thorpej 		/*
    261  1.1.2.1  thorpej 		 * Get this lock's turnstile.  This gets the interlock on
    262  1.1.2.1  thorpej 		 * the sleep queue.  Once we have that, we can perform the
    263  1.1.2.1  thorpej 		 * lock release operation.
    264  1.1.2.1  thorpej 		 */
    265  1.1.2.1  thorpej 		ts = turnstile_lookup(rwl);
    266  1.1.2.1  thorpej 
    267  1.1.2.1  thorpej 		/*
    268  1.1.2.1  thorpej 		 * Compute what we expect the new value of the lock
    269  1.1.2.1  thorpej 		 * to be.  Skip the wakeup step if there are no
    270  1.1.2.1  thorpej 		 * appropriate waiters.
    271  1.1.2.1  thorpej 		 */
    272  1.1.2.1  thorpej 		owner = rwl->rwl_owner;
    273  1.1.2.1  thorpej 		new = owner - decr;
    274  1.1.2.1  thorpej 		if ((new & (RWLOCK_THREAD |
    275  1.1.2.1  thorpej 			    RWLOCK_HAS_WAITERS)) != RWLOCK_HAS_WAITERS) {
    276  1.1.2.1  thorpej 			RWLOCK_RELEASE(rwl, owner, new, tmp);
    277  1.1.2.1  thorpej 			if (tmp == owner) {
    278  1.1.2.1  thorpej 				/* Ding! */
    279  1.1.2.1  thorpej 				turnstile_exit(rwl);
    280  1.1.2.1  thorpej 				break;
    281  1.1.2.1  thorpej 			}
    282  1.1.2.1  thorpej 			turnstile_exit(rwl);
    283  1.1.2.1  thorpej 			continue;
    284  1.1.2.1  thorpej 		}
    285  1.1.2.1  thorpej 
    286  1.1.2.1  thorpej 		/* We're about to wake everybody up; clear waiter bits. */
    287  1.1.2.1  thorpej 		new &= ~(RWLOCK_HAS_WAITERS | RWLOCK_WRITE_WANTED);
    288  1.1.2.1  thorpej 
    289  1.1.2.1  thorpej 		RWLOCK_RELEASE(rwl, owner, new, tmp);
    290  1.1.2.1  thorpej 		if (tmp != owner) {
    291  1.1.2.1  thorpej 			/* Oops, try again. */
    292  1.1.2.1  thorpej 			turnstile_exit(rwl);
    293  1.1.2.1  thorpej 			continue;
    294  1.1.2.1  thorpej 		}
    295  1.1.2.1  thorpej 
    296  1.1.2.1  thorpej 		/*
    297  1.1.2.1  thorpej 		 * Wake the thundering herd.
    298  1.1.2.1  thorpej 		 * XXX Should implement direct-handoff.
    299  1.1.2.1  thorpej 		 */
    300  1.1.2.1  thorpej 		KASSERT(ts != NULL);
    301  1.1.2.2  thorpej 		turnstile_wakeup(ts, TS_WRITER_Q,
    302  1.1.2.2  thorpej 		    ts->ts_sleepq[TS_WRITER_Q].tsq_waiters);
    303  1.1.2.1  thorpej 		break;
    304  1.1.2.1  thorpej 	}
    305  1.1.2.1  thorpej }
    306  1.1.2.1  thorpej 
    307  1.1.2.1  thorpej /*
    308  1.1.2.1  thorpej  * rw_downgrade:
    309  1.1.2.1  thorpej  *
    310  1.1.2.1  thorpej  *	Downgrade a write lock to a read lock.
    311  1.1.2.1  thorpej  */
    312  1.1.2.1  thorpej void
    313  1.1.2.1  thorpej rw_downgrade(krwlock_t *rwl)
    314  1.1.2.1  thorpej {
    315  1.1.2.1  thorpej 	struct turnstile *ts;
    316  1.1.2.1  thorpej 	unsigned long owner, tmp;
    317  1.1.2.1  thorpej 
    318  1.1.2.1  thorpej 	if (RWLOCK_OWNER(rwl) != curproc) {
    319  1.1.2.1  thorpej 		if (RWLOCK_OWNER(rwl) == NULL)
    320  1.1.2.1  thorpej 			panic("rw_downgrade: not owned");
    321  1.1.2.1  thorpej 		else
    322  1.1.2.1  thorpej 			panic("rw_downgrade: not owner, owner = %p, "
    323  1.1.2.1  thorpej 			    "current = %p", RWLOCK_OWNER(rwl), curproc);
    324  1.1.2.1  thorpej 	}
    325  1.1.2.1  thorpej 
    326  1.1.2.1  thorpej 	/* XXX This algorithm has to change if we do direct-handoff. */
    327  1.1.2.1  thorpej 	for (;;) {
    328  1.1.2.1  thorpej 		ts = turnstile_lookup(rwl);
    329  1.1.2.1  thorpej 
    330  1.1.2.1  thorpej 		owner = rwl->rwl_owner;
    331  1.1.2.1  thorpej 		RWLOCK_RELEASE(rwl, owner, RWLOCK_READ_INCR, tmp);
    332  1.1.2.1  thorpej 		if (tmp != owner) {
    333  1.1.2.1  thorpej 			/* Oops, try again. */
    334  1.1.2.1  thorpej 			turnstile_exit(rwl);
    335  1.1.2.1  thorpej 			continue;
    336  1.1.2.1  thorpej 		}
    337  1.1.2.1  thorpej 		if (owner & RWLOCK_HAS_WAITERS) {
    338  1.1.2.1  thorpej 			KASSERT(ts != NULL);
    339  1.1.2.2  thorpej 			turnstile_wakeup(ts, TS_WRITER_Q,
    340  1.1.2.2  thorpej 			    ts->ts_sleepq[TS_WRITER_Q].tsq_waiters);
    341  1.1.2.1  thorpej 		}
    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 	KASSERT((rwl->rwl_owner & RWLOCK_WRITE_LOCKED) == 0);
    346  1.1.2.1  thorpej 	KASSERT(RWLOCK_COUNT(rwl) != 0);
    347  1.1.2.1  thorpej }
    348  1.1.2.1  thorpej 
    349  1.1.2.1  thorpej /*
    350  1.1.2.1  thorpej  * rw_tryupgrade:
    351  1.1.2.1  thorpej  *
    352  1.1.2.1  thorpej  *	Try to upgrade an read lock to a write lock.
    353  1.1.2.1  thorpej  */
    354  1.1.2.1  thorpej int
    355  1.1.2.1  thorpej rw_tryupgrade(krwlock_t *rwl)
    356  1.1.2.1  thorpej {
    357  1.1.2.1  thorpej 	unsigned long owner, tmp;
    358  1.1.2.1  thorpej 
    359  1.1.2.1  thorpej 	KASSERT((rwl->rwl_owner & RWLOCK_WRITE_LOCKED) == 0);
    360  1.1.2.1  thorpej 	KASSERT(RWLOCK_COUNT(rwl) != 0);
    361  1.1.2.1  thorpej 
    362  1.1.2.1  thorpej 	for (;;) {
    363  1.1.2.1  thorpej 		/*
    364  1.1.2.1  thorpej 		 * Since we want to favor writers, we don't bother
    365  1.1.2.1  thorpej 		 * checking for waiting writers, we just scarf it.
    366  1.1.2.1  thorpej 		 *
    367  1.1.2.1  thorpej 		 * We must be the only reader.
    368  1.1.2.1  thorpej 		 */
    369  1.1.2.1  thorpej 		owner = rwl->rwl_owner;
    370  1.1.2.1  thorpej 		if ((owner & RWLOCK_THREAD) != RWLOCK_READ_INCR)
    371  1.1.2.1  thorpej 			return (0);
    372  1.1.2.1  thorpej 		RWLOCK_ACQUIRE(rwl, owner,
    373  1.1.2.1  thorpej 		    ((unsigned long) curproc) | RWLOCK_WRITE_LOCKED |
    374  1.1.2.1  thorpej 		    (owner & ~RWLOCK_THREAD), tmp);
    375  1.1.2.1  thorpej 		if (tmp == owner) {
    376  1.1.2.1  thorpej 			/* Ding! */
    377  1.1.2.1  thorpej 			break;
    378  1.1.2.1  thorpej 		}
    379  1.1.2.1  thorpej 	}
    380  1.1.2.1  thorpej 
    381  1.1.2.1  thorpej 	KASSERT(rwl->rwl_owner & RWLOCK_WRITE_LOCKED);
    382  1.1.2.1  thorpej 	KASSERT(RWLOCK_OWNER(rwl) == curproc);
    383  1.1.2.1  thorpej 	return (1);
    384  1.1.2.1  thorpej }
    385  1.1.2.1  thorpej 
    386  1.1.2.1  thorpej /*
    387  1.1.2.1  thorpej  * rw_read_held:
    388  1.1.2.1  thorpej  *
    389  1.1.2.1  thorpej  *	Returns true if the rwlock is held for reading.
    390  1.1.2.1  thorpej  */
    391  1.1.2.1  thorpej int
    392  1.1.2.1  thorpej rw_read_held(krwlock_t *rwl)
    393  1.1.2.1  thorpej {
    394  1.1.2.1  thorpej 	unsigned long owner = rwl->rwl_owner;
    395  1.1.2.1  thorpej 
    396  1.1.2.1  thorpej 	return ((owner & RWLOCK_WRITE_LOCKED) == 0 &&
    397  1.1.2.1  thorpej 	    (owner & RWLOCK_THREAD) != 0);
    398  1.1.2.1  thorpej }
    399  1.1.2.1  thorpej 
    400  1.1.2.1  thorpej /*
    401  1.1.2.1  thorpej  * rw_write_held:
    402  1.1.2.1  thorpej  *
    403  1.1.2.1  thorpej  *	Returns true if the rwlock is held for writing.
    404  1.1.2.1  thorpej  */
    405  1.1.2.1  thorpej int
    406  1.1.2.1  thorpej rw_write_held(krwlock_t *rwl)
    407  1.1.2.1  thorpej {
    408  1.1.2.1  thorpej 	unsigned long owner = rwl->rwl_owner;
    409  1.1.2.1  thorpej 
    410  1.1.2.1  thorpej 	return ((owner & RWLOCK_WRITE_LOCKED) != 0);
    411  1.1.2.1  thorpej }
    412  1.1.2.1  thorpej 
    413  1.1.2.1  thorpej /*
    414  1.1.2.1  thorpej  * rw_read_locked:
    415  1.1.2.1  thorpej  *
    416  1.1.2.1  thorpej  *	Like rw_read_held(), but asserts it.
    417  1.1.2.1  thorpej  */
    418  1.1.2.1  thorpej int
    419  1.1.2.1  thorpej rw_read_locked(krwlock_t *rwl)
    420  1.1.2.1  thorpej {
    421  1.1.2.1  thorpej 	int rv = rw_read_held(rwl);
    422  1.1.2.1  thorpej 
    423  1.1.2.1  thorpej #ifdef DIAGNOSTIC
    424  1.1.2.1  thorpej 	if (rv == 0)
    425  1.1.2.1  thorpej 		panic("rw_read_locked: not held");
    426  1.1.2.1  thorpej #endif
    427  1.1.2.1  thorpej 
    428  1.1.2.1  thorpej 	return (rv);
    429  1.1.2.1  thorpej }
    430  1.1.2.1  thorpej 
    431  1.1.2.1  thorpej /*
    432  1.1.2.1  thorpej  * rw_write_locked:
    433  1.1.2.1  thorpej  *
    434  1.1.2.1  thorpej  *	Like rw_write_held(), but asserts that we hold it.
    435  1.1.2.1  thorpej  */
    436  1.1.2.1  thorpej int
    437  1.1.2.1  thorpej rw_write_locked(krwlock_t *rwl)
    438  1.1.2.1  thorpej {
    439  1.1.2.1  thorpej 	int rv = rw_write_held(rwl);
    440  1.1.2.1  thorpej 
    441  1.1.2.1  thorpej #ifdef DIAGNOSTIC
    442  1.1.2.1  thorpej 	if (rv == 0)
    443  1.1.2.1  thorpej 		panic("rw_write_locked: not held");
    444  1.1.2.1  thorpej 	else if (RWLOCK_OWNER(rwl) != curproc)
    445  1.1.2.1  thorpej 		panic("rw_write_locked: not owner, owner = %p, "
    446  1.1.2.1  thorpej 		    "current = %p", RWLOCK_OWNER(rwl), curproc);
    447  1.1.2.1  thorpej #endif
    448  1.1.2.1  thorpej 
    449  1.1.2.1  thorpej 	return (rv);
    450  1.1.2.1  thorpej }
    451  1.1.2.1  thorpej 
    452  1.1.2.1  thorpej /*
    453  1.1.2.1  thorpej  * rw_owner:
    454  1.1.2.1  thorpej  *
    455  1.1.2.1  thorpej  *	Return the owner of the rwlock.
    456  1.1.2.1  thorpej  */
    457  1.1.2.1  thorpej struct proc *
    458  1.1.2.1  thorpej rw_owner(krwlock_t *rwl)
    459  1.1.2.1  thorpej {
    460  1.1.2.1  thorpej 	unsigned long owner = rwl->rwl_owner;
    461  1.1.2.1  thorpej 
    462  1.1.2.1  thorpej 	return ((owner & RWLOCK_WRITE_LOCKED) ?
    463  1.1.2.1  thorpej 	    ((struct proc *) (owner & RWLOCK_THREAD)) : NULL);
    464  1.1.2.1  thorpej }
    465