xref: /titanic_52/usr/src/lib/libc/port/threads/synch.c (revision db2bae3047e71d795bde12e3baa621f4b6cc8930)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include "lint.h"
28 #include "thr_uberdata.h"
29 #include <sys/rtpriocntl.h>
30 #include <sys/sdt.h>
31 #include <atomic.h>
32 
33 #if defined(THREAD_DEBUG)
34 #define	INCR32(x)	(((x) != UINT32_MAX)? (x)++ : 0)
35 #define	INCR(x)		((x)++)
36 #define	DECR(x)		((x)--)
37 #define	MAXINCR(m, x)	((m < ++x)? (m = x) : 0)
38 #else
39 #define	INCR32(x)
40 #define	INCR(x)
41 #define	DECR(x)
42 #define	MAXINCR(m, x)
43 #endif
44 
45 /*
46  * This mutex is initialized to be held by lwp#1.
47  * It is used to block a thread that has returned from a mutex_lock()
48  * of a LOCK_PRIO_INHERIT mutex with an unrecoverable error.
49  */
50 mutex_t	stall_mutex = DEFAULTMUTEX;
51 
52 static int shared_mutex_held(mutex_t *);
53 static int mutex_queuelock_adaptive(mutex_t *);
54 static void mutex_wakeup_all(mutex_t *);
55 
56 /*
57  * Lock statistics support functions.
58  */
59 void
60 record_begin_hold(tdb_mutex_stats_t *msp)
61 {
62 	tdb_incr(msp->mutex_lock);
63 	msp->mutex_begin_hold = gethrtime();
64 }
65 
66 hrtime_t
67 record_hold_time(tdb_mutex_stats_t *msp)
68 {
69 	hrtime_t now = gethrtime();
70 
71 	if (msp->mutex_begin_hold)
72 		msp->mutex_hold_time += now - msp->mutex_begin_hold;
73 	msp->mutex_begin_hold = 0;
74 	return (now);
75 }
76 
77 /*
78  * Called once at library initialization.
79  */
80 void
81 mutex_setup(void)
82 {
83 	if (set_lock_byte(&stall_mutex.mutex_lockw))
84 		thr_panic("mutex_setup() cannot acquire stall_mutex");
85 	stall_mutex.mutex_owner = (uintptr_t)curthread;
86 }
87 
88 /*
89  * The default spin count of 1000 is experimentally determined.
90  * On sun4u machines with any number of processors it could be raised
91  * to 10,000 but that (experimentally) makes almost no difference.
92  * The environment variable:
93  *	_THREAD_ADAPTIVE_SPIN=count
94  * can be used to override and set the count in the range [0 .. 1,000,000].
95  */
96 int	thread_adaptive_spin = 1000;
97 uint_t	thread_max_spinners = 100;
98 int	thread_queue_verify = 0;
99 static	int	ncpus;
100 
101 /*
102  * Distinguish spinning for queue locks from spinning for regular locks.
103  * We try harder to acquire queue locks by spinning.
104  * The environment variable:
105  *	_THREAD_QUEUE_SPIN=count
106  * can be used to override and set the count in the range [0 .. 1,000,000].
107  */
108 int	thread_queue_spin = 10000;
109 
110 #define	ALL_ATTRIBUTES				\
111 	(LOCK_RECURSIVE | LOCK_ERRORCHECK |	\
112 	LOCK_PRIO_INHERIT | LOCK_PRIO_PROTECT |	\
113 	LOCK_ROBUST)
114 
115 /*
116  * 'type' can be one of USYNC_THREAD, USYNC_PROCESS, or USYNC_PROCESS_ROBUST,
117  * augmented by zero or more the flags:
118  *	LOCK_RECURSIVE
119  *	LOCK_ERRORCHECK
120  *	LOCK_PRIO_INHERIT
121  *	LOCK_PRIO_PROTECT
122  *	LOCK_ROBUST
123  */
124 #pragma weak _mutex_init = mutex_init
125 /* ARGSUSED2 */
126 int
127 mutex_init(mutex_t *mp, int type, void *arg)
128 {
129 	int basetype = (type & ~ALL_ATTRIBUTES);
130 	const pcclass_t *pccp;
131 	int error = 0;
132 	int ceil;
133 
134 	if (basetype == USYNC_PROCESS_ROBUST) {
135 		/*
136 		 * USYNC_PROCESS_ROBUST is a deprecated historical type.
137 		 * We change it into (USYNC_PROCESS | LOCK_ROBUST) but
138 		 * retain the USYNC_PROCESS_ROBUST flag so we can return
139 		 * ELOCKUNMAPPED when necessary (only USYNC_PROCESS_ROBUST
140 		 * mutexes will ever draw ELOCKUNMAPPED).
141 		 */
142 		type |= (USYNC_PROCESS | LOCK_ROBUST);
143 		basetype = USYNC_PROCESS;
144 	}
145 
146 	if (type & LOCK_PRIO_PROTECT)
147 		pccp = get_info_by_policy(SCHED_FIFO);
148 	if ((basetype != USYNC_THREAD && basetype != USYNC_PROCESS) ||
149 	    (type & (LOCK_PRIO_INHERIT | LOCK_PRIO_PROTECT))
150 	    == (LOCK_PRIO_INHERIT | LOCK_PRIO_PROTECT) ||
151 	    ((type & LOCK_PRIO_PROTECT) &&
152 	    ((ceil = *(int *)arg) < pccp->pcc_primin ||
153 	    ceil > pccp->pcc_primax))) {
154 		error = EINVAL;
155 	} else if (type & LOCK_ROBUST) {
156 		/*
157 		 * Callers of mutex_init() with the LOCK_ROBUST attribute
158 		 * are required to pass an initially all-zero mutex.
159 		 * Multiple calls to mutex_init() are allowed; all but
160 		 * the first return EBUSY.  A call to mutex_init() is
161 		 * allowed to make an inconsistent robust lock consistent
162 		 * (for historical usage, even though the proper interface
163 		 * for this is mutex_consistent()).  Note that we use
164 		 * atomic_or_16() to set the LOCK_INITED flag so as
165 		 * not to disturb surrounding bits (LOCK_OWNERDEAD, etc).
166 		 */
167 		if (!(mp->mutex_flag & LOCK_INITED)) {
168 			mp->mutex_type = (uint8_t)type;
169 			atomic_or_16(&mp->mutex_flag, LOCK_INITED);
170 			mp->mutex_magic = MUTEX_MAGIC;
171 		} else if (type != mp->mutex_type ||
172 		    ((type & LOCK_PRIO_PROTECT) && mp->mutex_ceiling != ceil)) {
173 			error = EINVAL;
174 		} else if (mutex_consistent(mp) != 0) {
175 			error = EBUSY;
176 		}
177 		/* register a process robust mutex with the kernel */
178 		if (basetype == USYNC_PROCESS)
179 			register_lock(mp);
180 	} else {
181 		(void) memset(mp, 0, sizeof (*mp));
182 		mp->mutex_type = (uint8_t)type;
183 		mp->mutex_flag = LOCK_INITED;
184 		mp->mutex_magic = MUTEX_MAGIC;
185 	}
186 
187 	if (error == 0 && (type & LOCK_PRIO_PROTECT)) {
188 		mp->mutex_ceiling = ceil;
189 	}
190 
191 	/*
192 	 * This should be at the beginning of the function,
193 	 * but for the sake of old broken applications that
194 	 * do not have proper alignment for their mutexes
195 	 * (and don't check the return code from mutex_init),
196 	 * we put it here, after initializing the mutex regardless.
197 	 */
198 	if (error == 0 &&
199 	    ((uintptr_t)mp & (_LONG_LONG_ALIGNMENT - 1)) &&
200 	    curthread->ul_misaligned == 0)
201 		error = EINVAL;
202 
203 	return (error);
204 }
205 
206 /*
207  * Delete mp from list of ceiling mutexes owned by curthread.
208  * Return 1 if the head of the chain was updated.
209  */
210 int
211 _ceil_mylist_del(mutex_t *mp)
212 {
213 	ulwp_t *self = curthread;
214 	mxchain_t **mcpp;
215 	mxchain_t *mcp;
216 
217 	for (mcpp = &self->ul_mxchain;
218 	    (mcp = *mcpp) != NULL;
219 	    mcpp = &mcp->mxchain_next) {
220 		if (mcp->mxchain_mx == mp) {
221 			*mcpp = mcp->mxchain_next;
222 			lfree(mcp, sizeof (*mcp));
223 			return (mcpp == &self->ul_mxchain);
224 		}
225 	}
226 	return (0);
227 }
228 
229 /*
230  * Add mp to the list of ceiling mutexes owned by curthread.
231  * Return ENOMEM if no memory could be allocated.
232  */
233 int
234 _ceil_mylist_add(mutex_t *mp)
235 {
236 	ulwp_t *self = curthread;
237 	mxchain_t *mcp;
238 
239 	if ((mcp = lmalloc(sizeof (*mcp))) == NULL)
240 		return (ENOMEM);
241 	mcp->mxchain_mx = mp;
242 	mcp->mxchain_next = self->ul_mxchain;
243 	self->ul_mxchain = mcp;
244 	return (0);
245 }
246 
247 /*
248  * Helper function for _ceil_prio_inherit() and _ceil_prio_waive(), below.
249  */
250 static void
251 set_rt_priority(ulwp_t *self, int prio)
252 {
253 	pcparms_t pcparm;
254 
255 	pcparm.pc_cid = self->ul_rtclassid;
256 	((rtparms_t *)pcparm.pc_clparms)->rt_tqnsecs = RT_NOCHANGE;
257 	((rtparms_t *)pcparm.pc_clparms)->rt_pri = prio;
258 	(void) priocntl(P_LWPID, self->ul_lwpid, PC_SETPARMS, &pcparm);
259 }
260 
261 /*
262  * Inherit priority from ceiling.
263  * This changes the effective priority, not the assigned priority.
264  */
265 void
266 _ceil_prio_inherit(int prio)
267 {
268 	ulwp_t *self = curthread;
269 
270 	self->ul_epri = prio;
271 	set_rt_priority(self, prio);
272 }
273 
274 /*
275  * Waive inherited ceiling priority.  Inherit from head of owned ceiling locks
276  * if holding at least one ceiling lock.  If no ceiling locks are held at this
277  * point, disinherit completely, reverting back to assigned priority.
278  */
279 void
280 _ceil_prio_waive(void)
281 {
282 	ulwp_t *self = curthread;
283 	mxchain_t *mcp = self->ul_mxchain;
284 	int prio;
285 
286 	if (mcp == NULL) {
287 		prio = self->ul_pri;
288 		self->ul_epri = 0;
289 	} else {
290 		prio = mcp->mxchain_mx->mutex_ceiling;
291 		self->ul_epri = prio;
292 	}
293 	set_rt_priority(self, prio);
294 }
295 
296 /*
297  * Clear the lock byte.  Retain the waiters byte and the spinners byte.
298  * Return the old value of the lock word.
299  */
300 static uint32_t
301 clear_lockbyte(volatile uint32_t *lockword)
302 {
303 	uint32_t old;
304 	uint32_t new;
305 
306 	do {
307 		old = *lockword;
308 		new = old & ~LOCKMASK;
309 	} while (atomic_cas_32(lockword, old, new) != old);
310 
311 	return (old);
312 }
313 
314 /*
315  * Same as clear_lockbyte(), but operates on mutex_lockword64.
316  * The mutex_ownerpid field is cleared along with the lock byte.
317  */
318 static uint64_t
319 clear_lockbyte64(volatile uint64_t *lockword64)
320 {
321 	uint64_t old;
322 	uint64_t new;
323 
324 	do {
325 		old = *lockword64;
326 		new = old & ~LOCKMASK64;
327 	} while (atomic_cas_64(lockword64, old, new) != old);
328 
329 	return (old);
330 }
331 
332 /*
333  * Similar to set_lock_byte(), which only tries to set the lock byte.
334  * Here, we attempt to set the lock byte AND the mutex_ownerpid, keeping
335  * the remaining bytes constant.  This atomic operation is required for the
336  * correctness of process-shared robust locks, otherwise there would be
337  * a window or vulnerability in which the lock byte had been set but the
338  * mutex_ownerpid had not yet been set.  If the process were to die in
339  * this window of vulnerability (due to some other thread calling exit()
340  * or the process receiving a fatal signal), the mutex would be left locked
341  * but without a process-ID to determine which process was holding the lock.
342  * The kernel would then be unable to mark the robust mutex as LOCK_OWNERDEAD
343  * when the process died.  For all other cases of process-shared locks, this
344  * operation is just a convenience, for the sake of common code.
345  *
346  * This operation requires process-shared robust locks to be properly
347  * aligned on an 8-byte boundary, at least on sparc machines, lest the
348  * operation incur an alignment fault.  This is automatic when locks
349  * are declared properly using the mutex_t or pthread_mutex_t data types
350  * and the application does not allocate dynamic memory on less than an
351  * 8-byte boundary.  See the 'horrible hack' comments below for cases
352  * dealing with such broken applications.
353  */
354 static int
355 set_lock_byte64(volatile uint64_t *lockword64, pid_t ownerpid)
356 {
357 	uint64_t old;
358 	uint64_t new;
359 
360 	old = *lockword64 & ~LOCKMASK64;
361 	new = old | ((uint64_t)(uint_t)ownerpid << PIDSHIFT) | LOCKBYTE64;
362 	if (atomic_cas_64(lockword64, old, new) == old)
363 		return (LOCKCLEAR);
364 
365 	return (LOCKSET);
366 }
367 
368 /*
369  * Increment the spinners count in the mutex lock word.
370  * Return 0 on success.  Return -1 if the count would overflow.
371  */
372 static int
373 spinners_incr(volatile uint32_t *lockword, uint8_t max_spinners)
374 {
375 	uint32_t old;
376 	uint32_t new;
377 
378 	do {
379 		old = *lockword;
380 		if (((old & SPINNERMASK) >> SPINNERSHIFT) >= max_spinners)
381 			return (-1);
382 		new = old + (1 << SPINNERSHIFT);
383 	} while (atomic_cas_32(lockword, old, new) != old);
384 
385 	return (0);
386 }
387 
388 /*
389  * Decrement the spinners count in the mutex lock word.
390  * Return the new value of the lock word.
391  */
392 static uint32_t
393 spinners_decr(volatile uint32_t *lockword)
394 {
395 	uint32_t old;
396 	uint32_t new;
397 
398 	do {
399 		new = old = *lockword;
400 		if (new & SPINNERMASK)
401 			new -= (1 << SPINNERSHIFT);
402 	} while (atomic_cas_32(lockword, old, new) != old);
403 
404 	return (new);
405 }
406 
407 /*
408  * Non-preemptive spin locks.  Used by queue_lock().
409  * No lock statistics are gathered for these locks.
410  * No DTrace probes are provided for these locks.
411  */
412 void
413 spin_lock_set(mutex_t *mp)
414 {
415 	ulwp_t *self = curthread;
416 
417 	no_preempt(self);
418 	if (set_lock_byte(&mp->mutex_lockw) == 0) {
419 		mp->mutex_owner = (uintptr_t)self;
420 		return;
421 	}
422 	/*
423 	 * Spin for a while, attempting to acquire the lock.
424 	 */
425 	INCR32(self->ul_spin_lock_spin);
426 	if (mutex_queuelock_adaptive(mp) == 0 ||
427 	    set_lock_byte(&mp->mutex_lockw) == 0) {
428 		mp->mutex_owner = (uintptr_t)self;
429 		return;
430 	}
431 	/*
432 	 * Try harder if we were previously at a no premption level.
433 	 */
434 	if (self->ul_preempt > 1) {
435 		INCR32(self->ul_spin_lock_spin2);
436 		if (mutex_queuelock_adaptive(mp) == 0 ||
437 		    set_lock_byte(&mp->mutex_lockw) == 0) {
438 			mp->mutex_owner = (uintptr_t)self;
439 			return;
440 		}
441 	}
442 	/*
443 	 * Give up and block in the kernel for the mutex.
444 	 */
445 	INCR32(self->ul_spin_lock_sleep);
446 	(void) ___lwp_mutex_timedlock(mp, NULL);
447 	mp->mutex_owner = (uintptr_t)self;
448 }
449 
450 void
451 spin_lock_clear(mutex_t *mp)
452 {
453 	ulwp_t *self = curthread;
454 
455 	mp->mutex_owner = 0;
456 	if (atomic_swap_32(&mp->mutex_lockword, 0) & WAITERMASK) {
457 		(void) ___lwp_mutex_wakeup(mp, 0);
458 		INCR32(self->ul_spin_lock_wakeup);
459 	}
460 	preempt(self);
461 }
462 
463 /*
464  * Allocate the sleep queue hash table.
465  */
466 void
467 queue_alloc(void)
468 {
469 	ulwp_t *self = curthread;
470 	uberdata_t *udp = self->ul_uberdata;
471 	queue_head_t *qp;
472 	void *data;
473 	int i;
474 
475 	/*
476 	 * No locks are needed; we call here only when single-threaded.
477 	 */
478 	ASSERT(self == udp->ulwp_one);
479 	ASSERT(!udp->uberflags.uf_mt);
480 	if ((data = mmap(NULL, 2 * QHASHSIZE * sizeof (queue_head_t),
481 	    PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, (off_t)0))
482 	    == MAP_FAILED)
483 		thr_panic("cannot allocate thread queue_head table");
484 	udp->queue_head = qp = (queue_head_t *)data;
485 	for (i = 0; i < 2 * QHASHSIZE; qp++, i++) {
486 		qp->qh_type = (i < QHASHSIZE)? MX : CV;
487 		qp->qh_lock.mutex_flag = LOCK_INITED;
488 		qp->qh_lock.mutex_magic = MUTEX_MAGIC;
489 		qp->qh_hlist = &qp->qh_def_root;
490 #if defined(THREAD_DEBUG)
491 		qp->qh_hlen = 1;
492 		qp->qh_hmax = 1;
493 #endif
494 	}
495 }
496 
497 #if defined(THREAD_DEBUG)
498 
499 /*
500  * Debugging: verify correctness of a sleep queue.
501  */
502 void
503 QVERIFY(queue_head_t *qp)
504 {
505 	ulwp_t *self = curthread;
506 	uberdata_t *udp = self->ul_uberdata;
507 	queue_root_t *qrp;
508 	ulwp_t *ulwp;
509 	ulwp_t *prev;
510 	uint_t index;
511 	uint32_t cnt;
512 	char qtype;
513 	void *wchan;
514 
515 	ASSERT(qp >= udp->queue_head && (qp - udp->queue_head) < 2 * QHASHSIZE);
516 	ASSERT(MUTEX_OWNED(&qp->qh_lock, self));
517 	for (cnt = 0, qrp = qp->qh_hlist; qrp != NULL; qrp = qrp->qr_next) {
518 		cnt++;
519 		ASSERT((qrp->qr_head != NULL && qrp->qr_tail != NULL) ||
520 		    (qrp->qr_head == NULL && qrp->qr_tail == NULL));
521 	}
522 	ASSERT(qp->qh_hlen == cnt && qp->qh_hmax >= cnt);
523 	qtype = ((qp - udp->queue_head) < QHASHSIZE)? MX : CV;
524 	ASSERT(qp->qh_type == qtype);
525 	if (!thread_queue_verify)
526 		return;
527 	/* real expensive stuff, only for _THREAD_QUEUE_VERIFY */
528 	for (cnt = 0, qrp = qp->qh_hlist; qrp != NULL; qrp = qrp->qr_next) {
529 		for (prev = NULL, ulwp = qrp->qr_head; ulwp != NULL;
530 		    prev = ulwp, ulwp = ulwp->ul_link) {
531 			cnt++;
532 			if (ulwp->ul_writer)
533 				ASSERT(prev == NULL || prev->ul_writer);
534 			ASSERT(ulwp->ul_qtype == qtype);
535 			ASSERT(ulwp->ul_wchan != NULL);
536 			ASSERT(ulwp->ul_sleepq == qp);
537 			wchan = ulwp->ul_wchan;
538 			ASSERT(qrp->qr_wchan == wchan);
539 			index = QUEUE_HASH(wchan, qtype);
540 			ASSERT(&udp->queue_head[index] == qp);
541 		}
542 		ASSERT(qrp->qr_tail == prev);
543 	}
544 	ASSERT(qp->qh_qlen == cnt);
545 }
546 
547 #else	/* THREAD_DEBUG */
548 
549 #define	QVERIFY(qp)
550 
551 #endif	/* THREAD_DEBUG */
552 
553 /*
554  * Acquire a queue head.
555  */
556 queue_head_t *
557 queue_lock(void *wchan, int qtype)
558 {
559 	uberdata_t *udp = curthread->ul_uberdata;
560 	queue_head_t *qp;
561 	queue_root_t *qrp;
562 
563 	ASSERT(qtype == MX || qtype == CV);
564 
565 	/*
566 	 * It is possible that we could be called while still single-threaded.
567 	 * If so, we call queue_alloc() to allocate the queue_head[] array.
568 	 */
569 	if ((qp = udp->queue_head) == NULL) {
570 		queue_alloc();
571 		qp = udp->queue_head;
572 	}
573 	qp += QUEUE_HASH(wchan, qtype);
574 	spin_lock_set(&qp->qh_lock);
575 	for (qrp = qp->qh_hlist; qrp != NULL; qrp = qrp->qr_next)
576 		if (qrp->qr_wchan == wchan)
577 			break;
578 	if (qrp == NULL && qp->qh_def_root.qr_head == NULL) {
579 		/* the default queue root is available; use it */
580 		qrp = &qp->qh_def_root;
581 		qrp->qr_wchan = wchan;
582 		ASSERT(qrp->qr_next == NULL);
583 		ASSERT(qrp->qr_tail == NULL &&
584 		    qrp->qr_rtcount == 0 && qrp->qr_qlen == 0);
585 	}
586 	qp->qh_wchan = wchan;	/* valid until queue_unlock() is called */
587 	qp->qh_root = qrp;	/* valid until queue_unlock() is called */
588 	INCR32(qp->qh_lockcount);
589 	QVERIFY(qp);
590 	return (qp);
591 }
592 
593 /*
594  * Release a queue head.
595  */
596 void
597 queue_unlock(queue_head_t *qp)
598 {
599 	QVERIFY(qp);
600 	spin_lock_clear(&qp->qh_lock);
601 }
602 
603 /*
604  * For rwlock queueing, we must queue writers ahead of readers of the
605  * same priority.  We do this by making writers appear to have a half
606  * point higher priority for purposes of priority comparisons below.
607  */
608 #define	CMP_PRIO(ulwp)	((real_priority(ulwp) << 1) + (ulwp)->ul_writer)
609 
610 void
611 enqueue(queue_head_t *qp, ulwp_t *ulwp, int force_fifo)
612 {
613 	queue_root_t *qrp;
614 	ulwp_t **ulwpp;
615 	ulwp_t *next;
616 	int pri = CMP_PRIO(ulwp);
617 
618 	ASSERT(MUTEX_OWNED(&qp->qh_lock, curthread));
619 	ASSERT(ulwp->ul_sleepq != qp);
620 
621 	if ((qrp = qp->qh_root) == NULL) {
622 		/* use the thread's queue root for the linkage */
623 		qrp = &ulwp->ul_queue_root;
624 		qrp->qr_next = qp->qh_hlist;
625 		qrp->qr_prev = NULL;
626 		qrp->qr_head = NULL;
627 		qrp->qr_tail = NULL;
628 		qrp->qr_wchan = qp->qh_wchan;
629 		qrp->qr_rtcount = 0;
630 		qrp->qr_qlen = 0;
631 		qrp->qr_qmax = 0;
632 		qp->qh_hlist->qr_prev = qrp;
633 		qp->qh_hlist = qrp;
634 		qp->qh_root = qrp;
635 		MAXINCR(qp->qh_hmax, qp->qh_hlen);
636 	}
637 
638 	/*
639 	 * LIFO queue ordering is unfair and can lead to starvation,
640 	 * but it gives better performance for heavily contended locks.
641 	 * We use thread_queue_fifo (range is 0..8) to determine
642 	 * the frequency of FIFO vs LIFO queuing:
643 	 *	0 : every 256th time	(almost always LIFO)
644 	 *	1 : every 128th time
645 	 *	2 : every 64th  time
646 	 *	3 : every 32nd  time
647 	 *	4 : every 16th  time	(the default value, mostly LIFO)
648 	 *	5 : every 8th   time
649 	 *	6 : every 4th   time
650 	 *	7 : every 2nd   time
651 	 *	8 : every time		(never LIFO, always FIFO)
652 	 * Note that there is always some degree of FIFO ordering.
653 	 * This breaks live lock conditions that occur in applications
654 	 * that are written assuming (incorrectly) that threads acquire
655 	 * locks fairly, that is, in roughly round-robin order.
656 	 * In any event, the queue is maintained in kernel priority order.
657 	 *
658 	 * If force_fifo is non-zero, fifo queueing is forced.
659 	 * SUSV3 requires this for semaphores.
660 	 */
661 	if (qrp->qr_head == NULL) {
662 		/*
663 		 * The queue is empty.  LIFO/FIFO doesn't matter.
664 		 */
665 		ASSERT(qrp->qr_tail == NULL);
666 		ulwpp = &qrp->qr_head;
667 	} else if (force_fifo |
668 	    (((++qp->qh_qcnt << curthread->ul_queue_fifo) & 0xff) == 0)) {
669 		/*
670 		 * Enqueue after the last thread whose priority is greater
671 		 * than or equal to the priority of the thread being queued.
672 		 * Attempt first to go directly onto the tail of the queue.
673 		 */
674 		if (pri <= CMP_PRIO(qrp->qr_tail))
675 			ulwpp = &qrp->qr_tail->ul_link;
676 		else {
677 			for (ulwpp = &qrp->qr_head; (next = *ulwpp) != NULL;
678 			    ulwpp = &next->ul_link)
679 				if (pri > CMP_PRIO(next))
680 					break;
681 		}
682 	} else {
683 		/*
684 		 * Enqueue before the first thread whose priority is less
685 		 * than or equal to the priority of the thread being queued.
686 		 * Hopefully we can go directly onto the head of the queue.
687 		 */
688 		for (ulwpp = &qrp->qr_head; (next = *ulwpp) != NULL;
689 		    ulwpp = &next->ul_link)
690 			if (pri >= CMP_PRIO(next))
691 				break;
692 	}
693 	if ((ulwp->ul_link = *ulwpp) == NULL)
694 		qrp->qr_tail = ulwp;
695 	*ulwpp = ulwp;
696 
697 	ulwp->ul_sleepq = qp;
698 	ulwp->ul_wchan = qp->qh_wchan;
699 	ulwp->ul_qtype = qp->qh_type;
700 	if ((ulwp->ul_schedctl != NULL &&
701 	    ulwp->ul_schedctl->sc_cid == ulwp->ul_rtclassid) |
702 	    ulwp->ul_pilocks) {
703 		ulwp->ul_rtqueued = 1;
704 		qrp->qr_rtcount++;
705 	}
706 	MAXINCR(qrp->qr_qmax, qrp->qr_qlen);
707 	MAXINCR(qp->qh_qmax, qp->qh_qlen);
708 }
709 
710 /*
711  * Helper function for queue_slot() and queue_slot_rt().
712  * Try to find a non-suspended thread on the queue.
713  */
714 static ulwp_t **
715 queue_slot_runnable(ulwp_t **ulwpp, ulwp_t **prevp, int rt)
716 {
717 	ulwp_t *ulwp;
718 	ulwp_t **foundpp = NULL;
719 	int priority = -1;
720 	ulwp_t *prev;
721 	int tpri;
722 
723 	for (prev = NULL;
724 	    (ulwp = *ulwpp) != NULL;
725 	    prev = ulwp, ulwpp = &ulwp->ul_link) {
726 		if (ulwp->ul_stop)	/* skip suspended threads */
727 			continue;
728 		tpri = rt? CMP_PRIO(ulwp) : 0;
729 		if (tpri > priority) {
730 			foundpp = ulwpp;
731 			*prevp = prev;
732 			priority = tpri;
733 			if (!rt)
734 				break;
735 		}
736 	}
737 	return (foundpp);
738 }
739 
740 /*
741  * For real-time, we search the entire queue because the dispatch
742  * (kernel) priorities may have changed since enqueueing.
743  */
744 static ulwp_t **
745 queue_slot_rt(ulwp_t **ulwpp_org, ulwp_t **prevp)
746 {
747 	ulwp_t **ulwpp = ulwpp_org;
748 	ulwp_t *ulwp = *ulwpp;
749 	ulwp_t **foundpp = ulwpp;
750 	int priority = CMP_PRIO(ulwp);
751 	ulwp_t *prev;
752 	int tpri;
753 
754 	for (prev = ulwp, ulwpp = &ulwp->ul_link;
755 	    (ulwp = *ulwpp) != NULL;
756 	    prev = ulwp, ulwpp = &ulwp->ul_link) {
757 		tpri = CMP_PRIO(ulwp);
758 		if (tpri > priority) {
759 			foundpp = ulwpp;
760 			*prevp = prev;
761 			priority = tpri;
762 		}
763 	}
764 	ulwp = *foundpp;
765 
766 	/*
767 	 * Try not to return a suspended thread.
768 	 * This mimics the old libthread's behavior.
769 	 */
770 	if (ulwp->ul_stop &&
771 	    (ulwpp = queue_slot_runnable(ulwpp_org, prevp, 1)) != NULL) {
772 		foundpp = ulwpp;
773 		ulwp = *foundpp;
774 	}
775 	ulwp->ul_rt = 1;
776 	return (foundpp);
777 }
778 
779 ulwp_t **
780 queue_slot(queue_head_t *qp, ulwp_t **prevp, int *more)
781 {
782 	queue_root_t *qrp;
783 	ulwp_t **ulwpp;
784 	ulwp_t *ulwp;
785 	int rt;
786 
787 	ASSERT(MUTEX_OWNED(&qp->qh_lock, curthread));
788 
789 	if ((qrp = qp->qh_root) == NULL || (ulwp = qrp->qr_head) == NULL) {
790 		*more = 0;
791 		return (NULL);		/* no lwps on the queue */
792 	}
793 	rt = (qrp->qr_rtcount != 0);
794 	*prevp = NULL;
795 	if (ulwp->ul_link == NULL) {	/* only one lwp on the queue */
796 		*more = 0;
797 		ulwp->ul_rt = rt;
798 		return (&qrp->qr_head);
799 	}
800 	*more = 1;
801 
802 	if (rt)		/* real-time queue */
803 		return (queue_slot_rt(&qrp->qr_head, prevp));
804 	/*
805 	 * Try not to return a suspended thread.
806 	 * This mimics the old libthread's behavior.
807 	 */
808 	if (ulwp->ul_stop &&
809 	    (ulwpp = queue_slot_runnable(&qrp->qr_head, prevp, 0)) != NULL) {
810 		ulwp = *ulwpp;
811 		ulwp->ul_rt = 0;
812 		return (ulwpp);
813 	}
814 	/*
815 	 * The common case; just pick the first thread on the queue.
816 	 */
817 	ulwp->ul_rt = 0;
818 	return (&qrp->qr_head);
819 }
820 
821 /*
822  * Common code for unlinking an lwp from a user-level sleep queue.
823  */
824 void
825 queue_unlink(queue_head_t *qp, ulwp_t **ulwpp, ulwp_t *prev)
826 {
827 	queue_root_t *qrp = qp->qh_root;
828 	queue_root_t *nqrp;
829 	ulwp_t *ulwp = *ulwpp;
830 	ulwp_t *next;
831 
832 	ASSERT(MUTEX_OWNED(&qp->qh_lock, curthread));
833 	ASSERT(qp->qh_wchan != NULL && ulwp->ul_wchan == qp->qh_wchan);
834 
835 	DECR(qp->qh_qlen);
836 	DECR(qrp->qr_qlen);
837 	if (ulwp->ul_rtqueued) {
838 		ulwp->ul_rtqueued = 0;
839 		qrp->qr_rtcount--;
840 	}
841 	next = ulwp->ul_link;
842 	*ulwpp = next;
843 	ulwp->ul_link = NULL;
844 	if (qrp->qr_tail == ulwp)
845 		qrp->qr_tail = prev;
846 	if (qrp == &ulwp->ul_queue_root) {
847 		/*
848 		 * We can't continue to use the unlinked thread's
849 		 * queue root for the linkage.
850 		 */
851 		queue_root_t *qr_next = qrp->qr_next;
852 		queue_root_t *qr_prev = qrp->qr_prev;
853 
854 		if (qrp->qr_tail) {
855 			/* switch to using the last thread's queue root */
856 			ASSERT(qrp->qr_qlen != 0);
857 			nqrp = &qrp->qr_tail->ul_queue_root;
858 			*nqrp = *qrp;
859 			if (qr_next)
860 				qr_next->qr_prev = nqrp;
861 			if (qr_prev)
862 				qr_prev->qr_next = nqrp;
863 			else
864 				qp->qh_hlist = nqrp;
865 			qp->qh_root = nqrp;
866 		} else {
867 			/* empty queue root; just delete from the hash list */
868 			ASSERT(qrp->qr_qlen == 0);
869 			if (qr_next)
870 				qr_next->qr_prev = qr_prev;
871 			if (qr_prev)
872 				qr_prev->qr_next = qr_next;
873 			else
874 				qp->qh_hlist = qr_next;
875 			qp->qh_root = NULL;
876 			DECR(qp->qh_hlen);
877 		}
878 	}
879 }
880 
881 ulwp_t *
882 dequeue(queue_head_t *qp, int *more)
883 {
884 	ulwp_t **ulwpp;
885 	ulwp_t *ulwp;
886 	ulwp_t *prev;
887 
888 	if ((ulwpp = queue_slot(qp, &prev, more)) == NULL)
889 		return (NULL);
890 	ulwp = *ulwpp;
891 	queue_unlink(qp, ulwpp, prev);
892 	ulwp->ul_sleepq = NULL;
893 	ulwp->ul_wchan = NULL;
894 	return (ulwp);
895 }
896 
897 /*
898  * Return a pointer to the highest priority thread sleeping on wchan.
899  */
900 ulwp_t *
901 queue_waiter(queue_head_t *qp)
902 {
903 	ulwp_t **ulwpp;
904 	ulwp_t *prev;
905 	int more;
906 
907 	if ((ulwpp = queue_slot(qp, &prev, &more)) == NULL)
908 		return (NULL);
909 	return (*ulwpp);
910 }
911 
912 int
913 dequeue_self(queue_head_t *qp)
914 {
915 	ulwp_t *self = curthread;
916 	queue_root_t *qrp;
917 	ulwp_t **ulwpp;
918 	ulwp_t *ulwp;
919 	ulwp_t *prev;
920 	int found = 0;
921 
922 	ASSERT(MUTEX_OWNED(&qp->qh_lock, self));
923 
924 	/* find self on the sleep queue */
925 	if ((qrp = qp->qh_root) != NULL) {
926 		for (prev = NULL, ulwpp = &qrp->qr_head;
927 		    (ulwp = *ulwpp) != NULL;
928 		    prev = ulwp, ulwpp = &ulwp->ul_link) {
929 			if (ulwp == self) {
930 				queue_unlink(qp, ulwpp, prev);
931 				self->ul_cvmutex = NULL;
932 				self->ul_sleepq = NULL;
933 				self->ul_wchan = NULL;
934 				found = 1;
935 				break;
936 			}
937 		}
938 	}
939 
940 	if (!found)
941 		thr_panic("dequeue_self(): curthread not found on queue");
942 
943 	return ((qrp = qp->qh_root) != NULL && qrp->qr_head != NULL);
944 }
945 
946 /*
947  * Called from call_user_handler() and _thrp_suspend() to take
948  * ourself off of our sleep queue so we can grab locks.
949  */
950 void
951 unsleep_self(void)
952 {
953 	ulwp_t *self = curthread;
954 	queue_head_t *qp;
955 
956 	/*
957 	 * Calling enter_critical()/exit_critical() here would lead
958 	 * to recursion.  Just manipulate self->ul_critical directly.
959 	 */
960 	self->ul_critical++;
961 	while (self->ul_sleepq != NULL) {
962 		qp = queue_lock(self->ul_wchan, self->ul_qtype);
963 		/*
964 		 * We may have been moved from a CV queue to a
965 		 * mutex queue while we were attempting queue_lock().
966 		 * If so, just loop around and try again.
967 		 * dequeue_self() clears self->ul_sleepq.
968 		 */
969 		if (qp == self->ul_sleepq)
970 			(void) dequeue_self(qp);
971 		queue_unlock(qp);
972 	}
973 	self->ul_writer = 0;
974 	self->ul_critical--;
975 }
976 
977 /*
978  * Common code for calling the the ___lwp_mutex_timedlock() system call.
979  * Returns with mutex_owner and mutex_ownerpid set correctly.
980  */
981 static int
982 mutex_lock_kernel(mutex_t *mp, timespec_t *tsp, tdb_mutex_stats_t *msp)
983 {
984 	ulwp_t *self = curthread;
985 	uberdata_t *udp = self->ul_uberdata;
986 	int mtype = mp->mutex_type;
987 	hrtime_t begin_sleep;
988 	int acquired;
989 	int error;
990 
991 	self->ul_sp = stkptr();
992 	self->ul_wchan = mp;
993 	if (__td_event_report(self, TD_SLEEP, udp)) {
994 		self->ul_td_evbuf.eventnum = TD_SLEEP;
995 		self->ul_td_evbuf.eventdata = mp;
996 		tdb_event(TD_SLEEP, udp);
997 	}
998 	if (msp) {
999 		tdb_incr(msp->mutex_sleep);
1000 		begin_sleep = gethrtime();
1001 	}
1002 
1003 	DTRACE_PROBE1(plockstat, mutex__block, mp);
1004 
1005 	for (;;) {
1006 		/*
1007 		 * A return value of EOWNERDEAD or ELOCKUNMAPPED
1008 		 * means we successfully acquired the lock.
1009 		 */
1010 		if ((error = ___lwp_mutex_timedlock(mp, tsp)) != 0 &&
1011 		    error != EOWNERDEAD && error != ELOCKUNMAPPED) {
1012 			acquired = 0;
1013 			break;
1014 		}
1015 
1016 		if (mtype & USYNC_PROCESS) {
1017 			/*
1018 			 * Defend against forkall().  We may be the child,
1019 			 * in which case we don't actually own the mutex.
1020 			 */
1021 			enter_critical(self);
1022 			if (mp->mutex_ownerpid == udp->pid) {
1023 				mp->mutex_owner = (uintptr_t)self;
1024 				exit_critical(self);
1025 				acquired = 1;
1026 				break;
1027 			}
1028 			exit_critical(self);
1029 		} else {
1030 			mp->mutex_owner = (uintptr_t)self;
1031 			acquired = 1;
1032 			break;
1033 		}
1034 	}
1035 	if (msp)
1036 		msp->mutex_sleep_time += gethrtime() - begin_sleep;
1037 	self->ul_wchan = NULL;
1038 	self->ul_sp = 0;
1039 
1040 	if (acquired) {
1041 		DTRACE_PROBE2(plockstat, mutex__blocked, mp, 1);
1042 		DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0);
1043 	} else {
1044 		DTRACE_PROBE2(plockstat, mutex__blocked, mp, 0);
1045 		DTRACE_PROBE2(plockstat, mutex__error, mp, error);
1046 	}
1047 
1048 	return (error);
1049 }
1050 
1051 /*
1052  * Common code for calling the ___lwp_mutex_trylock() system call.
1053  * Returns with mutex_owner and mutex_ownerpid set correctly.
1054  */
1055 int
1056 mutex_trylock_kernel(mutex_t *mp)
1057 {
1058 	ulwp_t *self = curthread;
1059 	uberdata_t *udp = self->ul_uberdata;
1060 	int mtype = mp->mutex_type;
1061 	int error;
1062 	int acquired;
1063 
1064 	for (;;) {
1065 		/*
1066 		 * A return value of EOWNERDEAD or ELOCKUNMAPPED
1067 		 * means we successfully acquired the lock.
1068 		 */
1069 		if ((error = ___lwp_mutex_trylock(mp)) != 0 &&
1070 		    error != EOWNERDEAD && error != ELOCKUNMAPPED) {
1071 			acquired = 0;
1072 			break;
1073 		}
1074 
1075 		if (mtype & USYNC_PROCESS) {
1076 			/*
1077 			 * Defend against forkall().  We may be the child,
1078 			 * in which case we don't actually own the mutex.
1079 			 */
1080 			enter_critical(self);
1081 			if (mp->mutex_ownerpid == udp->pid) {
1082 				mp->mutex_owner = (uintptr_t)self;
1083 				exit_critical(self);
1084 				acquired = 1;
1085 				break;
1086 			}
1087 			exit_critical(self);
1088 		} else {
1089 			mp->mutex_owner = (uintptr_t)self;
1090 			acquired = 1;
1091 			break;
1092 		}
1093 	}
1094 
1095 	if (acquired) {
1096 		DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0);
1097 	} else if (error != EBUSY) {
1098 		DTRACE_PROBE2(plockstat, mutex__error, mp, error);
1099 	}
1100 
1101 	return (error);
1102 }
1103 
1104 volatile sc_shared_t *
1105 setup_schedctl(void)
1106 {
1107 	ulwp_t *self = curthread;
1108 	volatile sc_shared_t *scp;
1109 	sc_shared_t *tmp;
1110 
1111 	if ((scp = self->ul_schedctl) == NULL && /* no shared state yet */
1112 	    !self->ul_vfork &&			/* not a child of vfork() */
1113 	    !self->ul_schedctl_called) {	/* haven't been called before */
1114 		enter_critical(self);
1115 		self->ul_schedctl_called = &self->ul_uberdata->uberflags;
1116 		if ((tmp = __schedctl()) != (sc_shared_t *)(-1))
1117 			self->ul_schedctl = scp = tmp;
1118 		exit_critical(self);
1119 	}
1120 	/*
1121 	 * Unless the call to setup_schedctl() is surrounded
1122 	 * by enter_critical()/exit_critical(), the address
1123 	 * we are returning could be invalid due to a forkall()
1124 	 * having occurred in another thread.
1125 	 */
1126 	return (scp);
1127 }
1128 
1129 /*
1130  * Interfaces from libsched, incorporated into libc.
1131  * libsched.so.1 is now a filter library onto libc.
1132  */
1133 #pragma weak schedctl_lookup = schedctl_init
1134 schedctl_t *
1135 schedctl_init(void)
1136 {
1137 	volatile sc_shared_t *scp = setup_schedctl();
1138 	return ((scp == NULL)? NULL : (schedctl_t *)&scp->sc_preemptctl);
1139 }
1140 
1141 void
1142 schedctl_exit(void)
1143 {
1144 }
1145 
1146 /*
1147  * Contract private interface for java.
1148  * Set up the schedctl data if it doesn't exist yet.
1149  * Return a pointer to the pointer to the schedctl data.
1150  */
1151 volatile sc_shared_t *volatile *
1152 _thr_schedctl(void)
1153 {
1154 	ulwp_t *self = curthread;
1155 	volatile sc_shared_t *volatile *ptr;
1156 
1157 	if (self->ul_vfork)
1158 		return (NULL);
1159 	if (*(ptr = &self->ul_schedctl) == NULL)
1160 		(void) setup_schedctl();
1161 	return (ptr);
1162 }
1163 
1164 /*
1165  * Block signals and attempt to block preemption.
1166  * no_preempt()/preempt() must be used in pairs but can be nested.
1167  */
1168 void
1169 no_preempt(ulwp_t *self)
1170 {
1171 	volatile sc_shared_t *scp;
1172 
1173 	if (self->ul_preempt++ == 0) {
1174 		enter_critical(self);
1175 		if ((scp = self->ul_schedctl) != NULL ||
1176 		    (scp = setup_schedctl()) != NULL) {
1177 			/*
1178 			 * Save the pre-existing preempt value.
1179 			 */
1180 			self->ul_savpreempt = scp->sc_preemptctl.sc_nopreempt;
1181 			scp->sc_preemptctl.sc_nopreempt = 1;
1182 		}
1183 	}
1184 }
1185 
1186 /*
1187  * Undo the effects of no_preempt().
1188  */
1189 void
1190 preempt(ulwp_t *self)
1191 {
1192 	volatile sc_shared_t *scp;
1193 
1194 	ASSERT(self->ul_preempt > 0);
1195 	if (--self->ul_preempt == 0) {
1196 		if ((scp = self->ul_schedctl) != NULL) {
1197 			/*
1198 			 * Restore the pre-existing preempt value.
1199 			 */
1200 			scp->sc_preemptctl.sc_nopreempt = self->ul_savpreempt;
1201 			if (scp->sc_preemptctl.sc_yield &&
1202 			    scp->sc_preemptctl.sc_nopreempt == 0) {
1203 				yield();
1204 				if (scp->sc_preemptctl.sc_yield) {
1205 					/*
1206 					 * Shouldn't happen.  This is either
1207 					 * a race condition or the thread
1208 					 * just entered the real-time class.
1209 					 */
1210 					yield();
1211 					scp->sc_preemptctl.sc_yield = 0;
1212 				}
1213 			}
1214 		}
1215 		exit_critical(self);
1216 	}
1217 }
1218 
1219 /*
1220  * If a call to preempt() would cause the current thread to yield or to
1221  * take deferred actions in exit_critical(), then unpark the specified
1222  * lwp so it can run while we delay.  Return the original lwpid if the
1223  * unpark was not performed, else return zero.  The tests are a repeat
1224  * of some of the tests in preempt(), above.  This is a statistical
1225  * optimization solely for cond_sleep_queue(), below.
1226  */
1227 static lwpid_t
1228 preempt_unpark(ulwp_t *self, lwpid_t lwpid)
1229 {
1230 	volatile sc_shared_t *scp = self->ul_schedctl;
1231 
1232 	ASSERT(self->ul_preempt == 1 && self->ul_critical > 0);
1233 	if ((scp != NULL && scp->sc_preemptctl.sc_yield) ||
1234 	    (self->ul_curplease && self->ul_critical == 1)) {
1235 		(void) __lwp_unpark(lwpid);
1236 		lwpid = 0;
1237 	}
1238 	return (lwpid);
1239 }
1240 
1241 /*
1242  * Spin for a while (if 'tryhard' is true), trying to grab the lock.
1243  * If this fails, return EBUSY and let the caller deal with it.
1244  * If this succeeds, return 0 with mutex_owner set to curthread.
1245  */
1246 static int
1247 mutex_trylock_adaptive(mutex_t *mp, int tryhard)
1248 {
1249 	ulwp_t *self = curthread;
1250 	int error = EBUSY;
1251 	ulwp_t *ulwp;
1252 	volatile sc_shared_t *scp;
1253 	volatile uint8_t *lockp = (volatile uint8_t *)&mp->mutex_lockw;
1254 	volatile uint64_t *ownerp = (volatile uint64_t *)&mp->mutex_owner;
1255 	uint32_t new_lockword;
1256 	int count = 0;
1257 	int max_count;
1258 	uint8_t max_spinners;
1259 
1260 	ASSERT(!(mp->mutex_type & USYNC_PROCESS));
1261 
1262 	if (MUTEX_OWNER(mp) == self)
1263 		return (EBUSY);
1264 
1265 	/* short-cut, not definitive (see below) */
1266 	if (mp->mutex_flag & LOCK_NOTRECOVERABLE) {
1267 		ASSERT(mp->mutex_type & LOCK_ROBUST);
1268 		error = ENOTRECOVERABLE;
1269 		goto done;
1270 	}
1271 
1272 	/*
1273 	 * Make one attempt to acquire the lock before
1274 	 * incurring the overhead of the spin loop.
1275 	 */
1276 	if (set_lock_byte(lockp) == 0) {
1277 		*ownerp = (uintptr_t)self;
1278 		error = 0;
1279 		goto done;
1280 	}
1281 	if (!tryhard)
1282 		goto done;
1283 	if (ncpus == 0)
1284 		ncpus = (int)_sysconf(_SC_NPROCESSORS_ONLN);
1285 	if ((max_spinners = self->ul_max_spinners) >= ncpus)
1286 		max_spinners = ncpus - 1;
1287 	max_count = (max_spinners != 0)? self->ul_adaptive_spin : 0;
1288 	if (max_count == 0)
1289 		goto done;
1290 
1291 	/*
1292 	 * This spin loop is unfair to lwps that have already dropped into
1293 	 * the kernel to sleep.  They will starve on a highly-contended mutex.
1294 	 * This is just too bad.  The adaptive spin algorithm is intended
1295 	 * to allow programs with highly-contended locks (that is, broken
1296 	 * programs) to execute with reasonable speed despite their contention.
1297 	 * Being fair would reduce the speed of such programs and well-written
1298 	 * programs will not suffer in any case.
1299 	 */
1300 	enter_critical(self);
1301 	if (spinners_incr(&mp->mutex_lockword, max_spinners) == -1) {
1302 		exit_critical(self);
1303 		goto done;
1304 	}
1305 	DTRACE_PROBE1(plockstat, mutex__spin, mp);
1306 	for (count = 1; ; count++) {
1307 		if (*lockp == 0 && set_lock_byte(lockp) == 0) {
1308 			*ownerp = (uintptr_t)self;
1309 			error = 0;
1310 			break;
1311 		}
1312 		if (count == max_count)
1313 			break;
1314 		SMT_PAUSE();
1315 		/*
1316 		 * Stop spinning if the mutex owner is not running on
1317 		 * a processor; it will not drop the lock any time soon
1318 		 * and we would just be wasting time to keep spinning.
1319 		 *
1320 		 * Note that we are looking at another thread (ulwp_t)
1321 		 * without ensuring that the other thread does not exit.
1322 		 * The scheme relies on ulwp_t structures never being
1323 		 * deallocated by the library (the library employs a free
1324 		 * list of ulwp_t structs that are reused when new threads
1325 		 * are created) and on schedctl shared memory never being
1326 		 * deallocated once created via __schedctl().
1327 		 *
1328 		 * Thus, the worst that can happen when the spinning thread
1329 		 * looks at the owner's schedctl data is that it is looking
1330 		 * at some other thread's schedctl data.  This almost never
1331 		 * happens and is benign when it does.
1332 		 */
1333 		if ((ulwp = (ulwp_t *)(uintptr_t)*ownerp) != NULL &&
1334 		    ((scp = ulwp->ul_schedctl) == NULL ||
1335 		    scp->sc_state != SC_ONPROC))
1336 			break;
1337 	}
1338 	new_lockword = spinners_decr(&mp->mutex_lockword);
1339 	if (error && (new_lockword & (LOCKMASK | SPINNERMASK)) == 0) {
1340 		/*
1341 		 * We haven't yet acquired the lock, the lock
1342 		 * is free, and there are no other spinners.
1343 		 * Make one final attempt to acquire the lock.
1344 		 *
1345 		 * This isn't strictly necessary since mutex_lock_queue()
1346 		 * (the next action this thread will take if it doesn't
1347 		 * acquire the lock here) makes one attempt to acquire
1348 		 * the lock before putting the thread to sleep.
1349 		 *
1350 		 * If the next action for this thread (on failure here)
1351 		 * were not to call mutex_lock_queue(), this would be
1352 		 * necessary for correctness, to avoid ending up with an
1353 		 * unheld mutex with waiters but no one to wake them up.
1354 		 */
1355 		if (set_lock_byte(lockp) == 0) {
1356 			*ownerp = (uintptr_t)self;
1357 			error = 0;
1358 		}
1359 		count++;
1360 	}
1361 	exit_critical(self);
1362 
1363 done:
1364 	if (error == 0 && (mp->mutex_flag & LOCK_NOTRECOVERABLE)) {
1365 		ASSERT(mp->mutex_type & LOCK_ROBUST);
1366 		/*
1367 		 * We shouldn't own the mutex.
1368 		 * Just clear the lock; everyone has already been waked up.
1369 		 */
1370 		mp->mutex_owner = 0;
1371 		(void) clear_lockbyte(&mp->mutex_lockword);
1372 		error = ENOTRECOVERABLE;
1373 	}
1374 
1375 	if (error) {
1376 		if (count) {
1377 			DTRACE_PROBE2(plockstat, mutex__spun, 0, count);
1378 		}
1379 		if (error != EBUSY) {
1380 			DTRACE_PROBE2(plockstat, mutex__error, mp, error);
1381 		}
1382 	} else {
1383 		if (count) {
1384 			DTRACE_PROBE2(plockstat, mutex__spun, 1, count);
1385 		}
1386 		DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, count);
1387 		if (mp->mutex_flag & LOCK_OWNERDEAD) {
1388 			ASSERT(mp->mutex_type & LOCK_ROBUST);
1389 			error = EOWNERDEAD;
1390 		}
1391 	}
1392 
1393 	return (error);
1394 }
1395 
1396 /*
1397  * Same as mutex_trylock_adaptive(), except specifically for queue locks.
1398  * The owner field is not set here; the caller (spin_lock_set()) sets it.
1399  */
1400 static int
1401 mutex_queuelock_adaptive(mutex_t *mp)
1402 {
1403 	ulwp_t *ulwp;
1404 	volatile sc_shared_t *scp;
1405 	volatile uint8_t *lockp;
1406 	volatile uint64_t *ownerp;
1407 	int count = curthread->ul_queue_spin;
1408 
1409 	ASSERT(mp->mutex_type == USYNC_THREAD);
1410 
1411 	if (count == 0)
1412 		return (EBUSY);
1413 
1414 	lockp = (volatile uint8_t *)&mp->mutex_lockw;
1415 	ownerp = (volatile uint64_t *)&mp->mutex_owner;
1416 	while (--count >= 0) {
1417 		if (*lockp == 0 && set_lock_byte(lockp) == 0)
1418 			return (0);
1419 		SMT_PAUSE();
1420 		if ((ulwp = (ulwp_t *)(uintptr_t)*ownerp) != NULL &&
1421 		    ((scp = ulwp->ul_schedctl) == NULL ||
1422 		    scp->sc_state != SC_ONPROC))
1423 			break;
1424 	}
1425 
1426 	return (EBUSY);
1427 }
1428 
1429 /*
1430  * Like mutex_trylock_adaptive(), but for process-shared mutexes.
1431  * Spin for a while (if 'tryhard' is true), trying to grab the lock.
1432  * If this fails, return EBUSY and let the caller deal with it.
1433  * If this succeeds, return 0 with mutex_owner set to curthread
1434  * and mutex_ownerpid set to the current pid.
1435  */
1436 static int
1437 mutex_trylock_process(mutex_t *mp, int tryhard)
1438 {
1439 	ulwp_t *self = curthread;
1440 	uberdata_t *udp = self->ul_uberdata;
1441 	int error = EBUSY;
1442 	volatile uint64_t *lockp = (volatile uint64_t *)&mp->mutex_lockword64;
1443 	uint32_t new_lockword;
1444 	int count = 0;
1445 	int max_count;
1446 	uint8_t max_spinners;
1447 
1448 #if defined(__sparc) && !defined(_LP64)
1449 	/* horrible hack, necessary only on 32-bit sparc */
1450 	int fix_alignment_problem =
1451 	    (((uintptr_t)mp & (_LONG_LONG_ALIGNMENT - 1)) &&
1452 	    self->ul_misaligned && !(mp->mutex_type & LOCK_ROBUST));
1453 #endif
1454 
1455 	ASSERT(mp->mutex_type & USYNC_PROCESS);
1456 
1457 	if (shared_mutex_held(mp))
1458 		return (EBUSY);
1459 
1460 	/* short-cut, not definitive (see below) */
1461 	if (mp->mutex_flag & LOCK_NOTRECOVERABLE) {
1462 		ASSERT(mp->mutex_type & LOCK_ROBUST);
1463 		error = ENOTRECOVERABLE;
1464 		goto done;
1465 	}
1466 
1467 	/*
1468 	 * Make one attempt to acquire the lock before
1469 	 * incurring the overhead of the spin loop.
1470 	 */
1471 	enter_critical(self);
1472 #if defined(__sparc) && !defined(_LP64)
1473 	/* horrible hack, necessary only on 32-bit sparc */
1474 	if (fix_alignment_problem) {
1475 		if (set_lock_byte(&mp->mutex_lockw) == 0) {
1476 			mp->mutex_ownerpid = udp->pid;
1477 			mp->mutex_owner = (uintptr_t)self;
1478 			exit_critical(self);
1479 			error = 0;
1480 			goto done;
1481 		}
1482 	} else
1483 #endif
1484 	if (set_lock_byte64(lockp, udp->pid) == 0) {
1485 		mp->mutex_owner = (uintptr_t)self;
1486 		/* mp->mutex_ownerpid was set by set_lock_byte64() */
1487 		exit_critical(self);
1488 		error = 0;
1489 		goto done;
1490 	}
1491 	exit_critical(self);
1492 	if (!tryhard)
1493 		goto done;
1494 	if (ncpus == 0)
1495 		ncpus = (int)_sysconf(_SC_NPROCESSORS_ONLN);
1496 	if ((max_spinners = self->ul_max_spinners) >= ncpus)
1497 		max_spinners = ncpus - 1;
1498 	max_count = (max_spinners != 0)? self->ul_adaptive_spin : 0;
1499 	if (max_count == 0)
1500 		goto done;
1501 
1502 	/*
1503 	 * This is a process-shared mutex.
1504 	 * We cannot know if the owner is running on a processor.
1505 	 * We just spin and hope that it is on a processor.
1506 	 */
1507 	enter_critical(self);
1508 	if (spinners_incr(&mp->mutex_lockword, max_spinners) == -1) {
1509 		exit_critical(self);
1510 		goto done;
1511 	}
1512 	DTRACE_PROBE1(plockstat, mutex__spin, mp);
1513 	for (count = 1; ; count++) {
1514 #if defined(__sparc) && !defined(_LP64)
1515 		/* horrible hack, necessary only on 32-bit sparc */
1516 		if (fix_alignment_problem) {
1517 			if ((*lockp & LOCKMASK64) == 0 &&
1518 			    set_lock_byte(&mp->mutex_lockw) == 0) {
1519 				mp->mutex_ownerpid = udp->pid;
1520 				mp->mutex_owner = (uintptr_t)self;
1521 				error = 0;
1522 				break;
1523 			}
1524 		} else
1525 #endif
1526 		if ((*lockp & LOCKMASK64) == 0 &&
1527 		    set_lock_byte64(lockp, udp->pid) == 0) {
1528 			mp->mutex_owner = (uintptr_t)self;
1529 			/* mp->mutex_ownerpid was set by set_lock_byte64() */
1530 			error = 0;
1531 			break;
1532 		}
1533 		if (count == max_count)
1534 			break;
1535 		SMT_PAUSE();
1536 	}
1537 	new_lockword = spinners_decr(&mp->mutex_lockword);
1538 	if (error && (new_lockword & (LOCKMASK | SPINNERMASK)) == 0) {
1539 		/*
1540 		 * We haven't yet acquired the lock, the lock
1541 		 * is free, and there are no other spinners.
1542 		 * Make one final attempt to acquire the lock.
1543 		 *
1544 		 * This isn't strictly necessary since mutex_lock_kernel()
1545 		 * (the next action this thread will take if it doesn't
1546 		 * acquire the lock here) makes one attempt to acquire
1547 		 * the lock before putting the thread to sleep.
1548 		 *
1549 		 * If the next action for this thread (on failure here)
1550 		 * were not to call mutex_lock_kernel(), this would be
1551 		 * necessary for correctness, to avoid ending up with an
1552 		 * unheld mutex with waiters but no one to wake them up.
1553 		 */
1554 #if defined(__sparc) && !defined(_LP64)
1555 		/* horrible hack, necessary only on 32-bit sparc */
1556 		if (fix_alignment_problem) {
1557 			if (set_lock_byte(&mp->mutex_lockw) == 0) {
1558 				mp->mutex_ownerpid = udp->pid;
1559 				mp->mutex_owner = (uintptr_t)self;
1560 				error = 0;
1561 			}
1562 		} else
1563 #endif
1564 		if (set_lock_byte64(lockp, udp->pid) == 0) {
1565 			mp->mutex_owner = (uintptr_t)self;
1566 			/* mp->mutex_ownerpid was set by set_lock_byte64() */
1567 			error = 0;
1568 		}
1569 		count++;
1570 	}
1571 	exit_critical(self);
1572 
1573 done:
1574 	if (error == 0 && (mp->mutex_flag & LOCK_NOTRECOVERABLE)) {
1575 		ASSERT(mp->mutex_type & LOCK_ROBUST);
1576 		/*
1577 		 * We shouldn't own the mutex.
1578 		 * Just clear the lock; everyone has already been waked up.
1579 		 */
1580 		mp->mutex_owner = 0;
1581 		/* mp->mutex_ownerpid is cleared by clear_lockbyte64() */
1582 		(void) clear_lockbyte64(&mp->mutex_lockword64);
1583 		error = ENOTRECOVERABLE;
1584 	}
1585 
1586 	if (error) {
1587 		if (count) {
1588 			DTRACE_PROBE2(plockstat, mutex__spun, 0, count);
1589 		}
1590 		if (error != EBUSY) {
1591 			DTRACE_PROBE2(plockstat, mutex__error, mp, error);
1592 		}
1593 	} else {
1594 		if (count) {
1595 			DTRACE_PROBE2(plockstat, mutex__spun, 1, count);
1596 		}
1597 		DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, count);
1598 		if (mp->mutex_flag & (LOCK_OWNERDEAD | LOCK_UNMAPPED)) {
1599 			ASSERT(mp->mutex_type & LOCK_ROBUST);
1600 			if (mp->mutex_flag & LOCK_OWNERDEAD)
1601 				error = EOWNERDEAD;
1602 			else if (mp->mutex_type & USYNC_PROCESS_ROBUST)
1603 				error = ELOCKUNMAPPED;
1604 			else
1605 				error = EOWNERDEAD;
1606 		}
1607 	}
1608 
1609 	return (error);
1610 }
1611 
1612 /*
1613  * Mutex wakeup code for releasing a USYNC_THREAD mutex.
1614  * Returns the lwpid of the thread that was dequeued, if any.
1615  * The caller of mutex_wakeup() must call __lwp_unpark(lwpid)
1616  * to wake up the specified lwp.
1617  */
1618 static lwpid_t
1619 mutex_wakeup(mutex_t *mp)
1620 {
1621 	lwpid_t lwpid = 0;
1622 	int more;
1623 	queue_head_t *qp;
1624 	ulwp_t *ulwp;
1625 
1626 	/*
1627 	 * Dequeue a waiter from the sleep queue.  Don't touch the mutex
1628 	 * waiters bit if no one was found on the queue because the mutex
1629 	 * might have been deallocated or reallocated for another purpose.
1630 	 */
1631 	qp = queue_lock(mp, MX);
1632 	if ((ulwp = dequeue(qp, &more)) != NULL) {
1633 		lwpid = ulwp->ul_lwpid;
1634 		mp->mutex_waiters = more;
1635 	}
1636 	queue_unlock(qp);
1637 	return (lwpid);
1638 }
1639 
1640 /*
1641  * Mutex wakeup code for releasing all waiters on a USYNC_THREAD mutex.
1642  */
1643 static void
1644 mutex_wakeup_all(mutex_t *mp)
1645 {
1646 	queue_head_t *qp;
1647 	queue_root_t *qrp;
1648 	int nlwpid = 0;
1649 	int maxlwps = MAXLWPS;
1650 	ulwp_t *ulwp;
1651 	lwpid_t buffer[MAXLWPS];
1652 	lwpid_t *lwpid = buffer;
1653 
1654 	/*
1655 	 * Walk the list of waiters and prepare to wake up all of them.
1656 	 * The waiters flag has already been cleared from the mutex.
1657 	 *
1658 	 * We keep track of lwpids that are to be unparked in lwpid[].
1659 	 * __lwp_unpark_all() is called to unpark all of them after
1660 	 * they have been removed from the sleep queue and the sleep
1661 	 * queue lock has been dropped.  If we run out of space in our
1662 	 * on-stack buffer, we need to allocate more but we can't call
1663 	 * lmalloc() because we are holding a queue lock when the overflow
1664 	 * occurs and lmalloc() acquires a lock.  We can't use alloca()
1665 	 * either because the application may have allocated a small
1666 	 * stack and we don't want to overrun the stack.  So we call
1667 	 * alloc_lwpids() to allocate a bigger buffer using the mmap()
1668 	 * system call directly since that path acquires no locks.
1669 	 */
1670 	qp = queue_lock(mp, MX);
1671 	for (;;) {
1672 		if ((qrp = qp->qh_root) == NULL ||
1673 		    (ulwp = qrp->qr_head) == NULL)
1674 			break;
1675 		ASSERT(ulwp->ul_wchan == mp);
1676 		queue_unlink(qp, &qrp->qr_head, NULL);
1677 		ulwp->ul_sleepq = NULL;
1678 		ulwp->ul_wchan = NULL;
1679 		if (nlwpid == maxlwps)
1680 			lwpid = alloc_lwpids(lwpid, &nlwpid, &maxlwps);
1681 		lwpid[nlwpid++] = ulwp->ul_lwpid;
1682 	}
1683 
1684 	if (nlwpid == 0) {
1685 		queue_unlock(qp);
1686 	} else {
1687 		mp->mutex_waiters = 0;
1688 		no_preempt(curthread);
1689 		queue_unlock(qp);
1690 		if (nlwpid == 1)
1691 			(void) __lwp_unpark(lwpid[0]);
1692 		else
1693 			(void) __lwp_unpark_all(lwpid, nlwpid);
1694 		preempt(curthread);
1695 	}
1696 
1697 	if (lwpid != buffer)
1698 		(void) munmap((caddr_t)lwpid, maxlwps * sizeof (lwpid_t));
1699 }
1700 
1701 /*
1702  * Release a process-private mutex.
1703  * As an optimization, if there are waiters but there are also spinners
1704  * attempting to acquire the mutex, then don't bother waking up a waiter;
1705  * one of the spinners will acquire the mutex soon and it would be a waste
1706  * of resources to wake up some thread just to have it spin for a while
1707  * and then possibly go back to sleep.  See mutex_trylock_adaptive().
1708  */
1709 static lwpid_t
1710 mutex_unlock_queue(mutex_t *mp, int release_all)
1711 {
1712 	lwpid_t lwpid = 0;
1713 	uint32_t old_lockword;
1714 
1715 	DTRACE_PROBE2(plockstat, mutex__release, mp, 0);
1716 	mp->mutex_owner = 0;
1717 	old_lockword = clear_lockbyte(&mp->mutex_lockword);
1718 	if ((old_lockword & WAITERMASK) &&
1719 	    (release_all || (old_lockword & SPINNERMASK) == 0)) {
1720 		ulwp_t *self = curthread;
1721 		no_preempt(self);	/* ensure a prompt wakeup */
1722 		if (release_all)
1723 			mutex_wakeup_all(mp);
1724 		else
1725 			lwpid = mutex_wakeup(mp);
1726 		if (lwpid == 0)
1727 			preempt(self);
1728 	}
1729 	return (lwpid);
1730 }
1731 
1732 /*
1733  * Like mutex_unlock_queue(), but for process-shared mutexes.
1734  */
1735 static void
1736 mutex_unlock_process(mutex_t *mp, int release_all)
1737 {
1738 	ulwp_t *self = curthread;
1739 	uint64_t old_lockword64;
1740 
1741 	DTRACE_PROBE2(plockstat, mutex__release, mp, 0);
1742 	mp->mutex_owner = 0;
1743 #if defined(__sparc) && !defined(_LP64)
1744 	/* horrible hack, necessary only on 32-bit sparc */
1745 	if (((uintptr_t)mp & (_LONG_LONG_ALIGNMENT - 1)) &&
1746 	    self->ul_misaligned && !(mp->mutex_type & LOCK_ROBUST)) {
1747 		uint32_t old_lockword;
1748 		mp->mutex_ownerpid = 0;
1749 		old_lockword = clear_lockbyte(&mp->mutex_lockword);
1750 		if ((old_lockword & WAITERMASK) &&
1751 		    (release_all || (old_lockword & SPINNERMASK) == 0)) {
1752 			no_preempt(self);	/* ensure a prompt wakeup */
1753 			(void) ___lwp_mutex_wakeup(mp, release_all);
1754 			preempt(self);
1755 		}
1756 		return;
1757 	}
1758 #endif
1759 	/* mp->mutex_ownerpid is cleared by clear_lockbyte64() */
1760 	old_lockword64 = clear_lockbyte64(&mp->mutex_lockword64);
1761 	if ((old_lockword64 & WAITERMASK64) &&
1762 	    (release_all || (old_lockword64 & SPINNERMASK64) == 0)) {
1763 		no_preempt(self);	/* ensure a prompt wakeup */
1764 		(void) ___lwp_mutex_wakeup(mp, release_all);
1765 		preempt(self);
1766 	}
1767 }
1768 
1769 void
1770 stall(void)
1771 {
1772 	for (;;)
1773 		(void) mutex_lock_kernel(&stall_mutex, NULL, NULL);
1774 }
1775 
1776 /*
1777  * Acquire a USYNC_THREAD mutex via user-level sleep queues.
1778  * We failed set_lock_byte(&mp->mutex_lockw) before coming here.
1779  * If successful, returns with mutex_owner set correctly.
1780  */
1781 int
1782 mutex_lock_queue(ulwp_t *self, tdb_mutex_stats_t *msp, mutex_t *mp,
1783 	timespec_t *tsp)
1784 {
1785 	uberdata_t *udp = curthread->ul_uberdata;
1786 	queue_head_t *qp;
1787 	hrtime_t begin_sleep;
1788 	int error = 0;
1789 
1790 	self->ul_sp = stkptr();
1791 	if (__td_event_report(self, TD_SLEEP, udp)) {
1792 		self->ul_wchan = mp;
1793 		self->ul_td_evbuf.eventnum = TD_SLEEP;
1794 		self->ul_td_evbuf.eventdata = mp;
1795 		tdb_event(TD_SLEEP, udp);
1796 	}
1797 	if (msp) {
1798 		tdb_incr(msp->mutex_sleep);
1799 		begin_sleep = gethrtime();
1800 	}
1801 
1802 	DTRACE_PROBE1(plockstat, mutex__block, mp);
1803 
1804 	/*
1805 	 * Put ourself on the sleep queue, and while we are
1806 	 * unable to grab the lock, go park in the kernel.
1807 	 * Take ourself off the sleep queue after we acquire the lock.
1808 	 * The waiter bit can be set/cleared only while holding the queue lock.
1809 	 */
1810 	qp = queue_lock(mp, MX);
1811 	enqueue(qp, self, 0);
1812 	mp->mutex_waiters = 1;
1813 	for (;;) {
1814 		if (set_lock_byte(&mp->mutex_lockw) == 0) {
1815 			mp->mutex_owner = (uintptr_t)self;
1816 			mp->mutex_waiters = dequeue_self(qp);
1817 			break;
1818 		}
1819 		set_parking_flag(self, 1);
1820 		queue_unlock(qp);
1821 		/*
1822 		 * __lwp_park() will return the residual time in tsp
1823 		 * if we are unparked before the timeout expires.
1824 		 */
1825 		error = __lwp_park(tsp, 0);
1826 		set_parking_flag(self, 0);
1827 		/*
1828 		 * We could have taken a signal or suspended ourself.
1829 		 * If we did, then we removed ourself from the queue.
1830 		 * Someone else may have removed us from the queue
1831 		 * as a consequence of mutex_unlock().  We may have
1832 		 * gotten a timeout from __lwp_park().  Or we may still
1833 		 * be on the queue and this is just a spurious wakeup.
1834 		 */
1835 		qp = queue_lock(mp, MX);
1836 		if (self->ul_sleepq == NULL) {
1837 			if (error) {
1838 				mp->mutex_waiters = queue_waiter(qp)? 1 : 0;
1839 				if (error != EINTR)
1840 					break;
1841 				error = 0;
1842 			}
1843 			if (set_lock_byte(&mp->mutex_lockw) == 0) {
1844 				mp->mutex_owner = (uintptr_t)self;
1845 				break;
1846 			}
1847 			enqueue(qp, self, 0);
1848 			mp->mutex_waiters = 1;
1849 		}
1850 		ASSERT(self->ul_sleepq == qp &&
1851 		    self->ul_qtype == MX &&
1852 		    self->ul_wchan == mp);
1853 		if (error) {
1854 			if (error != EINTR) {
1855 				mp->mutex_waiters = dequeue_self(qp);
1856 				break;
1857 			}
1858 			error = 0;
1859 		}
1860 	}
1861 	ASSERT(self->ul_sleepq == NULL && self->ul_link == NULL &&
1862 	    self->ul_wchan == NULL);
1863 	self->ul_sp = 0;
1864 	queue_unlock(qp);
1865 
1866 	if (msp)
1867 		msp->mutex_sleep_time += gethrtime() - begin_sleep;
1868 
1869 	ASSERT(error == 0 || error == EINVAL || error == ETIME);
1870 
1871 	if (error == 0 && (mp->mutex_flag & LOCK_NOTRECOVERABLE)) {
1872 		ASSERT(mp->mutex_type & LOCK_ROBUST);
1873 		/*
1874 		 * We shouldn't own the mutex.
1875 		 * Just clear the lock; everyone has already been waked up.
1876 		 */
1877 		mp->mutex_owner = 0;
1878 		(void) clear_lockbyte(&mp->mutex_lockword);
1879 		error = ENOTRECOVERABLE;
1880 	}
1881 
1882 	if (error) {
1883 		DTRACE_PROBE2(plockstat, mutex__blocked, mp, 0);
1884 		DTRACE_PROBE2(plockstat, mutex__error, mp, error);
1885 	} else {
1886 		DTRACE_PROBE2(plockstat, mutex__blocked, mp, 1);
1887 		DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0);
1888 		if (mp->mutex_flag & LOCK_OWNERDEAD) {
1889 			ASSERT(mp->mutex_type & LOCK_ROBUST);
1890 			error = EOWNERDEAD;
1891 		}
1892 	}
1893 
1894 	return (error);
1895 }
1896 
1897 static int
1898 mutex_recursion(mutex_t *mp, int mtype, int try)
1899 {
1900 	ASSERT(mutex_held(mp));
1901 	ASSERT(mtype & (LOCK_RECURSIVE|LOCK_ERRORCHECK));
1902 	ASSERT(try == MUTEX_TRY || try == MUTEX_LOCK);
1903 
1904 	if (mtype & LOCK_RECURSIVE) {
1905 		if (mp->mutex_rcount == RECURSION_MAX) {
1906 			DTRACE_PROBE2(plockstat, mutex__error, mp, EAGAIN);
1907 			return (EAGAIN);
1908 		}
1909 		mp->mutex_rcount++;
1910 		DTRACE_PROBE3(plockstat, mutex__acquire, mp, 1, 0);
1911 		return (0);
1912 	}
1913 	if (try == MUTEX_LOCK) {
1914 		DTRACE_PROBE2(plockstat, mutex__error, mp, EDEADLK);
1915 		return (EDEADLK);
1916 	}
1917 	return (EBUSY);
1918 }
1919 
1920 /*
1921  * Register this USYNC_PROCESS|LOCK_ROBUST mutex with the kernel so
1922  * it can apply LOCK_OWNERDEAD|LOCK_UNMAPPED if it becomes necessary.
1923  * We use tdb_hash_lock here and in the synch object tracking code in
1924  * the tdb_agent.c file.  There is no conflict between these two usages.
1925  */
1926 void
1927 register_lock(mutex_t *mp)
1928 {
1929 	uberdata_t *udp = curthread->ul_uberdata;
1930 	uint_t hash = LOCK_HASH(mp);
1931 	robust_t *rlp;
1932 	robust_t **rlpp;
1933 	robust_t **table;
1934 
1935 	if ((table = udp->robustlocks) == NULL) {
1936 		lmutex_lock(&udp->tdb_hash_lock);
1937 		if ((table = udp->robustlocks) == NULL) {
1938 			table = lmalloc(LOCKHASHSZ * sizeof (robust_t *));
1939 			membar_producer();
1940 			udp->robustlocks = table;
1941 		}
1942 		lmutex_unlock(&udp->tdb_hash_lock);
1943 	}
1944 	membar_consumer();
1945 
1946 	/*
1947 	 * First search the registered table with no locks held.
1948 	 * This is safe because the table never shrinks
1949 	 * and we can only get a false negative.
1950 	 */
1951 	for (rlp = table[hash]; rlp != NULL; rlp = rlp->robust_next) {
1952 		if (rlp->robust_lock == mp)	/* already registered */
1953 			return;
1954 	}
1955 
1956 	/*
1957 	 * The lock was not found.
1958 	 * Repeat the operation with tdb_hash_lock held.
1959 	 */
1960 	lmutex_lock(&udp->tdb_hash_lock);
1961 
1962 	for (rlpp = &table[hash];
1963 	    (rlp = *rlpp) != NULL;
1964 	    rlpp = &rlp->robust_next) {
1965 		if (rlp->robust_lock == mp) {	/* already registered */
1966 			lmutex_unlock(&udp->tdb_hash_lock);
1967 			return;
1968 		}
1969 	}
1970 
1971 	/*
1972 	 * The lock has never been registered.
1973 	 * Register it now and add it to the table.
1974 	 */
1975 	(void) ___lwp_mutex_register(mp);
1976 	rlp = lmalloc(sizeof (*rlp));
1977 	rlp->robust_lock = mp;
1978 	membar_producer();
1979 	*rlpp = rlp;
1980 
1981 	lmutex_unlock(&udp->tdb_hash_lock);
1982 }
1983 
1984 /*
1985  * This is called in the child of fork()/forkall() to start over
1986  * with a clean slate.  (Each process must register its own locks.)
1987  * No locks are needed because all other threads are suspended or gone.
1988  */
1989 void
1990 unregister_locks(void)
1991 {
1992 	uberdata_t *udp = curthread->ul_uberdata;
1993 	uint_t hash;
1994 	robust_t **table;
1995 	robust_t *rlp;
1996 	robust_t *next;
1997 
1998 	if ((table = udp->robustlocks) != NULL) {
1999 		for (hash = 0; hash < LOCKHASHSZ; hash++) {
2000 			rlp = table[hash];
2001 			while (rlp != NULL) {
2002 				next = rlp->robust_next;
2003 				lfree(rlp, sizeof (*rlp));
2004 				rlp = next;
2005 			}
2006 		}
2007 		lfree(table, LOCKHASHSZ * sizeof (robust_t *));
2008 		udp->robustlocks = NULL;
2009 	}
2010 }
2011 
2012 /*
2013  * Returns with mutex_owner set correctly.
2014  */
2015 int
2016 mutex_lock_internal(mutex_t *mp, timespec_t *tsp, int try)
2017 {
2018 	ulwp_t *self = curthread;
2019 	uberdata_t *udp = self->ul_uberdata;
2020 	int mtype = mp->mutex_type;
2021 	tdb_mutex_stats_t *msp = MUTEX_STATS(mp, udp);
2022 	int error = 0;
2023 	int noceil = try & MUTEX_NOCEIL;
2024 	uint8_t ceil;
2025 	int myprio;
2026 
2027 	try &= ~MUTEX_NOCEIL;
2028 	ASSERT(try == MUTEX_TRY || try == MUTEX_LOCK);
2029 
2030 	if (!self->ul_schedctl_called)
2031 		(void) setup_schedctl();
2032 
2033 	if (msp && try == MUTEX_TRY)
2034 		tdb_incr(msp->mutex_try);
2035 
2036 	if ((mtype & (LOCK_RECURSIVE|LOCK_ERRORCHECK)) && mutex_held(mp))
2037 		return (mutex_recursion(mp, mtype, try));
2038 
2039 	if (self->ul_error_detection && try == MUTEX_LOCK &&
2040 	    tsp == NULL && mutex_held(mp))
2041 		lock_error(mp, "mutex_lock", NULL, NULL);
2042 
2043 	if ((mtype & LOCK_PRIO_PROTECT) && noceil == 0) {
2044 		update_sched(self);
2045 		if (self->ul_cid != self->ul_rtclassid) {
2046 			DTRACE_PROBE2(plockstat, mutex__error, mp, EPERM);
2047 			return (EPERM);
2048 		}
2049 		ceil = mp->mutex_ceiling;
2050 		myprio = self->ul_epri? self->ul_epri : self->ul_pri;
2051 		if (myprio > ceil) {
2052 			DTRACE_PROBE2(plockstat, mutex__error, mp, EINVAL);
2053 			return (EINVAL);
2054 		}
2055 		if ((error = _ceil_mylist_add(mp)) != 0) {
2056 			DTRACE_PROBE2(plockstat, mutex__error, mp, error);
2057 			return (error);
2058 		}
2059 		if (myprio < ceil)
2060 			_ceil_prio_inherit(ceil);
2061 	}
2062 
2063 	if ((mtype & (USYNC_PROCESS | LOCK_ROBUST))
2064 	    == (USYNC_PROCESS | LOCK_ROBUST))
2065 		register_lock(mp);
2066 
2067 	if (mtype & LOCK_PRIO_INHERIT) {
2068 		/* go straight to the kernel */
2069 		if (try == MUTEX_TRY)
2070 			error = mutex_trylock_kernel(mp);
2071 		else	/* MUTEX_LOCK */
2072 			error = mutex_lock_kernel(mp, tsp, msp);
2073 		/*
2074 		 * The kernel never sets or clears the lock byte
2075 		 * for LOCK_PRIO_INHERIT mutexes.
2076 		 * Set it here for consistency.
2077 		 */
2078 		switch (error) {
2079 		case 0:
2080 			self->ul_pilocks++;
2081 			mp->mutex_lockw = LOCKSET;
2082 			break;
2083 		case EOWNERDEAD:
2084 		case ELOCKUNMAPPED:
2085 			self->ul_pilocks++;
2086 			mp->mutex_lockw = LOCKSET;
2087 			/* FALLTHROUGH */
2088 		case ENOTRECOVERABLE:
2089 			ASSERT(mtype & LOCK_ROBUST);
2090 			break;
2091 		case EDEADLK:
2092 			if (try == MUTEX_TRY) {
2093 				error = EBUSY;
2094 			} else if (tsp != NULL) {	/* simulate a timeout */
2095 				/*
2096 				 * Note: mutex_timedlock() never returns EINTR.
2097 				 */
2098 				timespec_t ts = *tsp;
2099 				timespec_t rts;
2100 
2101 				while (__nanosleep(&ts, &rts) == EINTR)
2102 					ts = rts;
2103 				error = ETIME;
2104 			} else {		/* simulate a deadlock */
2105 				stall();
2106 			}
2107 			break;
2108 		}
2109 	} else if (mtype & USYNC_PROCESS) {
2110 		error = mutex_trylock_process(mp, try == MUTEX_LOCK);
2111 		if (error == EBUSY && try == MUTEX_LOCK)
2112 			error = mutex_lock_kernel(mp, tsp, msp);
2113 	} else {	/* USYNC_THREAD */
2114 		error = mutex_trylock_adaptive(mp, try == MUTEX_LOCK);
2115 		if (error == EBUSY && try == MUTEX_LOCK)
2116 			error = mutex_lock_queue(self, msp, mp, tsp);
2117 	}
2118 
2119 	switch (error) {
2120 	case 0:
2121 	case EOWNERDEAD:
2122 	case ELOCKUNMAPPED:
2123 		if (mtype & LOCK_ROBUST)
2124 			remember_lock(mp);
2125 		if (msp)
2126 			record_begin_hold(msp);
2127 		break;
2128 	default:
2129 		if ((mtype & LOCK_PRIO_PROTECT) && noceil == 0) {
2130 			(void) _ceil_mylist_del(mp);
2131 			if (myprio < ceil)
2132 				_ceil_prio_waive();
2133 		}
2134 		if (try == MUTEX_TRY) {
2135 			if (msp)
2136 				tdb_incr(msp->mutex_try_fail);
2137 			if (__td_event_report(self, TD_LOCK_TRY, udp)) {
2138 				self->ul_td_evbuf.eventnum = TD_LOCK_TRY;
2139 				tdb_event(TD_LOCK_TRY, udp);
2140 			}
2141 		}
2142 		break;
2143 	}
2144 
2145 	return (error);
2146 }
2147 
2148 int
2149 fast_process_lock(mutex_t *mp, timespec_t *tsp, int mtype, int try)
2150 {
2151 	ulwp_t *self = curthread;
2152 	uberdata_t *udp = self->ul_uberdata;
2153 
2154 	/*
2155 	 * We know that USYNC_PROCESS is set in mtype and that
2156 	 * zero, one, or both of the flags LOCK_RECURSIVE and
2157 	 * LOCK_ERRORCHECK are set, and that no other flags are set.
2158 	 */
2159 	ASSERT((mtype & ~(USYNC_PROCESS|LOCK_RECURSIVE|LOCK_ERRORCHECK)) == 0);
2160 	enter_critical(self);
2161 #if defined(__sparc) && !defined(_LP64)
2162 	/* horrible hack, necessary only on 32-bit sparc */
2163 	if (((uintptr_t)mp & (_LONG_LONG_ALIGNMENT - 1)) &&
2164 	    self->ul_misaligned) {
2165 		if (set_lock_byte(&mp->mutex_lockw) == 0) {
2166 			mp->mutex_ownerpid = udp->pid;
2167 			mp->mutex_owner = (uintptr_t)self;
2168 			exit_critical(self);
2169 			DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0);
2170 			return (0);
2171 		}
2172 	} else
2173 #endif
2174 	if (set_lock_byte64(&mp->mutex_lockword64, udp->pid) == 0) {
2175 		mp->mutex_owner = (uintptr_t)self;
2176 		/* mp->mutex_ownerpid was set by set_lock_byte64() */
2177 		exit_critical(self);
2178 		DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0);
2179 		return (0);
2180 	}
2181 	exit_critical(self);
2182 
2183 	if ((mtype & (LOCK_RECURSIVE|LOCK_ERRORCHECK)) && shared_mutex_held(mp))
2184 		return (mutex_recursion(mp, mtype, try));
2185 
2186 	if (try == MUTEX_LOCK) {
2187 		if (mutex_trylock_process(mp, 1) == 0)
2188 			return (0);
2189 		return (mutex_lock_kernel(mp, tsp, NULL));
2190 	}
2191 
2192 	if (__td_event_report(self, TD_LOCK_TRY, udp)) {
2193 		self->ul_td_evbuf.eventnum = TD_LOCK_TRY;
2194 		tdb_event(TD_LOCK_TRY, udp);
2195 	}
2196 	return (EBUSY);
2197 }
2198 
2199 static int
2200 mutex_lock_impl(mutex_t *mp, timespec_t *tsp)
2201 {
2202 	ulwp_t *self = curthread;
2203 	int mtype = mp->mutex_type;
2204 	uberflags_t *gflags;
2205 
2206 	if (((uintptr_t)mp & (_LONG_LONG_ALIGNMENT - 1)) &&
2207 	    self->ul_error_detection && self->ul_misaligned == 0)
2208 		lock_error(mp, "mutex_lock", NULL, "mutex is misaligned");
2209 
2210 	/*
2211 	 * Optimize the case of USYNC_THREAD, including
2212 	 * the LOCK_RECURSIVE and LOCK_ERRORCHECK cases,
2213 	 * no error detection, no lock statistics,
2214 	 * and the process has only a single thread.
2215 	 * (Most likely a traditional single-threaded application.)
2216 	 */
2217 	if (((mtype & ~(LOCK_RECURSIVE|LOCK_ERRORCHECK)) |
2218 	    self->ul_uberdata->uberflags.uf_all) == 0) {
2219 		/*
2220 		 * Only one thread exists so we don't need an atomic operation.
2221 		 */
2222 		if (mp->mutex_lockw == 0) {
2223 			mp->mutex_lockw = LOCKSET;
2224 			mp->mutex_owner = (uintptr_t)self;
2225 			DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0);
2226 			return (0);
2227 		}
2228 		if (mtype && MUTEX_OWNER(mp) == self)
2229 			return (mutex_recursion(mp, mtype, MUTEX_LOCK));
2230 		/*
2231 		 * We have reached a deadlock, probably because the
2232 		 * process is executing non-async-signal-safe code in
2233 		 * a signal handler and is attempting to acquire a lock
2234 		 * that it already owns.  This is not surprising, given
2235 		 * bad programming practices over the years that has
2236 		 * resulted in applications calling printf() and such
2237 		 * in their signal handlers.  Unless the user has told
2238 		 * us that the signal handlers are safe by setting:
2239 		 *	export _THREAD_ASYNC_SAFE=1
2240 		 * we return EDEADLK rather than actually deadlocking.
2241 		 */
2242 		if (tsp == NULL &&
2243 		    MUTEX_OWNER(mp) == self && !self->ul_async_safe) {
2244 			DTRACE_PROBE2(plockstat, mutex__error, mp, EDEADLK);
2245 			return (EDEADLK);
2246 		}
2247 	}
2248 
2249 	/*
2250 	 * Optimize the common cases of USYNC_THREAD or USYNC_PROCESS,
2251 	 * no error detection, and no lock statistics.
2252 	 * Include LOCK_RECURSIVE and LOCK_ERRORCHECK cases.
2253 	 */
2254 	if ((gflags = self->ul_schedctl_called) != NULL &&
2255 	    (gflags->uf_trs_ted |
2256 	    (mtype & ~(USYNC_PROCESS|LOCK_RECURSIVE|LOCK_ERRORCHECK))) == 0) {
2257 		if (mtype & USYNC_PROCESS)
2258 			return (fast_process_lock(mp, tsp, mtype, MUTEX_LOCK));
2259 		if (set_lock_byte(&mp->mutex_lockw) == 0) {
2260 			mp->mutex_owner = (uintptr_t)self;
2261 			DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0);
2262 			return (0);
2263 		}
2264 		if (mtype && MUTEX_OWNER(mp) == self)
2265 			return (mutex_recursion(mp, mtype, MUTEX_LOCK));
2266 		if (mutex_trylock_adaptive(mp, 1) != 0)
2267 			return (mutex_lock_queue(self, NULL, mp, tsp));
2268 		return (0);
2269 	}
2270 
2271 	/* else do it the long way */
2272 	return (mutex_lock_internal(mp, tsp, MUTEX_LOCK));
2273 }
2274 
2275 #pragma weak pthread_mutex_lock = mutex_lock
2276 #pragma weak _mutex_lock = mutex_lock
2277 int
2278 mutex_lock(mutex_t *mp)
2279 {
2280 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
2281 	return (mutex_lock_impl(mp, NULL));
2282 }
2283 
2284 int
2285 pthread_mutex_timedlock(pthread_mutex_t *_RESTRICT_KYWD mp,
2286 	const struct timespec *_RESTRICT_KYWD abstime)
2287 {
2288 	timespec_t tslocal;
2289 	int error;
2290 
2291 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
2292 	abstime_to_reltime(CLOCK_REALTIME, abstime, &tslocal);
2293 	error = mutex_lock_impl((mutex_t *)mp, &tslocal);
2294 	if (error == ETIME)
2295 		error = ETIMEDOUT;
2296 	return (error);
2297 }
2298 
2299 int
2300 pthread_mutex_reltimedlock_np(pthread_mutex_t *_RESTRICT_KYWD mp,
2301 	const struct timespec *_RESTRICT_KYWD reltime)
2302 {
2303 	timespec_t tslocal;
2304 	int error;
2305 
2306 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
2307 	tslocal = *reltime;
2308 	error = mutex_lock_impl((mutex_t *)mp, &tslocal);
2309 	if (error == ETIME)
2310 		error = ETIMEDOUT;
2311 	return (error);
2312 }
2313 
2314 #pragma weak pthread_mutex_trylock = mutex_trylock
2315 int
2316 mutex_trylock(mutex_t *mp)
2317 {
2318 	ulwp_t *self = curthread;
2319 	uberdata_t *udp = self->ul_uberdata;
2320 	int mtype = mp->mutex_type;
2321 	uberflags_t *gflags;
2322 
2323 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
2324 
2325 	/*
2326 	 * Optimize the case of USYNC_THREAD, including
2327 	 * the LOCK_RECURSIVE and LOCK_ERRORCHECK cases,
2328 	 * no error detection, no lock statistics,
2329 	 * and the process has only a single thread.
2330 	 * (Most likely a traditional single-threaded application.)
2331 	 */
2332 	if (((mtype & ~(LOCK_RECURSIVE|LOCK_ERRORCHECK)) |
2333 	    udp->uberflags.uf_all) == 0) {
2334 		/*
2335 		 * Only one thread exists so we don't need an atomic operation.
2336 		 */
2337 		if (mp->mutex_lockw == 0) {
2338 			mp->mutex_lockw = LOCKSET;
2339 			mp->mutex_owner = (uintptr_t)self;
2340 			DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0);
2341 			return (0);
2342 		}
2343 		if (mtype && MUTEX_OWNER(mp) == self)
2344 			return (mutex_recursion(mp, mtype, MUTEX_TRY));
2345 		return (EBUSY);
2346 	}
2347 
2348 	/*
2349 	 * Optimize the common cases of USYNC_THREAD or USYNC_PROCESS,
2350 	 * no error detection, and no lock statistics.
2351 	 * Include LOCK_RECURSIVE and LOCK_ERRORCHECK cases.
2352 	 */
2353 	if ((gflags = self->ul_schedctl_called) != NULL &&
2354 	    (gflags->uf_trs_ted |
2355 	    (mtype & ~(USYNC_PROCESS|LOCK_RECURSIVE|LOCK_ERRORCHECK))) == 0) {
2356 		if (mtype & USYNC_PROCESS)
2357 			return (fast_process_lock(mp, NULL, mtype, MUTEX_TRY));
2358 		if (set_lock_byte(&mp->mutex_lockw) == 0) {
2359 			mp->mutex_owner = (uintptr_t)self;
2360 			DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0);
2361 			return (0);
2362 		}
2363 		if (mtype && MUTEX_OWNER(mp) == self)
2364 			return (mutex_recursion(mp, mtype, MUTEX_TRY));
2365 		if (__td_event_report(self, TD_LOCK_TRY, udp)) {
2366 			self->ul_td_evbuf.eventnum = TD_LOCK_TRY;
2367 			tdb_event(TD_LOCK_TRY, udp);
2368 		}
2369 		return (EBUSY);
2370 	}
2371 
2372 	/* else do it the long way */
2373 	return (mutex_lock_internal(mp, NULL, MUTEX_TRY));
2374 }
2375 
2376 int
2377 mutex_unlock_internal(mutex_t *mp, int retain_robust_flags)
2378 {
2379 	ulwp_t *self = curthread;
2380 	uberdata_t *udp = self->ul_uberdata;
2381 	int mtype = mp->mutex_type;
2382 	tdb_mutex_stats_t *msp;
2383 	int error = 0;
2384 	int release_all;
2385 	lwpid_t lwpid;
2386 
2387 	if ((mtype & LOCK_ERRORCHECK) && !mutex_held(mp))
2388 		return (EPERM);
2389 
2390 	if (self->ul_error_detection && !mutex_held(mp))
2391 		lock_error(mp, "mutex_unlock", NULL, NULL);
2392 
2393 	if ((mtype & LOCK_RECURSIVE) && mp->mutex_rcount != 0) {
2394 		mp->mutex_rcount--;
2395 		DTRACE_PROBE2(plockstat, mutex__release, mp, 1);
2396 		return (0);
2397 	}
2398 
2399 	if ((msp = MUTEX_STATS(mp, udp)) != NULL)
2400 		(void) record_hold_time(msp);
2401 
2402 	if (!retain_robust_flags && !(mtype & LOCK_PRIO_INHERIT) &&
2403 	    (mp->mutex_flag & (LOCK_OWNERDEAD | LOCK_UNMAPPED))) {
2404 		ASSERT(mp->mutex_type & LOCK_ROBUST);
2405 		mp->mutex_flag &= ~(LOCK_OWNERDEAD | LOCK_UNMAPPED);
2406 		mp->mutex_flag |= LOCK_NOTRECOVERABLE;
2407 	}
2408 	release_all = ((mp->mutex_flag & LOCK_NOTRECOVERABLE) != 0);
2409 
2410 	if (mtype & LOCK_PRIO_INHERIT) {
2411 		no_preempt(self);
2412 		mp->mutex_owner = 0;
2413 		/* mp->mutex_ownerpid is cleared by ___lwp_mutex_unlock() */
2414 		DTRACE_PROBE2(plockstat, mutex__release, mp, 0);
2415 		mp->mutex_lockw = LOCKCLEAR;
2416 		self->ul_pilocks--;
2417 		error = ___lwp_mutex_unlock(mp);
2418 		preempt(self);
2419 	} else if (mtype & USYNC_PROCESS) {
2420 		mutex_unlock_process(mp, release_all);
2421 	} else {	/* USYNC_THREAD */
2422 		if ((lwpid = mutex_unlock_queue(mp, release_all)) != 0) {
2423 			(void) __lwp_unpark(lwpid);
2424 			preempt(self);
2425 		}
2426 	}
2427 
2428 	if (mtype & LOCK_ROBUST)
2429 		forget_lock(mp);
2430 
2431 	if ((mtype & LOCK_PRIO_PROTECT) && _ceil_mylist_del(mp))
2432 		_ceil_prio_waive();
2433 
2434 	return (error);
2435 }
2436 
2437 #pragma weak pthread_mutex_unlock = mutex_unlock
2438 #pragma weak _mutex_unlock = mutex_unlock
2439 int
2440 mutex_unlock(mutex_t *mp)
2441 {
2442 	ulwp_t *self = curthread;
2443 	int mtype = mp->mutex_type;
2444 	uberflags_t *gflags;
2445 	lwpid_t lwpid;
2446 	short el;
2447 
2448 	/*
2449 	 * Optimize the case of USYNC_THREAD, including
2450 	 * the LOCK_RECURSIVE and LOCK_ERRORCHECK cases,
2451 	 * no error detection, no lock statistics,
2452 	 * and the process has only a single thread.
2453 	 * (Most likely a traditional single-threaded application.)
2454 	 */
2455 	if (((mtype & ~(LOCK_RECURSIVE|LOCK_ERRORCHECK)) |
2456 	    self->ul_uberdata->uberflags.uf_all) == 0) {
2457 		if (mtype) {
2458 			/*
2459 			 * At this point we know that one or both of the
2460 			 * flags LOCK_RECURSIVE or LOCK_ERRORCHECK is set.
2461 			 */
2462 			if ((mtype & LOCK_ERRORCHECK) && !MUTEX_OWNED(mp, self))
2463 				return (EPERM);
2464 			if ((mtype & LOCK_RECURSIVE) && mp->mutex_rcount != 0) {
2465 				mp->mutex_rcount--;
2466 				DTRACE_PROBE2(plockstat, mutex__release, mp, 1);
2467 				return (0);
2468 			}
2469 		}
2470 		/*
2471 		 * Only one thread exists so we don't need an atomic operation.
2472 		 * Also, there can be no waiters.
2473 		 */
2474 		mp->mutex_owner = 0;
2475 		mp->mutex_lockword = 0;
2476 		DTRACE_PROBE2(plockstat, mutex__release, mp, 0);
2477 		return (0);
2478 	}
2479 
2480 	/*
2481 	 * Optimize the common cases of USYNC_THREAD or USYNC_PROCESS,
2482 	 * no error detection, and no lock statistics.
2483 	 * Include LOCK_RECURSIVE and LOCK_ERRORCHECK cases.
2484 	 */
2485 	if ((gflags = self->ul_schedctl_called) != NULL) {
2486 		if (((el = gflags->uf_trs_ted) | mtype) == 0) {
2487 fast_unlock:
2488 			if ((lwpid = mutex_unlock_queue(mp, 0)) != 0) {
2489 				(void) __lwp_unpark(lwpid);
2490 				preempt(self);
2491 			}
2492 			return (0);
2493 		}
2494 		if (el)		/* error detection or lock statistics */
2495 			goto slow_unlock;
2496 		if ((mtype & ~(LOCK_RECURSIVE|LOCK_ERRORCHECK)) == 0) {
2497 			/*
2498 			 * At this point we know that one or both of the
2499 			 * flags LOCK_RECURSIVE or LOCK_ERRORCHECK is set.
2500 			 */
2501 			if ((mtype & LOCK_ERRORCHECK) && !MUTEX_OWNED(mp, self))
2502 				return (EPERM);
2503 			if ((mtype & LOCK_RECURSIVE) && mp->mutex_rcount != 0) {
2504 				mp->mutex_rcount--;
2505 				DTRACE_PROBE2(plockstat, mutex__release, mp, 1);
2506 				return (0);
2507 			}
2508 			goto fast_unlock;
2509 		}
2510 		if ((mtype &
2511 		    ~(USYNC_PROCESS|LOCK_RECURSIVE|LOCK_ERRORCHECK)) == 0) {
2512 			/*
2513 			 * At this point we know that zero, one, or both of the
2514 			 * flags LOCK_RECURSIVE or LOCK_ERRORCHECK is set and
2515 			 * that the USYNC_PROCESS flag is set.
2516 			 */
2517 			if ((mtype & LOCK_ERRORCHECK) && !shared_mutex_held(mp))
2518 				return (EPERM);
2519 			if ((mtype & LOCK_RECURSIVE) && mp->mutex_rcount != 0) {
2520 				mp->mutex_rcount--;
2521 				DTRACE_PROBE2(plockstat, mutex__release, mp, 1);
2522 				return (0);
2523 			}
2524 			mutex_unlock_process(mp, 0);
2525 			return (0);
2526 		}
2527 	}
2528 
2529 	/* else do it the long way */
2530 slow_unlock:
2531 	return (mutex_unlock_internal(mp, 0));
2532 }
2533 
2534 /*
2535  * Internally to the library, almost all mutex lock/unlock actions
2536  * go through these lmutex_ functions, to protect critical regions.
2537  * We replicate a bit of code from mutex_lock() and mutex_unlock()
2538  * to make these functions faster since we know that the mutex type
2539  * of all internal locks is USYNC_THREAD.  We also know that internal
2540  * locking can never fail, so we panic if it does.
2541  */
2542 void
2543 lmutex_lock(mutex_t *mp)
2544 {
2545 	ulwp_t *self = curthread;
2546 	uberdata_t *udp = self->ul_uberdata;
2547 
2548 	ASSERT(mp->mutex_type == USYNC_THREAD);
2549 
2550 	enter_critical(self);
2551 	/*
2552 	 * Optimize the case of no lock statistics and only a single thread.
2553 	 * (Most likely a traditional single-threaded application.)
2554 	 */
2555 	if (udp->uberflags.uf_all == 0) {
2556 		/*
2557 		 * Only one thread exists; the mutex must be free.
2558 		 */
2559 		ASSERT(mp->mutex_lockw == 0);
2560 		mp->mutex_lockw = LOCKSET;
2561 		mp->mutex_owner = (uintptr_t)self;
2562 		DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0);
2563 	} else {
2564 		tdb_mutex_stats_t *msp = MUTEX_STATS(mp, udp);
2565 
2566 		if (!self->ul_schedctl_called)
2567 			(void) setup_schedctl();
2568 
2569 		if (set_lock_byte(&mp->mutex_lockw) == 0) {
2570 			mp->mutex_owner = (uintptr_t)self;
2571 			DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0);
2572 		} else if (mutex_trylock_adaptive(mp, 1) != 0) {
2573 			(void) mutex_lock_queue(self, msp, mp, NULL);
2574 		}
2575 
2576 		if (msp)
2577 			record_begin_hold(msp);
2578 	}
2579 }
2580 
2581 void
2582 lmutex_unlock(mutex_t *mp)
2583 {
2584 	ulwp_t *self = curthread;
2585 	uberdata_t *udp = self->ul_uberdata;
2586 
2587 	ASSERT(mp->mutex_type == USYNC_THREAD);
2588 
2589 	/*
2590 	 * Optimize the case of no lock statistics and only a single thread.
2591 	 * (Most likely a traditional single-threaded application.)
2592 	 */
2593 	if (udp->uberflags.uf_all == 0) {
2594 		/*
2595 		 * Only one thread exists so there can be no waiters.
2596 		 */
2597 		mp->mutex_owner = 0;
2598 		mp->mutex_lockword = 0;
2599 		DTRACE_PROBE2(plockstat, mutex__release, mp, 0);
2600 	} else {
2601 		tdb_mutex_stats_t *msp = MUTEX_STATS(mp, udp);
2602 		lwpid_t lwpid;
2603 
2604 		if (msp)
2605 			(void) record_hold_time(msp);
2606 		if ((lwpid = mutex_unlock_queue(mp, 0)) != 0) {
2607 			(void) __lwp_unpark(lwpid);
2608 			preempt(self);
2609 		}
2610 	}
2611 	exit_critical(self);
2612 }
2613 
2614 /*
2615  * For specialized code in libc, like the asynchronous i/o code,
2616  * the following sig_*() locking primitives are used in order
2617  * to make the code asynchronous signal safe.  Signals are
2618  * deferred while locks acquired by these functions are held.
2619  */
2620 void
2621 sig_mutex_lock(mutex_t *mp)
2622 {
2623 	sigoff(curthread);
2624 	(void) mutex_lock(mp);
2625 }
2626 
2627 void
2628 sig_mutex_unlock(mutex_t *mp)
2629 {
2630 	(void) mutex_unlock(mp);
2631 	sigon(curthread);
2632 }
2633 
2634 int
2635 sig_mutex_trylock(mutex_t *mp)
2636 {
2637 	int error;
2638 
2639 	sigoff(curthread);
2640 	if ((error = mutex_trylock(mp)) != 0)
2641 		sigon(curthread);
2642 	return (error);
2643 }
2644 
2645 /*
2646  * sig_cond_wait() is a cancellation point.
2647  */
2648 int
2649 sig_cond_wait(cond_t *cv, mutex_t *mp)
2650 {
2651 	int error;
2652 
2653 	ASSERT(curthread->ul_sigdefer != 0);
2654 	pthread_testcancel();
2655 	error = __cond_wait(cv, mp);
2656 	if (error == EINTR && curthread->ul_cursig) {
2657 		sig_mutex_unlock(mp);
2658 		/* take the deferred signal here */
2659 		sig_mutex_lock(mp);
2660 	}
2661 	pthread_testcancel();
2662 	return (error);
2663 }
2664 
2665 /*
2666  * sig_cond_reltimedwait() is a cancellation point.
2667  */
2668 int
2669 sig_cond_reltimedwait(cond_t *cv, mutex_t *mp, const timespec_t *ts)
2670 {
2671 	int error;
2672 
2673 	ASSERT(curthread->ul_sigdefer != 0);
2674 	pthread_testcancel();
2675 	error = __cond_reltimedwait(cv, mp, ts);
2676 	if (error == EINTR && curthread->ul_cursig) {
2677 		sig_mutex_unlock(mp);
2678 		/* take the deferred signal here */
2679 		sig_mutex_lock(mp);
2680 	}
2681 	pthread_testcancel();
2682 	return (error);
2683 }
2684 
2685 /*
2686  * For specialized code in libc, like the stdio code.
2687  * the following cancel_safe_*() locking primitives are used in
2688  * order to make the code cancellation-safe.  Cancellation is
2689  * deferred while locks acquired by these functions are held.
2690  */
2691 void
2692 cancel_safe_mutex_lock(mutex_t *mp)
2693 {
2694 	(void) mutex_lock(mp);
2695 	curthread->ul_libc_locks++;
2696 }
2697 
2698 int
2699 cancel_safe_mutex_trylock(mutex_t *mp)
2700 {
2701 	int error;
2702 
2703 	if ((error = mutex_trylock(mp)) == 0)
2704 		curthread->ul_libc_locks++;
2705 	return (error);
2706 }
2707 
2708 void
2709 cancel_safe_mutex_unlock(mutex_t *mp)
2710 {
2711 	ulwp_t *self = curthread;
2712 
2713 	ASSERT(self->ul_libc_locks != 0);
2714 
2715 	(void) mutex_unlock(mp);
2716 
2717 	/*
2718 	 * Decrement the count of locks held by cancel_safe_mutex_lock().
2719 	 * If we are then in a position to terminate cleanly and
2720 	 * if there is a pending cancellation and cancellation
2721 	 * is not disabled and we received EINTR from a recent
2722 	 * system call then perform the cancellation action now.
2723 	 */
2724 	if (--self->ul_libc_locks == 0 &&
2725 	    !(self->ul_vfork | self->ul_nocancel |
2726 	    self->ul_critical | self->ul_sigdefer) &&
2727 	    cancel_active())
2728 		pthread_exit(PTHREAD_CANCELED);
2729 }
2730 
2731 static int
2732 shared_mutex_held(mutex_t *mparg)
2733 {
2734 	/*
2735 	 * The 'volatile' is necessary to make sure the compiler doesn't
2736 	 * reorder the tests of the various components of the mutex.
2737 	 * They must be tested in this order:
2738 	 *	mutex_lockw
2739 	 *	mutex_owner
2740 	 *	mutex_ownerpid
2741 	 * This relies on the fact that everywhere mutex_lockw is cleared,
2742 	 * mutex_owner and mutex_ownerpid are cleared before mutex_lockw
2743 	 * is cleared, and that everywhere mutex_lockw is set, mutex_owner
2744 	 * and mutex_ownerpid are set after mutex_lockw is set, and that
2745 	 * mutex_lockw is set or cleared with a memory barrier.
2746 	 */
2747 	volatile mutex_t *mp = (volatile mutex_t *)mparg;
2748 	ulwp_t *self = curthread;
2749 	uberdata_t *udp = self->ul_uberdata;
2750 
2751 	return (MUTEX_OWNED(mp, self) && mp->mutex_ownerpid == udp->pid);
2752 }
2753 
2754 #pragma weak _mutex_held = mutex_held
2755 int
2756 mutex_held(mutex_t *mparg)
2757 {
2758 	volatile mutex_t *mp = (volatile mutex_t *)mparg;
2759 
2760 	if (mparg->mutex_type & USYNC_PROCESS)
2761 		return (shared_mutex_held(mparg));
2762 	return (MUTEX_OWNED(mp, curthread));
2763 }
2764 
2765 #pragma weak pthread_mutex_destroy = mutex_destroy
2766 #pragma weak _mutex_destroy = mutex_destroy
2767 int
2768 mutex_destroy(mutex_t *mp)
2769 {
2770 	if (mp->mutex_type & USYNC_PROCESS)
2771 		forget_lock(mp);
2772 	(void) memset(mp, 0, sizeof (*mp));
2773 	tdb_sync_obj_deregister(mp);
2774 	return (0);
2775 }
2776 
2777 #pragma weak pthread_mutex_consistent_np = mutex_consistent
2778 int
2779 mutex_consistent(mutex_t *mp)
2780 {
2781 	/*
2782 	 * Do this only for an inconsistent, initialized robust lock
2783 	 * that we hold.  For all other cases, return EINVAL.
2784 	 */
2785 	if (mutex_held(mp) &&
2786 	    (mp->mutex_type & LOCK_ROBUST) &&
2787 	    (mp->mutex_flag & LOCK_INITED) &&
2788 	    (mp->mutex_flag & (LOCK_OWNERDEAD | LOCK_UNMAPPED))) {
2789 		mp->mutex_flag &= ~(LOCK_OWNERDEAD | LOCK_UNMAPPED);
2790 		mp->mutex_rcount = 0;
2791 		return (0);
2792 	}
2793 	return (EINVAL);
2794 }
2795 
2796 /*
2797  * Spin locks are separate from ordinary mutexes,
2798  * but we use the same data structure for them.
2799  */
2800 
2801 int
2802 pthread_spin_init(pthread_spinlock_t *lock, int pshared)
2803 {
2804 	mutex_t *mp = (mutex_t *)lock;
2805 
2806 	(void) memset(mp, 0, sizeof (*mp));
2807 	if (pshared == PTHREAD_PROCESS_SHARED)
2808 		mp->mutex_type = USYNC_PROCESS;
2809 	else
2810 		mp->mutex_type = USYNC_THREAD;
2811 	mp->mutex_flag = LOCK_INITED;
2812 	mp->mutex_magic = MUTEX_MAGIC;
2813 
2814 	/*
2815 	 * This should be at the beginning of the function,
2816 	 * but for the sake of old broken applications that
2817 	 * do not have proper alignment for their mutexes
2818 	 * (and don't check the return code from pthread_spin_init),
2819 	 * we put it here, after initializing the mutex regardless.
2820 	 */
2821 	if (((uintptr_t)mp & (_LONG_LONG_ALIGNMENT - 1)) &&
2822 	    curthread->ul_misaligned == 0)
2823 		return (EINVAL);
2824 
2825 	return (0);
2826 }
2827 
2828 int
2829 pthread_spin_destroy(pthread_spinlock_t *lock)
2830 {
2831 	(void) memset(lock, 0, sizeof (*lock));
2832 	return (0);
2833 }
2834 
2835 int
2836 pthread_spin_trylock(pthread_spinlock_t *lock)
2837 {
2838 	mutex_t *mp = (mutex_t *)lock;
2839 	ulwp_t *self = curthread;
2840 	int error = 0;
2841 
2842 	no_preempt(self);
2843 	if (set_lock_byte(&mp->mutex_lockw) != 0)
2844 		error = EBUSY;
2845 	else {
2846 		mp->mutex_owner = (uintptr_t)self;
2847 		if (mp->mutex_type == USYNC_PROCESS)
2848 			mp->mutex_ownerpid = self->ul_uberdata->pid;
2849 		DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, 0);
2850 	}
2851 	preempt(self);
2852 	return (error);
2853 }
2854 
2855 int
2856 pthread_spin_lock(pthread_spinlock_t *lock)
2857 {
2858 	mutex_t *mp = (mutex_t *)lock;
2859 	ulwp_t *self = curthread;
2860 	volatile uint8_t *lockp = (volatile uint8_t *)&mp->mutex_lockw;
2861 	int count = 0;
2862 
2863 	ASSERT(!self->ul_critical || self->ul_bindflags);
2864 
2865 	DTRACE_PROBE1(plockstat, mutex__spin, mp);
2866 
2867 	/*
2868 	 * We don't care whether the owner is running on a processor.
2869 	 * We just spin because that's what this interface requires.
2870 	 */
2871 	for (;;) {
2872 		if (*lockp == 0) {	/* lock byte appears to be clear */
2873 			no_preempt(self);
2874 			if (set_lock_byte(lockp) == 0)
2875 				break;
2876 			preempt(self);
2877 		}
2878 		if (count < INT_MAX)
2879 			count++;
2880 		SMT_PAUSE();
2881 	}
2882 	mp->mutex_owner = (uintptr_t)self;
2883 	if (mp->mutex_type == USYNC_PROCESS)
2884 		mp->mutex_ownerpid = self->ul_uberdata->pid;
2885 	preempt(self);
2886 	if (count) {
2887 		DTRACE_PROBE2(plockstat, mutex__spun, 1, count);
2888 	}
2889 	DTRACE_PROBE3(plockstat, mutex__acquire, mp, 0, count);
2890 	return (0);
2891 }
2892 
2893 int
2894 pthread_spin_unlock(pthread_spinlock_t *lock)
2895 {
2896 	mutex_t *mp = (mutex_t *)lock;
2897 	ulwp_t *self = curthread;
2898 
2899 	no_preempt(self);
2900 	mp->mutex_owner = 0;
2901 	mp->mutex_ownerpid = 0;
2902 	DTRACE_PROBE2(plockstat, mutex__release, mp, 0);
2903 	(void) atomic_swap_32(&mp->mutex_lockword, 0);
2904 	preempt(self);
2905 	return (0);
2906 }
2907 
2908 #define	INITIAL_LOCKS	8	/* initial size of ul_heldlocks.array */
2909 
2910 /*
2911  * Find/allocate an entry for 'lock' in our array of held locks.
2912  */
2913 static mutex_t **
2914 find_lock_entry(mutex_t *lock)
2915 {
2916 	ulwp_t *self = curthread;
2917 	mutex_t **remembered = NULL;
2918 	mutex_t **lockptr;
2919 	uint_t nlocks;
2920 
2921 	if ((nlocks = self->ul_heldlockcnt) != 0)
2922 		lockptr = self->ul_heldlocks.array;
2923 	else {
2924 		nlocks = 1;
2925 		lockptr = &self->ul_heldlocks.single;
2926 	}
2927 
2928 	for (; nlocks; nlocks--, lockptr++) {
2929 		if (*lockptr == lock)
2930 			return (lockptr);
2931 		if (*lockptr == NULL && remembered == NULL)
2932 			remembered = lockptr;
2933 	}
2934 	if (remembered != NULL) {
2935 		*remembered = lock;
2936 		return (remembered);
2937 	}
2938 
2939 	/*
2940 	 * No entry available.  Allocate more space, converting
2941 	 * the single entry into an array of entries if necessary.
2942 	 */
2943 	if ((nlocks = self->ul_heldlockcnt) == 0) {
2944 		/*
2945 		 * Initial allocation of the array.
2946 		 * Convert the single entry into an array.
2947 		 */
2948 		self->ul_heldlockcnt = nlocks = INITIAL_LOCKS;
2949 		lockptr = lmalloc(nlocks * sizeof (mutex_t *));
2950 		/*
2951 		 * The single entry becomes the first entry in the array.
2952 		 */
2953 		*lockptr = self->ul_heldlocks.single;
2954 		self->ul_heldlocks.array = lockptr;
2955 		/*
2956 		 * Return the next available entry in the array.
2957 		 */
2958 		*++lockptr = lock;
2959 		return (lockptr);
2960 	}
2961 	/*
2962 	 * Reallocate the array, double the size each time.
2963 	 */
2964 	lockptr = lmalloc(nlocks * 2 * sizeof (mutex_t *));
2965 	(void) memcpy(lockptr, self->ul_heldlocks.array,
2966 	    nlocks * sizeof (mutex_t *));
2967 	lfree(self->ul_heldlocks.array, nlocks * sizeof (mutex_t *));
2968 	self->ul_heldlocks.array = lockptr;
2969 	self->ul_heldlockcnt *= 2;
2970 	/*
2971 	 * Return the next available entry in the newly allocated array.
2972 	 */
2973 	*(lockptr += nlocks) = lock;
2974 	return (lockptr);
2975 }
2976 
2977 /*
2978  * Insert 'lock' into our list of held locks.
2979  * Currently only used for LOCK_ROBUST mutexes.
2980  */
2981 void
2982 remember_lock(mutex_t *lock)
2983 {
2984 	(void) find_lock_entry(lock);
2985 }
2986 
2987 /*
2988  * Remove 'lock' from our list of held locks.
2989  * Currently only used for LOCK_ROBUST mutexes.
2990  */
2991 void
2992 forget_lock(mutex_t *lock)
2993 {
2994 	*find_lock_entry(lock) = NULL;
2995 }
2996 
2997 /*
2998  * Free the array of held locks.
2999  */
3000 void
3001 heldlock_free(ulwp_t *ulwp)
3002 {
3003 	uint_t nlocks;
3004 
3005 	if ((nlocks = ulwp->ul_heldlockcnt) != 0)
3006 		lfree(ulwp->ul_heldlocks.array, nlocks * sizeof (mutex_t *));
3007 	ulwp->ul_heldlockcnt = 0;
3008 	ulwp->ul_heldlocks.array = NULL;
3009 }
3010 
3011 /*
3012  * Mark all held LOCK_ROBUST mutexes LOCK_OWNERDEAD.
3013  * Called from _thrp_exit() to deal with abandoned locks.
3014  */
3015 void
3016 heldlock_exit(void)
3017 {
3018 	ulwp_t *self = curthread;
3019 	mutex_t **lockptr;
3020 	uint_t nlocks;
3021 	mutex_t *mp;
3022 
3023 	if ((nlocks = self->ul_heldlockcnt) != 0)
3024 		lockptr = self->ul_heldlocks.array;
3025 	else {
3026 		nlocks = 1;
3027 		lockptr = &self->ul_heldlocks.single;
3028 	}
3029 
3030 	for (; nlocks; nlocks--, lockptr++) {
3031 		/*
3032 		 * The kernel takes care of transitioning held
3033 		 * LOCK_PRIO_INHERIT mutexes to LOCK_OWNERDEAD.
3034 		 * We avoid that case here.
3035 		 */
3036 		if ((mp = *lockptr) != NULL &&
3037 		    mutex_held(mp) &&
3038 		    (mp->mutex_type & (LOCK_ROBUST | LOCK_PRIO_INHERIT)) ==
3039 		    LOCK_ROBUST) {
3040 			mp->mutex_rcount = 0;
3041 			if (!(mp->mutex_flag & LOCK_UNMAPPED))
3042 				mp->mutex_flag |= LOCK_OWNERDEAD;
3043 			(void) mutex_unlock_internal(mp, 1);
3044 		}
3045 	}
3046 
3047 	heldlock_free(self);
3048 }
3049 
3050 #pragma weak _cond_init = cond_init
3051 /* ARGSUSED2 */
3052 int
3053 cond_init(cond_t *cvp, int type, void *arg)
3054 {
3055 	if (type != USYNC_THREAD && type != USYNC_PROCESS)
3056 		return (EINVAL);
3057 	(void) memset(cvp, 0, sizeof (*cvp));
3058 	cvp->cond_type = (uint16_t)type;
3059 	cvp->cond_magic = COND_MAGIC;
3060 
3061 	/*
3062 	 * This should be at the beginning of the function,
3063 	 * but for the sake of old broken applications that
3064 	 * do not have proper alignment for their condvars
3065 	 * (and don't check the return code from cond_init),
3066 	 * we put it here, after initializing the condvar regardless.
3067 	 */
3068 	if (((uintptr_t)cvp & (_LONG_LONG_ALIGNMENT - 1)) &&
3069 	    curthread->ul_misaligned == 0)
3070 		return (EINVAL);
3071 
3072 	return (0);
3073 }
3074 
3075 /*
3076  * cond_sleep_queue(): utility function for cond_wait_queue().
3077  *
3078  * Go to sleep on a condvar sleep queue, expect to be waked up
3079  * by someone calling cond_signal() or cond_broadcast() or due
3080  * to receiving a UNIX signal or being cancelled, or just simply
3081  * due to a spurious wakeup (like someome calling forkall()).
3082  *
3083  * The associated mutex is *not* reacquired before returning.
3084  * That must be done by the caller of cond_sleep_queue().
3085  */
3086 static int
3087 cond_sleep_queue(cond_t *cvp, mutex_t *mp, timespec_t *tsp)
3088 {
3089 	ulwp_t *self = curthread;
3090 	queue_head_t *qp;
3091 	queue_head_t *mqp;
3092 	lwpid_t lwpid;
3093 	int signalled;
3094 	int error;
3095 	int cv_wake;
3096 	int release_all;
3097 
3098 	/*
3099 	 * Put ourself on the CV sleep queue, unlock the mutex, then
3100 	 * park ourself and unpark a candidate lwp to grab the mutex.
3101 	 * We must go onto the CV sleep queue before dropping the
3102 	 * mutex in order to guarantee atomicity of the operation.
3103 	 */
3104 	self->ul_sp = stkptr();
3105 	qp = queue_lock(cvp, CV);
3106 	enqueue(qp, self, 0);
3107 	cvp->cond_waiters_user = 1;
3108 	self->ul_cvmutex = mp;
3109 	self->ul_cv_wake = cv_wake = (tsp != NULL);
3110 	self->ul_signalled = 0;
3111 	if (mp->mutex_flag & LOCK_OWNERDEAD) {
3112 		mp->mutex_flag &= ~LOCK_OWNERDEAD;
3113 		mp->mutex_flag |= LOCK_NOTRECOVERABLE;
3114 	}
3115 	release_all = ((mp->mutex_flag & LOCK_NOTRECOVERABLE) != 0);
3116 	lwpid = mutex_unlock_queue(mp, release_all);
3117 	for (;;) {
3118 		set_parking_flag(self, 1);
3119 		queue_unlock(qp);
3120 		if (lwpid != 0) {
3121 			lwpid = preempt_unpark(self, lwpid);
3122 			preempt(self);
3123 		}
3124 		/*
3125 		 * We may have a deferred signal present,
3126 		 * in which case we should return EINTR.
3127 		 * Also, we may have received a SIGCANCEL; if so
3128 		 * and we are cancelable we should return EINTR.
3129 		 * We force an immediate EINTR return from
3130 		 * __lwp_park() by turning our parking flag off.
3131 		 */
3132 		if (self->ul_cursig != 0 ||
3133 		    (self->ul_cancelable && self->ul_cancel_pending))
3134 			set_parking_flag(self, 0);
3135 		/*
3136 		 * __lwp_park() will return the residual time in tsp
3137 		 * if we are unparked before the timeout expires.
3138 		 */
3139 		error = __lwp_park(tsp, lwpid);
3140 		set_parking_flag(self, 0);
3141 		lwpid = 0;	/* unpark the other lwp only once */
3142 		/*
3143 		 * We were waked up by cond_signal(), cond_broadcast(),
3144 		 * by an interrupt or timeout (EINTR or ETIME),
3145 		 * or we may just have gotten a spurious wakeup.
3146 		 */
3147 		qp = queue_lock(cvp, CV);
3148 		if (!cv_wake)
3149 			mqp = queue_lock(mp, MX);
3150 		if (self->ul_sleepq == NULL)
3151 			break;
3152 		/*
3153 		 * We are on either the condvar sleep queue or the
3154 		 * mutex sleep queue.  Break out of the sleep if we
3155 		 * were interrupted or we timed out (EINTR or ETIME).
3156 		 * Else this is a spurious wakeup; continue the loop.
3157 		 */
3158 		if (!cv_wake && self->ul_sleepq == mqp) { /* mutex queue */
3159 			if (error) {
3160 				mp->mutex_waiters = dequeue_self(mqp);
3161 				break;
3162 			}
3163 			tsp = NULL;	/* no more timeout */
3164 		} else if (self->ul_sleepq == qp) {	/* condvar queue */
3165 			if (error) {
3166 				cvp->cond_waiters_user = dequeue_self(qp);
3167 				break;
3168 			}
3169 			/*
3170 			 * Else a spurious wakeup on the condvar queue.
3171 			 * __lwp_park() has already adjusted the timeout.
3172 			 */
3173 		} else {
3174 			thr_panic("cond_sleep_queue(): thread not on queue");
3175 		}
3176 		if (!cv_wake)
3177 			queue_unlock(mqp);
3178 	}
3179 
3180 	self->ul_sp = 0;
3181 	self->ul_cv_wake = 0;
3182 	ASSERT(self->ul_cvmutex == NULL);
3183 	ASSERT(self->ul_sleepq == NULL && self->ul_link == NULL &&
3184 	    self->ul_wchan == NULL);
3185 
3186 	signalled = self->ul_signalled;
3187 	self->ul_signalled = 0;
3188 	queue_unlock(qp);
3189 	if (!cv_wake)
3190 		queue_unlock(mqp);
3191 
3192 	/*
3193 	 * If we were concurrently cond_signal()d and any of:
3194 	 * received a UNIX signal, were cancelled, or got a timeout,
3195 	 * then perform another cond_signal() to avoid consuming it.
3196 	 */
3197 	if (error && signalled)
3198 		(void) cond_signal(cvp);
3199 
3200 	return (error);
3201 }
3202 
3203 static void
3204 cond_wait_check_alignment(cond_t *cvp, mutex_t *mp)
3205 {
3206 	if ((uintptr_t)mp & (_LONG_LONG_ALIGNMENT - 1))
3207 		lock_error(mp, "cond_wait", cvp, "mutex is misaligned");
3208 	if ((uintptr_t)cvp & (_LONG_LONG_ALIGNMENT - 1))
3209 		lock_error(mp, "cond_wait", cvp, "condvar is misaligned");
3210 }
3211 
3212 int
3213 cond_wait_queue(cond_t *cvp, mutex_t *mp, timespec_t *tsp)
3214 {
3215 	ulwp_t *self = curthread;
3216 	int error;
3217 	int merror;
3218 
3219 	if (self->ul_error_detection && self->ul_misaligned == 0)
3220 		cond_wait_check_alignment(cvp, mp);
3221 
3222 	/*
3223 	 * The old thread library was programmed to defer signals
3224 	 * while in cond_wait() so that the associated mutex would
3225 	 * be guaranteed to be held when the application signal
3226 	 * handler was invoked.
3227 	 *
3228 	 * We do not behave this way by default; the state of the
3229 	 * associated mutex in the signal handler is undefined.
3230 	 *
3231 	 * To accommodate applications that depend on the old
3232 	 * behavior, the _THREAD_COND_WAIT_DEFER environment
3233 	 * variable can be set to 1 and we will behave in the
3234 	 * old way with respect to cond_wait().
3235 	 */
3236 	if (self->ul_cond_wait_defer)
3237 		sigoff(self);
3238 
3239 	error = cond_sleep_queue(cvp, mp, tsp);
3240 
3241 	/*
3242 	 * Reacquire the mutex.
3243 	 */
3244 	if ((merror = mutex_lock_impl(mp, NULL)) != 0)
3245 		error = merror;
3246 
3247 	/*
3248 	 * Take any deferred signal now, after we have reacquired the mutex.
3249 	 */
3250 	if (self->ul_cond_wait_defer)
3251 		sigon(self);
3252 
3253 	return (error);
3254 }
3255 
3256 /*
3257  * cond_sleep_kernel(): utility function for cond_wait_kernel().
3258  * See the comment ahead of cond_sleep_queue(), above.
3259  */
3260 static int
3261 cond_sleep_kernel(cond_t *cvp, mutex_t *mp, timespec_t *tsp)
3262 {
3263 	int mtype = mp->mutex_type;
3264 	ulwp_t *self = curthread;
3265 	int error;
3266 
3267 	if ((mtype & LOCK_PRIO_PROTECT) && _ceil_mylist_del(mp))
3268 		_ceil_prio_waive();
3269 
3270 	self->ul_sp = stkptr();
3271 	self->ul_wchan = cvp;
3272 	mp->mutex_owner = 0;
3273 	/* mp->mutex_ownerpid is cleared by ___lwp_cond_wait() */
3274 	if (mtype & LOCK_PRIO_INHERIT) {
3275 		mp->mutex_lockw = LOCKCLEAR;
3276 		self->ul_pilocks--;
3277 	}
3278 	/*
3279 	 * ___lwp_cond_wait() returns immediately with EINTR if
3280 	 * set_parking_flag(self,0) is called on this lwp before it
3281 	 * goes to sleep in the kernel.  sigacthandler() calls this
3282 	 * when a deferred signal is noted.  This assures that we don't
3283 	 * get stuck in ___lwp_cond_wait() with all signals blocked
3284 	 * due to taking a deferred signal before going to sleep.
3285 	 */
3286 	set_parking_flag(self, 1);
3287 	if (self->ul_cursig != 0 ||
3288 	    (self->ul_cancelable && self->ul_cancel_pending))
3289 		set_parking_flag(self, 0);
3290 	error = ___lwp_cond_wait(cvp, mp, tsp, 1);
3291 	set_parking_flag(self, 0);
3292 	self->ul_sp = 0;
3293 	self->ul_wchan = NULL;
3294 	return (error);
3295 }
3296 
3297 int
3298 cond_wait_kernel(cond_t *cvp, mutex_t *mp, timespec_t *tsp)
3299 {
3300 	ulwp_t *self = curthread;
3301 	int error;
3302 	int merror;
3303 
3304 	if (self->ul_error_detection && self->ul_misaligned == 0)
3305 		cond_wait_check_alignment(cvp, mp);
3306 
3307 	/*
3308 	 * See the large comment in cond_wait_queue(), above.
3309 	 */
3310 	if (self->ul_cond_wait_defer)
3311 		sigoff(self);
3312 
3313 	error = cond_sleep_kernel(cvp, mp, tsp);
3314 
3315 	/*
3316 	 * Override the return code from ___lwp_cond_wait()
3317 	 * with any non-zero return code from mutex_lock().
3318 	 * This addresses robust lock failures in particular;
3319 	 * the caller must see the EOWNERDEAD or ENOTRECOVERABLE
3320 	 * errors in order to take corrective action.
3321 	 */
3322 	if ((merror = mutex_lock_impl(mp, NULL)) != 0)
3323 		error = merror;
3324 
3325 	/*
3326 	 * Take any deferred signal now, after we have reacquired the mutex.
3327 	 */
3328 	if (self->ul_cond_wait_defer)
3329 		sigon(self);
3330 
3331 	return (error);
3332 }
3333 
3334 /*
3335  * Common code for cond_wait() and cond_timedwait()
3336  */
3337 int
3338 cond_wait_common(cond_t *cvp, mutex_t *mp, timespec_t *tsp)
3339 {
3340 	int mtype = mp->mutex_type;
3341 	hrtime_t begin_sleep = 0;
3342 	ulwp_t *self = curthread;
3343 	uberdata_t *udp = self->ul_uberdata;
3344 	tdb_cond_stats_t *csp = COND_STATS(cvp, udp);
3345 	tdb_mutex_stats_t *msp = MUTEX_STATS(mp, udp);
3346 	uint8_t rcount;
3347 	int error = 0;
3348 
3349 	/*
3350 	 * The SUSV3 Posix spec for pthread_cond_timedwait() states:
3351 	 *	Except in the case of [ETIMEDOUT], all these error checks
3352 	 *	shall act as if they were performed immediately at the
3353 	 *	beginning of processing for the function and shall cause
3354 	 *	an error return, in effect, prior to modifying the state
3355 	 *	of the mutex specified by mutex or the condition variable
3356 	 *	specified by cond.
3357 	 * Therefore, we must return EINVAL now if the timout is invalid.
3358 	 */
3359 	if (tsp != NULL &&
3360 	    (tsp->tv_sec < 0 || (ulong_t)tsp->tv_nsec >= NANOSEC))
3361 		return (EINVAL);
3362 
3363 	if (__td_event_report(self, TD_SLEEP, udp)) {
3364 		self->ul_sp = stkptr();
3365 		self->ul_wchan = cvp;
3366 		self->ul_td_evbuf.eventnum = TD_SLEEP;
3367 		self->ul_td_evbuf.eventdata = cvp;
3368 		tdb_event(TD_SLEEP, udp);
3369 		self->ul_sp = 0;
3370 	}
3371 	if (csp) {
3372 		if (tsp)
3373 			tdb_incr(csp->cond_timedwait);
3374 		else
3375 			tdb_incr(csp->cond_wait);
3376 	}
3377 	if (msp)
3378 		begin_sleep = record_hold_time(msp);
3379 	else if (csp)
3380 		begin_sleep = gethrtime();
3381 
3382 	if (self->ul_error_detection) {
3383 		if (!mutex_held(mp))
3384 			lock_error(mp, "cond_wait", cvp, NULL);
3385 		if ((mtype & LOCK_RECURSIVE) && mp->mutex_rcount != 0)
3386 			lock_error(mp, "recursive mutex in cond_wait",
3387 			    cvp, NULL);
3388 		if (cvp->cond_type & USYNC_PROCESS) {
3389 			if (!(mtype & USYNC_PROCESS))
3390 				lock_error(mp, "cond_wait", cvp,
3391 				    "condvar process-shared, "
3392 				    "mutex process-private");
3393 		} else {
3394 			if (mtype & USYNC_PROCESS)
3395 				lock_error(mp, "cond_wait", cvp,
3396 				    "condvar process-private, "
3397 				    "mutex process-shared");
3398 		}
3399 	}
3400 
3401 	/*
3402 	 * We deal with recursive mutexes by completely
3403 	 * dropping the lock and restoring the recursion
3404 	 * count after waking up.  This is arguably wrong,
3405 	 * but it obeys the principle of least astonishment.
3406 	 */
3407 	rcount = mp->mutex_rcount;
3408 	mp->mutex_rcount = 0;
3409 	if ((mtype &
3410 	    (USYNC_PROCESS | LOCK_PRIO_INHERIT | LOCK_PRIO_PROTECT)) |
3411 	    (cvp->cond_type & USYNC_PROCESS))
3412 		error = cond_wait_kernel(cvp, mp, tsp);
3413 	else
3414 		error = cond_wait_queue(cvp, mp, tsp);
3415 	mp->mutex_rcount = rcount;
3416 
3417 	if (csp) {
3418 		hrtime_t lapse = gethrtime() - begin_sleep;
3419 		if (tsp == NULL)
3420 			csp->cond_wait_sleep_time += lapse;
3421 		else {
3422 			csp->cond_timedwait_sleep_time += lapse;
3423 			if (error == ETIME)
3424 				tdb_incr(csp->cond_timedwait_timeout);
3425 		}
3426 	}
3427 	return (error);
3428 }
3429 
3430 /*
3431  * cond_wait() is a cancellation point but __cond_wait() is not.
3432  * Internally, libc calls the non-cancellation version.
3433  * Other libraries need to use pthread_setcancelstate(), as appropriate,
3434  * since __cond_wait() is not exported from libc.
3435  */
3436 int
3437 __cond_wait(cond_t *cvp, mutex_t *mp)
3438 {
3439 	ulwp_t *self = curthread;
3440 	uberdata_t *udp = self->ul_uberdata;
3441 	uberflags_t *gflags;
3442 
3443 	/*
3444 	 * Optimize the common case of USYNC_THREAD plus
3445 	 * no error detection, no lock statistics, and no event tracing.
3446 	 */
3447 	if ((gflags = self->ul_schedctl_called) != NULL &&
3448 	    (cvp->cond_type | mp->mutex_type | gflags->uf_trs_ted |
3449 	    self->ul_td_events_enable |
3450 	    udp->tdb.tdb_ev_global_mask.event_bits[0]) == 0)
3451 		return (cond_wait_queue(cvp, mp, NULL));
3452 
3453 	/*
3454 	 * Else do it the long way.
3455 	 */
3456 	return (cond_wait_common(cvp, mp, NULL));
3457 }
3458 
3459 #pragma weak _cond_wait = cond_wait
3460 int
3461 cond_wait(cond_t *cvp, mutex_t *mp)
3462 {
3463 	int error;
3464 
3465 	_cancelon();
3466 	error = __cond_wait(cvp, mp);
3467 	if (error == EINTR)
3468 		_canceloff();
3469 	else
3470 		_canceloff_nocancel();
3471 	return (error);
3472 }
3473 
3474 /*
3475  * pthread_cond_wait() is a cancellation point.
3476  */
3477 int
3478 pthread_cond_wait(pthread_cond_t *_RESTRICT_KYWD cvp,
3479 	pthread_mutex_t *_RESTRICT_KYWD mp)
3480 {
3481 	int error;
3482 
3483 	error = cond_wait((cond_t *)cvp, (mutex_t *)mp);
3484 	return ((error == EINTR)? 0 : error);
3485 }
3486 
3487 /*
3488  * cond_timedwait() is a cancellation point but __cond_timedwait() is not.
3489  */
3490 int
3491 __cond_timedwait(cond_t *cvp, mutex_t *mp, const timespec_t *abstime)
3492 {
3493 	clockid_t clock_id = cvp->cond_clockid;
3494 	timespec_t reltime;
3495 	int error;
3496 
3497 	if (clock_id != CLOCK_REALTIME && clock_id != CLOCK_HIGHRES)
3498 		clock_id = CLOCK_REALTIME;
3499 	abstime_to_reltime(clock_id, abstime, &reltime);
3500 	error = cond_wait_common(cvp, mp, &reltime);
3501 	if (error == ETIME && clock_id == CLOCK_HIGHRES) {
3502 		/*
3503 		 * Don't return ETIME if we didn't really get a timeout.
3504 		 * This can happen if we return because someone resets
3505 		 * the system clock.  Just return zero in this case,
3506 		 * giving a spurious wakeup but not a timeout.
3507 		 */
3508 		if ((hrtime_t)(uint32_t)abstime->tv_sec * NANOSEC +
3509 		    abstime->tv_nsec > gethrtime())
3510 			error = 0;
3511 	}
3512 	return (error);
3513 }
3514 
3515 int
3516 cond_timedwait(cond_t *cvp, mutex_t *mp, const timespec_t *abstime)
3517 {
3518 	int error;
3519 
3520 	_cancelon();
3521 	error = __cond_timedwait(cvp, mp, abstime);
3522 	if (error == EINTR)
3523 		_canceloff();
3524 	else
3525 		_canceloff_nocancel();
3526 	return (error);
3527 }
3528 
3529 /*
3530  * pthread_cond_timedwait() is a cancellation point.
3531  */
3532 int
3533 pthread_cond_timedwait(pthread_cond_t *_RESTRICT_KYWD cvp,
3534 	pthread_mutex_t *_RESTRICT_KYWD mp,
3535 	const struct timespec *_RESTRICT_KYWD abstime)
3536 {
3537 	int error;
3538 
3539 	error = cond_timedwait((cond_t *)cvp, (mutex_t *)mp, abstime);
3540 	if (error == ETIME)
3541 		error = ETIMEDOUT;
3542 	else if (error == EINTR)
3543 		error = 0;
3544 	return (error);
3545 }
3546 
3547 /*
3548  * cond_reltimedwait() is a cancellation point but __cond_reltimedwait() is not.
3549  */
3550 int
3551 __cond_reltimedwait(cond_t *cvp, mutex_t *mp, const timespec_t *reltime)
3552 {
3553 	timespec_t tslocal = *reltime;
3554 
3555 	return (cond_wait_common(cvp, mp, &tslocal));
3556 }
3557 
3558 int
3559 cond_reltimedwait(cond_t *cvp, mutex_t *mp, const timespec_t *reltime)
3560 {
3561 	int error;
3562 
3563 	_cancelon();
3564 	error = __cond_reltimedwait(cvp, mp, reltime);
3565 	if (error == EINTR)
3566 		_canceloff();
3567 	else
3568 		_canceloff_nocancel();
3569 	return (error);
3570 }
3571 
3572 int
3573 pthread_cond_reltimedwait_np(pthread_cond_t *_RESTRICT_KYWD cvp,
3574 	pthread_mutex_t *_RESTRICT_KYWD mp,
3575 	const struct timespec *_RESTRICT_KYWD reltime)
3576 {
3577 	int error;
3578 
3579 	error = cond_reltimedwait((cond_t *)cvp, (mutex_t *)mp, reltime);
3580 	if (error == ETIME)
3581 		error = ETIMEDOUT;
3582 	else if (error == EINTR)
3583 		error = 0;
3584 	return (error);
3585 }
3586 
3587 #pragma weak pthread_cond_signal = cond_signal
3588 #pragma weak _cond_signal = cond_signal
3589 int
3590 cond_signal(cond_t *cvp)
3591 {
3592 	ulwp_t *self = curthread;
3593 	uberdata_t *udp = self->ul_uberdata;
3594 	tdb_cond_stats_t *csp = COND_STATS(cvp, udp);
3595 	int error = 0;
3596 	int more;
3597 	lwpid_t lwpid;
3598 	queue_head_t *qp;
3599 	mutex_t *mp;
3600 	queue_head_t *mqp;
3601 	ulwp_t **ulwpp;
3602 	ulwp_t *ulwp;
3603 	ulwp_t *prev;
3604 
3605 	if (csp)
3606 		tdb_incr(csp->cond_signal);
3607 
3608 	if (cvp->cond_waiters_kernel)	/* someone sleeping in the kernel? */
3609 		error = _lwp_cond_signal(cvp);
3610 
3611 	if (!cvp->cond_waiters_user)	/* no one sleeping at user-level */
3612 		return (error);
3613 
3614 	/*
3615 	 * Move someone from the condvar sleep queue to the mutex sleep
3616 	 * queue for the mutex that he will acquire on being waked up.
3617 	 * We can do this only if we own the mutex he will acquire.
3618 	 * If we do not own the mutex, or if his ul_cv_wake flag
3619 	 * is set, just dequeue and unpark him.
3620 	 */
3621 	qp = queue_lock(cvp, CV);
3622 	ulwpp = queue_slot(qp, &prev, &more);
3623 	cvp->cond_waiters_user = more;
3624 	if (ulwpp == NULL) {	/* no one on the sleep queue */
3625 		queue_unlock(qp);
3626 		return (error);
3627 	}
3628 	ulwp = *ulwpp;
3629 
3630 	/*
3631 	 * Inform the thread that he was the recipient of a cond_signal().
3632 	 * This lets him deal with cond_signal() and, concurrently,
3633 	 * one or more of a cancellation, a UNIX signal, or a timeout.
3634 	 * These latter conditions must not consume a cond_signal().
3635 	 */
3636 	ulwp->ul_signalled = 1;
3637 
3638 	/*
3639 	 * Dequeue the waiter but leave his ul_sleepq non-NULL
3640 	 * while we move him to the mutex queue so that he can
3641 	 * deal properly with spurious wakeups.
3642 	 */
3643 	queue_unlink(qp, ulwpp, prev);
3644 
3645 	mp = ulwp->ul_cvmutex;		/* the mutex he will acquire */
3646 	ulwp->ul_cvmutex = NULL;
3647 	ASSERT(mp != NULL);
3648 
3649 	if (ulwp->ul_cv_wake || !MUTEX_OWNED(mp, self)) {
3650 		/* just wake him up */
3651 		lwpid = ulwp->ul_lwpid;
3652 		no_preempt(self);
3653 		ulwp->ul_sleepq = NULL;
3654 		ulwp->ul_wchan = NULL;
3655 		queue_unlock(qp);
3656 		(void) __lwp_unpark(lwpid);
3657 		preempt(self);
3658 	} else {
3659 		/* move him to the mutex queue */
3660 		mqp = queue_lock(mp, MX);
3661 		enqueue(mqp, ulwp, 0);
3662 		mp->mutex_waiters = 1;
3663 		queue_unlock(mqp);
3664 		queue_unlock(qp);
3665 	}
3666 
3667 	return (error);
3668 }
3669 
3670 /*
3671  * Utility function called by mutex_wakeup_all(), cond_broadcast(),
3672  * and rw_queue_release() to (re)allocate a big buffer to hold the
3673  * lwpids of all the threads to be set running after they are removed
3674  * from their sleep queues.  Since we are holding a queue lock, we
3675  * cannot call any function that might acquire a lock.  mmap(), munmap(),
3676  * lwp_unpark_all() are simple system calls and are safe in this regard.
3677  */
3678 lwpid_t *
3679 alloc_lwpids(lwpid_t *lwpid, int *nlwpid_ptr, int *maxlwps_ptr)
3680 {
3681 	/*
3682 	 * Allocate NEWLWPS ids on the first overflow.
3683 	 * Double the allocation each time after that.
3684 	 */
3685 	int nlwpid = *nlwpid_ptr;
3686 	int maxlwps = *maxlwps_ptr;
3687 	int first_allocation;
3688 	int newlwps;
3689 	void *vaddr;
3690 
3691 	ASSERT(nlwpid == maxlwps);
3692 
3693 	first_allocation = (maxlwps == MAXLWPS);
3694 	newlwps = first_allocation? NEWLWPS : 2 * maxlwps;
3695 	vaddr = mmap(NULL, newlwps * sizeof (lwpid_t),
3696 	    PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, (off_t)0);
3697 
3698 	if (vaddr == MAP_FAILED) {
3699 		/*
3700 		 * Let's hope this never happens.
3701 		 * If it does, then we have a terrible
3702 		 * thundering herd on our hands.
3703 		 */
3704 		(void) __lwp_unpark_all(lwpid, nlwpid);
3705 		*nlwpid_ptr = 0;
3706 	} else {
3707 		(void) memcpy(vaddr, lwpid, maxlwps * sizeof (lwpid_t));
3708 		if (!first_allocation)
3709 			(void) munmap((caddr_t)lwpid,
3710 			    maxlwps * sizeof (lwpid_t));
3711 		lwpid = vaddr;
3712 		*maxlwps_ptr = newlwps;
3713 	}
3714 
3715 	return (lwpid);
3716 }
3717 
3718 #pragma weak pthread_cond_broadcast = cond_broadcast
3719 #pragma weak _cond_broadcast = cond_broadcast
3720 int
3721 cond_broadcast(cond_t *cvp)
3722 {
3723 	ulwp_t *self = curthread;
3724 	uberdata_t *udp = self->ul_uberdata;
3725 	tdb_cond_stats_t *csp = COND_STATS(cvp, udp);
3726 	int error = 0;
3727 	queue_head_t *qp;
3728 	queue_root_t *qrp;
3729 	mutex_t *mp;
3730 	mutex_t *mp_cache = NULL;
3731 	queue_head_t *mqp = NULL;
3732 	ulwp_t *ulwp;
3733 	int nlwpid = 0;
3734 	int maxlwps = MAXLWPS;
3735 	lwpid_t buffer[MAXLWPS];
3736 	lwpid_t *lwpid = buffer;
3737 
3738 	if (csp)
3739 		tdb_incr(csp->cond_broadcast);
3740 
3741 	if (cvp->cond_waiters_kernel)	/* someone sleeping in the kernel? */
3742 		error = _lwp_cond_broadcast(cvp);
3743 
3744 	if (!cvp->cond_waiters_user)	/* no one sleeping at user-level */
3745 		return (error);
3746 
3747 	/*
3748 	 * Move everyone from the condvar sleep queue to the mutex sleep
3749 	 * queue for the mutex that they will acquire on being waked up.
3750 	 * We can do this only if we own the mutex they will acquire.
3751 	 * If we do not own the mutex, or if their ul_cv_wake flag
3752 	 * is set, just dequeue and unpark them.
3753 	 *
3754 	 * We keep track of lwpids that are to be unparked in lwpid[].
3755 	 * __lwp_unpark_all() is called to unpark all of them after
3756 	 * they have been removed from the sleep queue and the sleep
3757 	 * queue lock has been dropped.  If we run out of space in our
3758 	 * on-stack buffer, we need to allocate more but we can't call
3759 	 * lmalloc() because we are holding a queue lock when the overflow
3760 	 * occurs and lmalloc() acquires a lock.  We can't use alloca()
3761 	 * either because the application may have allocated a small
3762 	 * stack and we don't want to overrun the stack.  So we call
3763 	 * alloc_lwpids() to allocate a bigger buffer using the mmap()
3764 	 * system call directly since that path acquires no locks.
3765 	 */
3766 	qp = queue_lock(cvp, CV);
3767 	cvp->cond_waiters_user = 0;
3768 	for (;;) {
3769 		if ((qrp = qp->qh_root) == NULL ||
3770 		    (ulwp = qrp->qr_head) == NULL)
3771 			break;
3772 		ASSERT(ulwp->ul_wchan == cvp);
3773 		queue_unlink(qp, &qrp->qr_head, NULL);
3774 		mp = ulwp->ul_cvmutex;		/* his mutex */
3775 		ulwp->ul_cvmutex = NULL;
3776 		ASSERT(mp != NULL);
3777 		if (ulwp->ul_cv_wake || !MUTEX_OWNED(mp, self)) {
3778 			/* just wake him up */
3779 			ulwp->ul_sleepq = NULL;
3780 			ulwp->ul_wchan = NULL;
3781 			if (nlwpid == maxlwps)
3782 				lwpid = alloc_lwpids(lwpid, &nlwpid, &maxlwps);
3783 			lwpid[nlwpid++] = ulwp->ul_lwpid;
3784 		} else {
3785 			/* move him to the mutex queue */
3786 			if (mp != mp_cache) {
3787 				mp_cache = mp;
3788 				if (mqp != NULL)
3789 					queue_unlock(mqp);
3790 				mqp = queue_lock(mp, MX);
3791 			}
3792 			enqueue(mqp, ulwp, 0);
3793 			mp->mutex_waiters = 1;
3794 		}
3795 	}
3796 	if (mqp != NULL)
3797 		queue_unlock(mqp);
3798 	if (nlwpid == 0) {
3799 		queue_unlock(qp);
3800 	} else {
3801 		no_preempt(self);
3802 		queue_unlock(qp);
3803 		if (nlwpid == 1)
3804 			(void) __lwp_unpark(lwpid[0]);
3805 		else
3806 			(void) __lwp_unpark_all(lwpid, nlwpid);
3807 		preempt(self);
3808 	}
3809 	if (lwpid != buffer)
3810 		(void) munmap((caddr_t)lwpid, maxlwps * sizeof (lwpid_t));
3811 	return (error);
3812 }
3813 
3814 #pragma weak pthread_cond_destroy = cond_destroy
3815 int
3816 cond_destroy(cond_t *cvp)
3817 {
3818 	cvp->cond_magic = 0;
3819 	tdb_sync_obj_deregister(cvp);
3820 	return (0);
3821 }
3822 
3823 #if defined(THREAD_DEBUG)
3824 void
3825 assert_no_libc_locks_held(void)
3826 {
3827 	ASSERT(!curthread->ul_critical || curthread->ul_bindflags);
3828 }
3829 
3830 /* protected by link_lock */
3831 uint64_t spin_lock_spin;
3832 uint64_t spin_lock_spin2;
3833 uint64_t spin_lock_sleep;
3834 uint64_t spin_lock_wakeup;
3835 
3836 /*
3837  * Record spin lock statistics.
3838  * Called by a thread exiting itself in thrp_exit().
3839  * Also called via atexit() from the thread calling
3840  * exit() to do all the other threads as well.
3841  */
3842 void
3843 record_spin_locks(ulwp_t *ulwp)
3844 {
3845 	spin_lock_spin += ulwp->ul_spin_lock_spin;
3846 	spin_lock_spin2 += ulwp->ul_spin_lock_spin2;
3847 	spin_lock_sleep += ulwp->ul_spin_lock_sleep;
3848 	spin_lock_wakeup += ulwp->ul_spin_lock_wakeup;
3849 	ulwp->ul_spin_lock_spin = 0;
3850 	ulwp->ul_spin_lock_spin2 = 0;
3851 	ulwp->ul_spin_lock_sleep = 0;
3852 	ulwp->ul_spin_lock_wakeup = 0;
3853 }
3854 
3855 /*
3856  * atexit function:  dump the queue statistics to stderr.
3857  */
3858 #include <stdio.h>
3859 void
3860 dump_queue_statistics(void)
3861 {
3862 	uberdata_t *udp = curthread->ul_uberdata;
3863 	queue_head_t *qp;
3864 	int qn;
3865 	uint64_t spin_lock_total = 0;
3866 
3867 	if (udp->queue_head == NULL || thread_queue_dump == 0)
3868 		return;
3869 
3870 	if (fprintf(stderr, "\n%5d mutex queues:\n", QHASHSIZE) < 0 ||
3871 	    fprintf(stderr, "queue#   lockcount    max qlen    max hlen\n") < 0)
3872 		return;
3873 	for (qn = 0, qp = udp->queue_head; qn < QHASHSIZE; qn++, qp++) {
3874 		if (qp->qh_lockcount == 0)
3875 			continue;
3876 		spin_lock_total += qp->qh_lockcount;
3877 		if (fprintf(stderr, "%5d %12llu%12u%12u\n", qn,
3878 		    (u_longlong_t)qp->qh_lockcount,
3879 		    qp->qh_qmax, qp->qh_hmax) < 0)
3880 			return;
3881 	}
3882 
3883 	if (fprintf(stderr, "\n%5d condvar queues:\n", QHASHSIZE) < 0 ||
3884 	    fprintf(stderr, "queue#   lockcount    max qlen    max hlen\n") < 0)
3885 		return;
3886 	for (qn = 0; qn < QHASHSIZE; qn++, qp++) {
3887 		if (qp->qh_lockcount == 0)
3888 			continue;
3889 		spin_lock_total += qp->qh_lockcount;
3890 		if (fprintf(stderr, "%5d %12llu%12u%12u\n", qn,
3891 		    (u_longlong_t)qp->qh_lockcount,
3892 		    qp->qh_qmax, qp->qh_hmax) < 0)
3893 			return;
3894 	}
3895 
3896 	(void) fprintf(stderr, "\n  spin_lock_total  = %10llu\n",
3897 	    (u_longlong_t)spin_lock_total);
3898 	(void) fprintf(stderr, "  spin_lock_spin   = %10llu\n",
3899 	    (u_longlong_t)spin_lock_spin);
3900 	(void) fprintf(stderr, "  spin_lock_spin2  = %10llu\n",
3901 	    (u_longlong_t)spin_lock_spin2);
3902 	(void) fprintf(stderr, "  spin_lock_sleep  = %10llu\n",
3903 	    (u_longlong_t)spin_lock_sleep);
3904 	(void) fprintf(stderr, "  spin_lock_wakeup = %10llu\n",
3905 	    (u_longlong_t)spin_lock_wakeup);
3906 }
3907 #endif
3908