xref: /onnv-gate/usr/src/lib/libc/port/threads/rwlock.c (revision 7255:4f2e32b16726)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54570Sraf  * Common Development and Distribution License (the "License").
64570Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
214570Sraf 
220Sstevel@tonic-gate /*
236247Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include "lint.h"
300Sstevel@tonic-gate #include "thr_uberdata.h"
310Sstevel@tonic-gate #include <sys/sdt.h>
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #define	TRY_FLAG		0x10
340Sstevel@tonic-gate #define	READ_LOCK		0
350Sstevel@tonic-gate #define	WRITE_LOCK		1
360Sstevel@tonic-gate #define	READ_LOCK_TRY		(READ_LOCK | TRY_FLAG)
370Sstevel@tonic-gate #define	WRITE_LOCK_TRY		(WRITE_LOCK | TRY_FLAG)
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #define	NLOCKS	4	/* initial number of readlock_t structs allocated */
400Sstevel@tonic-gate 
414570Sraf #define	ASSERT_CONSISTENT_STATE(readers)		\
424570Sraf 	ASSERT(!((readers) & URW_WRITE_LOCKED) ||	\
434570Sraf 		((readers) & ~URW_HAS_WAITERS) == URW_WRITE_LOCKED)
444570Sraf 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  * Find/allocate an entry for rwlp in our array of rwlocks held for reading.
474570Sraf  * We must be deferring signals for this to be safe.
484574Sraf  * Else if we are returning an entry with ul_rdlockcnt == 0,
494570Sraf  * it could be reassigned behind our back in a signal handler.
500Sstevel@tonic-gate  */
510Sstevel@tonic-gate static readlock_t *
520Sstevel@tonic-gate rwl_entry(rwlock_t *rwlp)
530Sstevel@tonic-gate {
540Sstevel@tonic-gate 	ulwp_t *self = curthread;
550Sstevel@tonic-gate 	readlock_t *remembered = NULL;
560Sstevel@tonic-gate 	readlock_t *readlockp;
570Sstevel@tonic-gate 	uint_t nlocks;
580Sstevel@tonic-gate 
594570Sraf 	/* we must be deferring signals */
604570Sraf 	ASSERT((self->ul_critical + self->ul_sigdefer) != 0);
614570Sraf 
624574Sraf 	if ((nlocks = self->ul_rdlockcnt) != 0)
630Sstevel@tonic-gate 		readlockp = self->ul_readlock.array;
640Sstevel@tonic-gate 	else {
650Sstevel@tonic-gate 		nlocks = 1;
660Sstevel@tonic-gate 		readlockp = &self->ul_readlock.single;
670Sstevel@tonic-gate 	}
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	for (; nlocks; nlocks--, readlockp++) {
700Sstevel@tonic-gate 		if (readlockp->rd_rwlock == rwlp)
710Sstevel@tonic-gate 			return (readlockp);
720Sstevel@tonic-gate 		if (readlockp->rd_count == 0 && remembered == NULL)
730Sstevel@tonic-gate 			remembered = readlockp;
740Sstevel@tonic-gate 	}
750Sstevel@tonic-gate 	if (remembered != NULL) {
760Sstevel@tonic-gate 		remembered->rd_rwlock = rwlp;
770Sstevel@tonic-gate 		return (remembered);
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	/*
810Sstevel@tonic-gate 	 * No entry available.  Allocate more space, converting the single
820Sstevel@tonic-gate 	 * readlock_t entry into an array of readlock_t entries if necessary.
830Sstevel@tonic-gate 	 */
844574Sraf 	if ((nlocks = self->ul_rdlockcnt) == 0) {
850Sstevel@tonic-gate 		/*
860Sstevel@tonic-gate 		 * Initial allocation of the readlock_t array.
870Sstevel@tonic-gate 		 * Convert the single entry into an array.
880Sstevel@tonic-gate 		 */
894574Sraf 		self->ul_rdlockcnt = nlocks = NLOCKS;
900Sstevel@tonic-gate 		readlockp = lmalloc(nlocks * sizeof (readlock_t));
910Sstevel@tonic-gate 		/*
920Sstevel@tonic-gate 		 * The single readlock_t becomes the first entry in the array.
930Sstevel@tonic-gate 		 */
940Sstevel@tonic-gate 		*readlockp = self->ul_readlock.single;
950Sstevel@tonic-gate 		self->ul_readlock.single.rd_count = 0;
960Sstevel@tonic-gate 		self->ul_readlock.array = readlockp;
970Sstevel@tonic-gate 		/*
980Sstevel@tonic-gate 		 * Return the next available entry in the array.
990Sstevel@tonic-gate 		 */
1000Sstevel@tonic-gate 		(++readlockp)->rd_rwlock = rwlp;
1010Sstevel@tonic-gate 		return (readlockp);
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 	/*
1040Sstevel@tonic-gate 	 * Reallocate the array, double the size each time.
1050Sstevel@tonic-gate 	 */
1060Sstevel@tonic-gate 	readlockp = lmalloc(nlocks * 2 * sizeof (readlock_t));
1076515Sraf 	(void) memcpy(readlockp, self->ul_readlock.array,
1086247Sraf 	    nlocks * sizeof (readlock_t));
1090Sstevel@tonic-gate 	lfree(self->ul_readlock.array, nlocks * sizeof (readlock_t));
1100Sstevel@tonic-gate 	self->ul_readlock.array = readlockp;
1114574Sraf 	self->ul_rdlockcnt *= 2;
1120Sstevel@tonic-gate 	/*
1130Sstevel@tonic-gate 	 * Return the next available entry in the newly allocated array.
1140Sstevel@tonic-gate 	 */
1150Sstevel@tonic-gate 	(readlockp += nlocks)->rd_rwlock = rwlp;
1160Sstevel@tonic-gate 	return (readlockp);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate  * Free the array of rwlocks held for reading.
1210Sstevel@tonic-gate  */
1220Sstevel@tonic-gate void
1230Sstevel@tonic-gate rwl_free(ulwp_t *ulwp)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate 	uint_t nlocks;
1260Sstevel@tonic-gate 
1274574Sraf 	if ((nlocks = ulwp->ul_rdlockcnt) != 0)
1280Sstevel@tonic-gate 		lfree(ulwp->ul_readlock.array, nlocks * sizeof (readlock_t));
1294574Sraf 	ulwp->ul_rdlockcnt = 0;
1300Sstevel@tonic-gate 	ulwp->ul_readlock.single.rd_rwlock = NULL;
1310Sstevel@tonic-gate 	ulwp->ul_readlock.single.rd_count = 0;
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate /*
1350Sstevel@tonic-gate  * Check if a reader version of the lock is held by the current thread.
1360Sstevel@tonic-gate  */
1376812Sraf #pragma weak _rw_read_held = rw_read_held
1380Sstevel@tonic-gate int
1396812Sraf rw_read_held(rwlock_t *rwlp)
1400Sstevel@tonic-gate {
1414570Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
1424570Sraf 	uint32_t readers;
1434570Sraf 	ulwp_t *self = curthread;
1440Sstevel@tonic-gate 	readlock_t *readlockp;
1450Sstevel@tonic-gate 	uint_t nlocks;
1464570Sraf 	int rval = 0;
1470Sstevel@tonic-gate 
1484570Sraf 	no_preempt(self);
1494570Sraf 
1504570Sraf 	readers = *rwstate;
1514570Sraf 	ASSERT_CONSISTENT_STATE(readers);
1524570Sraf 	if (!(readers & URW_WRITE_LOCKED) &&
1534570Sraf 	    (readers & URW_READERS_MASK) != 0) {
1544570Sraf 		/*
1554570Sraf 		 * The lock is held for reading by some thread.
1564570Sraf 		 * Search our array of rwlocks held for reading for a match.
1574570Sraf 		 */
1584574Sraf 		if ((nlocks = self->ul_rdlockcnt) != 0)
1594570Sraf 			readlockp = self->ul_readlock.array;
1604570Sraf 		else {
1614570Sraf 			nlocks = 1;
1624570Sraf 			readlockp = &self->ul_readlock.single;
1634570Sraf 		}
1644570Sraf 		for (; nlocks; nlocks--, readlockp++) {
1654570Sraf 			if (readlockp->rd_rwlock == rwlp) {
1664570Sraf 				if (readlockp->rd_count)
1674570Sraf 					rval = 1;
1684570Sraf 				break;
1694570Sraf 			}
1704570Sraf 		}
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 
1734570Sraf 	preempt(self);
1744570Sraf 	return (rval);
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate /*
1780Sstevel@tonic-gate  * Check if a writer version of the lock is held by the current thread.
1790Sstevel@tonic-gate  */
1806812Sraf #pragma weak _rw_write_held = rw_write_held
1810Sstevel@tonic-gate int
1826812Sraf rw_write_held(rwlock_t *rwlp)
1830Sstevel@tonic-gate {
1844570Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
1854570Sraf 	uint32_t readers;
1860Sstevel@tonic-gate 	ulwp_t *self = curthread;
1874570Sraf 	int rval;
1884570Sraf 
1894570Sraf 	no_preempt(self);
1900Sstevel@tonic-gate 
1914570Sraf 	readers = *rwstate;
1924570Sraf 	ASSERT_CONSISTENT_STATE(readers);
1934570Sraf 	rval = ((readers & URW_WRITE_LOCKED) &&
1944570Sraf 	    rwlp->rwlock_owner == (uintptr_t)self &&
1954570Sraf 	    (rwlp->rwlock_type == USYNC_THREAD ||
1964570Sraf 	    rwlp->rwlock_ownerpid == self->ul_uberdata->pid));
1970Sstevel@tonic-gate 
1984570Sraf 	preempt(self);
1994570Sraf 	return (rval);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate 
2026812Sraf #pragma weak _rwlock_init = rwlock_init
2030Sstevel@tonic-gate /* ARGSUSED2 */
2040Sstevel@tonic-gate int
2056812Sraf rwlock_init(rwlock_t *rwlp, int type, void *arg)
2060Sstevel@tonic-gate {
207*7255Sraf 	ulwp_t *self = curthread;
208*7255Sraf 
2090Sstevel@tonic-gate 	if (type != USYNC_THREAD && type != USYNC_PROCESS)
2100Sstevel@tonic-gate 		return (EINVAL);
2110Sstevel@tonic-gate 	/*
2120Sstevel@tonic-gate 	 * Once reinitialized, we can no longer be holding a read or write lock.
2130Sstevel@tonic-gate 	 * We can do nothing about other threads that are holding read locks.
2140Sstevel@tonic-gate 	 */
215*7255Sraf 	sigoff(self);
2164570Sraf 	rwl_entry(rwlp)->rd_count = 0;
217*7255Sraf 	sigon(self);
2186515Sraf 	(void) memset(rwlp, 0, sizeof (*rwlp));
2190Sstevel@tonic-gate 	rwlp->rwlock_type = (uint16_t)type;
2200Sstevel@tonic-gate 	rwlp->rwlock_magic = RWL_MAGIC;
2210Sstevel@tonic-gate 	rwlp->mutex.mutex_type = (uint8_t)type;
2220Sstevel@tonic-gate 	rwlp->mutex.mutex_flag = LOCK_INITED;
2230Sstevel@tonic-gate 	rwlp->mutex.mutex_magic = MUTEX_MAGIC;
224*7255Sraf 
225*7255Sraf 	/*
226*7255Sraf 	 * This should be at the beginning of the function,
227*7255Sraf 	 * but for the sake of old broken applications that
228*7255Sraf 	 * do not have proper alignment for their rwlocks
229*7255Sraf 	 * (and don't check the return code from rwlock_init),
230*7255Sraf 	 * we put it here, after initializing the rwlock regardless.
231*7255Sraf 	 */
232*7255Sraf 	if (((uintptr_t)rwlp & (_LONG_LONG_ALIGNMENT - 1)) &&
233*7255Sraf 	    self->ul_misaligned == 0)
234*7255Sraf 		return (EINVAL);
235*7255Sraf 
2360Sstevel@tonic-gate 	return (0);
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate 
2396812Sraf #pragma weak pthread_rwlock_destroy = rwlock_destroy
2406812Sraf #pragma weak _rwlock_destroy = rwlock_destroy
2410Sstevel@tonic-gate int
2426812Sraf rwlock_destroy(rwlock_t *rwlp)
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate 	/*
2450Sstevel@tonic-gate 	 * Once destroyed, we can no longer be holding a read or write lock.
2460Sstevel@tonic-gate 	 * We can do nothing about other threads that are holding read locks.
2470Sstevel@tonic-gate 	 */
2484570Sraf 	sigoff(curthread);
2494570Sraf 	rwl_entry(rwlp)->rd_count = 0;
2504570Sraf 	sigon(curthread);
2510Sstevel@tonic-gate 	rwlp->rwlock_magic = 0;
2520Sstevel@tonic-gate 	tdb_sync_obj_deregister(rwlp);
2530Sstevel@tonic-gate 	return (0);
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate /*
2574570Sraf  * Attempt to acquire a readers lock.  Return true on success.
2584570Sraf  */
2594570Sraf static int
2604570Sraf read_lock_try(rwlock_t *rwlp, int ignore_waiters_flag)
2614570Sraf {
2624570Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
2634570Sraf 	uint32_t mask = ignore_waiters_flag?
2646247Sraf 	    URW_WRITE_LOCKED : (URW_HAS_WAITERS | URW_WRITE_LOCKED);
2654570Sraf 	uint32_t readers;
2664570Sraf 	ulwp_t *self = curthread;
2674570Sraf 
2684570Sraf 	no_preempt(self);
2694570Sraf 	while (((readers = *rwstate) & mask) == 0) {
2704570Sraf 		if (atomic_cas_32(rwstate, readers, readers + 1) == readers) {
2714570Sraf 			preempt(self);
2724570Sraf 			return (1);
2734570Sraf 		}
2744570Sraf 	}
2754570Sraf 	preempt(self);
2764570Sraf 	return (0);
2774570Sraf }
2784570Sraf 
2794570Sraf /*
2804570Sraf  * Attempt to release a reader lock.  Return true on success.
2814570Sraf  */
2824570Sraf static int
2834570Sraf read_unlock_try(rwlock_t *rwlp)
2844570Sraf {
2854570Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
2864570Sraf 	uint32_t readers;
2874570Sraf 	ulwp_t *self = curthread;
2884570Sraf 
2894570Sraf 	no_preempt(self);
2904570Sraf 	while (((readers = *rwstate) & URW_HAS_WAITERS) == 0) {
2914570Sraf 		if (atomic_cas_32(rwstate, readers, readers - 1) == readers) {
2924570Sraf 			preempt(self);
2934570Sraf 			return (1);
2944570Sraf 		}
2954570Sraf 	}
2964570Sraf 	preempt(self);
2974570Sraf 	return (0);
2984570Sraf }
2994570Sraf 
3004570Sraf /*
3014570Sraf  * Attempt to acquire a writer lock.  Return true on success.
3024570Sraf  */
3034570Sraf static int
3044570Sraf write_lock_try(rwlock_t *rwlp, int ignore_waiters_flag)
3054570Sraf {
3064570Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
3074570Sraf 	uint32_t mask = ignore_waiters_flag?
3086247Sraf 	    (URW_WRITE_LOCKED | URW_READERS_MASK) :
3096247Sraf 	    (URW_HAS_WAITERS | URW_WRITE_LOCKED | URW_READERS_MASK);
3104570Sraf 	ulwp_t *self = curthread;
3114570Sraf 	uint32_t readers;
3124570Sraf 
3134570Sraf 	no_preempt(self);
3144570Sraf 	while (((readers = *rwstate) & mask) == 0) {
3154570Sraf 		if (atomic_cas_32(rwstate, readers, readers | URW_WRITE_LOCKED)
3164570Sraf 		    == readers) {
3174570Sraf 			preempt(self);
3184570Sraf 			return (1);
3194570Sraf 		}
3204570Sraf 	}
3214570Sraf 	preempt(self);
3224570Sraf 	return (0);
3234570Sraf }
3244570Sraf 
3254570Sraf /*
3264570Sraf  * Attempt to release a writer lock.  Return true on success.
3274570Sraf  */
3284570Sraf static int
3294570Sraf write_unlock_try(rwlock_t *rwlp)
3304570Sraf {
3314570Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
3324570Sraf 	uint32_t readers;
3334570Sraf 	ulwp_t *self = curthread;
3344570Sraf 
3354570Sraf 	no_preempt(self);
3364570Sraf 	while (((readers = *rwstate) & URW_HAS_WAITERS) == 0) {
3374570Sraf 		if (atomic_cas_32(rwstate, readers, 0) == readers) {
3384570Sraf 			preempt(self);
3394570Sraf 			return (1);
3404570Sraf 		}
3414570Sraf 	}
3424570Sraf 	preempt(self);
3434570Sraf 	return (0);
3444570Sraf }
3454570Sraf 
3464570Sraf /*
3474570Sraf  * Wake up thread(s) sleeping on the rwlock queue and then
3480Sstevel@tonic-gate  * drop the queue lock.  Return non-zero if we wake up someone.
3494570Sraf  * This is called when a thread releases a lock that appears to have waiters.
3500Sstevel@tonic-gate  */
3510Sstevel@tonic-gate static int
3520Sstevel@tonic-gate rw_queue_release(queue_head_t *qp, rwlock_t *rwlp)
3530Sstevel@tonic-gate {
3544570Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
3554570Sraf 	uint32_t readers;
3564570Sraf 	uint32_t writers;
3574570Sraf 	ulwp_t **ulwpp;
3580Sstevel@tonic-gate 	ulwp_t *ulwp;
3596247Sraf 	ulwp_t *prev;
3606247Sraf 	int nlwpid = 0;
3616247Sraf 	int more;
3626247Sraf 	int maxlwps = MAXLWPS;
3634570Sraf 	lwpid_t buffer[MAXLWPS];
3644570Sraf 	lwpid_t *lwpid = buffer;
3654570Sraf 
3664570Sraf 	readers = *rwstate;
3674570Sraf 	ASSERT_CONSISTENT_STATE(readers);
3684570Sraf 	if (!(readers & URW_HAS_WAITERS)) {
3694570Sraf 		queue_unlock(qp);
3704570Sraf 		return (0);
3714570Sraf 	}
3724570Sraf 	readers &= URW_READERS_MASK;
3734570Sraf 	writers = 0;
3740Sstevel@tonic-gate 
3754570Sraf 	/*
3766247Sraf 	 * Examine the queue of waiters in priority order and prepare
3776247Sraf 	 * to wake up as many readers as we encounter before encountering
3786247Sraf 	 * a writer.  If the highest priority thread on the queue is a
3794570Sraf 	 * writer, stop there and wake it up.
3804570Sraf 	 *
3814570Sraf 	 * We keep track of lwpids that are to be unparked in lwpid[].
3824570Sraf 	 * __lwp_unpark_all() is called to unpark all of them after
3834570Sraf 	 * they have been removed from the sleep queue and the sleep
3844570Sraf 	 * queue lock has been dropped.  If we run out of space in our
3854570Sraf 	 * on-stack buffer, we need to allocate more but we can't call
3864570Sraf 	 * lmalloc() because we are holding a queue lock when the overflow
3874570Sraf 	 * occurs and lmalloc() acquires a lock.  We can't use alloca()
3884570Sraf 	 * either because the application may have allocated a small
3894570Sraf 	 * stack and we don't want to overrun the stack.  So we call
3904570Sraf 	 * alloc_lwpids() to allocate a bigger buffer using the mmap()
3914570Sraf 	 * system call directly since that path acquires no locks.
3924570Sraf 	 */
3936247Sraf 	while ((ulwpp = queue_slot(qp, &prev, &more)) != NULL) {
3946247Sraf 		ulwp = *ulwpp;
3956247Sraf 		ASSERT(ulwp->ul_wchan == rwlp);
3964570Sraf 		if (ulwp->ul_writer) {
3974570Sraf 			if (writers != 0 || readers != 0)
3984570Sraf 				break;
3994570Sraf 			/* one writer to wake */
4004570Sraf 			writers++;
4014570Sraf 		} else {
4024570Sraf 			if (writers != 0)
4034570Sraf 				break;
4044570Sraf 			/* at least one reader to wake */
4054570Sraf 			readers++;
4064570Sraf 			if (nlwpid == maxlwps)
4074570Sraf 				lwpid = alloc_lwpids(lwpid, &nlwpid, &maxlwps);
4084570Sraf 		}
4096247Sraf 		queue_unlink(qp, ulwpp, prev);
4106247Sraf 		ulwp->ul_sleepq = NULL;
4116247Sraf 		ulwp->ul_wchan = NULL;
4124570Sraf 		lwpid[nlwpid++] = ulwp->ul_lwpid;
4130Sstevel@tonic-gate 	}
4146247Sraf 	if (ulwpp == NULL)
4154570Sraf 		atomic_and_32(rwstate, ~URW_HAS_WAITERS);
4164570Sraf 	if (nlwpid == 0) {
4174570Sraf 		queue_unlock(qp);
4184570Sraf 	} else {
4196247Sraf 		ulwp_t *self = curthread;
4204570Sraf 		no_preempt(self);
4214570Sraf 		queue_unlock(qp);
4224570Sraf 		if (nlwpid == 1)
4234570Sraf 			(void) __lwp_unpark(lwpid[0]);
4244570Sraf 		else
4254570Sraf 			(void) __lwp_unpark_all(lwpid, nlwpid);
4264570Sraf 		preempt(self);
4274570Sraf 	}
4284570Sraf 	if (lwpid != buffer)
4296515Sraf 		(void) munmap((caddr_t)lwpid, maxlwps * sizeof (lwpid_t));
4304570Sraf 	return (nlwpid != 0);
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate /*
4340Sstevel@tonic-gate  * Common code for rdlock, timedrdlock, wrlock, timedwrlock, tryrdlock,
4350Sstevel@tonic-gate  * and trywrlock for process-shared (USYNC_PROCESS) rwlocks.
4360Sstevel@tonic-gate  *
4370Sstevel@tonic-gate  * Note: if the lock appears to be contended we call __lwp_rwlock_rdlock()
4380Sstevel@tonic-gate  * or __lwp_rwlock_wrlock() holding the mutex. These return with the mutex
4390Sstevel@tonic-gate  * released, and if they need to sleep will release the mutex first. In the
4400Sstevel@tonic-gate  * event of a spurious wakeup, these will return EAGAIN (because it is much
4410Sstevel@tonic-gate  * easier for us to re-acquire the mutex here).
4420Sstevel@tonic-gate  */
4430Sstevel@tonic-gate int
4440Sstevel@tonic-gate shared_rwlock_lock(rwlock_t *rwlp, timespec_t *tsp, int rd_wr)
4450Sstevel@tonic-gate {
4464570Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
4474570Sraf 	mutex_t *mp = &rwlp->mutex;
4484570Sraf 	uint32_t readers;
4494570Sraf 	int try_flag;
4504570Sraf 	int error;
4514570Sraf 
4524570Sraf 	try_flag = (rd_wr & TRY_FLAG);
4534570Sraf 	rd_wr &= ~TRY_FLAG;
4544570Sraf 	ASSERT(rd_wr == READ_LOCK || rd_wr == WRITE_LOCK);
4554570Sraf 
4564570Sraf 	if (!try_flag) {
4574570Sraf 		DTRACE_PROBE2(plockstat, rw__block, rwlp, rd_wr);
4584570Sraf 	}
4594570Sraf 
4604570Sraf 	do {
4614570Sraf 		if (try_flag && (*rwstate & URW_WRITE_LOCKED)) {
4624570Sraf 			error = EBUSY;
4634570Sraf 			break;
4644570Sraf 		}
4656515Sraf 		if ((error = mutex_lock(mp)) != 0)
4664570Sraf 			break;
4674570Sraf 		if (rd_wr == READ_LOCK) {
4684570Sraf 			if (read_lock_try(rwlp, 0)) {
4696515Sraf 				(void) mutex_unlock(mp);
4704570Sraf 				break;
4714570Sraf 			}
4724570Sraf 		} else {
4734570Sraf 			if (write_lock_try(rwlp, 0)) {
4746515Sraf 				(void) mutex_unlock(mp);
4754570Sraf 				break;
4764570Sraf 			}
4774570Sraf 		}
4784570Sraf 		atomic_or_32(rwstate, URW_HAS_WAITERS);
4794570Sraf 		readers = *rwstate;
4804570Sraf 		ASSERT_CONSISTENT_STATE(readers);
4814570Sraf 		/*
4824570Sraf 		 * The calls to __lwp_rwlock_*() below will release the mutex,
4834570Sraf 		 * so we need a dtrace probe here.
4844570Sraf 		 */
4854570Sraf 		mp->mutex_owner = 0;
4864570Sraf 		DTRACE_PROBE2(plockstat, mutex__release, mp, 0);
4874570Sraf 		/*
4884570Sraf 		 * The waiters bit may be inaccurate.
4894570Sraf 		 * Only the kernel knows for sure.
4904570Sraf 		 */
4914570Sraf 		if (rd_wr == READ_LOCK) {
4924570Sraf 			if (try_flag)
4934570Sraf 				error = __lwp_rwlock_tryrdlock(rwlp);
4944570Sraf 			else
4954570Sraf 				error = __lwp_rwlock_rdlock(rwlp, tsp);
4964570Sraf 		} else {
4974570Sraf 			if (try_flag)
4984570Sraf 				error = __lwp_rwlock_trywrlock(rwlp);
4994570Sraf 			else
5004570Sraf 				error = __lwp_rwlock_wrlock(rwlp, tsp);
5014570Sraf 		}
5024570Sraf 	} while (error == EAGAIN || error == EINTR);
5034570Sraf 
5044570Sraf 	if (!try_flag) {
5054570Sraf 		DTRACE_PROBE3(plockstat, rw__blocked, rwlp, rd_wr, error == 0);
5064570Sraf 	}
5074570Sraf 
5084570Sraf 	return (error);
5094570Sraf }
5104570Sraf 
5114570Sraf /*
5124570Sraf  * Common code for rdlock, timedrdlock, wrlock, timedwrlock, tryrdlock,
5134570Sraf  * and trywrlock for process-private (USYNC_THREAD) rwlocks.
5144570Sraf  */
5154570Sraf int
5164570Sraf rwlock_lock(rwlock_t *rwlp, timespec_t *tsp, int rd_wr)
5174570Sraf {
5184570Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
5194570Sraf 	uint32_t readers;
5200Sstevel@tonic-gate 	ulwp_t *self = curthread;
5214570Sraf 	queue_head_t *qp;
5224570Sraf 	ulwp_t *ulwp;
5230Sstevel@tonic-gate 	int try_flag;
5246247Sraf 	int ignore_waiters_flag;
5250Sstevel@tonic-gate 	int error = 0;
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	try_flag = (rd_wr & TRY_FLAG);
5280Sstevel@tonic-gate 	rd_wr &= ~TRY_FLAG;
5290Sstevel@tonic-gate 	ASSERT(rd_wr == READ_LOCK || rd_wr == WRITE_LOCK);
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	if (!try_flag) {
5320Sstevel@tonic-gate 		DTRACE_PROBE2(plockstat, rw__block, rwlp, rd_wr);
5330Sstevel@tonic-gate 	}
5340Sstevel@tonic-gate 
5354570Sraf 	qp = queue_lock(rwlp, MX);
5366247Sraf 	/* initial attempt to acquire the lock fails if there are waiters */
5376247Sraf 	ignore_waiters_flag = 0;
5384570Sraf 	while (error == 0) {
5390Sstevel@tonic-gate 		if (rd_wr == READ_LOCK) {
5406247Sraf 			if (read_lock_try(rwlp, ignore_waiters_flag))
5416247Sraf 				break;
5420Sstevel@tonic-gate 		} else {
5436247Sraf 			if (write_lock_try(rwlp, ignore_waiters_flag))
5446247Sraf 				break;
5450Sstevel@tonic-gate 		}
5466247Sraf 		/* subsequent attempts do not fail due to waiters */
5476247Sraf 		ignore_waiters_flag = 1;
5484570Sraf 		atomic_or_32(rwstate, URW_HAS_WAITERS);
5494570Sraf 		readers = *rwstate;
5504570Sraf 		ASSERT_CONSISTENT_STATE(readers);
5514570Sraf 		if ((readers & URW_WRITE_LOCKED) ||
5524570Sraf 		    (rd_wr == WRITE_LOCK &&
5534570Sraf 		    (readers & URW_READERS_MASK) != 0))
5540Sstevel@tonic-gate 			/* EMPTY */;	/* somebody holds the lock */
5556247Sraf 		else if ((ulwp = queue_waiter(qp)) == NULL) {
5564570Sraf 			atomic_and_32(rwstate, ~URW_HAS_WAITERS);
5576247Sraf 			continue;	/* no queued waiters, try again */
5580Sstevel@tonic-gate 		} else {
5596247Sraf 			/*
5606247Sraf 			 * Do a priority check on the queued waiter (the
5616247Sraf 			 * highest priority thread on the queue) to see
5626247Sraf 			 * if we should defer to him or just grab the lock.
5636247Sraf 			 */
5640Sstevel@tonic-gate 			int our_pri = real_priority(self);
5650Sstevel@tonic-gate 			int his_pri = real_priority(ulwp);
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 			if (rd_wr == WRITE_LOCK) {
5680Sstevel@tonic-gate 				/*
5690Sstevel@tonic-gate 				 * We defer to a queued thread that has
5700Sstevel@tonic-gate 				 * a higher priority than ours.
5710Sstevel@tonic-gate 				 */
5720Sstevel@tonic-gate 				if (his_pri <= our_pri)
5736247Sraf 					continue;	/* try again */
5740Sstevel@tonic-gate 			} else {
5750Sstevel@tonic-gate 				/*
5760Sstevel@tonic-gate 				 * We defer to a queued thread that has
5770Sstevel@tonic-gate 				 * a higher priority than ours or that
5780Sstevel@tonic-gate 				 * is a writer whose priority equals ours.
5790Sstevel@tonic-gate 				 */
5800Sstevel@tonic-gate 				if (his_pri < our_pri ||
5810Sstevel@tonic-gate 				    (his_pri == our_pri && !ulwp->ul_writer))
5826247Sraf 					continue;	/* try again */
5830Sstevel@tonic-gate 			}
5840Sstevel@tonic-gate 		}
5850Sstevel@tonic-gate 		/*
5860Sstevel@tonic-gate 		 * We are about to block.
5870Sstevel@tonic-gate 		 * If we're doing a trylock, return EBUSY instead.
5880Sstevel@tonic-gate 		 */
5890Sstevel@tonic-gate 		if (try_flag) {
5900Sstevel@tonic-gate 			error = EBUSY;
5910Sstevel@tonic-gate 			break;
5920Sstevel@tonic-gate 		}
5930Sstevel@tonic-gate 		/*
5946247Sraf 		 * Enqueue writers ahead of readers.
5950Sstevel@tonic-gate 		 */
5960Sstevel@tonic-gate 		self->ul_writer = rd_wr;	/* *must* be 0 or 1 */
5976247Sraf 		enqueue(qp, self, 0);
5980Sstevel@tonic-gate 		set_parking_flag(self, 1);
5990Sstevel@tonic-gate 		queue_unlock(qp);
6000Sstevel@tonic-gate 		if ((error = __lwp_park(tsp, 0)) == EINTR)
6016247Sraf 			error = ignore_waiters_flag = 0;
6020Sstevel@tonic-gate 		set_parking_flag(self, 0);
6030Sstevel@tonic-gate 		qp = queue_lock(rwlp, MX);
6046247Sraf 		if (self->ul_sleepq && dequeue_self(qp) == 0)
6054570Sraf 			atomic_and_32(rwstate, ~URW_HAS_WAITERS);
6066247Sraf 		self->ul_writer = 0;
6070Sstevel@tonic-gate 	}
6080Sstevel@tonic-gate 
6094570Sraf 	queue_unlock(qp);
6104570Sraf 
6114570Sraf 	if (!try_flag) {
6124570Sraf 		DTRACE_PROBE3(plockstat, rw__blocked, rwlp, rd_wr, error == 0);
6134570Sraf 	}
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	return (error);
6160Sstevel@tonic-gate }
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate int
6190Sstevel@tonic-gate rw_rdlock_impl(rwlock_t *rwlp, timespec_t *tsp)
6200Sstevel@tonic-gate {
6210Sstevel@tonic-gate 	ulwp_t *self = curthread;
6220Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
6230Sstevel@tonic-gate 	readlock_t *readlockp;
6240Sstevel@tonic-gate 	tdb_rwlock_stats_t *rwsp = RWLOCK_STATS(rwlp, udp);
6250Sstevel@tonic-gate 	int error;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	/*
6280Sstevel@tonic-gate 	 * If we already hold a readers lock on this rwlock,
6290Sstevel@tonic-gate 	 * just increment our reference count and return.
6300Sstevel@tonic-gate 	 */
6314570Sraf 	sigoff(self);
6320Sstevel@tonic-gate 	readlockp = rwl_entry(rwlp);
6330Sstevel@tonic-gate 	if (readlockp->rd_count != 0) {
6344570Sraf 		if (readlockp->rd_count == READ_LOCK_MAX) {
6354570Sraf 			sigon(self);
6364570Sraf 			error = EAGAIN;
6374570Sraf 			goto out;
6384570Sraf 		}
6394570Sraf 		sigon(self);
6404570Sraf 		error = 0;
6414570Sraf 		goto out;
6420Sstevel@tonic-gate 	}
6434570Sraf 	sigon(self);
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	/*
6460Sstevel@tonic-gate 	 * If we hold the writer lock, bail out.
6470Sstevel@tonic-gate 	 */
6486812Sraf 	if (rw_write_held(rwlp)) {
6490Sstevel@tonic-gate 		if (self->ul_error_detection)
6500Sstevel@tonic-gate 			rwlock_error(rwlp, "rwlock_rdlock",
6510Sstevel@tonic-gate 			    "calling thread owns the writer lock");
6524570Sraf 		error = EDEADLK;
6534570Sraf 		goto out;
6540Sstevel@tonic-gate 	}
6550Sstevel@tonic-gate 
6564570Sraf 	if (read_lock_try(rwlp, 0))
6574570Sraf 		error = 0;
6584570Sraf 	else if (rwlp->rwlock_type == USYNC_PROCESS)	/* kernel-level */
6590Sstevel@tonic-gate 		error = shared_rwlock_lock(rwlp, tsp, READ_LOCK);
6600Sstevel@tonic-gate 	else						/* user-level */
6610Sstevel@tonic-gate 		error = rwlock_lock(rwlp, tsp, READ_LOCK);
6620Sstevel@tonic-gate 
6634570Sraf out:
6640Sstevel@tonic-gate 	if (error == 0) {
6654570Sraf 		sigoff(self);
6664570Sraf 		rwl_entry(rwlp)->rd_count++;
6674570Sraf 		sigon(self);
6680Sstevel@tonic-gate 		if (rwsp)
6690Sstevel@tonic-gate 			tdb_incr(rwsp->rw_rdlock);
6704570Sraf 		DTRACE_PROBE2(plockstat, rw__acquire, rwlp, READ_LOCK);
6714570Sraf 	} else {
6724570Sraf 		DTRACE_PROBE3(plockstat, rw__error, rwlp, READ_LOCK, error);
6730Sstevel@tonic-gate 	}
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	return (error);
6760Sstevel@tonic-gate }
6770Sstevel@tonic-gate 
6786812Sraf #pragma weak pthread_rwlock_rdlock = rw_rdlock
6796812Sraf #pragma weak _rw_rdlock = rw_rdlock
6800Sstevel@tonic-gate int
6816812Sraf rw_rdlock(rwlock_t *rwlp)
6820Sstevel@tonic-gate {
6830Sstevel@tonic-gate 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
6840Sstevel@tonic-gate 	return (rw_rdlock_impl(rwlp, NULL));
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate void
6880Sstevel@tonic-gate lrw_rdlock(rwlock_t *rwlp)
6890Sstevel@tonic-gate {
6900Sstevel@tonic-gate 	enter_critical(curthread);
6910Sstevel@tonic-gate 	(void) rw_rdlock_impl(rwlp, NULL);
6920Sstevel@tonic-gate }
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate int
6956812Sraf pthread_rwlock_reltimedrdlock_np(pthread_rwlock_t *_RESTRICT_KYWD rwlp,
6966812Sraf     const struct timespec *_RESTRICT_KYWD reltime)
6970Sstevel@tonic-gate {
6980Sstevel@tonic-gate 	timespec_t tslocal = *reltime;
6990Sstevel@tonic-gate 	int error;
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
7026812Sraf 	error = rw_rdlock_impl((rwlock_t *)rwlp, &tslocal);
7030Sstevel@tonic-gate 	if (error == ETIME)
7040Sstevel@tonic-gate 		error = ETIMEDOUT;
7050Sstevel@tonic-gate 	return (error);
7060Sstevel@tonic-gate }
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate int
7096812Sraf pthread_rwlock_timedrdlock(pthread_rwlock_t *_RESTRICT_KYWD rwlp,
7106812Sraf     const struct timespec *_RESTRICT_KYWD abstime)
7110Sstevel@tonic-gate {
7120Sstevel@tonic-gate 	timespec_t tslocal;
7130Sstevel@tonic-gate 	int error;
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
7160Sstevel@tonic-gate 	abstime_to_reltime(CLOCK_REALTIME, abstime, &tslocal);
7176812Sraf 	error = rw_rdlock_impl((rwlock_t *)rwlp, &tslocal);
7180Sstevel@tonic-gate 	if (error == ETIME)
7190Sstevel@tonic-gate 		error = ETIMEDOUT;
7200Sstevel@tonic-gate 	return (error);
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate int
7240Sstevel@tonic-gate rw_wrlock_impl(rwlock_t *rwlp, timespec_t *tsp)
7250Sstevel@tonic-gate {
7260Sstevel@tonic-gate 	ulwp_t *self = curthread;
7270Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
7280Sstevel@tonic-gate 	tdb_rwlock_stats_t *rwsp = RWLOCK_STATS(rwlp, udp);
7290Sstevel@tonic-gate 	int error;
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	/*
7320Sstevel@tonic-gate 	 * If we hold a readers lock on this rwlock, bail out.
7330Sstevel@tonic-gate 	 */
7346812Sraf 	if (rw_read_held(rwlp)) {
7350Sstevel@tonic-gate 		if (self->ul_error_detection)
7360Sstevel@tonic-gate 			rwlock_error(rwlp, "rwlock_wrlock",
7370Sstevel@tonic-gate 			    "calling thread owns the readers lock");
7384570Sraf 		error = EDEADLK;
7394570Sraf 		goto out;
7400Sstevel@tonic-gate 	}
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	/*
7430Sstevel@tonic-gate 	 * If we hold the writer lock, bail out.
7440Sstevel@tonic-gate 	 */
7456812Sraf 	if (rw_write_held(rwlp)) {
7460Sstevel@tonic-gate 		if (self->ul_error_detection)
7470Sstevel@tonic-gate 			rwlock_error(rwlp, "rwlock_wrlock",
7480Sstevel@tonic-gate 			    "calling thread owns the writer lock");
7494570Sraf 		error = EDEADLK;
7504570Sraf 		goto out;
7510Sstevel@tonic-gate 	}
7520Sstevel@tonic-gate 
7534570Sraf 	if (write_lock_try(rwlp, 0))
7544570Sraf 		error = 0;
7554570Sraf 	else if (rwlp->rwlock_type == USYNC_PROCESS)	/* kernel-level */
7560Sstevel@tonic-gate 		error = shared_rwlock_lock(rwlp, tsp, WRITE_LOCK);
7574570Sraf 	else						/* user-level */
7580Sstevel@tonic-gate 		error = rwlock_lock(rwlp, tsp, WRITE_LOCK);
7590Sstevel@tonic-gate 
7604570Sraf out:
7614570Sraf 	if (error == 0) {
7624570Sraf 		rwlp->rwlock_owner = (uintptr_t)self;
7634570Sraf 		if (rwlp->rwlock_type == USYNC_PROCESS)
7644570Sraf 			rwlp->rwlock_ownerpid = udp->pid;
7654570Sraf 		if (rwsp) {
7664570Sraf 			tdb_incr(rwsp->rw_wrlock);
7674570Sraf 			rwsp->rw_wrlock_begin_hold = gethrtime();
7684570Sraf 		}
7694570Sraf 		DTRACE_PROBE2(plockstat, rw__acquire, rwlp, WRITE_LOCK);
7704570Sraf 	} else {
7714570Sraf 		DTRACE_PROBE3(plockstat, rw__error, rwlp, WRITE_LOCK, error);
7720Sstevel@tonic-gate 	}
7730Sstevel@tonic-gate 	return (error);
7740Sstevel@tonic-gate }
7750Sstevel@tonic-gate 
7766812Sraf #pragma weak pthread_rwlock_wrlock = rw_wrlock
7776812Sraf #pragma weak _rw_wrlock = rw_wrlock
7780Sstevel@tonic-gate int
7796812Sraf rw_wrlock(rwlock_t *rwlp)
7800Sstevel@tonic-gate {
7810Sstevel@tonic-gate 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
7820Sstevel@tonic-gate 	return (rw_wrlock_impl(rwlp, NULL));
7830Sstevel@tonic-gate }
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate void
7860Sstevel@tonic-gate lrw_wrlock(rwlock_t *rwlp)
7870Sstevel@tonic-gate {
7880Sstevel@tonic-gate 	enter_critical(curthread);
7890Sstevel@tonic-gate 	(void) rw_wrlock_impl(rwlp, NULL);
7900Sstevel@tonic-gate }
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate int
7936812Sraf pthread_rwlock_reltimedwrlock_np(pthread_rwlock_t *_RESTRICT_KYWD rwlp,
7946812Sraf     const struct timespec *_RESTRICT_KYWD reltime)
7950Sstevel@tonic-gate {
7960Sstevel@tonic-gate 	timespec_t tslocal = *reltime;
7970Sstevel@tonic-gate 	int error;
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
8006812Sraf 	error = rw_wrlock_impl((rwlock_t *)rwlp, &tslocal);
8010Sstevel@tonic-gate 	if (error == ETIME)
8020Sstevel@tonic-gate 		error = ETIMEDOUT;
8030Sstevel@tonic-gate 	return (error);
8040Sstevel@tonic-gate }
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate int
8076812Sraf pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlp, const timespec_t *abstime)
8080Sstevel@tonic-gate {
8090Sstevel@tonic-gate 	timespec_t tslocal;
8100Sstevel@tonic-gate 	int error;
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
8130Sstevel@tonic-gate 	abstime_to_reltime(CLOCK_REALTIME, abstime, &tslocal);
8146812Sraf 	error = rw_wrlock_impl((rwlock_t *)rwlp, &tslocal);
8150Sstevel@tonic-gate 	if (error == ETIME)
8160Sstevel@tonic-gate 		error = ETIMEDOUT;
8170Sstevel@tonic-gate 	return (error);
8180Sstevel@tonic-gate }
8190Sstevel@tonic-gate 
8206812Sraf #pragma weak pthread_rwlock_tryrdlock = rw_tryrdlock
8210Sstevel@tonic-gate int
8226812Sraf rw_tryrdlock(rwlock_t *rwlp)
8230Sstevel@tonic-gate {
8240Sstevel@tonic-gate 	ulwp_t *self = curthread;
8250Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
8260Sstevel@tonic-gate 	tdb_rwlock_stats_t *rwsp = RWLOCK_STATS(rwlp, udp);
8270Sstevel@tonic-gate 	readlock_t *readlockp;
8280Sstevel@tonic-gate 	int error;
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	if (rwsp)
8330Sstevel@tonic-gate 		tdb_incr(rwsp->rw_rdlock_try);
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	/*
8360Sstevel@tonic-gate 	 * If we already hold a readers lock on this rwlock,
8370Sstevel@tonic-gate 	 * just increment our reference count and return.
8380Sstevel@tonic-gate 	 */
8394570Sraf 	sigoff(self);
8400Sstevel@tonic-gate 	readlockp = rwl_entry(rwlp);
8410Sstevel@tonic-gate 	if (readlockp->rd_count != 0) {
8424570Sraf 		if (readlockp->rd_count == READ_LOCK_MAX) {
8434570Sraf 			sigon(self);
8444570Sraf 			error = EAGAIN;
8454570Sraf 			goto out;
8464570Sraf 		}
8474570Sraf 		sigon(self);
8484570Sraf 		error = 0;
8494570Sraf 		goto out;
8500Sstevel@tonic-gate 	}
8514570Sraf 	sigon(self);
8520Sstevel@tonic-gate 
8534570Sraf 	if (read_lock_try(rwlp, 0))
8544570Sraf 		error = 0;
8554570Sraf 	else if (rwlp->rwlock_type == USYNC_PROCESS)	/* kernel-level */
8560Sstevel@tonic-gate 		error = shared_rwlock_lock(rwlp, NULL, READ_LOCK_TRY);
8570Sstevel@tonic-gate 	else						/* user-level */
8580Sstevel@tonic-gate 		error = rwlock_lock(rwlp, NULL, READ_LOCK_TRY);
8590Sstevel@tonic-gate 
8604570Sraf out:
8614570Sraf 	if (error == 0) {
8624570Sraf 		sigoff(self);
8634570Sraf 		rwl_entry(rwlp)->rd_count++;
8644570Sraf 		sigon(self);
8654570Sraf 		DTRACE_PROBE2(plockstat, rw__acquire, rwlp, READ_LOCK);
8664570Sraf 	} else {
8674570Sraf 		if (rwsp)
8684570Sraf 			tdb_incr(rwsp->rw_rdlock_try_fail);
8694570Sraf 		if (error != EBUSY) {
8704570Sraf 			DTRACE_PROBE3(plockstat, rw__error, rwlp, READ_LOCK,
8714570Sraf 			    error);
8724570Sraf 		}
8734570Sraf 	}
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	return (error);
8760Sstevel@tonic-gate }
8770Sstevel@tonic-gate 
8786812Sraf #pragma weak pthread_rwlock_trywrlock = rw_trywrlock
8790Sstevel@tonic-gate int
8806812Sraf rw_trywrlock(rwlock_t *rwlp)
8810Sstevel@tonic-gate {
8820Sstevel@tonic-gate 	ulwp_t *self = curthread;
8830Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
8840Sstevel@tonic-gate 	tdb_rwlock_stats_t *rwsp = RWLOCK_STATS(rwlp, udp);
8850Sstevel@tonic-gate 	int error;
8860Sstevel@tonic-gate 
8874570Sraf 	ASSERT(!self->ul_critical || self->ul_bindflags);
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	if (rwsp)
8900Sstevel@tonic-gate 		tdb_incr(rwsp->rw_wrlock_try);
8910Sstevel@tonic-gate 
8924570Sraf 	if (write_lock_try(rwlp, 0))
8934570Sraf 		error = 0;
8944570Sraf 	else if (rwlp->rwlock_type == USYNC_PROCESS)	/* kernel-level */
8950Sstevel@tonic-gate 		error = shared_rwlock_lock(rwlp, NULL, WRITE_LOCK_TRY);
8964570Sraf 	else						/* user-level */
8970Sstevel@tonic-gate 		error = rwlock_lock(rwlp, NULL, WRITE_LOCK_TRY);
8984570Sraf 
8994570Sraf 	if (error == 0) {
9004570Sraf 		rwlp->rwlock_owner = (uintptr_t)self;
9014570Sraf 		if (rwlp->rwlock_type == USYNC_PROCESS)
9024570Sraf 			rwlp->rwlock_ownerpid = udp->pid;
9034570Sraf 		if (rwsp)
9044570Sraf 			rwsp->rw_wrlock_begin_hold = gethrtime();
9054570Sraf 		DTRACE_PROBE2(plockstat, rw__acquire, rwlp, WRITE_LOCK);
9064570Sraf 	} else {
9074570Sraf 		if (rwsp)
9080Sstevel@tonic-gate 			tdb_incr(rwsp->rw_wrlock_try_fail);
9094570Sraf 		if (error != EBUSY) {
9104570Sraf 			DTRACE_PROBE3(plockstat, rw__error, rwlp, WRITE_LOCK,
9114570Sraf 			    error);
9124570Sraf 		}
9130Sstevel@tonic-gate 	}
9140Sstevel@tonic-gate 	return (error);
9150Sstevel@tonic-gate }
9160Sstevel@tonic-gate 
9176812Sraf #pragma weak pthread_rwlock_unlock = rw_unlock
9186812Sraf #pragma weak _rw_unlock = rw_unlock
9190Sstevel@tonic-gate int
9206812Sraf rw_unlock(rwlock_t *rwlp)
9210Sstevel@tonic-gate {
9224570Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
9234570Sraf 	uint32_t readers;
9240Sstevel@tonic-gate 	ulwp_t *self = curthread;
9250Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
9260Sstevel@tonic-gate 	tdb_rwlock_stats_t *rwsp;
9274570Sraf 	queue_head_t *qp;
9284570Sraf 	int rd_wr;
9294570Sraf 	int waked = 0;
9300Sstevel@tonic-gate 
9314570Sraf 	readers = *rwstate;
9324570Sraf 	ASSERT_CONSISTENT_STATE(readers);
9334570Sraf 	if (readers & URW_WRITE_LOCKED) {
9344570Sraf 		rd_wr = WRITE_LOCK;
9354570Sraf 		readers = 0;
9364570Sraf 	} else {
9374570Sraf 		rd_wr = READ_LOCK;
9384570Sraf 		readers &= URW_READERS_MASK;
9390Sstevel@tonic-gate 	}
9400Sstevel@tonic-gate 
9414570Sraf 	if (rd_wr == WRITE_LOCK) {
9420Sstevel@tonic-gate 		/*
9430Sstevel@tonic-gate 		 * Since the writer lock is held, we'd better be
9440Sstevel@tonic-gate 		 * holding it, else we cannot legitimately be here.
9450Sstevel@tonic-gate 		 */
9466812Sraf 		if (!rw_write_held(rwlp)) {
9470Sstevel@tonic-gate 			if (self->ul_error_detection)
9480Sstevel@tonic-gate 				rwlock_error(rwlp, "rwlock_unlock",
9490Sstevel@tonic-gate 				    "writer lock held, "
9500Sstevel@tonic-gate 				    "but not by the calling thread");
9510Sstevel@tonic-gate 			return (EPERM);
9520Sstevel@tonic-gate 		}
9530Sstevel@tonic-gate 		if ((rwsp = RWLOCK_STATS(rwlp, udp)) != NULL) {
9540Sstevel@tonic-gate 			if (rwsp->rw_wrlock_begin_hold)
9550Sstevel@tonic-gate 				rwsp->rw_wrlock_hold_time +=
9560Sstevel@tonic-gate 				    gethrtime() - rwsp->rw_wrlock_begin_hold;
9570Sstevel@tonic-gate 			rwsp->rw_wrlock_begin_hold = 0;
9580Sstevel@tonic-gate 		}
9594570Sraf 		rwlp->rwlock_owner = 0;
9604570Sraf 		rwlp->rwlock_ownerpid = 0;
9614570Sraf 	} else if (readers > 0) {
9620Sstevel@tonic-gate 		/*
9630Sstevel@tonic-gate 		 * A readers lock is held; if we don't hold one, bail out.
9640Sstevel@tonic-gate 		 */
9654570Sraf 		readlock_t *readlockp;
9664570Sraf 
9674570Sraf 		sigoff(self);
9684570Sraf 		readlockp = rwl_entry(rwlp);
9690Sstevel@tonic-gate 		if (readlockp->rd_count == 0) {
9704570Sraf 			sigon(self);
9710Sstevel@tonic-gate 			if (self->ul_error_detection)
9720Sstevel@tonic-gate 				rwlock_error(rwlp, "rwlock_unlock",
9730Sstevel@tonic-gate 				    "readers lock held, "
9740Sstevel@tonic-gate 				    "but not by the calling thread");
9750Sstevel@tonic-gate 			return (EPERM);
9760Sstevel@tonic-gate 		}
9770Sstevel@tonic-gate 		/*
9780Sstevel@tonic-gate 		 * If we hold more than one readers lock on this rwlock,
9790Sstevel@tonic-gate 		 * just decrement our reference count and return.
9800Sstevel@tonic-gate 		 */
9810Sstevel@tonic-gate 		if (--readlockp->rd_count != 0) {
9824570Sraf 			sigon(self);
9834570Sraf 			goto out;
9840Sstevel@tonic-gate 		}
9854570Sraf 		sigon(self);
9860Sstevel@tonic-gate 	} else {
9870Sstevel@tonic-gate 		/*
9880Sstevel@tonic-gate 		 * This is a usage error.
9890Sstevel@tonic-gate 		 * No thread should release an unowned lock.
9900Sstevel@tonic-gate 		 */
9910Sstevel@tonic-gate 		if (self->ul_error_detection)
9920Sstevel@tonic-gate 			rwlock_error(rwlp, "rwlock_unlock", "lock not owned");
9930Sstevel@tonic-gate 		return (EPERM);
9940Sstevel@tonic-gate 	}
9950Sstevel@tonic-gate 
9964570Sraf 	if (rd_wr == WRITE_LOCK && write_unlock_try(rwlp)) {
9974570Sraf 		/* EMPTY */;
9984570Sraf 	} else if (rd_wr == READ_LOCK && read_unlock_try(rwlp)) {
9994570Sraf 		/* EMPTY */;
10004570Sraf 	} else if (rwlp->rwlock_type == USYNC_PROCESS) {
10016515Sraf 		(void) mutex_lock(&rwlp->mutex);
10024570Sraf 		(void) __lwp_rwlock_unlock(rwlp);
10036515Sraf 		(void) mutex_unlock(&rwlp->mutex);
10044570Sraf 		waked = 1;
10054570Sraf 	} else {
10060Sstevel@tonic-gate 		qp = queue_lock(rwlp, MX);
10074570Sraf 		if (rd_wr == READ_LOCK)
10084570Sraf 			atomic_dec_32(rwstate);
10094570Sraf 		else
10104570Sraf 			atomic_and_32(rwstate, ~URW_WRITE_LOCKED);
10110Sstevel@tonic-gate 		waked = rw_queue_release(qp, rwlp);
10120Sstevel@tonic-gate 	}
10130Sstevel@tonic-gate 
10144570Sraf out:
10154570Sraf 	DTRACE_PROBE2(plockstat, rw__release, rwlp, rd_wr);
10164570Sraf 
10170Sstevel@tonic-gate 	/*
10180Sstevel@tonic-gate 	 * Yield to the thread we just waked up, just in case we might
10190Sstevel@tonic-gate 	 * be about to grab the rwlock again immediately upon return.
10200Sstevel@tonic-gate 	 * This is pretty weak but it helps on a uniprocessor and also
10210Sstevel@tonic-gate 	 * when cpu affinity has assigned both ourself and the other
10220Sstevel@tonic-gate 	 * thread to the same CPU.  Note that lwp_yield() will yield
10230Sstevel@tonic-gate 	 * the processor only if the writer is at the same or higher
10240Sstevel@tonic-gate 	 * priority than ourself.  This provides more balanced program
10250Sstevel@tonic-gate 	 * behavior; it doesn't guarantee acquisition of the lock by
10260Sstevel@tonic-gate 	 * the pending writer.
10270Sstevel@tonic-gate 	 */
10280Sstevel@tonic-gate 	if (waked)
10296515Sraf 		yield();
10300Sstevel@tonic-gate 	return (0);
10310Sstevel@tonic-gate }
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate void
10340Sstevel@tonic-gate lrw_unlock(rwlock_t *rwlp)
10350Sstevel@tonic-gate {
10366812Sraf 	(void) rw_unlock(rwlp);
10370Sstevel@tonic-gate 	exit_critical(curthread);
10380Sstevel@tonic-gate }
1039