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 5*1893Sraf * Common Development and Distribution License (the "License"). 6*1893Sraf * 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 */ 211219Sraf 220Sstevel@tonic-gate /* 231219Sraf * Copyright 2006 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 <sys/sdt.h> 300Sstevel@tonic-gate 310Sstevel@tonic-gate #include "lint.h" 320Sstevel@tonic-gate #include "thr_uberdata.h" 330Sstevel@tonic-gate 340Sstevel@tonic-gate /* 350Sstevel@tonic-gate * This mutex is initialized to be held by lwp#1. 360Sstevel@tonic-gate * It is used to block a thread that has returned from a mutex_lock() 370Sstevel@tonic-gate * of a PTHREAD_PRIO_INHERIT mutex with an unrecoverable error. 380Sstevel@tonic-gate */ 390Sstevel@tonic-gate mutex_t stall_mutex = DEFAULTMUTEX; 400Sstevel@tonic-gate 410Sstevel@tonic-gate static int shared_mutex_held(mutex_t *); 420Sstevel@tonic-gate 430Sstevel@tonic-gate /* 440Sstevel@tonic-gate * Lock statistics support functions. 450Sstevel@tonic-gate */ 460Sstevel@tonic-gate void 470Sstevel@tonic-gate record_begin_hold(tdb_mutex_stats_t *msp) 480Sstevel@tonic-gate { 490Sstevel@tonic-gate tdb_incr(msp->mutex_lock); 500Sstevel@tonic-gate msp->mutex_begin_hold = gethrtime(); 510Sstevel@tonic-gate } 520Sstevel@tonic-gate 530Sstevel@tonic-gate hrtime_t 540Sstevel@tonic-gate record_hold_time(tdb_mutex_stats_t *msp) 550Sstevel@tonic-gate { 560Sstevel@tonic-gate hrtime_t now = gethrtime(); 570Sstevel@tonic-gate 580Sstevel@tonic-gate if (msp->mutex_begin_hold) 590Sstevel@tonic-gate msp->mutex_hold_time += now - msp->mutex_begin_hold; 600Sstevel@tonic-gate msp->mutex_begin_hold = 0; 610Sstevel@tonic-gate return (now); 620Sstevel@tonic-gate } 630Sstevel@tonic-gate 640Sstevel@tonic-gate /* 650Sstevel@tonic-gate * Called once at library initialization. 660Sstevel@tonic-gate */ 670Sstevel@tonic-gate void 680Sstevel@tonic-gate mutex_setup(void) 690Sstevel@tonic-gate { 700Sstevel@tonic-gate if (set_lock_byte(&stall_mutex.mutex_lockw)) 710Sstevel@tonic-gate thr_panic("mutex_setup() cannot acquire stall_mutex"); 720Sstevel@tonic-gate stall_mutex.mutex_owner = (uintptr_t)curthread; 730Sstevel@tonic-gate } 740Sstevel@tonic-gate 750Sstevel@tonic-gate /* 760Sstevel@tonic-gate * The default spin counts of 1000 and 500 are experimentally determined. 770Sstevel@tonic-gate * On sun4u machines with any number of processors they could be raised 780Sstevel@tonic-gate * to 10,000 but that (experimentally) makes almost no difference. 790Sstevel@tonic-gate * The environment variables: 800Sstevel@tonic-gate * _THREAD_ADAPTIVE_SPIN=count 810Sstevel@tonic-gate * _THREAD_RELEASE_SPIN=count 820Sstevel@tonic-gate * can be used to override and set the counts in the range [0 .. 1,000,000]. 830Sstevel@tonic-gate */ 840Sstevel@tonic-gate int thread_adaptive_spin = 1000; 850Sstevel@tonic-gate uint_t thread_max_spinners = 100; 860Sstevel@tonic-gate int thread_release_spin = 500; 870Sstevel@tonic-gate int thread_queue_verify = 0; 880Sstevel@tonic-gate static int ncpus; 890Sstevel@tonic-gate 900Sstevel@tonic-gate /* 910Sstevel@tonic-gate * Distinguish spinning for queue locks from spinning for regular locks. 920Sstevel@tonic-gate * The environment variable: 930Sstevel@tonic-gate * _THREAD_QUEUE_SPIN=count 940Sstevel@tonic-gate * can be used to override and set the count in the range [0 .. 1,000,000]. 950Sstevel@tonic-gate * There is no release spin concept for queue locks. 960Sstevel@tonic-gate */ 970Sstevel@tonic-gate int thread_queue_spin = 1000; 980Sstevel@tonic-gate 990Sstevel@tonic-gate /* 1000Sstevel@tonic-gate * Use the otherwise-unused 'mutex_ownerpid' field of a USYNC_THREAD 1010Sstevel@tonic-gate * mutex to be a count of adaptive spins in progress. 1020Sstevel@tonic-gate */ 1030Sstevel@tonic-gate #define mutex_spinners mutex_ownerpid 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate void 1060Sstevel@tonic-gate _mutex_set_typeattr(mutex_t *mp, int attr) 1070Sstevel@tonic-gate { 1080Sstevel@tonic-gate mp->mutex_type |= (uint8_t)attr; 1090Sstevel@tonic-gate } 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate /* 1120Sstevel@tonic-gate * 'type' can be one of USYNC_THREAD or USYNC_PROCESS, possibly 1130Sstevel@tonic-gate * augmented by the flags LOCK_RECURSIVE and/or LOCK_ERRORCHECK, 1140Sstevel@tonic-gate * or it can be USYNC_PROCESS_ROBUST with no extra flags. 1150Sstevel@tonic-gate */ 1160Sstevel@tonic-gate #pragma weak _private_mutex_init = __mutex_init 1170Sstevel@tonic-gate #pragma weak mutex_init = __mutex_init 1180Sstevel@tonic-gate #pragma weak _mutex_init = __mutex_init 1190Sstevel@tonic-gate /* ARGSUSED2 */ 1200Sstevel@tonic-gate int 1210Sstevel@tonic-gate __mutex_init(mutex_t *mp, int type, void *arg) 1220Sstevel@tonic-gate { 1230Sstevel@tonic-gate int error; 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate switch (type & ~(LOCK_RECURSIVE|LOCK_ERRORCHECK)) { 1260Sstevel@tonic-gate case USYNC_THREAD: 1270Sstevel@tonic-gate case USYNC_PROCESS: 1280Sstevel@tonic-gate (void) _memset(mp, 0, sizeof (*mp)); 1290Sstevel@tonic-gate mp->mutex_type = (uint8_t)type; 1300Sstevel@tonic-gate mp->mutex_flag = LOCK_INITED; 1310Sstevel@tonic-gate error = 0; 1320Sstevel@tonic-gate break; 1330Sstevel@tonic-gate case USYNC_PROCESS_ROBUST: 1340Sstevel@tonic-gate if (type & (LOCK_RECURSIVE|LOCK_ERRORCHECK)) 1350Sstevel@tonic-gate error = EINVAL; 1360Sstevel@tonic-gate else 1370Sstevel@tonic-gate error = ___lwp_mutex_init(mp, type); 1380Sstevel@tonic-gate break; 1390Sstevel@tonic-gate default: 1400Sstevel@tonic-gate error = EINVAL; 1410Sstevel@tonic-gate break; 1420Sstevel@tonic-gate } 1430Sstevel@tonic-gate if (error == 0) 1440Sstevel@tonic-gate mp->mutex_magic = MUTEX_MAGIC; 1450Sstevel@tonic-gate return (error); 1460Sstevel@tonic-gate } 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate /* 1490Sstevel@tonic-gate * Delete mp from list of ceil mutexes owned by curthread. 1500Sstevel@tonic-gate * Return 1 if the head of the chain was updated. 1510Sstevel@tonic-gate */ 1520Sstevel@tonic-gate int 1530Sstevel@tonic-gate _ceil_mylist_del(mutex_t *mp) 1540Sstevel@tonic-gate { 1550Sstevel@tonic-gate ulwp_t *self = curthread; 1560Sstevel@tonic-gate mxchain_t **mcpp; 1570Sstevel@tonic-gate mxchain_t *mcp; 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate mcpp = &self->ul_mxchain; 1600Sstevel@tonic-gate while ((*mcpp)->mxchain_mx != mp) 1610Sstevel@tonic-gate mcpp = &(*mcpp)->mxchain_next; 1620Sstevel@tonic-gate mcp = *mcpp; 1630Sstevel@tonic-gate *mcpp = mcp->mxchain_next; 1640Sstevel@tonic-gate lfree(mcp, sizeof (*mcp)); 1650Sstevel@tonic-gate return (mcpp == &self->ul_mxchain); 1660Sstevel@tonic-gate } 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate /* 1690Sstevel@tonic-gate * Add mp to head of list of ceil mutexes owned by curthread. 1700Sstevel@tonic-gate * Return ENOMEM if no memory could be allocated. 1710Sstevel@tonic-gate */ 1720Sstevel@tonic-gate int 1730Sstevel@tonic-gate _ceil_mylist_add(mutex_t *mp) 1740Sstevel@tonic-gate { 1750Sstevel@tonic-gate ulwp_t *self = curthread; 1760Sstevel@tonic-gate mxchain_t *mcp; 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate if ((mcp = lmalloc(sizeof (*mcp))) == NULL) 1790Sstevel@tonic-gate return (ENOMEM); 1800Sstevel@tonic-gate mcp->mxchain_mx = mp; 1810Sstevel@tonic-gate mcp->mxchain_next = self->ul_mxchain; 1820Sstevel@tonic-gate self->ul_mxchain = mcp; 1830Sstevel@tonic-gate return (0); 1840Sstevel@tonic-gate } 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate /* 1870Sstevel@tonic-gate * Inherit priority from ceiling. The inheritance impacts the effective 1880Sstevel@tonic-gate * priority, not the assigned priority. See _thread_setschedparam_main(). 1890Sstevel@tonic-gate */ 1900Sstevel@tonic-gate void 1910Sstevel@tonic-gate _ceil_prio_inherit(int ceil) 1920Sstevel@tonic-gate { 1930Sstevel@tonic-gate ulwp_t *self = curthread; 1940Sstevel@tonic-gate struct sched_param param; 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate (void) _memset(¶m, 0, sizeof (param)); 1970Sstevel@tonic-gate param.sched_priority = ceil; 1980Sstevel@tonic-gate if (_thread_setschedparam_main(self->ul_lwpid, 1990Sstevel@tonic-gate self->ul_policy, ¶m, PRIO_INHERIT)) { 2000Sstevel@tonic-gate /* 2010Sstevel@tonic-gate * Panic since unclear what error code to return. 2020Sstevel@tonic-gate * If we do return the error codes returned by above 2030Sstevel@tonic-gate * called routine, update the man page... 2040Sstevel@tonic-gate */ 2050Sstevel@tonic-gate thr_panic("_thread_setschedparam_main() fails"); 2060Sstevel@tonic-gate } 2070Sstevel@tonic-gate } 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate /* 2100Sstevel@tonic-gate * Waive inherited ceiling priority. Inherit from head of owned ceiling locks 2110Sstevel@tonic-gate * if holding at least one ceiling lock. If no ceiling locks are held at this 2120Sstevel@tonic-gate * point, disinherit completely, reverting back to assigned priority. 2130Sstevel@tonic-gate */ 2140Sstevel@tonic-gate void 2150Sstevel@tonic-gate _ceil_prio_waive(void) 2160Sstevel@tonic-gate { 2170Sstevel@tonic-gate ulwp_t *self = curthread; 2180Sstevel@tonic-gate struct sched_param param; 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate (void) _memset(¶m, 0, sizeof (param)); 2210Sstevel@tonic-gate if (self->ul_mxchain == NULL) { 2220Sstevel@tonic-gate /* 2230Sstevel@tonic-gate * No ceil locks held. Zero the epri, revert back to ul_pri. 2240Sstevel@tonic-gate * Since thread's hash lock is not held, one cannot just 2250Sstevel@tonic-gate * read ul_pri here...do it in the called routine... 2260Sstevel@tonic-gate */ 2270Sstevel@tonic-gate param.sched_priority = self->ul_pri; /* ignored */ 2280Sstevel@tonic-gate if (_thread_setschedparam_main(self->ul_lwpid, 2290Sstevel@tonic-gate self->ul_policy, ¶m, PRIO_DISINHERIT)) 2300Sstevel@tonic-gate thr_panic("_thread_setschedparam_main() fails"); 2310Sstevel@tonic-gate } else { 2320Sstevel@tonic-gate /* 2330Sstevel@tonic-gate * Set priority to that of the mutex at the head 2340Sstevel@tonic-gate * of the ceilmutex chain. 2350Sstevel@tonic-gate */ 2360Sstevel@tonic-gate param.sched_priority = 2370Sstevel@tonic-gate self->ul_mxchain->mxchain_mx->mutex_ceiling; 2380Sstevel@tonic-gate if (_thread_setschedparam_main(self->ul_lwpid, 2390Sstevel@tonic-gate self->ul_policy, ¶m, PRIO_INHERIT)) 2400Sstevel@tonic-gate thr_panic("_thread_setschedparam_main() fails"); 2410Sstevel@tonic-gate } 2420Sstevel@tonic-gate } 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate /* 2450Sstevel@tonic-gate * Non-preemptive spin locks. Used by queue_lock(). 2460Sstevel@tonic-gate * No lock statistics are gathered for these locks. 2470Sstevel@tonic-gate */ 2480Sstevel@tonic-gate void 2490Sstevel@tonic-gate spin_lock_set(mutex_t *mp) 2500Sstevel@tonic-gate { 2510Sstevel@tonic-gate ulwp_t *self = curthread; 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate no_preempt(self); 2540Sstevel@tonic-gate if (set_lock_byte(&mp->mutex_lockw) == 0) { 2550Sstevel@tonic-gate mp->mutex_owner = (uintptr_t)self; 2560Sstevel@tonic-gate return; 2570Sstevel@tonic-gate } 2580Sstevel@tonic-gate /* 2590Sstevel@tonic-gate * Spin for a while, attempting to acquire the lock. 2600Sstevel@tonic-gate */ 2610Sstevel@tonic-gate if (self->ul_spin_lock_spin != UINT_MAX) 2620Sstevel@tonic-gate self->ul_spin_lock_spin++; 2630Sstevel@tonic-gate if (mutex_queuelock_adaptive(mp) == 0 || 2640Sstevel@tonic-gate set_lock_byte(&mp->mutex_lockw) == 0) { 2650Sstevel@tonic-gate mp->mutex_owner = (uintptr_t)self; 2660Sstevel@tonic-gate return; 2670Sstevel@tonic-gate } 2680Sstevel@tonic-gate /* 2690Sstevel@tonic-gate * Try harder if we were previously at a no premption level. 2700Sstevel@tonic-gate */ 2710Sstevel@tonic-gate if (self->ul_preempt > 1) { 2720Sstevel@tonic-gate if (self->ul_spin_lock_spin2 != UINT_MAX) 2730Sstevel@tonic-gate self->ul_spin_lock_spin2++; 2740Sstevel@tonic-gate if (mutex_queuelock_adaptive(mp) == 0 || 2750Sstevel@tonic-gate set_lock_byte(&mp->mutex_lockw) == 0) { 2760Sstevel@tonic-gate mp->mutex_owner = (uintptr_t)self; 2770Sstevel@tonic-gate return; 2780Sstevel@tonic-gate } 2790Sstevel@tonic-gate } 2800Sstevel@tonic-gate /* 2810Sstevel@tonic-gate * Give up and block in the kernel for the mutex. 2820Sstevel@tonic-gate */ 2830Sstevel@tonic-gate if (self->ul_spin_lock_sleep != UINT_MAX) 2840Sstevel@tonic-gate self->ul_spin_lock_sleep++; 2850Sstevel@tonic-gate (void) ___lwp_mutex_timedlock(mp, NULL); 2860Sstevel@tonic-gate mp->mutex_owner = (uintptr_t)self; 2870Sstevel@tonic-gate } 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate void 2900Sstevel@tonic-gate spin_lock_clear(mutex_t *mp) 2910Sstevel@tonic-gate { 2920Sstevel@tonic-gate ulwp_t *self = curthread; 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate mp->mutex_owner = 0; 2950Sstevel@tonic-gate if (swap32(&mp->mutex_lockword, 0) & WAITERMASK) { 2960Sstevel@tonic-gate (void) ___lwp_mutex_wakeup(mp); 2970Sstevel@tonic-gate if (self->ul_spin_lock_wakeup != UINT_MAX) 2980Sstevel@tonic-gate self->ul_spin_lock_wakeup++; 2990Sstevel@tonic-gate } 3000Sstevel@tonic-gate preempt(self); 3010Sstevel@tonic-gate } 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate /* 3040Sstevel@tonic-gate * Allocate the sleep queue hash table. 3050Sstevel@tonic-gate */ 3060Sstevel@tonic-gate void 3070Sstevel@tonic-gate queue_alloc(void) 3080Sstevel@tonic-gate { 3090Sstevel@tonic-gate ulwp_t *self = curthread; 3100Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 3110Sstevel@tonic-gate void *data; 3120Sstevel@tonic-gate int i; 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate /* 3150Sstevel@tonic-gate * No locks are needed; we call here only when single-threaded. 3160Sstevel@tonic-gate */ 3170Sstevel@tonic-gate ASSERT(self == udp->ulwp_one); 3180Sstevel@tonic-gate ASSERT(!udp->uberflags.uf_mt); 3190Sstevel@tonic-gate if ((data = _private_mmap(NULL, 2 * QHASHSIZE * sizeof (queue_head_t), 3200Sstevel@tonic-gate PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, (off_t)0)) 3210Sstevel@tonic-gate == MAP_FAILED) 3220Sstevel@tonic-gate thr_panic("cannot allocate thread queue_head table"); 3230Sstevel@tonic-gate udp->queue_head = (queue_head_t *)data; 3240Sstevel@tonic-gate for (i = 0; i < 2 * QHASHSIZE; i++) 3250Sstevel@tonic-gate udp->queue_head[i].qh_lock.mutex_magic = MUTEX_MAGIC; 3260Sstevel@tonic-gate } 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate #if defined(THREAD_DEBUG) 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate /* 3310Sstevel@tonic-gate * Debugging: verify correctness of a sleep queue. 3320Sstevel@tonic-gate */ 3330Sstevel@tonic-gate void 3340Sstevel@tonic-gate QVERIFY(queue_head_t *qp) 3350Sstevel@tonic-gate { 3360Sstevel@tonic-gate ulwp_t *self = curthread; 3370Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 3380Sstevel@tonic-gate ulwp_t *ulwp; 3390Sstevel@tonic-gate ulwp_t *prev; 3400Sstevel@tonic-gate uint_t index; 3410Sstevel@tonic-gate uint32_t cnt = 0; 3420Sstevel@tonic-gate char qtype; 3430Sstevel@tonic-gate void *wchan; 3440Sstevel@tonic-gate 3450Sstevel@tonic-gate ASSERT(qp >= udp->queue_head && (qp - udp->queue_head) < 2 * QHASHSIZE); 3460Sstevel@tonic-gate ASSERT(MUTEX_OWNED(&qp->qh_lock, self)); 3470Sstevel@tonic-gate ASSERT((qp->qh_head != NULL && qp->qh_tail != NULL) || 3480Sstevel@tonic-gate (qp->qh_head == NULL && qp->qh_tail == NULL)); 3490Sstevel@tonic-gate if (!thread_queue_verify) 3500Sstevel@tonic-gate return; 3510Sstevel@tonic-gate /* real expensive stuff, only for _THREAD_QUEUE_VERIFY */ 3520Sstevel@tonic-gate qtype = ((qp - udp->queue_head) < QHASHSIZE)? MX : CV; 3530Sstevel@tonic-gate for (prev = NULL, ulwp = qp->qh_head; ulwp != NULL; 3540Sstevel@tonic-gate prev = ulwp, ulwp = ulwp->ul_link, cnt++) { 3550Sstevel@tonic-gate ASSERT(ulwp->ul_qtype == qtype); 3560Sstevel@tonic-gate ASSERT(ulwp->ul_wchan != NULL); 3570Sstevel@tonic-gate ASSERT(ulwp->ul_sleepq == qp); 3580Sstevel@tonic-gate wchan = ulwp->ul_wchan; 3590Sstevel@tonic-gate index = QUEUE_HASH(wchan, qtype); 3600Sstevel@tonic-gate ASSERT(&udp->queue_head[index] == qp); 3610Sstevel@tonic-gate } 3620Sstevel@tonic-gate ASSERT(qp->qh_tail == prev); 3630Sstevel@tonic-gate ASSERT(qp->qh_qlen == cnt); 3640Sstevel@tonic-gate } 3650Sstevel@tonic-gate 3660Sstevel@tonic-gate #else /* THREAD_DEBUG */ 3670Sstevel@tonic-gate 3680Sstevel@tonic-gate #define QVERIFY(qp) 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate #endif /* THREAD_DEBUG */ 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate /* 3730Sstevel@tonic-gate * Acquire a queue head. 3740Sstevel@tonic-gate */ 3750Sstevel@tonic-gate queue_head_t * 3760Sstevel@tonic-gate queue_lock(void *wchan, int qtype) 3770Sstevel@tonic-gate { 3780Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata; 3790Sstevel@tonic-gate queue_head_t *qp; 3800Sstevel@tonic-gate 3810Sstevel@tonic-gate ASSERT(qtype == MX || qtype == CV); 3820Sstevel@tonic-gate 3830Sstevel@tonic-gate /* 3840Sstevel@tonic-gate * It is possible that we could be called while still single-threaded. 3850Sstevel@tonic-gate * If so, we call queue_alloc() to allocate the queue_head[] array. 3860Sstevel@tonic-gate */ 3870Sstevel@tonic-gate if ((qp = udp->queue_head) == NULL) { 3880Sstevel@tonic-gate queue_alloc(); 3890Sstevel@tonic-gate qp = udp->queue_head; 3900Sstevel@tonic-gate } 3910Sstevel@tonic-gate qp += QUEUE_HASH(wchan, qtype); 3920Sstevel@tonic-gate spin_lock_set(&qp->qh_lock); 3930Sstevel@tonic-gate /* 3940Sstevel@tonic-gate * At once per nanosecond, qh_lockcount will wrap after 512 years. 3950Sstevel@tonic-gate * Were we to care about this, we could peg the value at UINT64_MAX. 3960Sstevel@tonic-gate */ 3970Sstevel@tonic-gate qp->qh_lockcount++; 3980Sstevel@tonic-gate QVERIFY(qp); 3990Sstevel@tonic-gate return (qp); 4000Sstevel@tonic-gate } 4010Sstevel@tonic-gate 4020Sstevel@tonic-gate /* 4030Sstevel@tonic-gate * Release a queue head. 4040Sstevel@tonic-gate */ 4050Sstevel@tonic-gate void 4060Sstevel@tonic-gate queue_unlock(queue_head_t *qp) 4070Sstevel@tonic-gate { 4080Sstevel@tonic-gate QVERIFY(qp); 4090Sstevel@tonic-gate spin_lock_clear(&qp->qh_lock); 4100Sstevel@tonic-gate } 4110Sstevel@tonic-gate 4120Sstevel@tonic-gate /* 4130Sstevel@tonic-gate * For rwlock queueing, we must queue writers ahead of readers of the 4140Sstevel@tonic-gate * same priority. We do this by making writers appear to have a half 4150Sstevel@tonic-gate * point higher priority for purposes of priority comparisons below. 4160Sstevel@tonic-gate */ 4170Sstevel@tonic-gate #define CMP_PRIO(ulwp) ((real_priority(ulwp) << 1) + (ulwp)->ul_writer) 4180Sstevel@tonic-gate 4190Sstevel@tonic-gate void 4200Sstevel@tonic-gate enqueue(queue_head_t *qp, ulwp_t *ulwp, void *wchan, int qtype) 4210Sstevel@tonic-gate { 4220Sstevel@tonic-gate ulwp_t **ulwpp; 4230Sstevel@tonic-gate ulwp_t *next; 4240Sstevel@tonic-gate int pri = CMP_PRIO(ulwp); 4250Sstevel@tonic-gate int force_fifo = (qtype & FIFOQ); 4260Sstevel@tonic-gate int do_fifo; 4270Sstevel@tonic-gate 4280Sstevel@tonic-gate qtype &= ~FIFOQ; 4290Sstevel@tonic-gate ASSERT(qtype == MX || qtype == CV); 4300Sstevel@tonic-gate ASSERT(MUTEX_OWNED(&qp->qh_lock, curthread)); 4310Sstevel@tonic-gate ASSERT(ulwp->ul_sleepq != qp); 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate /* 4340Sstevel@tonic-gate * LIFO queue ordering is unfair and can lead to starvation, 4350Sstevel@tonic-gate * but it gives better performance for heavily contended locks. 4360Sstevel@tonic-gate * We use thread_queue_fifo (range is 0..8) to determine 4370Sstevel@tonic-gate * the frequency of FIFO vs LIFO queuing: 4380Sstevel@tonic-gate * 0 : every 256th time (almost always LIFO) 4390Sstevel@tonic-gate * 1 : every 128th time 4400Sstevel@tonic-gate * 2 : every 64th time 4410Sstevel@tonic-gate * 3 : every 32nd time 4420Sstevel@tonic-gate * 4 : every 16th time (the default value, mostly LIFO) 4430Sstevel@tonic-gate * 5 : every 8th time 4440Sstevel@tonic-gate * 6 : every 4th time 4450Sstevel@tonic-gate * 7 : every 2nd time 4460Sstevel@tonic-gate * 8 : every time (never LIFO, always FIFO) 4470Sstevel@tonic-gate * Note that there is always some degree of FIFO ordering. 4480Sstevel@tonic-gate * This breaks live lock conditions that occur in applications 4490Sstevel@tonic-gate * that are written assuming (incorrectly) that threads acquire 4500Sstevel@tonic-gate * locks fairly, that is, in roughly round-robin order. 4510Sstevel@tonic-gate * In any event, the queue is maintained in priority order. 4520Sstevel@tonic-gate * 4530Sstevel@tonic-gate * If we are given the FIFOQ flag in qtype, fifo queueing is forced. 4540Sstevel@tonic-gate * SUSV3 requires this for semaphores. 4550Sstevel@tonic-gate */ 4560Sstevel@tonic-gate do_fifo = (force_fifo || 4570Sstevel@tonic-gate ((++qp->qh_qcnt << curthread->ul_queue_fifo) & 0xff) == 0); 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate if (qp->qh_head == NULL) { 4600Sstevel@tonic-gate /* 4610Sstevel@tonic-gate * The queue is empty. LIFO/FIFO doesn't matter. 4620Sstevel@tonic-gate */ 4630Sstevel@tonic-gate ASSERT(qp->qh_tail == NULL); 4640Sstevel@tonic-gate ulwpp = &qp->qh_head; 4650Sstevel@tonic-gate } else if (do_fifo) { 4660Sstevel@tonic-gate /* 4670Sstevel@tonic-gate * Enqueue after the last thread whose priority is greater 4680Sstevel@tonic-gate * than or equal to the priority of the thread being queued. 4690Sstevel@tonic-gate * Attempt first to go directly onto the tail of the queue. 4700Sstevel@tonic-gate */ 4710Sstevel@tonic-gate if (pri <= CMP_PRIO(qp->qh_tail)) 4720Sstevel@tonic-gate ulwpp = &qp->qh_tail->ul_link; 4730Sstevel@tonic-gate else { 4740Sstevel@tonic-gate for (ulwpp = &qp->qh_head; (next = *ulwpp) != NULL; 4750Sstevel@tonic-gate ulwpp = &next->ul_link) 4760Sstevel@tonic-gate if (pri > CMP_PRIO(next)) 4770Sstevel@tonic-gate break; 4780Sstevel@tonic-gate } 4790Sstevel@tonic-gate } else { 4800Sstevel@tonic-gate /* 4810Sstevel@tonic-gate * Enqueue before the first thread whose priority is less 4820Sstevel@tonic-gate * than or equal to the priority of the thread being queued. 4830Sstevel@tonic-gate * Hopefully we can go directly onto the head of the queue. 4840Sstevel@tonic-gate */ 4850Sstevel@tonic-gate for (ulwpp = &qp->qh_head; (next = *ulwpp) != NULL; 4860Sstevel@tonic-gate ulwpp = &next->ul_link) 4870Sstevel@tonic-gate if (pri >= CMP_PRIO(next)) 4880Sstevel@tonic-gate break; 4890Sstevel@tonic-gate } 4900Sstevel@tonic-gate if ((ulwp->ul_link = *ulwpp) == NULL) 4910Sstevel@tonic-gate qp->qh_tail = ulwp; 4920Sstevel@tonic-gate *ulwpp = ulwp; 4930Sstevel@tonic-gate 4940Sstevel@tonic-gate ulwp->ul_sleepq = qp; 4950Sstevel@tonic-gate ulwp->ul_wchan = wchan; 4960Sstevel@tonic-gate ulwp->ul_qtype = qtype; 4970Sstevel@tonic-gate if (qp->qh_qmax < ++qp->qh_qlen) 4980Sstevel@tonic-gate qp->qh_qmax = qp->qh_qlen; 4990Sstevel@tonic-gate } 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate /* 5020Sstevel@tonic-gate * Return a pointer to the queue slot of the 5030Sstevel@tonic-gate * highest priority thread on the queue. 5040Sstevel@tonic-gate * On return, prevp, if not NULL, will contain a pointer 5050Sstevel@tonic-gate * to the thread's predecessor on the queue 5060Sstevel@tonic-gate */ 5070Sstevel@tonic-gate static ulwp_t ** 5080Sstevel@tonic-gate queue_slot(queue_head_t *qp, void *wchan, int *more, ulwp_t **prevp) 5090Sstevel@tonic-gate { 5100Sstevel@tonic-gate ulwp_t **ulwpp; 5110Sstevel@tonic-gate ulwp_t *ulwp; 5120Sstevel@tonic-gate ulwp_t *prev = NULL; 5130Sstevel@tonic-gate ulwp_t **suspp = NULL; 5140Sstevel@tonic-gate ulwp_t *susprev; 5150Sstevel@tonic-gate 5160Sstevel@tonic-gate ASSERT(MUTEX_OWNED(&qp->qh_lock, curthread)); 5170Sstevel@tonic-gate 5180Sstevel@tonic-gate /* 5190Sstevel@tonic-gate * Find a waiter on the sleep queue. 5200Sstevel@tonic-gate */ 5210Sstevel@tonic-gate for (ulwpp = &qp->qh_head; (ulwp = *ulwpp) != NULL; 5220Sstevel@tonic-gate prev = ulwp, ulwpp = &ulwp->ul_link) { 5230Sstevel@tonic-gate if (ulwp->ul_wchan == wchan) { 5240Sstevel@tonic-gate if (!ulwp->ul_stop) 5250Sstevel@tonic-gate break; 5260Sstevel@tonic-gate /* 5270Sstevel@tonic-gate * Try not to return a suspended thread. 5280Sstevel@tonic-gate * This mimics the old libthread's behavior. 5290Sstevel@tonic-gate */ 5300Sstevel@tonic-gate if (suspp == NULL) { 5310Sstevel@tonic-gate suspp = ulwpp; 5320Sstevel@tonic-gate susprev = prev; 5330Sstevel@tonic-gate } 5340Sstevel@tonic-gate } 5350Sstevel@tonic-gate } 5360Sstevel@tonic-gate 5370Sstevel@tonic-gate if (ulwp == NULL && suspp != NULL) { 5380Sstevel@tonic-gate ulwp = *(ulwpp = suspp); 5390Sstevel@tonic-gate prev = susprev; 5400Sstevel@tonic-gate suspp = NULL; 5410Sstevel@tonic-gate } 5420Sstevel@tonic-gate if (ulwp == NULL) { 5430Sstevel@tonic-gate if (more != NULL) 5440Sstevel@tonic-gate *more = 0; 5450Sstevel@tonic-gate return (NULL); 5460Sstevel@tonic-gate } 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate if (prevp != NULL) 5490Sstevel@tonic-gate *prevp = prev; 5500Sstevel@tonic-gate if (more == NULL) 5510Sstevel@tonic-gate return (ulwpp); 5520Sstevel@tonic-gate 5530Sstevel@tonic-gate /* 5540Sstevel@tonic-gate * Scan the remainder of the queue for another waiter. 5550Sstevel@tonic-gate */ 5560Sstevel@tonic-gate if (suspp != NULL) { 5570Sstevel@tonic-gate *more = 1; 5580Sstevel@tonic-gate return (ulwpp); 5590Sstevel@tonic-gate } 5600Sstevel@tonic-gate for (ulwp = ulwp->ul_link; ulwp != NULL; ulwp = ulwp->ul_link) { 5610Sstevel@tonic-gate if (ulwp->ul_wchan == wchan) { 5620Sstevel@tonic-gate *more = 1; 5630Sstevel@tonic-gate return (ulwpp); 5640Sstevel@tonic-gate } 5650Sstevel@tonic-gate } 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate *more = 0; 5680Sstevel@tonic-gate return (ulwpp); 5690Sstevel@tonic-gate } 5700Sstevel@tonic-gate 5710Sstevel@tonic-gate ulwp_t * 5720Sstevel@tonic-gate dequeue(queue_head_t *qp, void *wchan, int *more) 5730Sstevel@tonic-gate { 5740Sstevel@tonic-gate ulwp_t **ulwpp; 5750Sstevel@tonic-gate ulwp_t *ulwp; 5760Sstevel@tonic-gate ulwp_t *prev; 5770Sstevel@tonic-gate 5780Sstevel@tonic-gate if ((ulwpp = queue_slot(qp, wchan, more, &prev)) == NULL) 5790Sstevel@tonic-gate return (NULL); 5800Sstevel@tonic-gate 5810Sstevel@tonic-gate /* 5820Sstevel@tonic-gate * Dequeue the waiter. 5830Sstevel@tonic-gate */ 5840Sstevel@tonic-gate ulwp = *ulwpp; 5850Sstevel@tonic-gate *ulwpp = ulwp->ul_link; 5860Sstevel@tonic-gate ulwp->ul_link = NULL; 5870Sstevel@tonic-gate if (qp->qh_tail == ulwp) 5880Sstevel@tonic-gate qp->qh_tail = prev; 5890Sstevel@tonic-gate qp->qh_qlen--; 5900Sstevel@tonic-gate ulwp->ul_sleepq = NULL; 5910Sstevel@tonic-gate ulwp->ul_wchan = NULL; 5920Sstevel@tonic-gate 5930Sstevel@tonic-gate return (ulwp); 5940Sstevel@tonic-gate } 5950Sstevel@tonic-gate 5960Sstevel@tonic-gate /* 5970Sstevel@tonic-gate * Return a pointer to the highest priority thread sleeping on wchan. 5980Sstevel@tonic-gate */ 5990Sstevel@tonic-gate ulwp_t * 6000Sstevel@tonic-gate queue_waiter(queue_head_t *qp, void *wchan) 6010Sstevel@tonic-gate { 6020Sstevel@tonic-gate ulwp_t **ulwpp; 6030Sstevel@tonic-gate 6040Sstevel@tonic-gate if ((ulwpp = queue_slot(qp, wchan, NULL, NULL)) == NULL) 6050Sstevel@tonic-gate return (NULL); 6060Sstevel@tonic-gate return (*ulwpp); 6070Sstevel@tonic-gate } 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate uint8_t 6100Sstevel@tonic-gate dequeue_self(queue_head_t *qp, void *wchan) 6110Sstevel@tonic-gate { 6120Sstevel@tonic-gate ulwp_t *self = curthread; 6130Sstevel@tonic-gate ulwp_t **ulwpp; 6140Sstevel@tonic-gate ulwp_t *ulwp; 6150Sstevel@tonic-gate ulwp_t *prev = NULL; 6160Sstevel@tonic-gate int found = 0; 6170Sstevel@tonic-gate int more = 0; 6180Sstevel@tonic-gate 6190Sstevel@tonic-gate ASSERT(MUTEX_OWNED(&qp->qh_lock, self)); 6200Sstevel@tonic-gate 6210Sstevel@tonic-gate /* find self on the sleep queue */ 6220Sstevel@tonic-gate for (ulwpp = &qp->qh_head; (ulwp = *ulwpp) != NULL; 6230Sstevel@tonic-gate prev = ulwp, ulwpp = &ulwp->ul_link) { 6240Sstevel@tonic-gate if (ulwp == self) { 6250Sstevel@tonic-gate /* dequeue ourself */ 6260Sstevel@tonic-gate *ulwpp = self->ul_link; 6270Sstevel@tonic-gate if (qp->qh_tail == self) 6280Sstevel@tonic-gate qp->qh_tail = prev; 6290Sstevel@tonic-gate qp->qh_qlen--; 6300Sstevel@tonic-gate ASSERT(self->ul_wchan == wchan); 6310Sstevel@tonic-gate self->ul_cvmutex = NULL; 6320Sstevel@tonic-gate self->ul_sleepq = NULL; 6330Sstevel@tonic-gate self->ul_wchan = NULL; 6340Sstevel@tonic-gate self->ul_cv_wake = 0; 6350Sstevel@tonic-gate self->ul_link = NULL; 6360Sstevel@tonic-gate found = 1; 6370Sstevel@tonic-gate break; 6380Sstevel@tonic-gate } 6390Sstevel@tonic-gate if (ulwp->ul_wchan == wchan) 6400Sstevel@tonic-gate more = 1; 6410Sstevel@tonic-gate } 6420Sstevel@tonic-gate 6430Sstevel@tonic-gate if (!found) 6440Sstevel@tonic-gate thr_panic("dequeue_self(): curthread not found on queue"); 6450Sstevel@tonic-gate 6460Sstevel@tonic-gate if (more) 6470Sstevel@tonic-gate return (1); 6480Sstevel@tonic-gate 6490Sstevel@tonic-gate /* scan the remainder of the queue for another waiter */ 6500Sstevel@tonic-gate for (ulwp = *ulwpp; ulwp != NULL; ulwp = ulwp->ul_link) { 6510Sstevel@tonic-gate if (ulwp->ul_wchan == wchan) 6520Sstevel@tonic-gate return (1); 6530Sstevel@tonic-gate } 6540Sstevel@tonic-gate 6550Sstevel@tonic-gate return (0); 6560Sstevel@tonic-gate } 6570Sstevel@tonic-gate 6580Sstevel@tonic-gate /* 6590Sstevel@tonic-gate * Called from call_user_handler() and _thrp_suspend() to take 6600Sstevel@tonic-gate * ourself off of our sleep queue so we can grab locks. 6610Sstevel@tonic-gate */ 6620Sstevel@tonic-gate void 6630Sstevel@tonic-gate unsleep_self(void) 6640Sstevel@tonic-gate { 6650Sstevel@tonic-gate ulwp_t *self = curthread; 6660Sstevel@tonic-gate queue_head_t *qp; 6670Sstevel@tonic-gate 6680Sstevel@tonic-gate /* 6690Sstevel@tonic-gate * Calling enter_critical()/exit_critical() here would lead 6700Sstevel@tonic-gate * to recursion. Just manipulate self->ul_critical directly. 6710Sstevel@tonic-gate */ 6720Sstevel@tonic-gate self->ul_critical++; 6730Sstevel@tonic-gate self->ul_writer = 0; 6740Sstevel@tonic-gate while (self->ul_sleepq != NULL) { 6750Sstevel@tonic-gate qp = queue_lock(self->ul_wchan, self->ul_qtype); 6760Sstevel@tonic-gate /* 6770Sstevel@tonic-gate * We may have been moved from a CV queue to a 6780Sstevel@tonic-gate * mutex queue while we were attempting queue_lock(). 6790Sstevel@tonic-gate * If so, just loop around and try again. 6800Sstevel@tonic-gate * dequeue_self() clears self->ul_sleepq. 6810Sstevel@tonic-gate */ 6820Sstevel@tonic-gate if (qp == self->ul_sleepq) 6830Sstevel@tonic-gate (void) dequeue_self(qp, self->ul_wchan); 6840Sstevel@tonic-gate queue_unlock(qp); 6850Sstevel@tonic-gate } 6860Sstevel@tonic-gate self->ul_critical--; 6870Sstevel@tonic-gate } 6880Sstevel@tonic-gate 6890Sstevel@tonic-gate /* 6900Sstevel@tonic-gate * Common code for calling the the ___lwp_mutex_timedlock() system call. 6910Sstevel@tonic-gate * Returns with mutex_owner and mutex_ownerpid set correctly. 6920Sstevel@tonic-gate */ 6930Sstevel@tonic-gate int 6940Sstevel@tonic-gate mutex_lock_kernel(mutex_t *mp, timespec_t *tsp, tdb_mutex_stats_t *msp) 6950Sstevel@tonic-gate { 6960Sstevel@tonic-gate ulwp_t *self = curthread; 6970Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 6980Sstevel@tonic-gate hrtime_t begin_sleep; 6990Sstevel@tonic-gate int error; 7000Sstevel@tonic-gate 7010Sstevel@tonic-gate self->ul_sp = stkptr(); 7020Sstevel@tonic-gate self->ul_wchan = mp; 7030Sstevel@tonic-gate if (__td_event_report(self, TD_SLEEP, udp)) { 7040Sstevel@tonic-gate self->ul_td_evbuf.eventnum = TD_SLEEP; 7050Sstevel@tonic-gate self->ul_td_evbuf.eventdata = mp; 7060Sstevel@tonic-gate tdb_event(TD_SLEEP, udp); 7070Sstevel@tonic-gate } 7080Sstevel@tonic-gate if (msp) { 7090Sstevel@tonic-gate tdb_incr(msp->mutex_sleep); 7100Sstevel@tonic-gate begin_sleep = gethrtime(); 7110Sstevel@tonic-gate } 7120Sstevel@tonic-gate 7130Sstevel@tonic-gate DTRACE_PROBE1(plockstat, mutex__block, mp); 7140Sstevel@tonic-gate 7150Sstevel@tonic-gate for (;;) { 7160Sstevel@tonic-gate if ((error = ___lwp_mutex_timedlock(mp, tsp)) != 0) { 7170Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__blocked, mp, 0); 7180Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__error, mp, error); 7190Sstevel@tonic-gate break; 7200Sstevel@tonic-gate } 7210Sstevel@tonic-gate 7220Sstevel@tonic-gate if (mp->mutex_type & (USYNC_PROCESS | USYNC_PROCESS_ROBUST)) { 7230Sstevel@tonic-gate /* 7240Sstevel@tonic-gate * Defend against forkall(). We may be the child, 7250Sstevel@tonic-gate * in which case we don't actually own the mutex. 7260Sstevel@tonic-gate */ 7270Sstevel@tonic-gate enter_critical(self); 7280Sstevel@tonic-gate if (mp->mutex_ownerpid == udp->pid) { 7290Sstevel@tonic-gate mp->mutex_owner = (uintptr_t)self; 7300Sstevel@tonic-gate exit_critical(self); 7310Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__blocked, mp, 1); 7320Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 7330Sstevel@tonic-gate 0, 0); 7340Sstevel@tonic-gate break; 7350Sstevel@tonic-gate } 7360Sstevel@tonic-gate exit_critical(self); 7370Sstevel@tonic-gate } else { 7380Sstevel@tonic-gate mp->mutex_owner = (uintptr_t)self; 7390Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__blocked, mp, 1); 7400Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0); 7410Sstevel@tonic-gate break; 7420Sstevel@tonic-gate } 7430Sstevel@tonic-gate } 7440Sstevel@tonic-gate if (msp) 7450Sstevel@tonic-gate msp->mutex_sleep_time += gethrtime() - begin_sleep; 7460Sstevel@tonic-gate self->ul_wchan = NULL; 7470Sstevel@tonic-gate self->ul_sp = 0; 7480Sstevel@tonic-gate 7490Sstevel@tonic-gate return (error); 7500Sstevel@tonic-gate } 7510Sstevel@tonic-gate 7520Sstevel@tonic-gate /* 7530Sstevel@tonic-gate * Common code for calling the ___lwp_mutex_trylock() system call. 7540Sstevel@tonic-gate * Returns with mutex_owner and mutex_ownerpid set correctly. 7550Sstevel@tonic-gate */ 7560Sstevel@tonic-gate int 7570Sstevel@tonic-gate mutex_trylock_kernel(mutex_t *mp) 7580Sstevel@tonic-gate { 7590Sstevel@tonic-gate ulwp_t *self = curthread; 7600Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 7610Sstevel@tonic-gate int error; 7620Sstevel@tonic-gate 7630Sstevel@tonic-gate for (;;) { 7640Sstevel@tonic-gate if ((error = ___lwp_mutex_trylock(mp)) != 0) { 7650Sstevel@tonic-gate if (error != EBUSY) { 7660Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__error, mp, 7670Sstevel@tonic-gate error); 7680Sstevel@tonic-gate } 7690Sstevel@tonic-gate break; 7700Sstevel@tonic-gate } 7710Sstevel@tonic-gate 7720Sstevel@tonic-gate if (mp->mutex_type & (USYNC_PROCESS | USYNC_PROCESS_ROBUST)) { 7730Sstevel@tonic-gate /* 7740Sstevel@tonic-gate * Defend against forkall(). We may be the child, 7750Sstevel@tonic-gate * in which case we don't actually own the mutex. 7760Sstevel@tonic-gate */ 7770Sstevel@tonic-gate enter_critical(self); 7780Sstevel@tonic-gate if (mp->mutex_ownerpid == udp->pid) { 7790Sstevel@tonic-gate mp->mutex_owner = (uintptr_t)self; 7800Sstevel@tonic-gate exit_critical(self); 7810Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 7820Sstevel@tonic-gate 0, 0); 7830Sstevel@tonic-gate break; 7840Sstevel@tonic-gate } 7850Sstevel@tonic-gate exit_critical(self); 7860Sstevel@tonic-gate } else { 7870Sstevel@tonic-gate mp->mutex_owner = (uintptr_t)self; 7880Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0); 7890Sstevel@tonic-gate break; 7900Sstevel@tonic-gate } 7910Sstevel@tonic-gate } 7920Sstevel@tonic-gate 7930Sstevel@tonic-gate return (error); 7940Sstevel@tonic-gate } 7950Sstevel@tonic-gate 7960Sstevel@tonic-gate volatile sc_shared_t * 7970Sstevel@tonic-gate setup_schedctl(void) 7980Sstevel@tonic-gate { 7990Sstevel@tonic-gate ulwp_t *self = curthread; 8000Sstevel@tonic-gate volatile sc_shared_t *scp; 8010Sstevel@tonic-gate sc_shared_t *tmp; 8020Sstevel@tonic-gate 8030Sstevel@tonic-gate if ((scp = self->ul_schedctl) == NULL && /* no shared state yet */ 8040Sstevel@tonic-gate !self->ul_vfork && /* not a child of vfork() */ 8050Sstevel@tonic-gate !self->ul_schedctl_called) { /* haven't been called before */ 8060Sstevel@tonic-gate enter_critical(self); 8070Sstevel@tonic-gate self->ul_schedctl_called = &self->ul_uberdata->uberflags; 8080Sstevel@tonic-gate if ((tmp = __schedctl()) != (sc_shared_t *)(-1)) 8090Sstevel@tonic-gate self->ul_schedctl = scp = tmp; 8100Sstevel@tonic-gate exit_critical(self); 8110Sstevel@tonic-gate } 8120Sstevel@tonic-gate /* 8130Sstevel@tonic-gate * Unless the call to setup_schedctl() is surrounded 8140Sstevel@tonic-gate * by enter_critical()/exit_critical(), the address 8150Sstevel@tonic-gate * we are returning could be invalid due to a forkall() 8160Sstevel@tonic-gate * having occurred in another thread. 8170Sstevel@tonic-gate */ 8180Sstevel@tonic-gate return (scp); 8190Sstevel@tonic-gate } 8200Sstevel@tonic-gate 8210Sstevel@tonic-gate /* 8220Sstevel@tonic-gate * Interfaces from libsched, incorporated into libc. 8230Sstevel@tonic-gate * libsched.so.1 is now a filter library onto libc. 8240Sstevel@tonic-gate */ 8250Sstevel@tonic-gate #pragma weak schedctl_lookup = _schedctl_init 8260Sstevel@tonic-gate #pragma weak _schedctl_lookup = _schedctl_init 8270Sstevel@tonic-gate #pragma weak schedctl_init = _schedctl_init 8280Sstevel@tonic-gate schedctl_t * 8290Sstevel@tonic-gate _schedctl_init(void) 8300Sstevel@tonic-gate { 8310Sstevel@tonic-gate volatile sc_shared_t *scp = setup_schedctl(); 8320Sstevel@tonic-gate return ((scp == NULL)? NULL : (schedctl_t *)&scp->sc_preemptctl); 8330Sstevel@tonic-gate } 8340Sstevel@tonic-gate 8350Sstevel@tonic-gate #pragma weak schedctl_exit = _schedctl_exit 8360Sstevel@tonic-gate void 8370Sstevel@tonic-gate _schedctl_exit(void) 8380Sstevel@tonic-gate { 8390Sstevel@tonic-gate } 8400Sstevel@tonic-gate 8410Sstevel@tonic-gate /* 8420Sstevel@tonic-gate * Contract private interface for java. 8430Sstevel@tonic-gate * Set up the schedctl data if it doesn't exist yet. 8440Sstevel@tonic-gate * Return a pointer to the pointer to the schedctl data. 8450Sstevel@tonic-gate */ 8460Sstevel@tonic-gate volatile sc_shared_t *volatile * 8470Sstevel@tonic-gate _thr_schedctl(void) 8480Sstevel@tonic-gate { 8490Sstevel@tonic-gate ulwp_t *self = curthread; 8500Sstevel@tonic-gate volatile sc_shared_t *volatile *ptr; 8510Sstevel@tonic-gate 8520Sstevel@tonic-gate if (self->ul_vfork) 8530Sstevel@tonic-gate return (NULL); 8540Sstevel@tonic-gate if (*(ptr = &self->ul_schedctl) == NULL) 8550Sstevel@tonic-gate (void) setup_schedctl(); 8560Sstevel@tonic-gate return (ptr); 8570Sstevel@tonic-gate } 8580Sstevel@tonic-gate 8590Sstevel@tonic-gate /* 8600Sstevel@tonic-gate * Block signals and attempt to block preemption. 8610Sstevel@tonic-gate * no_preempt()/preempt() must be used in pairs but can be nested. 8620Sstevel@tonic-gate */ 8630Sstevel@tonic-gate void 8640Sstevel@tonic-gate no_preempt(ulwp_t *self) 8650Sstevel@tonic-gate { 8660Sstevel@tonic-gate volatile sc_shared_t *scp; 8670Sstevel@tonic-gate 8680Sstevel@tonic-gate if (self->ul_preempt++ == 0) { 8690Sstevel@tonic-gate enter_critical(self); 8700Sstevel@tonic-gate if ((scp = self->ul_schedctl) != NULL || 8710Sstevel@tonic-gate (scp = setup_schedctl()) != NULL) { 8720Sstevel@tonic-gate /* 8730Sstevel@tonic-gate * Save the pre-existing preempt value. 8740Sstevel@tonic-gate */ 8750Sstevel@tonic-gate self->ul_savpreempt = scp->sc_preemptctl.sc_nopreempt; 8760Sstevel@tonic-gate scp->sc_preemptctl.sc_nopreempt = 1; 8770Sstevel@tonic-gate } 8780Sstevel@tonic-gate } 8790Sstevel@tonic-gate } 8800Sstevel@tonic-gate 8810Sstevel@tonic-gate /* 8820Sstevel@tonic-gate * Undo the effects of no_preempt(). 8830Sstevel@tonic-gate */ 8840Sstevel@tonic-gate void 8850Sstevel@tonic-gate preempt(ulwp_t *self) 8860Sstevel@tonic-gate { 8870Sstevel@tonic-gate volatile sc_shared_t *scp; 8880Sstevel@tonic-gate 8890Sstevel@tonic-gate ASSERT(self->ul_preempt > 0); 8900Sstevel@tonic-gate if (--self->ul_preempt == 0) { 8910Sstevel@tonic-gate if ((scp = self->ul_schedctl) != NULL) { 8920Sstevel@tonic-gate /* 8930Sstevel@tonic-gate * Restore the pre-existing preempt value. 8940Sstevel@tonic-gate */ 8950Sstevel@tonic-gate scp->sc_preemptctl.sc_nopreempt = self->ul_savpreempt; 8960Sstevel@tonic-gate if (scp->sc_preemptctl.sc_yield && 8970Sstevel@tonic-gate scp->sc_preemptctl.sc_nopreempt == 0) { 8980Sstevel@tonic-gate lwp_yield(); 8990Sstevel@tonic-gate if (scp->sc_preemptctl.sc_yield) { 9000Sstevel@tonic-gate /* 9010Sstevel@tonic-gate * Shouldn't happen. This is either 9020Sstevel@tonic-gate * a race condition or the thread 9030Sstevel@tonic-gate * just entered the real-time class. 9040Sstevel@tonic-gate */ 9050Sstevel@tonic-gate lwp_yield(); 9060Sstevel@tonic-gate scp->sc_preemptctl.sc_yield = 0; 9070Sstevel@tonic-gate } 9080Sstevel@tonic-gate } 9090Sstevel@tonic-gate } 9100Sstevel@tonic-gate exit_critical(self); 9110Sstevel@tonic-gate } 9120Sstevel@tonic-gate } 9130Sstevel@tonic-gate 9140Sstevel@tonic-gate /* 9150Sstevel@tonic-gate * If a call to preempt() would cause the current thread to yield or to 9160Sstevel@tonic-gate * take deferred actions in exit_critical(), then unpark the specified 9170Sstevel@tonic-gate * lwp so it can run while we delay. Return the original lwpid if the 9180Sstevel@tonic-gate * unpark was not performed, else return zero. The tests are a repeat 9190Sstevel@tonic-gate * of some of the tests in preempt(), above. This is a statistical 9200Sstevel@tonic-gate * optimization solely for cond_sleep_queue(), below. 9210Sstevel@tonic-gate */ 9220Sstevel@tonic-gate static lwpid_t 9230Sstevel@tonic-gate preempt_unpark(ulwp_t *self, lwpid_t lwpid) 9240Sstevel@tonic-gate { 9250Sstevel@tonic-gate volatile sc_shared_t *scp = self->ul_schedctl; 9260Sstevel@tonic-gate 9270Sstevel@tonic-gate ASSERT(self->ul_preempt == 1 && self->ul_critical > 0); 9280Sstevel@tonic-gate if ((scp != NULL && scp->sc_preemptctl.sc_yield) || 9290Sstevel@tonic-gate (self->ul_curplease && self->ul_critical == 1)) { 9300Sstevel@tonic-gate (void) __lwp_unpark(lwpid); 9310Sstevel@tonic-gate lwpid = 0; 9320Sstevel@tonic-gate } 9330Sstevel@tonic-gate return (lwpid); 9340Sstevel@tonic-gate } 9350Sstevel@tonic-gate 9360Sstevel@tonic-gate /* 9370Sstevel@tonic-gate * Spin for a while, trying to grab the lock. We know that we 9380Sstevel@tonic-gate * failed set_lock_byte(&mp->mutex_lockw) once before coming here. 9390Sstevel@tonic-gate * If this fails, return EBUSY and let the caller deal with it. 9400Sstevel@tonic-gate * If this succeeds, return 0 with mutex_owner set to curthread. 9410Sstevel@tonic-gate */ 9420Sstevel@tonic-gate int 9430Sstevel@tonic-gate mutex_trylock_adaptive(mutex_t *mp) 9440Sstevel@tonic-gate { 9450Sstevel@tonic-gate ulwp_t *self = curthread; 9460Sstevel@tonic-gate ulwp_t *ulwp; 9470Sstevel@tonic-gate volatile sc_shared_t *scp; 9480Sstevel@tonic-gate volatile uint8_t *lockp; 9490Sstevel@tonic-gate volatile uint64_t *ownerp; 9500Sstevel@tonic-gate int count, max = self->ul_adaptive_spin; 9510Sstevel@tonic-gate 9520Sstevel@tonic-gate ASSERT(!(mp->mutex_type & (USYNC_PROCESS | USYNC_PROCESS_ROBUST))); 9530Sstevel@tonic-gate 9540Sstevel@tonic-gate if (max == 0 || (mp->mutex_spinners >= self->ul_max_spinners)) 9550Sstevel@tonic-gate return (EBUSY); 9560Sstevel@tonic-gate 9570Sstevel@tonic-gate lockp = (volatile uint8_t *)&mp->mutex_lockw; 9580Sstevel@tonic-gate ownerp = (volatile uint64_t *)&mp->mutex_owner; 9590Sstevel@tonic-gate 9600Sstevel@tonic-gate DTRACE_PROBE1(plockstat, mutex__spin, mp); 9610Sstevel@tonic-gate 9620Sstevel@tonic-gate /* 9630Sstevel@tonic-gate * This spin loop is unfair to lwps that have already dropped into 9640Sstevel@tonic-gate * the kernel to sleep. They will starve on a highly-contended mutex. 9650Sstevel@tonic-gate * This is just too bad. The adaptive spin algorithm is intended 9660Sstevel@tonic-gate * to allow programs with highly-contended locks (that is, broken 9670Sstevel@tonic-gate * programs) to execute with reasonable speed despite their contention. 9680Sstevel@tonic-gate * Being fair would reduce the speed of such programs and well-written 9690Sstevel@tonic-gate * programs will not suffer in any case. 9700Sstevel@tonic-gate */ 9710Sstevel@tonic-gate enter_critical(self); /* protects ul_schedctl */ 9720Sstevel@tonic-gate incr32(&mp->mutex_spinners); 9730Sstevel@tonic-gate for (count = 0; count < max; count++) { 9740Sstevel@tonic-gate if (*lockp == 0 && set_lock_byte(lockp) == 0) { 9750Sstevel@tonic-gate *ownerp = (uintptr_t)self; 9760Sstevel@tonic-gate decr32(&mp->mutex_spinners); 9770Sstevel@tonic-gate exit_critical(self); 9780Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__spun, 1, count); 9790Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, count); 9800Sstevel@tonic-gate return (0); 9810Sstevel@tonic-gate } 9820Sstevel@tonic-gate SMT_PAUSE(); 9830Sstevel@tonic-gate /* 9840Sstevel@tonic-gate * Stop spinning if the mutex owner is not running on 9850Sstevel@tonic-gate * a processor; it will not drop the lock any time soon 9860Sstevel@tonic-gate * and we would just be wasting time to keep spinning. 9870Sstevel@tonic-gate * 9880Sstevel@tonic-gate * Note that we are looking at another thread (ulwp_t) 9890Sstevel@tonic-gate * without ensuring that the other thread does not exit. 9900Sstevel@tonic-gate * The scheme relies on ulwp_t structures never being 9910Sstevel@tonic-gate * deallocated by the library (the library employs a free 9920Sstevel@tonic-gate * list of ulwp_t structs that are reused when new threads 9930Sstevel@tonic-gate * are created) and on schedctl shared memory never being 9940Sstevel@tonic-gate * deallocated once created via __schedctl(). 9950Sstevel@tonic-gate * 9960Sstevel@tonic-gate * Thus, the worst that can happen when the spinning thread 9970Sstevel@tonic-gate * looks at the owner's schedctl data is that it is looking 9980Sstevel@tonic-gate * at some other thread's schedctl data. This almost never 9990Sstevel@tonic-gate * happens and is benign when it does. 10000Sstevel@tonic-gate */ 10010Sstevel@tonic-gate if ((ulwp = (ulwp_t *)(uintptr_t)*ownerp) != NULL && 10020Sstevel@tonic-gate ((scp = ulwp->ul_schedctl) == NULL || 10030Sstevel@tonic-gate scp->sc_state != SC_ONPROC)) 10040Sstevel@tonic-gate break; 10050Sstevel@tonic-gate } 10060Sstevel@tonic-gate decr32(&mp->mutex_spinners); 10070Sstevel@tonic-gate exit_critical(self); 10080Sstevel@tonic-gate 10090Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__spun, 0, count); 10100Sstevel@tonic-gate 10110Sstevel@tonic-gate return (EBUSY); 10120Sstevel@tonic-gate } 10130Sstevel@tonic-gate 10140Sstevel@tonic-gate /* 10150Sstevel@tonic-gate * Same as mutex_trylock_adaptive(), except specifically for queue locks. 10160Sstevel@tonic-gate * The owner field is not set here; the caller (spin_lock_set()) sets it. 10170Sstevel@tonic-gate */ 10180Sstevel@tonic-gate int 10190Sstevel@tonic-gate mutex_queuelock_adaptive(mutex_t *mp) 10200Sstevel@tonic-gate { 10210Sstevel@tonic-gate ulwp_t *ulwp; 10220Sstevel@tonic-gate volatile sc_shared_t *scp; 10230Sstevel@tonic-gate volatile uint8_t *lockp; 10240Sstevel@tonic-gate volatile uint64_t *ownerp; 10250Sstevel@tonic-gate int count = curthread->ul_queue_spin; 10260Sstevel@tonic-gate 10270Sstevel@tonic-gate ASSERT(mp->mutex_type == USYNC_THREAD); 10280Sstevel@tonic-gate 10290Sstevel@tonic-gate if (count == 0) 10300Sstevel@tonic-gate return (EBUSY); 10310Sstevel@tonic-gate 10320Sstevel@tonic-gate lockp = (volatile uint8_t *)&mp->mutex_lockw; 10330Sstevel@tonic-gate ownerp = (volatile uint64_t *)&mp->mutex_owner; 10340Sstevel@tonic-gate while (--count >= 0) { 10350Sstevel@tonic-gate if (*lockp == 0 && set_lock_byte(lockp) == 0) 10360Sstevel@tonic-gate return (0); 10370Sstevel@tonic-gate SMT_PAUSE(); 10380Sstevel@tonic-gate if ((ulwp = (ulwp_t *)(uintptr_t)*ownerp) != NULL && 10390Sstevel@tonic-gate ((scp = ulwp->ul_schedctl) == NULL || 10400Sstevel@tonic-gate scp->sc_state != SC_ONPROC)) 10410Sstevel@tonic-gate break; 10420Sstevel@tonic-gate } 10430Sstevel@tonic-gate 10440Sstevel@tonic-gate return (EBUSY); 10450Sstevel@tonic-gate } 10460Sstevel@tonic-gate 10470Sstevel@tonic-gate /* 10480Sstevel@tonic-gate * Like mutex_trylock_adaptive(), but for process-shared mutexes. 10490Sstevel@tonic-gate * Spin for a while, trying to grab the lock. We know that we 10500Sstevel@tonic-gate * failed set_lock_byte(&mp->mutex_lockw) once before coming here. 10510Sstevel@tonic-gate * If this fails, return EBUSY and let the caller deal with it. 10520Sstevel@tonic-gate * If this succeeds, return 0 with mutex_owner set to curthread 10530Sstevel@tonic-gate * and mutex_ownerpid set to the current pid. 10540Sstevel@tonic-gate */ 10550Sstevel@tonic-gate int 10560Sstevel@tonic-gate mutex_trylock_process(mutex_t *mp) 10570Sstevel@tonic-gate { 10580Sstevel@tonic-gate ulwp_t *self = curthread; 10590Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 10600Sstevel@tonic-gate int count; 10610Sstevel@tonic-gate volatile uint8_t *lockp; 10620Sstevel@tonic-gate volatile uint64_t *ownerp; 10630Sstevel@tonic-gate volatile int32_t *pidp; 10640Sstevel@tonic-gate pid_t pid, newpid; 10650Sstevel@tonic-gate uint64_t owner, newowner; 10660Sstevel@tonic-gate 10670Sstevel@tonic-gate if ((count = ncpus) == 0) 10680Sstevel@tonic-gate count = ncpus = (int)_sysconf(_SC_NPROCESSORS_ONLN); 10690Sstevel@tonic-gate count = (count > 1)? self->ul_adaptive_spin : 0; 10700Sstevel@tonic-gate 10710Sstevel@tonic-gate ASSERT((mp->mutex_type & ~(LOCK_RECURSIVE|LOCK_ERRORCHECK)) == 10720Sstevel@tonic-gate USYNC_PROCESS); 10730Sstevel@tonic-gate 10740Sstevel@tonic-gate if (count == 0) 10750Sstevel@tonic-gate return (EBUSY); 10760Sstevel@tonic-gate 10770Sstevel@tonic-gate lockp = (volatile uint8_t *)&mp->mutex_lockw; 10780Sstevel@tonic-gate ownerp = (volatile uint64_t *)&mp->mutex_owner; 10790Sstevel@tonic-gate pidp = (volatile int32_t *)&mp->mutex_ownerpid; 10800Sstevel@tonic-gate owner = *ownerp; 10810Sstevel@tonic-gate pid = *pidp; 10820Sstevel@tonic-gate /* 10830Sstevel@tonic-gate * This is a process-shared mutex. 10840Sstevel@tonic-gate * We cannot know if the owner is running on a processor. 10850Sstevel@tonic-gate * We just spin and hope that it is on a processor. 10860Sstevel@tonic-gate */ 10870Sstevel@tonic-gate while (--count >= 0) { 10880Sstevel@tonic-gate if (*lockp == 0) { 10890Sstevel@tonic-gate enter_critical(self); 10900Sstevel@tonic-gate if (set_lock_byte(lockp) == 0) { 10910Sstevel@tonic-gate *ownerp = (uintptr_t)self; 10920Sstevel@tonic-gate *pidp = udp->pid; 10930Sstevel@tonic-gate exit_critical(self); 10940Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 10950Sstevel@tonic-gate 0, 0); 10960Sstevel@tonic-gate return (0); 10970Sstevel@tonic-gate } 10980Sstevel@tonic-gate exit_critical(self); 10990Sstevel@tonic-gate } else if ((newowner = *ownerp) == owner && 11000Sstevel@tonic-gate (newpid = *pidp) == pid) { 11010Sstevel@tonic-gate SMT_PAUSE(); 11020Sstevel@tonic-gate continue; 11030Sstevel@tonic-gate } 11040Sstevel@tonic-gate /* 11050Sstevel@tonic-gate * The owner of the lock changed; start the count over again. 11060Sstevel@tonic-gate * This may be too aggressive; it needs testing. 11070Sstevel@tonic-gate */ 11080Sstevel@tonic-gate owner = newowner; 11090Sstevel@tonic-gate pid = newpid; 11100Sstevel@tonic-gate count = self->ul_adaptive_spin; 11110Sstevel@tonic-gate } 11120Sstevel@tonic-gate 11130Sstevel@tonic-gate return (EBUSY); 11140Sstevel@tonic-gate } 11150Sstevel@tonic-gate 11160Sstevel@tonic-gate /* 11170Sstevel@tonic-gate * Mutex wakeup code for releasing a USYNC_THREAD mutex. 11180Sstevel@tonic-gate * Returns the lwpid of the thread that was dequeued, if any. 11190Sstevel@tonic-gate * The caller of mutex_wakeup() must call __lwp_unpark(lwpid) 11200Sstevel@tonic-gate * to wake up the specified lwp. 11210Sstevel@tonic-gate */ 11220Sstevel@tonic-gate lwpid_t 11230Sstevel@tonic-gate mutex_wakeup(mutex_t *mp) 11240Sstevel@tonic-gate { 11250Sstevel@tonic-gate lwpid_t lwpid = 0; 11260Sstevel@tonic-gate queue_head_t *qp; 11270Sstevel@tonic-gate ulwp_t *ulwp; 11280Sstevel@tonic-gate int more; 11290Sstevel@tonic-gate 11300Sstevel@tonic-gate /* 11310Sstevel@tonic-gate * Dequeue a waiter from the sleep queue. Don't touch the mutex 11320Sstevel@tonic-gate * waiters bit if no one was found on the queue because the mutex 11330Sstevel@tonic-gate * might have been deallocated or reallocated for another purpose. 11340Sstevel@tonic-gate */ 11350Sstevel@tonic-gate qp = queue_lock(mp, MX); 11360Sstevel@tonic-gate if ((ulwp = dequeue(qp, mp, &more)) != NULL) { 11370Sstevel@tonic-gate lwpid = ulwp->ul_lwpid; 11380Sstevel@tonic-gate mp->mutex_waiters = (more? 1 : 0); 11390Sstevel@tonic-gate } 11400Sstevel@tonic-gate queue_unlock(qp); 11410Sstevel@tonic-gate return (lwpid); 11420Sstevel@tonic-gate } 11430Sstevel@tonic-gate 11440Sstevel@tonic-gate /* 11450Sstevel@tonic-gate * Spin for a while, testing to see if the lock has been grabbed. 11460Sstevel@tonic-gate * If this fails, call mutex_wakeup() to release a waiter. 11470Sstevel@tonic-gate */ 11480Sstevel@tonic-gate lwpid_t 11490Sstevel@tonic-gate mutex_unlock_queue(mutex_t *mp) 11500Sstevel@tonic-gate { 11510Sstevel@tonic-gate ulwp_t *self = curthread; 11520Sstevel@tonic-gate uint32_t *lockw = &mp->mutex_lockword; 11530Sstevel@tonic-gate lwpid_t lwpid; 11540Sstevel@tonic-gate volatile uint8_t *lockp; 11550Sstevel@tonic-gate volatile uint32_t *spinp; 11560Sstevel@tonic-gate int count; 11570Sstevel@tonic-gate 11580Sstevel@tonic-gate /* 11590Sstevel@tonic-gate * We use the swap primitive to clear the lock, but we must 11600Sstevel@tonic-gate * atomically retain the waiters bit for the remainder of this 11610Sstevel@tonic-gate * code to work. We first check to see if the waiters bit is 11620Sstevel@tonic-gate * set and if so clear the lock by swapping in a word containing 11630Sstevel@tonic-gate * only the waiters bit. This could produce a false positive test 11640Sstevel@tonic-gate * for whether there are waiters that need to be waked up, but 11650Sstevel@tonic-gate * this just causes an extra call to mutex_wakeup() to do nothing. 11660Sstevel@tonic-gate * The opposite case is more delicate: If there are no waiters, 11670Sstevel@tonic-gate * we swap in a zero lock byte and a zero waiters bit. The result 11680Sstevel@tonic-gate * of the swap could indicate that there really was a waiter so in 11690Sstevel@tonic-gate * this case we go directly to mutex_wakeup() without performing 11700Sstevel@tonic-gate * any of the adaptive code because the waiter bit has been cleared 11710Sstevel@tonic-gate * and the adaptive code is unreliable in this case. 11720Sstevel@tonic-gate */ 11730Sstevel@tonic-gate if (!(*lockw & WAITERMASK)) { /* no waiter exists right now */ 11740Sstevel@tonic-gate mp->mutex_owner = 0; 11750Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__release, mp, 0); 11760Sstevel@tonic-gate if (!(swap32(lockw, 0) & WAITERMASK)) /* still no waiters */ 11770Sstevel@tonic-gate return (0); 11780Sstevel@tonic-gate no_preempt(self); /* ensure a prompt wakeup */ 11790Sstevel@tonic-gate lwpid = mutex_wakeup(mp); 11800Sstevel@tonic-gate } else { 11810Sstevel@tonic-gate no_preempt(self); /* ensure a prompt wakeup */ 11820Sstevel@tonic-gate lockp = (volatile uint8_t *)&mp->mutex_lockw; 11830Sstevel@tonic-gate spinp = (volatile uint32_t *)&mp->mutex_spinners; 11840Sstevel@tonic-gate mp->mutex_owner = 0; 11850Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__release, mp, 0); 11860Sstevel@tonic-gate (void) swap32(lockw, WAITER); /* clear lock, retain waiter */ 11870Sstevel@tonic-gate 11880Sstevel@tonic-gate /* 11890Sstevel@tonic-gate * We spin here fewer times than mutex_trylock_adaptive(). 11900Sstevel@tonic-gate * We are trying to balance two conflicting goals: 11910Sstevel@tonic-gate * 1. Avoid waking up anyone if a spinning thread 11920Sstevel@tonic-gate * grabs the lock. 11930Sstevel@tonic-gate * 2. Wake up a sleeping thread promptly to get on 11940Sstevel@tonic-gate * with useful work. 11950Sstevel@tonic-gate * We don't spin at all if there is no acquiring spinner; 11960Sstevel@tonic-gate * (mp->mutex_spinners is non-zero if there are spinners). 11970Sstevel@tonic-gate */ 11980Sstevel@tonic-gate for (count = self->ul_release_spin; 11990Sstevel@tonic-gate *spinp && count > 0; count--) { 12000Sstevel@tonic-gate /* 12010Sstevel@tonic-gate * There is a waiter that we will have to wake 12020Sstevel@tonic-gate * up unless someone else grabs the lock while 12030Sstevel@tonic-gate * we are busy spinning. Like the spin loop in 12040Sstevel@tonic-gate * mutex_trylock_adaptive(), this spin loop is 12050Sstevel@tonic-gate * unfair to lwps that have already dropped into 12060Sstevel@tonic-gate * the kernel to sleep. They will starve on a 12070Sstevel@tonic-gate * highly-contended mutex. Too bad. 12080Sstevel@tonic-gate */ 12090Sstevel@tonic-gate if (*lockp != 0) { /* somebody grabbed the lock */ 12100Sstevel@tonic-gate preempt(self); 12110Sstevel@tonic-gate return (0); 12120Sstevel@tonic-gate } 12130Sstevel@tonic-gate SMT_PAUSE(); 12140Sstevel@tonic-gate } 12150Sstevel@tonic-gate 12160Sstevel@tonic-gate /* 12170Sstevel@tonic-gate * No one grabbed the lock. 12180Sstevel@tonic-gate * Wake up some lwp that is waiting for it. 12190Sstevel@tonic-gate */ 12200Sstevel@tonic-gate mp->mutex_waiters = 0; 12210Sstevel@tonic-gate lwpid = mutex_wakeup(mp); 12220Sstevel@tonic-gate } 12230Sstevel@tonic-gate 12240Sstevel@tonic-gate if (lwpid == 0) 12250Sstevel@tonic-gate preempt(self); 12260Sstevel@tonic-gate return (lwpid); 12270Sstevel@tonic-gate } 12280Sstevel@tonic-gate 12290Sstevel@tonic-gate /* 12300Sstevel@tonic-gate * Like mutex_unlock_queue(), but for process-shared mutexes. 12310Sstevel@tonic-gate * We tested the waiters field before calling here and it was non-zero. 12320Sstevel@tonic-gate */ 12330Sstevel@tonic-gate void 12340Sstevel@tonic-gate mutex_unlock_process(mutex_t *mp) 12350Sstevel@tonic-gate { 12360Sstevel@tonic-gate ulwp_t *self = curthread; 12370Sstevel@tonic-gate int count; 12380Sstevel@tonic-gate volatile uint8_t *lockp; 12390Sstevel@tonic-gate 12400Sstevel@tonic-gate /* 12410Sstevel@tonic-gate * See the comments in mutex_unlock_queue(), above. 12420Sstevel@tonic-gate */ 12430Sstevel@tonic-gate if ((count = ncpus) == 0) 12440Sstevel@tonic-gate count = ncpus = (int)_sysconf(_SC_NPROCESSORS_ONLN); 12450Sstevel@tonic-gate count = (count > 1)? self->ul_release_spin : 0; 12460Sstevel@tonic-gate no_preempt(self); 12470Sstevel@tonic-gate mp->mutex_owner = 0; 12480Sstevel@tonic-gate mp->mutex_ownerpid = 0; 12490Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__release, mp, 0); 12500Sstevel@tonic-gate if (count == 0) { 12510Sstevel@tonic-gate /* clear lock, test waiter */ 12520Sstevel@tonic-gate if (!(swap32(&mp->mutex_lockword, 0) & WAITERMASK)) { 12530Sstevel@tonic-gate /* no waiters now */ 12540Sstevel@tonic-gate preempt(self); 12550Sstevel@tonic-gate return; 12560Sstevel@tonic-gate } 12570Sstevel@tonic-gate } else { 12580Sstevel@tonic-gate /* clear lock, retain waiter */ 12590Sstevel@tonic-gate (void) swap32(&mp->mutex_lockword, WAITER); 12600Sstevel@tonic-gate lockp = (volatile uint8_t *)&mp->mutex_lockw; 12610Sstevel@tonic-gate while (--count >= 0) { 12620Sstevel@tonic-gate if (*lockp != 0) { 12630Sstevel@tonic-gate /* somebody grabbed the lock */ 12640Sstevel@tonic-gate preempt(self); 12650Sstevel@tonic-gate return; 12660Sstevel@tonic-gate } 12670Sstevel@tonic-gate SMT_PAUSE(); 12680Sstevel@tonic-gate } 12690Sstevel@tonic-gate /* 12700Sstevel@tonic-gate * We must clear the waiters field before going 12710Sstevel@tonic-gate * to the kernel, else it could remain set forever. 12720Sstevel@tonic-gate */ 12730Sstevel@tonic-gate mp->mutex_waiters = 0; 12740Sstevel@tonic-gate } 12750Sstevel@tonic-gate (void) ___lwp_mutex_wakeup(mp); 12760Sstevel@tonic-gate preempt(self); 12770Sstevel@tonic-gate } 12780Sstevel@tonic-gate 12790Sstevel@tonic-gate /* 12800Sstevel@tonic-gate * Return the real priority of a thread. 12810Sstevel@tonic-gate */ 12820Sstevel@tonic-gate int 12830Sstevel@tonic-gate real_priority(ulwp_t *ulwp) 12840Sstevel@tonic-gate { 12850Sstevel@tonic-gate if (ulwp->ul_epri == 0) 12860Sstevel@tonic-gate return (ulwp->ul_mappedpri? ulwp->ul_mappedpri : ulwp->ul_pri); 12870Sstevel@tonic-gate return (ulwp->ul_emappedpri? ulwp->ul_emappedpri : ulwp->ul_epri); 12880Sstevel@tonic-gate } 12890Sstevel@tonic-gate 12900Sstevel@tonic-gate void 12910Sstevel@tonic-gate stall(void) 12920Sstevel@tonic-gate { 12930Sstevel@tonic-gate for (;;) 12940Sstevel@tonic-gate (void) mutex_lock_kernel(&stall_mutex, NULL, NULL); 12950Sstevel@tonic-gate } 12960Sstevel@tonic-gate 12970Sstevel@tonic-gate /* 12980Sstevel@tonic-gate * Acquire a USYNC_THREAD mutex via user-level sleep queues. 12990Sstevel@tonic-gate * We failed set_lock_byte(&mp->mutex_lockw) before coming here. 13000Sstevel@tonic-gate * Returns with mutex_owner set correctly. 13010Sstevel@tonic-gate */ 13020Sstevel@tonic-gate int 13030Sstevel@tonic-gate mutex_lock_queue(ulwp_t *self, tdb_mutex_stats_t *msp, mutex_t *mp, 13040Sstevel@tonic-gate timespec_t *tsp) 13050Sstevel@tonic-gate { 13060Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata; 13070Sstevel@tonic-gate queue_head_t *qp; 13080Sstevel@tonic-gate hrtime_t begin_sleep; 13090Sstevel@tonic-gate int error = 0; 13100Sstevel@tonic-gate 13110Sstevel@tonic-gate self->ul_sp = stkptr(); 13120Sstevel@tonic-gate if (__td_event_report(self, TD_SLEEP, udp)) { 13130Sstevel@tonic-gate self->ul_wchan = mp; 13140Sstevel@tonic-gate self->ul_td_evbuf.eventnum = TD_SLEEP; 13150Sstevel@tonic-gate self->ul_td_evbuf.eventdata = mp; 13160Sstevel@tonic-gate tdb_event(TD_SLEEP, udp); 13170Sstevel@tonic-gate } 13180Sstevel@tonic-gate if (msp) { 13190Sstevel@tonic-gate tdb_incr(msp->mutex_sleep); 13200Sstevel@tonic-gate begin_sleep = gethrtime(); 13210Sstevel@tonic-gate } 13220Sstevel@tonic-gate 13230Sstevel@tonic-gate DTRACE_PROBE1(plockstat, mutex__block, mp); 13240Sstevel@tonic-gate 13250Sstevel@tonic-gate /* 13260Sstevel@tonic-gate * Put ourself on the sleep queue, and while we are 13270Sstevel@tonic-gate * unable to grab the lock, go park in the kernel. 13280Sstevel@tonic-gate * Take ourself off the sleep queue after we acquire the lock. 13290Sstevel@tonic-gate * The waiter bit can be set/cleared only while holding the queue lock. 13300Sstevel@tonic-gate */ 13310Sstevel@tonic-gate qp = queue_lock(mp, MX); 13320Sstevel@tonic-gate enqueue(qp, self, mp, MX); 13330Sstevel@tonic-gate mp->mutex_waiters = 1; 13340Sstevel@tonic-gate for (;;) { 13350Sstevel@tonic-gate if (set_lock_byte(&mp->mutex_lockw) == 0) { 13360Sstevel@tonic-gate mp->mutex_owner = (uintptr_t)self; 13370Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__blocked, mp, 1); 13380Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0); 13390Sstevel@tonic-gate mp->mutex_waiters = dequeue_self(qp, mp); 13400Sstevel@tonic-gate break; 13410Sstevel@tonic-gate } 13420Sstevel@tonic-gate set_parking_flag(self, 1); 13430Sstevel@tonic-gate queue_unlock(qp); 13440Sstevel@tonic-gate /* 13450Sstevel@tonic-gate * __lwp_park() will return the residual time in tsp 13460Sstevel@tonic-gate * if we are unparked before the timeout expires. 13470Sstevel@tonic-gate */ 13480Sstevel@tonic-gate if ((error = __lwp_park(tsp, 0)) == EINTR) 13490Sstevel@tonic-gate error = 0; 13500Sstevel@tonic-gate set_parking_flag(self, 0); 13510Sstevel@tonic-gate /* 13520Sstevel@tonic-gate * We could have taken a signal or suspended ourself. 13530Sstevel@tonic-gate * If we did, then we removed ourself from the queue. 13540Sstevel@tonic-gate * Someone else may have removed us from the queue 13550Sstevel@tonic-gate * as a consequence of mutex_unlock(). We may have 13560Sstevel@tonic-gate * gotten a timeout from __lwp_park(). Or we may still 13570Sstevel@tonic-gate * be on the queue and this is just a spurious wakeup. 13580Sstevel@tonic-gate */ 13590Sstevel@tonic-gate qp = queue_lock(mp, MX); 13600Sstevel@tonic-gate if (self->ul_sleepq == NULL) { 13610Sstevel@tonic-gate if (error) { 13620Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__blocked, mp, 0); 13630Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__error, mp, 13640Sstevel@tonic-gate error); 13650Sstevel@tonic-gate break; 13660Sstevel@tonic-gate } 13670Sstevel@tonic-gate if (set_lock_byte(&mp->mutex_lockw) == 0) { 13680Sstevel@tonic-gate mp->mutex_owner = (uintptr_t)self; 13690Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__blocked, mp, 1); 13700Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 13710Sstevel@tonic-gate 0, 0); 13720Sstevel@tonic-gate break; 13730Sstevel@tonic-gate } 13740Sstevel@tonic-gate enqueue(qp, self, mp, MX); 13750Sstevel@tonic-gate mp->mutex_waiters = 1; 13760Sstevel@tonic-gate } 13770Sstevel@tonic-gate ASSERT(self->ul_sleepq == qp && 13780Sstevel@tonic-gate self->ul_qtype == MX && 13790Sstevel@tonic-gate self->ul_wchan == mp); 13800Sstevel@tonic-gate if (error) { 13810Sstevel@tonic-gate mp->mutex_waiters = dequeue_self(qp, mp); 13820Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__blocked, mp, 0); 13830Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__error, mp, error); 13840Sstevel@tonic-gate break; 13850Sstevel@tonic-gate } 13860Sstevel@tonic-gate } 13870Sstevel@tonic-gate 13880Sstevel@tonic-gate ASSERT(self->ul_sleepq == NULL && self->ul_link == NULL && 13890Sstevel@tonic-gate self->ul_wchan == NULL); 13900Sstevel@tonic-gate self->ul_sp = 0; 13910Sstevel@tonic-gate 13920Sstevel@tonic-gate queue_unlock(qp); 13930Sstevel@tonic-gate if (msp) 13940Sstevel@tonic-gate msp->mutex_sleep_time += gethrtime() - begin_sleep; 13950Sstevel@tonic-gate 13960Sstevel@tonic-gate ASSERT(error == 0 || error == EINVAL || error == ETIME); 13970Sstevel@tonic-gate return (error); 13980Sstevel@tonic-gate } 13990Sstevel@tonic-gate 14000Sstevel@tonic-gate /* 14010Sstevel@tonic-gate * Returns with mutex_owner set correctly. 14020Sstevel@tonic-gate */ 14030Sstevel@tonic-gate int 14040Sstevel@tonic-gate mutex_lock_internal(mutex_t *mp, timespec_t *tsp, int try) 14050Sstevel@tonic-gate { 14060Sstevel@tonic-gate ulwp_t *self = curthread; 14070Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 14080Sstevel@tonic-gate int mtype = mp->mutex_type; 14090Sstevel@tonic-gate tdb_mutex_stats_t *msp = MUTEX_STATS(mp, udp); 14100Sstevel@tonic-gate int error = 0; 14110Sstevel@tonic-gate 14120Sstevel@tonic-gate ASSERT(try == MUTEX_TRY || try == MUTEX_LOCK); 14130Sstevel@tonic-gate 14140Sstevel@tonic-gate if (!self->ul_schedctl_called) 14150Sstevel@tonic-gate (void) setup_schedctl(); 14160Sstevel@tonic-gate 14170Sstevel@tonic-gate if (msp && try == MUTEX_TRY) 14180Sstevel@tonic-gate tdb_incr(msp->mutex_try); 14190Sstevel@tonic-gate 14200Sstevel@tonic-gate if ((mtype & (LOCK_RECURSIVE|LOCK_ERRORCHECK)) && mutex_is_held(mp)) { 14210Sstevel@tonic-gate if (mtype & LOCK_RECURSIVE) { 14220Sstevel@tonic-gate if (mp->mutex_rcount == RECURSION_MAX) { 14230Sstevel@tonic-gate error = EAGAIN; 14240Sstevel@tonic-gate } else { 14250Sstevel@tonic-gate mp->mutex_rcount++; 14260Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 14270Sstevel@tonic-gate 1, 0); 14280Sstevel@tonic-gate return (0); 14290Sstevel@tonic-gate } 14300Sstevel@tonic-gate } else if (try == MUTEX_TRY) { 14310Sstevel@tonic-gate return (EBUSY); 14320Sstevel@tonic-gate } else { 14330Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__error, mp, EDEADLK); 14340Sstevel@tonic-gate return (EDEADLK); 14350Sstevel@tonic-gate } 14360Sstevel@tonic-gate } 14370Sstevel@tonic-gate 14380Sstevel@tonic-gate if (self->ul_error_detection && try == MUTEX_LOCK && 14390Sstevel@tonic-gate tsp == NULL && mutex_is_held(mp)) 14400Sstevel@tonic-gate lock_error(mp, "mutex_lock", NULL, NULL); 14410Sstevel@tonic-gate 14420Sstevel@tonic-gate if (mtype & 14430Sstevel@tonic-gate (USYNC_PROCESS_ROBUST|PTHREAD_PRIO_INHERIT|PTHREAD_PRIO_PROTECT)) { 14440Sstevel@tonic-gate uint8_t ceil; 14450Sstevel@tonic-gate int myprio; 14460Sstevel@tonic-gate 14470Sstevel@tonic-gate if (mtype & PTHREAD_PRIO_PROTECT) { 14480Sstevel@tonic-gate ceil = mp->mutex_ceiling; 14490Sstevel@tonic-gate ASSERT(_validate_rt_prio(SCHED_FIFO, ceil) == 0); 14500Sstevel@tonic-gate myprio = real_priority(self); 14510Sstevel@tonic-gate if (myprio > ceil) { 14520Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__error, mp, 14530Sstevel@tonic-gate EINVAL); 14540Sstevel@tonic-gate return (EINVAL); 14550Sstevel@tonic-gate } 14560Sstevel@tonic-gate if ((error = _ceil_mylist_add(mp)) != 0) { 14570Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__error, mp, 14580Sstevel@tonic-gate error); 14590Sstevel@tonic-gate return (error); 14600Sstevel@tonic-gate } 14610Sstevel@tonic-gate if (myprio < ceil) 14620Sstevel@tonic-gate _ceil_prio_inherit(ceil); 14630Sstevel@tonic-gate } 14640Sstevel@tonic-gate 14650Sstevel@tonic-gate if (mtype & PTHREAD_PRIO_INHERIT) { 14660Sstevel@tonic-gate /* go straight to the kernel */ 14670Sstevel@tonic-gate if (try == MUTEX_TRY) 14680Sstevel@tonic-gate error = mutex_trylock_kernel(mp); 14690Sstevel@tonic-gate else /* MUTEX_LOCK */ 14700Sstevel@tonic-gate error = mutex_lock_kernel(mp, tsp, msp); 14710Sstevel@tonic-gate /* 14720Sstevel@tonic-gate * The kernel never sets or clears the lock byte 14730Sstevel@tonic-gate * for PTHREAD_PRIO_INHERIT mutexes. 14740Sstevel@tonic-gate * Set it here for debugging consistency. 14750Sstevel@tonic-gate */ 14760Sstevel@tonic-gate switch (error) { 14770Sstevel@tonic-gate case 0: 14780Sstevel@tonic-gate case EOWNERDEAD: 14790Sstevel@tonic-gate mp->mutex_lockw = LOCKSET; 14800Sstevel@tonic-gate break; 14810Sstevel@tonic-gate } 14820Sstevel@tonic-gate } else if (mtype & USYNC_PROCESS_ROBUST) { 14830Sstevel@tonic-gate /* go straight to the kernel */ 14840Sstevel@tonic-gate if (try == MUTEX_TRY) 14850Sstevel@tonic-gate error = mutex_trylock_kernel(mp); 14860Sstevel@tonic-gate else /* MUTEX_LOCK */ 14870Sstevel@tonic-gate error = mutex_lock_kernel(mp, tsp, msp); 14880Sstevel@tonic-gate } else { /* PTHREAD_PRIO_PROTECT */ 14890Sstevel@tonic-gate /* 14900Sstevel@tonic-gate * Try once at user level before going to the kernel. 14910Sstevel@tonic-gate * If this is a process shared mutex then protect 14920Sstevel@tonic-gate * against forkall() while setting mp->mutex_ownerpid. 14930Sstevel@tonic-gate */ 14940Sstevel@tonic-gate if (mtype & (USYNC_PROCESS | USYNC_PROCESS_ROBUST)) { 14950Sstevel@tonic-gate enter_critical(self); 14960Sstevel@tonic-gate if (set_lock_byte(&mp->mutex_lockw) == 0) { 14970Sstevel@tonic-gate mp->mutex_owner = (uintptr_t)self; 14980Sstevel@tonic-gate mp->mutex_ownerpid = udp->pid; 14990Sstevel@tonic-gate exit_critical(self); 15000Sstevel@tonic-gate DTRACE_PROBE3(plockstat, 15010Sstevel@tonic-gate mutex__acquire, mp, 0, 0); 15020Sstevel@tonic-gate } else { 15030Sstevel@tonic-gate exit_critical(self); 15040Sstevel@tonic-gate error = EBUSY; 15050Sstevel@tonic-gate } 15060Sstevel@tonic-gate } else { 15070Sstevel@tonic-gate if (set_lock_byte(&mp->mutex_lockw) == 0) { 15080Sstevel@tonic-gate mp->mutex_owner = (uintptr_t)self; 15090Sstevel@tonic-gate DTRACE_PROBE3(plockstat, 15100Sstevel@tonic-gate mutex__acquire, mp, 0, 0); 15110Sstevel@tonic-gate } else { 15120Sstevel@tonic-gate error = EBUSY; 15130Sstevel@tonic-gate } 15140Sstevel@tonic-gate } 15150Sstevel@tonic-gate if (error && try == MUTEX_LOCK) 15160Sstevel@tonic-gate error = mutex_lock_kernel(mp, tsp, msp); 15170Sstevel@tonic-gate } 15180Sstevel@tonic-gate 15190Sstevel@tonic-gate if (error) { 15200Sstevel@tonic-gate if (mtype & PTHREAD_PRIO_INHERIT) { 15210Sstevel@tonic-gate switch (error) { 15220Sstevel@tonic-gate case EOWNERDEAD: 15230Sstevel@tonic-gate case ENOTRECOVERABLE: 15240Sstevel@tonic-gate if (mtype & PTHREAD_MUTEX_ROBUST_NP) 15250Sstevel@tonic-gate break; 15260Sstevel@tonic-gate if (error == EOWNERDEAD) { 15270Sstevel@tonic-gate /* 15280Sstevel@tonic-gate * We own the mutex; unlock it. 15290Sstevel@tonic-gate * It becomes ENOTRECOVERABLE. 15300Sstevel@tonic-gate * All waiters are waked up. 15310Sstevel@tonic-gate */ 15320Sstevel@tonic-gate mp->mutex_owner = 0; 15330Sstevel@tonic-gate mp->mutex_ownerpid = 0; 15340Sstevel@tonic-gate DTRACE_PROBE2(plockstat, 15350Sstevel@tonic-gate mutex__release, mp, 0); 15360Sstevel@tonic-gate mp->mutex_lockw = LOCKCLEAR; 15370Sstevel@tonic-gate (void) ___lwp_mutex_unlock(mp); 15380Sstevel@tonic-gate } 15390Sstevel@tonic-gate /* FALLTHROUGH */ 15400Sstevel@tonic-gate case EDEADLK: 15410Sstevel@tonic-gate if (try == MUTEX_LOCK) 15420Sstevel@tonic-gate stall(); 15430Sstevel@tonic-gate error = EBUSY; 15440Sstevel@tonic-gate break; 15450Sstevel@tonic-gate } 15460Sstevel@tonic-gate } 15470Sstevel@tonic-gate if ((mtype & PTHREAD_PRIO_PROTECT) && 15480Sstevel@tonic-gate error != EOWNERDEAD) { 15490Sstevel@tonic-gate (void) _ceil_mylist_del(mp); 15500Sstevel@tonic-gate if (myprio < ceil) 15510Sstevel@tonic-gate _ceil_prio_waive(); 15520Sstevel@tonic-gate } 15530Sstevel@tonic-gate } 15540Sstevel@tonic-gate } else if (mtype & USYNC_PROCESS) { 15550Sstevel@tonic-gate /* 15560Sstevel@tonic-gate * This is a process shared mutex. Protect against 15570Sstevel@tonic-gate * forkall() while setting mp->mutex_ownerpid. 15580Sstevel@tonic-gate */ 15590Sstevel@tonic-gate enter_critical(self); 15600Sstevel@tonic-gate if (set_lock_byte(&mp->mutex_lockw) == 0) { 15610Sstevel@tonic-gate mp->mutex_owner = (uintptr_t)self; 15620Sstevel@tonic-gate mp->mutex_ownerpid = udp->pid; 15630Sstevel@tonic-gate exit_critical(self); 15640Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0); 15650Sstevel@tonic-gate } else { 15660Sstevel@tonic-gate /* try a little harder */ 15670Sstevel@tonic-gate exit_critical(self); 15680Sstevel@tonic-gate error = mutex_trylock_process(mp); 15690Sstevel@tonic-gate } 15700Sstevel@tonic-gate if (error && try == MUTEX_LOCK) 15710Sstevel@tonic-gate error = mutex_lock_kernel(mp, tsp, msp); 15720Sstevel@tonic-gate } else { /* USYNC_THREAD */ 15730Sstevel@tonic-gate /* try once */ 15740Sstevel@tonic-gate if (set_lock_byte(&mp->mutex_lockw) == 0) { 15750Sstevel@tonic-gate mp->mutex_owner = (uintptr_t)self; 15760Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0); 15770Sstevel@tonic-gate } else { 15780Sstevel@tonic-gate /* try a little harder if we don't own the mutex */ 15790Sstevel@tonic-gate error = EBUSY; 15800Sstevel@tonic-gate if (MUTEX_OWNER(mp) != self) 15810Sstevel@tonic-gate error = mutex_trylock_adaptive(mp); 15820Sstevel@tonic-gate if (error && try == MUTEX_LOCK) /* go park */ 15830Sstevel@tonic-gate error = mutex_lock_queue(self, msp, mp, tsp); 15840Sstevel@tonic-gate } 15850Sstevel@tonic-gate } 15860Sstevel@tonic-gate 15870Sstevel@tonic-gate switch (error) { 15880Sstevel@tonic-gate case EOWNERDEAD: 15890Sstevel@tonic-gate case ELOCKUNMAPPED: 15900Sstevel@tonic-gate mp->mutex_owner = (uintptr_t)self; 15910Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0); 15920Sstevel@tonic-gate /* FALLTHROUGH */ 15930Sstevel@tonic-gate case 0: 15940Sstevel@tonic-gate if (msp) 15950Sstevel@tonic-gate record_begin_hold(msp); 15960Sstevel@tonic-gate break; 15970Sstevel@tonic-gate default: 15980Sstevel@tonic-gate if (try == MUTEX_TRY) { 15990Sstevel@tonic-gate if (msp) 16000Sstevel@tonic-gate tdb_incr(msp->mutex_try_fail); 16010Sstevel@tonic-gate if (__td_event_report(self, TD_LOCK_TRY, udp)) { 16020Sstevel@tonic-gate self->ul_td_evbuf.eventnum = TD_LOCK_TRY; 16030Sstevel@tonic-gate tdb_event(TD_LOCK_TRY, udp); 16040Sstevel@tonic-gate } 16050Sstevel@tonic-gate } 16060Sstevel@tonic-gate break; 16070Sstevel@tonic-gate } 16080Sstevel@tonic-gate 16090Sstevel@tonic-gate return (error); 16100Sstevel@tonic-gate } 16110Sstevel@tonic-gate 16120Sstevel@tonic-gate int 16130Sstevel@tonic-gate fast_process_lock(mutex_t *mp, timespec_t *tsp, int mtype, int try) 16140Sstevel@tonic-gate { 16150Sstevel@tonic-gate ulwp_t *self = curthread; 16160Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 16170Sstevel@tonic-gate 16180Sstevel@tonic-gate /* 16190Sstevel@tonic-gate * We know that USYNC_PROCESS is set in mtype and that 16200Sstevel@tonic-gate * zero, one, or both of the flags LOCK_RECURSIVE and 16210Sstevel@tonic-gate * LOCK_ERRORCHECK are set, and that no other flags are set. 16220Sstevel@tonic-gate */ 16230Sstevel@tonic-gate enter_critical(self); 16240Sstevel@tonic-gate if (set_lock_byte(&mp->mutex_lockw) == 0) { 16250Sstevel@tonic-gate mp->mutex_owner = (uintptr_t)self; 16260Sstevel@tonic-gate mp->mutex_ownerpid = udp->pid; 16270Sstevel@tonic-gate exit_critical(self); 16280Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0); 16290Sstevel@tonic-gate return (0); 16300Sstevel@tonic-gate } 16310Sstevel@tonic-gate exit_critical(self); 16320Sstevel@tonic-gate 16330Sstevel@tonic-gate if ((mtype & ~USYNC_PROCESS) && shared_mutex_held(mp)) { 16340Sstevel@tonic-gate if (mtype & LOCK_RECURSIVE) { 16350Sstevel@tonic-gate if (mp->mutex_rcount == RECURSION_MAX) 16360Sstevel@tonic-gate return (EAGAIN); 16370Sstevel@tonic-gate mp->mutex_rcount++; 16380Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 1, 0); 16390Sstevel@tonic-gate return (0); 16400Sstevel@tonic-gate } 16410Sstevel@tonic-gate if (try == MUTEX_LOCK) { 16420Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__error, mp, EDEADLK); 16430Sstevel@tonic-gate return (EDEADLK); 16440Sstevel@tonic-gate } 16450Sstevel@tonic-gate return (EBUSY); 16460Sstevel@tonic-gate } 16470Sstevel@tonic-gate 16480Sstevel@tonic-gate /* try a little harder if we don't own the mutex */ 16490Sstevel@tonic-gate if (!shared_mutex_held(mp) && mutex_trylock_process(mp) == 0) 16500Sstevel@tonic-gate return (0); 16510Sstevel@tonic-gate 16520Sstevel@tonic-gate if (try == MUTEX_LOCK) 16530Sstevel@tonic-gate return (mutex_lock_kernel(mp, tsp, NULL)); 16540Sstevel@tonic-gate 16550Sstevel@tonic-gate if (__td_event_report(self, TD_LOCK_TRY, udp)) { 16560Sstevel@tonic-gate self->ul_td_evbuf.eventnum = TD_LOCK_TRY; 16570Sstevel@tonic-gate tdb_event(TD_LOCK_TRY, udp); 16580Sstevel@tonic-gate } 16590Sstevel@tonic-gate return (EBUSY); 16600Sstevel@tonic-gate } 16610Sstevel@tonic-gate 16620Sstevel@tonic-gate static int 16630Sstevel@tonic-gate slow_lock(ulwp_t *self, mutex_t *mp, timespec_t *tsp) 16640Sstevel@tonic-gate { 16650Sstevel@tonic-gate int error = 0; 16660Sstevel@tonic-gate 16670Sstevel@tonic-gate if (MUTEX_OWNER(mp) == self || mutex_trylock_adaptive(mp) != 0) 16680Sstevel@tonic-gate error = mutex_lock_queue(self, NULL, mp, tsp); 16690Sstevel@tonic-gate return (error); 16700Sstevel@tonic-gate } 16710Sstevel@tonic-gate 16720Sstevel@tonic-gate int 16730Sstevel@tonic-gate mutex_lock_impl(mutex_t *mp, timespec_t *tsp) 16740Sstevel@tonic-gate { 16750Sstevel@tonic-gate ulwp_t *self = curthread; 16760Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 16770Sstevel@tonic-gate uberflags_t *gflags; 16780Sstevel@tonic-gate int mtype; 16790Sstevel@tonic-gate 16800Sstevel@tonic-gate /* 16810Sstevel@tonic-gate * Optimize the case of USYNC_THREAD, including 16820Sstevel@tonic-gate * the LOCK_RECURSIVE and LOCK_ERRORCHECK cases, 16830Sstevel@tonic-gate * no error detection, no lock statistics, 16840Sstevel@tonic-gate * and the process has only a single thread. 16850Sstevel@tonic-gate * (Most likely a traditional single-threaded application.) 16860Sstevel@tonic-gate */ 16870Sstevel@tonic-gate if ((((mtype = mp->mutex_type) & ~(LOCK_RECURSIVE|LOCK_ERRORCHECK)) | 16880Sstevel@tonic-gate udp->uberflags.uf_all) == 0) { 16890Sstevel@tonic-gate /* 16900Sstevel@tonic-gate * Only one thread exists so we don't need an atomic operation. 16910Sstevel@tonic-gate */ 16920Sstevel@tonic-gate if (mp->mutex_lockw == 0) { 16930Sstevel@tonic-gate mp->mutex_lockw = LOCKSET; 16940Sstevel@tonic-gate mp->mutex_owner = (uintptr_t)self; 16950Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0); 16960Sstevel@tonic-gate return (0); 16970Sstevel@tonic-gate } 16980Sstevel@tonic-gate if (mtype && MUTEX_OWNER(mp) == self) { 16990Sstevel@tonic-gate /* 17000Sstevel@tonic-gate * LOCK_RECURSIVE, LOCK_ERRORCHECK, or both. 17010Sstevel@tonic-gate */ 17020Sstevel@tonic-gate if (mtype & LOCK_RECURSIVE) { 17030Sstevel@tonic-gate if (mp->mutex_rcount == RECURSION_MAX) 17040Sstevel@tonic-gate return (EAGAIN); 17050Sstevel@tonic-gate mp->mutex_rcount++; 17060Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 17070Sstevel@tonic-gate 1, 0); 17080Sstevel@tonic-gate return (0); 17090Sstevel@tonic-gate } 17100Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__error, mp, EDEADLK); 17110Sstevel@tonic-gate return (EDEADLK); /* LOCK_ERRORCHECK */ 17120Sstevel@tonic-gate } 17130Sstevel@tonic-gate /* 17140Sstevel@tonic-gate * We have reached a deadlock, probably because the 17150Sstevel@tonic-gate * process is executing non-async-signal-safe code in 17160Sstevel@tonic-gate * a signal handler and is attempting to acquire a lock 17170Sstevel@tonic-gate * that it already owns. This is not surprising, given 17180Sstevel@tonic-gate * bad programming practices over the years that has 17190Sstevel@tonic-gate * resulted in applications calling printf() and such 17200Sstevel@tonic-gate * in their signal handlers. Unless the user has told 17210Sstevel@tonic-gate * us that the signal handlers are safe by setting: 17220Sstevel@tonic-gate * export _THREAD_ASYNC_SAFE=1 17230Sstevel@tonic-gate * we return EDEADLK rather than actually deadlocking. 17240Sstevel@tonic-gate */ 17250Sstevel@tonic-gate if (tsp == NULL && 17260Sstevel@tonic-gate MUTEX_OWNER(mp) == self && !self->ul_async_safe) { 17270Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__error, mp, EDEADLK); 17280Sstevel@tonic-gate return (EDEADLK); 17290Sstevel@tonic-gate } 17300Sstevel@tonic-gate } 17310Sstevel@tonic-gate 17320Sstevel@tonic-gate /* 17330Sstevel@tonic-gate * Optimize the common cases of USYNC_THREAD or USYNC_PROCESS, 17340Sstevel@tonic-gate * no error detection, and no lock statistics. 17350Sstevel@tonic-gate * Include LOCK_RECURSIVE and LOCK_ERRORCHECK cases. 17360Sstevel@tonic-gate */ 17370Sstevel@tonic-gate if ((gflags = self->ul_schedctl_called) != NULL && 17380Sstevel@tonic-gate (gflags->uf_trs_ted | 17390Sstevel@tonic-gate (mtype & ~(USYNC_PROCESS|LOCK_RECURSIVE|LOCK_ERRORCHECK))) == 0) { 17400Sstevel@tonic-gate 17410Sstevel@tonic-gate if (mtype & USYNC_PROCESS) 17420Sstevel@tonic-gate return (fast_process_lock(mp, tsp, mtype, MUTEX_LOCK)); 17430Sstevel@tonic-gate 17440Sstevel@tonic-gate if (set_lock_byte(&mp->mutex_lockw) == 0) { 17450Sstevel@tonic-gate mp->mutex_owner = (uintptr_t)self; 17460Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0); 17470Sstevel@tonic-gate return (0); 17480Sstevel@tonic-gate } 17490Sstevel@tonic-gate 17500Sstevel@tonic-gate if (mtype && MUTEX_OWNER(mp) == self) { 17510Sstevel@tonic-gate if (mtype & LOCK_RECURSIVE) { 17520Sstevel@tonic-gate if (mp->mutex_rcount == RECURSION_MAX) 17530Sstevel@tonic-gate return (EAGAIN); 17540Sstevel@tonic-gate mp->mutex_rcount++; 17550Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 17560Sstevel@tonic-gate 1, 0); 17570Sstevel@tonic-gate return (0); 17580Sstevel@tonic-gate } 17590Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__error, mp, EDEADLK); 17600Sstevel@tonic-gate return (EDEADLK); /* LOCK_ERRORCHECK */ 17610Sstevel@tonic-gate } 17620Sstevel@tonic-gate 17630Sstevel@tonic-gate return (slow_lock(self, mp, tsp)); 17640Sstevel@tonic-gate } 17650Sstevel@tonic-gate 17660Sstevel@tonic-gate /* else do it the long way */ 17670Sstevel@tonic-gate return (mutex_lock_internal(mp, tsp, MUTEX_LOCK)); 17680Sstevel@tonic-gate } 17690Sstevel@tonic-gate 17700Sstevel@tonic-gate #pragma weak _private_mutex_lock = __mutex_lock 17710Sstevel@tonic-gate #pragma weak mutex_lock = __mutex_lock 17720Sstevel@tonic-gate #pragma weak _mutex_lock = __mutex_lock 17730Sstevel@tonic-gate #pragma weak pthread_mutex_lock = __mutex_lock 17740Sstevel@tonic-gate #pragma weak _pthread_mutex_lock = __mutex_lock 17750Sstevel@tonic-gate int 17760Sstevel@tonic-gate __mutex_lock(mutex_t *mp) 17770Sstevel@tonic-gate { 17780Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 17790Sstevel@tonic-gate return (mutex_lock_impl(mp, NULL)); 17800Sstevel@tonic-gate } 17810Sstevel@tonic-gate 17820Sstevel@tonic-gate #pragma weak pthread_mutex_timedlock = _pthread_mutex_timedlock 17830Sstevel@tonic-gate int 17840Sstevel@tonic-gate _pthread_mutex_timedlock(mutex_t *mp, const timespec_t *abstime) 17850Sstevel@tonic-gate { 17860Sstevel@tonic-gate timespec_t tslocal; 17870Sstevel@tonic-gate int error; 17880Sstevel@tonic-gate 17890Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 17900Sstevel@tonic-gate abstime_to_reltime(CLOCK_REALTIME, abstime, &tslocal); 17910Sstevel@tonic-gate error = mutex_lock_impl(mp, &tslocal); 17920Sstevel@tonic-gate if (error == ETIME) 17930Sstevel@tonic-gate error = ETIMEDOUT; 17940Sstevel@tonic-gate return (error); 17950Sstevel@tonic-gate } 17960Sstevel@tonic-gate 17970Sstevel@tonic-gate #pragma weak pthread_mutex_reltimedlock_np = _pthread_mutex_reltimedlock_np 17980Sstevel@tonic-gate int 17990Sstevel@tonic-gate _pthread_mutex_reltimedlock_np(mutex_t *mp, const timespec_t *reltime) 18000Sstevel@tonic-gate { 18010Sstevel@tonic-gate timespec_t tslocal; 18020Sstevel@tonic-gate int error; 18030Sstevel@tonic-gate 18040Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 18050Sstevel@tonic-gate tslocal = *reltime; 18060Sstevel@tonic-gate error = mutex_lock_impl(mp, &tslocal); 18070Sstevel@tonic-gate if (error == ETIME) 18080Sstevel@tonic-gate error = ETIMEDOUT; 18090Sstevel@tonic-gate return (error); 18100Sstevel@tonic-gate } 18110Sstevel@tonic-gate 18120Sstevel@tonic-gate static int 18130Sstevel@tonic-gate slow_trylock(mutex_t *mp, ulwp_t *self) 18140Sstevel@tonic-gate { 18150Sstevel@tonic-gate if (MUTEX_OWNER(mp) == self || 18160Sstevel@tonic-gate mutex_trylock_adaptive(mp) != 0) { 18170Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 18180Sstevel@tonic-gate 18190Sstevel@tonic-gate if (__td_event_report(self, TD_LOCK_TRY, udp)) { 18200Sstevel@tonic-gate self->ul_td_evbuf.eventnum = TD_LOCK_TRY; 18210Sstevel@tonic-gate tdb_event(TD_LOCK_TRY, udp); 18220Sstevel@tonic-gate } 18230Sstevel@tonic-gate return (EBUSY); 18240Sstevel@tonic-gate } 18250Sstevel@tonic-gate return (0); 18260Sstevel@tonic-gate } 18270Sstevel@tonic-gate 18280Sstevel@tonic-gate #pragma weak _private_mutex_trylock = __mutex_trylock 18290Sstevel@tonic-gate #pragma weak mutex_trylock = __mutex_trylock 18300Sstevel@tonic-gate #pragma weak _mutex_trylock = __mutex_trylock 18310Sstevel@tonic-gate #pragma weak pthread_mutex_trylock = __mutex_trylock 18320Sstevel@tonic-gate #pragma weak _pthread_mutex_trylock = __mutex_trylock 18330Sstevel@tonic-gate int 18340Sstevel@tonic-gate __mutex_trylock(mutex_t *mp) 18350Sstevel@tonic-gate { 18360Sstevel@tonic-gate ulwp_t *self = curthread; 18370Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 18380Sstevel@tonic-gate uberflags_t *gflags; 18390Sstevel@tonic-gate int mtype; 18400Sstevel@tonic-gate 18410Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 18420Sstevel@tonic-gate /* 18430Sstevel@tonic-gate * Optimize the case of USYNC_THREAD, including 18440Sstevel@tonic-gate * the LOCK_RECURSIVE and LOCK_ERRORCHECK cases, 18450Sstevel@tonic-gate * no error detection, no lock statistics, 18460Sstevel@tonic-gate * and the process has only a single thread. 18470Sstevel@tonic-gate * (Most likely a traditional single-threaded application.) 18480Sstevel@tonic-gate */ 18490Sstevel@tonic-gate if ((((mtype = mp->mutex_type) & ~(LOCK_RECURSIVE|LOCK_ERRORCHECK)) | 18500Sstevel@tonic-gate udp->uberflags.uf_all) == 0) { 18510Sstevel@tonic-gate /* 18520Sstevel@tonic-gate * Only one thread exists so we don't need an atomic operation. 18530Sstevel@tonic-gate */ 18540Sstevel@tonic-gate if (mp->mutex_lockw == 0) { 18550Sstevel@tonic-gate mp->mutex_lockw = LOCKSET; 18560Sstevel@tonic-gate mp->mutex_owner = (uintptr_t)self; 18570Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0); 18580Sstevel@tonic-gate return (0); 18590Sstevel@tonic-gate } 18600Sstevel@tonic-gate if (mtype && MUTEX_OWNER(mp) == self) { 18610Sstevel@tonic-gate if (mtype & LOCK_RECURSIVE) { 18620Sstevel@tonic-gate if (mp->mutex_rcount == RECURSION_MAX) 18630Sstevel@tonic-gate return (EAGAIN); 18640Sstevel@tonic-gate mp->mutex_rcount++; 18650Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 18660Sstevel@tonic-gate 1, 0); 18670Sstevel@tonic-gate return (0); 18680Sstevel@tonic-gate } 18690Sstevel@tonic-gate return (EDEADLK); /* LOCK_ERRORCHECK */ 18700Sstevel@tonic-gate } 18710Sstevel@tonic-gate return (EBUSY); 18720Sstevel@tonic-gate } 18730Sstevel@tonic-gate 18740Sstevel@tonic-gate /* 18750Sstevel@tonic-gate * Optimize the common cases of USYNC_THREAD or USYNC_PROCESS, 18760Sstevel@tonic-gate * no error detection, and no lock statistics. 18770Sstevel@tonic-gate * Include LOCK_RECURSIVE and LOCK_ERRORCHECK cases. 18780Sstevel@tonic-gate */ 18790Sstevel@tonic-gate if ((gflags = self->ul_schedctl_called) != NULL && 18800Sstevel@tonic-gate (gflags->uf_trs_ted | 18810Sstevel@tonic-gate (mtype & ~(USYNC_PROCESS|LOCK_RECURSIVE|LOCK_ERRORCHECK))) == 0) { 18820Sstevel@tonic-gate 18830Sstevel@tonic-gate if (mtype & USYNC_PROCESS) 18840Sstevel@tonic-gate return (fast_process_lock(mp, NULL, mtype, MUTEX_TRY)); 18850Sstevel@tonic-gate 18860Sstevel@tonic-gate if (set_lock_byte(&mp->mutex_lockw) == 0) { 18870Sstevel@tonic-gate mp->mutex_owner = (uintptr_t)self; 18880Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0); 18890Sstevel@tonic-gate return (0); 18900Sstevel@tonic-gate } 18910Sstevel@tonic-gate 18920Sstevel@tonic-gate if (mtype && MUTEX_OWNER(mp) == self) { 18930Sstevel@tonic-gate if (mtype & LOCK_RECURSIVE) { 18940Sstevel@tonic-gate if (mp->mutex_rcount == RECURSION_MAX) 18950Sstevel@tonic-gate return (EAGAIN); 18960Sstevel@tonic-gate mp->mutex_rcount++; 18970Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 18980Sstevel@tonic-gate 1, 0); 18990Sstevel@tonic-gate return (0); 19000Sstevel@tonic-gate } 19010Sstevel@tonic-gate return (EBUSY); /* LOCK_ERRORCHECK */ 19020Sstevel@tonic-gate } 19030Sstevel@tonic-gate 19040Sstevel@tonic-gate return (slow_trylock(mp, self)); 19050Sstevel@tonic-gate } 19060Sstevel@tonic-gate 19070Sstevel@tonic-gate /* else do it the long way */ 19080Sstevel@tonic-gate return (mutex_lock_internal(mp, NULL, MUTEX_TRY)); 19090Sstevel@tonic-gate } 19100Sstevel@tonic-gate 19110Sstevel@tonic-gate int 19120Sstevel@tonic-gate mutex_unlock_internal(mutex_t *mp) 19130Sstevel@tonic-gate { 19140Sstevel@tonic-gate ulwp_t *self = curthread; 19150Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 19160Sstevel@tonic-gate int mtype = mp->mutex_type; 19170Sstevel@tonic-gate tdb_mutex_stats_t *msp; 19180Sstevel@tonic-gate int error; 19190Sstevel@tonic-gate lwpid_t lwpid; 19200Sstevel@tonic-gate 19210Sstevel@tonic-gate if ((mtype & LOCK_ERRORCHECK) && !mutex_is_held(mp)) 19220Sstevel@tonic-gate return (EPERM); 19230Sstevel@tonic-gate 19240Sstevel@tonic-gate if (self->ul_error_detection && !mutex_is_held(mp)) 19250Sstevel@tonic-gate lock_error(mp, "mutex_unlock", NULL, NULL); 19260Sstevel@tonic-gate 19270Sstevel@tonic-gate if ((mtype & LOCK_RECURSIVE) && mp->mutex_rcount != 0) { 19280Sstevel@tonic-gate mp->mutex_rcount--; 19290Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__release, mp, 1); 19300Sstevel@tonic-gate return (0); 19310Sstevel@tonic-gate } 19320Sstevel@tonic-gate 19330Sstevel@tonic-gate if ((msp = MUTEX_STATS(mp, udp)) != NULL) 19340Sstevel@tonic-gate (void) record_hold_time(msp); 19350Sstevel@tonic-gate 19360Sstevel@tonic-gate if (mtype & 19370Sstevel@tonic-gate (USYNC_PROCESS_ROBUST|PTHREAD_PRIO_INHERIT|PTHREAD_PRIO_PROTECT)) { 19380Sstevel@tonic-gate no_preempt(self); 19390Sstevel@tonic-gate mp->mutex_owner = 0; 19400Sstevel@tonic-gate mp->mutex_ownerpid = 0; 19410Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__release, mp, 0); 19420Sstevel@tonic-gate if (mtype & PTHREAD_PRIO_INHERIT) { 19430Sstevel@tonic-gate mp->mutex_lockw = LOCKCLEAR; 19440Sstevel@tonic-gate error = ___lwp_mutex_unlock(mp); 19450Sstevel@tonic-gate } else if (mtype & USYNC_PROCESS_ROBUST) { 19460Sstevel@tonic-gate error = ___lwp_mutex_unlock(mp); 19470Sstevel@tonic-gate } else { 19480Sstevel@tonic-gate if (swap32(&mp->mutex_lockword, 0) & WAITERMASK) 19490Sstevel@tonic-gate (void) ___lwp_mutex_wakeup(mp); 19500Sstevel@tonic-gate error = 0; 19510Sstevel@tonic-gate } 19520Sstevel@tonic-gate if (mtype & PTHREAD_PRIO_PROTECT) { 19530Sstevel@tonic-gate if (_ceil_mylist_del(mp)) 19540Sstevel@tonic-gate _ceil_prio_waive(); 19550Sstevel@tonic-gate } 19560Sstevel@tonic-gate preempt(self); 19570Sstevel@tonic-gate } else if (mtype & USYNC_PROCESS) { 19580Sstevel@tonic-gate if (mp->mutex_lockword & WAITERMASK) 19590Sstevel@tonic-gate mutex_unlock_process(mp); 19600Sstevel@tonic-gate else { 19610Sstevel@tonic-gate mp->mutex_owner = 0; 19620Sstevel@tonic-gate mp->mutex_ownerpid = 0; 19630Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__release, mp, 0); 19640Sstevel@tonic-gate if (swap32(&mp->mutex_lockword, 0) & WAITERMASK) { 19650Sstevel@tonic-gate no_preempt(self); 19660Sstevel@tonic-gate (void) ___lwp_mutex_wakeup(mp); 19670Sstevel@tonic-gate preempt(self); 19680Sstevel@tonic-gate } 19690Sstevel@tonic-gate } 19700Sstevel@tonic-gate error = 0; 19710Sstevel@tonic-gate } else { /* USYNC_THREAD */ 19720Sstevel@tonic-gate if ((lwpid = mutex_unlock_queue(mp)) != 0) { 19730Sstevel@tonic-gate (void) __lwp_unpark(lwpid); 19740Sstevel@tonic-gate preempt(self); 19750Sstevel@tonic-gate } 19760Sstevel@tonic-gate error = 0; 19770Sstevel@tonic-gate } 19780Sstevel@tonic-gate 19790Sstevel@tonic-gate return (error); 19800Sstevel@tonic-gate } 19810Sstevel@tonic-gate 19820Sstevel@tonic-gate #pragma weak _private_mutex_unlock = __mutex_unlock 19830Sstevel@tonic-gate #pragma weak mutex_unlock = __mutex_unlock 19840Sstevel@tonic-gate #pragma weak _mutex_unlock = __mutex_unlock 19850Sstevel@tonic-gate #pragma weak pthread_mutex_unlock = __mutex_unlock 19860Sstevel@tonic-gate #pragma weak _pthread_mutex_unlock = __mutex_unlock 19870Sstevel@tonic-gate int 19880Sstevel@tonic-gate __mutex_unlock(mutex_t *mp) 19890Sstevel@tonic-gate { 19900Sstevel@tonic-gate ulwp_t *self = curthread; 19910Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 19920Sstevel@tonic-gate uberflags_t *gflags; 19930Sstevel@tonic-gate lwpid_t lwpid; 19940Sstevel@tonic-gate int mtype; 19950Sstevel@tonic-gate short el; 19960Sstevel@tonic-gate 19970Sstevel@tonic-gate /* 19980Sstevel@tonic-gate * Optimize the case of USYNC_THREAD, including 19990Sstevel@tonic-gate * the LOCK_RECURSIVE and LOCK_ERRORCHECK cases, 20000Sstevel@tonic-gate * no error detection, no lock statistics, 20010Sstevel@tonic-gate * and the process has only a single thread. 20020Sstevel@tonic-gate * (Most likely a traditional single-threaded application.) 20030Sstevel@tonic-gate */ 20040Sstevel@tonic-gate if ((((mtype = mp->mutex_type) & ~(LOCK_RECURSIVE|LOCK_ERRORCHECK)) | 20050Sstevel@tonic-gate udp->uberflags.uf_all) == 0) { 20060Sstevel@tonic-gate if (mtype) { 20070Sstevel@tonic-gate /* 20080Sstevel@tonic-gate * At this point we know that one or both of the 20090Sstevel@tonic-gate * flags LOCK_RECURSIVE or LOCK_ERRORCHECK is set. 20100Sstevel@tonic-gate */ 20110Sstevel@tonic-gate if ((mtype & LOCK_ERRORCHECK) && !MUTEX_OWNED(mp, self)) 20120Sstevel@tonic-gate return (EPERM); 20130Sstevel@tonic-gate if ((mtype & LOCK_RECURSIVE) && mp->mutex_rcount != 0) { 20140Sstevel@tonic-gate mp->mutex_rcount--; 20150Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__release, mp, 1); 20160Sstevel@tonic-gate return (0); 20170Sstevel@tonic-gate } 20180Sstevel@tonic-gate } 20190Sstevel@tonic-gate /* 20200Sstevel@tonic-gate * Only one thread exists so we don't need an atomic operation. 20210Sstevel@tonic-gate * Also, there can be no waiters. 20220Sstevel@tonic-gate */ 20230Sstevel@tonic-gate mp->mutex_owner = 0; 20240Sstevel@tonic-gate mp->mutex_lockword = 0; 20250Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__release, mp, 0); 20260Sstevel@tonic-gate return (0); 20270Sstevel@tonic-gate } 20280Sstevel@tonic-gate 20290Sstevel@tonic-gate /* 20300Sstevel@tonic-gate * Optimize the common cases of USYNC_THREAD or USYNC_PROCESS, 20310Sstevel@tonic-gate * no error detection, and no lock statistics. 20320Sstevel@tonic-gate * Include LOCK_RECURSIVE and LOCK_ERRORCHECK cases. 20330Sstevel@tonic-gate */ 20340Sstevel@tonic-gate if ((gflags = self->ul_schedctl_called) != NULL) { 20350Sstevel@tonic-gate if (((el = gflags->uf_trs_ted) | mtype) == 0) { 20360Sstevel@tonic-gate fast_unlock: 20370Sstevel@tonic-gate if (!(mp->mutex_lockword & WAITERMASK)) { 20380Sstevel@tonic-gate /* no waiter exists right now */ 20390Sstevel@tonic-gate mp->mutex_owner = 0; 20400Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__release, mp, 0); 20410Sstevel@tonic-gate if (swap32(&mp->mutex_lockword, 0) & 20420Sstevel@tonic-gate WAITERMASK) { 20430Sstevel@tonic-gate /* a waiter suddenly appeared */ 20440Sstevel@tonic-gate no_preempt(self); 20450Sstevel@tonic-gate if ((lwpid = mutex_wakeup(mp)) != 0) 20460Sstevel@tonic-gate (void) __lwp_unpark(lwpid); 20470Sstevel@tonic-gate preempt(self); 20480Sstevel@tonic-gate } 20490Sstevel@tonic-gate } else if ((lwpid = mutex_unlock_queue(mp)) != 0) { 20500Sstevel@tonic-gate (void) __lwp_unpark(lwpid); 20510Sstevel@tonic-gate preempt(self); 20520Sstevel@tonic-gate } 20530Sstevel@tonic-gate return (0); 20540Sstevel@tonic-gate } 20550Sstevel@tonic-gate if (el) /* error detection or lock statistics */ 20560Sstevel@tonic-gate goto slow_unlock; 20570Sstevel@tonic-gate if ((mtype & ~(LOCK_RECURSIVE|LOCK_ERRORCHECK)) == 0) { 20580Sstevel@tonic-gate /* 20590Sstevel@tonic-gate * At this point we know that one or both of the 20600Sstevel@tonic-gate * flags LOCK_RECURSIVE or LOCK_ERRORCHECK is set. 20610Sstevel@tonic-gate */ 20620Sstevel@tonic-gate if ((mtype & LOCK_ERRORCHECK) && !MUTEX_OWNED(mp, self)) 20630Sstevel@tonic-gate return (EPERM); 20640Sstevel@tonic-gate if ((mtype & LOCK_RECURSIVE) && mp->mutex_rcount != 0) { 20650Sstevel@tonic-gate mp->mutex_rcount--; 20660Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__release, mp, 1); 20670Sstevel@tonic-gate return (0); 20680Sstevel@tonic-gate } 20690Sstevel@tonic-gate goto fast_unlock; 20700Sstevel@tonic-gate } 20710Sstevel@tonic-gate if ((mtype & 20720Sstevel@tonic-gate ~(USYNC_PROCESS|LOCK_RECURSIVE|LOCK_ERRORCHECK)) == 0) { 20730Sstevel@tonic-gate /* 20740Sstevel@tonic-gate * At this point we know that zero, one, or both of the 20750Sstevel@tonic-gate * flags LOCK_RECURSIVE or LOCK_ERRORCHECK is set and 20760Sstevel@tonic-gate * that the USYNC_PROCESS flag is set. 20770Sstevel@tonic-gate */ 20780Sstevel@tonic-gate if ((mtype & LOCK_ERRORCHECK) && !shared_mutex_held(mp)) 20790Sstevel@tonic-gate return (EPERM); 20800Sstevel@tonic-gate if ((mtype & LOCK_RECURSIVE) && mp->mutex_rcount != 0) { 20810Sstevel@tonic-gate mp->mutex_rcount--; 20820Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__release, mp, 1); 20830Sstevel@tonic-gate return (0); 20840Sstevel@tonic-gate } 20850Sstevel@tonic-gate if (mp->mutex_lockword & WAITERMASK) 20860Sstevel@tonic-gate mutex_unlock_process(mp); 20870Sstevel@tonic-gate else { 20880Sstevel@tonic-gate mp->mutex_owner = 0; 20890Sstevel@tonic-gate mp->mutex_ownerpid = 0; 20900Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__release, mp, 0); 20910Sstevel@tonic-gate if (swap32(&mp->mutex_lockword, 0) & 20920Sstevel@tonic-gate WAITERMASK) { 20930Sstevel@tonic-gate no_preempt(self); 20940Sstevel@tonic-gate (void) ___lwp_mutex_wakeup(mp); 20950Sstevel@tonic-gate preempt(self); 20960Sstevel@tonic-gate } 20970Sstevel@tonic-gate } 20980Sstevel@tonic-gate return (0); 20990Sstevel@tonic-gate } 21000Sstevel@tonic-gate } 21010Sstevel@tonic-gate 21020Sstevel@tonic-gate /* else do it the long way */ 21030Sstevel@tonic-gate slow_unlock: 21040Sstevel@tonic-gate return (mutex_unlock_internal(mp)); 21050Sstevel@tonic-gate } 21060Sstevel@tonic-gate 21070Sstevel@tonic-gate /* 21080Sstevel@tonic-gate * Internally to the library, almost all mutex lock/unlock actions 21090Sstevel@tonic-gate * go through these lmutex_ functions, to protect critical regions. 21100Sstevel@tonic-gate * We replicate a bit of code from __mutex_lock() and __mutex_unlock() 21110Sstevel@tonic-gate * to make these functions faster since we know that the mutex type 21120Sstevel@tonic-gate * of all internal locks is USYNC_THREAD. We also know that internal 21130Sstevel@tonic-gate * locking can never fail, so we panic if it does. 21140Sstevel@tonic-gate */ 21150Sstevel@tonic-gate void 21160Sstevel@tonic-gate lmutex_lock(mutex_t *mp) 21170Sstevel@tonic-gate { 21180Sstevel@tonic-gate ulwp_t *self = curthread; 21190Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 21200Sstevel@tonic-gate 21210Sstevel@tonic-gate ASSERT(mp->mutex_type == USYNC_THREAD); 21220Sstevel@tonic-gate 21230Sstevel@tonic-gate enter_critical(self); 21240Sstevel@tonic-gate /* 21250Sstevel@tonic-gate * Optimize the case of no lock statistics and only a single thread. 21260Sstevel@tonic-gate * (Most likely a traditional single-threaded application.) 21270Sstevel@tonic-gate */ 21280Sstevel@tonic-gate if (udp->uberflags.uf_all == 0) { 21290Sstevel@tonic-gate /* 21300Sstevel@tonic-gate * Only one thread exists; the mutex must be free. 21310Sstevel@tonic-gate */ 21320Sstevel@tonic-gate ASSERT(mp->mutex_lockw == 0); 21330Sstevel@tonic-gate mp->mutex_lockw = LOCKSET; 21340Sstevel@tonic-gate mp->mutex_owner = (uintptr_t)self; 21350Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0); 21360Sstevel@tonic-gate } else { 21370Sstevel@tonic-gate tdb_mutex_stats_t *msp = MUTEX_STATS(mp, udp); 21380Sstevel@tonic-gate 21390Sstevel@tonic-gate if (!self->ul_schedctl_called) 21400Sstevel@tonic-gate (void) setup_schedctl(); 21410Sstevel@tonic-gate 21420Sstevel@tonic-gate if (set_lock_byte(&mp->mutex_lockw) == 0) { 21430Sstevel@tonic-gate mp->mutex_owner = (uintptr_t)self; 21440Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0); 21450Sstevel@tonic-gate } else if (mutex_trylock_adaptive(mp) != 0) { 21460Sstevel@tonic-gate (void) mutex_lock_queue(self, msp, mp, NULL); 21470Sstevel@tonic-gate } 21480Sstevel@tonic-gate 21490Sstevel@tonic-gate if (msp) 21500Sstevel@tonic-gate record_begin_hold(msp); 21510Sstevel@tonic-gate } 21520Sstevel@tonic-gate } 21530Sstevel@tonic-gate 21540Sstevel@tonic-gate void 21550Sstevel@tonic-gate lmutex_unlock(mutex_t *mp) 21560Sstevel@tonic-gate { 21570Sstevel@tonic-gate ulwp_t *self = curthread; 21580Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 21590Sstevel@tonic-gate 21600Sstevel@tonic-gate ASSERT(mp->mutex_type == USYNC_THREAD); 21610Sstevel@tonic-gate 21620Sstevel@tonic-gate /* 21630Sstevel@tonic-gate * Optimize the case of no lock statistics and only a single thread. 21640Sstevel@tonic-gate * (Most likely a traditional single-threaded application.) 21650Sstevel@tonic-gate */ 21660Sstevel@tonic-gate if (udp->uberflags.uf_all == 0) { 21670Sstevel@tonic-gate /* 21680Sstevel@tonic-gate * Only one thread exists so there can be no waiters. 21690Sstevel@tonic-gate */ 21700Sstevel@tonic-gate mp->mutex_owner = 0; 21710Sstevel@tonic-gate mp->mutex_lockword = 0; 21720Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__release, mp, 0); 21730Sstevel@tonic-gate } else { 21740Sstevel@tonic-gate tdb_mutex_stats_t *msp = MUTEX_STATS(mp, udp); 21750Sstevel@tonic-gate lwpid_t lwpid; 21760Sstevel@tonic-gate 21770Sstevel@tonic-gate if (msp) 21780Sstevel@tonic-gate (void) record_hold_time(msp); 21790Sstevel@tonic-gate if ((lwpid = mutex_unlock_queue(mp)) != 0) { 21800Sstevel@tonic-gate (void) __lwp_unpark(lwpid); 21810Sstevel@tonic-gate preempt(self); 21820Sstevel@tonic-gate } 21830Sstevel@tonic-gate } 21840Sstevel@tonic-gate exit_critical(self); 21850Sstevel@tonic-gate } 21860Sstevel@tonic-gate 21870Sstevel@tonic-gate static int 21880Sstevel@tonic-gate shared_mutex_held(mutex_t *mparg) 21890Sstevel@tonic-gate { 21900Sstevel@tonic-gate /* 21910Sstevel@tonic-gate * There is an inherent data race in the current ownership design. 21920Sstevel@tonic-gate * The mutex_owner and mutex_ownerpid fields cannot be set or tested 21930Sstevel@tonic-gate * atomically as a pair. The original implementation tested each 21940Sstevel@tonic-gate * field just once. This was exposed to trivial false positives in 21950Sstevel@tonic-gate * the case of multiple multithreaded processes with thread addresses 21960Sstevel@tonic-gate * in common. To close the window to an acceptable level we now use a 21970Sstevel@tonic-gate * sequence of five tests: pid-thr-pid-thr-pid. This ensures that any 21980Sstevel@tonic-gate * single interruption will still leave one uninterrupted sequence of 21990Sstevel@tonic-gate * pid-thr-pid tests intact. 22000Sstevel@tonic-gate * 22010Sstevel@tonic-gate * It is assumed that all updates are always ordered thr-pid and that 22020Sstevel@tonic-gate * we have TSO hardware. 22030Sstevel@tonic-gate */ 22040Sstevel@tonic-gate volatile mutex_t *mp = (volatile mutex_t *)mparg; 22050Sstevel@tonic-gate ulwp_t *self = curthread; 22060Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 22070Sstevel@tonic-gate 22080Sstevel@tonic-gate if (mp->mutex_ownerpid != udp->pid) 22090Sstevel@tonic-gate return (0); 22100Sstevel@tonic-gate 22110Sstevel@tonic-gate if (!MUTEX_OWNED(mp, self)) 22120Sstevel@tonic-gate return (0); 22130Sstevel@tonic-gate 22140Sstevel@tonic-gate if (mp->mutex_ownerpid != udp->pid) 22150Sstevel@tonic-gate return (0); 22160Sstevel@tonic-gate 22170Sstevel@tonic-gate if (!MUTEX_OWNED(mp, self)) 22180Sstevel@tonic-gate return (0); 22190Sstevel@tonic-gate 22200Sstevel@tonic-gate if (mp->mutex_ownerpid != udp->pid) 22210Sstevel@tonic-gate return (0); 22220Sstevel@tonic-gate 22230Sstevel@tonic-gate return (1); 22240Sstevel@tonic-gate } 22250Sstevel@tonic-gate 22260Sstevel@tonic-gate /* 22270Sstevel@tonic-gate * Some crufty old programs define their own version of _mutex_held() 22280Sstevel@tonic-gate * to be simply return(1). This breaks internal libc logic, so we 22290Sstevel@tonic-gate * define a private version for exclusive use by libc, mutex_is_held(), 22300Sstevel@tonic-gate * and also a new public function, __mutex_held(), to be used in new 22310Sstevel@tonic-gate * code to circumvent these crufty old programs. 22320Sstevel@tonic-gate */ 22330Sstevel@tonic-gate #pragma weak mutex_held = mutex_is_held 22340Sstevel@tonic-gate #pragma weak _mutex_held = mutex_is_held 22350Sstevel@tonic-gate #pragma weak __mutex_held = mutex_is_held 22360Sstevel@tonic-gate int 22370Sstevel@tonic-gate mutex_is_held(mutex_t *mp) 22380Sstevel@tonic-gate { 22390Sstevel@tonic-gate if (mp->mutex_type & (USYNC_PROCESS | USYNC_PROCESS_ROBUST)) 22400Sstevel@tonic-gate return (shared_mutex_held(mp)); 22410Sstevel@tonic-gate return (MUTEX_OWNED(mp, curthread)); 22420Sstevel@tonic-gate } 22430Sstevel@tonic-gate 22440Sstevel@tonic-gate #pragma weak _private_mutex_destroy = __mutex_destroy 22450Sstevel@tonic-gate #pragma weak mutex_destroy = __mutex_destroy 22460Sstevel@tonic-gate #pragma weak _mutex_destroy = __mutex_destroy 22470Sstevel@tonic-gate #pragma weak pthread_mutex_destroy = __mutex_destroy 22480Sstevel@tonic-gate #pragma weak _pthread_mutex_destroy = __mutex_destroy 22490Sstevel@tonic-gate int 22500Sstevel@tonic-gate __mutex_destroy(mutex_t *mp) 22510Sstevel@tonic-gate { 22520Sstevel@tonic-gate mp->mutex_magic = 0; 22530Sstevel@tonic-gate mp->mutex_flag &= ~LOCK_INITED; 22540Sstevel@tonic-gate tdb_sync_obj_deregister(mp); 22550Sstevel@tonic-gate return (0); 22560Sstevel@tonic-gate } 22570Sstevel@tonic-gate 22580Sstevel@tonic-gate /* 22590Sstevel@tonic-gate * Spin locks are separate from ordinary mutexes, 22600Sstevel@tonic-gate * but we use the same data structure for them. 22610Sstevel@tonic-gate */ 22620Sstevel@tonic-gate 22630Sstevel@tonic-gate #pragma weak pthread_spin_init = _pthread_spin_init 22640Sstevel@tonic-gate int 22650Sstevel@tonic-gate _pthread_spin_init(pthread_spinlock_t *lock, int pshared) 22660Sstevel@tonic-gate { 22670Sstevel@tonic-gate mutex_t *mp = (mutex_t *)lock; 22680Sstevel@tonic-gate 22690Sstevel@tonic-gate (void) _memset(mp, 0, sizeof (*mp)); 22700Sstevel@tonic-gate if (pshared == PTHREAD_PROCESS_SHARED) 22710Sstevel@tonic-gate mp->mutex_type = USYNC_PROCESS; 22720Sstevel@tonic-gate else 22730Sstevel@tonic-gate mp->mutex_type = USYNC_THREAD; 22740Sstevel@tonic-gate mp->mutex_flag = LOCK_INITED; 22750Sstevel@tonic-gate mp->mutex_magic = MUTEX_MAGIC; 22760Sstevel@tonic-gate return (0); 22770Sstevel@tonic-gate } 22780Sstevel@tonic-gate 22790Sstevel@tonic-gate #pragma weak pthread_spin_destroy = _pthread_spin_destroy 22800Sstevel@tonic-gate int 22810Sstevel@tonic-gate _pthread_spin_destroy(pthread_spinlock_t *lock) 22820Sstevel@tonic-gate { 22830Sstevel@tonic-gate (void) _memset(lock, 0, sizeof (*lock)); 22840Sstevel@tonic-gate return (0); 22850Sstevel@tonic-gate } 22860Sstevel@tonic-gate 22870Sstevel@tonic-gate #pragma weak pthread_spin_trylock = _pthread_spin_trylock 22880Sstevel@tonic-gate int 22890Sstevel@tonic-gate _pthread_spin_trylock(pthread_spinlock_t *lock) 22900Sstevel@tonic-gate { 22910Sstevel@tonic-gate mutex_t *mp = (mutex_t *)lock; 22920Sstevel@tonic-gate ulwp_t *self = curthread; 22930Sstevel@tonic-gate int error = 0; 22940Sstevel@tonic-gate 22950Sstevel@tonic-gate no_preempt(self); 22960Sstevel@tonic-gate if (set_lock_byte(&mp->mutex_lockw) != 0) 22970Sstevel@tonic-gate error = EBUSY; 22980Sstevel@tonic-gate else { 22990Sstevel@tonic-gate mp->mutex_owner = (uintptr_t)self; 23000Sstevel@tonic-gate if (mp->mutex_type == USYNC_PROCESS) 23010Sstevel@tonic-gate mp->mutex_ownerpid = self->ul_uberdata->pid; 23020Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0); 23030Sstevel@tonic-gate } 23040Sstevel@tonic-gate preempt(self); 23050Sstevel@tonic-gate return (error); 23060Sstevel@tonic-gate } 23070Sstevel@tonic-gate 23080Sstevel@tonic-gate #pragma weak pthread_spin_lock = _pthread_spin_lock 23090Sstevel@tonic-gate int 23100Sstevel@tonic-gate _pthread_spin_lock(pthread_spinlock_t *lock) 23110Sstevel@tonic-gate { 23120Sstevel@tonic-gate volatile uint8_t *lockp = 23130Sstevel@tonic-gate (volatile uint8_t *)&((mutex_t *)lock)->mutex_lockw; 23140Sstevel@tonic-gate 23150Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 23160Sstevel@tonic-gate /* 23170Sstevel@tonic-gate * We don't care whether the owner is running on a processor. 23180Sstevel@tonic-gate * We just spin because that's what this interface requires. 23190Sstevel@tonic-gate */ 23200Sstevel@tonic-gate for (;;) { 23210Sstevel@tonic-gate if (*lockp == 0) { /* lock byte appears to be clear */ 23220Sstevel@tonic-gate if (_pthread_spin_trylock(lock) == 0) 23230Sstevel@tonic-gate return (0); 23240Sstevel@tonic-gate } 23250Sstevel@tonic-gate SMT_PAUSE(); 23260Sstevel@tonic-gate } 23270Sstevel@tonic-gate } 23280Sstevel@tonic-gate 23290Sstevel@tonic-gate #pragma weak pthread_spin_unlock = _pthread_spin_unlock 23300Sstevel@tonic-gate int 23310Sstevel@tonic-gate _pthread_spin_unlock(pthread_spinlock_t *lock) 23320Sstevel@tonic-gate { 23330Sstevel@tonic-gate mutex_t *mp = (mutex_t *)lock; 23340Sstevel@tonic-gate ulwp_t *self = curthread; 23350Sstevel@tonic-gate 23360Sstevel@tonic-gate no_preempt(self); 23370Sstevel@tonic-gate mp->mutex_owner = 0; 23380Sstevel@tonic-gate mp->mutex_ownerpid = 0; 23390Sstevel@tonic-gate DTRACE_PROBE2(plockstat, mutex__release, mp, 0); 23400Sstevel@tonic-gate (void) swap32(&mp->mutex_lockword, 0); 23410Sstevel@tonic-gate preempt(self); 23420Sstevel@tonic-gate return (0); 23430Sstevel@tonic-gate } 23440Sstevel@tonic-gate 23450Sstevel@tonic-gate #pragma weak cond_init = _cond_init 23460Sstevel@tonic-gate /* ARGSUSED2 */ 23470Sstevel@tonic-gate int 23480Sstevel@tonic-gate _cond_init(cond_t *cvp, int type, void *arg) 23490Sstevel@tonic-gate { 23500Sstevel@tonic-gate if (type != USYNC_THREAD && type != USYNC_PROCESS) 23510Sstevel@tonic-gate return (EINVAL); 23520Sstevel@tonic-gate (void) _memset(cvp, 0, sizeof (*cvp)); 23530Sstevel@tonic-gate cvp->cond_type = (uint16_t)type; 23540Sstevel@tonic-gate cvp->cond_magic = COND_MAGIC; 23550Sstevel@tonic-gate return (0); 23560Sstevel@tonic-gate } 23570Sstevel@tonic-gate 23580Sstevel@tonic-gate /* 23590Sstevel@tonic-gate * cond_sleep_queue(): utility function for cond_wait_queue(). 23600Sstevel@tonic-gate * 23610Sstevel@tonic-gate * Go to sleep on a condvar sleep queue, expect to be waked up 23620Sstevel@tonic-gate * by someone calling cond_signal() or cond_broadcast() or due 23630Sstevel@tonic-gate * to receiving a UNIX signal or being cancelled, or just simply 23640Sstevel@tonic-gate * due to a spurious wakeup (like someome calling forkall()). 23650Sstevel@tonic-gate * 23660Sstevel@tonic-gate * The associated mutex is *not* reacquired before returning. 23670Sstevel@tonic-gate * That must be done by the caller of cond_sleep_queue(). 23680Sstevel@tonic-gate */ 23690Sstevel@tonic-gate int 23700Sstevel@tonic-gate cond_sleep_queue(cond_t *cvp, mutex_t *mp, timespec_t *tsp) 23710Sstevel@tonic-gate { 23720Sstevel@tonic-gate ulwp_t *self = curthread; 23730Sstevel@tonic-gate queue_head_t *qp; 23740Sstevel@tonic-gate queue_head_t *mqp; 23750Sstevel@tonic-gate lwpid_t lwpid; 23760Sstevel@tonic-gate int signalled; 23770Sstevel@tonic-gate int error; 23780Sstevel@tonic-gate 23790Sstevel@tonic-gate /* 23800Sstevel@tonic-gate * Put ourself on the CV sleep queue, unlock the mutex, then 23810Sstevel@tonic-gate * park ourself and unpark a candidate lwp to grab the mutex. 23820Sstevel@tonic-gate * We must go onto the CV sleep queue before dropping the 23830Sstevel@tonic-gate * mutex in order to guarantee atomicity of the operation. 23840Sstevel@tonic-gate */ 23850Sstevel@tonic-gate self->ul_sp = stkptr(); 23860Sstevel@tonic-gate qp = queue_lock(cvp, CV); 23870Sstevel@tonic-gate enqueue(qp, self, cvp, CV); 23880Sstevel@tonic-gate cvp->cond_waiters_user = 1; 23890Sstevel@tonic-gate self->ul_cvmutex = mp; 23900Sstevel@tonic-gate self->ul_cv_wake = (tsp != NULL); 23910Sstevel@tonic-gate self->ul_signalled = 0; 23920Sstevel@tonic-gate lwpid = mutex_unlock_queue(mp); 23930Sstevel@tonic-gate for (;;) { 23940Sstevel@tonic-gate set_parking_flag(self, 1); 23950Sstevel@tonic-gate queue_unlock(qp); 23960Sstevel@tonic-gate if (lwpid != 0) { 23970Sstevel@tonic-gate lwpid = preempt_unpark(self, lwpid); 23980Sstevel@tonic-gate preempt(self); 23990Sstevel@tonic-gate } 24000Sstevel@tonic-gate /* 24010Sstevel@tonic-gate * We may have a deferred signal present, 24020Sstevel@tonic-gate * in which case we should return EINTR. 24030Sstevel@tonic-gate * Also, we may have received a SIGCANCEL; if so 24040Sstevel@tonic-gate * and we are cancelable we should return EINTR. 24050Sstevel@tonic-gate * We force an immediate EINTR return from 24060Sstevel@tonic-gate * __lwp_park() by turning our parking flag off. 24070Sstevel@tonic-gate */ 24080Sstevel@tonic-gate if (self->ul_cursig != 0 || 24090Sstevel@tonic-gate (self->ul_cancelable && self->ul_cancel_pending)) 24100Sstevel@tonic-gate set_parking_flag(self, 0); 24110Sstevel@tonic-gate /* 24120Sstevel@tonic-gate * __lwp_park() will return the residual time in tsp 24130Sstevel@tonic-gate * if we are unparked before the timeout expires. 24140Sstevel@tonic-gate */ 24150Sstevel@tonic-gate error = __lwp_park(tsp, lwpid); 24160Sstevel@tonic-gate set_parking_flag(self, 0); 24170Sstevel@tonic-gate lwpid = 0; /* unpark the other lwp only once */ 24180Sstevel@tonic-gate /* 24190Sstevel@tonic-gate * We were waked up by cond_signal(), cond_broadcast(), 24200Sstevel@tonic-gate * by an interrupt or timeout (EINTR or ETIME), 24210Sstevel@tonic-gate * or we may just have gotten a spurious wakeup. 24220Sstevel@tonic-gate */ 24230Sstevel@tonic-gate qp = queue_lock(cvp, CV); 24240Sstevel@tonic-gate mqp = queue_lock(mp, MX); 24250Sstevel@tonic-gate if (self->ul_sleepq == NULL) 24260Sstevel@tonic-gate break; 24270Sstevel@tonic-gate /* 24280Sstevel@tonic-gate * We are on either the condvar sleep queue or the 2429*1893Sraf * mutex sleep queue. Break out of the sleep if we 2430*1893Sraf * were interrupted or we timed out (EINTR or ETIME). 24310Sstevel@tonic-gate * Else this is a spurious wakeup; continue the loop. 24320Sstevel@tonic-gate */ 2433*1893Sraf if (self->ul_sleepq == mqp) { /* mutex queue */ 2434*1893Sraf if (error) { 2435*1893Sraf mp->mutex_waiters = dequeue_self(mqp, mp); 2436*1893Sraf break; 2437*1893Sraf } 2438*1893Sraf tsp = NULL; /* no more timeout */ 2439*1893Sraf } else if (self->ul_sleepq == qp) { /* condvar queue */ 24400Sstevel@tonic-gate if (error) { 24410Sstevel@tonic-gate cvp->cond_waiters_user = dequeue_self(qp, cvp); 24420Sstevel@tonic-gate break; 24430Sstevel@tonic-gate } 24440Sstevel@tonic-gate /* 24450Sstevel@tonic-gate * Else a spurious wakeup on the condvar queue. 24460Sstevel@tonic-gate * __lwp_park() has already adjusted the timeout. 24470Sstevel@tonic-gate */ 24480Sstevel@tonic-gate } else { 24490Sstevel@tonic-gate thr_panic("cond_sleep_queue(): thread not on queue"); 24500Sstevel@tonic-gate } 24510Sstevel@tonic-gate queue_unlock(mqp); 24520Sstevel@tonic-gate } 24530Sstevel@tonic-gate 24540Sstevel@tonic-gate self->ul_sp = 0; 24550Sstevel@tonic-gate ASSERT(self->ul_cvmutex == NULL && self->ul_cv_wake == 0); 24560Sstevel@tonic-gate ASSERT(self->ul_sleepq == NULL && self->ul_link == NULL && 24570Sstevel@tonic-gate self->ul_wchan == NULL); 24580Sstevel@tonic-gate 24590Sstevel@tonic-gate signalled = self->ul_signalled; 24600Sstevel@tonic-gate self->ul_signalled = 0; 24610Sstevel@tonic-gate queue_unlock(qp); 24620Sstevel@tonic-gate queue_unlock(mqp); 24630Sstevel@tonic-gate 24640Sstevel@tonic-gate /* 24650Sstevel@tonic-gate * If we were concurrently cond_signal()d and any of: 24660Sstevel@tonic-gate * received a UNIX signal, were cancelled, or got a timeout, 24670Sstevel@tonic-gate * then perform another cond_signal() to avoid consuming it. 24680Sstevel@tonic-gate */ 24690Sstevel@tonic-gate if (error && signalled) 24700Sstevel@tonic-gate (void) cond_signal_internal(cvp); 24710Sstevel@tonic-gate 24720Sstevel@tonic-gate return (error); 24730Sstevel@tonic-gate } 24740Sstevel@tonic-gate 24750Sstevel@tonic-gate int 24760Sstevel@tonic-gate cond_wait_queue(cond_t *cvp, mutex_t *mp, timespec_t *tsp, 24770Sstevel@tonic-gate tdb_mutex_stats_t *msp) 24780Sstevel@tonic-gate { 24790Sstevel@tonic-gate ulwp_t *self = curthread; 24800Sstevel@tonic-gate int error; 24810Sstevel@tonic-gate 24820Sstevel@tonic-gate /* 24830Sstevel@tonic-gate * The old thread library was programmed to defer signals 24840Sstevel@tonic-gate * while in cond_wait() so that the associated mutex would 24850Sstevel@tonic-gate * be guaranteed to be held when the application signal 24860Sstevel@tonic-gate * handler was invoked. 24870Sstevel@tonic-gate * 24880Sstevel@tonic-gate * We do not behave this way by default; the state of the 24890Sstevel@tonic-gate * associated mutex in the signal handler is undefined. 24900Sstevel@tonic-gate * 24910Sstevel@tonic-gate * To accommodate applications that depend on the old 24920Sstevel@tonic-gate * behavior, the _THREAD_COND_WAIT_DEFER environment 24930Sstevel@tonic-gate * variable can be set to 1 and we will behave in the 24940Sstevel@tonic-gate * old way with respect to cond_wait(). 24950Sstevel@tonic-gate */ 24960Sstevel@tonic-gate if (self->ul_cond_wait_defer) 24970Sstevel@tonic-gate sigoff(self); 24980Sstevel@tonic-gate 24990Sstevel@tonic-gate error = cond_sleep_queue(cvp, mp, tsp); 25000Sstevel@tonic-gate 25010Sstevel@tonic-gate /* 25020Sstevel@tonic-gate * Reacquire the mutex. 25030Sstevel@tonic-gate */ 25040Sstevel@tonic-gate if (set_lock_byte(&mp->mutex_lockw) == 0) { 25050Sstevel@tonic-gate mp->mutex_owner = (uintptr_t)self; 25060Sstevel@tonic-gate DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0); 25070Sstevel@tonic-gate } else if (mutex_trylock_adaptive(mp) != 0) { 25080Sstevel@tonic-gate (void) mutex_lock_queue(self, msp, mp, NULL); 25090Sstevel@tonic-gate } 25100Sstevel@tonic-gate 25110Sstevel@tonic-gate if (msp) 25120Sstevel@tonic-gate record_begin_hold(msp); 25130Sstevel@tonic-gate 25140Sstevel@tonic-gate /* 25150Sstevel@tonic-gate * Take any deferred signal now, after we have reacquired the mutex. 25160Sstevel@tonic-gate */ 25170Sstevel@tonic-gate if (self->ul_cond_wait_defer) 25180Sstevel@tonic-gate sigon(self); 25190Sstevel@tonic-gate 25200Sstevel@tonic-gate return (error); 25210Sstevel@tonic-gate } 25220Sstevel@tonic-gate 25230Sstevel@tonic-gate /* 25240Sstevel@tonic-gate * cond_sleep_kernel(): utility function for cond_wait_kernel(). 25250Sstevel@tonic-gate * See the comment ahead of cond_sleep_queue(), above. 25260Sstevel@tonic-gate */ 25270Sstevel@tonic-gate int 25280Sstevel@tonic-gate cond_sleep_kernel(cond_t *cvp, mutex_t *mp, timespec_t *tsp) 25290Sstevel@tonic-gate { 25300Sstevel@tonic-gate int mtype = mp->mutex_type; 25310Sstevel@tonic-gate ulwp_t *self = curthread; 25320Sstevel@tonic-gate int error; 25330Sstevel@tonic-gate 25340Sstevel@tonic-gate if (mtype & PTHREAD_PRIO_PROTECT) { 25350Sstevel@tonic-gate if (_ceil_mylist_del(mp)) 25360Sstevel@tonic-gate _ceil_prio_waive(); 25370Sstevel@tonic-gate } 25380Sstevel@tonic-gate 25390Sstevel@tonic-gate self->ul_sp = stkptr(); 25400Sstevel@tonic-gate self->ul_wchan = cvp; 25410Sstevel@tonic-gate mp->mutex_owner = 0; 25420Sstevel@tonic-gate mp->mutex_ownerpid = 0; 25430Sstevel@tonic-gate if (mtype & PTHREAD_PRIO_INHERIT) 25440Sstevel@tonic-gate mp->mutex_lockw = LOCKCLEAR; 25450Sstevel@tonic-gate /* 25460Sstevel@tonic-gate * ___lwp_cond_wait() returns immediately with EINTR if 25470Sstevel@tonic-gate * set_parking_flag(self,0) is called on this lwp before it 25480Sstevel@tonic-gate * goes to sleep in the kernel. sigacthandler() calls this 25490Sstevel@tonic-gate * when a deferred signal is noted. This assures that we don't 25500Sstevel@tonic-gate * get stuck in ___lwp_cond_wait() with all signals blocked 25510Sstevel@tonic-gate * due to taking a deferred signal before going to sleep. 25520Sstevel@tonic-gate */ 25530Sstevel@tonic-gate set_parking_flag(self, 1); 25540Sstevel@tonic-gate if (self->ul_cursig != 0 || 25550Sstevel@tonic-gate (self->ul_cancelable && self->ul_cancel_pending)) 25560Sstevel@tonic-gate set_parking_flag(self, 0); 25570Sstevel@tonic-gate error = ___lwp_cond_wait(cvp, mp, tsp, 1); 25580Sstevel@tonic-gate set_parking_flag(self, 0); 25590Sstevel@tonic-gate self->ul_sp = 0; 25600Sstevel@tonic-gate self->ul_wchan = NULL; 25610Sstevel@tonic-gate return (error); 25620Sstevel@tonic-gate } 25630Sstevel@tonic-gate 25640Sstevel@tonic-gate int 25650Sstevel@tonic-gate cond_wait_kernel(cond_t *cvp, mutex_t *mp, timespec_t *tsp) 25660Sstevel@tonic-gate { 25670Sstevel@tonic-gate ulwp_t *self = curthread; 25680Sstevel@tonic-gate int error; 25690Sstevel@tonic-gate int merror; 25700Sstevel@tonic-gate 25710Sstevel@tonic-gate /* 25720Sstevel@tonic-gate * See the large comment in cond_wait_queue(), above. 25730Sstevel@tonic-gate */ 25740Sstevel@tonic-gate if (self->ul_cond_wait_defer) 25750Sstevel@tonic-gate sigoff(self); 25760Sstevel@tonic-gate 25770Sstevel@tonic-gate error = cond_sleep_kernel(cvp, mp, tsp); 25780Sstevel@tonic-gate 25790Sstevel@tonic-gate /* 25800Sstevel@tonic-gate * Override the return code from ___lwp_cond_wait() 25810Sstevel@tonic-gate * with any non-zero return code from mutex_lock(). 25820Sstevel@tonic-gate * This addresses robust lock failures in particular; 25830Sstevel@tonic-gate * the caller must see the EOWNERDEAD or ENOTRECOVERABLE 25840Sstevel@tonic-gate * errors in order to take corrective action. 25850Sstevel@tonic-gate */ 25860Sstevel@tonic-gate if ((merror = _private_mutex_lock(mp)) != 0) 25870Sstevel@tonic-gate error = merror; 25880Sstevel@tonic-gate 25890Sstevel@tonic-gate /* 25900Sstevel@tonic-gate * Take any deferred signal now, after we have reacquired the mutex. 25910Sstevel@tonic-gate */ 25920Sstevel@tonic-gate if (self->ul_cond_wait_defer) 25930Sstevel@tonic-gate sigon(self); 25940Sstevel@tonic-gate 25950Sstevel@tonic-gate return (error); 25960Sstevel@tonic-gate } 25970Sstevel@tonic-gate 25980Sstevel@tonic-gate /* 25990Sstevel@tonic-gate * Common code for _cond_wait() and _cond_timedwait() 26000Sstevel@tonic-gate */ 26010Sstevel@tonic-gate int 26020Sstevel@tonic-gate cond_wait_common(cond_t *cvp, mutex_t *mp, timespec_t *tsp) 26030Sstevel@tonic-gate { 26040Sstevel@tonic-gate int mtype = mp->mutex_type; 26050Sstevel@tonic-gate hrtime_t begin_sleep = 0; 26060Sstevel@tonic-gate ulwp_t *self = curthread; 26070Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 26080Sstevel@tonic-gate tdb_cond_stats_t *csp = COND_STATS(cvp, udp); 26090Sstevel@tonic-gate tdb_mutex_stats_t *msp = MUTEX_STATS(mp, udp); 26100Sstevel@tonic-gate uint8_t rcount; 26110Sstevel@tonic-gate int error = 0; 26120Sstevel@tonic-gate 26130Sstevel@tonic-gate /* 26140Sstevel@tonic-gate * The SUSV3 Posix spec for pthread_cond_timedwait() states: 26150Sstevel@tonic-gate * Except in the case of [ETIMEDOUT], all these error checks 26160Sstevel@tonic-gate * shall act as if they were performed immediately at the 26170Sstevel@tonic-gate * beginning of processing for the function and shall cause 26180Sstevel@tonic-gate * an error return, in effect, prior to modifying the state 26190Sstevel@tonic-gate * of the mutex specified by mutex or the condition variable 26200Sstevel@tonic-gate * specified by cond. 26210Sstevel@tonic-gate * Therefore, we must return EINVAL now if the timout is invalid. 26220Sstevel@tonic-gate */ 26230Sstevel@tonic-gate if (tsp != NULL && 26240Sstevel@tonic-gate (tsp->tv_sec < 0 || (ulong_t)tsp->tv_nsec >= NANOSEC)) 26250Sstevel@tonic-gate return (EINVAL); 26260Sstevel@tonic-gate 26270Sstevel@tonic-gate if (__td_event_report(self, TD_SLEEP, udp)) { 26280Sstevel@tonic-gate self->ul_sp = stkptr(); 26290Sstevel@tonic-gate self->ul_wchan = cvp; 26300Sstevel@tonic-gate self->ul_td_evbuf.eventnum = TD_SLEEP; 26310Sstevel@tonic-gate self->ul_td_evbuf.eventdata = cvp; 26320Sstevel@tonic-gate tdb_event(TD_SLEEP, udp); 26330Sstevel@tonic-gate self->ul_sp = 0; 26340Sstevel@tonic-gate } 26350Sstevel@tonic-gate if (csp) { 26360Sstevel@tonic-gate if (tsp) 26370Sstevel@tonic-gate tdb_incr(csp->cond_timedwait); 26380Sstevel@tonic-gate else 26390Sstevel@tonic-gate tdb_incr(csp->cond_wait); 26400Sstevel@tonic-gate } 26410Sstevel@tonic-gate if (msp) 26420Sstevel@tonic-gate begin_sleep = record_hold_time(msp); 26430Sstevel@tonic-gate else if (csp) 26440Sstevel@tonic-gate begin_sleep = gethrtime(); 26450Sstevel@tonic-gate 26460Sstevel@tonic-gate if (self->ul_error_detection) { 26470Sstevel@tonic-gate if (!mutex_is_held(mp)) 26480Sstevel@tonic-gate lock_error(mp, "cond_wait", cvp, NULL); 26490Sstevel@tonic-gate if ((mtype & LOCK_RECURSIVE) && mp->mutex_rcount != 0) 26500Sstevel@tonic-gate lock_error(mp, "recursive mutex in cond_wait", 26510Sstevel@tonic-gate cvp, NULL); 26520Sstevel@tonic-gate if (cvp->cond_type & USYNC_PROCESS) { 26530Sstevel@tonic-gate if (!(mtype & (USYNC_PROCESS | USYNC_PROCESS_ROBUST))) 26540Sstevel@tonic-gate lock_error(mp, "cond_wait", cvp, 26550Sstevel@tonic-gate "condvar process-shared, " 26560Sstevel@tonic-gate "mutex process-private"); 26570Sstevel@tonic-gate } else { 26580Sstevel@tonic-gate if (mtype & (USYNC_PROCESS | USYNC_PROCESS_ROBUST)) 26590Sstevel@tonic-gate lock_error(mp, "cond_wait", cvp, 26600Sstevel@tonic-gate "condvar process-private, " 26610Sstevel@tonic-gate "mutex process-shared"); 26620Sstevel@tonic-gate } 26630Sstevel@tonic-gate } 26640Sstevel@tonic-gate 26650Sstevel@tonic-gate /* 26660Sstevel@tonic-gate * We deal with recursive mutexes by completely 26670Sstevel@tonic-gate * dropping the lock and restoring the recursion 26680Sstevel@tonic-gate * count after waking up. This is arguably wrong, 26690Sstevel@tonic-gate * but it obeys the principle of least astonishment. 26700Sstevel@tonic-gate */ 26710Sstevel@tonic-gate rcount = mp->mutex_rcount; 26720Sstevel@tonic-gate mp->mutex_rcount = 0; 26730Sstevel@tonic-gate if ((mtype & (USYNC_PROCESS | USYNC_PROCESS_ROBUST | 26740Sstevel@tonic-gate PTHREAD_PRIO_INHERIT | PTHREAD_PRIO_PROTECT)) | 26750Sstevel@tonic-gate (cvp->cond_type & USYNC_PROCESS)) 26760Sstevel@tonic-gate error = cond_wait_kernel(cvp, mp, tsp); 26770Sstevel@tonic-gate else 26780Sstevel@tonic-gate error = cond_wait_queue(cvp, mp, tsp, msp); 26790Sstevel@tonic-gate mp->mutex_rcount = rcount; 26800Sstevel@tonic-gate 26810Sstevel@tonic-gate if (csp) { 26820Sstevel@tonic-gate hrtime_t lapse = gethrtime() - begin_sleep; 26830Sstevel@tonic-gate if (tsp == NULL) 26840Sstevel@tonic-gate csp->cond_wait_sleep_time += lapse; 26850Sstevel@tonic-gate else { 26860Sstevel@tonic-gate csp->cond_timedwait_sleep_time += lapse; 26870Sstevel@tonic-gate if (error == ETIME) 26880Sstevel@tonic-gate tdb_incr(csp->cond_timedwait_timeout); 26890Sstevel@tonic-gate } 26900Sstevel@tonic-gate } 26910Sstevel@tonic-gate return (error); 26920Sstevel@tonic-gate } 26930Sstevel@tonic-gate 26940Sstevel@tonic-gate /* 26950Sstevel@tonic-gate * cond_wait() is a cancellation point but _cond_wait() is not. 26960Sstevel@tonic-gate * System libraries call the non-cancellation version. 26970Sstevel@tonic-gate * It is expected that only applications call the cancellation version. 26980Sstevel@tonic-gate */ 26990Sstevel@tonic-gate int 27000Sstevel@tonic-gate _cond_wait(cond_t *cvp, mutex_t *mp) 27010Sstevel@tonic-gate { 27020Sstevel@tonic-gate ulwp_t *self = curthread; 27030Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 27040Sstevel@tonic-gate uberflags_t *gflags; 27050Sstevel@tonic-gate 27060Sstevel@tonic-gate /* 27070Sstevel@tonic-gate * Optimize the common case of USYNC_THREAD plus 27080Sstevel@tonic-gate * no error detection, no lock statistics, and no event tracing. 27090Sstevel@tonic-gate */ 27100Sstevel@tonic-gate if ((gflags = self->ul_schedctl_called) != NULL && 27110Sstevel@tonic-gate (cvp->cond_type | mp->mutex_type | gflags->uf_trs_ted | 27120Sstevel@tonic-gate self->ul_td_events_enable | 27130Sstevel@tonic-gate udp->tdb.tdb_ev_global_mask.event_bits[0]) == 0) 27140Sstevel@tonic-gate return (cond_wait_queue(cvp, mp, NULL, NULL)); 27150Sstevel@tonic-gate 27160Sstevel@tonic-gate /* 27170Sstevel@tonic-gate * Else do it the long way. 27180Sstevel@tonic-gate */ 27190Sstevel@tonic-gate return (cond_wait_common(cvp, mp, NULL)); 27200Sstevel@tonic-gate } 27210Sstevel@tonic-gate 27220Sstevel@tonic-gate int 27230Sstevel@tonic-gate cond_wait(cond_t *cvp, mutex_t *mp) 27240Sstevel@tonic-gate { 27250Sstevel@tonic-gate int error; 27260Sstevel@tonic-gate 27270Sstevel@tonic-gate _cancelon(); 27280Sstevel@tonic-gate error = _cond_wait(cvp, mp); 27290Sstevel@tonic-gate if (error == EINTR) 27300Sstevel@tonic-gate _canceloff(); 27310Sstevel@tonic-gate else 27320Sstevel@tonic-gate _canceloff_nocancel(); 27330Sstevel@tonic-gate return (error); 27340Sstevel@tonic-gate } 27350Sstevel@tonic-gate 27360Sstevel@tonic-gate #pragma weak pthread_cond_wait = _pthread_cond_wait 27370Sstevel@tonic-gate int 27380Sstevel@tonic-gate _pthread_cond_wait(cond_t *cvp, mutex_t *mp) 27390Sstevel@tonic-gate { 27400Sstevel@tonic-gate int error; 27410Sstevel@tonic-gate 27420Sstevel@tonic-gate error = cond_wait(cvp, mp); 27430Sstevel@tonic-gate return ((error == EINTR)? 0 : error); 27440Sstevel@tonic-gate } 27450Sstevel@tonic-gate 27460Sstevel@tonic-gate /* 27470Sstevel@tonic-gate * cond_timedwait() is a cancellation point but _cond_timedwait() is not. 27480Sstevel@tonic-gate * System libraries call the non-cancellation version. 27490Sstevel@tonic-gate * It is expected that only applications call the cancellation version. 27500Sstevel@tonic-gate */ 27510Sstevel@tonic-gate int 27520Sstevel@tonic-gate _cond_timedwait(cond_t *cvp, mutex_t *mp, const timespec_t *abstime) 27530Sstevel@tonic-gate { 27540Sstevel@tonic-gate clockid_t clock_id = cvp->cond_clockid; 27550Sstevel@tonic-gate timespec_t reltime; 27560Sstevel@tonic-gate int error; 27570Sstevel@tonic-gate 27580Sstevel@tonic-gate if (clock_id != CLOCK_REALTIME && clock_id != CLOCK_HIGHRES) 27590Sstevel@tonic-gate clock_id = CLOCK_REALTIME; 27600Sstevel@tonic-gate abstime_to_reltime(clock_id, abstime, &reltime); 27610Sstevel@tonic-gate error = cond_wait_common(cvp, mp, &reltime); 27620Sstevel@tonic-gate if (error == ETIME && clock_id == CLOCK_HIGHRES) { 27630Sstevel@tonic-gate /* 27640Sstevel@tonic-gate * Don't return ETIME if we didn't really get a timeout. 27650Sstevel@tonic-gate * This can happen if we return because someone resets 27660Sstevel@tonic-gate * the system clock. Just return zero in this case, 27670Sstevel@tonic-gate * giving a spurious wakeup but not a timeout. 27680Sstevel@tonic-gate */ 27690Sstevel@tonic-gate if ((hrtime_t)(uint32_t)abstime->tv_sec * NANOSEC + 27700Sstevel@tonic-gate abstime->tv_nsec > gethrtime()) 27710Sstevel@tonic-gate error = 0; 27720Sstevel@tonic-gate } 27730Sstevel@tonic-gate return (error); 27740Sstevel@tonic-gate } 27750Sstevel@tonic-gate 27760Sstevel@tonic-gate int 27770Sstevel@tonic-gate cond_timedwait(cond_t *cvp, mutex_t *mp, const timespec_t *abstime) 27780Sstevel@tonic-gate { 27790Sstevel@tonic-gate int error; 27800Sstevel@tonic-gate 27810Sstevel@tonic-gate _cancelon(); 27820Sstevel@tonic-gate error = _cond_timedwait(cvp, mp, abstime); 27830Sstevel@tonic-gate if (error == EINTR) 27840Sstevel@tonic-gate _canceloff(); 27850Sstevel@tonic-gate else 27860Sstevel@tonic-gate _canceloff_nocancel(); 27870Sstevel@tonic-gate return (error); 27880Sstevel@tonic-gate } 27890Sstevel@tonic-gate 27900Sstevel@tonic-gate #pragma weak pthread_cond_timedwait = _pthread_cond_timedwait 27910Sstevel@tonic-gate int 27920Sstevel@tonic-gate _pthread_cond_timedwait(cond_t *cvp, mutex_t *mp, const timespec_t *abstime) 27930Sstevel@tonic-gate { 27940Sstevel@tonic-gate int error; 27950Sstevel@tonic-gate 27960Sstevel@tonic-gate error = cond_timedwait(cvp, mp, abstime); 27970Sstevel@tonic-gate if (error == ETIME) 27980Sstevel@tonic-gate error = ETIMEDOUT; 27990Sstevel@tonic-gate else if (error == EINTR) 28000Sstevel@tonic-gate error = 0; 28010Sstevel@tonic-gate return (error); 28020Sstevel@tonic-gate } 28030Sstevel@tonic-gate 28040Sstevel@tonic-gate /* 28050Sstevel@tonic-gate * cond_reltimedwait() is a cancellation point but _cond_reltimedwait() 28060Sstevel@tonic-gate * is not. System libraries call the non-cancellation version. 28070Sstevel@tonic-gate * It is expected that only applications call the cancellation version. 28080Sstevel@tonic-gate */ 28090Sstevel@tonic-gate int 28100Sstevel@tonic-gate _cond_reltimedwait(cond_t *cvp, mutex_t *mp, const timespec_t *reltime) 28110Sstevel@tonic-gate { 28120Sstevel@tonic-gate timespec_t tslocal = *reltime; 28130Sstevel@tonic-gate 28140Sstevel@tonic-gate return (cond_wait_common(cvp, mp, &tslocal)); 28150Sstevel@tonic-gate } 28160Sstevel@tonic-gate 28170Sstevel@tonic-gate #pragma weak cond_reltimedwait = _cond_reltimedwait_cancel 28180Sstevel@tonic-gate int 28190Sstevel@tonic-gate _cond_reltimedwait_cancel(cond_t *cvp, mutex_t *mp, const timespec_t *reltime) 28200Sstevel@tonic-gate { 28210Sstevel@tonic-gate int error; 28220Sstevel@tonic-gate 28230Sstevel@tonic-gate _cancelon(); 28240Sstevel@tonic-gate error = _cond_reltimedwait(cvp, mp, reltime); 28250Sstevel@tonic-gate if (error == EINTR) 28260Sstevel@tonic-gate _canceloff(); 28270Sstevel@tonic-gate else 28280Sstevel@tonic-gate _canceloff_nocancel(); 28290Sstevel@tonic-gate return (error); 28300Sstevel@tonic-gate } 28310Sstevel@tonic-gate 28320Sstevel@tonic-gate #pragma weak pthread_cond_reltimedwait_np = _pthread_cond_reltimedwait_np 28330Sstevel@tonic-gate int 28340Sstevel@tonic-gate _pthread_cond_reltimedwait_np(cond_t *cvp, mutex_t *mp, 28350Sstevel@tonic-gate const timespec_t *reltime) 28360Sstevel@tonic-gate { 28370Sstevel@tonic-gate int error; 28380Sstevel@tonic-gate 28390Sstevel@tonic-gate error = _cond_reltimedwait_cancel(cvp, mp, reltime); 28400Sstevel@tonic-gate if (error == ETIME) 28410Sstevel@tonic-gate error = ETIMEDOUT; 28420Sstevel@tonic-gate else if (error == EINTR) 28430Sstevel@tonic-gate error = 0; 28440Sstevel@tonic-gate return (error); 28450Sstevel@tonic-gate } 28460Sstevel@tonic-gate 28470Sstevel@tonic-gate #pragma weak pthread_cond_signal = cond_signal_internal 28480Sstevel@tonic-gate #pragma weak _pthread_cond_signal = cond_signal_internal 28490Sstevel@tonic-gate #pragma weak cond_signal = cond_signal_internal 28500Sstevel@tonic-gate #pragma weak _cond_signal = cond_signal_internal 28510Sstevel@tonic-gate int 28520Sstevel@tonic-gate cond_signal_internal(cond_t *cvp) 28530Sstevel@tonic-gate { 28540Sstevel@tonic-gate ulwp_t *self = curthread; 28550Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 28560Sstevel@tonic-gate tdb_cond_stats_t *csp = COND_STATS(cvp, udp); 28570Sstevel@tonic-gate int error = 0; 28580Sstevel@tonic-gate queue_head_t *qp; 28590Sstevel@tonic-gate mutex_t *mp; 28600Sstevel@tonic-gate queue_head_t *mqp; 28610Sstevel@tonic-gate ulwp_t **ulwpp; 28620Sstevel@tonic-gate ulwp_t *ulwp; 28630Sstevel@tonic-gate ulwp_t *prev = NULL; 28640Sstevel@tonic-gate ulwp_t *next; 28650Sstevel@tonic-gate ulwp_t **suspp = NULL; 28660Sstevel@tonic-gate ulwp_t *susprev; 28670Sstevel@tonic-gate 28680Sstevel@tonic-gate if (csp) 28690Sstevel@tonic-gate tdb_incr(csp->cond_signal); 28700Sstevel@tonic-gate 28710Sstevel@tonic-gate if (cvp->cond_waiters_kernel) /* someone sleeping in the kernel? */ 28720Sstevel@tonic-gate error = __lwp_cond_signal(cvp); 28730Sstevel@tonic-gate 28740Sstevel@tonic-gate if (!cvp->cond_waiters_user) /* no one sleeping at user-level */ 28750Sstevel@tonic-gate return (error); 28760Sstevel@tonic-gate 28770Sstevel@tonic-gate /* 28780Sstevel@tonic-gate * Move someone from the condvar sleep queue to the mutex sleep 28790Sstevel@tonic-gate * queue for the mutex that he will acquire on being waked up. 28800Sstevel@tonic-gate * We can do this only if we own the mutex he will acquire. 28810Sstevel@tonic-gate * If we do not own the mutex, or if his ul_cv_wake flag 28820Sstevel@tonic-gate * is set, just dequeue and unpark him. 28830Sstevel@tonic-gate */ 28840Sstevel@tonic-gate qp = queue_lock(cvp, CV); 28850Sstevel@tonic-gate for (ulwpp = &qp->qh_head; (ulwp = *ulwpp) != NULL; 28860Sstevel@tonic-gate prev = ulwp, ulwpp = &ulwp->ul_link) { 28870Sstevel@tonic-gate if (ulwp->ul_wchan == cvp) { 28880Sstevel@tonic-gate if (!ulwp->ul_stop) 28890Sstevel@tonic-gate break; 28900Sstevel@tonic-gate /* 28910Sstevel@tonic-gate * Try not to dequeue a suspended thread. 28920Sstevel@tonic-gate * This mimics the old libthread's behavior. 28930Sstevel@tonic-gate */ 28940Sstevel@tonic-gate if (suspp == NULL) { 28950Sstevel@tonic-gate suspp = ulwpp; 28960Sstevel@tonic-gate susprev = prev; 28970Sstevel@tonic-gate } 28980Sstevel@tonic-gate } 28990Sstevel@tonic-gate } 29000Sstevel@tonic-gate if (ulwp == NULL && suspp != NULL) { 29010Sstevel@tonic-gate ulwp = *(ulwpp = suspp); 29020Sstevel@tonic-gate prev = susprev; 29030Sstevel@tonic-gate suspp = NULL; 29040Sstevel@tonic-gate } 29050Sstevel@tonic-gate if (ulwp == NULL) { /* no one on the sleep queue */ 29060Sstevel@tonic-gate cvp->cond_waiters_user = 0; 29070Sstevel@tonic-gate queue_unlock(qp); 29080Sstevel@tonic-gate return (error); 29090Sstevel@tonic-gate } 29100Sstevel@tonic-gate /* 29110Sstevel@tonic-gate * Scan the remainder of the CV queue for another waiter. 29120Sstevel@tonic-gate */ 29130Sstevel@tonic-gate if (suspp != NULL) { 29140Sstevel@tonic-gate next = *suspp; 29150Sstevel@tonic-gate } else { 29160Sstevel@tonic-gate for (next = ulwp->ul_link; next != NULL; next = next->ul_link) 29170Sstevel@tonic-gate if (next->ul_wchan == cvp) 29180Sstevel@tonic-gate break; 29190Sstevel@tonic-gate } 29200Sstevel@tonic-gate if (next == NULL) 29210Sstevel@tonic-gate cvp->cond_waiters_user = 0; 29220Sstevel@tonic-gate 29230Sstevel@tonic-gate /* 29240Sstevel@tonic-gate * Inform the thread that he was the recipient of a cond_signal(). 29250Sstevel@tonic-gate * This lets him deal with cond_signal() and, concurrently, 29260Sstevel@tonic-gate * one or more of a cancellation, a UNIX signal, or a timeout. 29270Sstevel@tonic-gate * These latter conditions must not consume a cond_signal(). 29280Sstevel@tonic-gate */ 29290Sstevel@tonic-gate ulwp->ul_signalled = 1; 29300Sstevel@tonic-gate 29310Sstevel@tonic-gate /* 29320Sstevel@tonic-gate * Dequeue the waiter but leave his ul_sleepq non-NULL 29330Sstevel@tonic-gate * while we move him to the mutex queue so that he can 29340Sstevel@tonic-gate * deal properly with spurious wakeups. 29350Sstevel@tonic-gate */ 29360Sstevel@tonic-gate *ulwpp = ulwp->ul_link; 29370Sstevel@tonic-gate if (qp->qh_tail == ulwp) 29380Sstevel@tonic-gate qp->qh_tail = prev; 29390Sstevel@tonic-gate qp->qh_qlen--; 29400Sstevel@tonic-gate ulwp->ul_link = NULL; 29410Sstevel@tonic-gate 29420Sstevel@tonic-gate mp = ulwp->ul_cvmutex; /* the mutex he will acquire */ 29430Sstevel@tonic-gate ulwp->ul_cvmutex = NULL; 29440Sstevel@tonic-gate ASSERT(mp != NULL); 29450Sstevel@tonic-gate 29460Sstevel@tonic-gate if (ulwp->ul_cv_wake || !MUTEX_OWNED(mp, self)) { 29470Sstevel@tonic-gate lwpid_t lwpid = ulwp->ul_lwpid; 29480Sstevel@tonic-gate 29490Sstevel@tonic-gate no_preempt(self); 29500Sstevel@tonic-gate ulwp->ul_sleepq = NULL; 29510Sstevel@tonic-gate ulwp->ul_wchan = NULL; 29520Sstevel@tonic-gate ulwp->ul_cv_wake = 0; 29530Sstevel@tonic-gate queue_unlock(qp); 29540Sstevel@tonic-gate (void) __lwp_unpark(lwpid); 29550Sstevel@tonic-gate preempt(self); 29560Sstevel@tonic-gate } else { 29570Sstevel@tonic-gate mqp = queue_lock(mp, MX); 29580Sstevel@tonic-gate enqueue(mqp, ulwp, mp, MX); 29590Sstevel@tonic-gate mp->mutex_waiters = 1; 29600Sstevel@tonic-gate queue_unlock(mqp); 29610Sstevel@tonic-gate queue_unlock(qp); 29620Sstevel@tonic-gate } 29630Sstevel@tonic-gate 29640Sstevel@tonic-gate return (error); 29650Sstevel@tonic-gate } 29660Sstevel@tonic-gate 29670Sstevel@tonic-gate #define MAXLWPS 128 /* max remembered lwpids before overflow */ 29680Sstevel@tonic-gate #define NEWLWPS 2048 /* max remembered lwpids at first overflow */ 29690Sstevel@tonic-gate 29700Sstevel@tonic-gate #pragma weak pthread_cond_broadcast = cond_broadcast_internal 29710Sstevel@tonic-gate #pragma weak _pthread_cond_broadcast = cond_broadcast_internal 29720Sstevel@tonic-gate #pragma weak cond_broadcast = cond_broadcast_internal 29730Sstevel@tonic-gate #pragma weak _cond_broadcast = cond_broadcast_internal 29740Sstevel@tonic-gate int 29750Sstevel@tonic-gate cond_broadcast_internal(cond_t *cvp) 29760Sstevel@tonic-gate { 29770Sstevel@tonic-gate ulwp_t *self = curthread; 29780Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 29790Sstevel@tonic-gate tdb_cond_stats_t *csp = COND_STATS(cvp, udp); 29800Sstevel@tonic-gate int error = 0; 29810Sstevel@tonic-gate queue_head_t *qp; 29820Sstevel@tonic-gate mutex_t *mp; 29830Sstevel@tonic-gate queue_head_t *mqp; 29840Sstevel@tonic-gate mutex_t *mp_cache = NULL; 29850Sstevel@tonic-gate queue_head_t *mqp_cache = NULL; 29860Sstevel@tonic-gate ulwp_t **ulwpp; 29870Sstevel@tonic-gate ulwp_t *ulwp; 29880Sstevel@tonic-gate ulwp_t *prev = NULL; 29890Sstevel@tonic-gate lwpid_t buffer[MAXLWPS]; 29900Sstevel@tonic-gate lwpid_t *lwpid = buffer; 29910Sstevel@tonic-gate int nlwpid = 0; 29920Sstevel@tonic-gate int maxlwps = MAXLWPS; 29930Sstevel@tonic-gate 29940Sstevel@tonic-gate if (csp) 29950Sstevel@tonic-gate tdb_incr(csp->cond_broadcast); 29960Sstevel@tonic-gate 29970Sstevel@tonic-gate if (cvp->cond_waiters_kernel) /* someone sleeping in the kernel? */ 29980Sstevel@tonic-gate error = __lwp_cond_broadcast(cvp); 29990Sstevel@tonic-gate 30000Sstevel@tonic-gate if (!cvp->cond_waiters_user) /* no one sleeping at user-level */ 30010Sstevel@tonic-gate return (error); 30020Sstevel@tonic-gate 30030Sstevel@tonic-gate /* 30040Sstevel@tonic-gate * Move everyone from the condvar sleep queue to the mutex sleep 30050Sstevel@tonic-gate * queue for the mutex that they will acquire on being waked up. 30060Sstevel@tonic-gate * We can do this only if we own the mutex they will acquire. 30070Sstevel@tonic-gate * If we do not own the mutex, or if their ul_cv_wake flag 30080Sstevel@tonic-gate * is set, just dequeue and unpark them. 30090Sstevel@tonic-gate * 30100Sstevel@tonic-gate * We keep track of lwpids that are to be unparked in lwpid[]. 30110Sstevel@tonic-gate * __lwp_unpark_all() is called to unpark all of them after 30120Sstevel@tonic-gate * they have been removed from the sleep queue and the sleep 30130Sstevel@tonic-gate * queue lock has been dropped. If we run out of space in our 30140Sstevel@tonic-gate * on-stack buffer, we need to allocate more but we can't call 30150Sstevel@tonic-gate * lmalloc() because we are holding a queue lock when the overflow 30160Sstevel@tonic-gate * occurs and lmalloc() acquires a lock. We can't use alloca() 30170Sstevel@tonic-gate * either because the application may have allocated a small stack 30180Sstevel@tonic-gate * and we don't want to overrun the stack. So we use the mmap() 30190Sstevel@tonic-gate * system call directly since that path acquires no locks. 30200Sstevel@tonic-gate */ 30210Sstevel@tonic-gate qp = queue_lock(cvp, CV); 30220Sstevel@tonic-gate cvp->cond_waiters_user = 0; 30230Sstevel@tonic-gate ulwpp = &qp->qh_head; 30240Sstevel@tonic-gate while ((ulwp = *ulwpp) != NULL) { 30250Sstevel@tonic-gate 30260Sstevel@tonic-gate if (ulwp->ul_wchan != cvp) { 30270Sstevel@tonic-gate prev = ulwp; 30280Sstevel@tonic-gate ulwpp = &ulwp->ul_link; 30290Sstevel@tonic-gate continue; 30300Sstevel@tonic-gate } 30310Sstevel@tonic-gate 30320Sstevel@tonic-gate *ulwpp = ulwp->ul_link; 30330Sstevel@tonic-gate if (qp->qh_tail == ulwp) 30340Sstevel@tonic-gate qp->qh_tail = prev; 30350Sstevel@tonic-gate qp->qh_qlen--; 30360Sstevel@tonic-gate ulwp->ul_link = NULL; 30370Sstevel@tonic-gate 30380Sstevel@tonic-gate mp = ulwp->ul_cvmutex; /* his mutex */ 30390Sstevel@tonic-gate ulwp->ul_cvmutex = NULL; 30400Sstevel@tonic-gate ASSERT(mp != NULL); 30410Sstevel@tonic-gate 30420Sstevel@tonic-gate if (ulwp->ul_cv_wake || !MUTEX_OWNED(mp, self)) { 30430Sstevel@tonic-gate ulwp->ul_sleepq = NULL; 30440Sstevel@tonic-gate ulwp->ul_wchan = NULL; 30450Sstevel@tonic-gate ulwp->ul_cv_wake = 0; 30460Sstevel@tonic-gate if (nlwpid == maxlwps) { 30470Sstevel@tonic-gate /* 30480Sstevel@tonic-gate * Allocate NEWLWPS ids on the first overflow. 30490Sstevel@tonic-gate * Double the allocation each time after that. 30500Sstevel@tonic-gate */ 30510Sstevel@tonic-gate int newlwps = (lwpid == buffer)? NEWLWPS : 30520Sstevel@tonic-gate 2 * maxlwps; 30530Sstevel@tonic-gate void *vaddr = _private_mmap(NULL, 30540Sstevel@tonic-gate newlwps * sizeof (lwpid_t), 30550Sstevel@tonic-gate PROT_READ|PROT_WRITE, 30560Sstevel@tonic-gate MAP_PRIVATE|MAP_ANON, -1, (off_t)0); 30570Sstevel@tonic-gate if (vaddr == MAP_FAILED) { 30580Sstevel@tonic-gate /* 30590Sstevel@tonic-gate * Let's hope this never happens. 30600Sstevel@tonic-gate * If it does, then we have a terrible 30610Sstevel@tonic-gate * thundering herd on our hands. 30620Sstevel@tonic-gate */ 30630Sstevel@tonic-gate (void) __lwp_unpark_all(lwpid, nlwpid); 30640Sstevel@tonic-gate nlwpid = 0; 30650Sstevel@tonic-gate } else { 30660Sstevel@tonic-gate (void) _memcpy(vaddr, lwpid, 30670Sstevel@tonic-gate maxlwps * sizeof (lwpid_t)); 30680Sstevel@tonic-gate if (lwpid != buffer) 30690Sstevel@tonic-gate (void) _private_munmap(lwpid, 30700Sstevel@tonic-gate maxlwps * sizeof (lwpid_t)); 30710Sstevel@tonic-gate lwpid = vaddr; 30720Sstevel@tonic-gate maxlwps = newlwps; 30730Sstevel@tonic-gate } 30740Sstevel@tonic-gate } 30750Sstevel@tonic-gate lwpid[nlwpid++] = ulwp->ul_lwpid; 30760Sstevel@tonic-gate } else { 30770Sstevel@tonic-gate if (mp != mp_cache) { 30780Sstevel@tonic-gate if (mqp_cache != NULL) 30790Sstevel@tonic-gate queue_unlock(mqp_cache); 30800Sstevel@tonic-gate mqp_cache = queue_lock(mp, MX); 30810Sstevel@tonic-gate mp_cache = mp; 30820Sstevel@tonic-gate } 30830Sstevel@tonic-gate mqp = mqp_cache; 30840Sstevel@tonic-gate enqueue(mqp, ulwp, mp, MX); 30850Sstevel@tonic-gate mp->mutex_waiters = 1; 30860Sstevel@tonic-gate } 30870Sstevel@tonic-gate } 30880Sstevel@tonic-gate if (mqp_cache != NULL) 30890Sstevel@tonic-gate queue_unlock(mqp_cache); 30900Sstevel@tonic-gate queue_unlock(qp); 30910Sstevel@tonic-gate if (nlwpid) { 30920Sstevel@tonic-gate if (nlwpid == 1) 30930Sstevel@tonic-gate (void) __lwp_unpark(lwpid[0]); 30940Sstevel@tonic-gate else 30950Sstevel@tonic-gate (void) __lwp_unpark_all(lwpid, nlwpid); 30960Sstevel@tonic-gate } 30970Sstevel@tonic-gate if (lwpid != buffer) 30980Sstevel@tonic-gate (void) _private_munmap(lwpid, maxlwps * sizeof (lwpid_t)); 30990Sstevel@tonic-gate 31000Sstevel@tonic-gate return (error); 31010Sstevel@tonic-gate } 31020Sstevel@tonic-gate 31030Sstevel@tonic-gate #pragma weak pthread_cond_destroy = _cond_destroy 31040Sstevel@tonic-gate #pragma weak _pthread_cond_destroy = _cond_destroy 31050Sstevel@tonic-gate #pragma weak cond_destroy = _cond_destroy 31060Sstevel@tonic-gate int 31070Sstevel@tonic-gate _cond_destroy(cond_t *cvp) 31080Sstevel@tonic-gate { 31090Sstevel@tonic-gate cvp->cond_magic = 0; 31100Sstevel@tonic-gate tdb_sync_obj_deregister(cvp); 31110Sstevel@tonic-gate return (0); 31120Sstevel@tonic-gate } 31130Sstevel@tonic-gate 31140Sstevel@tonic-gate #if defined(THREAD_DEBUG) 31150Sstevel@tonic-gate void 31160Sstevel@tonic-gate assert_no_libc_locks_held(void) 31170Sstevel@tonic-gate { 31180Sstevel@tonic-gate ASSERT(!curthread->ul_critical || curthread->ul_bindflags); 31190Sstevel@tonic-gate } 31200Sstevel@tonic-gate #endif 31210Sstevel@tonic-gate 31220Sstevel@tonic-gate /* protected by link_lock */ 31230Sstevel@tonic-gate uint64_t spin_lock_spin; 31240Sstevel@tonic-gate uint64_t spin_lock_spin2; 31250Sstevel@tonic-gate uint64_t spin_lock_sleep; 31260Sstevel@tonic-gate uint64_t spin_lock_wakeup; 31270Sstevel@tonic-gate 31280Sstevel@tonic-gate /* 31290Sstevel@tonic-gate * Record spin lock statistics. 31300Sstevel@tonic-gate * Called by a thread exiting itself in thrp_exit(). 31310Sstevel@tonic-gate * Also called via atexit() from the thread calling 31320Sstevel@tonic-gate * exit() to do all the other threads as well. 31330Sstevel@tonic-gate */ 31340Sstevel@tonic-gate void 31350Sstevel@tonic-gate record_spin_locks(ulwp_t *ulwp) 31360Sstevel@tonic-gate { 31370Sstevel@tonic-gate spin_lock_spin += ulwp->ul_spin_lock_spin; 31380Sstevel@tonic-gate spin_lock_spin2 += ulwp->ul_spin_lock_spin2; 31390Sstevel@tonic-gate spin_lock_sleep += ulwp->ul_spin_lock_sleep; 31400Sstevel@tonic-gate spin_lock_wakeup += ulwp->ul_spin_lock_wakeup; 31410Sstevel@tonic-gate ulwp->ul_spin_lock_spin = 0; 31420Sstevel@tonic-gate ulwp->ul_spin_lock_spin2 = 0; 31430Sstevel@tonic-gate ulwp->ul_spin_lock_sleep = 0; 31440Sstevel@tonic-gate ulwp->ul_spin_lock_wakeup = 0; 31450Sstevel@tonic-gate } 31460Sstevel@tonic-gate 31470Sstevel@tonic-gate /* 31480Sstevel@tonic-gate * atexit function: dump the queue statistics to stderr. 31490Sstevel@tonic-gate */ 31501219Sraf #if !defined(__lint) 31511219Sraf #define fprintf _fprintf 31521219Sraf #endif 31530Sstevel@tonic-gate #include <stdio.h> 31540Sstevel@tonic-gate void 31550Sstevel@tonic-gate dump_queue_statistics(void) 31560Sstevel@tonic-gate { 31570Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata; 31580Sstevel@tonic-gate queue_head_t *qp; 31590Sstevel@tonic-gate int qn; 31600Sstevel@tonic-gate uint64_t spin_lock_total = 0; 31610Sstevel@tonic-gate 31620Sstevel@tonic-gate if (udp->queue_head == NULL || thread_queue_dump == 0) 31630Sstevel@tonic-gate return; 31640Sstevel@tonic-gate 31650Sstevel@tonic-gate if (fprintf(stderr, "\n%5d mutex queues:\n", QHASHSIZE) < 0 || 31660Sstevel@tonic-gate fprintf(stderr, "queue# lockcount max qlen\n") < 0) 31670Sstevel@tonic-gate return; 31680Sstevel@tonic-gate for (qn = 0, qp = udp->queue_head; qn < QHASHSIZE; qn++, qp++) { 31690Sstevel@tonic-gate if (qp->qh_lockcount == 0) 31700Sstevel@tonic-gate continue; 31710Sstevel@tonic-gate spin_lock_total += qp->qh_lockcount; 31720Sstevel@tonic-gate if (fprintf(stderr, "%5d %12llu%12u\n", qn, 31730Sstevel@tonic-gate (u_longlong_t)qp->qh_lockcount, qp->qh_qmax) < 0) 31740Sstevel@tonic-gate return; 31750Sstevel@tonic-gate } 31760Sstevel@tonic-gate 31770Sstevel@tonic-gate if (fprintf(stderr, "\n%5d condvar queues:\n", QHASHSIZE) < 0 || 31780Sstevel@tonic-gate fprintf(stderr, "queue# lockcount max qlen\n") < 0) 31790Sstevel@tonic-gate return; 31800Sstevel@tonic-gate for (qn = 0; qn < QHASHSIZE; qn++, qp++) { 31810Sstevel@tonic-gate if (qp->qh_lockcount == 0) 31820Sstevel@tonic-gate continue; 31830Sstevel@tonic-gate spin_lock_total += qp->qh_lockcount; 31840Sstevel@tonic-gate if (fprintf(stderr, "%5d %12llu%12u\n", qn, 31850Sstevel@tonic-gate (u_longlong_t)qp->qh_lockcount, qp->qh_qmax) < 0) 31860Sstevel@tonic-gate return; 31870Sstevel@tonic-gate } 31880Sstevel@tonic-gate 31890Sstevel@tonic-gate (void) fprintf(stderr, "\n spin_lock_total = %10llu\n", 31900Sstevel@tonic-gate (u_longlong_t)spin_lock_total); 31910Sstevel@tonic-gate (void) fprintf(stderr, " spin_lock_spin = %10llu\n", 31920Sstevel@tonic-gate (u_longlong_t)spin_lock_spin); 31930Sstevel@tonic-gate (void) fprintf(stderr, " spin_lock_spin2 = %10llu\n", 31940Sstevel@tonic-gate (u_longlong_t)spin_lock_spin2); 31950Sstevel@tonic-gate (void) fprintf(stderr, " spin_lock_sleep = %10llu\n", 31960Sstevel@tonic-gate (u_longlong_t)spin_lock_sleep); 31970Sstevel@tonic-gate (void) fprintf(stderr, " spin_lock_wakeup = %10llu\n", 31980Sstevel@tonic-gate (u_longlong_t)spin_lock_wakeup); 31990Sstevel@tonic-gate } 3200