xref: /onnv-gate/usr/src/lib/libc/port/threads/rwlock.c (revision 6247:ad4c702ff226)
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 /*
23*6247Sraf  * 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));
1070Sstevel@tonic-gate 	(void) _memcpy(readlockp, self->ul_readlock.array,
108*6247Sraf 	    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  * rw_read_is_held() is private to libc.
1370Sstevel@tonic-gate  */
1380Sstevel@tonic-gate #pragma weak rw_read_is_held = _rw_read_held
1390Sstevel@tonic-gate #pragma weak rw_read_held = _rw_read_held
1400Sstevel@tonic-gate int
1410Sstevel@tonic-gate _rw_read_held(rwlock_t *rwlp)
1420Sstevel@tonic-gate {
1434570Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
1444570Sraf 	uint32_t readers;
1454570Sraf 	ulwp_t *self = curthread;
1460Sstevel@tonic-gate 	readlock_t *readlockp;
1470Sstevel@tonic-gate 	uint_t nlocks;
1484570Sraf 	int rval = 0;
1490Sstevel@tonic-gate 
1504570Sraf 	no_preempt(self);
1514570Sraf 
1524570Sraf 	readers = *rwstate;
1534570Sraf 	ASSERT_CONSISTENT_STATE(readers);
1544570Sraf 	if (!(readers & URW_WRITE_LOCKED) &&
1554570Sraf 	    (readers & URW_READERS_MASK) != 0) {
1564570Sraf 		/*
1574570Sraf 		 * The lock is held for reading by some thread.
1584570Sraf 		 * Search our array of rwlocks held for reading for a match.
1594570Sraf 		 */
1604574Sraf 		if ((nlocks = self->ul_rdlockcnt) != 0)
1614570Sraf 			readlockp = self->ul_readlock.array;
1624570Sraf 		else {
1634570Sraf 			nlocks = 1;
1644570Sraf 			readlockp = &self->ul_readlock.single;
1654570Sraf 		}
1664570Sraf 		for (; nlocks; nlocks--, readlockp++) {
1674570Sraf 			if (readlockp->rd_rwlock == rwlp) {
1684570Sraf 				if (readlockp->rd_count)
1694570Sraf 					rval = 1;
1704570Sraf 				break;
1714570Sraf 			}
1724570Sraf 		}
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate 
1754570Sraf 	preempt(self);
1764570Sraf 	return (rval);
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate /*
1800Sstevel@tonic-gate  * Check if a writer version of the lock is held by the current thread.
1810Sstevel@tonic-gate  * rw_write_is_held() is private to libc.
1820Sstevel@tonic-gate  */
1830Sstevel@tonic-gate #pragma weak rw_write_is_held = _rw_write_held
1840Sstevel@tonic-gate #pragma weak rw_write_held = _rw_write_held
1850Sstevel@tonic-gate int
1860Sstevel@tonic-gate _rw_write_held(rwlock_t *rwlp)
1870Sstevel@tonic-gate {
1884570Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
1894570Sraf 	uint32_t readers;
1900Sstevel@tonic-gate 	ulwp_t *self = curthread;
1914570Sraf 	int rval;
1924570Sraf 
1934570Sraf 	no_preempt(self);
1940Sstevel@tonic-gate 
1954570Sraf 	readers = *rwstate;
1964570Sraf 	ASSERT_CONSISTENT_STATE(readers);
1974570Sraf 	rval = ((readers & URW_WRITE_LOCKED) &&
1984570Sraf 	    rwlp->rwlock_owner == (uintptr_t)self &&
1994570Sraf 	    (rwlp->rwlock_type == USYNC_THREAD ||
2004570Sraf 	    rwlp->rwlock_ownerpid == self->ul_uberdata->pid));
2010Sstevel@tonic-gate 
2024570Sraf 	preempt(self);
2034570Sraf 	return (rval);
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate #pragma weak rwlock_init = __rwlock_init
2070Sstevel@tonic-gate #pragma weak _rwlock_init = __rwlock_init
2080Sstevel@tonic-gate /* ARGSUSED2 */
2090Sstevel@tonic-gate int
2100Sstevel@tonic-gate __rwlock_init(rwlock_t *rwlp, int type, void *arg)
2110Sstevel@tonic-gate {
2120Sstevel@tonic-gate 	if (type != USYNC_THREAD && type != USYNC_PROCESS)
2130Sstevel@tonic-gate 		return (EINVAL);
2140Sstevel@tonic-gate 	/*
2150Sstevel@tonic-gate 	 * Once reinitialized, we can no longer be holding a read or write lock.
2160Sstevel@tonic-gate 	 * We can do nothing about other threads that are holding read locks.
2170Sstevel@tonic-gate 	 */
2184570Sraf 	sigoff(curthread);
2194570Sraf 	rwl_entry(rwlp)->rd_count = 0;
2204570Sraf 	sigon(curthread);
2210Sstevel@tonic-gate 	(void) _memset(rwlp, 0, sizeof (*rwlp));
2220Sstevel@tonic-gate 	rwlp->rwlock_type = (uint16_t)type;
2230Sstevel@tonic-gate 	rwlp->rwlock_magic = RWL_MAGIC;
2240Sstevel@tonic-gate 	rwlp->mutex.mutex_type = (uint8_t)type;
2250Sstevel@tonic-gate 	rwlp->mutex.mutex_flag = LOCK_INITED;
2260Sstevel@tonic-gate 	rwlp->mutex.mutex_magic = MUTEX_MAGIC;
2270Sstevel@tonic-gate 	return (0);
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate #pragma weak rwlock_destroy = __rwlock_destroy
2310Sstevel@tonic-gate #pragma weak _rwlock_destroy = __rwlock_destroy
2320Sstevel@tonic-gate #pragma weak pthread_rwlock_destroy = __rwlock_destroy
2330Sstevel@tonic-gate #pragma weak _pthread_rwlock_destroy = __rwlock_destroy
2340Sstevel@tonic-gate int
2350Sstevel@tonic-gate __rwlock_destroy(rwlock_t *rwlp)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate 	/*
2380Sstevel@tonic-gate 	 * Once destroyed, we can no longer be holding a read or write lock.
2390Sstevel@tonic-gate 	 * We can do nothing about other threads that are holding read locks.
2400Sstevel@tonic-gate 	 */
2414570Sraf 	sigoff(curthread);
2424570Sraf 	rwl_entry(rwlp)->rd_count = 0;
2434570Sraf 	sigon(curthread);
2440Sstevel@tonic-gate 	rwlp->rwlock_magic = 0;
2450Sstevel@tonic-gate 	tdb_sync_obj_deregister(rwlp);
2460Sstevel@tonic-gate 	return (0);
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate /*
2504570Sraf  * Attempt to acquire a readers lock.  Return true on success.
2514570Sraf  */
2524570Sraf static int
2534570Sraf read_lock_try(rwlock_t *rwlp, int ignore_waiters_flag)
2544570Sraf {
2554570Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
2564570Sraf 	uint32_t mask = ignore_waiters_flag?
257*6247Sraf 	    URW_WRITE_LOCKED : (URW_HAS_WAITERS | URW_WRITE_LOCKED);
2584570Sraf 	uint32_t readers;
2594570Sraf 	ulwp_t *self = curthread;
2604570Sraf 
2614570Sraf 	no_preempt(self);
2624570Sraf 	while (((readers = *rwstate) & mask) == 0) {
2634570Sraf 		if (atomic_cas_32(rwstate, readers, readers + 1) == readers) {
2644570Sraf 			preempt(self);
2654570Sraf 			return (1);
2664570Sraf 		}
2674570Sraf 	}
2684570Sraf 	preempt(self);
2694570Sraf 	return (0);
2704570Sraf }
2714570Sraf 
2724570Sraf /*
2734570Sraf  * Attempt to release a reader lock.  Return true on success.
2744570Sraf  */
2754570Sraf static int
2764570Sraf read_unlock_try(rwlock_t *rwlp)
2774570Sraf {
2784570Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
2794570Sraf 	uint32_t readers;
2804570Sraf 	ulwp_t *self = curthread;
2814570Sraf 
2824570Sraf 	no_preempt(self);
2834570Sraf 	while (((readers = *rwstate) & URW_HAS_WAITERS) == 0) {
2844570Sraf 		if (atomic_cas_32(rwstate, readers, readers - 1) == readers) {
2854570Sraf 			preempt(self);
2864570Sraf 			return (1);
2874570Sraf 		}
2884570Sraf 	}
2894570Sraf 	preempt(self);
2904570Sraf 	return (0);
2914570Sraf }
2924570Sraf 
2934570Sraf /*
2944570Sraf  * Attempt to acquire a writer lock.  Return true on success.
2954570Sraf  */
2964570Sraf static int
2974570Sraf write_lock_try(rwlock_t *rwlp, int ignore_waiters_flag)
2984570Sraf {
2994570Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
3004570Sraf 	uint32_t mask = ignore_waiters_flag?
301*6247Sraf 	    (URW_WRITE_LOCKED | URW_READERS_MASK) :
302*6247Sraf 	    (URW_HAS_WAITERS | URW_WRITE_LOCKED | URW_READERS_MASK);
3034570Sraf 	ulwp_t *self = curthread;
3044570Sraf 	uint32_t readers;
3054570Sraf 
3064570Sraf 	no_preempt(self);
3074570Sraf 	while (((readers = *rwstate) & mask) == 0) {
3084570Sraf 		if (atomic_cas_32(rwstate, readers, readers | URW_WRITE_LOCKED)
3094570Sraf 		    == readers) {
3104570Sraf 			preempt(self);
3114570Sraf 			return (1);
3124570Sraf 		}
3134570Sraf 	}
3144570Sraf 	preempt(self);
3154570Sraf 	return (0);
3164570Sraf }
3174570Sraf 
3184570Sraf /*
3194570Sraf  * Attempt to release a writer lock.  Return true on success.
3204570Sraf  */
3214570Sraf static int
3224570Sraf write_unlock_try(rwlock_t *rwlp)
3234570Sraf {
3244570Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
3254570Sraf 	uint32_t readers;
3264570Sraf 	ulwp_t *self = curthread;
3274570Sraf 
3284570Sraf 	no_preempt(self);
3294570Sraf 	while (((readers = *rwstate) & URW_HAS_WAITERS) == 0) {
3304570Sraf 		if (atomic_cas_32(rwstate, readers, 0) == readers) {
3314570Sraf 			preempt(self);
3324570Sraf 			return (1);
3334570Sraf 		}
3344570Sraf 	}
3354570Sraf 	preempt(self);
3364570Sraf 	return (0);
3374570Sraf }
3384570Sraf 
3394570Sraf /*
3404570Sraf  * Wake up thread(s) sleeping on the rwlock queue and then
3410Sstevel@tonic-gate  * drop the queue lock.  Return non-zero if we wake up someone.
3424570Sraf  * This is called when a thread releases a lock that appears to have waiters.
3430Sstevel@tonic-gate  */
3440Sstevel@tonic-gate static int
3450Sstevel@tonic-gate rw_queue_release(queue_head_t *qp, rwlock_t *rwlp)
3460Sstevel@tonic-gate {
3474570Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
3484570Sraf 	uint32_t readers;
3494570Sraf 	uint32_t writers;
3504570Sraf 	ulwp_t **ulwpp;
3510Sstevel@tonic-gate 	ulwp_t *ulwp;
352*6247Sraf 	ulwp_t *prev;
353*6247Sraf 	int nlwpid = 0;
354*6247Sraf 	int more;
355*6247Sraf 	int maxlwps = MAXLWPS;
3564570Sraf 	lwpid_t buffer[MAXLWPS];
3574570Sraf 	lwpid_t *lwpid = buffer;
3584570Sraf 
3594570Sraf 	readers = *rwstate;
3604570Sraf 	ASSERT_CONSISTENT_STATE(readers);
3614570Sraf 	if (!(readers & URW_HAS_WAITERS)) {
3624570Sraf 		queue_unlock(qp);
3634570Sraf 		return (0);
3644570Sraf 	}
3654570Sraf 	readers &= URW_READERS_MASK;
3664570Sraf 	writers = 0;
3670Sstevel@tonic-gate 
3684570Sraf 	/*
369*6247Sraf 	 * Examine the queue of waiters in priority order and prepare
370*6247Sraf 	 * to wake up as many readers as we encounter before encountering
371*6247Sraf 	 * a writer.  If the highest priority thread on the queue is a
3724570Sraf 	 * writer, stop there and wake it up.
3734570Sraf 	 *
3744570Sraf 	 * We keep track of lwpids that are to be unparked in lwpid[].
3754570Sraf 	 * __lwp_unpark_all() is called to unpark all of them after
3764570Sraf 	 * they have been removed from the sleep queue and the sleep
3774570Sraf 	 * queue lock has been dropped.  If we run out of space in our
3784570Sraf 	 * on-stack buffer, we need to allocate more but we can't call
3794570Sraf 	 * lmalloc() because we are holding a queue lock when the overflow
3804570Sraf 	 * occurs and lmalloc() acquires a lock.  We can't use alloca()
3814570Sraf 	 * either because the application may have allocated a small
3824570Sraf 	 * stack and we don't want to overrun the stack.  So we call
3834570Sraf 	 * alloc_lwpids() to allocate a bigger buffer using the mmap()
3844570Sraf 	 * system call directly since that path acquires no locks.
3854570Sraf 	 */
386*6247Sraf 	while ((ulwpp = queue_slot(qp, &prev, &more)) != NULL) {
387*6247Sraf 		ulwp = *ulwpp;
388*6247Sraf 		ASSERT(ulwp->ul_wchan == rwlp);
3894570Sraf 		if (ulwp->ul_writer) {
3904570Sraf 			if (writers != 0 || readers != 0)
3914570Sraf 				break;
3924570Sraf 			/* one writer to wake */
3934570Sraf 			writers++;
3944570Sraf 		} else {
3954570Sraf 			if (writers != 0)
3964570Sraf 				break;
3974570Sraf 			/* at least one reader to wake */
3984570Sraf 			readers++;
3994570Sraf 			if (nlwpid == maxlwps)
4004570Sraf 				lwpid = alloc_lwpids(lwpid, &nlwpid, &maxlwps);
4014570Sraf 		}
402*6247Sraf 		queue_unlink(qp, ulwpp, prev);
403*6247Sraf 		ulwp->ul_sleepq = NULL;
404*6247Sraf 		ulwp->ul_wchan = NULL;
4054570Sraf 		lwpid[nlwpid++] = ulwp->ul_lwpid;
4060Sstevel@tonic-gate 	}
407*6247Sraf 	if (ulwpp == NULL)
4084570Sraf 		atomic_and_32(rwstate, ~URW_HAS_WAITERS);
4094570Sraf 	if (nlwpid == 0) {
4104570Sraf 		queue_unlock(qp);
4114570Sraf 	} else {
412*6247Sraf 		ulwp_t *self = curthread;
4134570Sraf 		no_preempt(self);
4144570Sraf 		queue_unlock(qp);
4154570Sraf 		if (nlwpid == 1)
4164570Sraf 			(void) __lwp_unpark(lwpid[0]);
4174570Sraf 		else
4184570Sraf 			(void) __lwp_unpark_all(lwpid, nlwpid);
4194570Sraf 		preempt(self);
4204570Sraf 	}
4214570Sraf 	if (lwpid != buffer)
4224570Sraf 		(void) _private_munmap(lwpid, maxlwps * sizeof (lwpid_t));
4234570Sraf 	return (nlwpid != 0);
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate /*
4270Sstevel@tonic-gate  * Common code for rdlock, timedrdlock, wrlock, timedwrlock, tryrdlock,
4280Sstevel@tonic-gate  * and trywrlock for process-shared (USYNC_PROCESS) rwlocks.
4290Sstevel@tonic-gate  *
4300Sstevel@tonic-gate  * Note: if the lock appears to be contended we call __lwp_rwlock_rdlock()
4310Sstevel@tonic-gate  * or __lwp_rwlock_wrlock() holding the mutex. These return with the mutex
4320Sstevel@tonic-gate  * released, and if they need to sleep will release the mutex first. In the
4330Sstevel@tonic-gate  * event of a spurious wakeup, these will return EAGAIN (because it is much
4340Sstevel@tonic-gate  * easier for us to re-acquire the mutex here).
4350Sstevel@tonic-gate  */
4360Sstevel@tonic-gate int
4370Sstevel@tonic-gate shared_rwlock_lock(rwlock_t *rwlp, timespec_t *tsp, int rd_wr)
4380Sstevel@tonic-gate {
4394570Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
4404570Sraf 	mutex_t *mp = &rwlp->mutex;
4414570Sraf 	uint32_t readers;
4424570Sraf 	int try_flag;
4434570Sraf 	int error;
4444570Sraf 
4454570Sraf 	try_flag = (rd_wr & TRY_FLAG);
4464570Sraf 	rd_wr &= ~TRY_FLAG;
4474570Sraf 	ASSERT(rd_wr == READ_LOCK || rd_wr == WRITE_LOCK);
4484570Sraf 
4494570Sraf 	if (!try_flag) {
4504570Sraf 		DTRACE_PROBE2(plockstat, rw__block, rwlp, rd_wr);
4514570Sraf 	}
4524570Sraf 
4534570Sraf 	do {
4544570Sraf 		if (try_flag && (*rwstate & URW_WRITE_LOCKED)) {
4554570Sraf 			error = EBUSY;
4564570Sraf 			break;
4574570Sraf 		}
4584570Sraf 		if ((error = _private_mutex_lock(mp)) != 0)
4594570Sraf 			break;
4604570Sraf 		if (rd_wr == READ_LOCK) {
4614570Sraf 			if (read_lock_try(rwlp, 0)) {
4624570Sraf 				(void) _private_mutex_unlock(mp);
4634570Sraf 				break;
4644570Sraf 			}
4654570Sraf 		} else {
4664570Sraf 			if (write_lock_try(rwlp, 0)) {
4674570Sraf 				(void) _private_mutex_unlock(mp);
4684570Sraf 				break;
4694570Sraf 			}
4704570Sraf 		}
4714570Sraf 		atomic_or_32(rwstate, URW_HAS_WAITERS);
4724570Sraf 		readers = *rwstate;
4734570Sraf 		ASSERT_CONSISTENT_STATE(readers);
4744570Sraf 		/*
4754570Sraf 		 * The calls to __lwp_rwlock_*() below will release the mutex,
4764570Sraf 		 * so we need a dtrace probe here.
4774570Sraf 		 */
4784570Sraf 		mp->mutex_owner = 0;
4794570Sraf 		DTRACE_PROBE2(plockstat, mutex__release, mp, 0);
4804570Sraf 		/*
4814570Sraf 		 * The waiters bit may be inaccurate.
4824570Sraf 		 * Only the kernel knows for sure.
4834570Sraf 		 */
4844570Sraf 		if (rd_wr == READ_LOCK) {
4854570Sraf 			if (try_flag)
4864570Sraf 				error = __lwp_rwlock_tryrdlock(rwlp);
4874570Sraf 			else
4884570Sraf 				error = __lwp_rwlock_rdlock(rwlp, tsp);
4894570Sraf 		} else {
4904570Sraf 			if (try_flag)
4914570Sraf 				error = __lwp_rwlock_trywrlock(rwlp);
4924570Sraf 			else
4934570Sraf 				error = __lwp_rwlock_wrlock(rwlp, tsp);
4944570Sraf 		}
4954570Sraf 	} while (error == EAGAIN || error == EINTR);
4964570Sraf 
4974570Sraf 	if (!try_flag) {
4984570Sraf 		DTRACE_PROBE3(plockstat, rw__blocked, rwlp, rd_wr, error == 0);
4994570Sraf 	}
5004570Sraf 
5014570Sraf 	return (error);
5024570Sraf }
5034570Sraf 
5044570Sraf /*
5054570Sraf  * Common code for rdlock, timedrdlock, wrlock, timedwrlock, tryrdlock,
5064570Sraf  * and trywrlock for process-private (USYNC_THREAD) rwlocks.
5074570Sraf  */
5084570Sraf int
5094570Sraf rwlock_lock(rwlock_t *rwlp, timespec_t *tsp, int rd_wr)
5104570Sraf {
5114570Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
5124570Sraf 	uint32_t readers;
5130Sstevel@tonic-gate 	ulwp_t *self = curthread;
5144570Sraf 	queue_head_t *qp;
5154570Sraf 	ulwp_t *ulwp;
5160Sstevel@tonic-gate 	int try_flag;
517*6247Sraf 	int ignore_waiters_flag;
5180Sstevel@tonic-gate 	int error = 0;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	try_flag = (rd_wr & TRY_FLAG);
5210Sstevel@tonic-gate 	rd_wr &= ~TRY_FLAG;
5220Sstevel@tonic-gate 	ASSERT(rd_wr == READ_LOCK || rd_wr == WRITE_LOCK);
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	if (!try_flag) {
5250Sstevel@tonic-gate 		DTRACE_PROBE2(plockstat, rw__block, rwlp, rd_wr);
5260Sstevel@tonic-gate 	}
5270Sstevel@tonic-gate 
5284570Sraf 	qp = queue_lock(rwlp, MX);
529*6247Sraf 	/* initial attempt to acquire the lock fails if there are waiters */
530*6247Sraf 	ignore_waiters_flag = 0;
5314570Sraf 	while (error == 0) {
5320Sstevel@tonic-gate 		if (rd_wr == READ_LOCK) {
533*6247Sraf 			if (read_lock_try(rwlp, ignore_waiters_flag))
534*6247Sraf 				break;
5350Sstevel@tonic-gate 		} else {
536*6247Sraf 			if (write_lock_try(rwlp, ignore_waiters_flag))
537*6247Sraf 				break;
5380Sstevel@tonic-gate 		}
539*6247Sraf 		/* subsequent attempts do not fail due to waiters */
540*6247Sraf 		ignore_waiters_flag = 1;
5414570Sraf 		atomic_or_32(rwstate, URW_HAS_WAITERS);
5424570Sraf 		readers = *rwstate;
5434570Sraf 		ASSERT_CONSISTENT_STATE(readers);
5444570Sraf 		if ((readers & URW_WRITE_LOCKED) ||
5454570Sraf 		    (rd_wr == WRITE_LOCK &&
5464570Sraf 		    (readers & URW_READERS_MASK) != 0))
5470Sstevel@tonic-gate 			/* EMPTY */;	/* somebody holds the lock */
548*6247Sraf 		else if ((ulwp = queue_waiter(qp)) == NULL) {
5494570Sraf 			atomic_and_32(rwstate, ~URW_HAS_WAITERS);
550*6247Sraf 			continue;	/* no queued waiters, try again */
5510Sstevel@tonic-gate 		} else {
552*6247Sraf 			/*
553*6247Sraf 			 * Do a priority check on the queued waiter (the
554*6247Sraf 			 * highest priority thread on the queue) to see
555*6247Sraf 			 * if we should defer to him or just grab the lock.
556*6247Sraf 			 */
5570Sstevel@tonic-gate 			int our_pri = real_priority(self);
5580Sstevel@tonic-gate 			int his_pri = real_priority(ulwp);
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 			if (rd_wr == WRITE_LOCK) {
5610Sstevel@tonic-gate 				/*
5620Sstevel@tonic-gate 				 * We defer to a queued thread that has
5630Sstevel@tonic-gate 				 * a higher priority than ours.
5640Sstevel@tonic-gate 				 */
5650Sstevel@tonic-gate 				if (his_pri <= our_pri)
566*6247Sraf 					continue;	/* try again */
5670Sstevel@tonic-gate 			} else {
5680Sstevel@tonic-gate 				/*
5690Sstevel@tonic-gate 				 * We defer to a queued thread that has
5700Sstevel@tonic-gate 				 * a higher priority than ours or that
5710Sstevel@tonic-gate 				 * is a writer whose priority equals ours.
5720Sstevel@tonic-gate 				 */
5730Sstevel@tonic-gate 				if (his_pri < our_pri ||
5740Sstevel@tonic-gate 				    (his_pri == our_pri && !ulwp->ul_writer))
575*6247Sraf 					continue;	/* try again */
5760Sstevel@tonic-gate 			}
5770Sstevel@tonic-gate 		}
5780Sstevel@tonic-gate 		/*
5790Sstevel@tonic-gate 		 * We are about to block.
5800Sstevel@tonic-gate 		 * If we're doing a trylock, return EBUSY instead.
5810Sstevel@tonic-gate 		 */
5820Sstevel@tonic-gate 		if (try_flag) {
5830Sstevel@tonic-gate 			error = EBUSY;
5840Sstevel@tonic-gate 			break;
5850Sstevel@tonic-gate 		}
5860Sstevel@tonic-gate 		/*
587*6247Sraf 		 * Enqueue writers ahead of readers.
5880Sstevel@tonic-gate 		 */
5890Sstevel@tonic-gate 		self->ul_writer = rd_wr;	/* *must* be 0 or 1 */
590*6247Sraf 		enqueue(qp, self, 0);
5910Sstevel@tonic-gate 		set_parking_flag(self, 1);
5920Sstevel@tonic-gate 		queue_unlock(qp);
5930Sstevel@tonic-gate 		if ((error = __lwp_park(tsp, 0)) == EINTR)
594*6247Sraf 			error = ignore_waiters_flag = 0;
5950Sstevel@tonic-gate 		set_parking_flag(self, 0);
5960Sstevel@tonic-gate 		qp = queue_lock(rwlp, MX);
597*6247Sraf 		if (self->ul_sleepq && dequeue_self(qp) == 0)
5984570Sraf 			atomic_and_32(rwstate, ~URW_HAS_WAITERS);
599*6247Sraf 		self->ul_writer = 0;
6000Sstevel@tonic-gate 	}
6010Sstevel@tonic-gate 
6024570Sraf 	queue_unlock(qp);
6034570Sraf 
6044570Sraf 	if (!try_flag) {
6054570Sraf 		DTRACE_PROBE3(plockstat, rw__blocked, rwlp, rd_wr, error == 0);
6064570Sraf 	}
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	return (error);
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate int
6120Sstevel@tonic-gate rw_rdlock_impl(rwlock_t *rwlp, timespec_t *tsp)
6130Sstevel@tonic-gate {
6140Sstevel@tonic-gate 	ulwp_t *self = curthread;
6150Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
6160Sstevel@tonic-gate 	readlock_t *readlockp;
6170Sstevel@tonic-gate 	tdb_rwlock_stats_t *rwsp = RWLOCK_STATS(rwlp, udp);
6180Sstevel@tonic-gate 	int error;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	/*
6210Sstevel@tonic-gate 	 * If we already hold a readers lock on this rwlock,
6220Sstevel@tonic-gate 	 * just increment our reference count and return.
6230Sstevel@tonic-gate 	 */
6244570Sraf 	sigoff(self);
6250Sstevel@tonic-gate 	readlockp = rwl_entry(rwlp);
6260Sstevel@tonic-gate 	if (readlockp->rd_count != 0) {
6274570Sraf 		if (readlockp->rd_count == READ_LOCK_MAX) {
6284570Sraf 			sigon(self);
6294570Sraf 			error = EAGAIN;
6304570Sraf 			goto out;
6314570Sraf 		}
6324570Sraf 		sigon(self);
6334570Sraf 		error = 0;
6344570Sraf 		goto out;
6350Sstevel@tonic-gate 	}
6364570Sraf 	sigon(self);
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	/*
6390Sstevel@tonic-gate 	 * If we hold the writer lock, bail out.
6400Sstevel@tonic-gate 	 */
6410Sstevel@tonic-gate 	if (rw_write_is_held(rwlp)) {
6420Sstevel@tonic-gate 		if (self->ul_error_detection)
6430Sstevel@tonic-gate 			rwlock_error(rwlp, "rwlock_rdlock",
6440Sstevel@tonic-gate 			    "calling thread owns the writer lock");
6454570Sraf 		error = EDEADLK;
6464570Sraf 		goto out;
6470Sstevel@tonic-gate 	}
6480Sstevel@tonic-gate 
6494570Sraf 	if (read_lock_try(rwlp, 0))
6504570Sraf 		error = 0;
6514570Sraf 	else if (rwlp->rwlock_type == USYNC_PROCESS)	/* kernel-level */
6520Sstevel@tonic-gate 		error = shared_rwlock_lock(rwlp, tsp, READ_LOCK);
6530Sstevel@tonic-gate 	else						/* user-level */
6540Sstevel@tonic-gate 		error = rwlock_lock(rwlp, tsp, READ_LOCK);
6550Sstevel@tonic-gate 
6564570Sraf out:
6570Sstevel@tonic-gate 	if (error == 0) {
6584570Sraf 		sigoff(self);
6594570Sraf 		rwl_entry(rwlp)->rd_count++;
6604570Sraf 		sigon(self);
6610Sstevel@tonic-gate 		if (rwsp)
6620Sstevel@tonic-gate 			tdb_incr(rwsp->rw_rdlock);
6634570Sraf 		DTRACE_PROBE2(plockstat, rw__acquire, rwlp, READ_LOCK);
6644570Sraf 	} else {
6654570Sraf 		DTRACE_PROBE3(plockstat, rw__error, rwlp, READ_LOCK, error);
6660Sstevel@tonic-gate 	}
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	return (error);
6690Sstevel@tonic-gate }
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate #pragma weak rw_rdlock = __rw_rdlock
6720Sstevel@tonic-gate #pragma weak _rw_rdlock = __rw_rdlock
6730Sstevel@tonic-gate #pragma weak pthread_rwlock_rdlock = __rw_rdlock
6740Sstevel@tonic-gate #pragma weak _pthread_rwlock_rdlock = __rw_rdlock
6750Sstevel@tonic-gate int
6760Sstevel@tonic-gate __rw_rdlock(rwlock_t *rwlp)
6770Sstevel@tonic-gate {
6780Sstevel@tonic-gate 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
6790Sstevel@tonic-gate 	return (rw_rdlock_impl(rwlp, NULL));
6800Sstevel@tonic-gate }
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate void
6830Sstevel@tonic-gate lrw_rdlock(rwlock_t *rwlp)
6840Sstevel@tonic-gate {
6850Sstevel@tonic-gate 	enter_critical(curthread);
6860Sstevel@tonic-gate 	(void) rw_rdlock_impl(rwlp, NULL);
6870Sstevel@tonic-gate }
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate #pragma weak pthread_rwlock_reltimedrdlock_np = \
6900Sstevel@tonic-gate 	_pthread_rwlock_reltimedrdlock_np
6910Sstevel@tonic-gate int
6920Sstevel@tonic-gate _pthread_rwlock_reltimedrdlock_np(rwlock_t *rwlp, const timespec_t *reltime)
6930Sstevel@tonic-gate {
6940Sstevel@tonic-gate 	timespec_t tslocal = *reltime;
6950Sstevel@tonic-gate 	int error;
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
6980Sstevel@tonic-gate 	error = rw_rdlock_impl(rwlp, &tslocal);
6990Sstevel@tonic-gate 	if (error == ETIME)
7000Sstevel@tonic-gate 		error = ETIMEDOUT;
7010Sstevel@tonic-gate 	return (error);
7020Sstevel@tonic-gate }
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate #pragma weak pthread_rwlock_timedrdlock = _pthread_rwlock_timedrdlock
7050Sstevel@tonic-gate int
7060Sstevel@tonic-gate _pthread_rwlock_timedrdlock(rwlock_t *rwlp, const timespec_t *abstime)
7070Sstevel@tonic-gate {
7080Sstevel@tonic-gate 	timespec_t tslocal;
7090Sstevel@tonic-gate 	int error;
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
7120Sstevel@tonic-gate 	abstime_to_reltime(CLOCK_REALTIME, abstime, &tslocal);
7130Sstevel@tonic-gate 	error = rw_rdlock_impl(rwlp, &tslocal);
7140Sstevel@tonic-gate 	if (error == ETIME)
7150Sstevel@tonic-gate 		error = ETIMEDOUT;
7160Sstevel@tonic-gate 	return (error);
7170Sstevel@tonic-gate }
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate int
7200Sstevel@tonic-gate rw_wrlock_impl(rwlock_t *rwlp, timespec_t *tsp)
7210Sstevel@tonic-gate {
7220Sstevel@tonic-gate 	ulwp_t *self = curthread;
7230Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
7240Sstevel@tonic-gate 	tdb_rwlock_stats_t *rwsp = RWLOCK_STATS(rwlp, udp);
7250Sstevel@tonic-gate 	int error;
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	/*
7280Sstevel@tonic-gate 	 * If we hold a readers lock on this rwlock, bail out.
7290Sstevel@tonic-gate 	 */
7300Sstevel@tonic-gate 	if (rw_read_is_held(rwlp)) {
7310Sstevel@tonic-gate 		if (self->ul_error_detection)
7320Sstevel@tonic-gate 			rwlock_error(rwlp, "rwlock_wrlock",
7330Sstevel@tonic-gate 			    "calling thread owns the readers lock");
7344570Sraf 		error = EDEADLK;
7354570Sraf 		goto out;
7360Sstevel@tonic-gate 	}
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	/*
7390Sstevel@tonic-gate 	 * If we hold the writer lock, bail out.
7400Sstevel@tonic-gate 	 */
7410Sstevel@tonic-gate 	if (rw_write_is_held(rwlp)) {
7420Sstevel@tonic-gate 		if (self->ul_error_detection)
7430Sstevel@tonic-gate 			rwlock_error(rwlp, "rwlock_wrlock",
7440Sstevel@tonic-gate 			    "calling thread owns the writer lock");
7454570Sraf 		error = EDEADLK;
7464570Sraf 		goto out;
7470Sstevel@tonic-gate 	}
7480Sstevel@tonic-gate 
7494570Sraf 	if (write_lock_try(rwlp, 0))
7504570Sraf 		error = 0;
7514570Sraf 	else if (rwlp->rwlock_type == USYNC_PROCESS)	/* kernel-level */
7520Sstevel@tonic-gate 		error = shared_rwlock_lock(rwlp, tsp, WRITE_LOCK);
7534570Sraf 	else						/* user-level */
7540Sstevel@tonic-gate 		error = rwlock_lock(rwlp, tsp, WRITE_LOCK);
7550Sstevel@tonic-gate 
7564570Sraf out:
7574570Sraf 	if (error == 0) {
7584570Sraf 		rwlp->rwlock_owner = (uintptr_t)self;
7594570Sraf 		if (rwlp->rwlock_type == USYNC_PROCESS)
7604570Sraf 			rwlp->rwlock_ownerpid = udp->pid;
7614570Sraf 		if (rwsp) {
7624570Sraf 			tdb_incr(rwsp->rw_wrlock);
7634570Sraf 			rwsp->rw_wrlock_begin_hold = gethrtime();
7644570Sraf 		}
7654570Sraf 		DTRACE_PROBE2(plockstat, rw__acquire, rwlp, WRITE_LOCK);
7664570Sraf 	} else {
7674570Sraf 		DTRACE_PROBE3(plockstat, rw__error, rwlp, WRITE_LOCK, error);
7680Sstevel@tonic-gate 	}
7690Sstevel@tonic-gate 	return (error);
7700Sstevel@tonic-gate }
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate #pragma weak rw_wrlock = __rw_wrlock
7730Sstevel@tonic-gate #pragma weak _rw_wrlock = __rw_wrlock
7740Sstevel@tonic-gate #pragma weak pthread_rwlock_wrlock = __rw_wrlock
7750Sstevel@tonic-gate #pragma weak _pthread_rwlock_wrlock = __rw_wrlock
7760Sstevel@tonic-gate int
7770Sstevel@tonic-gate __rw_wrlock(rwlock_t *rwlp)
7780Sstevel@tonic-gate {
7790Sstevel@tonic-gate 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
7800Sstevel@tonic-gate 	return (rw_wrlock_impl(rwlp, NULL));
7810Sstevel@tonic-gate }
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate void
7840Sstevel@tonic-gate lrw_wrlock(rwlock_t *rwlp)
7850Sstevel@tonic-gate {
7860Sstevel@tonic-gate 	enter_critical(curthread);
7870Sstevel@tonic-gate 	(void) rw_wrlock_impl(rwlp, NULL);
7880Sstevel@tonic-gate }
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate #pragma weak pthread_rwlock_reltimedwrlock_np = \
7910Sstevel@tonic-gate 	_pthread_rwlock_reltimedwrlock_np
7920Sstevel@tonic-gate int
7930Sstevel@tonic-gate _pthread_rwlock_reltimedwrlock_np(rwlock_t *rwlp, const timespec_t *reltime)
7940Sstevel@tonic-gate {
7950Sstevel@tonic-gate 	timespec_t tslocal = *reltime;
7960Sstevel@tonic-gate 	int error;
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
7990Sstevel@tonic-gate 	error = rw_wrlock_impl(rwlp, &tslocal);
8000Sstevel@tonic-gate 	if (error == ETIME)
8010Sstevel@tonic-gate 		error = ETIMEDOUT;
8020Sstevel@tonic-gate 	return (error);
8030Sstevel@tonic-gate }
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate #pragma weak pthread_rwlock_timedwrlock = _pthread_rwlock_timedwrlock
8060Sstevel@tonic-gate int
8070Sstevel@tonic-gate _pthread_rwlock_timedwrlock(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);
8140Sstevel@tonic-gate 	error = rw_wrlock_impl(rwlp, &tslocal);
8150Sstevel@tonic-gate 	if (error == ETIME)
8160Sstevel@tonic-gate 		error = ETIMEDOUT;
8170Sstevel@tonic-gate 	return (error);
8180Sstevel@tonic-gate }
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate #pragma weak rw_tryrdlock = __rw_tryrdlock
8210Sstevel@tonic-gate #pragma weak _rw_tryrdlock = __rw_tryrdlock
8220Sstevel@tonic-gate #pragma weak pthread_rwlock_tryrdlock = __rw_tryrdlock
8230Sstevel@tonic-gate #pragma weak _pthread_rwlock_tryrdlock = __rw_tryrdlock
8240Sstevel@tonic-gate int
8250Sstevel@tonic-gate __rw_tryrdlock(rwlock_t *rwlp)
8260Sstevel@tonic-gate {
8270Sstevel@tonic-gate 	ulwp_t *self = curthread;
8280Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
8290Sstevel@tonic-gate 	tdb_rwlock_stats_t *rwsp = RWLOCK_STATS(rwlp, udp);
8300Sstevel@tonic-gate 	readlock_t *readlockp;
8310Sstevel@tonic-gate 	int error;
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	if (rwsp)
8360Sstevel@tonic-gate 		tdb_incr(rwsp->rw_rdlock_try);
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 	/*
8390Sstevel@tonic-gate 	 * If we already hold a readers lock on this rwlock,
8400Sstevel@tonic-gate 	 * just increment our reference count and return.
8410Sstevel@tonic-gate 	 */
8424570Sraf 	sigoff(self);
8430Sstevel@tonic-gate 	readlockp = rwl_entry(rwlp);
8440Sstevel@tonic-gate 	if (readlockp->rd_count != 0) {
8454570Sraf 		if (readlockp->rd_count == READ_LOCK_MAX) {
8464570Sraf 			sigon(self);
8474570Sraf 			error = EAGAIN;
8484570Sraf 			goto out;
8494570Sraf 		}
8504570Sraf 		sigon(self);
8514570Sraf 		error = 0;
8524570Sraf 		goto out;
8530Sstevel@tonic-gate 	}
8544570Sraf 	sigon(self);
8550Sstevel@tonic-gate 
8564570Sraf 	if (read_lock_try(rwlp, 0))
8574570Sraf 		error = 0;
8584570Sraf 	else if (rwlp->rwlock_type == USYNC_PROCESS)	/* kernel-level */
8590Sstevel@tonic-gate 		error = shared_rwlock_lock(rwlp, NULL, READ_LOCK_TRY);
8600Sstevel@tonic-gate 	else						/* user-level */
8610Sstevel@tonic-gate 		error = rwlock_lock(rwlp, NULL, READ_LOCK_TRY);
8620Sstevel@tonic-gate 
8634570Sraf out:
8644570Sraf 	if (error == 0) {
8654570Sraf 		sigoff(self);
8664570Sraf 		rwl_entry(rwlp)->rd_count++;
8674570Sraf 		sigon(self);
8684570Sraf 		DTRACE_PROBE2(plockstat, rw__acquire, rwlp, READ_LOCK);
8694570Sraf 	} else {
8704570Sraf 		if (rwsp)
8714570Sraf 			tdb_incr(rwsp->rw_rdlock_try_fail);
8724570Sraf 		if (error != EBUSY) {
8734570Sraf 			DTRACE_PROBE3(plockstat, rw__error, rwlp, READ_LOCK,
8744570Sraf 			    error);
8754570Sraf 		}
8764570Sraf 	}
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	return (error);
8790Sstevel@tonic-gate }
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate #pragma weak rw_trywrlock = __rw_trywrlock
8820Sstevel@tonic-gate #pragma weak _rw_trywrlock = __rw_trywrlock
8830Sstevel@tonic-gate #pragma weak pthread_rwlock_trywrlock = __rw_trywrlock
8840Sstevel@tonic-gate #pragma weak _pthread_rwlock_trywrlock = __rw_trywrlock
8850Sstevel@tonic-gate int
8860Sstevel@tonic-gate __rw_trywrlock(rwlock_t *rwlp)
8870Sstevel@tonic-gate {
8880Sstevel@tonic-gate 	ulwp_t *self = curthread;
8890Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
8900Sstevel@tonic-gate 	tdb_rwlock_stats_t *rwsp = RWLOCK_STATS(rwlp, udp);
8910Sstevel@tonic-gate 	int error;
8920Sstevel@tonic-gate 
8934570Sraf 	ASSERT(!self->ul_critical || self->ul_bindflags);
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 	if (rwsp)
8960Sstevel@tonic-gate 		tdb_incr(rwsp->rw_wrlock_try);
8970Sstevel@tonic-gate 
8984570Sraf 	if (write_lock_try(rwlp, 0))
8994570Sraf 		error = 0;
9004570Sraf 	else if (rwlp->rwlock_type == USYNC_PROCESS)	/* kernel-level */
9010Sstevel@tonic-gate 		error = shared_rwlock_lock(rwlp, NULL, WRITE_LOCK_TRY);
9024570Sraf 	else						/* user-level */
9030Sstevel@tonic-gate 		error = rwlock_lock(rwlp, NULL, WRITE_LOCK_TRY);
9044570Sraf 
9054570Sraf 	if (error == 0) {
9064570Sraf 		rwlp->rwlock_owner = (uintptr_t)self;
9074570Sraf 		if (rwlp->rwlock_type == USYNC_PROCESS)
9084570Sraf 			rwlp->rwlock_ownerpid = udp->pid;
9094570Sraf 		if (rwsp)
9104570Sraf 			rwsp->rw_wrlock_begin_hold = gethrtime();
9114570Sraf 		DTRACE_PROBE2(plockstat, rw__acquire, rwlp, WRITE_LOCK);
9124570Sraf 	} else {
9134570Sraf 		if (rwsp)
9140Sstevel@tonic-gate 			tdb_incr(rwsp->rw_wrlock_try_fail);
9154570Sraf 		if (error != EBUSY) {
9164570Sraf 			DTRACE_PROBE3(plockstat, rw__error, rwlp, WRITE_LOCK,
9174570Sraf 			    error);
9184570Sraf 		}
9190Sstevel@tonic-gate 	}
9200Sstevel@tonic-gate 	return (error);
9210Sstevel@tonic-gate }
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate #pragma weak rw_unlock = __rw_unlock
9240Sstevel@tonic-gate #pragma weak _rw_unlock = __rw_unlock
9250Sstevel@tonic-gate #pragma weak pthread_rwlock_unlock = __rw_unlock
9260Sstevel@tonic-gate #pragma weak _pthread_rwlock_unlock = __rw_unlock
9270Sstevel@tonic-gate int
9280Sstevel@tonic-gate __rw_unlock(rwlock_t *rwlp)
9290Sstevel@tonic-gate {
9304570Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
9314570Sraf 	uint32_t readers;
9320Sstevel@tonic-gate 	ulwp_t *self = curthread;
9330Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
9340Sstevel@tonic-gate 	tdb_rwlock_stats_t *rwsp;
9354570Sraf 	queue_head_t *qp;
9364570Sraf 	int rd_wr;
9374570Sraf 	int waked = 0;
9380Sstevel@tonic-gate 
9394570Sraf 	readers = *rwstate;
9404570Sraf 	ASSERT_CONSISTENT_STATE(readers);
9414570Sraf 	if (readers & URW_WRITE_LOCKED) {
9424570Sraf 		rd_wr = WRITE_LOCK;
9434570Sraf 		readers = 0;
9444570Sraf 	} else {
9454570Sraf 		rd_wr = READ_LOCK;
9464570Sraf 		readers &= URW_READERS_MASK;
9470Sstevel@tonic-gate 	}
9480Sstevel@tonic-gate 
9494570Sraf 	if (rd_wr == WRITE_LOCK) {
9500Sstevel@tonic-gate 		/*
9510Sstevel@tonic-gate 		 * Since the writer lock is held, we'd better be
9520Sstevel@tonic-gate 		 * holding it, else we cannot legitimately be here.
9530Sstevel@tonic-gate 		 */
9540Sstevel@tonic-gate 		if (!rw_write_is_held(rwlp)) {
9550Sstevel@tonic-gate 			if (self->ul_error_detection)
9560Sstevel@tonic-gate 				rwlock_error(rwlp, "rwlock_unlock",
9570Sstevel@tonic-gate 				    "writer lock held, "
9580Sstevel@tonic-gate 				    "but not by the calling thread");
9590Sstevel@tonic-gate 			return (EPERM);
9600Sstevel@tonic-gate 		}
9610Sstevel@tonic-gate 		if ((rwsp = RWLOCK_STATS(rwlp, udp)) != NULL) {
9620Sstevel@tonic-gate 			if (rwsp->rw_wrlock_begin_hold)
9630Sstevel@tonic-gate 				rwsp->rw_wrlock_hold_time +=
9640Sstevel@tonic-gate 				    gethrtime() - rwsp->rw_wrlock_begin_hold;
9650Sstevel@tonic-gate 			rwsp->rw_wrlock_begin_hold = 0;
9660Sstevel@tonic-gate 		}
9674570Sraf 		rwlp->rwlock_owner = 0;
9684570Sraf 		rwlp->rwlock_ownerpid = 0;
9694570Sraf 	} else if (readers > 0) {
9700Sstevel@tonic-gate 		/*
9710Sstevel@tonic-gate 		 * A readers lock is held; if we don't hold one, bail out.
9720Sstevel@tonic-gate 		 */
9734570Sraf 		readlock_t *readlockp;
9744570Sraf 
9754570Sraf 		sigoff(self);
9764570Sraf 		readlockp = rwl_entry(rwlp);
9770Sstevel@tonic-gate 		if (readlockp->rd_count == 0) {
9784570Sraf 			sigon(self);
9790Sstevel@tonic-gate 			if (self->ul_error_detection)
9800Sstevel@tonic-gate 				rwlock_error(rwlp, "rwlock_unlock",
9810Sstevel@tonic-gate 				    "readers lock held, "
9820Sstevel@tonic-gate 				    "but not by the calling thread");
9830Sstevel@tonic-gate 			return (EPERM);
9840Sstevel@tonic-gate 		}
9850Sstevel@tonic-gate 		/*
9860Sstevel@tonic-gate 		 * If we hold more than one readers lock on this rwlock,
9870Sstevel@tonic-gate 		 * just decrement our reference count and return.
9880Sstevel@tonic-gate 		 */
9890Sstevel@tonic-gate 		if (--readlockp->rd_count != 0) {
9904570Sraf 			sigon(self);
9914570Sraf 			goto out;
9920Sstevel@tonic-gate 		}
9934570Sraf 		sigon(self);
9940Sstevel@tonic-gate 	} else {
9950Sstevel@tonic-gate 		/*
9960Sstevel@tonic-gate 		 * This is a usage error.
9970Sstevel@tonic-gate 		 * No thread should release an unowned lock.
9980Sstevel@tonic-gate 		 */
9990Sstevel@tonic-gate 		if (self->ul_error_detection)
10000Sstevel@tonic-gate 			rwlock_error(rwlp, "rwlock_unlock", "lock not owned");
10010Sstevel@tonic-gate 		return (EPERM);
10020Sstevel@tonic-gate 	}
10030Sstevel@tonic-gate 
10044570Sraf 	if (rd_wr == WRITE_LOCK && write_unlock_try(rwlp)) {
10054570Sraf 		/* EMPTY */;
10064570Sraf 	} else if (rd_wr == READ_LOCK && read_unlock_try(rwlp)) {
10074570Sraf 		/* EMPTY */;
10084570Sraf 	} else if (rwlp->rwlock_type == USYNC_PROCESS) {
10094570Sraf 		(void) _private_mutex_lock(&rwlp->mutex);
10104570Sraf 		(void) __lwp_rwlock_unlock(rwlp);
10114570Sraf 		(void) _private_mutex_unlock(&rwlp->mutex);
10124570Sraf 		waked = 1;
10134570Sraf 	} else {
10140Sstevel@tonic-gate 		qp = queue_lock(rwlp, MX);
10154570Sraf 		if (rd_wr == READ_LOCK)
10164570Sraf 			atomic_dec_32(rwstate);
10174570Sraf 		else
10184570Sraf 			atomic_and_32(rwstate, ~URW_WRITE_LOCKED);
10190Sstevel@tonic-gate 		waked = rw_queue_release(qp, rwlp);
10200Sstevel@tonic-gate 	}
10210Sstevel@tonic-gate 
10224570Sraf out:
10234570Sraf 	DTRACE_PROBE2(plockstat, rw__release, rwlp, rd_wr);
10244570Sraf 
10250Sstevel@tonic-gate 	/*
10260Sstevel@tonic-gate 	 * Yield to the thread we just waked up, just in case we might
10270Sstevel@tonic-gate 	 * be about to grab the rwlock again immediately upon return.
10280Sstevel@tonic-gate 	 * This is pretty weak but it helps on a uniprocessor and also
10290Sstevel@tonic-gate 	 * when cpu affinity has assigned both ourself and the other
10300Sstevel@tonic-gate 	 * thread to the same CPU.  Note that lwp_yield() will yield
10310Sstevel@tonic-gate 	 * the processor only if the writer is at the same or higher
10320Sstevel@tonic-gate 	 * priority than ourself.  This provides more balanced program
10330Sstevel@tonic-gate 	 * behavior; it doesn't guarantee acquisition of the lock by
10340Sstevel@tonic-gate 	 * the pending writer.
10350Sstevel@tonic-gate 	 */
10360Sstevel@tonic-gate 	if (waked)
10370Sstevel@tonic-gate 		lwp_yield();
10380Sstevel@tonic-gate 	return (0);
10390Sstevel@tonic-gate }
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate void
10420Sstevel@tonic-gate lrw_unlock(rwlock_t *rwlp)
10430Sstevel@tonic-gate {
10440Sstevel@tonic-gate 	(void) __rw_unlock(rwlp);
10450Sstevel@tonic-gate 	exit_critical(curthread);
10460Sstevel@tonic-gate }
1047