Home | History | Annotate | Line # | Download | only in libpthread
pthread_mutex.c revision 1.70
      1 /*	$NetBSD: pthread_mutex.c,v 1.70 2020/01/29 21:11:24 kamil 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.70 2020/01/29 21:11:24 kamil 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 0
    126 	/*
    127 	 * Always initialize the mutex structure, maybe be used later
    128 	 * and the cost should be minimal.
    129 	 */
    130 	if (__predict_false(__uselibcstub))
    131 		return __libc_mutex_init_stub(ptm, attr);
    132 #endif
    133 
    134 	if (attr == NULL) {
    135 		type = PTHREAD_MUTEX_NORMAL;
    136 		proto = PTHREAD_PRIO_NONE;
    137 		ceil = 0;
    138 	} else {
    139 		val = (uintptr_t)attr->ptma_private;
    140 
    141 		type = MUTEX_GET_TYPE(val);
    142 		proto = MUTEX_GET_PROTOCOL(val);
    143 		ceil = MUTEX_GET_CEILING(val);
    144 	}
    145 	switch (type) {
    146 	case PTHREAD_MUTEX_ERRORCHECK:
    147 		__cpu_simple_lock_set(&ptm->ptm_errorcheck);
    148 		ptm->ptm_owner = NULL;
    149 		break;
    150 	case PTHREAD_MUTEX_RECURSIVE:
    151 		__cpu_simple_lock_clear(&ptm->ptm_errorcheck);
    152 		ptm->ptm_owner = (void *)MUTEX_RECURSIVE_BIT;
    153 		break;
    154 	default:
    155 		__cpu_simple_lock_clear(&ptm->ptm_errorcheck);
    156 		ptm->ptm_owner = NULL;
    157 		break;
    158 	}
    159 	switch (proto) {
    160 	case PTHREAD_PRIO_PROTECT:
    161 		val = (uintptr_t)ptm->ptm_owner;
    162 		val |= MUTEX_PROTECT_BIT;
    163 		ptm->ptm_owner = (void *)val;
    164 		break;
    165 
    166 	}
    167 	ptm->ptm_magic = _PT_MUTEX_MAGIC;
    168 	ptm->ptm_waiters = NULL;
    169 	ptm->ptm_recursed = 0;
    170 	ptm->ptm_ceiling = (unsigned char)ceil;
    171 
    172 	return 0;
    173 }
    174 
    175 int
    176 pthread_mutex_destroy(pthread_mutex_t *ptm)
    177 {
    178 
    179 	if (__predict_false(__uselibcstub))
    180 		return __libc_mutex_destroy_stub(ptm);
    181 
    182 	pthread__error(EINVAL, "Invalid mutex",
    183 	    ptm->ptm_magic == _PT_MUTEX_MAGIC);
    184 	pthread__error(EBUSY, "Destroying locked mutex",
    185 	    MUTEX_OWNER(ptm->ptm_owner) == 0);
    186 
    187 	ptm->ptm_magic = _PT_MUTEX_DEAD;
    188 	return 0;
    189 }
    190 
    191 int
    192 pthread_mutex_lock(pthread_mutex_t *ptm)
    193 {
    194 	pthread_t self;
    195 	void *val;
    196 
    197 	if (__predict_false(__uselibcstub))
    198 		return __libc_mutex_lock_stub(ptm);
    199 
    200 	pthread__error(EINVAL, "Invalid mutex",
    201 	    ptm->ptm_magic == _PT_MUTEX_MAGIC);
    202 
    203 	self = pthread__self();
    204 	val = atomic_cas_ptr(&ptm->ptm_owner, NULL, self);
    205 	if (__predict_true(val == NULL)) {
    206 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    207 		membar_enter();
    208 #endif
    209 		return 0;
    210 	}
    211 	return pthread__mutex_lock_slow(ptm, NULL);
    212 }
    213 
    214 int
    215 pthread_mutex_timedlock(pthread_mutex_t* ptm, const struct timespec *ts)
    216 {
    217 	pthread_t self;
    218 	void *val;
    219 
    220 	pthread__error(EINVAL, "Invalid mutex",
    221 	    ptm->ptm_magic == _PT_MUTEX_MAGIC);
    222 
    223 	self = pthread__self();
    224 	val = atomic_cas_ptr(&ptm->ptm_owner, NULL, self);
    225 	if (__predict_true(val == NULL)) {
    226 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    227 		membar_enter();
    228 #endif
    229 		return 0;
    230 	}
    231 	return pthread__mutex_lock_slow(ptm, ts);
    232 }
    233 
    234 /* We want function call overhead. */
    235 NOINLINE static void
    236 pthread__mutex_pause(void)
    237 {
    238 
    239 	pthread__smt_pause();
    240 }
    241 
    242 /*
    243  * Spin while the holder is running.  'lwpctl' gives us the true
    244  * status of the thread.
    245  */
    246 NOINLINE static void *
    247 pthread__mutex_spin(pthread_mutex_t *ptm, pthread_t owner)
    248 {
    249 	pthread_t thread;
    250 	unsigned int count, i;
    251 
    252 	for (count = 2;; owner = ptm->ptm_owner) {
    253 		thread = (pthread_t)MUTEX_OWNER(owner);
    254 		if (thread == NULL)
    255 			break;
    256 		if (thread->pt_lwpctl->lc_curcpu == LWPCTL_CPU_NONE)
    257 			break;
    258 		if (count < 128)
    259 			count += count;
    260 		for (i = count; i != 0; i--)
    261 			pthread__mutex_pause();
    262 	}
    263 
    264 	return owner;
    265 }
    266 
    267 NOINLINE static bool
    268 pthread__mutex_setwaiters(pthread_t self, pthread_mutex_t *ptm)
    269 {
    270 	void *owner, *next;
    271 
    272 	/*
    273 	 * Note that the mutex can become unlocked before we set
    274 	 * the waiters bit.  If that happens it's not safe to sleep
    275 	 * as we may never be awoken: we must remove the current
    276 	 * thread from the waiters list and try again.
    277 	 *
    278 	 * Because we are doing this atomically, we can't remove
    279 	 * one waiter: we must remove all waiters and awken them,
    280 	 * then sleep in _lwp_park() until we have been awoken.
    281 	 *
    282 	 * Issue a memory barrier to ensure that we are reading
    283 	 * the value of ptm_owner/pt_mutexwait after we have entered
    284 	 * the waiters list (the CAS itself must be atomic).
    285 	 */
    286 	for (owner = ptm->ptm_owner;; owner = next) {
    287 		if (MUTEX_OWNER(owner) == 0) {
    288 			pthread__mutex_wakeup(self, ptm);
    289 			return true;
    290 		}
    291 		if (MUTEX_HAS_WAITERS(owner)) {
    292 			return false;
    293 		}
    294 		next = atomic_cas_ptr(&ptm->ptm_owner, owner,
    295 		    (void *)((uintptr_t)owner | MUTEX_WAITERS_BIT));
    296 	}
    297 }
    298 
    299 NOINLINE static int
    300 pthread__mutex_lock_slow(pthread_mutex_t *ptm, const struct timespec *ts)
    301 {
    302 	void *waiters, *new, *owner, *next;
    303 	pthread_t self;
    304 	int serrno;
    305 	int error;
    306 
    307 	owner = ptm->ptm_owner;
    308 	self = pthread__self();
    309 
    310 	/* Recursive or errorcheck? */
    311 	if (MUTEX_OWNER(owner) == (uintptr_t)self) {
    312 		if (MUTEX_RECURSIVE(owner)) {
    313 			if (ptm->ptm_recursed == INT_MAX)
    314 				return EAGAIN;
    315 			ptm->ptm_recursed++;
    316 			return 0;
    317 		}
    318 		if (__SIMPLELOCK_LOCKED_P(&ptm->ptm_errorcheck))
    319 			return EDEADLK;
    320 	}
    321 
    322 	/* priority protect */
    323 	if (MUTEX_PROTECT(owner) && _sched_protect(ptm->ptm_ceiling) == -1) {
    324 		return errno;
    325 	}
    326 	serrno = errno;
    327 	for (;; owner = ptm->ptm_owner) {
    328 		/* Spin while the owner is running. */
    329 		if (MUTEX_OWNER(owner) != (uintptr_t)self)
    330 			owner = pthread__mutex_spin(ptm, owner);
    331 
    332 		/* If it has become free, try to acquire it again. */
    333 		if (MUTEX_OWNER(owner) == 0) {
    334 			do {
    335 				new = (void *)
    336 				    ((uintptr_t)self | (uintptr_t)owner);
    337 				next = atomic_cas_ptr(&ptm->ptm_owner, owner,
    338 				    new);
    339 				if (next == owner) {
    340 					errno = serrno;
    341 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    342 					membar_enter();
    343 #endif
    344 					return 0;
    345 				}
    346 				owner = next;
    347 			} while (MUTEX_OWNER(owner) == 0);
    348 			/*
    349 			 * We have lost the race to acquire the mutex.
    350 			 * The new owner could be running on another
    351 			 * CPU, in which case we should spin and avoid
    352 			 * the overhead of blocking.
    353 			 */
    354 			continue;
    355 		}
    356 
    357 		/*
    358 		 * Nope, still held.  Add thread to the list of waiters.
    359 		 * Issue a memory barrier to ensure mutexwait/mutexnext
    360 		 * are visible before we enter the waiters list.
    361 		 */
    362 		self->pt_mutexwait = 1;
    363 		for (waiters = ptm->ptm_waiters;; waiters = next) {
    364 			self->pt_mutexnext = waiters;
    365 			membar_producer();
    366 			next = atomic_cas_ptr(&ptm->ptm_waiters, waiters, self);
    367 			if (next == waiters)
    368 			    	break;
    369 		}
    370 
    371 		/* Set the waiters bit and block. */
    372 		membar_sync();
    373 		if (pthread__mutex_setwaiters(self, ptm)) {
    374 			continue;
    375 		}
    376 
    377 		/*
    378 		 * We may have been awoken by the current thread above,
    379 		 * or will be awoken by the current holder of the mutex.
    380 		 * The key requirement is that we must not proceed until
    381 		 * told that we are no longer waiting (via pt_mutexwait
    382 		 * being set to zero).  Otherwise it is unsafe to re-enter
    383 		 * the thread onto the waiters list.
    384 		 */
    385 		membar_sync();
    386 		while (self->pt_mutexwait) {
    387 			error = _lwp_park(CLOCK_REALTIME, TIMER_ABSTIME,
    388 			    __UNCONST(ts), self->pt_unpark,
    389 			    __UNVOLATILE(&ptm->ptm_waiters),
    390 			    __UNVOLATILE(&ptm->ptm_waiters));
    391 			self->pt_unpark = 0;
    392 			if (__predict_true(error != -1)) {
    393 				continue;
    394 			}
    395 			if (errno == ETIMEDOUT && self->pt_mutexwait) {
    396 				/*Remove self from waiters list*/
    397 				pthread__mutex_wakeup(self, ptm);
    398 				/*priority protect*/
    399 				if (MUTEX_PROTECT(owner))
    400 					(void)_sched_protect(-1);
    401 				return ETIMEDOUT;
    402 			}
    403 		}
    404 	}
    405 }
    406 
    407 int
    408 pthread_mutex_trylock(pthread_mutex_t *ptm)
    409 {
    410 	pthread_t self;
    411 	void *val, *new, *next;
    412 
    413 	if (__predict_false(__uselibcstub))
    414 		return __libc_mutex_trylock_stub(ptm);
    415 
    416 	pthread__error(EINVAL, "Invalid mutex",
    417 	    ptm->ptm_magic == _PT_MUTEX_MAGIC);
    418 
    419 	self = pthread__self();
    420 	val = atomic_cas_ptr(&ptm->ptm_owner, NULL, self);
    421 	if (__predict_true(val == NULL)) {
    422 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    423 		membar_enter();
    424 #endif
    425 		return 0;
    426 	}
    427 
    428 	if (MUTEX_RECURSIVE(val)) {
    429 		if (MUTEX_OWNER(val) == 0) {
    430 			new = (void *)((uintptr_t)self | (uintptr_t)val);
    431 			next = atomic_cas_ptr(&ptm->ptm_owner, val, new);
    432 			if (__predict_true(next == val)) {
    433 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    434 				membar_enter();
    435 #endif
    436 				return 0;
    437 			}
    438 		}
    439 		if (MUTEX_OWNER(val) == (uintptr_t)self) {
    440 			if (ptm->ptm_recursed == INT_MAX)
    441 				return EAGAIN;
    442 			ptm->ptm_recursed++;
    443 			return 0;
    444 		}
    445 	}
    446 
    447 	return EBUSY;
    448 }
    449 
    450 int
    451 pthread_mutex_unlock(pthread_mutex_t *ptm)
    452 {
    453 	pthread_t self;
    454 	void *value;
    455 
    456 	if (__predict_false(__uselibcstub))
    457 		return __libc_mutex_unlock_stub(ptm);
    458 
    459 	pthread__error(EINVAL, "Invalid mutex",
    460 	    ptm->ptm_magic == _PT_MUTEX_MAGIC);
    461 
    462 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    463 	membar_exit();
    464 #endif
    465 	self = pthread__self();
    466 	value = atomic_cas_ptr(&ptm->ptm_owner, self, NULL);
    467 	if (__predict_true(value == self)) {
    468 		pthread__smt_wake();
    469 		return 0;
    470 	}
    471 	return pthread__mutex_unlock_slow(ptm);
    472 }
    473 
    474 NOINLINE static int
    475 pthread__mutex_unlock_slow(pthread_mutex_t *ptm)
    476 {
    477 	pthread_t self, owner, new;
    478 	int weown, error;
    479 
    480 	self = pthread__self();
    481 	owner = ptm->ptm_owner;
    482 	weown = (MUTEX_OWNER(owner) == (uintptr_t)self);
    483 	error = 0;
    484 
    485 	if (__SIMPLELOCK_LOCKED_P(&ptm->ptm_errorcheck)) {
    486 		if (!weown) {
    487 			error = EPERM;
    488 			new = owner;
    489 		} else {
    490 			new = NULL;
    491 		}
    492 	} else if (MUTEX_RECURSIVE(owner)) {
    493 		if (!weown) {
    494 			error = EPERM;
    495 			new = owner;
    496 		} else if (ptm->ptm_recursed) {
    497 			ptm->ptm_recursed--;
    498 			new = owner;
    499 		} else {
    500 			new = (pthread_t)MUTEX_RECURSIVE_BIT;
    501 		}
    502 	} else {
    503 		pthread__error(EPERM,
    504 		    "Unlocking unlocked mutex", (owner != NULL));
    505 		pthread__error(EPERM,
    506 		    "Unlocking mutex owned by another thread", weown);
    507 		new = NULL;
    508 	}
    509 
    510 	/*
    511 	 * Release the mutex.  If there appear to be waiters, then
    512 	 * wake them up.
    513 	 */
    514 	if (new != owner) {
    515 		owner = atomic_swap_ptr(&ptm->ptm_owner, new);
    516 		if (__predict_false(MUTEX_PROTECT(owner))) {
    517 			/* restore elevated priority */
    518 			(void)_sched_protect(-1);
    519 		}
    520 		if (MUTEX_HAS_WAITERS(owner) != 0) {
    521 			pthread__mutex_wakeup(self, ptm);
    522 			return 0;
    523 		}
    524 		error = 0;
    525 	}
    526 
    527 	if (self->pt_nwaiters == 1) {
    528 		/*
    529 		 * If the calling thread is about to block, defer
    530 		 * unparking the target until _lwp_park() is called.
    531 		 */
    532 		if (self->pt_willpark && self->pt_unpark == 0) {
    533 			self->pt_unpark = self->pt_waiters[0];
    534 		} else {
    535 			(void)_lwp_unpark(self->pt_waiters[0],
    536 			    __UNVOLATILE(&ptm->ptm_waiters));
    537 		}
    538 	} else if (self->pt_nwaiters > 0) {
    539 		(void)_lwp_unpark_all(self->pt_waiters, self->pt_nwaiters,
    540 		    __UNVOLATILE(&ptm->ptm_waiters));
    541 	}
    542 	self->pt_nwaiters = 0;
    543 
    544 	return error;
    545 }
    546 
    547 /*
    548  * pthread__mutex_wakeup: unpark threads waiting for us
    549  *
    550  * unpark threads on the ptm->ptm_waiters list and self->pt_waiters.
    551  */
    552 
    553 static void
    554 pthread__mutex_wakeup(pthread_t self, pthread_mutex_t *ptm)
    555 {
    556 	pthread_t thread, next;
    557 	ssize_t n, rv;
    558 
    559 	/* Take ownership of the current set of waiters. */
    560 	thread = atomic_swap_ptr(&ptm->ptm_waiters, NULL);
    561 	membar_datadep_consumer(); /* for alpha */
    562 	pthread__smt_wake();
    563 
    564 	for (;;) {
    565 		/*
    566 		 * Pull waiters from the queue and add to our list.
    567 		 * Use a memory barrier to ensure that we safely
    568 		 * read the value of pt_mutexnext before 'thread'
    569 		 * sees pt_mutexwait being cleared.
    570 		 */
    571 		for (n = self->pt_nwaiters, self->pt_nwaiters = 0;
    572 		    n < pthread__unpark_max && thread != NULL;
    573 		    thread = next) {
    574 		    	next = thread->pt_mutexnext;
    575 		    	if (thread != self) {
    576 				self->pt_waiters[n++] = thread->pt_lid;
    577 				membar_sync();
    578 			}
    579 			thread->pt_mutexwait = 0;
    580 			/* No longer safe to touch 'thread' */
    581 		}
    582 
    583 		switch (n) {
    584 		case 0:
    585 			return;
    586 		case 1:
    587 			/*
    588 			 * If the calling thread is about to block,
    589 			 * defer unparking the target until _lwp_park()
    590 			 * is called.
    591 			 */
    592 			if (self->pt_willpark && self->pt_unpark == 0) {
    593 				self->pt_unpark = self->pt_waiters[0];
    594 				return;
    595 			}
    596 			rv = (ssize_t)_lwp_unpark(self->pt_waiters[0],
    597 			    __UNVOLATILE(&ptm->ptm_waiters));
    598 			if (rv != 0 && errno != EALREADY && errno != EINTR &&
    599 			    errno != ESRCH) {
    600 				pthread__errorfunc(__FILE__, __LINE__,
    601 				    __func__, "_lwp_unpark failed");
    602 			}
    603 			return;
    604 		default:
    605 			rv = _lwp_unpark_all(self->pt_waiters, (size_t)n,
    606 			    __UNVOLATILE(&ptm->ptm_waiters));
    607 			if (rv != 0 && errno != EINTR) {
    608 				pthread__errorfunc(__FILE__, __LINE__,
    609 				    __func__, "_lwp_unpark_all failed");
    610 			}
    611 			break;
    612 		}
    613 	}
    614 }
    615 
    616 int
    617 pthread_mutexattr_init(pthread_mutexattr_t *attr)
    618 {
    619 	if (__predict_false(__uselibcstub))
    620 		return __libc_mutexattr_init_stub(attr);
    621 
    622 	attr->ptma_magic = _PT_MUTEXATTR_MAGIC;
    623 	attr->ptma_private = (void *)PTHREAD_MUTEX_DEFAULT;
    624 	return 0;
    625 }
    626 
    627 int
    628 pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
    629 {
    630 	if (__predict_false(__uselibcstub))
    631 		return __libc_mutexattr_destroy_stub(attr);
    632 
    633 	pthread__error(EINVAL, "Invalid mutex attribute",
    634 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    635 
    636 	attr->ptma_magic = _PT_MUTEXATTR_DEAD;
    637 
    638 	return 0;
    639 }
    640 
    641 int
    642 pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *typep)
    643 {
    644 
    645 	pthread__error(EINVAL, "Invalid mutex attribute",
    646 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    647 
    648 	*typep = MUTEX_GET_TYPE(attr->ptma_private);
    649 	return 0;
    650 }
    651 
    652 int
    653 pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
    654 {
    655 
    656 	if (__predict_false(__uselibcstub))
    657 		return __libc_mutexattr_settype_stub(attr, type);
    658 
    659 	pthread__error(EINVAL, "Invalid mutex attribute",
    660 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    661 
    662 	switch (type) {
    663 	case PTHREAD_MUTEX_NORMAL:
    664 	case PTHREAD_MUTEX_ERRORCHECK:
    665 	case PTHREAD_MUTEX_RECURSIVE:
    666 		MUTEX_SET_TYPE(attr->ptma_private, type);
    667 		return 0;
    668 	default:
    669 		return EINVAL;
    670 	}
    671 }
    672 
    673 int
    674 pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr, int*proto)
    675 {
    676 
    677 	pthread__error(EINVAL, "Invalid mutex attribute",
    678 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    679 
    680 	*proto = MUTEX_GET_PROTOCOL(attr->ptma_private);
    681 	return 0;
    682 }
    683 
    684 int
    685 pthread_mutexattr_setprotocol(pthread_mutexattr_t* attr, int proto)
    686 {
    687 
    688 	pthread__error(EINVAL, "Invalid mutex attribute",
    689 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    690 
    691 	switch (proto) {
    692 	case PTHREAD_PRIO_NONE:
    693 	case PTHREAD_PRIO_PROTECT:
    694 		MUTEX_SET_PROTOCOL(attr->ptma_private, proto);
    695 		return 0;
    696 	case PTHREAD_PRIO_INHERIT:
    697 		return ENOTSUP;
    698 	default:
    699 		return EINVAL;
    700 	}
    701 }
    702 
    703 int
    704 pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *attr, int *ceil)
    705 {
    706 
    707 	pthread__error(EINVAL, "Invalid mutex attribute",
    708 		attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    709 
    710 	*ceil = MUTEX_GET_CEILING(attr->ptma_private);
    711 	return 0;
    712 }
    713 
    714 int
    715 pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr, int ceil)
    716 {
    717 
    718 	pthread__error(EINVAL, "Invalid mutex attribute",
    719 		attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    720 
    721 	if (ceil & ~0xff)
    722 		return EINVAL;
    723 
    724 	MUTEX_SET_CEILING(attr->ptma_private, ceil);
    725 	return 0;
    726 }
    727 
    728 #ifdef _PTHREAD_PSHARED
    729 int
    730 pthread_mutexattr_getpshared(const pthread_mutexattr_t * __restrict attr,
    731     int * __restrict pshared)
    732 {
    733 
    734 	pthread__error(EINVAL, "Invalid mutex attribute",
    735 		attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    736 
    737 	*pshared = PTHREAD_PROCESS_PRIVATE;
    738 	return 0;
    739 }
    740 
    741 int
    742 pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
    743 {
    744 
    745 	pthread__error(EINVAL, "Invalid mutex attribute",
    746 		attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    747 
    748 	switch(pshared) {
    749 	case PTHREAD_PROCESS_PRIVATE:
    750 		return 0;
    751 	case PTHREAD_PROCESS_SHARED:
    752 		return ENOSYS;
    753 	}
    754 	return EINVAL;
    755 }
    756 #endif
    757 
    758 /*
    759  * pthread__mutex_deferwake: try to defer unparking threads in self->pt_waiters
    760  *
    761  * In order to avoid unnecessary contention on the interlocking mutex,
    762  * we defer waking up threads until we unlock the mutex.  The threads will
    763  * be woken up when the calling thread (self) releases the first mutex with
    764  * MUTEX_DEFERRED_BIT set.  It likely be the mutex 'ptm', but no problem
    765  * even if it isn't.
    766  */
    767 
    768 void
    769 pthread__mutex_deferwake(pthread_t self, pthread_mutex_t *ptm)
    770 {
    771 
    772 	if (__predict_false(ptm == NULL ||
    773 	    MUTEX_OWNER(ptm->ptm_owner) != (uintptr_t)self)) {
    774 	    	(void)_lwp_unpark_all(self->pt_waiters, self->pt_nwaiters,
    775 	    	    __UNVOLATILE(&ptm->ptm_waiters));
    776 	    	self->pt_nwaiters = 0;
    777 	} else {
    778 		atomic_or_ulong((volatile unsigned long *)
    779 		    (uintptr_t)&ptm->ptm_owner,
    780 		    (unsigned long)MUTEX_DEFERRED_BIT);
    781 	}
    782 }
    783 
    784 int
    785 pthread_mutex_getprioceiling(const pthread_mutex_t *ptm, int *ceil)
    786 {
    787 
    788 	pthread__error(EINVAL, "Invalid mutex",
    789 	    ptm->ptm_magic == _PT_MUTEX_MAGIC);
    790 
    791 	*ceil = ptm->ptm_ceiling;
    792 	return 0;
    793 }
    794 
    795 int
    796 pthread_mutex_setprioceiling(pthread_mutex_t *ptm, int ceil, int *old_ceil)
    797 {
    798 	int error;
    799 
    800 	pthread__error(EINVAL, "Invalid mutex",
    801 	    ptm->ptm_magic == _PT_MUTEX_MAGIC);
    802 
    803 	error = pthread_mutex_lock(ptm);
    804 	if (error == 0) {
    805 		*old_ceil = ptm->ptm_ceiling;
    806 		/*check range*/
    807 		ptm->ptm_ceiling = ceil;
    808 		pthread_mutex_unlock(ptm);
    809 	}
    810 	return error;
    811 }
    812 
    813 int
    814 _pthread_mutex_held_np(pthread_mutex_t *ptm)
    815 {
    816 
    817 	return MUTEX_OWNER(ptm->ptm_owner) == (uintptr_t)pthread__self();
    818 }
    819 
    820 pthread_t
    821 _pthread_mutex_owner_np(pthread_mutex_t *ptm)
    822 {
    823 
    824 	return (pthread_t)MUTEX_OWNER(ptm->ptm_owner);
    825 }
    826