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 /* 23*e54ab87fSRoger A. Faulkner * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #include "lint.h" 287c478bd9Sstevel@tonic-gate #include "thr_uberdata.h" 297c478bd9Sstevel@tonic-gate #include <sys/sdt.h> 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #define TRY_FLAG 0x10 327c478bd9Sstevel@tonic-gate #define READ_LOCK 0 337c478bd9Sstevel@tonic-gate #define WRITE_LOCK 1 347c478bd9Sstevel@tonic-gate #define READ_LOCK_TRY (READ_LOCK | TRY_FLAG) 357c478bd9Sstevel@tonic-gate #define WRITE_LOCK_TRY (WRITE_LOCK | TRY_FLAG) 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate #define NLOCKS 4 /* initial number of readlock_t structs allocated */ 387c478bd9Sstevel@tonic-gate 3941efec22Sraf #define ASSERT_CONSISTENT_STATE(readers) \ 4041efec22Sraf ASSERT(!((readers) & URW_WRITE_LOCKED) || \ 4141efec22Sraf ((readers) & ~URW_HAS_WAITERS) == URW_WRITE_LOCKED) 4241efec22Sraf 437c478bd9Sstevel@tonic-gate /* 447c478bd9Sstevel@tonic-gate * Find/allocate an entry for rwlp in our array of rwlocks held for reading. 4541efec22Sraf * We must be deferring signals for this to be safe. 46883492d5Sraf * Else if we are returning an entry with ul_rdlockcnt == 0, 4741efec22Sraf * it could be reassigned behind our back in a signal handler. 487c478bd9Sstevel@tonic-gate */ 497c478bd9Sstevel@tonic-gate static readlock_t * 507c478bd9Sstevel@tonic-gate rwl_entry(rwlock_t *rwlp) 517c478bd9Sstevel@tonic-gate { 527c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 537c478bd9Sstevel@tonic-gate readlock_t *remembered = NULL; 547c478bd9Sstevel@tonic-gate readlock_t *readlockp; 557c478bd9Sstevel@tonic-gate uint_t nlocks; 567c478bd9Sstevel@tonic-gate 5741efec22Sraf /* we must be deferring signals */ 5841efec22Sraf ASSERT((self->ul_critical + self->ul_sigdefer) != 0); 5941efec22Sraf 60883492d5Sraf if ((nlocks = self->ul_rdlockcnt) != 0) 617c478bd9Sstevel@tonic-gate readlockp = self->ul_readlock.array; 627c478bd9Sstevel@tonic-gate else { 637c478bd9Sstevel@tonic-gate nlocks = 1; 647c478bd9Sstevel@tonic-gate readlockp = &self->ul_readlock.single; 657c478bd9Sstevel@tonic-gate } 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate for (; nlocks; nlocks--, readlockp++) { 687c478bd9Sstevel@tonic-gate if (readlockp->rd_rwlock == rwlp) 697c478bd9Sstevel@tonic-gate return (readlockp); 707c478bd9Sstevel@tonic-gate if (readlockp->rd_count == 0 && remembered == NULL) 717c478bd9Sstevel@tonic-gate remembered = readlockp; 727c478bd9Sstevel@tonic-gate } 737c478bd9Sstevel@tonic-gate if (remembered != NULL) { 747c478bd9Sstevel@tonic-gate remembered->rd_rwlock = rwlp; 757c478bd9Sstevel@tonic-gate return (remembered); 767c478bd9Sstevel@tonic-gate } 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate /* 797c478bd9Sstevel@tonic-gate * No entry available. Allocate more space, converting the single 807c478bd9Sstevel@tonic-gate * readlock_t entry into an array of readlock_t entries if necessary. 817c478bd9Sstevel@tonic-gate */ 82883492d5Sraf if ((nlocks = self->ul_rdlockcnt) == 0) { 837c478bd9Sstevel@tonic-gate /* 847c478bd9Sstevel@tonic-gate * Initial allocation of the readlock_t array. 857c478bd9Sstevel@tonic-gate * Convert the single entry into an array. 867c478bd9Sstevel@tonic-gate */ 87883492d5Sraf self->ul_rdlockcnt = nlocks = NLOCKS; 887c478bd9Sstevel@tonic-gate readlockp = lmalloc(nlocks * sizeof (readlock_t)); 897c478bd9Sstevel@tonic-gate /* 907c478bd9Sstevel@tonic-gate * The single readlock_t becomes the first entry in the array. 917c478bd9Sstevel@tonic-gate */ 927c478bd9Sstevel@tonic-gate *readlockp = self->ul_readlock.single; 937c478bd9Sstevel@tonic-gate self->ul_readlock.single.rd_count = 0; 947c478bd9Sstevel@tonic-gate self->ul_readlock.array = readlockp; 957c478bd9Sstevel@tonic-gate /* 967c478bd9Sstevel@tonic-gate * Return the next available entry in the array. 977c478bd9Sstevel@tonic-gate */ 987c478bd9Sstevel@tonic-gate (++readlockp)->rd_rwlock = rwlp; 997c478bd9Sstevel@tonic-gate return (readlockp); 1007c478bd9Sstevel@tonic-gate } 1017c478bd9Sstevel@tonic-gate /* 1027c478bd9Sstevel@tonic-gate * Reallocate the array, double the size each time. 1037c478bd9Sstevel@tonic-gate */ 1047c478bd9Sstevel@tonic-gate readlockp = lmalloc(nlocks * 2 * sizeof (readlock_t)); 1058cd45542Sraf (void) memcpy(readlockp, self->ul_readlock.array, 1067c478bd9Sstevel@tonic-gate nlocks * sizeof (readlock_t)); 1077c478bd9Sstevel@tonic-gate lfree(self->ul_readlock.array, nlocks * sizeof (readlock_t)); 1087c478bd9Sstevel@tonic-gate self->ul_readlock.array = readlockp; 109883492d5Sraf self->ul_rdlockcnt *= 2; 1107c478bd9Sstevel@tonic-gate /* 1117c478bd9Sstevel@tonic-gate * Return the next available entry in the newly allocated array. 1127c478bd9Sstevel@tonic-gate */ 1137c478bd9Sstevel@tonic-gate (readlockp += nlocks)->rd_rwlock = rwlp; 1147c478bd9Sstevel@tonic-gate return (readlockp); 1157c478bd9Sstevel@tonic-gate } 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate /* 1187c478bd9Sstevel@tonic-gate * Free the array of rwlocks held for reading. 1197c478bd9Sstevel@tonic-gate */ 1207c478bd9Sstevel@tonic-gate void 1217c478bd9Sstevel@tonic-gate rwl_free(ulwp_t *ulwp) 1227c478bd9Sstevel@tonic-gate { 1237c478bd9Sstevel@tonic-gate uint_t nlocks; 1247c478bd9Sstevel@tonic-gate 125883492d5Sraf if ((nlocks = ulwp->ul_rdlockcnt) != 0) 1267c478bd9Sstevel@tonic-gate lfree(ulwp->ul_readlock.array, nlocks * sizeof (readlock_t)); 127883492d5Sraf ulwp->ul_rdlockcnt = 0; 1287c478bd9Sstevel@tonic-gate ulwp->ul_readlock.single.rd_rwlock = NULL; 1297c478bd9Sstevel@tonic-gate ulwp->ul_readlock.single.rd_count = 0; 1307c478bd9Sstevel@tonic-gate } 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate /* 1337c478bd9Sstevel@tonic-gate * Check if a reader version of the lock is held by the current thread. 1347c478bd9Sstevel@tonic-gate */ 1357257d1b4Sraf #pragma weak _rw_read_held = rw_read_held 1367c478bd9Sstevel@tonic-gate int 1377257d1b4Sraf rw_read_held(rwlock_t *rwlp) 1387c478bd9Sstevel@tonic-gate { 13941efec22Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 14041efec22Sraf uint32_t readers; 14141efec22Sraf ulwp_t *self = curthread; 1427c478bd9Sstevel@tonic-gate readlock_t *readlockp; 1437c478bd9Sstevel@tonic-gate uint_t nlocks; 14441efec22Sraf int rval = 0; 1457c478bd9Sstevel@tonic-gate 14641efec22Sraf no_preempt(self); 1477c478bd9Sstevel@tonic-gate 14841efec22Sraf readers = *rwstate; 14941efec22Sraf ASSERT_CONSISTENT_STATE(readers); 15041efec22Sraf if (!(readers & URW_WRITE_LOCKED) && 15141efec22Sraf (readers & URW_READERS_MASK) != 0) { 1527c478bd9Sstevel@tonic-gate /* 1537c478bd9Sstevel@tonic-gate * The lock is held for reading by some thread. 1547c478bd9Sstevel@tonic-gate * Search our array of rwlocks held for reading for a match. 1557c478bd9Sstevel@tonic-gate */ 156883492d5Sraf if ((nlocks = self->ul_rdlockcnt) != 0) 1577c478bd9Sstevel@tonic-gate readlockp = self->ul_readlock.array; 1587c478bd9Sstevel@tonic-gate else { 1597c478bd9Sstevel@tonic-gate nlocks = 1; 1607c478bd9Sstevel@tonic-gate readlockp = &self->ul_readlock.single; 1617c478bd9Sstevel@tonic-gate } 16241efec22Sraf for (; nlocks; nlocks--, readlockp++) { 16341efec22Sraf if (readlockp->rd_rwlock == rwlp) { 16441efec22Sraf if (readlockp->rd_count) 16541efec22Sraf rval = 1; 16641efec22Sraf break; 16741efec22Sraf } 16841efec22Sraf } 16941efec22Sraf } 1707c478bd9Sstevel@tonic-gate 17141efec22Sraf preempt(self); 17241efec22Sraf return (rval); 1737c478bd9Sstevel@tonic-gate } 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate /* 1767c478bd9Sstevel@tonic-gate * Check if a writer version of the lock is held by the current thread. 1777c478bd9Sstevel@tonic-gate */ 1787257d1b4Sraf #pragma weak _rw_write_held = rw_write_held 1797c478bd9Sstevel@tonic-gate int 1807257d1b4Sraf rw_write_held(rwlock_t *rwlp) 1817c478bd9Sstevel@tonic-gate { 18241efec22Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 18341efec22Sraf uint32_t readers; 1847c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 18541efec22Sraf int rval; 1867c478bd9Sstevel@tonic-gate 18741efec22Sraf no_preempt(self); 1887c478bd9Sstevel@tonic-gate 18941efec22Sraf readers = *rwstate; 19041efec22Sraf ASSERT_CONSISTENT_STATE(readers); 19141efec22Sraf rval = ((readers & URW_WRITE_LOCKED) && 19241efec22Sraf rwlp->rwlock_owner == (uintptr_t)self && 19341efec22Sraf (rwlp->rwlock_type == USYNC_THREAD || 19441efec22Sraf rwlp->rwlock_ownerpid == self->ul_uberdata->pid)); 19541efec22Sraf 19641efec22Sraf preempt(self); 19741efec22Sraf return (rval); 1987c478bd9Sstevel@tonic-gate } 1997c478bd9Sstevel@tonic-gate 2007257d1b4Sraf #pragma weak _rwlock_init = rwlock_init 2017c478bd9Sstevel@tonic-gate /* ARGSUSED2 */ 2027c478bd9Sstevel@tonic-gate int 2037257d1b4Sraf rwlock_init(rwlock_t *rwlp, int type, void *arg) 2047c478bd9Sstevel@tonic-gate { 2057c5714f6Sraf ulwp_t *self = curthread; 2067c5714f6Sraf 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 */ 2137c5714f6Sraf sigoff(self); 2147c478bd9Sstevel@tonic-gate rwl_entry(rwlp)->rd_count = 0; 2157c5714f6Sraf sigon(self); 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; 2227c5714f6Sraf 2237c5714f6Sraf /* 2247c5714f6Sraf * This should be at the beginning of the function, 2257c5714f6Sraf * but for the sake of old broken applications that 2267c5714f6Sraf * do not have proper alignment for their rwlocks 2277c5714f6Sraf * (and don't check the return code from rwlock_init), 2287c5714f6Sraf * we put it here, after initializing the rwlock regardless. 2297c5714f6Sraf */ 2307c5714f6Sraf if (((uintptr_t)rwlp & (_LONG_LONG_ALIGNMENT - 1)) && 2317c5714f6Sraf self->ul_misaligned == 0) 2327c5714f6Sraf return (EINVAL); 2337c5714f6Sraf 2347c478bd9Sstevel@tonic-gate return (0); 2357c478bd9Sstevel@tonic-gate } 2367c478bd9Sstevel@tonic-gate 2377257d1b4Sraf #pragma weak pthread_rwlock_destroy = rwlock_destroy 2387257d1b4Sraf #pragma weak _rwlock_destroy = rwlock_destroy 2397c478bd9Sstevel@tonic-gate int 2407257d1b4Sraf rwlock_destroy(rwlock_t *rwlp) 2417c478bd9Sstevel@tonic-gate { 242*e54ab87fSRoger A. Faulkner ulwp_t *self = curthread; 243*e54ab87fSRoger A. Faulkner 2447c478bd9Sstevel@tonic-gate /* 2457c478bd9Sstevel@tonic-gate * Once destroyed, we can no longer be holding a read or write lock. 2467c478bd9Sstevel@tonic-gate * We can do nothing about other threads that are holding read locks. 2477c478bd9Sstevel@tonic-gate */ 248*e54ab87fSRoger A. Faulkner sigoff(self); 2497c478bd9Sstevel@tonic-gate rwl_entry(rwlp)->rd_count = 0; 250*e54ab87fSRoger A. Faulkner sigon(self); 2517c478bd9Sstevel@tonic-gate rwlp->rwlock_magic = 0; 2527c478bd9Sstevel@tonic-gate tdb_sync_obj_deregister(rwlp); 2537c478bd9Sstevel@tonic-gate return (0); 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate /* 25741efec22Sraf * Attempt to acquire a readers lock. Return true on success. 2587c478bd9Sstevel@tonic-gate */ 2597c478bd9Sstevel@tonic-gate static int 26041efec22Sraf read_lock_try(rwlock_t *rwlp, int ignore_waiters_flag) 2617c478bd9Sstevel@tonic-gate { 26241efec22Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 26341efec22Sraf uint32_t mask = ignore_waiters_flag? 26441efec22Sraf URW_WRITE_LOCKED : (URW_HAS_WAITERS | URW_WRITE_LOCKED); 26541efec22Sraf uint32_t readers; 2667c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate no_preempt(self); 26941efec22Sraf while (((readers = *rwstate) & mask) == 0) { 27041efec22Sraf if (atomic_cas_32(rwstate, readers, readers + 1) == readers) { 2717c478bd9Sstevel@tonic-gate preempt(self); 2727c478bd9Sstevel@tonic-gate return (1); 2737c478bd9Sstevel@tonic-gate } 2747c478bd9Sstevel@tonic-gate } 27541efec22Sraf preempt(self); 27641efec22Sraf return (0); 2777c478bd9Sstevel@tonic-gate } 27841efec22Sraf 27941efec22Sraf /* 28041efec22Sraf * Attempt to release a reader lock. Return true on success. 28141efec22Sraf */ 28241efec22Sraf static int 28341efec22Sraf read_unlock_try(rwlock_t *rwlp) 28441efec22Sraf { 28541efec22Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 28641efec22Sraf uint32_t readers; 28741efec22Sraf ulwp_t *self = curthread; 28841efec22Sraf 28941efec22Sraf no_preempt(self); 29041efec22Sraf while (((readers = *rwstate) & URW_HAS_WAITERS) == 0) { 29141efec22Sraf if (atomic_cas_32(rwstate, readers, readers - 1) == readers) { 29241efec22Sraf preempt(self); 29341efec22Sraf return (1); 29441efec22Sraf } 29541efec22Sraf } 29641efec22Sraf preempt(self); 29741efec22Sraf return (0); 29841efec22Sraf } 29941efec22Sraf 30041efec22Sraf /* 30141efec22Sraf * Attempt to acquire a writer lock. Return true on success. 30241efec22Sraf */ 30341efec22Sraf static int 30441efec22Sraf write_lock_try(rwlock_t *rwlp, int ignore_waiters_flag) 30541efec22Sraf { 30641efec22Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 30741efec22Sraf uint32_t mask = ignore_waiters_flag? 30841efec22Sraf (URW_WRITE_LOCKED | URW_READERS_MASK) : 30941efec22Sraf (URW_HAS_WAITERS | URW_WRITE_LOCKED | URW_READERS_MASK); 31041efec22Sraf ulwp_t *self = curthread; 31141efec22Sraf uint32_t readers; 31241efec22Sraf 31341efec22Sraf no_preempt(self); 31441efec22Sraf while (((readers = *rwstate) & mask) == 0) { 31541efec22Sraf if (atomic_cas_32(rwstate, readers, readers | URW_WRITE_LOCKED) 31641efec22Sraf == readers) { 31741efec22Sraf preempt(self); 31841efec22Sraf return (1); 31941efec22Sraf } 32041efec22Sraf } 32141efec22Sraf preempt(self); 32241efec22Sraf return (0); 32341efec22Sraf } 32441efec22Sraf 32541efec22Sraf /* 32641efec22Sraf * Attempt to release a writer lock. Return true on success. 32741efec22Sraf */ 32841efec22Sraf static int 32941efec22Sraf write_unlock_try(rwlock_t *rwlp) 33041efec22Sraf { 33141efec22Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 33241efec22Sraf uint32_t readers; 33341efec22Sraf ulwp_t *self = curthread; 33441efec22Sraf 33541efec22Sraf no_preempt(self); 33641efec22Sraf while (((readers = *rwstate) & URW_HAS_WAITERS) == 0) { 33741efec22Sraf if (atomic_cas_32(rwstate, readers, 0) == readers) { 33841efec22Sraf preempt(self); 33941efec22Sraf return (1); 34041efec22Sraf } 34141efec22Sraf } 34241efec22Sraf preempt(self); 34341efec22Sraf return (0); 34441efec22Sraf } 34541efec22Sraf 34641efec22Sraf /* 34741efec22Sraf * Wake up thread(s) sleeping on the rwlock queue and then 34841efec22Sraf * drop the queue lock. Return non-zero if we wake up someone. 34941efec22Sraf * This is called when a thread releases a lock that appears to have waiters. 35041efec22Sraf */ 35141efec22Sraf static int 35241efec22Sraf rw_queue_release(queue_head_t *qp, rwlock_t *rwlp) 35341efec22Sraf { 35441efec22Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 35541efec22Sraf uint32_t readers; 35641efec22Sraf uint32_t writers; 35741efec22Sraf ulwp_t **ulwpp; 35841efec22Sraf ulwp_t *ulwp; 359d4204c85Sraf ulwp_t *prev; 360d4204c85Sraf int nlwpid = 0; 361d4204c85Sraf int more; 362d4204c85Sraf int maxlwps = MAXLWPS; 36341efec22Sraf lwpid_t buffer[MAXLWPS]; 36441efec22Sraf lwpid_t *lwpid = buffer; 36541efec22Sraf 36641efec22Sraf readers = *rwstate; 36741efec22Sraf ASSERT_CONSISTENT_STATE(readers); 36841efec22Sraf if (!(readers & URW_HAS_WAITERS)) { 3697c478bd9Sstevel@tonic-gate queue_unlock(qp); 3707c478bd9Sstevel@tonic-gate return (0); 3717c478bd9Sstevel@tonic-gate } 37241efec22Sraf readers &= URW_READERS_MASK; 37341efec22Sraf writers = 0; 37441efec22Sraf 37541efec22Sraf /* 376d4204c85Sraf * Examine the queue of waiters in priority order and prepare 377d4204c85Sraf * to wake up as many readers as we encounter before encountering 378d4204c85Sraf * a writer. If the highest priority thread on the queue is a 37941efec22Sraf * writer, stop there and wake it up. 38041efec22Sraf * 38141efec22Sraf * We keep track of lwpids that are to be unparked in lwpid[]. 38241efec22Sraf * __lwp_unpark_all() is called to unpark all of them after 38341efec22Sraf * they have been removed from the sleep queue and the sleep 38441efec22Sraf * queue lock has been dropped. If we run out of space in our 38541efec22Sraf * on-stack buffer, we need to allocate more but we can't call 38641efec22Sraf * lmalloc() because we are holding a queue lock when the overflow 38741efec22Sraf * occurs and lmalloc() acquires a lock. We can't use alloca() 38841efec22Sraf * either because the application may have allocated a small 38941efec22Sraf * stack and we don't want to overrun the stack. So we call 39041efec22Sraf * alloc_lwpids() to allocate a bigger buffer using the mmap() 39141efec22Sraf * system call directly since that path acquires no locks. 39241efec22Sraf */ 393d4204c85Sraf while ((ulwpp = queue_slot(qp, &prev, &more)) != NULL) { 394d4204c85Sraf ulwp = *ulwpp; 395d4204c85Sraf ASSERT(ulwp->ul_wchan == rwlp); 39641efec22Sraf if (ulwp->ul_writer) { 39741efec22Sraf if (writers != 0 || readers != 0) 39841efec22Sraf break; 39941efec22Sraf /* one writer to wake */ 40041efec22Sraf writers++; 40141efec22Sraf } else { 40241efec22Sraf if (writers != 0) 40341efec22Sraf break; 40441efec22Sraf /* at least one reader to wake */ 40541efec22Sraf readers++; 40641efec22Sraf if (nlwpid == maxlwps) 40741efec22Sraf lwpid = alloc_lwpids(lwpid, &nlwpid, &maxlwps); 40841efec22Sraf } 409d4204c85Sraf queue_unlink(qp, ulwpp, prev); 410d4204c85Sraf ulwp->ul_sleepq = NULL; 411d4204c85Sraf ulwp->ul_wchan = NULL; 41241efec22Sraf lwpid[nlwpid++] = ulwp->ul_lwpid; 41341efec22Sraf } 414d4204c85Sraf if (ulwpp == NULL) 41541efec22Sraf atomic_and_32(rwstate, ~URW_HAS_WAITERS); 41641efec22Sraf if (nlwpid == 0) { 41741efec22Sraf queue_unlock(qp); 41841efec22Sraf } else { 419d4204c85Sraf ulwp_t *self = curthread; 42041efec22Sraf no_preempt(self); 42141efec22Sraf queue_unlock(qp); 42241efec22Sraf if (nlwpid == 1) 42341efec22Sraf (void) __lwp_unpark(lwpid[0]); 42441efec22Sraf else 42541efec22Sraf (void) __lwp_unpark_all(lwpid, nlwpid); 42641efec22Sraf preempt(self); 42741efec22Sraf } 42841efec22Sraf if (lwpid != buffer) 4298cd45542Sraf (void) munmap((caddr_t)lwpid, maxlwps * sizeof (lwpid_t)); 43041efec22Sraf return (nlwpid != 0); 43141efec22Sraf } 4327c478bd9Sstevel@tonic-gate 4337c478bd9Sstevel@tonic-gate /* 4347c478bd9Sstevel@tonic-gate * Common code for rdlock, timedrdlock, wrlock, timedwrlock, tryrdlock, 4357c478bd9Sstevel@tonic-gate * and trywrlock for process-shared (USYNC_PROCESS) rwlocks. 4367c478bd9Sstevel@tonic-gate * 4377c478bd9Sstevel@tonic-gate * Note: if the lock appears to be contended we call __lwp_rwlock_rdlock() 4387c478bd9Sstevel@tonic-gate * or __lwp_rwlock_wrlock() holding the mutex. These return with the mutex 4397c478bd9Sstevel@tonic-gate * released, and if they need to sleep will release the mutex first. In the 4407c478bd9Sstevel@tonic-gate * event of a spurious wakeup, these will return EAGAIN (because it is much 4417c478bd9Sstevel@tonic-gate * easier for us to re-acquire the mutex here). 4427c478bd9Sstevel@tonic-gate */ 4437c478bd9Sstevel@tonic-gate int 4447c478bd9Sstevel@tonic-gate shared_rwlock_lock(rwlock_t *rwlp, timespec_t *tsp, int rd_wr) 4457c478bd9Sstevel@tonic-gate { 44641efec22Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 44741efec22Sraf mutex_t *mp = &rwlp->mutex; 44841efec22Sraf uint32_t readers; 4497c478bd9Sstevel@tonic-gate int try_flag; 45041efec22Sraf int error; 4517c478bd9Sstevel@tonic-gate 4527c478bd9Sstevel@tonic-gate try_flag = (rd_wr & TRY_FLAG); 4537c478bd9Sstevel@tonic-gate rd_wr &= ~TRY_FLAG; 4547c478bd9Sstevel@tonic-gate ASSERT(rd_wr == READ_LOCK || rd_wr == WRITE_LOCK); 4557c478bd9Sstevel@tonic-gate 4567c478bd9Sstevel@tonic-gate if (!try_flag) { 4577c478bd9Sstevel@tonic-gate DTRACE_PROBE2(plockstat, rw__block, rwlp, rd_wr); 4587c478bd9Sstevel@tonic-gate } 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate do { 46141efec22Sraf if (try_flag && (*rwstate & URW_WRITE_LOCKED)) { 46241efec22Sraf error = EBUSY; 4637c478bd9Sstevel@tonic-gate break; 46441efec22Sraf } 4658cd45542Sraf if ((error = mutex_lock(mp)) != 0) 46641efec22Sraf break; 4677c478bd9Sstevel@tonic-gate if (rd_wr == READ_LOCK) { 46841efec22Sraf if (read_lock_try(rwlp, 0)) { 4698cd45542Sraf (void) mutex_unlock(mp); 47041efec22Sraf break; 4717c478bd9Sstevel@tonic-gate } 4727c478bd9Sstevel@tonic-gate } else { 47341efec22Sraf if (write_lock_try(rwlp, 0)) { 4748cd45542Sraf (void) mutex_unlock(mp); 47541efec22Sraf break; 4767c478bd9Sstevel@tonic-gate } 47741efec22Sraf } 47841efec22Sraf atomic_or_32(rwstate, URW_HAS_WAITERS); 47941efec22Sraf readers = *rwstate; 48041efec22Sraf ASSERT_CONSISTENT_STATE(readers); 4817c478bd9Sstevel@tonic-gate /* 48241efec22Sraf * The calls to __lwp_rwlock_*() below will release the mutex, 483328cc3e9SRoger A. Faulkner * so we need a dtrace probe here. The owner field of the 484328cc3e9SRoger A. Faulkner * mutex is cleared in the kernel when the mutex is released, 485328cc3e9SRoger A. Faulkner * so we should not clear it here. 4867c478bd9Sstevel@tonic-gate */ 48741efec22Sraf DTRACE_PROBE2(plockstat, mutex__release, mp, 0); 4887c478bd9Sstevel@tonic-gate /* 4897c478bd9Sstevel@tonic-gate * The waiters bit may be inaccurate. 4907c478bd9Sstevel@tonic-gate * Only the kernel knows for sure. 4917c478bd9Sstevel@tonic-gate */ 49241efec22Sraf if (rd_wr == READ_LOCK) { 49341efec22Sraf if (try_flag) 49441efec22Sraf error = __lwp_rwlock_tryrdlock(rwlp); 49541efec22Sraf else 49641efec22Sraf error = __lwp_rwlock_rdlock(rwlp, tsp); 4977c478bd9Sstevel@tonic-gate } else { 49841efec22Sraf if (try_flag) 49941efec22Sraf error = __lwp_rwlock_trywrlock(rwlp); 50041efec22Sraf else 5017c478bd9Sstevel@tonic-gate error = __lwp_rwlock_wrlock(rwlp, tsp); 5027c478bd9Sstevel@tonic-gate } 50341efec22Sraf } while (error == EAGAIN || error == EINTR); 5047c478bd9Sstevel@tonic-gate 5057c478bd9Sstevel@tonic-gate if (!try_flag) { 50641efec22Sraf DTRACE_PROBE3(plockstat, rw__blocked, rwlp, rd_wr, error == 0); 5077c478bd9Sstevel@tonic-gate } 5087c478bd9Sstevel@tonic-gate 5097c478bd9Sstevel@tonic-gate return (error); 5107c478bd9Sstevel@tonic-gate } 5117c478bd9Sstevel@tonic-gate 5127c478bd9Sstevel@tonic-gate /* 5137c478bd9Sstevel@tonic-gate * Common code for rdlock, timedrdlock, wrlock, timedwrlock, tryrdlock, 5147c478bd9Sstevel@tonic-gate * and trywrlock for process-private (USYNC_THREAD) rwlocks. 5157c478bd9Sstevel@tonic-gate */ 5167c478bd9Sstevel@tonic-gate int 5177c478bd9Sstevel@tonic-gate rwlock_lock(rwlock_t *rwlp, timespec_t *tsp, int rd_wr) 5187c478bd9Sstevel@tonic-gate { 51941efec22Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 52041efec22Sraf uint32_t readers; 5217c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 5227c478bd9Sstevel@tonic-gate queue_head_t *qp; 5237c478bd9Sstevel@tonic-gate ulwp_t *ulwp; 5247c478bd9Sstevel@tonic-gate int try_flag; 525d4204c85Sraf int ignore_waiters_flag; 5267c478bd9Sstevel@tonic-gate int error = 0; 5277c478bd9Sstevel@tonic-gate 5287c478bd9Sstevel@tonic-gate try_flag = (rd_wr & TRY_FLAG); 5297c478bd9Sstevel@tonic-gate rd_wr &= ~TRY_FLAG; 5307c478bd9Sstevel@tonic-gate ASSERT(rd_wr == READ_LOCK || rd_wr == WRITE_LOCK); 5317c478bd9Sstevel@tonic-gate 5327c478bd9Sstevel@tonic-gate if (!try_flag) { 5337c478bd9Sstevel@tonic-gate DTRACE_PROBE2(plockstat, rw__block, rwlp, rd_wr); 5347c478bd9Sstevel@tonic-gate } 5357c478bd9Sstevel@tonic-gate 5367c478bd9Sstevel@tonic-gate qp = queue_lock(rwlp, MX); 537d4204c85Sraf /* initial attempt to acquire the lock fails if there are waiters */ 538d4204c85Sraf ignore_waiters_flag = 0; 5397c478bd9Sstevel@tonic-gate while (error == 0) { 54041efec22Sraf if (rd_wr == READ_LOCK) { 541d4204c85Sraf if (read_lock_try(rwlp, ignore_waiters_flag)) 542d4204c85Sraf break; 54341efec22Sraf } else { 544d4204c85Sraf if (write_lock_try(rwlp, ignore_waiters_flag)) 545d4204c85Sraf break; 54641efec22Sraf } 547d4204c85Sraf /* subsequent attempts do not fail due to waiters */ 548d4204c85Sraf ignore_waiters_flag = 1; 54941efec22Sraf atomic_or_32(rwstate, URW_HAS_WAITERS); 55041efec22Sraf readers = *rwstate; 55141efec22Sraf ASSERT_CONSISTENT_STATE(readers); 55241efec22Sraf if ((readers & URW_WRITE_LOCKED) || 55341efec22Sraf (rd_wr == WRITE_LOCK && 55441efec22Sraf (readers & URW_READERS_MASK) != 0)) 5557c478bd9Sstevel@tonic-gate /* EMPTY */; /* somebody holds the lock */ 556d4204c85Sraf else if ((ulwp = queue_waiter(qp)) == NULL) { 55741efec22Sraf atomic_and_32(rwstate, ~URW_HAS_WAITERS); 558d4204c85Sraf continue; /* no queued waiters, try again */ 5597c478bd9Sstevel@tonic-gate } else { 560d4204c85Sraf /* 561d4204c85Sraf * Do a priority check on the queued waiter (the 562d4204c85Sraf * highest priority thread on the queue) to see 563d4204c85Sraf * if we should defer to him or just grab the lock. 564d4204c85Sraf */ 5657c478bd9Sstevel@tonic-gate int our_pri = real_priority(self); 5667c478bd9Sstevel@tonic-gate int his_pri = real_priority(ulwp); 5677c478bd9Sstevel@tonic-gate 5687c478bd9Sstevel@tonic-gate if (rd_wr == WRITE_LOCK) { 5697c478bd9Sstevel@tonic-gate /* 5707c478bd9Sstevel@tonic-gate * We defer to a queued thread that has 5717c478bd9Sstevel@tonic-gate * a higher priority than ours. 5727c478bd9Sstevel@tonic-gate */ 5737c478bd9Sstevel@tonic-gate if (his_pri <= our_pri) 574d4204c85Sraf continue; /* try again */ 5757c478bd9Sstevel@tonic-gate } else { 5767c478bd9Sstevel@tonic-gate /* 5777c478bd9Sstevel@tonic-gate * We defer to a queued thread that has 5787c478bd9Sstevel@tonic-gate * a higher priority than ours or that 5797c478bd9Sstevel@tonic-gate * is a writer whose priority equals ours. 5807c478bd9Sstevel@tonic-gate */ 5817c478bd9Sstevel@tonic-gate if (his_pri < our_pri || 5827c478bd9Sstevel@tonic-gate (his_pri == our_pri && !ulwp->ul_writer)) 583d4204c85Sraf continue; /* try again */ 5847c478bd9Sstevel@tonic-gate } 5857c478bd9Sstevel@tonic-gate } 5867c478bd9Sstevel@tonic-gate /* 5877c478bd9Sstevel@tonic-gate * We are about to block. 5887c478bd9Sstevel@tonic-gate * If we're doing a trylock, return EBUSY instead. 5897c478bd9Sstevel@tonic-gate */ 5907c478bd9Sstevel@tonic-gate if (try_flag) { 5917c478bd9Sstevel@tonic-gate error = EBUSY; 5927c478bd9Sstevel@tonic-gate break; 5937c478bd9Sstevel@tonic-gate } 5947c478bd9Sstevel@tonic-gate /* 595d4204c85Sraf * Enqueue writers ahead of readers. 5967c478bd9Sstevel@tonic-gate */ 5977c478bd9Sstevel@tonic-gate self->ul_writer = rd_wr; /* *must* be 0 or 1 */ 598d4204c85Sraf enqueue(qp, self, 0); 5997c478bd9Sstevel@tonic-gate set_parking_flag(self, 1); 6007c478bd9Sstevel@tonic-gate queue_unlock(qp); 6017c478bd9Sstevel@tonic-gate if ((error = __lwp_park(tsp, 0)) == EINTR) 602d4204c85Sraf error = ignore_waiters_flag = 0; 6037c478bd9Sstevel@tonic-gate set_parking_flag(self, 0); 6047c478bd9Sstevel@tonic-gate qp = queue_lock(rwlp, MX); 605d4204c85Sraf if (self->ul_sleepq && dequeue_self(qp) == 0) 60641efec22Sraf atomic_and_32(rwstate, ~URW_HAS_WAITERS); 607d4204c85Sraf self->ul_writer = 0; 6087c478bd9Sstevel@tonic-gate } 6097c478bd9Sstevel@tonic-gate 61041efec22Sraf queue_unlock(qp); 61141efec22Sraf 61241efec22Sraf if (!try_flag) { 61341efec22Sraf DTRACE_PROBE3(plockstat, rw__blocked, rwlp, rd_wr, error == 0); 61441efec22Sraf } 6157c478bd9Sstevel@tonic-gate 6167c478bd9Sstevel@tonic-gate return (error); 6177c478bd9Sstevel@tonic-gate } 6187c478bd9Sstevel@tonic-gate 6197c478bd9Sstevel@tonic-gate int 6207c478bd9Sstevel@tonic-gate rw_rdlock_impl(rwlock_t *rwlp, timespec_t *tsp) 6217c478bd9Sstevel@tonic-gate { 6227c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 6237c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 6247c478bd9Sstevel@tonic-gate readlock_t *readlockp; 6257c478bd9Sstevel@tonic-gate tdb_rwlock_stats_t *rwsp = RWLOCK_STATS(rwlp, udp); 6267c478bd9Sstevel@tonic-gate int error; 6277c478bd9Sstevel@tonic-gate 6287c478bd9Sstevel@tonic-gate /* 6297c478bd9Sstevel@tonic-gate * If we already hold a readers lock on this rwlock, 6307c478bd9Sstevel@tonic-gate * just increment our reference count and return. 6317c478bd9Sstevel@tonic-gate */ 63241efec22Sraf sigoff(self); 6337c478bd9Sstevel@tonic-gate readlockp = rwl_entry(rwlp); 6347c478bd9Sstevel@tonic-gate if (readlockp->rd_count != 0) { 63541efec22Sraf if (readlockp->rd_count == READ_LOCK_MAX) { 63641efec22Sraf sigon(self); 63741efec22Sraf error = EAGAIN; 63841efec22Sraf goto out; 6397c478bd9Sstevel@tonic-gate } 64041efec22Sraf sigon(self); 64141efec22Sraf error = 0; 64241efec22Sraf goto out; 64341efec22Sraf } 64441efec22Sraf sigon(self); 6457c478bd9Sstevel@tonic-gate 6467c478bd9Sstevel@tonic-gate /* 6477c478bd9Sstevel@tonic-gate * If we hold the writer lock, bail out. 6487c478bd9Sstevel@tonic-gate */ 6497257d1b4Sraf if (rw_write_held(rwlp)) { 6507c478bd9Sstevel@tonic-gate if (self->ul_error_detection) 6517c478bd9Sstevel@tonic-gate rwlock_error(rwlp, "rwlock_rdlock", 6527c478bd9Sstevel@tonic-gate "calling thread owns the writer lock"); 65341efec22Sraf error = EDEADLK; 65441efec22Sraf goto out; 6557c478bd9Sstevel@tonic-gate } 6567c478bd9Sstevel@tonic-gate 65741efec22Sraf if (read_lock_try(rwlp, 0)) 65841efec22Sraf error = 0; 65941efec22Sraf else if (rwlp->rwlock_type == USYNC_PROCESS) /* kernel-level */ 6607c478bd9Sstevel@tonic-gate error = shared_rwlock_lock(rwlp, tsp, READ_LOCK); 6617c478bd9Sstevel@tonic-gate else /* user-level */ 6627c478bd9Sstevel@tonic-gate error = rwlock_lock(rwlp, tsp, READ_LOCK); 6637c478bd9Sstevel@tonic-gate 66441efec22Sraf out: 6657c478bd9Sstevel@tonic-gate if (error == 0) { 66641efec22Sraf sigoff(self); 66741efec22Sraf rwl_entry(rwlp)->rd_count++; 66841efec22Sraf sigon(self); 6697c478bd9Sstevel@tonic-gate if (rwsp) 6707c478bd9Sstevel@tonic-gate tdb_incr(rwsp->rw_rdlock); 67141efec22Sraf DTRACE_PROBE2(plockstat, rw__acquire, rwlp, READ_LOCK); 67241efec22Sraf } else { 67341efec22Sraf DTRACE_PROBE3(plockstat, rw__error, rwlp, READ_LOCK, error); 6747c478bd9Sstevel@tonic-gate } 6757c478bd9Sstevel@tonic-gate 6767c478bd9Sstevel@tonic-gate return (error); 6777c478bd9Sstevel@tonic-gate } 6787c478bd9Sstevel@tonic-gate 6797257d1b4Sraf #pragma weak pthread_rwlock_rdlock = rw_rdlock 6807257d1b4Sraf #pragma weak _rw_rdlock = rw_rdlock 6817c478bd9Sstevel@tonic-gate int 6827257d1b4Sraf rw_rdlock(rwlock_t *rwlp) 6837c478bd9Sstevel@tonic-gate { 6847c478bd9Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 6857c478bd9Sstevel@tonic-gate return (rw_rdlock_impl(rwlp, NULL)); 6867c478bd9Sstevel@tonic-gate } 6877c478bd9Sstevel@tonic-gate 6887c478bd9Sstevel@tonic-gate void 6897c478bd9Sstevel@tonic-gate lrw_rdlock(rwlock_t *rwlp) 6907c478bd9Sstevel@tonic-gate { 6917c478bd9Sstevel@tonic-gate enter_critical(curthread); 6927c478bd9Sstevel@tonic-gate (void) rw_rdlock_impl(rwlp, NULL); 6937c478bd9Sstevel@tonic-gate } 6947c478bd9Sstevel@tonic-gate 6957c478bd9Sstevel@tonic-gate int 6967257d1b4Sraf pthread_rwlock_reltimedrdlock_np(pthread_rwlock_t *_RESTRICT_KYWD rwlp, 6977257d1b4Sraf const struct timespec *_RESTRICT_KYWD reltime) 6987c478bd9Sstevel@tonic-gate { 6997c478bd9Sstevel@tonic-gate timespec_t tslocal = *reltime; 7007c478bd9Sstevel@tonic-gate int error; 7017c478bd9Sstevel@tonic-gate 7027c478bd9Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 7037257d1b4Sraf 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 7107257d1b4Sraf pthread_rwlock_timedrdlock(pthread_rwlock_t *_RESTRICT_KYWD rwlp, 7117257d1b4Sraf const struct timespec *_RESTRICT_KYWD abstime) 7127c478bd9Sstevel@tonic-gate { 7137c478bd9Sstevel@tonic-gate timespec_t tslocal; 7147c478bd9Sstevel@tonic-gate int error; 7157c478bd9Sstevel@tonic-gate 7167c478bd9Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 7177c478bd9Sstevel@tonic-gate abstime_to_reltime(CLOCK_REALTIME, abstime, &tslocal); 7187257d1b4Sraf error = rw_rdlock_impl((rwlock_t *)rwlp, &tslocal); 7197c478bd9Sstevel@tonic-gate if (error == ETIME) 7207c478bd9Sstevel@tonic-gate error = ETIMEDOUT; 7217c478bd9Sstevel@tonic-gate return (error); 7227c478bd9Sstevel@tonic-gate } 7237c478bd9Sstevel@tonic-gate 7247c478bd9Sstevel@tonic-gate int 7257c478bd9Sstevel@tonic-gate rw_wrlock_impl(rwlock_t *rwlp, timespec_t *tsp) 7267c478bd9Sstevel@tonic-gate { 7277c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 7287c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 7297c478bd9Sstevel@tonic-gate tdb_rwlock_stats_t *rwsp = RWLOCK_STATS(rwlp, udp); 7307c478bd9Sstevel@tonic-gate int error; 7317c478bd9Sstevel@tonic-gate 7327c478bd9Sstevel@tonic-gate /* 7337c478bd9Sstevel@tonic-gate * If we hold a readers lock on this rwlock, bail out. 7347c478bd9Sstevel@tonic-gate */ 7357257d1b4Sraf if (rw_read_held(rwlp)) { 7367c478bd9Sstevel@tonic-gate if (self->ul_error_detection) 7377c478bd9Sstevel@tonic-gate rwlock_error(rwlp, "rwlock_wrlock", 7387c478bd9Sstevel@tonic-gate "calling thread owns the readers lock"); 73941efec22Sraf error = EDEADLK; 74041efec22Sraf goto out; 7417c478bd9Sstevel@tonic-gate } 7427c478bd9Sstevel@tonic-gate 7437c478bd9Sstevel@tonic-gate /* 7447c478bd9Sstevel@tonic-gate * If we hold the writer lock, bail out. 7457c478bd9Sstevel@tonic-gate */ 7467257d1b4Sraf if (rw_write_held(rwlp)) { 7477c478bd9Sstevel@tonic-gate if (self->ul_error_detection) 7487c478bd9Sstevel@tonic-gate rwlock_error(rwlp, "rwlock_wrlock", 7497c478bd9Sstevel@tonic-gate "calling thread owns the writer lock"); 75041efec22Sraf error = EDEADLK; 75141efec22Sraf goto out; 7527c478bd9Sstevel@tonic-gate } 7537c478bd9Sstevel@tonic-gate 75441efec22Sraf if (write_lock_try(rwlp, 0)) 75541efec22Sraf error = 0; 75641efec22Sraf else if (rwlp->rwlock_type == USYNC_PROCESS) /* kernel-level */ 7577c478bd9Sstevel@tonic-gate error = shared_rwlock_lock(rwlp, tsp, WRITE_LOCK); 75841efec22Sraf else /* user-level */ 7597c478bd9Sstevel@tonic-gate error = rwlock_lock(rwlp, tsp, WRITE_LOCK); 7607c478bd9Sstevel@tonic-gate 76141efec22Sraf out: 76241efec22Sraf if (error == 0) { 76341efec22Sraf rwlp->rwlock_owner = (uintptr_t)self; 76441efec22Sraf if (rwlp->rwlock_type == USYNC_PROCESS) 76541efec22Sraf rwlp->rwlock_ownerpid = udp->pid; 76641efec22Sraf if (rwsp) { 7677c478bd9Sstevel@tonic-gate tdb_incr(rwsp->rw_wrlock); 7687c478bd9Sstevel@tonic-gate rwsp->rw_wrlock_begin_hold = gethrtime(); 7697c478bd9Sstevel@tonic-gate } 77041efec22Sraf DTRACE_PROBE2(plockstat, rw__acquire, rwlp, WRITE_LOCK); 77141efec22Sraf } else { 77241efec22Sraf DTRACE_PROBE3(plockstat, rw__error, rwlp, WRITE_LOCK, error); 77341efec22Sraf } 7747c478bd9Sstevel@tonic-gate return (error); 7757c478bd9Sstevel@tonic-gate } 7767c478bd9Sstevel@tonic-gate 7777257d1b4Sraf #pragma weak pthread_rwlock_wrlock = rw_wrlock 7787257d1b4Sraf #pragma weak _rw_wrlock = rw_wrlock 7797c478bd9Sstevel@tonic-gate int 7807257d1b4Sraf rw_wrlock(rwlock_t *rwlp) 7817c478bd9Sstevel@tonic-gate { 7827c478bd9Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 7837c478bd9Sstevel@tonic-gate return (rw_wrlock_impl(rwlp, NULL)); 7847c478bd9Sstevel@tonic-gate } 7857c478bd9Sstevel@tonic-gate 7867c478bd9Sstevel@tonic-gate void 7877c478bd9Sstevel@tonic-gate lrw_wrlock(rwlock_t *rwlp) 7887c478bd9Sstevel@tonic-gate { 7897c478bd9Sstevel@tonic-gate enter_critical(curthread); 7907c478bd9Sstevel@tonic-gate (void) rw_wrlock_impl(rwlp, NULL); 7917c478bd9Sstevel@tonic-gate } 7927c478bd9Sstevel@tonic-gate 7937c478bd9Sstevel@tonic-gate int 7947257d1b4Sraf pthread_rwlock_reltimedwrlock_np(pthread_rwlock_t *_RESTRICT_KYWD rwlp, 7957257d1b4Sraf const struct timespec *_RESTRICT_KYWD reltime) 7967c478bd9Sstevel@tonic-gate { 7977c478bd9Sstevel@tonic-gate timespec_t tslocal = *reltime; 7987c478bd9Sstevel@tonic-gate int error; 7997c478bd9Sstevel@tonic-gate 8007c478bd9Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 8017257d1b4Sraf error = rw_wrlock_impl((rwlock_t *)rwlp, &tslocal); 8027c478bd9Sstevel@tonic-gate if (error == ETIME) 8037c478bd9Sstevel@tonic-gate error = ETIMEDOUT; 8047c478bd9Sstevel@tonic-gate return (error); 8057c478bd9Sstevel@tonic-gate } 8067c478bd9Sstevel@tonic-gate 8077c478bd9Sstevel@tonic-gate int 8087257d1b4Sraf pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlp, const timespec_t *abstime) 8097c478bd9Sstevel@tonic-gate { 8107c478bd9Sstevel@tonic-gate timespec_t tslocal; 8117c478bd9Sstevel@tonic-gate int error; 8127c478bd9Sstevel@tonic-gate 8137c478bd9Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 8147c478bd9Sstevel@tonic-gate abstime_to_reltime(CLOCK_REALTIME, abstime, &tslocal); 8157257d1b4Sraf error = rw_wrlock_impl((rwlock_t *)rwlp, &tslocal); 8167c478bd9Sstevel@tonic-gate if (error == ETIME) 8177c478bd9Sstevel@tonic-gate error = ETIMEDOUT; 8187c478bd9Sstevel@tonic-gate return (error); 8197c478bd9Sstevel@tonic-gate } 8207c478bd9Sstevel@tonic-gate 8217257d1b4Sraf #pragma weak pthread_rwlock_tryrdlock = rw_tryrdlock 8227c478bd9Sstevel@tonic-gate int 8237257d1b4Sraf rw_tryrdlock(rwlock_t *rwlp) 8247c478bd9Sstevel@tonic-gate { 8257c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 8267c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 8277c478bd9Sstevel@tonic-gate tdb_rwlock_stats_t *rwsp = RWLOCK_STATS(rwlp, udp); 8287c478bd9Sstevel@tonic-gate readlock_t *readlockp; 8297c478bd9Sstevel@tonic-gate int error; 8307c478bd9Sstevel@tonic-gate 8317c478bd9Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 8327c478bd9Sstevel@tonic-gate 8337c478bd9Sstevel@tonic-gate if (rwsp) 8347c478bd9Sstevel@tonic-gate tdb_incr(rwsp->rw_rdlock_try); 8357c478bd9Sstevel@tonic-gate 8367c478bd9Sstevel@tonic-gate /* 8377c478bd9Sstevel@tonic-gate * If we already hold a readers lock on this rwlock, 8387c478bd9Sstevel@tonic-gate * just increment our reference count and return. 8397c478bd9Sstevel@tonic-gate */ 84041efec22Sraf sigoff(self); 8417c478bd9Sstevel@tonic-gate readlockp = rwl_entry(rwlp); 8427c478bd9Sstevel@tonic-gate if (readlockp->rd_count != 0) { 84341efec22Sraf if (readlockp->rd_count == READ_LOCK_MAX) { 84441efec22Sraf sigon(self); 84541efec22Sraf error = EAGAIN; 84641efec22Sraf goto out; 8477c478bd9Sstevel@tonic-gate } 84841efec22Sraf sigon(self); 84941efec22Sraf error = 0; 85041efec22Sraf goto out; 85141efec22Sraf } 85241efec22Sraf sigon(self); 8537c478bd9Sstevel@tonic-gate 85441efec22Sraf if (read_lock_try(rwlp, 0)) 85541efec22Sraf error = 0; 85641efec22Sraf else if (rwlp->rwlock_type == USYNC_PROCESS) /* kernel-level */ 8577c478bd9Sstevel@tonic-gate error = shared_rwlock_lock(rwlp, NULL, READ_LOCK_TRY); 8587c478bd9Sstevel@tonic-gate else /* user-level */ 8597c478bd9Sstevel@tonic-gate error = rwlock_lock(rwlp, NULL, READ_LOCK_TRY); 8607c478bd9Sstevel@tonic-gate 86141efec22Sraf out: 86241efec22Sraf if (error == 0) { 86341efec22Sraf sigoff(self); 86441efec22Sraf rwl_entry(rwlp)->rd_count++; 86541efec22Sraf sigon(self); 86641efec22Sraf DTRACE_PROBE2(plockstat, rw__acquire, rwlp, READ_LOCK); 86741efec22Sraf } else { 86841efec22Sraf if (rwsp) 8697c478bd9Sstevel@tonic-gate tdb_incr(rwsp->rw_rdlock_try_fail); 87041efec22Sraf if (error != EBUSY) { 87141efec22Sraf DTRACE_PROBE3(plockstat, rw__error, rwlp, READ_LOCK, 87241efec22Sraf error); 87341efec22Sraf } 87441efec22Sraf } 8757c478bd9Sstevel@tonic-gate 8767c478bd9Sstevel@tonic-gate return (error); 8777c478bd9Sstevel@tonic-gate } 8787c478bd9Sstevel@tonic-gate 8797257d1b4Sraf #pragma weak pthread_rwlock_trywrlock = rw_trywrlock 8807c478bd9Sstevel@tonic-gate int 8817257d1b4Sraf rw_trywrlock(rwlock_t *rwlp) 8827c478bd9Sstevel@tonic-gate { 8837c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 8847c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 8857c478bd9Sstevel@tonic-gate tdb_rwlock_stats_t *rwsp = RWLOCK_STATS(rwlp, udp); 8867c478bd9Sstevel@tonic-gate int error; 8877c478bd9Sstevel@tonic-gate 88841efec22Sraf ASSERT(!self->ul_critical || self->ul_bindflags); 8897c478bd9Sstevel@tonic-gate 8907c478bd9Sstevel@tonic-gate if (rwsp) 8917c478bd9Sstevel@tonic-gate tdb_incr(rwsp->rw_wrlock_try); 8927c478bd9Sstevel@tonic-gate 89341efec22Sraf if (write_lock_try(rwlp, 0)) 89441efec22Sraf error = 0; 89541efec22Sraf else if (rwlp->rwlock_type == USYNC_PROCESS) /* kernel-level */ 8967c478bd9Sstevel@tonic-gate error = shared_rwlock_lock(rwlp, NULL, WRITE_LOCK_TRY); 89741efec22Sraf else /* user-level */ 8987c478bd9Sstevel@tonic-gate error = rwlock_lock(rwlp, NULL, WRITE_LOCK_TRY); 89941efec22Sraf 90041efec22Sraf if (error == 0) { 90141efec22Sraf rwlp->rwlock_owner = (uintptr_t)self; 90241efec22Sraf if (rwlp->rwlock_type == USYNC_PROCESS) 90341efec22Sraf rwlp->rwlock_ownerpid = udp->pid; 90441efec22Sraf if (rwsp) 9057c478bd9Sstevel@tonic-gate rwsp->rw_wrlock_begin_hold = gethrtime(); 90641efec22Sraf DTRACE_PROBE2(plockstat, rw__acquire, rwlp, WRITE_LOCK); 90741efec22Sraf } else { 90841efec22Sraf if (rwsp) 90941efec22Sraf tdb_incr(rwsp->rw_wrlock_try_fail); 91041efec22Sraf if (error != EBUSY) { 91141efec22Sraf DTRACE_PROBE3(plockstat, rw__error, rwlp, WRITE_LOCK, 91241efec22Sraf error); 91341efec22Sraf } 9147c478bd9Sstevel@tonic-gate } 9157c478bd9Sstevel@tonic-gate return (error); 9167c478bd9Sstevel@tonic-gate } 9177c478bd9Sstevel@tonic-gate 9187257d1b4Sraf #pragma weak pthread_rwlock_unlock = rw_unlock 9197257d1b4Sraf #pragma weak _rw_unlock = rw_unlock 9207c478bd9Sstevel@tonic-gate int 9217257d1b4Sraf rw_unlock(rwlock_t *rwlp) 9227c478bd9Sstevel@tonic-gate { 92341efec22Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 92441efec22Sraf uint32_t readers; 9257c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 9267c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 9277c478bd9Sstevel@tonic-gate tdb_rwlock_stats_t *rwsp; 92841efec22Sraf queue_head_t *qp; 92941efec22Sraf int rd_wr; 93041efec22Sraf int waked = 0; 9317c478bd9Sstevel@tonic-gate 93241efec22Sraf readers = *rwstate; 93341efec22Sraf ASSERT_CONSISTENT_STATE(readers); 93441efec22Sraf if (readers & URW_WRITE_LOCKED) { 93541efec22Sraf rd_wr = WRITE_LOCK; 93641efec22Sraf readers = 0; 93741efec22Sraf } else { 93841efec22Sraf rd_wr = READ_LOCK; 93941efec22Sraf readers &= URW_READERS_MASK; 9407c478bd9Sstevel@tonic-gate } 9417c478bd9Sstevel@tonic-gate 94241efec22Sraf if (rd_wr == WRITE_LOCK) { 9437c478bd9Sstevel@tonic-gate /* 9447c478bd9Sstevel@tonic-gate * Since the writer lock is held, we'd better be 9457c478bd9Sstevel@tonic-gate * holding it, else we cannot legitimately be here. 9467c478bd9Sstevel@tonic-gate */ 9477257d1b4Sraf if (!rw_write_held(rwlp)) { 9487c478bd9Sstevel@tonic-gate if (self->ul_error_detection) 9497c478bd9Sstevel@tonic-gate rwlock_error(rwlp, "rwlock_unlock", 9507c478bd9Sstevel@tonic-gate "writer lock held, " 9517c478bd9Sstevel@tonic-gate "but not by the calling thread"); 9527c478bd9Sstevel@tonic-gate return (EPERM); 9537c478bd9Sstevel@tonic-gate } 9547c478bd9Sstevel@tonic-gate if ((rwsp = RWLOCK_STATS(rwlp, udp)) != NULL) { 9557c478bd9Sstevel@tonic-gate if (rwsp->rw_wrlock_begin_hold) 9567c478bd9Sstevel@tonic-gate rwsp->rw_wrlock_hold_time += 9577c478bd9Sstevel@tonic-gate gethrtime() - rwsp->rw_wrlock_begin_hold; 9587c478bd9Sstevel@tonic-gate rwsp->rw_wrlock_begin_hold = 0; 9597c478bd9Sstevel@tonic-gate } 96041efec22Sraf rwlp->rwlock_owner = 0; 96141efec22Sraf rwlp->rwlock_ownerpid = 0; 96241efec22Sraf } else if (readers > 0) { 9637c478bd9Sstevel@tonic-gate /* 9647c478bd9Sstevel@tonic-gate * A readers lock is held; if we don't hold one, bail out. 9657c478bd9Sstevel@tonic-gate */ 96641efec22Sraf readlock_t *readlockp; 96741efec22Sraf 96841efec22Sraf sigoff(self); 96941efec22Sraf readlockp = rwl_entry(rwlp); 9707c478bd9Sstevel@tonic-gate if (readlockp->rd_count == 0) { 97141efec22Sraf sigon(self); 9727c478bd9Sstevel@tonic-gate if (self->ul_error_detection) 9737c478bd9Sstevel@tonic-gate rwlock_error(rwlp, "rwlock_unlock", 9747c478bd9Sstevel@tonic-gate "readers lock held, " 9757c478bd9Sstevel@tonic-gate "but not by the calling thread"); 9767c478bd9Sstevel@tonic-gate return (EPERM); 9777c478bd9Sstevel@tonic-gate } 9787c478bd9Sstevel@tonic-gate /* 9797c478bd9Sstevel@tonic-gate * If we hold more than one readers lock on this rwlock, 9807c478bd9Sstevel@tonic-gate * just decrement our reference count and return. 9817c478bd9Sstevel@tonic-gate */ 9827c478bd9Sstevel@tonic-gate if (--readlockp->rd_count != 0) { 98341efec22Sraf sigon(self); 98441efec22Sraf goto out; 9857c478bd9Sstevel@tonic-gate } 98641efec22Sraf sigon(self); 9877c478bd9Sstevel@tonic-gate } else { 9887c478bd9Sstevel@tonic-gate /* 9897c478bd9Sstevel@tonic-gate * This is a usage error. 9907c478bd9Sstevel@tonic-gate * No thread should release an unowned lock. 9917c478bd9Sstevel@tonic-gate */ 9927c478bd9Sstevel@tonic-gate if (self->ul_error_detection) 9937c478bd9Sstevel@tonic-gate rwlock_error(rwlp, "rwlock_unlock", "lock not owned"); 9947c478bd9Sstevel@tonic-gate return (EPERM); 9957c478bd9Sstevel@tonic-gate } 9967c478bd9Sstevel@tonic-gate 99741efec22Sraf if (rd_wr == WRITE_LOCK && write_unlock_try(rwlp)) { 99841efec22Sraf /* EMPTY */; 99941efec22Sraf } else if (rd_wr == READ_LOCK && read_unlock_try(rwlp)) { 100041efec22Sraf /* EMPTY */; 100141efec22Sraf } else if (rwlp->rwlock_type == USYNC_PROCESS) { 10028cd45542Sraf (void) mutex_lock(&rwlp->mutex); 100341efec22Sraf (void) __lwp_rwlock_unlock(rwlp); 10048cd45542Sraf (void) mutex_unlock(&rwlp->mutex); 100541efec22Sraf waked = 1; 10067c478bd9Sstevel@tonic-gate } else { 10077c478bd9Sstevel@tonic-gate qp = queue_lock(rwlp, MX); 100841efec22Sraf if (rd_wr == READ_LOCK) 100941efec22Sraf atomic_dec_32(rwstate); 101041efec22Sraf else 101141efec22Sraf atomic_and_32(rwstate, ~URW_WRITE_LOCKED); 10127c478bd9Sstevel@tonic-gate waked = rw_queue_release(qp, rwlp); 10137c478bd9Sstevel@tonic-gate } 10147c478bd9Sstevel@tonic-gate 101541efec22Sraf out: 101641efec22Sraf DTRACE_PROBE2(plockstat, rw__release, rwlp, rd_wr); 101741efec22Sraf 10187c478bd9Sstevel@tonic-gate /* 10197c478bd9Sstevel@tonic-gate * Yield to the thread we just waked up, just in case we might 10207c478bd9Sstevel@tonic-gate * be about to grab the rwlock again immediately upon return. 10217c478bd9Sstevel@tonic-gate * This is pretty weak but it helps on a uniprocessor and also 10227c478bd9Sstevel@tonic-gate * when cpu affinity has assigned both ourself and the other 10237c478bd9Sstevel@tonic-gate * thread to the same CPU. Note that lwp_yield() will yield 10247c478bd9Sstevel@tonic-gate * the processor only if the writer is at the same or higher 10257c478bd9Sstevel@tonic-gate * priority than ourself. This provides more balanced program 10267c478bd9Sstevel@tonic-gate * behavior; it doesn't guarantee acquisition of the lock by 10277c478bd9Sstevel@tonic-gate * the pending writer. 10287c478bd9Sstevel@tonic-gate */ 10297c478bd9Sstevel@tonic-gate if (waked) 10308cd45542Sraf yield(); 10317c478bd9Sstevel@tonic-gate return (0); 10327c478bd9Sstevel@tonic-gate } 10337c478bd9Sstevel@tonic-gate 10347c478bd9Sstevel@tonic-gate void 10357c478bd9Sstevel@tonic-gate lrw_unlock(rwlock_t *rwlp) 10367c478bd9Sstevel@tonic-gate { 10377257d1b4Sraf (void) rw_unlock(rwlp); 10387c478bd9Sstevel@tonic-gate exit_critical(curthread); 10397c478bd9Sstevel@tonic-gate } 1040