Home | History | Annotate | Line # | Download | only in libpthread
pthread_mutex.c revision 1.64
      1 /*	$NetBSD: pthread_mutex.c,v 1.64 2017/12/08 09:24:31 kre Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001, 2003, 2006, 2007, 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Nathan J. Williams, by Jason R. Thorpe, and by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * To track threads waiting for mutexes to be released, we use lockless
     34  * lists built on atomic operations and memory barriers.
     35  *
     36  * A simple spinlock would be faster and make the code easier to
     37  * follow, but spinlocks are problematic in userspace.  If a thread is
     38  * preempted by the kernel while holding a spinlock, any other thread
     39  * attempting to acquire that spinlock will needlessly busy wait.
     40  *
     41  * There is no good way to know that the holding thread is no longer
     42  * running, nor to request a wake-up once it has begun running again.
     43  * Of more concern, threads in the SCHED_FIFO class do not have a
     44  * limited time quantum and so could spin forever, preventing the
     45  * thread holding the spinlock from getting CPU time: it would never
     46  * be released.
     47  */
     48 
     49 #include <sys/cdefs.h>
     50 __RCSID("$NetBSD: pthread_mutex.c,v 1.64 2017/12/08 09:24:31 kre Exp $");
     51 
     52 #include <sys/types.h>
     53 #include <sys/lwpctl.h>
     54 #include <sys/sched.h>
     55 #include <sys/lock.h>
     56 
     57 #include <errno.h>
     58 #include <limits.h>
     59 #include <stdlib.h>
     60 #include <time.h>
     61 #include <string.h>
     62 #include <stdio.h>
     63 
     64 #include "pthread.h"
     65 #include "pthread_int.h"
     66 #include "reentrant.h"
     67 
     68 #define	MUTEX_WAITERS_BIT		((uintptr_t)0x01)
     69 #define	MUTEX_RECURSIVE_BIT		((uintptr_t)0x02)
     70 #define	MUTEX_DEFERRED_BIT		((uintptr_t)0x04)
     71 #define	MUTEX_PROTECT_BIT		((uintptr_t)0x08)
     72 #define	MUTEX_THREAD			((uintptr_t)~0x0f)
     73 
     74 #define	MUTEX_HAS_WAITERS(x)		((uintptr_t)(x) & MUTEX_WAITERS_BIT)
     75 #define	MUTEX_RECURSIVE(x)		((uintptr_t)(x) & MUTEX_RECURSIVE_BIT)
     76 #define	MUTEX_PROTECT(x)		((uintptr_t)(x) & MUTEX_PROTECT_BIT)
     77 #define	MUTEX_OWNER(x)			((uintptr_t)(x) & MUTEX_THREAD)
     78 
     79 #define	MUTEX_GET_TYPE(x)		\
     80     ((int)(((uintptr_t)(x) & 0x000000ff) >> 0))
     81 #define	MUTEX_SET_TYPE(x, t) 		\
     82     (x) = (void *)(((uintptr_t)(x) & ~0x000000ff) | ((t) << 0))
     83 #define	MUTEX_GET_PROTOCOL(x)		\
     84     ((int)(((uintptr_t)(x) & 0x0000ff00) >> 8))
     85 #define	MUTEX_SET_PROTOCOL(x, p)	\
     86     (x) = (void *)(((uintptr_t)(x) & ~0x0000ff00) | ((p) << 8))
     87 #define	MUTEX_GET_CEILING(x)		\
     88     ((int)(((uintptr_t)(x) & 0x00ff0000) >> 16))
     89 #define	MUTEX_SET_CEILING(x, c)	\
     90     (x) = (void *)(((uintptr_t)(x) & ~0x00ff0000) | ((c) << 16))
     91 
     92 #if __GNUC_PREREQ__(3, 0)
     93 #define	NOINLINE		__attribute ((noinline))
     94 #else
     95 #define	NOINLINE		/* nothing */
     96 #endif
     97 
     98 static void	pthread__mutex_wakeup(pthread_t, pthread_mutex_t *);
     99 static int	pthread__mutex_lock_slow(pthread_mutex_t *,
    100     const struct timespec *);
    101 static int	pthread__mutex_unlock_slow(pthread_mutex_t *);
    102 static void	pthread__mutex_pause(void);
    103 
    104 int		_pthread_mutex_held_np(pthread_mutex_t *);
    105 pthread_t	_pthread_mutex_owner_np(pthread_mutex_t *);
    106 
    107 __weak_alias(pthread_mutex_held_np,_pthread_mutex_held_np)
    108 __weak_alias(pthread_mutex_owner_np,_pthread_mutex_owner_np)
    109 
    110 __strong_alias(__libc_mutex_init,pthread_mutex_init)
    111 __strong_alias(__libc_mutex_lock,pthread_mutex_lock)
    112 __strong_alias(__libc_mutex_trylock,pthread_mutex_trylock)
    113 __strong_alias(__libc_mutex_unlock,pthread_mutex_unlock)
    114 __strong_alias(__libc_mutex_destroy,pthread_mutex_destroy)
    115 
    116 __strong_alias(__libc_mutexattr_init,pthread_mutexattr_init)
    117 __strong_alias(__libc_mutexattr_destroy,pthread_mutexattr_destroy)
    118 __strong_alias(__libc_mutexattr_settype,pthread_mutexattr_settype)
    119 
    120 int
    121 pthread_mutex_init(pthread_mutex_t *ptm, const pthread_mutexattr_t *attr)
    122 {
    123 	uintptr_t type, proto, val, ceil;
    124 
    125 	if (__predict_false(__uselibcstub))
    126 		return __libc_mutex_init_stub(ptm, attr);
    127 
    128 	if (attr == NULL) {
    129 		type = PTHREAD_MUTEX_NORMAL;
    130 		proto = PTHREAD_PRIO_NONE;
    131 		ceil = 0;
    132 	} else {
    133 		val = (uintptr_t)attr->ptma_private;
    134 
    135 		type = MUTEX_GET_TYPE(val);
    136 		proto = MUTEX_GET_PROTOCOL(val);
    137 		ceil = MUTEX_GET_CEILING(val);
    138 	}
    139 	switch (type) {
    140 	case PTHREAD_MUTEX_ERRORCHECK:
    141 		__cpu_simple_lock_set(&ptm->ptm_errorcheck);
    142 		ptm->ptm_owner = NULL;
    143 		break;
    144 	case PTHREAD_MUTEX_RECURSIVE:
    145 		__cpu_simple_lock_clear(&ptm->ptm_errorcheck);
    146 		ptm->ptm_owner = (void *)MUTEX_RECURSIVE_BIT;
    147 		break;
    148 	default:
    149 		__cpu_simple_lock_clear(&ptm->ptm_errorcheck);
    150 		ptm->ptm_owner = NULL;
    151 		break;
    152 	}
    153 	switch (proto) {
    154 	case PTHREAD_PRIO_PROTECT:
    155 		val = (uintptr_t)ptm->ptm_owner;
    156 		val |= MUTEX_PROTECT_BIT;
    157 		ptm->ptm_owner = (void *)val;
    158 		break;
    159 
    160 	}
    161 	ptm->ptm_magic = _PT_MUTEX_MAGIC;
    162 	ptm->ptm_waiters = NULL;
    163 	ptm->ptm_recursed = 0;
    164 	ptm->ptm_ceiling = (unsigned char)ceil;
    165 
    166 	return 0;
    167 }
    168 
    169 int
    170 pthread_mutex_destroy(pthread_mutex_t *ptm)
    171 {
    172 
    173 	if (__predict_false(__uselibcstub))
    174 		return __libc_mutex_destroy_stub(ptm);
    175 
    176 	pthread__error(EINVAL, "Invalid mutex",
    177 	    ptm->ptm_magic == _PT_MUTEX_MAGIC);
    178 	pthread__error(EBUSY, "Destroying locked mutex",
    179 	    MUTEX_OWNER(ptm->ptm_owner) == 0);
    180 
    181 	ptm->ptm_magic = _PT_MUTEX_DEAD;
    182 	return 0;
    183 }
    184 
    185 int
    186 pthread_mutex_lock(pthread_mutex_t *ptm)
    187 {
    188 	pthread_t self;
    189 	void *val;
    190 
    191 	if (__predict_false(__uselibcstub))
    192 		return __libc_mutex_lock_stub(ptm);
    193 
    194 	self = pthread__self();
    195 	val = atomic_cas_ptr(&ptm->ptm_owner, NULL, self);
    196 	if (__predict_true(val == NULL)) {
    197 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    198 		membar_enter();
    199 #endif
    200 		return 0;
    201 	}
    202 	return pthread__mutex_lock_slow(ptm, NULL);
    203 }
    204 
    205 int
    206 pthread_mutex_timedlock(pthread_mutex_t* ptm, const struct timespec *ts)
    207 {
    208 	pthread_t self;
    209 	void *val;
    210 
    211 	self = pthread__self();
    212 	val = atomic_cas_ptr(&ptm->ptm_owner, NULL, self);
    213 	if (__predict_true(val == NULL)) {
    214 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    215 		membar_enter();
    216 #endif
    217 		return 0;
    218 	}
    219 	return pthread__mutex_lock_slow(ptm, ts);
    220 }
    221 
    222 /* We want function call overhead. */
    223 NOINLINE static void
    224 pthread__mutex_pause(void)
    225 {
    226 
    227 	pthread__smt_pause();
    228 }
    229 
    230 /*
    231  * Spin while the holder is running.  'lwpctl' gives us the true
    232  * status of the thread.  pt_blocking is set by libpthread in order
    233  * to cut out system call and kernel spinlock overhead on remote CPUs
    234  * (could represent many thousands of clock cycles).  pt_blocking also
    235  * makes this thread yield if the target is calling sched_yield().
    236  */
    237 NOINLINE static void *
    238 pthread__mutex_spin(pthread_mutex_t *ptm, pthread_t owner)
    239 {
    240 	pthread_t thread;
    241 	unsigned int count, i;
    242 
    243 	for (count = 2;; owner = ptm->ptm_owner) {
    244 		thread = (pthread_t)MUTEX_OWNER(owner);
    245 		if (thread == NULL)
    246 			break;
    247 		if (thread->pt_lwpctl->lc_curcpu == LWPCTL_CPU_NONE ||
    248 		    thread->pt_blocking)
    249 			break;
    250 		if (count < 128)
    251 			count += count;
    252 		for (i = count; i != 0; i--)
    253 			pthread__mutex_pause();
    254 	}
    255 
    256 	return owner;
    257 }
    258 
    259 NOINLINE static void
    260 pthread__mutex_setwaiters(pthread_t self, pthread_mutex_t *ptm)
    261 {
    262 	void *new, *owner;
    263 
    264 	/*
    265 	 * Note that the mutex can become unlocked before we set
    266 	 * the waiters bit.  If that happens it's not safe to sleep
    267 	 * as we may never be awoken: we must remove the current
    268 	 * thread from the waiters list and try again.
    269 	 *
    270 	 * Because we are doing this atomically, we can't remove
    271 	 * one waiter: we must remove all waiters and awken them,
    272 	 * then sleep in _lwp_park() until we have been awoken.
    273 	 *
    274 	 * Issue a memory barrier to ensure that we are reading
    275 	 * the value of ptm_owner/pt_mutexwait after we have entered
    276 	 * the waiters list (the CAS itself must be atomic).
    277 	 */
    278 again:
    279 	membar_consumer();
    280 	owner = ptm->ptm_owner;
    281 
    282 	if (MUTEX_OWNER(owner) == 0) {
    283 		pthread__mutex_wakeup(self, ptm);
    284 		return;
    285 	}
    286 	if (!MUTEX_HAS_WAITERS(owner)) {
    287 		new = (void *)((uintptr_t)owner | MUTEX_WAITERS_BIT);
    288 		if (atomic_cas_ptr(&ptm->ptm_owner, owner, new) != owner) {
    289 			goto again;
    290 		}
    291 	}
    292 
    293 	/*
    294 	 * Note that pthread_mutex_unlock() can do a non-interlocked CAS.
    295 	 * We cannot know if the presence of the waiters bit is stable
    296 	 * while the holding thread is running.  There are many assumptions;
    297 	 * see sys/kern/kern_mutex.c for details.  In short, we must spin if
    298 	 * we see that the holder is running again.
    299 	 */
    300 	membar_sync();
    301 	if (MUTEX_OWNER(owner) != (uintptr_t)self)
    302 		pthread__mutex_spin(ptm, owner);
    303 
    304 	if (membar_consumer(), !MUTEX_HAS_WAITERS(ptm->ptm_owner)) {
    305 		goto again;
    306 	}
    307 }
    308 
    309 NOINLINE static int
    310 pthread__mutex_lock_slow(pthread_mutex_t *ptm, const struct timespec *ts)
    311 {
    312 	void *waiters, *new, *owner, *next;
    313 	pthread_t self;
    314 	int serrno;
    315 	int error;
    316 
    317 	pthread__error(EINVAL, "Invalid mutex",
    318 	    ptm->ptm_magic == _PT_MUTEX_MAGIC);
    319 
    320 	owner = ptm->ptm_owner;
    321 	self = pthread__self();
    322 
    323 	/* Recursive or errorcheck? */
    324 	if (MUTEX_OWNER(owner) == (uintptr_t)self) {
    325 		if (MUTEX_RECURSIVE(owner)) {
    326 			if (ptm->ptm_recursed == INT_MAX)
    327 				return EAGAIN;
    328 			ptm->ptm_recursed++;
    329 			return 0;
    330 		}
    331 		if (__SIMPLELOCK_LOCKED_P(&ptm->ptm_errorcheck))
    332 			return EDEADLK;
    333 	}
    334 
    335 	/* priority protect */
    336 	if (MUTEX_PROTECT(owner) && _sched_protect(ptm->ptm_ceiling) == -1) {
    337 		return errno;
    338 	}
    339 	serrno = errno;
    340 	for (;; owner = ptm->ptm_owner) {
    341 		/* Spin while the owner is running. */
    342 		if (MUTEX_OWNER(owner) != (uintptr_t)self)
    343 			owner = pthread__mutex_spin(ptm, owner);
    344 
    345 		/* If it has become free, try to acquire it again. */
    346 		if (MUTEX_OWNER(owner) == 0) {
    347 			do {
    348 				new = (void *)
    349 				    ((uintptr_t)self | (uintptr_t)owner);
    350 				next = atomic_cas_ptr(&ptm->ptm_owner, owner,
    351 				    new);
    352 				if (next == owner) {
    353 					errno = serrno;
    354 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    355 					membar_enter();
    356 #endif
    357 					return 0;
    358 				}
    359 				owner = next;
    360 			} while (MUTEX_OWNER(owner) == 0);
    361 			/*
    362 			 * We have lost the race to acquire the mutex.
    363 			 * The new owner could be running on another
    364 			 * CPU, in which case we should spin and avoid
    365 			 * the overhead of blocking.
    366 			 */
    367 			continue;
    368 		}
    369 
    370 		/*
    371 		 * Nope, still held.  Add thread to the list of waiters.
    372 		 * Issue a memory barrier to ensure mutexwait/mutexnext
    373 		 * are visible before we enter the waiters list.
    374 		 */
    375 		self->pt_mutexwait = 1;
    376 		for (waiters = ptm->ptm_waiters;; waiters = next) {
    377 			self->pt_mutexnext = waiters;
    378 			membar_producer();
    379 			next = atomic_cas_ptr(&ptm->ptm_waiters, waiters, self);
    380 			if (next == waiters)
    381 			    	break;
    382 		}
    383 
    384 		/* Set the waiters bit and block. */
    385 		pthread__mutex_setwaiters(self, ptm);
    386 
    387 		/*
    388 		 * We may have been awoken by the current thread above,
    389 		 * or will be awoken by the current holder of the mutex.
    390 		 * The key requirement is that we must not proceed until
    391 		 * told that we are no longer waiting (via pt_mutexwait
    392 		 * being set to zero).  Otherwise it is unsafe to re-enter
    393 		 * the thread onto the waiters list.
    394 		 */
    395 		while (self->pt_mutexwait) {
    396 			self->pt_blocking++;
    397 			error = _lwp_park(CLOCK_REALTIME, TIMER_ABSTIME,
    398 			    __UNCONST(ts), self->pt_unpark,
    399 			    __UNVOLATILE(&ptm->ptm_waiters),
    400 			    __UNVOLATILE(&ptm->ptm_waiters));
    401 			self->pt_unpark = 0;
    402 			self->pt_blocking--;
    403 			membar_sync();
    404 			if (__predict_true(error != -1)) {
    405 				continue;
    406 			}
    407 			if (errno == ETIMEDOUT && self->pt_mutexwait) {
    408 				/*Remove self from waiters list*/
    409 				pthread__mutex_wakeup(self, ptm);
    410 				/*priority protect*/
    411 				if (MUTEX_PROTECT(owner))
    412 					(void)_sched_protect(-1);
    413 				return ETIMEDOUT;
    414 			}
    415 		}
    416 	}
    417 }
    418 
    419 int
    420 pthread_mutex_trylock(pthread_mutex_t *ptm)
    421 {
    422 	pthread_t self;
    423 	void *val, *new, *next;
    424 
    425 	if (__predict_false(__uselibcstub))
    426 		return __libc_mutex_trylock_stub(ptm);
    427 
    428 	self = pthread__self();
    429 	val = atomic_cas_ptr(&ptm->ptm_owner, NULL, self);
    430 	if (__predict_true(val == NULL)) {
    431 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    432 		membar_enter();
    433 #endif
    434 		return 0;
    435 	}
    436 
    437 	if (MUTEX_RECURSIVE(val)) {
    438 		if (MUTEX_OWNER(val) == 0) {
    439 			new = (void *)((uintptr_t)self | (uintptr_t)val);
    440 			next = atomic_cas_ptr(&ptm->ptm_owner, val, new);
    441 			if (__predict_true(next == val)) {
    442 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    443 				membar_enter();
    444 #endif
    445 				return 0;
    446 			}
    447 		}
    448 		if (MUTEX_OWNER(val) == (uintptr_t)self) {
    449 			if (ptm->ptm_recursed == INT_MAX)
    450 				return EAGAIN;
    451 			ptm->ptm_recursed++;
    452 			return 0;
    453 		}
    454 	}
    455 
    456 	return EBUSY;
    457 }
    458 
    459 int
    460 pthread_mutex_unlock(pthread_mutex_t *ptm)
    461 {
    462 	pthread_t self;
    463 	void *value;
    464 
    465 	if (__predict_false(__uselibcstub))
    466 		return __libc_mutex_unlock_stub(ptm);
    467 
    468 	/*
    469 	 * Note this may be a non-interlocked CAS.  See lock_slow()
    470 	 * above and sys/kern/kern_mutex.c for details.
    471 	 */
    472 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    473 	membar_exit();
    474 #endif
    475 	self = pthread__self();
    476 	value = atomic_cas_ptr_ni(&ptm->ptm_owner, self, NULL);
    477 	if (__predict_true(value == self)) {
    478 		pthread__smt_wake();
    479 		return 0;
    480 	}
    481 	return pthread__mutex_unlock_slow(ptm);
    482 }
    483 
    484 NOINLINE static int
    485 pthread__mutex_unlock_slow(pthread_mutex_t *ptm)
    486 {
    487 	pthread_t self, owner, new;
    488 	int weown, error, deferred;
    489 
    490 	pthread__error(EINVAL, "Invalid mutex",
    491 	    ptm->ptm_magic == _PT_MUTEX_MAGIC);
    492 
    493 	self = pthread__self();
    494 	owner = ptm->ptm_owner;
    495 	weown = (MUTEX_OWNER(owner) == (uintptr_t)self);
    496 	deferred = (int)((uintptr_t)owner & MUTEX_DEFERRED_BIT);
    497 	error = 0;
    498 
    499 	if (__SIMPLELOCK_LOCKED_P(&ptm->ptm_errorcheck)) {
    500 		if (!weown) {
    501 			error = EPERM;
    502 			new = owner;
    503 		} else {
    504 			new = NULL;
    505 		}
    506 	} else if (MUTEX_RECURSIVE(owner)) {
    507 		if (!weown) {
    508 			error = EPERM;
    509 			new = owner;
    510 		} else if (ptm->ptm_recursed) {
    511 			ptm->ptm_recursed--;
    512 			new = owner;
    513 		} else {
    514 			new = (pthread_t)MUTEX_RECURSIVE_BIT;
    515 		}
    516 	} else {
    517 		pthread__error(EPERM,
    518 		    "Unlocking unlocked mutex", (owner != NULL));
    519 		pthread__error(EPERM,
    520 		    "Unlocking mutex owned by another thread", weown);
    521 		new = NULL;
    522 	}
    523 
    524 	/*
    525 	 * Release the mutex.  If there appear to be waiters, then
    526 	 * wake them up.
    527 	 */
    528 	if (new != owner) {
    529 		owner = atomic_swap_ptr(&ptm->ptm_owner, new);
    530 		if (__predict_false(MUTEX_PROTECT(owner))) {
    531 			/* restore elevated priority */
    532 			(void)_sched_protect(-1);
    533 		}
    534 		if (MUTEX_HAS_WAITERS(owner) != 0) {
    535 			pthread__mutex_wakeup(self, ptm);
    536 			return 0;
    537 		}
    538 	}
    539 
    540 	/*
    541 	 * There were no waiters, but we may have deferred waking
    542 	 * other threads until mutex unlock - we must wake them now.
    543 	 */
    544 	if (!deferred)
    545 		return error;
    546 
    547 	if (self->pt_nwaiters == 1) {
    548 		/*
    549 		 * If the calling thread is about to block, defer
    550 		 * unparking the target until _lwp_park() is called.
    551 		 */
    552 		if (self->pt_willpark && self->pt_unpark == 0) {
    553 			self->pt_unpark = self->pt_waiters[0];
    554 		} else {
    555 			(void)_lwp_unpark(self->pt_waiters[0],
    556 			    __UNVOLATILE(&ptm->ptm_waiters));
    557 		}
    558 	} else {
    559 		(void)_lwp_unpark_all(self->pt_waiters, self->pt_nwaiters,
    560 		    __UNVOLATILE(&ptm->ptm_waiters));
    561 	}
    562 	self->pt_nwaiters = 0;
    563 
    564 	return error;
    565 }
    566 
    567 /*
    568  * pthread__mutex_wakeup: unpark threads waiting for us
    569  *
    570  * unpark threads on the ptm->ptm_waiters list and self->pt_waiters.
    571  */
    572 
    573 static void
    574 pthread__mutex_wakeup(pthread_t self, pthread_mutex_t *ptm)
    575 {
    576 	pthread_t thread, next;
    577 	ssize_t n, rv;
    578 
    579 	/*
    580 	 * Take ownership of the current set of waiters.  No
    581 	 * need for a memory barrier following this, all loads
    582 	 * are dependent upon 'thread'.
    583 	 */
    584 	thread = atomic_swap_ptr(&ptm->ptm_waiters, NULL);
    585 	pthread__smt_wake();
    586 
    587 	for (;;) {
    588 		/*
    589 		 * Pull waiters from the queue and add to our list.
    590 		 * Use a memory barrier to ensure that we safely
    591 		 * read the value of pt_mutexnext before 'thread'
    592 		 * sees pt_mutexwait being cleared.
    593 		 */
    594 		for (n = self->pt_nwaiters, self->pt_nwaiters = 0;
    595 		    n < pthread__unpark_max && thread != NULL;
    596 		    thread = next) {
    597 		    	next = thread->pt_mutexnext;
    598 		    	if (thread != self) {
    599 				self->pt_waiters[n++] = thread->pt_lid;
    600 				membar_sync();
    601 			}
    602 			thread->pt_mutexwait = 0;
    603 			/* No longer safe to touch 'thread' */
    604 		}
    605 
    606 		switch (n) {
    607 		case 0:
    608 			return;
    609 		case 1:
    610 			/*
    611 			 * If the calling thread is about to block,
    612 			 * defer unparking the target until _lwp_park()
    613 			 * is called.
    614 			 */
    615 			if (self->pt_willpark && self->pt_unpark == 0) {
    616 				self->pt_unpark = self->pt_waiters[0];
    617 				return;
    618 			}
    619 			rv = (ssize_t)_lwp_unpark(self->pt_waiters[0],
    620 			    __UNVOLATILE(&ptm->ptm_waiters));
    621 			if (rv != 0 && errno != EALREADY && errno != EINTR &&
    622 			    errno != ESRCH) {
    623 				pthread__errorfunc(__FILE__, __LINE__,
    624 				    __func__, "_lwp_unpark failed");
    625 			}
    626 			return;
    627 		default:
    628 			rv = _lwp_unpark_all(self->pt_waiters, (size_t)n,
    629 			    __UNVOLATILE(&ptm->ptm_waiters));
    630 			if (rv != 0 && errno != EINTR) {
    631 				pthread__errorfunc(__FILE__, __LINE__,
    632 				    __func__, "_lwp_unpark_all failed");
    633 			}
    634 			break;
    635 		}
    636 	}
    637 }
    638 
    639 int
    640 pthread_mutexattr_init(pthread_mutexattr_t *attr)
    641 {
    642 	if (__predict_false(__uselibcstub))
    643 		return __libc_mutexattr_init_stub(attr);
    644 
    645 	attr->ptma_magic = _PT_MUTEXATTR_MAGIC;
    646 	attr->ptma_private = (void *)PTHREAD_MUTEX_DEFAULT;
    647 	return 0;
    648 }
    649 
    650 int
    651 pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
    652 {
    653 	if (__predict_false(__uselibcstub))
    654 		return __libc_mutexattr_destroy_stub(attr);
    655 
    656 	pthread__error(EINVAL, "Invalid mutex attribute",
    657 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    658 
    659 	return 0;
    660 }
    661 
    662 int
    663 pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *typep)
    664 {
    665 
    666 	pthread__error(EINVAL, "Invalid mutex attribute",
    667 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    668 
    669 	*typep = MUTEX_GET_TYPE(attr->ptma_private);
    670 	return 0;
    671 }
    672 
    673 int
    674 pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
    675 {
    676 
    677 	if (__predict_false(__uselibcstub))
    678 		return __libc_mutexattr_settype_stub(attr, type);
    679 
    680 	pthread__error(EINVAL, "Invalid mutex attribute",
    681 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    682 
    683 	switch (type) {
    684 	case PTHREAD_MUTEX_NORMAL:
    685 	case PTHREAD_MUTEX_ERRORCHECK:
    686 	case PTHREAD_MUTEX_RECURSIVE:
    687 		MUTEX_SET_TYPE(attr->ptma_private, type);
    688 		return 0;
    689 	default:
    690 		return EINVAL;
    691 	}
    692 }
    693 
    694 int
    695 pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr, int*proto)
    696 {
    697 
    698 	pthread__error(EINVAL, "Invalid mutex attribute",
    699 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    700 
    701 	*proto = MUTEX_GET_PROTOCOL(attr->ptma_private);
    702 	return 0;
    703 }
    704 
    705 int
    706 pthread_mutexattr_setprotocol(pthread_mutexattr_t* attr, int proto)
    707 {
    708 
    709 	pthread__error(EINVAL, "Invalid mutex attribute",
    710 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    711 
    712 	switch (proto) {
    713 	case PTHREAD_PRIO_NONE:
    714 	case PTHREAD_PRIO_PROTECT:
    715 		MUTEX_SET_PROTOCOL(attr->ptma_private, proto);
    716 		return 0;
    717 	case PTHREAD_PRIO_INHERIT:
    718 		return ENOTSUP;
    719 	default:
    720 		return EINVAL;
    721 	}
    722 }
    723 
    724 int
    725 pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *attr, int *ceil)
    726 {
    727 
    728 	pthread__error(EINVAL, "Invalid mutex attribute",
    729 		attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    730 
    731 	*ceil = MUTEX_GET_CEILING(attr->ptma_private);
    732 	return 0;
    733 }
    734 
    735 int
    736 pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr, int ceil)
    737 {
    738 
    739 	pthread__error(EINVAL, "Invalid mutex attribute",
    740 		attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    741 
    742 	if (ceil & ~0xff)
    743 		return EINVAL;
    744 
    745 	MUTEX_SET_CEILING(attr->ptma_private, ceil);
    746 	return 0;
    747 }
    748 
    749 #ifdef _PTHREAD_PSHARED
    750 int
    751 pthread_mutexattr_getpshared(const pthread_mutexattr_t * __restrict attr,
    752     int * __restrict pshared)
    753 {
    754 
    755 	*pshared = PTHREAD_PROCESS_PRIVATE;
    756 	return 0;
    757 }
    758 
    759 int
    760 pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
    761 {
    762 
    763 	switch(pshared) {
    764 	case PTHREAD_PROCESS_PRIVATE:
    765 		return 0;
    766 	case PTHREAD_PROCESS_SHARED:
    767 		return ENOSYS;
    768 	}
    769 	return EINVAL;
    770 }
    771 #endif
    772 
    773 /*
    774  * pthread__mutex_deferwake: try to defer unparking threads in self->pt_waiters
    775  *
    776  * In order to avoid unnecessary contention on the interlocking mutex,
    777  * we defer waking up threads until we unlock the mutex.  The threads will
    778  * be woken up when the calling thread (self) releases the first mutex with
    779  * MUTEX_DEFERRED_BIT set.  It likely be the mutex 'ptm', but no problem
    780  * even if it isn't.
    781  */
    782 
    783 void
    784 pthread__mutex_deferwake(pthread_t self, pthread_mutex_t *ptm)
    785 {
    786 
    787 	if (__predict_false(ptm == NULL ||
    788 	    MUTEX_OWNER(ptm->ptm_owner) != (uintptr_t)self)) {
    789 	    	(void)_lwp_unpark_all(self->pt_waiters, self->pt_nwaiters,
    790 	    	    __UNVOLATILE(&ptm->ptm_waiters));
    791 	    	self->pt_nwaiters = 0;
    792 	} else {
    793 		atomic_or_ulong((volatile unsigned long *)
    794 		    (uintptr_t)&ptm->ptm_owner,
    795 		    (unsigned long)MUTEX_DEFERRED_BIT);
    796 	}
    797 }
    798 
    799 int
    800 pthread_mutex_getprioceiling(const pthread_mutex_t *ptm, int *ceil)
    801 {
    802 	*ceil = ptm->ptm_ceiling;
    803 	return 0;
    804 }
    805 
    806 int
    807 pthread_mutex_setprioceiling(pthread_mutex_t *ptm, int ceil, int *old_ceil)
    808 {
    809 	int error;
    810 
    811 	error = pthread_mutex_lock(ptm);
    812 	if (error == 0) {
    813 		*old_ceil = ptm->ptm_ceiling;
    814 		/*check range*/
    815 		ptm->ptm_ceiling = ceil;
    816 		pthread_mutex_unlock(ptm);
    817 	}
    818 	return error;
    819 }
    820 
    821 int
    822 _pthread_mutex_held_np(pthread_mutex_t *ptm)
    823 {
    824 
    825 	return MUTEX_OWNER(ptm->ptm_owner) == (uintptr_t)pthread__self();
    826 }
    827 
    828 pthread_t
    829 _pthread_mutex_owner_np(pthread_mutex_t *ptm)
    830 {
    831 
    832 	return (pthread_t)MUTEX_OWNER(ptm->ptm_owner);
    833 }
    834