10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 54570Sraf * Common Development and Distribution License (the "License"). 64570Sraf * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 214570Sraf 220Sstevel@tonic-gate /* 236247Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #include "lint.h" 280Sstevel@tonic-gate #include "thr_uberdata.h" 290Sstevel@tonic-gate #include <sys/sdt.h> 300Sstevel@tonic-gate 310Sstevel@tonic-gate #define TRY_FLAG 0x10 320Sstevel@tonic-gate #define READ_LOCK 0 330Sstevel@tonic-gate #define WRITE_LOCK 1 340Sstevel@tonic-gate #define READ_LOCK_TRY (READ_LOCK | TRY_FLAG) 350Sstevel@tonic-gate #define WRITE_LOCK_TRY (WRITE_LOCK | TRY_FLAG) 360Sstevel@tonic-gate 370Sstevel@tonic-gate #define NLOCKS 4 /* initial number of readlock_t structs allocated */ 380Sstevel@tonic-gate 394570Sraf #define ASSERT_CONSISTENT_STATE(readers) \ 404570Sraf ASSERT(!((readers) & URW_WRITE_LOCKED) || \ 414570Sraf ((readers) & ~URW_HAS_WAITERS) == URW_WRITE_LOCKED) 424570Sraf 430Sstevel@tonic-gate /* 440Sstevel@tonic-gate * Find/allocate an entry for rwlp in our array of rwlocks held for reading. 454570Sraf * We must be deferring signals for this to be safe. 464574Sraf * Else if we are returning an entry with ul_rdlockcnt == 0, 474570Sraf * it could be reassigned behind our back in a signal handler. 480Sstevel@tonic-gate */ 490Sstevel@tonic-gate static readlock_t * 500Sstevel@tonic-gate rwl_entry(rwlock_t *rwlp) 510Sstevel@tonic-gate { 520Sstevel@tonic-gate ulwp_t *self = curthread; 530Sstevel@tonic-gate readlock_t *remembered = NULL; 540Sstevel@tonic-gate readlock_t *readlockp; 550Sstevel@tonic-gate uint_t nlocks; 560Sstevel@tonic-gate 574570Sraf /* we must be deferring signals */ 584570Sraf ASSERT((self->ul_critical + self->ul_sigdefer) != 0); 594570Sraf 604574Sraf if ((nlocks = self->ul_rdlockcnt) != 0) 610Sstevel@tonic-gate readlockp = self->ul_readlock.array; 620Sstevel@tonic-gate else { 630Sstevel@tonic-gate nlocks = 1; 640Sstevel@tonic-gate readlockp = &self->ul_readlock.single; 650Sstevel@tonic-gate } 660Sstevel@tonic-gate 670Sstevel@tonic-gate for (; nlocks; nlocks--, readlockp++) { 680Sstevel@tonic-gate if (readlockp->rd_rwlock == rwlp) 690Sstevel@tonic-gate return (readlockp); 700Sstevel@tonic-gate if (readlockp->rd_count == 0 && remembered == NULL) 710Sstevel@tonic-gate remembered = readlockp; 720Sstevel@tonic-gate } 730Sstevel@tonic-gate if (remembered != NULL) { 740Sstevel@tonic-gate remembered->rd_rwlock = rwlp; 750Sstevel@tonic-gate return (remembered); 760Sstevel@tonic-gate } 770Sstevel@tonic-gate 780Sstevel@tonic-gate /* 790Sstevel@tonic-gate * No entry available. Allocate more space, converting the single 800Sstevel@tonic-gate * readlock_t entry into an array of readlock_t entries if necessary. 810Sstevel@tonic-gate */ 824574Sraf if ((nlocks = self->ul_rdlockcnt) == 0) { 830Sstevel@tonic-gate /* 840Sstevel@tonic-gate * Initial allocation of the readlock_t array. 850Sstevel@tonic-gate * Convert the single entry into an array. 860Sstevel@tonic-gate */ 874574Sraf self->ul_rdlockcnt = nlocks = NLOCKS; 880Sstevel@tonic-gate readlockp = lmalloc(nlocks * sizeof (readlock_t)); 890Sstevel@tonic-gate /* 900Sstevel@tonic-gate * The single readlock_t becomes the first entry in the array. 910Sstevel@tonic-gate */ 920Sstevel@tonic-gate *readlockp = self->ul_readlock.single; 930Sstevel@tonic-gate self->ul_readlock.single.rd_count = 0; 940Sstevel@tonic-gate self->ul_readlock.array = readlockp; 950Sstevel@tonic-gate /* 960Sstevel@tonic-gate * Return the next available entry in the array. 970Sstevel@tonic-gate */ 980Sstevel@tonic-gate (++readlockp)->rd_rwlock = rwlp; 990Sstevel@tonic-gate return (readlockp); 1000Sstevel@tonic-gate } 1010Sstevel@tonic-gate /* 1020Sstevel@tonic-gate * Reallocate the array, double the size each time. 1030Sstevel@tonic-gate */ 1040Sstevel@tonic-gate readlockp = lmalloc(nlocks * 2 * sizeof (readlock_t)); 1056515Sraf (void) memcpy(readlockp, self->ul_readlock.array, 1066247Sraf nlocks * sizeof (readlock_t)); 1070Sstevel@tonic-gate lfree(self->ul_readlock.array, nlocks * sizeof (readlock_t)); 1080Sstevel@tonic-gate self->ul_readlock.array = readlockp; 1094574Sraf self->ul_rdlockcnt *= 2; 1100Sstevel@tonic-gate /* 1110Sstevel@tonic-gate * Return the next available entry in the newly allocated array. 1120Sstevel@tonic-gate */ 1130Sstevel@tonic-gate (readlockp += nlocks)->rd_rwlock = rwlp; 1140Sstevel@tonic-gate return (readlockp); 1150Sstevel@tonic-gate } 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate /* 1180Sstevel@tonic-gate * Free the array of rwlocks held for reading. 1190Sstevel@tonic-gate */ 1200Sstevel@tonic-gate void 1210Sstevel@tonic-gate rwl_free(ulwp_t *ulwp) 1220Sstevel@tonic-gate { 1230Sstevel@tonic-gate uint_t nlocks; 1240Sstevel@tonic-gate 1254574Sraf if ((nlocks = ulwp->ul_rdlockcnt) != 0) 1260Sstevel@tonic-gate lfree(ulwp->ul_readlock.array, nlocks * sizeof (readlock_t)); 1274574Sraf ulwp->ul_rdlockcnt = 0; 1280Sstevel@tonic-gate ulwp->ul_readlock.single.rd_rwlock = NULL; 1290Sstevel@tonic-gate ulwp->ul_readlock.single.rd_count = 0; 1300Sstevel@tonic-gate } 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate /* 1330Sstevel@tonic-gate * Check if a reader version of the lock is held by the current thread. 1340Sstevel@tonic-gate */ 1356812Sraf #pragma weak _rw_read_held = rw_read_held 1360Sstevel@tonic-gate int 1376812Sraf rw_read_held(rwlock_t *rwlp) 1380Sstevel@tonic-gate { 1394570Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 1404570Sraf uint32_t readers; 1414570Sraf ulwp_t *self = curthread; 1420Sstevel@tonic-gate readlock_t *readlockp; 1430Sstevel@tonic-gate uint_t nlocks; 1444570Sraf int rval = 0; 1450Sstevel@tonic-gate 1464570Sraf no_preempt(self); 1474570Sraf 1484570Sraf readers = *rwstate; 1494570Sraf ASSERT_CONSISTENT_STATE(readers); 1504570Sraf if (!(readers & URW_WRITE_LOCKED) && 1514570Sraf (readers & URW_READERS_MASK) != 0) { 1524570Sraf /* 1534570Sraf * The lock is held for reading by some thread. 1544570Sraf * Search our array of rwlocks held for reading for a match. 1554570Sraf */ 1564574Sraf if ((nlocks = self->ul_rdlockcnt) != 0) 1574570Sraf readlockp = self->ul_readlock.array; 1584570Sraf else { 1594570Sraf nlocks = 1; 1604570Sraf readlockp = &self->ul_readlock.single; 1614570Sraf } 1624570Sraf for (; nlocks; nlocks--, readlockp++) { 1634570Sraf if (readlockp->rd_rwlock == rwlp) { 1644570Sraf if (readlockp->rd_count) 1654570Sraf rval = 1; 1664570Sraf break; 1674570Sraf } 1684570Sraf } 1690Sstevel@tonic-gate } 1700Sstevel@tonic-gate 1714570Sraf preempt(self); 1724570Sraf return (rval); 1730Sstevel@tonic-gate } 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate /* 1760Sstevel@tonic-gate * Check if a writer version of the lock is held by the current thread. 1770Sstevel@tonic-gate */ 1786812Sraf #pragma weak _rw_write_held = rw_write_held 1790Sstevel@tonic-gate int 1806812Sraf rw_write_held(rwlock_t *rwlp) 1810Sstevel@tonic-gate { 1824570Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 1834570Sraf uint32_t readers; 1840Sstevel@tonic-gate ulwp_t *self = curthread; 1854570Sraf int rval; 1864570Sraf 1874570Sraf no_preempt(self); 1880Sstevel@tonic-gate 1894570Sraf readers = *rwstate; 1904570Sraf ASSERT_CONSISTENT_STATE(readers); 1914570Sraf rval = ((readers & URW_WRITE_LOCKED) && 1924570Sraf rwlp->rwlock_owner == (uintptr_t)self && 1934570Sraf (rwlp->rwlock_type == USYNC_THREAD || 1944570Sraf rwlp->rwlock_ownerpid == self->ul_uberdata->pid)); 1950Sstevel@tonic-gate 1964570Sraf preempt(self); 1974570Sraf return (rval); 1980Sstevel@tonic-gate } 1990Sstevel@tonic-gate 2006812Sraf #pragma weak _rwlock_init = rwlock_init 2010Sstevel@tonic-gate /* ARGSUSED2 */ 2020Sstevel@tonic-gate int 2036812Sraf rwlock_init(rwlock_t *rwlp, int type, void *arg) 2040Sstevel@tonic-gate { 2057255Sraf ulwp_t *self = curthread; 2067255Sraf 2070Sstevel@tonic-gate if (type != USYNC_THREAD && type != USYNC_PROCESS) 2080Sstevel@tonic-gate return (EINVAL); 2090Sstevel@tonic-gate /* 2100Sstevel@tonic-gate * Once reinitialized, we can no longer be holding a read or write lock. 2110Sstevel@tonic-gate * We can do nothing about other threads that are holding read locks. 2120Sstevel@tonic-gate */ 2137255Sraf sigoff(self); 2144570Sraf rwl_entry(rwlp)->rd_count = 0; 2157255Sraf sigon(self); 2166515Sraf (void) memset(rwlp, 0, sizeof (*rwlp)); 2170Sstevel@tonic-gate rwlp->rwlock_type = (uint16_t)type; 2180Sstevel@tonic-gate rwlp->rwlock_magic = RWL_MAGIC; 2190Sstevel@tonic-gate rwlp->mutex.mutex_type = (uint8_t)type; 2200Sstevel@tonic-gate rwlp->mutex.mutex_flag = LOCK_INITED; 2210Sstevel@tonic-gate rwlp->mutex.mutex_magic = MUTEX_MAGIC; 2227255Sraf 2237255Sraf /* 2247255Sraf * This should be at the beginning of the function, 2257255Sraf * but for the sake of old broken applications that 2267255Sraf * do not have proper alignment for their rwlocks 2277255Sraf * (and don't check the return code from rwlock_init), 2287255Sraf * we put it here, after initializing the rwlock regardless. 2297255Sraf */ 2307255Sraf if (((uintptr_t)rwlp & (_LONG_LONG_ALIGNMENT - 1)) && 2317255Sraf self->ul_misaligned == 0) 2327255Sraf return (EINVAL); 2337255Sraf 2340Sstevel@tonic-gate return (0); 2350Sstevel@tonic-gate } 2360Sstevel@tonic-gate 2376812Sraf #pragma weak pthread_rwlock_destroy = rwlock_destroy 2386812Sraf #pragma weak _rwlock_destroy = rwlock_destroy 2390Sstevel@tonic-gate int 2406812Sraf rwlock_destroy(rwlock_t *rwlp) 2410Sstevel@tonic-gate { 2420Sstevel@tonic-gate /* 2430Sstevel@tonic-gate * Once destroyed, we can no longer be holding a read or write lock. 2440Sstevel@tonic-gate * We can do nothing about other threads that are holding read locks. 2450Sstevel@tonic-gate */ 2464570Sraf sigoff(curthread); 2474570Sraf rwl_entry(rwlp)->rd_count = 0; 2484570Sraf sigon(curthread); 2490Sstevel@tonic-gate rwlp->rwlock_magic = 0; 2500Sstevel@tonic-gate tdb_sync_obj_deregister(rwlp); 2510Sstevel@tonic-gate return (0); 2520Sstevel@tonic-gate } 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate /* 2554570Sraf * Attempt to acquire a readers lock. Return true on success. 2564570Sraf */ 2574570Sraf static int 2584570Sraf read_lock_try(rwlock_t *rwlp, int ignore_waiters_flag) 2594570Sraf { 2604570Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 2614570Sraf uint32_t mask = ignore_waiters_flag? 2626247Sraf URW_WRITE_LOCKED : (URW_HAS_WAITERS | URW_WRITE_LOCKED); 2634570Sraf uint32_t readers; 2644570Sraf ulwp_t *self = curthread; 2654570Sraf 2664570Sraf no_preempt(self); 2674570Sraf while (((readers = *rwstate) & mask) == 0) { 2684570Sraf if (atomic_cas_32(rwstate, readers, readers + 1) == readers) { 2694570Sraf preempt(self); 2704570Sraf return (1); 2714570Sraf } 2724570Sraf } 2734570Sraf preempt(self); 2744570Sraf return (0); 2754570Sraf } 2764570Sraf 2774570Sraf /* 2784570Sraf * Attempt to release a reader lock. Return true on success. 2794570Sraf */ 2804570Sraf static int 2814570Sraf read_unlock_try(rwlock_t *rwlp) 2824570Sraf { 2834570Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 2844570Sraf uint32_t readers; 2854570Sraf ulwp_t *self = curthread; 2864570Sraf 2874570Sraf no_preempt(self); 2884570Sraf while (((readers = *rwstate) & URW_HAS_WAITERS) == 0) { 2894570Sraf if (atomic_cas_32(rwstate, readers, readers - 1) == readers) { 2904570Sraf preempt(self); 2914570Sraf return (1); 2924570Sraf } 2934570Sraf } 2944570Sraf preempt(self); 2954570Sraf return (0); 2964570Sraf } 2974570Sraf 2984570Sraf /* 2994570Sraf * Attempt to acquire a writer lock. Return true on success. 3004570Sraf */ 3014570Sraf static int 3024570Sraf write_lock_try(rwlock_t *rwlp, int ignore_waiters_flag) 3034570Sraf { 3044570Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 3054570Sraf uint32_t mask = ignore_waiters_flag? 3066247Sraf (URW_WRITE_LOCKED | URW_READERS_MASK) : 3076247Sraf (URW_HAS_WAITERS | URW_WRITE_LOCKED | URW_READERS_MASK); 3084570Sraf ulwp_t *self = curthread; 3094570Sraf uint32_t readers; 3104570Sraf 3114570Sraf no_preempt(self); 3124570Sraf while (((readers = *rwstate) & mask) == 0) { 3134570Sraf if (atomic_cas_32(rwstate, readers, readers | URW_WRITE_LOCKED) 3144570Sraf == readers) { 3154570Sraf preempt(self); 3164570Sraf return (1); 3174570Sraf } 3184570Sraf } 3194570Sraf preempt(self); 3204570Sraf return (0); 3214570Sraf } 3224570Sraf 3234570Sraf /* 3244570Sraf * Attempt to release a writer lock. Return true on success. 3254570Sraf */ 3264570Sraf static int 3274570Sraf write_unlock_try(rwlock_t *rwlp) 3284570Sraf { 3294570Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 3304570Sraf uint32_t readers; 3314570Sraf ulwp_t *self = curthread; 3324570Sraf 3334570Sraf no_preempt(self); 3344570Sraf while (((readers = *rwstate) & URW_HAS_WAITERS) == 0) { 3354570Sraf if (atomic_cas_32(rwstate, readers, 0) == readers) { 3364570Sraf preempt(self); 3374570Sraf return (1); 3384570Sraf } 3394570Sraf } 3404570Sraf preempt(self); 3414570Sraf return (0); 3424570Sraf } 3434570Sraf 3444570Sraf /* 3454570Sraf * Wake up thread(s) sleeping on the rwlock queue and then 3460Sstevel@tonic-gate * drop the queue lock. Return non-zero if we wake up someone. 3474570Sraf * This is called when a thread releases a lock that appears to have waiters. 3480Sstevel@tonic-gate */ 3490Sstevel@tonic-gate static int 3500Sstevel@tonic-gate rw_queue_release(queue_head_t *qp, rwlock_t *rwlp) 3510Sstevel@tonic-gate { 3524570Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 3534570Sraf uint32_t readers; 3544570Sraf uint32_t writers; 3554570Sraf ulwp_t **ulwpp; 3560Sstevel@tonic-gate ulwp_t *ulwp; 3576247Sraf ulwp_t *prev; 3586247Sraf int nlwpid = 0; 3596247Sraf int more; 3606247Sraf int maxlwps = MAXLWPS; 3614570Sraf lwpid_t buffer[MAXLWPS]; 3624570Sraf lwpid_t *lwpid = buffer; 3634570Sraf 3644570Sraf readers = *rwstate; 3654570Sraf ASSERT_CONSISTENT_STATE(readers); 3664570Sraf if (!(readers & URW_HAS_WAITERS)) { 3674570Sraf queue_unlock(qp); 3684570Sraf return (0); 3694570Sraf } 3704570Sraf readers &= URW_READERS_MASK; 3714570Sraf writers = 0; 3720Sstevel@tonic-gate 3734570Sraf /* 3746247Sraf * Examine the queue of waiters in priority order and prepare 3756247Sraf * to wake up as many readers as we encounter before encountering 3766247Sraf * a writer. If the highest priority thread on the queue is a 3774570Sraf * writer, stop there and wake it up. 3784570Sraf * 3794570Sraf * We keep track of lwpids that are to be unparked in lwpid[]. 3804570Sraf * __lwp_unpark_all() is called to unpark all of them after 3814570Sraf * they have been removed from the sleep queue and the sleep 3824570Sraf * queue lock has been dropped. If we run out of space in our 3834570Sraf * on-stack buffer, we need to allocate more but we can't call 3844570Sraf * lmalloc() because we are holding a queue lock when the overflow 3854570Sraf * occurs and lmalloc() acquires a lock. We can't use alloca() 3864570Sraf * either because the application may have allocated a small 3874570Sraf * stack and we don't want to overrun the stack. So we call 3884570Sraf * alloc_lwpids() to allocate a bigger buffer using the mmap() 3894570Sraf * system call directly since that path acquires no locks. 3904570Sraf */ 3916247Sraf while ((ulwpp = queue_slot(qp, &prev, &more)) != NULL) { 3926247Sraf ulwp = *ulwpp; 3936247Sraf ASSERT(ulwp->ul_wchan == rwlp); 3944570Sraf if (ulwp->ul_writer) { 3954570Sraf if (writers != 0 || readers != 0) 3964570Sraf break; 3974570Sraf /* one writer to wake */ 3984570Sraf writers++; 3994570Sraf } else { 4004570Sraf if (writers != 0) 4014570Sraf break; 4024570Sraf /* at least one reader to wake */ 4034570Sraf readers++; 4044570Sraf if (nlwpid == maxlwps) 4054570Sraf lwpid = alloc_lwpids(lwpid, &nlwpid, &maxlwps); 4064570Sraf } 4076247Sraf queue_unlink(qp, ulwpp, prev); 4086247Sraf ulwp->ul_sleepq = NULL; 4096247Sraf ulwp->ul_wchan = NULL; 4104570Sraf lwpid[nlwpid++] = ulwp->ul_lwpid; 4110Sstevel@tonic-gate } 4126247Sraf if (ulwpp == NULL) 4134570Sraf atomic_and_32(rwstate, ~URW_HAS_WAITERS); 4144570Sraf if (nlwpid == 0) { 4154570Sraf queue_unlock(qp); 4164570Sraf } else { 4176247Sraf ulwp_t *self = curthread; 4184570Sraf no_preempt(self); 4194570Sraf queue_unlock(qp); 4204570Sraf if (nlwpid == 1) 4214570Sraf (void) __lwp_unpark(lwpid[0]); 4224570Sraf else 4234570Sraf (void) __lwp_unpark_all(lwpid, nlwpid); 4244570Sraf preempt(self); 4254570Sraf } 4264570Sraf if (lwpid != buffer) 4276515Sraf (void) munmap((caddr_t)lwpid, maxlwps * sizeof (lwpid_t)); 4284570Sraf return (nlwpid != 0); 4290Sstevel@tonic-gate } 4300Sstevel@tonic-gate 4310Sstevel@tonic-gate /* 4320Sstevel@tonic-gate * Common code for rdlock, timedrdlock, wrlock, timedwrlock, tryrdlock, 4330Sstevel@tonic-gate * and trywrlock for process-shared (USYNC_PROCESS) rwlocks. 4340Sstevel@tonic-gate * 4350Sstevel@tonic-gate * Note: if the lock appears to be contended we call __lwp_rwlock_rdlock() 4360Sstevel@tonic-gate * or __lwp_rwlock_wrlock() holding the mutex. These return with the mutex 4370Sstevel@tonic-gate * released, and if they need to sleep will release the mutex first. In the 4380Sstevel@tonic-gate * event of a spurious wakeup, these will return EAGAIN (because it is much 4390Sstevel@tonic-gate * easier for us to re-acquire the mutex here). 4400Sstevel@tonic-gate */ 4410Sstevel@tonic-gate int 4420Sstevel@tonic-gate shared_rwlock_lock(rwlock_t *rwlp, timespec_t *tsp, int rd_wr) 4430Sstevel@tonic-gate { 4444570Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 4454570Sraf mutex_t *mp = &rwlp->mutex; 4464570Sraf uint32_t readers; 4474570Sraf int try_flag; 4484570Sraf int error; 4494570Sraf 4504570Sraf try_flag = (rd_wr & TRY_FLAG); 4514570Sraf rd_wr &= ~TRY_FLAG; 4524570Sraf ASSERT(rd_wr == READ_LOCK || rd_wr == WRITE_LOCK); 4534570Sraf 4544570Sraf if (!try_flag) { 4554570Sraf DTRACE_PROBE2(plockstat, rw__block, rwlp, rd_wr); 4564570Sraf } 4574570Sraf 4584570Sraf do { 4594570Sraf if (try_flag && (*rwstate & URW_WRITE_LOCKED)) { 4604570Sraf error = EBUSY; 4614570Sraf break; 4624570Sraf } 4636515Sraf if ((error = mutex_lock(mp)) != 0) 4644570Sraf break; 4654570Sraf if (rd_wr == READ_LOCK) { 4664570Sraf if (read_lock_try(rwlp, 0)) { 4676515Sraf (void) mutex_unlock(mp); 4684570Sraf break; 4694570Sraf } 4704570Sraf } else { 4714570Sraf if (write_lock_try(rwlp, 0)) { 4726515Sraf (void) mutex_unlock(mp); 4734570Sraf break; 4744570Sraf } 4754570Sraf } 4764570Sraf atomic_or_32(rwstate, URW_HAS_WAITERS); 4774570Sraf readers = *rwstate; 4784570Sraf ASSERT_CONSISTENT_STATE(readers); 4794570Sraf /* 4804570Sraf * The calls to __lwp_rwlock_*() below will release the mutex, 481*7907SRoger.Faulkner@Sun.COM * so we need a dtrace probe here. The owner field of the 482*7907SRoger.Faulkner@Sun.COM * mutex is cleared in the kernel when the mutex is released, 483*7907SRoger.Faulkner@Sun.COM * so we should not clear it here. 4844570Sraf */ 4854570Sraf DTRACE_PROBE2(plockstat, mutex__release, mp, 0); 4864570Sraf /* 4874570Sraf * The waiters bit may be inaccurate. 4884570Sraf * Only the kernel knows for sure. 4894570Sraf */ 4904570Sraf if (rd_wr == READ_LOCK) { 4914570Sraf if (try_flag) 4924570Sraf error = __lwp_rwlock_tryrdlock(rwlp); 4934570Sraf else 4944570Sraf error = __lwp_rwlock_rdlock(rwlp, tsp); 4954570Sraf } else { 4964570Sraf if (try_flag) 4974570Sraf error = __lwp_rwlock_trywrlock(rwlp); 4984570Sraf else 4994570Sraf error = __lwp_rwlock_wrlock(rwlp, tsp); 5004570Sraf } 5014570Sraf } while (error == EAGAIN || error == EINTR); 5024570Sraf 5034570Sraf if (!try_flag) { 5044570Sraf DTRACE_PROBE3(plockstat, rw__blocked, rwlp, rd_wr, error == 0); 5054570Sraf } 5064570Sraf 5074570Sraf return (error); 5084570Sraf } 5094570Sraf 5104570Sraf /* 5114570Sraf * Common code for rdlock, timedrdlock, wrlock, timedwrlock, tryrdlock, 5124570Sraf * and trywrlock for process-private (USYNC_THREAD) rwlocks. 5134570Sraf */ 5144570Sraf int 5154570Sraf rwlock_lock(rwlock_t *rwlp, timespec_t *tsp, int rd_wr) 5164570Sraf { 5174570Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 5184570Sraf uint32_t readers; 5190Sstevel@tonic-gate ulwp_t *self = curthread; 5204570Sraf queue_head_t *qp; 5214570Sraf ulwp_t *ulwp; 5220Sstevel@tonic-gate int try_flag; 5236247Sraf int ignore_waiters_flag; 5240Sstevel@tonic-gate int error = 0; 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate try_flag = (rd_wr & TRY_FLAG); 5270Sstevel@tonic-gate rd_wr &= ~TRY_FLAG; 5280Sstevel@tonic-gate ASSERT(rd_wr == READ_LOCK || rd_wr == WRITE_LOCK); 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate if (!try_flag) { 5310Sstevel@tonic-gate DTRACE_PROBE2(plockstat, rw__block, rwlp, rd_wr); 5320Sstevel@tonic-gate } 5330Sstevel@tonic-gate 5344570Sraf qp = queue_lock(rwlp, MX); 5356247Sraf /* initial attempt to acquire the lock fails if there are waiters */ 5366247Sraf ignore_waiters_flag = 0; 5374570Sraf while (error == 0) { 5380Sstevel@tonic-gate if (rd_wr == READ_LOCK) { 5396247Sraf if (read_lock_try(rwlp, ignore_waiters_flag)) 5406247Sraf break; 5410Sstevel@tonic-gate } else { 5426247Sraf if (write_lock_try(rwlp, ignore_waiters_flag)) 5436247Sraf break; 5440Sstevel@tonic-gate } 5456247Sraf /* subsequent attempts do not fail due to waiters */ 5466247Sraf ignore_waiters_flag = 1; 5474570Sraf atomic_or_32(rwstate, URW_HAS_WAITERS); 5484570Sraf readers = *rwstate; 5494570Sraf ASSERT_CONSISTENT_STATE(readers); 5504570Sraf if ((readers & URW_WRITE_LOCKED) || 5514570Sraf (rd_wr == WRITE_LOCK && 5524570Sraf (readers & URW_READERS_MASK) != 0)) 5530Sstevel@tonic-gate /* EMPTY */; /* somebody holds the lock */ 5546247Sraf else if ((ulwp = queue_waiter(qp)) == NULL) { 5554570Sraf atomic_and_32(rwstate, ~URW_HAS_WAITERS); 5566247Sraf continue; /* no queued waiters, try again */ 5570Sstevel@tonic-gate } else { 5586247Sraf /* 5596247Sraf * Do a priority check on the queued waiter (the 5606247Sraf * highest priority thread on the queue) to see 5616247Sraf * if we should defer to him or just grab the lock. 5626247Sraf */ 5630Sstevel@tonic-gate int our_pri = real_priority(self); 5640Sstevel@tonic-gate int his_pri = real_priority(ulwp); 5650Sstevel@tonic-gate 5660Sstevel@tonic-gate if (rd_wr == WRITE_LOCK) { 5670Sstevel@tonic-gate /* 5680Sstevel@tonic-gate * We defer to a queued thread that has 5690Sstevel@tonic-gate * a higher priority than ours. 5700Sstevel@tonic-gate */ 5710Sstevel@tonic-gate if (his_pri <= our_pri) 5726247Sraf continue; /* try again */ 5730Sstevel@tonic-gate } else { 5740Sstevel@tonic-gate /* 5750Sstevel@tonic-gate * We defer to a queued thread that has 5760Sstevel@tonic-gate * a higher priority than ours or that 5770Sstevel@tonic-gate * is a writer whose priority equals ours. 5780Sstevel@tonic-gate */ 5790Sstevel@tonic-gate if (his_pri < our_pri || 5800Sstevel@tonic-gate (his_pri == our_pri && !ulwp->ul_writer)) 5816247Sraf continue; /* try again */ 5820Sstevel@tonic-gate } 5830Sstevel@tonic-gate } 5840Sstevel@tonic-gate /* 5850Sstevel@tonic-gate * We are about to block. 5860Sstevel@tonic-gate * If we're doing a trylock, return EBUSY instead. 5870Sstevel@tonic-gate */ 5880Sstevel@tonic-gate if (try_flag) { 5890Sstevel@tonic-gate error = EBUSY; 5900Sstevel@tonic-gate break; 5910Sstevel@tonic-gate } 5920Sstevel@tonic-gate /* 5936247Sraf * Enqueue writers ahead of readers. 5940Sstevel@tonic-gate */ 5950Sstevel@tonic-gate self->ul_writer = rd_wr; /* *must* be 0 or 1 */ 5966247Sraf enqueue(qp, self, 0); 5970Sstevel@tonic-gate set_parking_flag(self, 1); 5980Sstevel@tonic-gate queue_unlock(qp); 5990Sstevel@tonic-gate if ((error = __lwp_park(tsp, 0)) == EINTR) 6006247Sraf error = ignore_waiters_flag = 0; 6010Sstevel@tonic-gate set_parking_flag(self, 0); 6020Sstevel@tonic-gate qp = queue_lock(rwlp, MX); 6036247Sraf if (self->ul_sleepq && dequeue_self(qp) == 0) 6044570Sraf atomic_and_32(rwstate, ~URW_HAS_WAITERS); 6056247Sraf self->ul_writer = 0; 6060Sstevel@tonic-gate } 6070Sstevel@tonic-gate 6084570Sraf queue_unlock(qp); 6094570Sraf 6104570Sraf if (!try_flag) { 6114570Sraf DTRACE_PROBE3(plockstat, rw__blocked, rwlp, rd_wr, error == 0); 6124570Sraf } 6130Sstevel@tonic-gate 6140Sstevel@tonic-gate return (error); 6150Sstevel@tonic-gate } 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate int 6180Sstevel@tonic-gate rw_rdlock_impl(rwlock_t *rwlp, timespec_t *tsp) 6190Sstevel@tonic-gate { 6200Sstevel@tonic-gate ulwp_t *self = curthread; 6210Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 6220Sstevel@tonic-gate readlock_t *readlockp; 6230Sstevel@tonic-gate tdb_rwlock_stats_t *rwsp = RWLOCK_STATS(rwlp, udp); 6240Sstevel@tonic-gate int error; 6250Sstevel@tonic-gate 6260Sstevel@tonic-gate /* 6270Sstevel@tonic-gate * If we already hold a readers lock on this rwlock, 6280Sstevel@tonic-gate * just increment our reference count and return. 6290Sstevel@tonic-gate */ 6304570Sraf sigoff(self); 6310Sstevel@tonic-gate readlockp = rwl_entry(rwlp); 6320Sstevel@tonic-gate if (readlockp->rd_count != 0) { 6334570Sraf if (readlockp->rd_count == READ_LOCK_MAX) { 6344570Sraf sigon(self); 6354570Sraf error = EAGAIN; 6364570Sraf goto out; 6374570Sraf } 6384570Sraf sigon(self); 6394570Sraf error = 0; 6404570Sraf goto out; 6410Sstevel@tonic-gate } 6424570Sraf sigon(self); 6430Sstevel@tonic-gate 6440Sstevel@tonic-gate /* 6450Sstevel@tonic-gate * If we hold the writer lock, bail out. 6460Sstevel@tonic-gate */ 6476812Sraf if (rw_write_held(rwlp)) { 6480Sstevel@tonic-gate if (self->ul_error_detection) 6490Sstevel@tonic-gate rwlock_error(rwlp, "rwlock_rdlock", 6500Sstevel@tonic-gate "calling thread owns the writer lock"); 6514570Sraf error = EDEADLK; 6524570Sraf goto out; 6530Sstevel@tonic-gate } 6540Sstevel@tonic-gate 6554570Sraf if (read_lock_try(rwlp, 0)) 6564570Sraf error = 0; 6574570Sraf else if (rwlp->rwlock_type == USYNC_PROCESS) /* kernel-level */ 6580Sstevel@tonic-gate error = shared_rwlock_lock(rwlp, tsp, READ_LOCK); 6590Sstevel@tonic-gate else /* user-level */ 6600Sstevel@tonic-gate error = rwlock_lock(rwlp, tsp, READ_LOCK); 6610Sstevel@tonic-gate 6624570Sraf out: 6630Sstevel@tonic-gate if (error == 0) { 6644570Sraf sigoff(self); 6654570Sraf rwl_entry(rwlp)->rd_count++; 6664570Sraf sigon(self); 6670Sstevel@tonic-gate if (rwsp) 6680Sstevel@tonic-gate tdb_incr(rwsp->rw_rdlock); 6694570Sraf DTRACE_PROBE2(plockstat, rw__acquire, rwlp, READ_LOCK); 6704570Sraf } else { 6714570Sraf DTRACE_PROBE3(plockstat, rw__error, rwlp, READ_LOCK, error); 6720Sstevel@tonic-gate } 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate return (error); 6750Sstevel@tonic-gate } 6760Sstevel@tonic-gate 6776812Sraf #pragma weak pthread_rwlock_rdlock = rw_rdlock 6786812Sraf #pragma weak _rw_rdlock = rw_rdlock 6790Sstevel@tonic-gate int 6806812Sraf rw_rdlock(rwlock_t *rwlp) 6810Sstevel@tonic-gate { 6820Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 6830Sstevel@tonic-gate return (rw_rdlock_impl(rwlp, NULL)); 6840Sstevel@tonic-gate } 6850Sstevel@tonic-gate 6860Sstevel@tonic-gate void 6870Sstevel@tonic-gate lrw_rdlock(rwlock_t *rwlp) 6880Sstevel@tonic-gate { 6890Sstevel@tonic-gate enter_critical(curthread); 6900Sstevel@tonic-gate (void) rw_rdlock_impl(rwlp, NULL); 6910Sstevel@tonic-gate } 6920Sstevel@tonic-gate 6930Sstevel@tonic-gate int 6946812Sraf pthread_rwlock_reltimedrdlock_np(pthread_rwlock_t *_RESTRICT_KYWD rwlp, 6956812Sraf const struct timespec *_RESTRICT_KYWD reltime) 6960Sstevel@tonic-gate { 6970Sstevel@tonic-gate timespec_t tslocal = *reltime; 6980Sstevel@tonic-gate int error; 6990Sstevel@tonic-gate 7000Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 7016812Sraf error = rw_rdlock_impl((rwlock_t *)rwlp, &tslocal); 7020Sstevel@tonic-gate if (error == ETIME) 7030Sstevel@tonic-gate error = ETIMEDOUT; 7040Sstevel@tonic-gate return (error); 7050Sstevel@tonic-gate } 7060Sstevel@tonic-gate 7070Sstevel@tonic-gate int 7086812Sraf pthread_rwlock_timedrdlock(pthread_rwlock_t *_RESTRICT_KYWD rwlp, 7096812Sraf const struct timespec *_RESTRICT_KYWD abstime) 7100Sstevel@tonic-gate { 7110Sstevel@tonic-gate timespec_t tslocal; 7120Sstevel@tonic-gate int error; 7130Sstevel@tonic-gate 7140Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 7150Sstevel@tonic-gate abstime_to_reltime(CLOCK_REALTIME, abstime, &tslocal); 7166812Sraf error = rw_rdlock_impl((rwlock_t *)rwlp, &tslocal); 7170Sstevel@tonic-gate if (error == ETIME) 7180Sstevel@tonic-gate error = ETIMEDOUT; 7190Sstevel@tonic-gate return (error); 7200Sstevel@tonic-gate } 7210Sstevel@tonic-gate 7220Sstevel@tonic-gate int 7230Sstevel@tonic-gate rw_wrlock_impl(rwlock_t *rwlp, timespec_t *tsp) 7240Sstevel@tonic-gate { 7250Sstevel@tonic-gate ulwp_t *self = curthread; 7260Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 7270Sstevel@tonic-gate tdb_rwlock_stats_t *rwsp = RWLOCK_STATS(rwlp, udp); 7280Sstevel@tonic-gate int error; 7290Sstevel@tonic-gate 7300Sstevel@tonic-gate /* 7310Sstevel@tonic-gate * If we hold a readers lock on this rwlock, bail out. 7320Sstevel@tonic-gate */ 7336812Sraf if (rw_read_held(rwlp)) { 7340Sstevel@tonic-gate if (self->ul_error_detection) 7350Sstevel@tonic-gate rwlock_error(rwlp, "rwlock_wrlock", 7360Sstevel@tonic-gate "calling thread owns the readers lock"); 7374570Sraf error = EDEADLK; 7384570Sraf goto out; 7390Sstevel@tonic-gate } 7400Sstevel@tonic-gate 7410Sstevel@tonic-gate /* 7420Sstevel@tonic-gate * If we hold the writer lock, bail out. 7430Sstevel@tonic-gate */ 7446812Sraf if (rw_write_held(rwlp)) { 7450Sstevel@tonic-gate if (self->ul_error_detection) 7460Sstevel@tonic-gate rwlock_error(rwlp, "rwlock_wrlock", 7470Sstevel@tonic-gate "calling thread owns the writer lock"); 7484570Sraf error = EDEADLK; 7494570Sraf goto out; 7500Sstevel@tonic-gate } 7510Sstevel@tonic-gate 7524570Sraf if (write_lock_try(rwlp, 0)) 7534570Sraf error = 0; 7544570Sraf else if (rwlp->rwlock_type == USYNC_PROCESS) /* kernel-level */ 7550Sstevel@tonic-gate error = shared_rwlock_lock(rwlp, tsp, WRITE_LOCK); 7564570Sraf else /* user-level */ 7570Sstevel@tonic-gate error = rwlock_lock(rwlp, tsp, WRITE_LOCK); 7580Sstevel@tonic-gate 7594570Sraf out: 7604570Sraf if (error == 0) { 7614570Sraf rwlp->rwlock_owner = (uintptr_t)self; 7624570Sraf if (rwlp->rwlock_type == USYNC_PROCESS) 7634570Sraf rwlp->rwlock_ownerpid = udp->pid; 7644570Sraf if (rwsp) { 7654570Sraf tdb_incr(rwsp->rw_wrlock); 7664570Sraf rwsp->rw_wrlock_begin_hold = gethrtime(); 7674570Sraf } 7684570Sraf DTRACE_PROBE2(plockstat, rw__acquire, rwlp, WRITE_LOCK); 7694570Sraf } else { 7704570Sraf DTRACE_PROBE3(plockstat, rw__error, rwlp, WRITE_LOCK, error); 7710Sstevel@tonic-gate } 7720Sstevel@tonic-gate return (error); 7730Sstevel@tonic-gate } 7740Sstevel@tonic-gate 7756812Sraf #pragma weak pthread_rwlock_wrlock = rw_wrlock 7766812Sraf #pragma weak _rw_wrlock = rw_wrlock 7770Sstevel@tonic-gate int 7786812Sraf rw_wrlock(rwlock_t *rwlp) 7790Sstevel@tonic-gate { 7800Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 7810Sstevel@tonic-gate return (rw_wrlock_impl(rwlp, NULL)); 7820Sstevel@tonic-gate } 7830Sstevel@tonic-gate 7840Sstevel@tonic-gate void 7850Sstevel@tonic-gate lrw_wrlock(rwlock_t *rwlp) 7860Sstevel@tonic-gate { 7870Sstevel@tonic-gate enter_critical(curthread); 7880Sstevel@tonic-gate (void) rw_wrlock_impl(rwlp, NULL); 7890Sstevel@tonic-gate } 7900Sstevel@tonic-gate 7910Sstevel@tonic-gate int 7926812Sraf pthread_rwlock_reltimedwrlock_np(pthread_rwlock_t *_RESTRICT_KYWD rwlp, 7936812Sraf const struct timespec *_RESTRICT_KYWD reltime) 7940Sstevel@tonic-gate { 7950Sstevel@tonic-gate timespec_t tslocal = *reltime; 7960Sstevel@tonic-gate int error; 7970Sstevel@tonic-gate 7980Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 7996812Sraf error = rw_wrlock_impl((rwlock_t *)rwlp, &tslocal); 8000Sstevel@tonic-gate if (error == ETIME) 8010Sstevel@tonic-gate error = ETIMEDOUT; 8020Sstevel@tonic-gate return (error); 8030Sstevel@tonic-gate } 8040Sstevel@tonic-gate 8050Sstevel@tonic-gate int 8066812Sraf pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlp, const timespec_t *abstime) 8070Sstevel@tonic-gate { 8080Sstevel@tonic-gate timespec_t tslocal; 8090Sstevel@tonic-gate int error; 8100Sstevel@tonic-gate 8110Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 8120Sstevel@tonic-gate abstime_to_reltime(CLOCK_REALTIME, abstime, &tslocal); 8136812Sraf error = rw_wrlock_impl((rwlock_t *)rwlp, &tslocal); 8140Sstevel@tonic-gate if (error == ETIME) 8150Sstevel@tonic-gate error = ETIMEDOUT; 8160Sstevel@tonic-gate return (error); 8170Sstevel@tonic-gate } 8180Sstevel@tonic-gate 8196812Sraf #pragma weak pthread_rwlock_tryrdlock = rw_tryrdlock 8200Sstevel@tonic-gate int 8216812Sraf rw_tryrdlock(rwlock_t *rwlp) 8220Sstevel@tonic-gate { 8230Sstevel@tonic-gate ulwp_t *self = curthread; 8240Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 8250Sstevel@tonic-gate tdb_rwlock_stats_t *rwsp = RWLOCK_STATS(rwlp, udp); 8260Sstevel@tonic-gate readlock_t *readlockp; 8270Sstevel@tonic-gate int error; 8280Sstevel@tonic-gate 8290Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 8300Sstevel@tonic-gate 8310Sstevel@tonic-gate if (rwsp) 8320Sstevel@tonic-gate tdb_incr(rwsp->rw_rdlock_try); 8330Sstevel@tonic-gate 8340Sstevel@tonic-gate /* 8350Sstevel@tonic-gate * If we already hold a readers lock on this rwlock, 8360Sstevel@tonic-gate * just increment our reference count and return. 8370Sstevel@tonic-gate */ 8384570Sraf sigoff(self); 8390Sstevel@tonic-gate readlockp = rwl_entry(rwlp); 8400Sstevel@tonic-gate if (readlockp->rd_count != 0) { 8414570Sraf if (readlockp->rd_count == READ_LOCK_MAX) { 8424570Sraf sigon(self); 8434570Sraf error = EAGAIN; 8444570Sraf goto out; 8454570Sraf } 8464570Sraf sigon(self); 8474570Sraf error = 0; 8484570Sraf goto out; 8490Sstevel@tonic-gate } 8504570Sraf sigon(self); 8510Sstevel@tonic-gate 8524570Sraf if (read_lock_try(rwlp, 0)) 8534570Sraf error = 0; 8544570Sraf else if (rwlp->rwlock_type == USYNC_PROCESS) /* kernel-level */ 8550Sstevel@tonic-gate error = shared_rwlock_lock(rwlp, NULL, READ_LOCK_TRY); 8560Sstevel@tonic-gate else /* user-level */ 8570Sstevel@tonic-gate error = rwlock_lock(rwlp, NULL, READ_LOCK_TRY); 8580Sstevel@tonic-gate 8594570Sraf out: 8604570Sraf if (error == 0) { 8614570Sraf sigoff(self); 8624570Sraf rwl_entry(rwlp)->rd_count++; 8634570Sraf sigon(self); 8644570Sraf DTRACE_PROBE2(plockstat, rw__acquire, rwlp, READ_LOCK); 8654570Sraf } else { 8664570Sraf if (rwsp) 8674570Sraf tdb_incr(rwsp->rw_rdlock_try_fail); 8684570Sraf if (error != EBUSY) { 8694570Sraf DTRACE_PROBE3(plockstat, rw__error, rwlp, READ_LOCK, 8704570Sraf error); 8714570Sraf } 8724570Sraf } 8730Sstevel@tonic-gate 8740Sstevel@tonic-gate return (error); 8750Sstevel@tonic-gate } 8760Sstevel@tonic-gate 8776812Sraf #pragma weak pthread_rwlock_trywrlock = rw_trywrlock 8780Sstevel@tonic-gate int 8796812Sraf rw_trywrlock(rwlock_t *rwlp) 8800Sstevel@tonic-gate { 8810Sstevel@tonic-gate ulwp_t *self = curthread; 8820Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 8830Sstevel@tonic-gate tdb_rwlock_stats_t *rwsp = RWLOCK_STATS(rwlp, udp); 8840Sstevel@tonic-gate int error; 8850Sstevel@tonic-gate 8864570Sraf ASSERT(!self->ul_critical || self->ul_bindflags); 8870Sstevel@tonic-gate 8880Sstevel@tonic-gate if (rwsp) 8890Sstevel@tonic-gate tdb_incr(rwsp->rw_wrlock_try); 8900Sstevel@tonic-gate 8914570Sraf if (write_lock_try(rwlp, 0)) 8924570Sraf error = 0; 8934570Sraf else if (rwlp->rwlock_type == USYNC_PROCESS) /* kernel-level */ 8940Sstevel@tonic-gate error = shared_rwlock_lock(rwlp, NULL, WRITE_LOCK_TRY); 8954570Sraf else /* user-level */ 8960Sstevel@tonic-gate error = rwlock_lock(rwlp, NULL, WRITE_LOCK_TRY); 8974570Sraf 8984570Sraf if (error == 0) { 8994570Sraf rwlp->rwlock_owner = (uintptr_t)self; 9004570Sraf if (rwlp->rwlock_type == USYNC_PROCESS) 9014570Sraf rwlp->rwlock_ownerpid = udp->pid; 9024570Sraf if (rwsp) 9034570Sraf rwsp->rw_wrlock_begin_hold = gethrtime(); 9044570Sraf DTRACE_PROBE2(plockstat, rw__acquire, rwlp, WRITE_LOCK); 9054570Sraf } else { 9064570Sraf if (rwsp) 9070Sstevel@tonic-gate tdb_incr(rwsp->rw_wrlock_try_fail); 9084570Sraf if (error != EBUSY) { 9094570Sraf DTRACE_PROBE3(plockstat, rw__error, rwlp, WRITE_LOCK, 9104570Sraf error); 9114570Sraf } 9120Sstevel@tonic-gate } 9130Sstevel@tonic-gate return (error); 9140Sstevel@tonic-gate } 9150Sstevel@tonic-gate 9166812Sraf #pragma weak pthread_rwlock_unlock = rw_unlock 9176812Sraf #pragma weak _rw_unlock = rw_unlock 9180Sstevel@tonic-gate int 9196812Sraf rw_unlock(rwlock_t *rwlp) 9200Sstevel@tonic-gate { 9214570Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 9224570Sraf uint32_t readers; 9230Sstevel@tonic-gate ulwp_t *self = curthread; 9240Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 9250Sstevel@tonic-gate tdb_rwlock_stats_t *rwsp; 9264570Sraf queue_head_t *qp; 9274570Sraf int rd_wr; 9284570Sraf int waked = 0; 9290Sstevel@tonic-gate 9304570Sraf readers = *rwstate; 9314570Sraf ASSERT_CONSISTENT_STATE(readers); 9324570Sraf if (readers & URW_WRITE_LOCKED) { 9334570Sraf rd_wr = WRITE_LOCK; 9344570Sraf readers = 0; 9354570Sraf } else { 9364570Sraf rd_wr = READ_LOCK; 9374570Sraf readers &= URW_READERS_MASK; 9380Sstevel@tonic-gate } 9390Sstevel@tonic-gate 9404570Sraf if (rd_wr == WRITE_LOCK) { 9410Sstevel@tonic-gate /* 9420Sstevel@tonic-gate * Since the writer lock is held, we'd better be 9430Sstevel@tonic-gate * holding it, else we cannot legitimately be here. 9440Sstevel@tonic-gate */ 9456812Sraf if (!rw_write_held(rwlp)) { 9460Sstevel@tonic-gate if (self->ul_error_detection) 9470Sstevel@tonic-gate rwlock_error(rwlp, "rwlock_unlock", 9480Sstevel@tonic-gate "writer lock held, " 9490Sstevel@tonic-gate "but not by the calling thread"); 9500Sstevel@tonic-gate return (EPERM); 9510Sstevel@tonic-gate } 9520Sstevel@tonic-gate if ((rwsp = RWLOCK_STATS(rwlp, udp)) != NULL) { 9530Sstevel@tonic-gate if (rwsp->rw_wrlock_begin_hold) 9540Sstevel@tonic-gate rwsp->rw_wrlock_hold_time += 9550Sstevel@tonic-gate gethrtime() - rwsp->rw_wrlock_begin_hold; 9560Sstevel@tonic-gate rwsp->rw_wrlock_begin_hold = 0; 9570Sstevel@tonic-gate } 9584570Sraf rwlp->rwlock_owner = 0; 9594570Sraf rwlp->rwlock_ownerpid = 0; 9604570Sraf } else if (readers > 0) { 9610Sstevel@tonic-gate /* 9620Sstevel@tonic-gate * A readers lock is held; if we don't hold one, bail out. 9630Sstevel@tonic-gate */ 9644570Sraf readlock_t *readlockp; 9654570Sraf 9664570Sraf sigoff(self); 9674570Sraf readlockp = rwl_entry(rwlp); 9680Sstevel@tonic-gate if (readlockp->rd_count == 0) { 9694570Sraf sigon(self); 9700Sstevel@tonic-gate if (self->ul_error_detection) 9710Sstevel@tonic-gate rwlock_error(rwlp, "rwlock_unlock", 9720Sstevel@tonic-gate "readers lock held, " 9730Sstevel@tonic-gate "but not by the calling thread"); 9740Sstevel@tonic-gate return (EPERM); 9750Sstevel@tonic-gate } 9760Sstevel@tonic-gate /* 9770Sstevel@tonic-gate * If we hold more than one readers lock on this rwlock, 9780Sstevel@tonic-gate * just decrement our reference count and return. 9790Sstevel@tonic-gate */ 9800Sstevel@tonic-gate if (--readlockp->rd_count != 0) { 9814570Sraf sigon(self); 9824570Sraf goto out; 9830Sstevel@tonic-gate } 9844570Sraf sigon(self); 9850Sstevel@tonic-gate } else { 9860Sstevel@tonic-gate /* 9870Sstevel@tonic-gate * This is a usage error. 9880Sstevel@tonic-gate * No thread should release an unowned lock. 9890Sstevel@tonic-gate */ 9900Sstevel@tonic-gate if (self->ul_error_detection) 9910Sstevel@tonic-gate rwlock_error(rwlp, "rwlock_unlock", "lock not owned"); 9920Sstevel@tonic-gate return (EPERM); 9930Sstevel@tonic-gate } 9940Sstevel@tonic-gate 9954570Sraf if (rd_wr == WRITE_LOCK && write_unlock_try(rwlp)) { 9964570Sraf /* EMPTY */; 9974570Sraf } else if (rd_wr == READ_LOCK && read_unlock_try(rwlp)) { 9984570Sraf /* EMPTY */; 9994570Sraf } else if (rwlp->rwlock_type == USYNC_PROCESS) { 10006515Sraf (void) mutex_lock(&rwlp->mutex); 10014570Sraf (void) __lwp_rwlock_unlock(rwlp); 10026515Sraf (void) mutex_unlock(&rwlp->mutex); 10034570Sraf waked = 1; 10044570Sraf } else { 10050Sstevel@tonic-gate qp = queue_lock(rwlp, MX); 10064570Sraf if (rd_wr == READ_LOCK) 10074570Sraf atomic_dec_32(rwstate); 10084570Sraf else 10094570Sraf atomic_and_32(rwstate, ~URW_WRITE_LOCKED); 10100Sstevel@tonic-gate waked = rw_queue_release(qp, rwlp); 10110Sstevel@tonic-gate } 10120Sstevel@tonic-gate 10134570Sraf out: 10144570Sraf DTRACE_PROBE2(plockstat, rw__release, rwlp, rd_wr); 10154570Sraf 10160Sstevel@tonic-gate /* 10170Sstevel@tonic-gate * Yield to the thread we just waked up, just in case we might 10180Sstevel@tonic-gate * be about to grab the rwlock again immediately upon return. 10190Sstevel@tonic-gate * This is pretty weak but it helps on a uniprocessor and also 10200Sstevel@tonic-gate * when cpu affinity has assigned both ourself and the other 10210Sstevel@tonic-gate * thread to the same CPU. Note that lwp_yield() will yield 10220Sstevel@tonic-gate * the processor only if the writer is at the same or higher 10230Sstevel@tonic-gate * priority than ourself. This provides more balanced program 10240Sstevel@tonic-gate * behavior; it doesn't guarantee acquisition of the lock by 10250Sstevel@tonic-gate * the pending writer. 10260Sstevel@tonic-gate */ 10270Sstevel@tonic-gate if (waked) 10286515Sraf yield(); 10290Sstevel@tonic-gate return (0); 10300Sstevel@tonic-gate } 10310Sstevel@tonic-gate 10320Sstevel@tonic-gate void 10330Sstevel@tonic-gate lrw_unlock(rwlock_t *rwlp) 10340Sstevel@tonic-gate { 10356812Sraf (void) rw_unlock(rwlp); 10360Sstevel@tonic-gate exit_critical(curthread); 10370Sstevel@tonic-gate } 1038