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 /* 234570Sraf * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include "lint.h" 300Sstevel@tonic-gate #include "thr_uberdata.h" 310Sstevel@tonic-gate #include <sys/sdt.h> 320Sstevel@tonic-gate 330Sstevel@tonic-gate #define TRY_FLAG 0x10 340Sstevel@tonic-gate #define READ_LOCK 0 350Sstevel@tonic-gate #define WRITE_LOCK 1 360Sstevel@tonic-gate #define READ_LOCK_TRY (READ_LOCK | TRY_FLAG) 370Sstevel@tonic-gate #define WRITE_LOCK_TRY (WRITE_LOCK | TRY_FLAG) 380Sstevel@tonic-gate 390Sstevel@tonic-gate #define NLOCKS 4 /* initial number of readlock_t structs allocated */ 400Sstevel@tonic-gate 414570Sraf #define ASSERT_CONSISTENT_STATE(readers) \ 424570Sraf ASSERT(!((readers) & URW_WRITE_LOCKED) || \ 434570Sraf ((readers) & ~URW_HAS_WAITERS) == URW_WRITE_LOCKED) 444570Sraf 450Sstevel@tonic-gate /* 460Sstevel@tonic-gate * Find/allocate an entry for rwlp in our array of rwlocks held for reading. 474570Sraf * We must be deferring signals for this to be safe. 48*4574Sraf * Else if we are returning an entry with ul_rdlockcnt == 0, 494570Sraf * it could be reassigned behind our back in a signal handler. 500Sstevel@tonic-gate */ 510Sstevel@tonic-gate static readlock_t * 520Sstevel@tonic-gate rwl_entry(rwlock_t *rwlp) 530Sstevel@tonic-gate { 540Sstevel@tonic-gate ulwp_t *self = curthread; 550Sstevel@tonic-gate readlock_t *remembered = NULL; 560Sstevel@tonic-gate readlock_t *readlockp; 570Sstevel@tonic-gate uint_t nlocks; 580Sstevel@tonic-gate 594570Sraf /* we must be deferring signals */ 604570Sraf ASSERT((self->ul_critical + self->ul_sigdefer) != 0); 614570Sraf 62*4574Sraf if ((nlocks = self->ul_rdlockcnt) != 0) 630Sstevel@tonic-gate readlockp = self->ul_readlock.array; 640Sstevel@tonic-gate else { 650Sstevel@tonic-gate nlocks = 1; 660Sstevel@tonic-gate readlockp = &self->ul_readlock.single; 670Sstevel@tonic-gate } 680Sstevel@tonic-gate 690Sstevel@tonic-gate for (; nlocks; nlocks--, readlockp++) { 700Sstevel@tonic-gate if (readlockp->rd_rwlock == rwlp) 710Sstevel@tonic-gate return (readlockp); 720Sstevel@tonic-gate if (readlockp->rd_count == 0 && remembered == NULL) 730Sstevel@tonic-gate remembered = readlockp; 740Sstevel@tonic-gate } 750Sstevel@tonic-gate if (remembered != NULL) { 760Sstevel@tonic-gate remembered->rd_rwlock = rwlp; 770Sstevel@tonic-gate return (remembered); 780Sstevel@tonic-gate } 790Sstevel@tonic-gate 800Sstevel@tonic-gate /* 810Sstevel@tonic-gate * No entry available. Allocate more space, converting the single 820Sstevel@tonic-gate * readlock_t entry into an array of readlock_t entries if necessary. 830Sstevel@tonic-gate */ 84*4574Sraf if ((nlocks = self->ul_rdlockcnt) == 0) { 850Sstevel@tonic-gate /* 860Sstevel@tonic-gate * Initial allocation of the readlock_t array. 870Sstevel@tonic-gate * Convert the single entry into an array. 880Sstevel@tonic-gate */ 89*4574Sraf self->ul_rdlockcnt = nlocks = NLOCKS; 900Sstevel@tonic-gate readlockp = lmalloc(nlocks * sizeof (readlock_t)); 910Sstevel@tonic-gate /* 920Sstevel@tonic-gate * The single readlock_t becomes the first entry in the array. 930Sstevel@tonic-gate */ 940Sstevel@tonic-gate *readlockp = self->ul_readlock.single; 950Sstevel@tonic-gate self->ul_readlock.single.rd_count = 0; 960Sstevel@tonic-gate self->ul_readlock.array = readlockp; 970Sstevel@tonic-gate /* 980Sstevel@tonic-gate * Return the next available entry in the array. 990Sstevel@tonic-gate */ 1000Sstevel@tonic-gate (++readlockp)->rd_rwlock = rwlp; 1010Sstevel@tonic-gate return (readlockp); 1020Sstevel@tonic-gate } 1030Sstevel@tonic-gate /* 1040Sstevel@tonic-gate * Reallocate the array, double the size each time. 1050Sstevel@tonic-gate */ 1060Sstevel@tonic-gate readlockp = lmalloc(nlocks * 2 * sizeof (readlock_t)); 1070Sstevel@tonic-gate (void) _memcpy(readlockp, self->ul_readlock.array, 1080Sstevel@tonic-gate nlocks * sizeof (readlock_t)); 1090Sstevel@tonic-gate lfree(self->ul_readlock.array, nlocks * sizeof (readlock_t)); 1100Sstevel@tonic-gate self->ul_readlock.array = readlockp; 111*4574Sraf self->ul_rdlockcnt *= 2; 1120Sstevel@tonic-gate /* 1130Sstevel@tonic-gate * Return the next available entry in the newly allocated array. 1140Sstevel@tonic-gate */ 1150Sstevel@tonic-gate (readlockp += nlocks)->rd_rwlock = rwlp; 1160Sstevel@tonic-gate return (readlockp); 1170Sstevel@tonic-gate } 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate /* 1200Sstevel@tonic-gate * Free the array of rwlocks held for reading. 1210Sstevel@tonic-gate */ 1220Sstevel@tonic-gate void 1230Sstevel@tonic-gate rwl_free(ulwp_t *ulwp) 1240Sstevel@tonic-gate { 1250Sstevel@tonic-gate uint_t nlocks; 1260Sstevel@tonic-gate 127*4574Sraf if ((nlocks = ulwp->ul_rdlockcnt) != 0) 1280Sstevel@tonic-gate lfree(ulwp->ul_readlock.array, nlocks * sizeof (readlock_t)); 129*4574Sraf ulwp->ul_rdlockcnt = 0; 1300Sstevel@tonic-gate ulwp->ul_readlock.single.rd_rwlock = NULL; 1310Sstevel@tonic-gate ulwp->ul_readlock.single.rd_count = 0; 1320Sstevel@tonic-gate } 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate /* 1350Sstevel@tonic-gate * Check if a reader version of the lock is held by the current thread. 1360Sstevel@tonic-gate * rw_read_is_held() is private to libc. 1370Sstevel@tonic-gate */ 1380Sstevel@tonic-gate #pragma weak rw_read_is_held = _rw_read_held 1390Sstevel@tonic-gate #pragma weak rw_read_held = _rw_read_held 1400Sstevel@tonic-gate int 1410Sstevel@tonic-gate _rw_read_held(rwlock_t *rwlp) 1420Sstevel@tonic-gate { 1434570Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 1444570Sraf uint32_t readers; 1454570Sraf ulwp_t *self = curthread; 1460Sstevel@tonic-gate readlock_t *readlockp; 1470Sstevel@tonic-gate uint_t nlocks; 1484570Sraf int rval = 0; 1490Sstevel@tonic-gate 1504570Sraf no_preempt(self); 1514570Sraf 1524570Sraf readers = *rwstate; 1534570Sraf ASSERT_CONSISTENT_STATE(readers); 1544570Sraf if (!(readers & URW_WRITE_LOCKED) && 1554570Sraf (readers & URW_READERS_MASK) != 0) { 1564570Sraf /* 1574570Sraf * The lock is held for reading by some thread. 1584570Sraf * Search our array of rwlocks held for reading for a match. 1594570Sraf */ 160*4574Sraf if ((nlocks = self->ul_rdlockcnt) != 0) 1614570Sraf readlockp = self->ul_readlock.array; 1624570Sraf else { 1634570Sraf nlocks = 1; 1644570Sraf readlockp = &self->ul_readlock.single; 1654570Sraf } 1664570Sraf for (; nlocks; nlocks--, readlockp++) { 1674570Sraf if (readlockp->rd_rwlock == rwlp) { 1684570Sraf if (readlockp->rd_count) 1694570Sraf rval = 1; 1704570Sraf break; 1714570Sraf } 1724570Sraf } 1730Sstevel@tonic-gate } 1740Sstevel@tonic-gate 1754570Sraf preempt(self); 1764570Sraf return (rval); 1770Sstevel@tonic-gate } 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate /* 1800Sstevel@tonic-gate * Check if a writer version of the lock is held by the current thread. 1810Sstevel@tonic-gate * rw_write_is_held() is private to libc. 1820Sstevel@tonic-gate */ 1830Sstevel@tonic-gate #pragma weak rw_write_is_held = _rw_write_held 1840Sstevel@tonic-gate #pragma weak rw_write_held = _rw_write_held 1850Sstevel@tonic-gate int 1860Sstevel@tonic-gate _rw_write_held(rwlock_t *rwlp) 1870Sstevel@tonic-gate { 1884570Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 1894570Sraf uint32_t readers; 1900Sstevel@tonic-gate ulwp_t *self = curthread; 1914570Sraf int rval; 1924570Sraf 1934570Sraf no_preempt(self); 1940Sstevel@tonic-gate 1954570Sraf readers = *rwstate; 1964570Sraf ASSERT_CONSISTENT_STATE(readers); 1974570Sraf rval = ((readers & URW_WRITE_LOCKED) && 1984570Sraf rwlp->rwlock_owner == (uintptr_t)self && 1994570Sraf (rwlp->rwlock_type == USYNC_THREAD || 2004570Sraf rwlp->rwlock_ownerpid == self->ul_uberdata->pid)); 2010Sstevel@tonic-gate 2024570Sraf preempt(self); 2034570Sraf return (rval); 2040Sstevel@tonic-gate } 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate #pragma weak rwlock_init = __rwlock_init 2070Sstevel@tonic-gate #pragma weak _rwlock_init = __rwlock_init 2080Sstevel@tonic-gate /* ARGSUSED2 */ 2090Sstevel@tonic-gate int 2100Sstevel@tonic-gate __rwlock_init(rwlock_t *rwlp, int type, void *arg) 2110Sstevel@tonic-gate { 2120Sstevel@tonic-gate if (type != USYNC_THREAD && type != USYNC_PROCESS) 2130Sstevel@tonic-gate return (EINVAL); 2140Sstevel@tonic-gate /* 2150Sstevel@tonic-gate * Once reinitialized, we can no longer be holding a read or write lock. 2160Sstevel@tonic-gate * We can do nothing about other threads that are holding read locks. 2170Sstevel@tonic-gate */ 2184570Sraf sigoff(curthread); 2194570Sraf rwl_entry(rwlp)->rd_count = 0; 2204570Sraf sigon(curthread); 2210Sstevel@tonic-gate (void) _memset(rwlp, 0, sizeof (*rwlp)); 2220Sstevel@tonic-gate rwlp->rwlock_type = (uint16_t)type; 2230Sstevel@tonic-gate rwlp->rwlock_magic = RWL_MAGIC; 2240Sstevel@tonic-gate rwlp->mutex.mutex_type = (uint8_t)type; 2250Sstevel@tonic-gate rwlp->mutex.mutex_flag = LOCK_INITED; 2260Sstevel@tonic-gate rwlp->mutex.mutex_magic = MUTEX_MAGIC; 2270Sstevel@tonic-gate return (0); 2280Sstevel@tonic-gate } 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate #pragma weak rwlock_destroy = __rwlock_destroy 2310Sstevel@tonic-gate #pragma weak _rwlock_destroy = __rwlock_destroy 2320Sstevel@tonic-gate #pragma weak pthread_rwlock_destroy = __rwlock_destroy 2330Sstevel@tonic-gate #pragma weak _pthread_rwlock_destroy = __rwlock_destroy 2340Sstevel@tonic-gate int 2350Sstevel@tonic-gate __rwlock_destroy(rwlock_t *rwlp) 2360Sstevel@tonic-gate { 2370Sstevel@tonic-gate /* 2380Sstevel@tonic-gate * Once destroyed, we can no longer be holding a read or write lock. 2390Sstevel@tonic-gate * We can do nothing about other threads that are holding read locks. 2400Sstevel@tonic-gate */ 2414570Sraf sigoff(curthread); 2424570Sraf rwl_entry(rwlp)->rd_count = 0; 2434570Sraf sigon(curthread); 2440Sstevel@tonic-gate rwlp->rwlock_magic = 0; 2450Sstevel@tonic-gate tdb_sync_obj_deregister(rwlp); 2460Sstevel@tonic-gate return (0); 2470Sstevel@tonic-gate } 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate /* 2504570Sraf * Attempt to acquire a readers lock. Return true on success. 2514570Sraf */ 2524570Sraf static int 2534570Sraf read_lock_try(rwlock_t *rwlp, int ignore_waiters_flag) 2544570Sraf { 2554570Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 2564570Sraf uint32_t mask = ignore_waiters_flag? 2574570Sraf URW_WRITE_LOCKED : (URW_HAS_WAITERS | URW_WRITE_LOCKED); 2584570Sraf uint32_t readers; 2594570Sraf ulwp_t *self = curthread; 2604570Sraf 2614570Sraf no_preempt(self); 2624570Sraf while (((readers = *rwstate) & mask) == 0) { 2634570Sraf if (atomic_cas_32(rwstate, readers, readers + 1) == readers) { 2644570Sraf preempt(self); 2654570Sraf return (1); 2664570Sraf } 2674570Sraf } 2684570Sraf preempt(self); 2694570Sraf return (0); 2704570Sraf } 2714570Sraf 2724570Sraf /* 2734570Sraf * Attempt to release a reader lock. Return true on success. 2744570Sraf */ 2754570Sraf static int 2764570Sraf read_unlock_try(rwlock_t *rwlp) 2774570Sraf { 2784570Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 2794570Sraf uint32_t readers; 2804570Sraf ulwp_t *self = curthread; 2814570Sraf 2824570Sraf no_preempt(self); 2834570Sraf while (((readers = *rwstate) & URW_HAS_WAITERS) == 0) { 2844570Sraf if (atomic_cas_32(rwstate, readers, readers - 1) == readers) { 2854570Sraf preempt(self); 2864570Sraf return (1); 2874570Sraf } 2884570Sraf } 2894570Sraf preempt(self); 2904570Sraf return (0); 2914570Sraf } 2924570Sraf 2934570Sraf /* 2944570Sraf * Attempt to acquire a writer lock. Return true on success. 2954570Sraf */ 2964570Sraf static int 2974570Sraf write_lock_try(rwlock_t *rwlp, int ignore_waiters_flag) 2984570Sraf { 2994570Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 3004570Sraf uint32_t mask = ignore_waiters_flag? 3014570Sraf (URW_WRITE_LOCKED | URW_READERS_MASK) : 3024570Sraf (URW_HAS_WAITERS | URW_WRITE_LOCKED | URW_READERS_MASK); 3034570Sraf ulwp_t *self = curthread; 3044570Sraf uint32_t readers; 3054570Sraf 3064570Sraf no_preempt(self); 3074570Sraf while (((readers = *rwstate) & mask) == 0) { 3084570Sraf if (atomic_cas_32(rwstate, readers, readers | URW_WRITE_LOCKED) 3094570Sraf == readers) { 3104570Sraf preempt(self); 3114570Sraf return (1); 3124570Sraf } 3134570Sraf } 3144570Sraf preempt(self); 3154570Sraf return (0); 3164570Sraf } 3174570Sraf 3184570Sraf /* 3194570Sraf * Attempt to release a writer lock. Return true on success. 3204570Sraf */ 3214570Sraf static int 3224570Sraf write_unlock_try(rwlock_t *rwlp) 3234570Sraf { 3244570Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 3254570Sraf uint32_t readers; 3264570Sraf ulwp_t *self = curthread; 3274570Sraf 3284570Sraf no_preempt(self); 3294570Sraf while (((readers = *rwstate) & URW_HAS_WAITERS) == 0) { 3304570Sraf if (atomic_cas_32(rwstate, readers, 0) == readers) { 3314570Sraf preempt(self); 3324570Sraf return (1); 3334570Sraf } 3344570Sraf } 3354570Sraf preempt(self); 3364570Sraf return (0); 3374570Sraf } 3384570Sraf 3394570Sraf /* 3404570Sraf * Wake up thread(s) sleeping on the rwlock queue and then 3410Sstevel@tonic-gate * drop the queue lock. Return non-zero if we wake up someone. 3424570Sraf * This is called when a thread releases a lock that appears to have waiters. 3430Sstevel@tonic-gate */ 3440Sstevel@tonic-gate static int 3450Sstevel@tonic-gate rw_queue_release(queue_head_t *qp, rwlock_t *rwlp) 3460Sstevel@tonic-gate { 3474570Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 3484570Sraf uint32_t readers; 3494570Sraf uint32_t writers; 3504570Sraf int nlwpid = 0; 3514570Sraf int maxlwps = MAXLWPS; 3524570Sraf ulwp_t *self; 3534570Sraf ulwp_t **ulwpp; 3540Sstevel@tonic-gate ulwp_t *ulwp; 3554570Sraf ulwp_t *prev = NULL; 3564570Sraf lwpid_t buffer[MAXLWPS]; 3574570Sraf lwpid_t *lwpid = buffer; 3584570Sraf 3594570Sraf readers = *rwstate; 3604570Sraf ASSERT_CONSISTENT_STATE(readers); 3614570Sraf if (!(readers & URW_HAS_WAITERS)) { 3624570Sraf queue_unlock(qp); 3634570Sraf return (0); 3644570Sraf } 3654570Sraf readers &= URW_READERS_MASK; 3664570Sraf writers = 0; 3670Sstevel@tonic-gate 3684570Sraf /* 3694570Sraf * Walk the list of waiters and prepare to wake up as 3704570Sraf * many readers as we encounter before encountering 3714570Sraf * a writer. If the first thread on the list is a 3724570Sraf * writer, stop there and wake it up. 3734570Sraf * 3744570Sraf * We keep track of lwpids that are to be unparked in lwpid[]. 3754570Sraf * __lwp_unpark_all() is called to unpark all of them after 3764570Sraf * they have been removed from the sleep queue and the sleep 3774570Sraf * queue lock has been dropped. If we run out of space in our 3784570Sraf * on-stack buffer, we need to allocate more but we can't call 3794570Sraf * lmalloc() because we are holding a queue lock when the overflow 3804570Sraf * occurs and lmalloc() acquires a lock. We can't use alloca() 3814570Sraf * either because the application may have allocated a small 3824570Sraf * stack and we don't want to overrun the stack. So we call 3834570Sraf * alloc_lwpids() to allocate a bigger buffer using the mmap() 3844570Sraf * system call directly since that path acquires no locks. 3854570Sraf */ 3864570Sraf ulwpp = &qp->qh_head; 3874570Sraf while ((ulwp = *ulwpp) != NULL) { 3884570Sraf if (ulwp->ul_wchan != rwlp) { 3894570Sraf prev = ulwp; 3904570Sraf ulwpp = &ulwp->ul_link; 3914570Sraf continue; 3920Sstevel@tonic-gate } 3934570Sraf if (ulwp->ul_writer) { 3944570Sraf if (writers != 0 || readers != 0) 3954570Sraf break; 3964570Sraf /* one writer to wake */ 3974570Sraf writers++; 3984570Sraf } else { 3994570Sraf if (writers != 0) 4004570Sraf break; 4014570Sraf /* at least one reader to wake */ 4024570Sraf readers++; 4034570Sraf if (nlwpid == maxlwps) 4044570Sraf lwpid = alloc_lwpids(lwpid, &nlwpid, &maxlwps); 4054570Sraf } 4064570Sraf (void) queue_unlink(qp, ulwpp, prev); 4074570Sraf lwpid[nlwpid++] = ulwp->ul_lwpid; 4080Sstevel@tonic-gate } 4094570Sraf if (ulwp == NULL) 4104570Sraf atomic_and_32(rwstate, ~URW_HAS_WAITERS); 4114570Sraf if (nlwpid == 0) { 4124570Sraf queue_unlock(qp); 4134570Sraf } else { 4144570Sraf self = curthread; 4154570Sraf no_preempt(self); 4164570Sraf queue_unlock(qp); 4174570Sraf if (nlwpid == 1) 4184570Sraf (void) __lwp_unpark(lwpid[0]); 4194570Sraf else 4204570Sraf (void) __lwp_unpark_all(lwpid, nlwpid); 4214570Sraf preempt(self); 4224570Sraf } 4234570Sraf if (lwpid != buffer) 4244570Sraf (void) _private_munmap(lwpid, maxlwps * sizeof (lwpid_t)); 4254570Sraf return (nlwpid != 0); 4260Sstevel@tonic-gate } 4270Sstevel@tonic-gate 4280Sstevel@tonic-gate /* 4290Sstevel@tonic-gate * Common code for rdlock, timedrdlock, wrlock, timedwrlock, tryrdlock, 4300Sstevel@tonic-gate * and trywrlock for process-shared (USYNC_PROCESS) rwlocks. 4310Sstevel@tonic-gate * 4320Sstevel@tonic-gate * Note: if the lock appears to be contended we call __lwp_rwlock_rdlock() 4330Sstevel@tonic-gate * or __lwp_rwlock_wrlock() holding the mutex. These return with the mutex 4340Sstevel@tonic-gate * released, and if they need to sleep will release the mutex first. In the 4350Sstevel@tonic-gate * event of a spurious wakeup, these will return EAGAIN (because it is much 4360Sstevel@tonic-gate * easier for us to re-acquire the mutex here). 4370Sstevel@tonic-gate */ 4380Sstevel@tonic-gate int 4390Sstevel@tonic-gate shared_rwlock_lock(rwlock_t *rwlp, timespec_t *tsp, int rd_wr) 4400Sstevel@tonic-gate { 4414570Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 4424570Sraf mutex_t *mp = &rwlp->mutex; 4434570Sraf /* LINTED set but not used */ 4444570Sraf uint32_t readers; 4454570Sraf int try_flag; 4464570Sraf int error; 4474570Sraf 4484570Sraf try_flag = (rd_wr & TRY_FLAG); 4494570Sraf rd_wr &= ~TRY_FLAG; 4504570Sraf ASSERT(rd_wr == READ_LOCK || rd_wr == WRITE_LOCK); 4514570Sraf 4524570Sraf if (!try_flag) { 4534570Sraf DTRACE_PROBE2(plockstat, rw__block, rwlp, rd_wr); 4544570Sraf } 4554570Sraf 4564570Sraf do { 4574570Sraf if (try_flag && (*rwstate & URW_WRITE_LOCKED)) { 4584570Sraf error = EBUSY; 4594570Sraf break; 4604570Sraf } 4614570Sraf if ((error = _private_mutex_lock(mp)) != 0) 4624570Sraf break; 4634570Sraf if (rd_wr == READ_LOCK) { 4644570Sraf if (read_lock_try(rwlp, 0)) { 4654570Sraf (void) _private_mutex_unlock(mp); 4664570Sraf break; 4674570Sraf } 4684570Sraf } else { 4694570Sraf if (write_lock_try(rwlp, 0)) { 4704570Sraf (void) _private_mutex_unlock(mp); 4714570Sraf break; 4724570Sraf } 4734570Sraf } 4744570Sraf atomic_or_32(rwstate, URW_HAS_WAITERS); 4754570Sraf readers = *rwstate; 4764570Sraf ASSERT_CONSISTENT_STATE(readers); 4774570Sraf /* 4784570Sraf * The calls to __lwp_rwlock_*() below will release the mutex, 4794570Sraf * so we need a dtrace probe here. 4804570Sraf */ 4814570Sraf mp->mutex_owner = 0; 4824570Sraf DTRACE_PROBE2(plockstat, mutex__release, mp, 0); 4834570Sraf /* 4844570Sraf * The waiters bit may be inaccurate. 4854570Sraf * Only the kernel knows for sure. 4864570Sraf */ 4874570Sraf if (rd_wr == READ_LOCK) { 4884570Sraf if (try_flag) 4894570Sraf error = __lwp_rwlock_tryrdlock(rwlp); 4904570Sraf else 4914570Sraf error = __lwp_rwlock_rdlock(rwlp, tsp); 4924570Sraf } else { 4934570Sraf if (try_flag) 4944570Sraf error = __lwp_rwlock_trywrlock(rwlp); 4954570Sraf else 4964570Sraf error = __lwp_rwlock_wrlock(rwlp, tsp); 4974570Sraf } 4984570Sraf } while (error == EAGAIN || error == EINTR); 4994570Sraf 5004570Sraf if (!try_flag) { 5014570Sraf DTRACE_PROBE3(plockstat, rw__blocked, rwlp, rd_wr, error == 0); 5024570Sraf } 5034570Sraf 5044570Sraf return (error); 5054570Sraf } 5064570Sraf 5074570Sraf /* 5084570Sraf * Common code for rdlock, timedrdlock, wrlock, timedwrlock, tryrdlock, 5094570Sraf * and trywrlock for process-private (USYNC_THREAD) rwlocks. 5104570Sraf */ 5114570Sraf int 5124570Sraf rwlock_lock(rwlock_t *rwlp, timespec_t *tsp, int rd_wr) 5134570Sraf { 5144570Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 5154570Sraf uint32_t readers; 5160Sstevel@tonic-gate ulwp_t *self = curthread; 5174570Sraf queue_head_t *qp; 5184570Sraf ulwp_t *ulwp; 5190Sstevel@tonic-gate int try_flag; 5200Sstevel@tonic-gate int error = 0; 5210Sstevel@tonic-gate 5220Sstevel@tonic-gate try_flag = (rd_wr & TRY_FLAG); 5230Sstevel@tonic-gate rd_wr &= ~TRY_FLAG; 5240Sstevel@tonic-gate ASSERT(rd_wr == READ_LOCK || rd_wr == WRITE_LOCK); 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate if (!try_flag) { 5270Sstevel@tonic-gate DTRACE_PROBE2(plockstat, rw__block, rwlp, rd_wr); 5280Sstevel@tonic-gate } 5290Sstevel@tonic-gate 5304570Sraf qp = queue_lock(rwlp, MX); 5314570Sraf retry: 5324570Sraf while (error == 0) { 5330Sstevel@tonic-gate if (rd_wr == READ_LOCK) { 5344570Sraf if (read_lock_try(rwlp, 0)) 5354570Sraf goto out; 5360Sstevel@tonic-gate } else { 5374570Sraf if (write_lock_try(rwlp, 0)) 5384570Sraf goto out; 5390Sstevel@tonic-gate } 5404570Sraf atomic_or_32(rwstate, URW_HAS_WAITERS); 5414570Sraf readers = *rwstate; 5424570Sraf ASSERT_CONSISTENT_STATE(readers); 5434570Sraf if ((readers & URW_WRITE_LOCKED) || 5444570Sraf (rd_wr == WRITE_LOCK && 5454570Sraf (readers & URW_READERS_MASK) != 0)) 5460Sstevel@tonic-gate /* EMPTY */; /* somebody holds the lock */ 5470Sstevel@tonic-gate else if ((ulwp = queue_waiter(qp, rwlp)) == NULL) { 5484570Sraf atomic_and_32(rwstate, ~URW_HAS_WAITERS); 5490Sstevel@tonic-gate break; /* no queued waiters */ 5500Sstevel@tonic-gate } else { 5510Sstevel@tonic-gate int our_pri = real_priority(self); 5520Sstevel@tonic-gate int his_pri = real_priority(ulwp); 5530Sstevel@tonic-gate 5540Sstevel@tonic-gate if (rd_wr == WRITE_LOCK) { 5550Sstevel@tonic-gate /* 5560Sstevel@tonic-gate * We defer to a queued thread that has 5570Sstevel@tonic-gate * a higher priority than ours. 5580Sstevel@tonic-gate */ 5590Sstevel@tonic-gate if (his_pri <= our_pri) 5600Sstevel@tonic-gate break; 5610Sstevel@tonic-gate } else { 5620Sstevel@tonic-gate /* 5630Sstevel@tonic-gate * We defer to a queued thread that has 5640Sstevel@tonic-gate * a higher priority than ours or that 5650Sstevel@tonic-gate * is a writer whose priority equals ours. 5660Sstevel@tonic-gate */ 5670Sstevel@tonic-gate if (his_pri < our_pri || 5680Sstevel@tonic-gate (his_pri == our_pri && !ulwp->ul_writer)) 5690Sstevel@tonic-gate break; 5700Sstevel@tonic-gate } 5710Sstevel@tonic-gate } 5720Sstevel@tonic-gate /* 5730Sstevel@tonic-gate * We are about to block. 5740Sstevel@tonic-gate * If we're doing a trylock, return EBUSY instead. 5750Sstevel@tonic-gate */ 5760Sstevel@tonic-gate if (try_flag) { 5770Sstevel@tonic-gate error = EBUSY; 5780Sstevel@tonic-gate break; 5790Sstevel@tonic-gate } 5800Sstevel@tonic-gate /* 5810Sstevel@tonic-gate * Enqueue writers ahead of readers of the 5820Sstevel@tonic-gate * same priority. 5830Sstevel@tonic-gate */ 5840Sstevel@tonic-gate self->ul_writer = rd_wr; /* *must* be 0 or 1 */ 5850Sstevel@tonic-gate enqueue(qp, self, rwlp, MX); 5860Sstevel@tonic-gate set_parking_flag(self, 1); 5870Sstevel@tonic-gate queue_unlock(qp); 5880Sstevel@tonic-gate if ((error = __lwp_park(tsp, 0)) == EINTR) 5890Sstevel@tonic-gate error = 0; 5900Sstevel@tonic-gate self->ul_writer = 0; 5910Sstevel@tonic-gate set_parking_flag(self, 0); 5920Sstevel@tonic-gate qp = queue_lock(rwlp, MX); 5934570Sraf if (self->ul_sleepq && dequeue_self(qp, rwlp) == 0) 5944570Sraf atomic_and_32(rwstate, ~URW_HAS_WAITERS); 5950Sstevel@tonic-gate } 5960Sstevel@tonic-gate 5970Sstevel@tonic-gate if (error == 0) { 5984570Sraf if (rd_wr == READ_LOCK) { 5994570Sraf if (!read_lock_try(rwlp, 1)) 6004570Sraf goto retry; 6014570Sraf } else { 6024570Sraf if (!write_lock_try(rwlp, 1)) 6034570Sraf goto retry; 6040Sstevel@tonic-gate } 6050Sstevel@tonic-gate } 6060Sstevel@tonic-gate 6074570Sraf out: 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 */ 6470Sstevel@tonic-gate if (rw_write_is_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 6770Sstevel@tonic-gate #pragma weak rw_rdlock = __rw_rdlock 6780Sstevel@tonic-gate #pragma weak _rw_rdlock = __rw_rdlock 6790Sstevel@tonic-gate #pragma weak pthread_rwlock_rdlock = __rw_rdlock 6800Sstevel@tonic-gate #pragma weak _pthread_rwlock_rdlock = __rw_rdlock 6810Sstevel@tonic-gate int 6820Sstevel@tonic-gate __rw_rdlock(rwlock_t *rwlp) 6830Sstevel@tonic-gate { 6840Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 6850Sstevel@tonic-gate return (rw_rdlock_impl(rwlp, NULL)); 6860Sstevel@tonic-gate } 6870Sstevel@tonic-gate 6880Sstevel@tonic-gate void 6890Sstevel@tonic-gate lrw_rdlock(rwlock_t *rwlp) 6900Sstevel@tonic-gate { 6910Sstevel@tonic-gate enter_critical(curthread); 6920Sstevel@tonic-gate (void) rw_rdlock_impl(rwlp, NULL); 6930Sstevel@tonic-gate } 6940Sstevel@tonic-gate 6950Sstevel@tonic-gate #pragma weak pthread_rwlock_reltimedrdlock_np = \ 6960Sstevel@tonic-gate _pthread_rwlock_reltimedrdlock_np 6970Sstevel@tonic-gate int 6980Sstevel@tonic-gate _pthread_rwlock_reltimedrdlock_np(rwlock_t *rwlp, const timespec_t *reltime) 6990Sstevel@tonic-gate { 7000Sstevel@tonic-gate timespec_t tslocal = *reltime; 7010Sstevel@tonic-gate int error; 7020Sstevel@tonic-gate 7030Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 7040Sstevel@tonic-gate error = rw_rdlock_impl(rwlp, &tslocal); 7050Sstevel@tonic-gate if (error == ETIME) 7060Sstevel@tonic-gate error = ETIMEDOUT; 7070Sstevel@tonic-gate return (error); 7080Sstevel@tonic-gate } 7090Sstevel@tonic-gate 7100Sstevel@tonic-gate #pragma weak pthread_rwlock_timedrdlock = _pthread_rwlock_timedrdlock 7110Sstevel@tonic-gate int 7120Sstevel@tonic-gate _pthread_rwlock_timedrdlock(rwlock_t *rwlp, const timespec_t *abstime) 7130Sstevel@tonic-gate { 7140Sstevel@tonic-gate timespec_t tslocal; 7150Sstevel@tonic-gate int error; 7160Sstevel@tonic-gate 7170Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 7180Sstevel@tonic-gate abstime_to_reltime(CLOCK_REALTIME, abstime, &tslocal); 7190Sstevel@tonic-gate error = rw_rdlock_impl(rwlp, &tslocal); 7200Sstevel@tonic-gate if (error == ETIME) 7210Sstevel@tonic-gate error = ETIMEDOUT; 7220Sstevel@tonic-gate return (error); 7230Sstevel@tonic-gate } 7240Sstevel@tonic-gate 7250Sstevel@tonic-gate int 7260Sstevel@tonic-gate rw_wrlock_impl(rwlock_t *rwlp, timespec_t *tsp) 7270Sstevel@tonic-gate { 7280Sstevel@tonic-gate ulwp_t *self = curthread; 7290Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 7300Sstevel@tonic-gate tdb_rwlock_stats_t *rwsp = RWLOCK_STATS(rwlp, udp); 7310Sstevel@tonic-gate int error; 7320Sstevel@tonic-gate 7330Sstevel@tonic-gate /* 7340Sstevel@tonic-gate * If we hold a readers lock on this rwlock, bail out. 7350Sstevel@tonic-gate */ 7360Sstevel@tonic-gate if (rw_read_is_held(rwlp)) { 7370Sstevel@tonic-gate if (self->ul_error_detection) 7380Sstevel@tonic-gate rwlock_error(rwlp, "rwlock_wrlock", 7390Sstevel@tonic-gate "calling thread owns the readers lock"); 7404570Sraf error = EDEADLK; 7414570Sraf goto out; 7420Sstevel@tonic-gate } 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate /* 7450Sstevel@tonic-gate * If we hold the writer lock, bail out. 7460Sstevel@tonic-gate */ 7470Sstevel@tonic-gate if (rw_write_is_held(rwlp)) { 7480Sstevel@tonic-gate if (self->ul_error_detection) 7490Sstevel@tonic-gate rwlock_error(rwlp, "rwlock_wrlock", 7500Sstevel@tonic-gate "calling thread owns the writer lock"); 7514570Sraf error = EDEADLK; 7524570Sraf goto out; 7530Sstevel@tonic-gate } 7540Sstevel@tonic-gate 7554570Sraf if (write_lock_try(rwlp, 0)) 7564570Sraf error = 0; 7574570Sraf else if (rwlp->rwlock_type == USYNC_PROCESS) /* kernel-level */ 7580Sstevel@tonic-gate error = shared_rwlock_lock(rwlp, tsp, WRITE_LOCK); 7594570Sraf else /* user-level */ 7600Sstevel@tonic-gate error = rwlock_lock(rwlp, tsp, WRITE_LOCK); 7610Sstevel@tonic-gate 7624570Sraf out: 7634570Sraf if (error == 0) { 7644570Sraf rwlp->rwlock_owner = (uintptr_t)self; 7654570Sraf if (rwlp->rwlock_type == USYNC_PROCESS) 7664570Sraf rwlp->rwlock_ownerpid = udp->pid; 7674570Sraf if (rwsp) { 7684570Sraf tdb_incr(rwsp->rw_wrlock); 7694570Sraf rwsp->rw_wrlock_begin_hold = gethrtime(); 7704570Sraf } 7714570Sraf DTRACE_PROBE2(plockstat, rw__acquire, rwlp, WRITE_LOCK); 7724570Sraf } else { 7734570Sraf DTRACE_PROBE3(plockstat, rw__error, rwlp, WRITE_LOCK, error); 7740Sstevel@tonic-gate } 7750Sstevel@tonic-gate return (error); 7760Sstevel@tonic-gate } 7770Sstevel@tonic-gate 7780Sstevel@tonic-gate #pragma weak rw_wrlock = __rw_wrlock 7790Sstevel@tonic-gate #pragma weak _rw_wrlock = __rw_wrlock 7800Sstevel@tonic-gate #pragma weak pthread_rwlock_wrlock = __rw_wrlock 7810Sstevel@tonic-gate #pragma weak _pthread_rwlock_wrlock = __rw_wrlock 7820Sstevel@tonic-gate int 7830Sstevel@tonic-gate __rw_wrlock(rwlock_t *rwlp) 7840Sstevel@tonic-gate { 7850Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 7860Sstevel@tonic-gate return (rw_wrlock_impl(rwlp, NULL)); 7870Sstevel@tonic-gate } 7880Sstevel@tonic-gate 7890Sstevel@tonic-gate void 7900Sstevel@tonic-gate lrw_wrlock(rwlock_t *rwlp) 7910Sstevel@tonic-gate { 7920Sstevel@tonic-gate enter_critical(curthread); 7930Sstevel@tonic-gate (void) rw_wrlock_impl(rwlp, NULL); 7940Sstevel@tonic-gate } 7950Sstevel@tonic-gate 7960Sstevel@tonic-gate #pragma weak pthread_rwlock_reltimedwrlock_np = \ 7970Sstevel@tonic-gate _pthread_rwlock_reltimedwrlock_np 7980Sstevel@tonic-gate int 7990Sstevel@tonic-gate _pthread_rwlock_reltimedwrlock_np(rwlock_t *rwlp, const timespec_t *reltime) 8000Sstevel@tonic-gate { 8010Sstevel@tonic-gate timespec_t tslocal = *reltime; 8020Sstevel@tonic-gate int error; 8030Sstevel@tonic-gate 8040Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 8050Sstevel@tonic-gate error = rw_wrlock_impl(rwlp, &tslocal); 8060Sstevel@tonic-gate if (error == ETIME) 8070Sstevel@tonic-gate error = ETIMEDOUT; 8080Sstevel@tonic-gate return (error); 8090Sstevel@tonic-gate } 8100Sstevel@tonic-gate 8110Sstevel@tonic-gate #pragma weak pthread_rwlock_timedwrlock = _pthread_rwlock_timedwrlock 8120Sstevel@tonic-gate int 8130Sstevel@tonic-gate _pthread_rwlock_timedwrlock(rwlock_t *rwlp, const timespec_t *abstime) 8140Sstevel@tonic-gate { 8150Sstevel@tonic-gate timespec_t tslocal; 8160Sstevel@tonic-gate int error; 8170Sstevel@tonic-gate 8180Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 8190Sstevel@tonic-gate abstime_to_reltime(CLOCK_REALTIME, abstime, &tslocal); 8200Sstevel@tonic-gate error = rw_wrlock_impl(rwlp, &tslocal); 8210Sstevel@tonic-gate if (error == ETIME) 8220Sstevel@tonic-gate error = ETIMEDOUT; 8230Sstevel@tonic-gate return (error); 8240Sstevel@tonic-gate } 8250Sstevel@tonic-gate 8260Sstevel@tonic-gate #pragma weak rw_tryrdlock = __rw_tryrdlock 8270Sstevel@tonic-gate #pragma weak _rw_tryrdlock = __rw_tryrdlock 8280Sstevel@tonic-gate #pragma weak pthread_rwlock_tryrdlock = __rw_tryrdlock 8290Sstevel@tonic-gate #pragma weak _pthread_rwlock_tryrdlock = __rw_tryrdlock 8300Sstevel@tonic-gate int 8310Sstevel@tonic-gate __rw_tryrdlock(rwlock_t *rwlp) 8320Sstevel@tonic-gate { 8330Sstevel@tonic-gate ulwp_t *self = curthread; 8340Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 8350Sstevel@tonic-gate tdb_rwlock_stats_t *rwsp = RWLOCK_STATS(rwlp, udp); 8360Sstevel@tonic-gate readlock_t *readlockp; 8370Sstevel@tonic-gate int error; 8380Sstevel@tonic-gate 8390Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 8400Sstevel@tonic-gate 8410Sstevel@tonic-gate if (rwsp) 8420Sstevel@tonic-gate tdb_incr(rwsp->rw_rdlock_try); 8430Sstevel@tonic-gate 8440Sstevel@tonic-gate /* 8450Sstevel@tonic-gate * If we already hold a readers lock on this rwlock, 8460Sstevel@tonic-gate * just increment our reference count and return. 8470Sstevel@tonic-gate */ 8484570Sraf sigoff(self); 8490Sstevel@tonic-gate readlockp = rwl_entry(rwlp); 8500Sstevel@tonic-gate if (readlockp->rd_count != 0) { 8514570Sraf if (readlockp->rd_count == READ_LOCK_MAX) { 8524570Sraf sigon(self); 8534570Sraf error = EAGAIN; 8544570Sraf goto out; 8554570Sraf } 8564570Sraf sigon(self); 8574570Sraf error = 0; 8584570Sraf goto out; 8590Sstevel@tonic-gate } 8604570Sraf sigon(self); 8610Sstevel@tonic-gate 8624570Sraf if (read_lock_try(rwlp, 0)) 8634570Sraf error = 0; 8644570Sraf else if (rwlp->rwlock_type == USYNC_PROCESS) /* kernel-level */ 8650Sstevel@tonic-gate error = shared_rwlock_lock(rwlp, NULL, READ_LOCK_TRY); 8660Sstevel@tonic-gate else /* user-level */ 8670Sstevel@tonic-gate error = rwlock_lock(rwlp, NULL, READ_LOCK_TRY); 8680Sstevel@tonic-gate 8694570Sraf out: 8704570Sraf if (error == 0) { 8714570Sraf sigoff(self); 8724570Sraf rwl_entry(rwlp)->rd_count++; 8734570Sraf sigon(self); 8744570Sraf DTRACE_PROBE2(plockstat, rw__acquire, rwlp, READ_LOCK); 8754570Sraf } else { 8764570Sraf if (rwsp) 8774570Sraf tdb_incr(rwsp->rw_rdlock_try_fail); 8784570Sraf if (error != EBUSY) { 8794570Sraf DTRACE_PROBE3(plockstat, rw__error, rwlp, READ_LOCK, 8804570Sraf error); 8814570Sraf } 8824570Sraf } 8830Sstevel@tonic-gate 8840Sstevel@tonic-gate return (error); 8850Sstevel@tonic-gate } 8860Sstevel@tonic-gate 8870Sstevel@tonic-gate #pragma weak rw_trywrlock = __rw_trywrlock 8880Sstevel@tonic-gate #pragma weak _rw_trywrlock = __rw_trywrlock 8890Sstevel@tonic-gate #pragma weak pthread_rwlock_trywrlock = __rw_trywrlock 8900Sstevel@tonic-gate #pragma weak _pthread_rwlock_trywrlock = __rw_trywrlock 8910Sstevel@tonic-gate int 8920Sstevel@tonic-gate __rw_trywrlock(rwlock_t *rwlp) 8930Sstevel@tonic-gate { 8940Sstevel@tonic-gate ulwp_t *self = curthread; 8950Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 8960Sstevel@tonic-gate tdb_rwlock_stats_t *rwsp = RWLOCK_STATS(rwlp, udp); 8970Sstevel@tonic-gate int error; 8980Sstevel@tonic-gate 8994570Sraf ASSERT(!self->ul_critical || self->ul_bindflags); 9000Sstevel@tonic-gate 9010Sstevel@tonic-gate if (rwsp) 9020Sstevel@tonic-gate tdb_incr(rwsp->rw_wrlock_try); 9030Sstevel@tonic-gate 9044570Sraf if (write_lock_try(rwlp, 0)) 9054570Sraf error = 0; 9064570Sraf else if (rwlp->rwlock_type == USYNC_PROCESS) /* kernel-level */ 9070Sstevel@tonic-gate error = shared_rwlock_lock(rwlp, NULL, WRITE_LOCK_TRY); 9084570Sraf else /* user-level */ 9090Sstevel@tonic-gate error = rwlock_lock(rwlp, NULL, WRITE_LOCK_TRY); 9104570Sraf 9114570Sraf if (error == 0) { 9124570Sraf rwlp->rwlock_owner = (uintptr_t)self; 9134570Sraf if (rwlp->rwlock_type == USYNC_PROCESS) 9144570Sraf rwlp->rwlock_ownerpid = udp->pid; 9154570Sraf if (rwsp) 9164570Sraf rwsp->rw_wrlock_begin_hold = gethrtime(); 9174570Sraf DTRACE_PROBE2(plockstat, rw__acquire, rwlp, WRITE_LOCK); 9184570Sraf } else { 9194570Sraf if (rwsp) 9200Sstevel@tonic-gate tdb_incr(rwsp->rw_wrlock_try_fail); 9214570Sraf if (error != EBUSY) { 9224570Sraf DTRACE_PROBE3(plockstat, rw__error, rwlp, WRITE_LOCK, 9234570Sraf error); 9244570Sraf } 9250Sstevel@tonic-gate } 9260Sstevel@tonic-gate return (error); 9270Sstevel@tonic-gate } 9280Sstevel@tonic-gate 9290Sstevel@tonic-gate #pragma weak rw_unlock = __rw_unlock 9300Sstevel@tonic-gate #pragma weak _rw_unlock = __rw_unlock 9310Sstevel@tonic-gate #pragma weak pthread_rwlock_unlock = __rw_unlock 9320Sstevel@tonic-gate #pragma weak _pthread_rwlock_unlock = __rw_unlock 9330Sstevel@tonic-gate int 9340Sstevel@tonic-gate __rw_unlock(rwlock_t *rwlp) 9350Sstevel@tonic-gate { 9364570Sraf volatile uint32_t *rwstate = (volatile uint32_t *)&rwlp->rwlock_readers; 9374570Sraf uint32_t readers; 9380Sstevel@tonic-gate ulwp_t *self = curthread; 9390Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 9400Sstevel@tonic-gate tdb_rwlock_stats_t *rwsp; 9414570Sraf queue_head_t *qp; 9424570Sraf int rd_wr; 9434570Sraf int waked = 0; 9440Sstevel@tonic-gate 9454570Sraf readers = *rwstate; 9464570Sraf ASSERT_CONSISTENT_STATE(readers); 9474570Sraf if (readers & URW_WRITE_LOCKED) { 9484570Sraf rd_wr = WRITE_LOCK; 9494570Sraf readers = 0; 9504570Sraf } else { 9514570Sraf rd_wr = READ_LOCK; 9524570Sraf readers &= URW_READERS_MASK; 9530Sstevel@tonic-gate } 9540Sstevel@tonic-gate 9554570Sraf if (rd_wr == WRITE_LOCK) { 9560Sstevel@tonic-gate /* 9570Sstevel@tonic-gate * Since the writer lock is held, we'd better be 9580Sstevel@tonic-gate * holding it, else we cannot legitimately be here. 9590Sstevel@tonic-gate */ 9600Sstevel@tonic-gate if (!rw_write_is_held(rwlp)) { 9610Sstevel@tonic-gate if (self->ul_error_detection) 9620Sstevel@tonic-gate rwlock_error(rwlp, "rwlock_unlock", 9630Sstevel@tonic-gate "writer lock held, " 9640Sstevel@tonic-gate "but not by the calling thread"); 9650Sstevel@tonic-gate return (EPERM); 9660Sstevel@tonic-gate } 9670Sstevel@tonic-gate if ((rwsp = RWLOCK_STATS(rwlp, udp)) != NULL) { 9680Sstevel@tonic-gate if (rwsp->rw_wrlock_begin_hold) 9690Sstevel@tonic-gate rwsp->rw_wrlock_hold_time += 9700Sstevel@tonic-gate gethrtime() - rwsp->rw_wrlock_begin_hold; 9710Sstevel@tonic-gate rwsp->rw_wrlock_begin_hold = 0; 9720Sstevel@tonic-gate } 9734570Sraf rwlp->rwlock_owner = 0; 9744570Sraf rwlp->rwlock_ownerpid = 0; 9754570Sraf } else if (readers > 0) { 9760Sstevel@tonic-gate /* 9770Sstevel@tonic-gate * A readers lock is held; if we don't hold one, bail out. 9780Sstevel@tonic-gate */ 9794570Sraf readlock_t *readlockp; 9804570Sraf 9814570Sraf sigoff(self); 9824570Sraf readlockp = rwl_entry(rwlp); 9830Sstevel@tonic-gate if (readlockp->rd_count == 0) { 9844570Sraf sigon(self); 9850Sstevel@tonic-gate if (self->ul_error_detection) 9860Sstevel@tonic-gate rwlock_error(rwlp, "rwlock_unlock", 9870Sstevel@tonic-gate "readers lock held, " 9880Sstevel@tonic-gate "but not by the calling thread"); 9890Sstevel@tonic-gate return (EPERM); 9900Sstevel@tonic-gate } 9910Sstevel@tonic-gate /* 9920Sstevel@tonic-gate * If we hold more than one readers lock on this rwlock, 9930Sstevel@tonic-gate * just decrement our reference count and return. 9940Sstevel@tonic-gate */ 9950Sstevel@tonic-gate if (--readlockp->rd_count != 0) { 9964570Sraf sigon(self); 9974570Sraf goto out; 9980Sstevel@tonic-gate } 9994570Sraf sigon(self); 10000Sstevel@tonic-gate } else { 10010Sstevel@tonic-gate /* 10020Sstevel@tonic-gate * This is a usage error. 10030Sstevel@tonic-gate * No thread should release an unowned lock. 10040Sstevel@tonic-gate */ 10050Sstevel@tonic-gate if (self->ul_error_detection) 10060Sstevel@tonic-gate rwlock_error(rwlp, "rwlock_unlock", "lock not owned"); 10070Sstevel@tonic-gate return (EPERM); 10080Sstevel@tonic-gate } 10090Sstevel@tonic-gate 10104570Sraf if (rd_wr == WRITE_LOCK && write_unlock_try(rwlp)) { 10114570Sraf /* EMPTY */; 10124570Sraf } else if (rd_wr == READ_LOCK && read_unlock_try(rwlp)) { 10134570Sraf /* EMPTY */; 10144570Sraf } else if (rwlp->rwlock_type == USYNC_PROCESS) { 10154570Sraf (void) _private_mutex_lock(&rwlp->mutex); 10164570Sraf (void) __lwp_rwlock_unlock(rwlp); 10174570Sraf (void) _private_mutex_unlock(&rwlp->mutex); 10184570Sraf waked = 1; 10194570Sraf } else { 10200Sstevel@tonic-gate qp = queue_lock(rwlp, MX); 10214570Sraf if (rd_wr == READ_LOCK) 10224570Sraf atomic_dec_32(rwstate); 10234570Sraf else 10244570Sraf atomic_and_32(rwstate, ~URW_WRITE_LOCKED); 10250Sstevel@tonic-gate waked = rw_queue_release(qp, rwlp); 10260Sstevel@tonic-gate } 10270Sstevel@tonic-gate 10284570Sraf out: 10294570Sraf DTRACE_PROBE2(plockstat, rw__release, rwlp, rd_wr); 10304570Sraf 10310Sstevel@tonic-gate /* 10320Sstevel@tonic-gate * Yield to the thread we just waked up, just in case we might 10330Sstevel@tonic-gate * be about to grab the rwlock again immediately upon return. 10340Sstevel@tonic-gate * This is pretty weak but it helps on a uniprocessor and also 10350Sstevel@tonic-gate * when cpu affinity has assigned both ourself and the other 10360Sstevel@tonic-gate * thread to the same CPU. Note that lwp_yield() will yield 10370Sstevel@tonic-gate * the processor only if the writer is at the same or higher 10380Sstevel@tonic-gate * priority than ourself. This provides more balanced program 10390Sstevel@tonic-gate * behavior; it doesn't guarantee acquisition of the lock by 10400Sstevel@tonic-gate * the pending writer. 10410Sstevel@tonic-gate */ 10420Sstevel@tonic-gate if (waked) 10430Sstevel@tonic-gate lwp_yield(); 10440Sstevel@tonic-gate return (0); 10450Sstevel@tonic-gate } 10460Sstevel@tonic-gate 10470Sstevel@tonic-gate void 10480Sstevel@tonic-gate lrw_unlock(rwlock_t *rwlp) 10490Sstevel@tonic-gate { 10500Sstevel@tonic-gate (void) __rw_unlock(rwlp); 10510Sstevel@tonic-gate exit_critical(curthread); 10520Sstevel@tonic-gate } 1053