xref: /illumos-gate/usr/src/lib/libc/port/threads/rwlock.c (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
541efec22Sraf  * Common Development and Distribution License (the "License").
641efec22Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2141efec22Sraf 
227c478bd9Sstevel@tonic-gate /*
23d4204c85Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include "lint.h"
307c478bd9Sstevel@tonic-gate #include "thr_uberdata.h"
317c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #define	TRY_FLAG		0x10
347c478bd9Sstevel@tonic-gate #define	READ_LOCK		0
357c478bd9Sstevel@tonic-gate #define	WRITE_LOCK		1
367c478bd9Sstevel@tonic-gate #define	READ_LOCK_TRY		(READ_LOCK | TRY_FLAG)
377c478bd9Sstevel@tonic-gate #define	WRITE_LOCK_TRY		(WRITE_LOCK | TRY_FLAG)
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #define	NLOCKS	4	/* initial number of readlock_t structs allocated */
407c478bd9Sstevel@tonic-gate 
4141efec22Sraf #define	ASSERT_CONSISTENT_STATE(readers)		\
4241efec22Sraf 	ASSERT(!((readers) & URW_WRITE_LOCKED) ||	\
4341efec22Sraf 		((readers) & ~URW_HAS_WAITERS) == URW_WRITE_LOCKED)
4441efec22Sraf 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * Find/allocate an entry for rwlp in our array of rwlocks held for reading.
4741efec22Sraf  * We must be deferring signals for this to be safe.
48883492d5Sraf  * Else if we are returning an entry with ul_rdlockcnt == 0,
4941efec22Sraf  * it could be reassigned behind our back in a signal handler.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate static readlock_t *
527c478bd9Sstevel@tonic-gate rwl_entry(rwlock_t *rwlp)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
557c478bd9Sstevel@tonic-gate 	readlock_t *remembered = NULL;
567c478bd9Sstevel@tonic-gate 	readlock_t *readlockp;
577c478bd9Sstevel@tonic-gate 	uint_t nlocks;
587c478bd9Sstevel@tonic-gate 
5941efec22Sraf 	/* we must be deferring signals */
6041efec22Sraf 	ASSERT((self->ul_critical + self->ul_sigdefer) != 0);
6141efec22Sraf 
62883492d5Sraf 	if ((nlocks = self->ul_rdlockcnt) != 0)
637c478bd9Sstevel@tonic-gate 		readlockp = self->ul_readlock.array;
647c478bd9Sstevel@tonic-gate 	else {
657c478bd9Sstevel@tonic-gate 		nlocks = 1;
667c478bd9Sstevel@tonic-gate 		readlockp = &self->ul_readlock.single;
677c478bd9Sstevel@tonic-gate 	}
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	for (; nlocks; nlocks--, readlockp++) {
707c478bd9Sstevel@tonic-gate 		if (readlockp->rd_rwlock == rwlp)
717c478bd9Sstevel@tonic-gate 			return (readlockp);
727c478bd9Sstevel@tonic-gate 		if (readlockp->rd_count == 0 && remembered == NULL)
737c478bd9Sstevel@tonic-gate 			remembered = readlockp;
747c478bd9Sstevel@tonic-gate 	}
757c478bd9Sstevel@tonic-gate 	if (remembered != NULL) {
767c478bd9Sstevel@tonic-gate 		remembered->rd_rwlock = rwlp;
777c478bd9Sstevel@tonic-gate 		return (remembered);
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	/*
817c478bd9Sstevel@tonic-gate 	 * No entry available.  Allocate more space, converting the single
827c478bd9Sstevel@tonic-gate 	 * readlock_t entry into an array of readlock_t entries if necessary.
837c478bd9Sstevel@tonic-gate 	 */
84883492d5Sraf 	if ((nlocks = self->ul_rdlockcnt) == 0) {
857c478bd9Sstevel@tonic-gate 		/*
867c478bd9Sstevel@tonic-gate 		 * Initial allocation of the readlock_t array.
877c478bd9Sstevel@tonic-gate 		 * Convert the single entry into an array.
887c478bd9Sstevel@tonic-gate 		 */
89883492d5Sraf 		self->ul_rdlockcnt = nlocks = NLOCKS;
907c478bd9Sstevel@tonic-gate 		readlockp = lmalloc(nlocks * sizeof (readlock_t));
917c478bd9Sstevel@tonic-gate 		/*
927c478bd9Sstevel@tonic-gate 		 * The single readlock_t becomes the first entry in the array.
937c478bd9Sstevel@tonic-gate 		 */
947c478bd9Sstevel@tonic-gate 		*readlockp = self->ul_readlock.single;
957c478bd9Sstevel@tonic-gate 		self->ul_readlock.single.rd_count = 0;
967c478bd9Sstevel@tonic-gate 		self->ul_readlock.array = readlockp;
977c478bd9Sstevel@tonic-gate 		/*
987c478bd9Sstevel@tonic-gate 		 * Return the next available entry in the array.
997c478bd9Sstevel@tonic-gate 		 */
1007c478bd9Sstevel@tonic-gate 		(++readlockp)->rd_rwlock = rwlp;
1017c478bd9Sstevel@tonic-gate 		return (readlockp);
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate 	/*
1047c478bd9Sstevel@tonic-gate 	 * Reallocate the array, double the size each time.
1057c478bd9Sstevel@tonic-gate 	 */
1067c478bd9Sstevel@tonic-gate 	readlockp = lmalloc(nlocks * 2 * sizeof (readlock_t));
1078cd45542Sraf 	(void) memcpy(readlockp, self->ul_readlock.array,
1087c478bd9Sstevel@tonic-gate 	    nlocks * sizeof (readlock_t));
1097c478bd9Sstevel@tonic-gate 	lfree(self->ul_readlock.array, nlocks * sizeof (readlock_t));
1107c478bd9Sstevel@tonic-gate 	self->ul_readlock.array = readlockp;
111883492d5Sraf 	self->ul_rdlockcnt *= 2;
1127c478bd9Sstevel@tonic-gate 	/*
1137c478bd9Sstevel@tonic-gate 	 * Return the next available entry in the newly allocated array.
1147c478bd9Sstevel@tonic-gate 	 */
1157c478bd9Sstevel@tonic-gate 	(readlockp += nlocks)->rd_rwlock = rwlp;
1167c478bd9Sstevel@tonic-gate 	return (readlockp);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  * Free the array of rwlocks held for reading.
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate void
1237c478bd9Sstevel@tonic-gate rwl_free(ulwp_t *ulwp)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	uint_t nlocks;
1267c478bd9Sstevel@tonic-gate 
127883492d5Sraf 	if ((nlocks = ulwp->ul_rdlockcnt) != 0)
1287c478bd9Sstevel@tonic-gate 		lfree(ulwp->ul_readlock.array, nlocks * sizeof (readlock_t));
129883492d5Sraf 	ulwp->ul_rdlockcnt = 0;
1307c478bd9Sstevel@tonic-gate 	ulwp->ul_readlock.single.rd_rwlock = NULL;
1317c478bd9Sstevel@tonic-gate 	ulwp->ul_readlock.single.rd_count = 0;
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate  * Check if a reader version of the lock is held by the current thread.
1367c478bd9Sstevel@tonic-gate  */
137*7257d1b4Sraf #pragma weak _rw_read_held = rw_read_held
1387c478bd9Sstevel@tonic-gate int
139*7257d1b4Sraf rw_read_held(rwlock_t *rwlp)
1407c478bd9Sstevel@tonic-gate {
14141efec22Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
14241efec22Sraf 	uint32_t readers;
14341efec22Sraf 	ulwp_t *self = curthread;
1447c478bd9Sstevel@tonic-gate 	readlock_t *readlockp;
1457c478bd9Sstevel@tonic-gate 	uint_t nlocks;
14641efec22Sraf 	int rval = 0;
1477c478bd9Sstevel@tonic-gate 
14841efec22Sraf 	no_preempt(self);
1497c478bd9Sstevel@tonic-gate 
15041efec22Sraf 	readers = *rwstate;
15141efec22Sraf 	ASSERT_CONSISTENT_STATE(readers);
15241efec22Sraf 	if (!(readers & URW_WRITE_LOCKED) &&
15341efec22Sraf 	    (readers & URW_READERS_MASK) != 0) {
1547c478bd9Sstevel@tonic-gate 		/*
1557c478bd9Sstevel@tonic-gate 		 * The lock is held for reading by some thread.
1567c478bd9Sstevel@tonic-gate 		 * Search our array of rwlocks held for reading for a match.
1577c478bd9Sstevel@tonic-gate 		 */
158883492d5Sraf 		if ((nlocks = self->ul_rdlockcnt) != 0)
1597c478bd9Sstevel@tonic-gate 			readlockp = self->ul_readlock.array;
1607c478bd9Sstevel@tonic-gate 		else {
1617c478bd9Sstevel@tonic-gate 			nlocks = 1;
1627c478bd9Sstevel@tonic-gate 			readlockp = &self->ul_readlock.single;
1637c478bd9Sstevel@tonic-gate 		}
16441efec22Sraf 		for (; nlocks; nlocks--, readlockp++) {
16541efec22Sraf 			if (readlockp->rd_rwlock == rwlp) {
16641efec22Sraf 				if (readlockp->rd_count)
16741efec22Sraf 					rval = 1;
16841efec22Sraf 				break;
16941efec22Sraf 			}
17041efec22Sraf 		}
17141efec22Sraf 	}
1727c478bd9Sstevel@tonic-gate 
17341efec22Sraf 	preempt(self);
17441efec22Sraf 	return (rval);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /*
1787c478bd9Sstevel@tonic-gate  * Check if a writer version of the lock is held by the current thread.
1797c478bd9Sstevel@tonic-gate  */
180*7257d1b4Sraf #pragma weak _rw_write_held = rw_write_held
1817c478bd9Sstevel@tonic-gate int
182*7257d1b4Sraf rw_write_held(rwlock_t *rwlp)
1837c478bd9Sstevel@tonic-gate {
18441efec22Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
18541efec22Sraf 	uint32_t readers;
1867c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
18741efec22Sraf 	int rval;
1887c478bd9Sstevel@tonic-gate 
18941efec22Sraf 	no_preempt(self);
1907c478bd9Sstevel@tonic-gate 
19141efec22Sraf 	readers = *rwstate;
19241efec22Sraf 	ASSERT_CONSISTENT_STATE(readers);
19341efec22Sraf 	rval = ((readers & URW_WRITE_LOCKED) &&
19441efec22Sraf 	    rwlp->rwlock_owner == (uintptr_t)self &&
19541efec22Sraf 	    (rwlp->rwlock_type == USYNC_THREAD ||
19641efec22Sraf 	    rwlp->rwlock_ownerpid == self->ul_uberdata->pid));
19741efec22Sraf 
19841efec22Sraf 	preempt(self);
19941efec22Sraf 	return (rval);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate 
202*7257d1b4Sraf #pragma weak _rwlock_init = rwlock_init
2037c478bd9Sstevel@tonic-gate /* ARGSUSED2 */
2047c478bd9Sstevel@tonic-gate int
205*7257d1b4Sraf rwlock_init(rwlock_t *rwlp, int type, void *arg)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	if (type != USYNC_THREAD && type != USYNC_PROCESS)
2087c478bd9Sstevel@tonic-gate 		return (EINVAL);
2097c478bd9Sstevel@tonic-gate 	/*
2107c478bd9Sstevel@tonic-gate 	 * Once reinitialized, we can no longer be holding a read or write lock.
2117c478bd9Sstevel@tonic-gate 	 * We can do nothing about other threads that are holding read locks.
2127c478bd9Sstevel@tonic-gate 	 */
21341efec22Sraf 	sigoff(curthread);
2147c478bd9Sstevel@tonic-gate 	rwl_entry(rwlp)->rd_count = 0;
21541efec22Sraf 	sigon(curthread);
2168cd45542Sraf 	(void) memset(rwlp, 0, sizeof (*rwlp));
2177c478bd9Sstevel@tonic-gate 	rwlp->rwlock_type = (uint16_t)type;
2187c478bd9Sstevel@tonic-gate 	rwlp->rwlock_magic = RWL_MAGIC;
2197c478bd9Sstevel@tonic-gate 	rwlp->mutex.mutex_type = (uint8_t)type;
2207c478bd9Sstevel@tonic-gate 	rwlp->mutex.mutex_flag = LOCK_INITED;
2217c478bd9Sstevel@tonic-gate 	rwlp->mutex.mutex_magic = MUTEX_MAGIC;
2227c478bd9Sstevel@tonic-gate 	return (0);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
225*7257d1b4Sraf #pragma weak pthread_rwlock_destroy = rwlock_destroy
226*7257d1b4Sraf #pragma weak _rwlock_destroy = rwlock_destroy
2277c478bd9Sstevel@tonic-gate int
228*7257d1b4Sraf rwlock_destroy(rwlock_t *rwlp)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 	/*
2317c478bd9Sstevel@tonic-gate 	 * Once destroyed, we can no longer be holding a read or write lock.
2327c478bd9Sstevel@tonic-gate 	 * We can do nothing about other threads that are holding read locks.
2337c478bd9Sstevel@tonic-gate 	 */
23441efec22Sraf 	sigoff(curthread);
2357c478bd9Sstevel@tonic-gate 	rwl_entry(rwlp)->rd_count = 0;
23641efec22Sraf 	sigon(curthread);
2377c478bd9Sstevel@tonic-gate 	rwlp->rwlock_magic = 0;
2387c478bd9Sstevel@tonic-gate 	tdb_sync_obj_deregister(rwlp);
2397c478bd9Sstevel@tonic-gate 	return (0);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate /*
24341efec22Sraf  * Attempt to acquire a readers lock.  Return true on success.
2447c478bd9Sstevel@tonic-gate  */
2457c478bd9Sstevel@tonic-gate static int
24641efec22Sraf read_lock_try(rwlock_t *rwlp, int ignore_waiters_flag)
2477c478bd9Sstevel@tonic-gate {
24841efec22Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
24941efec22Sraf 	uint32_t mask = ignore_waiters_flag?
25041efec22Sraf 	    URW_WRITE_LOCKED : (URW_HAS_WAITERS | URW_WRITE_LOCKED);
25141efec22Sraf 	uint32_t readers;
2527c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	no_preempt(self);
25541efec22Sraf 	while (((readers = *rwstate) & mask) == 0) {
25641efec22Sraf 		if (atomic_cas_32(rwstate, readers, readers + 1) == readers) {
2577c478bd9Sstevel@tonic-gate 			preempt(self);
2587c478bd9Sstevel@tonic-gate 			return (1);
2597c478bd9Sstevel@tonic-gate 		}
2607c478bd9Sstevel@tonic-gate 	}
26141efec22Sraf 	preempt(self);
26241efec22Sraf 	return (0);
2637c478bd9Sstevel@tonic-gate }
26441efec22Sraf 
26541efec22Sraf /*
26641efec22Sraf  * Attempt to release a reader lock.  Return true on success.
26741efec22Sraf  */
26841efec22Sraf static int
26941efec22Sraf read_unlock_try(rwlock_t *rwlp)
27041efec22Sraf {
27141efec22Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
27241efec22Sraf 	uint32_t readers;
27341efec22Sraf 	ulwp_t *self = curthread;
27441efec22Sraf 
27541efec22Sraf 	no_preempt(self);
27641efec22Sraf 	while (((readers = *rwstate) & URW_HAS_WAITERS) == 0) {
27741efec22Sraf 		if (atomic_cas_32(rwstate, readers, readers - 1) == readers) {
27841efec22Sraf 			preempt(self);
27941efec22Sraf 			return (1);
28041efec22Sraf 		}
28141efec22Sraf 	}
28241efec22Sraf 	preempt(self);
28341efec22Sraf 	return (0);
28441efec22Sraf }
28541efec22Sraf 
28641efec22Sraf /*
28741efec22Sraf  * Attempt to acquire a writer lock.  Return true on success.
28841efec22Sraf  */
28941efec22Sraf static int
29041efec22Sraf write_lock_try(rwlock_t *rwlp, int ignore_waiters_flag)
29141efec22Sraf {
29241efec22Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
29341efec22Sraf 	uint32_t mask = ignore_waiters_flag?
29441efec22Sraf 	    (URW_WRITE_LOCKED | URW_READERS_MASK) :
29541efec22Sraf 	    (URW_HAS_WAITERS | URW_WRITE_LOCKED | URW_READERS_MASK);
29641efec22Sraf 	ulwp_t *self = curthread;
29741efec22Sraf 	uint32_t readers;
29841efec22Sraf 
29941efec22Sraf 	no_preempt(self);
30041efec22Sraf 	while (((readers = *rwstate) & mask) == 0) {
30141efec22Sraf 		if (atomic_cas_32(rwstate, readers, readers | URW_WRITE_LOCKED)
30241efec22Sraf 		    == readers) {
30341efec22Sraf 			preempt(self);
30441efec22Sraf 			return (1);
30541efec22Sraf 		}
30641efec22Sraf 	}
30741efec22Sraf 	preempt(self);
30841efec22Sraf 	return (0);
30941efec22Sraf }
31041efec22Sraf 
31141efec22Sraf /*
31241efec22Sraf  * Attempt to release a writer lock.  Return true on success.
31341efec22Sraf  */
31441efec22Sraf static int
31541efec22Sraf write_unlock_try(rwlock_t *rwlp)
31641efec22Sraf {
31741efec22Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
31841efec22Sraf 	uint32_t readers;
31941efec22Sraf 	ulwp_t *self = curthread;
32041efec22Sraf 
32141efec22Sraf 	no_preempt(self);
32241efec22Sraf 	while (((readers = *rwstate) & URW_HAS_WAITERS) == 0) {
32341efec22Sraf 		if (atomic_cas_32(rwstate, readers, 0) == readers) {
32441efec22Sraf 			preempt(self);
32541efec22Sraf 			return (1);
32641efec22Sraf 		}
32741efec22Sraf 	}
32841efec22Sraf 	preempt(self);
32941efec22Sraf 	return (0);
33041efec22Sraf }
33141efec22Sraf 
33241efec22Sraf /*
33341efec22Sraf  * Wake up thread(s) sleeping on the rwlock queue and then
33441efec22Sraf  * drop the queue lock.  Return non-zero if we wake up someone.
33541efec22Sraf  * This is called when a thread releases a lock that appears to have waiters.
33641efec22Sraf  */
33741efec22Sraf static int
33841efec22Sraf rw_queue_release(queue_head_t *qp, rwlock_t *rwlp)
33941efec22Sraf {
34041efec22Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
34141efec22Sraf 	uint32_t readers;
34241efec22Sraf 	uint32_t writers;
34341efec22Sraf 	ulwp_t **ulwpp;
34441efec22Sraf 	ulwp_t *ulwp;
345d4204c85Sraf 	ulwp_t *prev;
346d4204c85Sraf 	int nlwpid = 0;
347d4204c85Sraf 	int more;
348d4204c85Sraf 	int maxlwps = MAXLWPS;
34941efec22Sraf 	lwpid_t buffer[MAXLWPS];
35041efec22Sraf 	lwpid_t *lwpid = buffer;
35141efec22Sraf 
35241efec22Sraf 	readers = *rwstate;
35341efec22Sraf 	ASSERT_CONSISTENT_STATE(readers);
35441efec22Sraf 	if (!(readers & URW_HAS_WAITERS)) {
3557c478bd9Sstevel@tonic-gate 		queue_unlock(qp);
3567c478bd9Sstevel@tonic-gate 		return (0);
3577c478bd9Sstevel@tonic-gate 	}
35841efec22Sraf 	readers &= URW_READERS_MASK;
35941efec22Sraf 	writers = 0;
36041efec22Sraf 
36141efec22Sraf 	/*
362d4204c85Sraf 	 * Examine the queue of waiters in priority order and prepare
363d4204c85Sraf 	 * to wake up as many readers as we encounter before encountering
364d4204c85Sraf 	 * a writer.  If the highest priority thread on the queue is a
36541efec22Sraf 	 * writer, stop there and wake it up.
36641efec22Sraf 	 *
36741efec22Sraf 	 * We keep track of lwpids that are to be unparked in lwpid[].
36841efec22Sraf 	 * __lwp_unpark_all() is called to unpark all of them after
36941efec22Sraf 	 * they have been removed from the sleep queue and the sleep
37041efec22Sraf 	 * queue lock has been dropped.  If we run out of space in our
37141efec22Sraf 	 * on-stack buffer, we need to allocate more but we can't call
37241efec22Sraf 	 * lmalloc() because we are holding a queue lock when the overflow
37341efec22Sraf 	 * occurs and lmalloc() acquires a lock.  We can't use alloca()
37441efec22Sraf 	 * either because the application may have allocated a small
37541efec22Sraf 	 * stack and we don't want to overrun the stack.  So we call
37641efec22Sraf 	 * alloc_lwpids() to allocate a bigger buffer using the mmap()
37741efec22Sraf 	 * system call directly since that path acquires no locks.
37841efec22Sraf 	 */
379d4204c85Sraf 	while ((ulwpp = queue_slot(qp, &prev, &more)) != NULL) {
380d4204c85Sraf 		ulwp = *ulwpp;
381d4204c85Sraf 		ASSERT(ulwp->ul_wchan == rwlp);
38241efec22Sraf 		if (ulwp->ul_writer) {
38341efec22Sraf 			if (writers != 0 || readers != 0)
38441efec22Sraf 				break;
38541efec22Sraf 			/* one writer to wake */
38641efec22Sraf 			writers++;
38741efec22Sraf 		} else {
38841efec22Sraf 			if (writers != 0)
38941efec22Sraf 				break;
39041efec22Sraf 			/* at least one reader to wake */
39141efec22Sraf 			readers++;
39241efec22Sraf 			if (nlwpid == maxlwps)
39341efec22Sraf 				lwpid = alloc_lwpids(lwpid, &nlwpid, &maxlwps);
39441efec22Sraf 		}
395d4204c85Sraf 		queue_unlink(qp, ulwpp, prev);
396d4204c85Sraf 		ulwp->ul_sleepq = NULL;
397d4204c85Sraf 		ulwp->ul_wchan = NULL;
39841efec22Sraf 		lwpid[nlwpid++] = ulwp->ul_lwpid;
39941efec22Sraf 	}
400d4204c85Sraf 	if (ulwpp == NULL)
40141efec22Sraf 		atomic_and_32(rwstate, ~URW_HAS_WAITERS);
40241efec22Sraf 	if (nlwpid == 0) {
40341efec22Sraf 		queue_unlock(qp);
40441efec22Sraf 	} else {
405d4204c85Sraf 		ulwp_t *self = curthread;
40641efec22Sraf 		no_preempt(self);
40741efec22Sraf 		queue_unlock(qp);
40841efec22Sraf 		if (nlwpid == 1)
40941efec22Sraf 			(void) __lwp_unpark(lwpid[0]);
41041efec22Sraf 		else
41141efec22Sraf 			(void) __lwp_unpark_all(lwpid, nlwpid);
41241efec22Sraf 		preempt(self);
41341efec22Sraf 	}
41441efec22Sraf 	if (lwpid != buffer)
4158cd45542Sraf 		(void) munmap((caddr_t)lwpid, maxlwps * sizeof (lwpid_t));
41641efec22Sraf 	return (nlwpid != 0);
41741efec22Sraf }
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate /*
4207c478bd9Sstevel@tonic-gate  * Common code for rdlock, timedrdlock, wrlock, timedwrlock, tryrdlock,
4217c478bd9Sstevel@tonic-gate  * and trywrlock for process-shared (USYNC_PROCESS) rwlocks.
4227c478bd9Sstevel@tonic-gate  *
4237c478bd9Sstevel@tonic-gate  * Note: if the lock appears to be contended we call __lwp_rwlock_rdlock()
4247c478bd9Sstevel@tonic-gate  * or __lwp_rwlock_wrlock() holding the mutex. These return with the mutex
4257c478bd9Sstevel@tonic-gate  * released, and if they need to sleep will release the mutex first. In the
4267c478bd9Sstevel@tonic-gate  * event of a spurious wakeup, these will return EAGAIN (because it is much
4277c478bd9Sstevel@tonic-gate  * easier for us to re-acquire the mutex here).
4287c478bd9Sstevel@tonic-gate  */
4297c478bd9Sstevel@tonic-gate int
4307c478bd9Sstevel@tonic-gate shared_rwlock_lock(rwlock_t *rwlp, timespec_t *tsp, int rd_wr)
4317c478bd9Sstevel@tonic-gate {
43241efec22Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
43341efec22Sraf 	mutex_t *mp = &rwlp->mutex;
43441efec22Sraf 	uint32_t readers;
4357c478bd9Sstevel@tonic-gate 	int try_flag;
43641efec22Sraf 	int error;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	try_flag = (rd_wr & TRY_FLAG);
4397c478bd9Sstevel@tonic-gate 	rd_wr &= ~TRY_FLAG;
4407c478bd9Sstevel@tonic-gate 	ASSERT(rd_wr == READ_LOCK || rd_wr == WRITE_LOCK);
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	if (!try_flag) {
4437c478bd9Sstevel@tonic-gate 		DTRACE_PROBE2(plockstat, rw__block, rwlp, rd_wr);
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	do {
44741efec22Sraf 		if (try_flag && (*rwstate & URW_WRITE_LOCKED)) {
44841efec22Sraf 			error = EBUSY;
4497c478bd9Sstevel@tonic-gate 			break;
45041efec22Sraf 		}
4518cd45542Sraf 		if ((error = mutex_lock(mp)) != 0)
45241efec22Sraf 			break;
4537c478bd9Sstevel@tonic-gate 		if (rd_wr == READ_LOCK) {
45441efec22Sraf 			if (read_lock_try(rwlp, 0)) {
4558cd45542Sraf 				(void) mutex_unlock(mp);
45641efec22Sraf 				break;
4577c478bd9Sstevel@tonic-gate 			}
4587c478bd9Sstevel@tonic-gate 		} else {
45941efec22Sraf 			if (write_lock_try(rwlp, 0)) {
4608cd45542Sraf 				(void) mutex_unlock(mp);
46141efec22Sraf 				break;
4627c478bd9Sstevel@tonic-gate 			}
46341efec22Sraf 		}
46441efec22Sraf 		atomic_or_32(rwstate, URW_HAS_WAITERS);
46541efec22Sraf 		readers = *rwstate;
46641efec22Sraf 		ASSERT_CONSISTENT_STATE(readers);
4677c478bd9Sstevel@tonic-gate 		/*
46841efec22Sraf 		 * The calls to __lwp_rwlock_*() below will release the mutex,
46941efec22Sraf 		 * so we need a dtrace probe here.
4707c478bd9Sstevel@tonic-gate 		 */
47141efec22Sraf 		mp->mutex_owner = 0;
47241efec22Sraf 		DTRACE_PROBE2(plockstat, mutex__release, mp, 0);
4737c478bd9Sstevel@tonic-gate 		/*
4747c478bd9Sstevel@tonic-gate 		 * The waiters bit may be inaccurate.
4757c478bd9Sstevel@tonic-gate 		 * Only the kernel knows for sure.
4767c478bd9Sstevel@tonic-gate 		 */
47741efec22Sraf 		if (rd_wr == READ_LOCK) {
47841efec22Sraf 			if (try_flag)
47941efec22Sraf 				error = __lwp_rwlock_tryrdlock(rwlp);
48041efec22Sraf 			else
48141efec22Sraf 				error = __lwp_rwlock_rdlock(rwlp, tsp);
4827c478bd9Sstevel@tonic-gate 		} else {
48341efec22Sraf 			if (try_flag)
48441efec22Sraf 				error = __lwp_rwlock_trywrlock(rwlp);
48541efec22Sraf 			else
4867c478bd9Sstevel@tonic-gate 				error = __lwp_rwlock_wrlock(rwlp, tsp);
4877c478bd9Sstevel@tonic-gate 		}
48841efec22Sraf 	} while (error == EAGAIN || error == EINTR);
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	if (!try_flag) {
49141efec22Sraf 		DTRACE_PROBE3(plockstat, rw__blocked, rwlp, rd_wr, error == 0);
4927c478bd9Sstevel@tonic-gate 	}
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	return (error);
4957c478bd9Sstevel@tonic-gate }
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate /*
4987c478bd9Sstevel@tonic-gate  * Common code for rdlock, timedrdlock, wrlock, timedwrlock, tryrdlock,
4997c478bd9Sstevel@tonic-gate  * and trywrlock for process-private (USYNC_THREAD) rwlocks.
5007c478bd9Sstevel@tonic-gate  */
5017c478bd9Sstevel@tonic-gate int
5027c478bd9Sstevel@tonic-gate rwlock_lock(rwlock_t *rwlp, timespec_t *tsp, int rd_wr)
5037c478bd9Sstevel@tonic-gate {
50441efec22Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
50541efec22Sraf 	uint32_t readers;
5067c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
5077c478bd9Sstevel@tonic-gate 	queue_head_t *qp;
5087c478bd9Sstevel@tonic-gate 	ulwp_t *ulwp;
5097c478bd9Sstevel@tonic-gate 	int try_flag;
510d4204c85Sraf 	int ignore_waiters_flag;
5117c478bd9Sstevel@tonic-gate 	int error = 0;
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	try_flag = (rd_wr & TRY_FLAG);
5147c478bd9Sstevel@tonic-gate 	rd_wr &= ~TRY_FLAG;
5157c478bd9Sstevel@tonic-gate 	ASSERT(rd_wr == READ_LOCK || rd_wr == WRITE_LOCK);
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	if (!try_flag) {
5187c478bd9Sstevel@tonic-gate 		DTRACE_PROBE2(plockstat, rw__block, rwlp, rd_wr);
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	qp = queue_lock(rwlp, MX);
522d4204c85Sraf 	/* initial attempt to acquire the lock fails if there are waiters */
523d4204c85Sraf 	ignore_waiters_flag = 0;
5247c478bd9Sstevel@tonic-gate 	while (error == 0) {
52541efec22Sraf 		if (rd_wr == READ_LOCK) {
526d4204c85Sraf 			if (read_lock_try(rwlp, ignore_waiters_flag))
527d4204c85Sraf 				break;
52841efec22Sraf 		} else {
529d4204c85Sraf 			if (write_lock_try(rwlp, ignore_waiters_flag))
530d4204c85Sraf 				break;
53141efec22Sraf 		}
532d4204c85Sraf 		/* subsequent attempts do not fail due to waiters */
533d4204c85Sraf 		ignore_waiters_flag = 1;
53441efec22Sraf 		atomic_or_32(rwstate, URW_HAS_WAITERS);
53541efec22Sraf 		readers = *rwstate;
53641efec22Sraf 		ASSERT_CONSISTENT_STATE(readers);
53741efec22Sraf 		if ((readers & URW_WRITE_LOCKED) ||
53841efec22Sraf 		    (rd_wr == WRITE_LOCK &&
53941efec22Sraf 		    (readers & URW_READERS_MASK) != 0))
5407c478bd9Sstevel@tonic-gate 			/* EMPTY */;	/* somebody holds the lock */
541d4204c85Sraf 		else if ((ulwp = queue_waiter(qp)) == NULL) {
54241efec22Sraf 			atomic_and_32(rwstate, ~URW_HAS_WAITERS);
543d4204c85Sraf 			continue;	/* no queued waiters, try again */
5447c478bd9Sstevel@tonic-gate 		} else {
545d4204c85Sraf 			/*
546d4204c85Sraf 			 * Do a priority check on the queued waiter (the
547d4204c85Sraf 			 * highest priority thread on the queue) to see
548d4204c85Sraf 			 * if we should defer to him or just grab the lock.
549d4204c85Sraf 			 */
5507c478bd9Sstevel@tonic-gate 			int our_pri = real_priority(self);
5517c478bd9Sstevel@tonic-gate 			int his_pri = real_priority(ulwp);
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 			if (rd_wr == WRITE_LOCK) {
5547c478bd9Sstevel@tonic-gate 				/*
5557c478bd9Sstevel@tonic-gate 				 * We defer to a queued thread that has
5567c478bd9Sstevel@tonic-gate 				 * a higher priority than ours.
5577c478bd9Sstevel@tonic-gate 				 */
5587c478bd9Sstevel@tonic-gate 				if (his_pri <= our_pri)
559d4204c85Sraf 					continue;	/* try again */
5607c478bd9Sstevel@tonic-gate 			} else {
5617c478bd9Sstevel@tonic-gate 				/*
5627c478bd9Sstevel@tonic-gate 				 * We defer to a queued thread that has
5637c478bd9Sstevel@tonic-gate 				 * a higher priority than ours or that
5647c478bd9Sstevel@tonic-gate 				 * is a writer whose priority equals ours.
5657c478bd9Sstevel@tonic-gate 				 */
5667c478bd9Sstevel@tonic-gate 				if (his_pri < our_pri ||
5677c478bd9Sstevel@tonic-gate 				    (his_pri == our_pri && !ulwp->ul_writer))
568d4204c85Sraf 					continue;	/* try again */
5697c478bd9Sstevel@tonic-gate 			}
5707c478bd9Sstevel@tonic-gate 		}
5717c478bd9Sstevel@tonic-gate 		/*
5727c478bd9Sstevel@tonic-gate 		 * We are about to block.
5737c478bd9Sstevel@tonic-gate 		 * If we're doing a trylock, return EBUSY instead.
5747c478bd9Sstevel@tonic-gate 		 */
5757c478bd9Sstevel@tonic-gate 		if (try_flag) {
5767c478bd9Sstevel@tonic-gate 			error = EBUSY;
5777c478bd9Sstevel@tonic-gate 			break;
5787c478bd9Sstevel@tonic-gate 		}
5797c478bd9Sstevel@tonic-gate 		/*
580d4204c85Sraf 		 * Enqueue writers ahead of readers.
5817c478bd9Sstevel@tonic-gate 		 */
5827c478bd9Sstevel@tonic-gate 		self->ul_writer = rd_wr;	/* *must* be 0 or 1 */
583d4204c85Sraf 		enqueue(qp, self, 0);
5847c478bd9Sstevel@tonic-gate 		set_parking_flag(self, 1);
5857c478bd9Sstevel@tonic-gate 		queue_unlock(qp);
5867c478bd9Sstevel@tonic-gate 		if ((error = __lwp_park(tsp, 0)) == EINTR)
587d4204c85Sraf 			error = ignore_waiters_flag = 0;
5887c478bd9Sstevel@tonic-gate 		set_parking_flag(self, 0);
5897c478bd9Sstevel@tonic-gate 		qp = queue_lock(rwlp, MX);
590d4204c85Sraf 		if (self->ul_sleepq && dequeue_self(qp) == 0)
59141efec22Sraf 			atomic_and_32(rwstate, ~URW_HAS_WAITERS);
592d4204c85Sraf 		self->ul_writer = 0;
5937c478bd9Sstevel@tonic-gate 	}
5947c478bd9Sstevel@tonic-gate 
59541efec22Sraf 	queue_unlock(qp);
59641efec22Sraf 
59741efec22Sraf 	if (!try_flag) {
59841efec22Sraf 		DTRACE_PROBE3(plockstat, rw__blocked, rwlp, rd_wr, error == 0);
59941efec22Sraf 	}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	return (error);
6027c478bd9Sstevel@tonic-gate }
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate int
6057c478bd9Sstevel@tonic-gate rw_rdlock_impl(rwlock_t *rwlp, timespec_t *tsp)
6067c478bd9Sstevel@tonic-gate {
6077c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
6087c478bd9Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
6097c478bd9Sstevel@tonic-gate 	readlock_t *readlockp;
6107c478bd9Sstevel@tonic-gate 	tdb_rwlock_stats_t *rwsp = RWLOCK_STATS(rwlp, udp);
6117c478bd9Sstevel@tonic-gate 	int error;
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	/*
6147c478bd9Sstevel@tonic-gate 	 * If we already hold a readers lock on this rwlock,
6157c478bd9Sstevel@tonic-gate 	 * just increment our reference count and return.
6167c478bd9Sstevel@tonic-gate 	 */
61741efec22Sraf 	sigoff(self);
6187c478bd9Sstevel@tonic-gate 	readlockp = rwl_entry(rwlp);
6197c478bd9Sstevel@tonic-gate 	if (readlockp->rd_count != 0) {
62041efec22Sraf 		if (readlockp->rd_count == READ_LOCK_MAX) {
62141efec22Sraf 			sigon(self);
62241efec22Sraf 			error = EAGAIN;
62341efec22Sraf 			goto out;
6247c478bd9Sstevel@tonic-gate 		}
62541efec22Sraf 		sigon(self);
62641efec22Sraf 		error = 0;
62741efec22Sraf 		goto out;
62841efec22Sraf 	}
62941efec22Sraf 	sigon(self);
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	/*
6327c478bd9Sstevel@tonic-gate 	 * If we hold the writer lock, bail out.
6337c478bd9Sstevel@tonic-gate 	 */
634*7257d1b4Sraf 	if (rw_write_held(rwlp)) {
6357c478bd9Sstevel@tonic-gate 		if (self->ul_error_detection)
6367c478bd9Sstevel@tonic-gate 			rwlock_error(rwlp, "rwlock_rdlock",
6377c478bd9Sstevel@tonic-gate 			    "calling thread owns the writer lock");
63841efec22Sraf 		error = EDEADLK;
63941efec22Sraf 		goto out;
6407c478bd9Sstevel@tonic-gate 	}
6417c478bd9Sstevel@tonic-gate 
64241efec22Sraf 	if (read_lock_try(rwlp, 0))
64341efec22Sraf 		error = 0;
64441efec22Sraf 	else if (rwlp->rwlock_type == USYNC_PROCESS)	/* kernel-level */
6457c478bd9Sstevel@tonic-gate 		error = shared_rwlock_lock(rwlp, tsp, READ_LOCK);
6467c478bd9Sstevel@tonic-gate 	else						/* user-level */
6477c478bd9Sstevel@tonic-gate 		error = rwlock_lock(rwlp, tsp, READ_LOCK);
6487c478bd9Sstevel@tonic-gate 
64941efec22Sraf out:
6507c478bd9Sstevel@tonic-gate 	if (error == 0) {
65141efec22Sraf 		sigoff(self);
65241efec22Sraf 		rwl_entry(rwlp)->rd_count++;
65341efec22Sraf 		sigon(self);
6547c478bd9Sstevel@tonic-gate 		if (rwsp)
6557c478bd9Sstevel@tonic-gate 			tdb_incr(rwsp->rw_rdlock);
65641efec22Sraf 		DTRACE_PROBE2(plockstat, rw__acquire, rwlp, READ_LOCK);
65741efec22Sraf 	} else {
65841efec22Sraf 		DTRACE_PROBE3(plockstat, rw__error, rwlp, READ_LOCK, error);
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	return (error);
6627c478bd9Sstevel@tonic-gate }
6637c478bd9Sstevel@tonic-gate 
664*7257d1b4Sraf #pragma weak pthread_rwlock_rdlock = rw_rdlock
665*7257d1b4Sraf #pragma weak _rw_rdlock = rw_rdlock
6667c478bd9Sstevel@tonic-gate int
667*7257d1b4Sraf rw_rdlock(rwlock_t *rwlp)
6687c478bd9Sstevel@tonic-gate {
6697c478bd9Sstevel@tonic-gate 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
6707c478bd9Sstevel@tonic-gate 	return (rw_rdlock_impl(rwlp, NULL));
6717c478bd9Sstevel@tonic-gate }
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate void
6747c478bd9Sstevel@tonic-gate lrw_rdlock(rwlock_t *rwlp)
6757c478bd9Sstevel@tonic-gate {
6767c478bd9Sstevel@tonic-gate 	enter_critical(curthread);
6777c478bd9Sstevel@tonic-gate 	(void) rw_rdlock_impl(rwlp, NULL);
6787c478bd9Sstevel@tonic-gate }
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate int
681*7257d1b4Sraf pthread_rwlock_reltimedrdlock_np(pthread_rwlock_t *_RESTRICT_KYWD rwlp,
682*7257d1b4Sraf     const struct timespec *_RESTRICT_KYWD reltime)
6837c478bd9Sstevel@tonic-gate {
6847c478bd9Sstevel@tonic-gate 	timespec_t tslocal = *reltime;
6857c478bd9Sstevel@tonic-gate 	int error;
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
688*7257d1b4Sraf 	error = rw_rdlock_impl((rwlock_t *)rwlp, &tslocal);
6897c478bd9Sstevel@tonic-gate 	if (error == ETIME)
6907c478bd9Sstevel@tonic-gate 		error = ETIMEDOUT;
6917c478bd9Sstevel@tonic-gate 	return (error);
6927c478bd9Sstevel@tonic-gate }
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate int
695*7257d1b4Sraf pthread_rwlock_timedrdlock(pthread_rwlock_t *_RESTRICT_KYWD rwlp,
696*7257d1b4Sraf     const struct timespec *_RESTRICT_KYWD abstime)
6977c478bd9Sstevel@tonic-gate {
6987c478bd9Sstevel@tonic-gate 	timespec_t tslocal;
6997c478bd9Sstevel@tonic-gate 	int error;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
7027c478bd9Sstevel@tonic-gate 	abstime_to_reltime(CLOCK_REALTIME, abstime, &tslocal);
703*7257d1b4Sraf 	error = rw_rdlock_impl((rwlock_t *)rwlp, &tslocal);
7047c478bd9Sstevel@tonic-gate 	if (error == ETIME)
7057c478bd9Sstevel@tonic-gate 		error = ETIMEDOUT;
7067c478bd9Sstevel@tonic-gate 	return (error);
7077c478bd9Sstevel@tonic-gate }
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate int
7107c478bd9Sstevel@tonic-gate rw_wrlock_impl(rwlock_t *rwlp, timespec_t *tsp)
7117c478bd9Sstevel@tonic-gate {
7127c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
7137c478bd9Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
7147c478bd9Sstevel@tonic-gate 	tdb_rwlock_stats_t *rwsp = RWLOCK_STATS(rwlp, udp);
7157c478bd9Sstevel@tonic-gate 	int error;
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	/*
7187c478bd9Sstevel@tonic-gate 	 * If we hold a readers lock on this rwlock, bail out.
7197c478bd9Sstevel@tonic-gate 	 */
720*7257d1b4Sraf 	if (rw_read_held(rwlp)) {
7217c478bd9Sstevel@tonic-gate 		if (self->ul_error_detection)
7227c478bd9Sstevel@tonic-gate 			rwlock_error(rwlp, "rwlock_wrlock",
7237c478bd9Sstevel@tonic-gate 			    "calling thread owns the readers lock");
72441efec22Sraf 		error = EDEADLK;
72541efec22Sraf 		goto out;
7267c478bd9Sstevel@tonic-gate 	}
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	/*
7297c478bd9Sstevel@tonic-gate 	 * If we hold the writer lock, bail out.
7307c478bd9Sstevel@tonic-gate 	 */
731*7257d1b4Sraf 	if (rw_write_held(rwlp)) {
7327c478bd9Sstevel@tonic-gate 		if (self->ul_error_detection)
7337c478bd9Sstevel@tonic-gate 			rwlock_error(rwlp, "rwlock_wrlock",
7347c478bd9Sstevel@tonic-gate 			    "calling thread owns the writer lock");
73541efec22Sraf 		error = EDEADLK;
73641efec22Sraf 		goto out;
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 
73941efec22Sraf 	if (write_lock_try(rwlp, 0))
74041efec22Sraf 		error = 0;
74141efec22Sraf 	else if (rwlp->rwlock_type == USYNC_PROCESS)	/* kernel-level */
7427c478bd9Sstevel@tonic-gate 		error = shared_rwlock_lock(rwlp, tsp, WRITE_LOCK);
74341efec22Sraf 	else						/* user-level */
7447c478bd9Sstevel@tonic-gate 		error = rwlock_lock(rwlp, tsp, WRITE_LOCK);
7457c478bd9Sstevel@tonic-gate 
74641efec22Sraf out:
74741efec22Sraf 	if (error == 0) {
74841efec22Sraf 		rwlp->rwlock_owner = (uintptr_t)self;
74941efec22Sraf 		if (rwlp->rwlock_type == USYNC_PROCESS)
75041efec22Sraf 			rwlp->rwlock_ownerpid = udp->pid;
75141efec22Sraf 		if (rwsp) {
7527c478bd9Sstevel@tonic-gate 			tdb_incr(rwsp->rw_wrlock);
7537c478bd9Sstevel@tonic-gate 			rwsp->rw_wrlock_begin_hold = gethrtime();
7547c478bd9Sstevel@tonic-gate 		}
75541efec22Sraf 		DTRACE_PROBE2(plockstat, rw__acquire, rwlp, WRITE_LOCK);
75641efec22Sraf 	} else {
75741efec22Sraf 		DTRACE_PROBE3(plockstat, rw__error, rwlp, WRITE_LOCK, error);
75841efec22Sraf 	}
7597c478bd9Sstevel@tonic-gate 	return (error);
7607c478bd9Sstevel@tonic-gate }
7617c478bd9Sstevel@tonic-gate 
762*7257d1b4Sraf #pragma weak pthread_rwlock_wrlock = rw_wrlock
763*7257d1b4Sraf #pragma weak _rw_wrlock = rw_wrlock
7647c478bd9Sstevel@tonic-gate int
765*7257d1b4Sraf rw_wrlock(rwlock_t *rwlp)
7667c478bd9Sstevel@tonic-gate {
7677c478bd9Sstevel@tonic-gate 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
7687c478bd9Sstevel@tonic-gate 	return (rw_wrlock_impl(rwlp, NULL));
7697c478bd9Sstevel@tonic-gate }
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate void
7727c478bd9Sstevel@tonic-gate lrw_wrlock(rwlock_t *rwlp)
7737c478bd9Sstevel@tonic-gate {
7747c478bd9Sstevel@tonic-gate 	enter_critical(curthread);
7757c478bd9Sstevel@tonic-gate 	(void) rw_wrlock_impl(rwlp, NULL);
7767c478bd9Sstevel@tonic-gate }
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate int
779*7257d1b4Sraf pthread_rwlock_reltimedwrlock_np(pthread_rwlock_t *_RESTRICT_KYWD rwlp,
780*7257d1b4Sraf     const struct timespec *_RESTRICT_KYWD reltime)
7817c478bd9Sstevel@tonic-gate {
7827c478bd9Sstevel@tonic-gate 	timespec_t tslocal = *reltime;
7837c478bd9Sstevel@tonic-gate 	int error;
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
786*7257d1b4Sraf 	error = rw_wrlock_impl((rwlock_t *)rwlp, &tslocal);
7877c478bd9Sstevel@tonic-gate 	if (error == ETIME)
7887c478bd9Sstevel@tonic-gate 		error = ETIMEDOUT;
7897c478bd9Sstevel@tonic-gate 	return (error);
7907c478bd9Sstevel@tonic-gate }
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate int
793*7257d1b4Sraf pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlp, const timespec_t *abstime)
7947c478bd9Sstevel@tonic-gate {
7957c478bd9Sstevel@tonic-gate 	timespec_t tslocal;
7967c478bd9Sstevel@tonic-gate 	int error;
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
7997c478bd9Sstevel@tonic-gate 	abstime_to_reltime(CLOCK_REALTIME, abstime, &tslocal);
800*7257d1b4Sraf 	error = rw_wrlock_impl((rwlock_t *)rwlp, &tslocal);
8017c478bd9Sstevel@tonic-gate 	if (error == ETIME)
8027c478bd9Sstevel@tonic-gate 		error = ETIMEDOUT;
8037c478bd9Sstevel@tonic-gate 	return (error);
8047c478bd9Sstevel@tonic-gate }
8057c478bd9Sstevel@tonic-gate 
806*7257d1b4Sraf #pragma weak pthread_rwlock_tryrdlock = rw_tryrdlock
8077c478bd9Sstevel@tonic-gate int
808*7257d1b4Sraf rw_tryrdlock(rwlock_t *rwlp)
8097c478bd9Sstevel@tonic-gate {
8107c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
8117c478bd9Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
8127c478bd9Sstevel@tonic-gate 	tdb_rwlock_stats_t *rwsp = RWLOCK_STATS(rwlp, udp);
8137c478bd9Sstevel@tonic-gate 	readlock_t *readlockp;
8147c478bd9Sstevel@tonic-gate 	int error;
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	if (rwsp)
8197c478bd9Sstevel@tonic-gate 		tdb_incr(rwsp->rw_rdlock_try);
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	/*
8227c478bd9Sstevel@tonic-gate 	 * If we already hold a readers lock on this rwlock,
8237c478bd9Sstevel@tonic-gate 	 * just increment our reference count and return.
8247c478bd9Sstevel@tonic-gate 	 */
82541efec22Sraf 	sigoff(self);
8267c478bd9Sstevel@tonic-gate 	readlockp = rwl_entry(rwlp);
8277c478bd9Sstevel@tonic-gate 	if (readlockp->rd_count != 0) {
82841efec22Sraf 		if (readlockp->rd_count == READ_LOCK_MAX) {
82941efec22Sraf 			sigon(self);
83041efec22Sraf 			error = EAGAIN;
83141efec22Sraf 			goto out;
8327c478bd9Sstevel@tonic-gate 		}
83341efec22Sraf 		sigon(self);
83441efec22Sraf 		error = 0;
83541efec22Sraf 		goto out;
83641efec22Sraf 	}
83741efec22Sraf 	sigon(self);
8387c478bd9Sstevel@tonic-gate 
83941efec22Sraf 	if (read_lock_try(rwlp, 0))
84041efec22Sraf 		error = 0;
84141efec22Sraf 	else if (rwlp->rwlock_type == USYNC_PROCESS)	/* kernel-level */
8427c478bd9Sstevel@tonic-gate 		error = shared_rwlock_lock(rwlp, NULL, READ_LOCK_TRY);
8437c478bd9Sstevel@tonic-gate 	else						/* user-level */
8447c478bd9Sstevel@tonic-gate 		error = rwlock_lock(rwlp, NULL, READ_LOCK_TRY);
8457c478bd9Sstevel@tonic-gate 
84641efec22Sraf out:
84741efec22Sraf 	if (error == 0) {
84841efec22Sraf 		sigoff(self);
84941efec22Sraf 		rwl_entry(rwlp)->rd_count++;
85041efec22Sraf 		sigon(self);
85141efec22Sraf 		DTRACE_PROBE2(plockstat, rw__acquire, rwlp, READ_LOCK);
85241efec22Sraf 	} else {
85341efec22Sraf 		if (rwsp)
8547c478bd9Sstevel@tonic-gate 			tdb_incr(rwsp->rw_rdlock_try_fail);
85541efec22Sraf 		if (error != EBUSY) {
85641efec22Sraf 			DTRACE_PROBE3(plockstat, rw__error, rwlp, READ_LOCK,
85741efec22Sraf 			    error);
85841efec22Sraf 		}
85941efec22Sraf 	}
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	return (error);
8627c478bd9Sstevel@tonic-gate }
8637c478bd9Sstevel@tonic-gate 
864*7257d1b4Sraf #pragma weak pthread_rwlock_trywrlock = rw_trywrlock
8657c478bd9Sstevel@tonic-gate int
866*7257d1b4Sraf rw_trywrlock(rwlock_t *rwlp)
8677c478bd9Sstevel@tonic-gate {
8687c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
8697c478bd9Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
8707c478bd9Sstevel@tonic-gate 	tdb_rwlock_stats_t *rwsp = RWLOCK_STATS(rwlp, udp);
8717c478bd9Sstevel@tonic-gate 	int error;
8727c478bd9Sstevel@tonic-gate 
87341efec22Sraf 	ASSERT(!self->ul_critical || self->ul_bindflags);
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	if (rwsp)
8767c478bd9Sstevel@tonic-gate 		tdb_incr(rwsp->rw_wrlock_try);
8777c478bd9Sstevel@tonic-gate 
87841efec22Sraf 	if (write_lock_try(rwlp, 0))
87941efec22Sraf 		error = 0;
88041efec22Sraf 	else if (rwlp->rwlock_type == USYNC_PROCESS)	/* kernel-level */
8817c478bd9Sstevel@tonic-gate 		error = shared_rwlock_lock(rwlp, NULL, WRITE_LOCK_TRY);
88241efec22Sraf 	else						/* user-level */
8837c478bd9Sstevel@tonic-gate 		error = rwlock_lock(rwlp, NULL, WRITE_LOCK_TRY);
88441efec22Sraf 
88541efec22Sraf 	if (error == 0) {
88641efec22Sraf 		rwlp->rwlock_owner = (uintptr_t)self;
88741efec22Sraf 		if (rwlp->rwlock_type == USYNC_PROCESS)
88841efec22Sraf 			rwlp->rwlock_ownerpid = udp->pid;
88941efec22Sraf 		if (rwsp)
8907c478bd9Sstevel@tonic-gate 			rwsp->rw_wrlock_begin_hold = gethrtime();
89141efec22Sraf 		DTRACE_PROBE2(plockstat, rw__acquire, rwlp, WRITE_LOCK);
89241efec22Sraf 	} else {
89341efec22Sraf 		if (rwsp)
89441efec22Sraf 			tdb_incr(rwsp->rw_wrlock_try_fail);
89541efec22Sraf 		if (error != EBUSY) {
89641efec22Sraf 			DTRACE_PROBE3(plockstat, rw__error, rwlp, WRITE_LOCK,
89741efec22Sraf 			    error);
89841efec22Sraf 		}
8997c478bd9Sstevel@tonic-gate 	}
9007c478bd9Sstevel@tonic-gate 	return (error);
9017c478bd9Sstevel@tonic-gate }
9027c478bd9Sstevel@tonic-gate 
903*7257d1b4Sraf #pragma weak pthread_rwlock_unlock = rw_unlock
904*7257d1b4Sraf #pragma weak _rw_unlock = rw_unlock
9057c478bd9Sstevel@tonic-gate int
906*7257d1b4Sraf rw_unlock(rwlock_t *rwlp)
9077c478bd9Sstevel@tonic-gate {
90841efec22Sraf 	volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers;
90941efec22Sraf 	uint32_t readers;
9107c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
9117c478bd9Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
9127c478bd9Sstevel@tonic-gate 	tdb_rwlock_stats_t *rwsp;
91341efec22Sraf 	queue_head_t *qp;
91441efec22Sraf 	int rd_wr;
91541efec22Sraf 	int waked = 0;
9167c478bd9Sstevel@tonic-gate 
91741efec22Sraf 	readers = *rwstate;
91841efec22Sraf 	ASSERT_CONSISTENT_STATE(readers);
91941efec22Sraf 	if (readers & URW_WRITE_LOCKED) {
92041efec22Sraf 		rd_wr = WRITE_LOCK;
92141efec22Sraf 		readers = 0;
92241efec22Sraf 	} else {
92341efec22Sraf 		rd_wr = READ_LOCK;
92441efec22Sraf 		readers &= URW_READERS_MASK;
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate 
92741efec22Sraf 	if (rd_wr == WRITE_LOCK) {
9287c478bd9Sstevel@tonic-gate 		/*
9297c478bd9Sstevel@tonic-gate 		 * Since the writer lock is held, we'd better be
9307c478bd9Sstevel@tonic-gate 		 * holding it, else we cannot legitimately be here.
9317c478bd9Sstevel@tonic-gate 		 */
932*7257d1b4Sraf 		if (!rw_write_held(rwlp)) {
9337c478bd9Sstevel@tonic-gate 			if (self->ul_error_detection)
9347c478bd9Sstevel@tonic-gate 				rwlock_error(rwlp, "rwlock_unlock",
9357c478bd9Sstevel@tonic-gate 				    "writer lock held, "
9367c478bd9Sstevel@tonic-gate 				    "but not by the calling thread");
9377c478bd9Sstevel@tonic-gate 			return (EPERM);
9387c478bd9Sstevel@tonic-gate 		}
9397c478bd9Sstevel@tonic-gate 		if ((rwsp = RWLOCK_STATS(rwlp, udp)) != NULL) {
9407c478bd9Sstevel@tonic-gate 			if (rwsp->rw_wrlock_begin_hold)
9417c478bd9Sstevel@tonic-gate 				rwsp->rw_wrlock_hold_time +=
9427c478bd9Sstevel@tonic-gate 				    gethrtime() - rwsp->rw_wrlock_begin_hold;
9437c478bd9Sstevel@tonic-gate 			rwsp->rw_wrlock_begin_hold = 0;
9447c478bd9Sstevel@tonic-gate 		}
94541efec22Sraf 		rwlp->rwlock_owner = 0;
94641efec22Sraf 		rwlp->rwlock_ownerpid = 0;
94741efec22Sraf 	} else if (readers > 0) {
9487c478bd9Sstevel@tonic-gate 		/*
9497c478bd9Sstevel@tonic-gate 		 * A readers lock is held; if we don't hold one, bail out.
9507c478bd9Sstevel@tonic-gate 		 */
95141efec22Sraf 		readlock_t *readlockp;
95241efec22Sraf 
95341efec22Sraf 		sigoff(self);
95441efec22Sraf 		readlockp = rwl_entry(rwlp);
9557c478bd9Sstevel@tonic-gate 		if (readlockp->rd_count == 0) {
95641efec22Sraf 			sigon(self);
9577c478bd9Sstevel@tonic-gate 			if (self->ul_error_detection)
9587c478bd9Sstevel@tonic-gate 				rwlock_error(rwlp, "rwlock_unlock",
9597c478bd9Sstevel@tonic-gate 				    "readers lock held, "
9607c478bd9Sstevel@tonic-gate 				    "but not by the calling thread");
9617c478bd9Sstevel@tonic-gate 			return (EPERM);
9627c478bd9Sstevel@tonic-gate 		}
9637c478bd9Sstevel@tonic-gate 		/*
9647c478bd9Sstevel@tonic-gate 		 * If we hold more than one readers lock on this rwlock,
9657c478bd9Sstevel@tonic-gate 		 * just decrement our reference count and return.
9667c478bd9Sstevel@tonic-gate 		 */
9677c478bd9Sstevel@tonic-gate 		if (--readlockp->rd_count != 0) {
96841efec22Sraf 			sigon(self);
96941efec22Sraf 			goto out;
9707c478bd9Sstevel@tonic-gate 		}
97141efec22Sraf 		sigon(self);
9727c478bd9Sstevel@tonic-gate 	} else {
9737c478bd9Sstevel@tonic-gate 		/*
9747c478bd9Sstevel@tonic-gate 		 * This is a usage error.
9757c478bd9Sstevel@tonic-gate 		 * No thread should release an unowned lock.
9767c478bd9Sstevel@tonic-gate 		 */
9777c478bd9Sstevel@tonic-gate 		if (self->ul_error_detection)
9787c478bd9Sstevel@tonic-gate 			rwlock_error(rwlp, "rwlock_unlock", "lock not owned");
9797c478bd9Sstevel@tonic-gate 		return (EPERM);
9807c478bd9Sstevel@tonic-gate 	}
9817c478bd9Sstevel@tonic-gate 
98241efec22Sraf 	if (rd_wr == WRITE_LOCK && write_unlock_try(rwlp)) {
98341efec22Sraf 		/* EMPTY */;
98441efec22Sraf 	} else if (rd_wr == READ_LOCK && read_unlock_try(rwlp)) {
98541efec22Sraf 		/* EMPTY */;
98641efec22Sraf 	} else if (rwlp->rwlock_type == USYNC_PROCESS) {
9878cd45542Sraf 		(void) mutex_lock(&rwlp->mutex);
98841efec22Sraf 		(void) __lwp_rwlock_unlock(rwlp);
9898cd45542Sraf 		(void) mutex_unlock(&rwlp->mutex);
99041efec22Sraf 		waked = 1;
9917c478bd9Sstevel@tonic-gate 	} else {
9927c478bd9Sstevel@tonic-gate 		qp = queue_lock(rwlp, MX);
99341efec22Sraf 		if (rd_wr == READ_LOCK)
99441efec22Sraf 			atomic_dec_32(rwstate);
99541efec22Sraf 		else
99641efec22Sraf 			atomic_and_32(rwstate, ~URW_WRITE_LOCKED);
9977c478bd9Sstevel@tonic-gate 		waked = rw_queue_release(qp, rwlp);
9987c478bd9Sstevel@tonic-gate 	}
9997c478bd9Sstevel@tonic-gate 
100041efec22Sraf out:
100141efec22Sraf 	DTRACE_PROBE2(plockstat, rw__release, rwlp, rd_wr);
100241efec22Sraf 
10037c478bd9Sstevel@tonic-gate 	/*
10047c478bd9Sstevel@tonic-gate 	 * Yield to the thread we just waked up, just in case we might
10057c478bd9Sstevel@tonic-gate 	 * be about to grab the rwlock again immediately upon return.
10067c478bd9Sstevel@tonic-gate 	 * This is pretty weak but it helps on a uniprocessor and also
10077c478bd9Sstevel@tonic-gate 	 * when cpu affinity has assigned both ourself and the other
10087c478bd9Sstevel@tonic-gate 	 * thread to the same CPU.  Note that lwp_yield() will yield
10097c478bd9Sstevel@tonic-gate 	 * the processor only if the writer is at the same or higher
10107c478bd9Sstevel@tonic-gate 	 * priority than ourself.  This provides more balanced program
10117c478bd9Sstevel@tonic-gate 	 * behavior; it doesn't guarantee acquisition of the lock by
10127c478bd9Sstevel@tonic-gate 	 * the pending writer.
10137c478bd9Sstevel@tonic-gate 	 */
10147c478bd9Sstevel@tonic-gate 	if (waked)
10158cd45542Sraf 		yield();
10167c478bd9Sstevel@tonic-gate 	return (0);
10177c478bd9Sstevel@tonic-gate }
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate void
10207c478bd9Sstevel@tonic-gate lrw_unlock(rwlock_t *rwlp)
10217c478bd9Sstevel@tonic-gate {
1022*7257d1b4Sraf 	(void) rw_unlock(rwlp);
10237c478bd9Sstevel@tonic-gate 	exit_critical(curthread);
10247c478bd9Sstevel@tonic-gate }
1025