Home | History | Annotate | Line # | Download | only in libpthread
pthread_mutex.c revision 1.79
      1 /*	$NetBSD: pthread_mutex.c,v 1.79 2020/06/03 22:10:24 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001, 2003, 2006, 2007, 2008, 2020 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.79 2020/06/03 22:10:24 ad 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_PROTECT_BIT		((uintptr_t)0x08)
     71 #define	MUTEX_THREAD			((uintptr_t)~0x0f)
     72 
     73 #define	MUTEX_HAS_WAITERS(x)		((uintptr_t)(x) & MUTEX_WAITERS_BIT)
     74 #define	MUTEX_RECURSIVE(x)		((uintptr_t)(x) & MUTEX_RECURSIVE_BIT)
     75 #define	MUTEX_PROTECT(x)		((uintptr_t)(x) & MUTEX_PROTECT_BIT)
     76 #define	MUTEX_OWNER(x)			((uintptr_t)(x) & MUTEX_THREAD)
     77 
     78 #define	MUTEX_GET_TYPE(x)		\
     79     ((int)(((uintptr_t)(x) & 0x000000ff) >> 0))
     80 #define	MUTEX_SET_TYPE(x, t) 		\
     81     (x) = (void *)(((uintptr_t)(x) & ~0x000000ff) | ((t) << 0))
     82 #define	MUTEX_GET_PROTOCOL(x)		\
     83     ((int)(((uintptr_t)(x) & 0x0000ff00) >> 8))
     84 #define	MUTEX_SET_PROTOCOL(x, p)	\
     85     (x) = (void *)(((uintptr_t)(x) & ~0x0000ff00) | ((p) << 8))
     86 #define	MUTEX_GET_CEILING(x)		\
     87     ((int)(((uintptr_t)(x) & 0x00ff0000) >> 16))
     88 #define	MUTEX_SET_CEILING(x, c)	\
     89     (x) = (void *)(((uintptr_t)(x) & ~0x00ff0000) | ((c) << 16))
     90 
     91 #if __GNUC_PREREQ__(3, 0)
     92 #define	NOINLINE		__attribute ((noinline))
     93 #else
     94 #define	NOINLINE		/* nothing */
     95 #endif
     96 
     97 static void	pthread__mutex_wakeup(pthread_t, pthread_mutex_t *);
     98 static int	pthread__mutex_lock_slow(pthread_mutex_t *,
     99     const struct timespec *);
    100 static void	pthread__mutex_pause(void);
    101 
    102 int		_pthread_mutex_held_np(pthread_mutex_t *);
    103 pthread_t	_pthread_mutex_owner_np(pthread_mutex_t *);
    104 
    105 __weak_alias(pthread_mutex_held_np,_pthread_mutex_held_np)
    106 __weak_alias(pthread_mutex_owner_np,_pthread_mutex_owner_np)
    107 
    108 __strong_alias(__libc_mutex_init,pthread_mutex_init)
    109 __strong_alias(__libc_mutex_lock,pthread_mutex_lock)
    110 __strong_alias(__libc_mutex_trylock,pthread_mutex_trylock)
    111 __strong_alias(__libc_mutex_unlock,pthread_mutex_unlock)
    112 __strong_alias(__libc_mutex_destroy,pthread_mutex_destroy)
    113 
    114 __strong_alias(__libc_mutexattr_init,pthread_mutexattr_init)
    115 __strong_alias(__libc_mutexattr_destroy,pthread_mutexattr_destroy)
    116 __strong_alias(__libc_mutexattr_settype,pthread_mutexattr_settype)
    117 
    118 int
    119 pthread_mutex_init(pthread_mutex_t *ptm, const pthread_mutexattr_t *attr)
    120 {
    121 	uintptr_t type, proto, val, ceil;
    122 
    123 #if 0
    124 	/*
    125 	 * Always initialize the mutex structure, maybe be used later
    126 	 * and the cost should be minimal.
    127 	 */
    128 	if (__predict_false(__uselibcstub))
    129 		return __libc_mutex_init_stub(ptm, attr);
    130 #endif
    131 
    132 	pthread__error(EINVAL, "Invalid mutes attribute",
    133 	    attr == NULL || attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    134 
    135 	if (attr == NULL) {
    136 		type = PTHREAD_MUTEX_NORMAL;
    137 		proto = PTHREAD_PRIO_NONE;
    138 		ceil = 0;
    139 	} else {
    140 		val = (uintptr_t)attr->ptma_private;
    141 
    142 		type = MUTEX_GET_TYPE(val);
    143 		proto = MUTEX_GET_PROTOCOL(val);
    144 		ceil = MUTEX_GET_CEILING(val);
    145 	}
    146 	switch (type) {
    147 	case PTHREAD_MUTEX_ERRORCHECK:
    148 		__cpu_simple_lock_set(&ptm->ptm_errorcheck);
    149 		ptm->ptm_owner = NULL;
    150 		break;
    151 	case PTHREAD_MUTEX_RECURSIVE:
    152 		__cpu_simple_lock_clear(&ptm->ptm_errorcheck);
    153 		ptm->ptm_owner = (void *)MUTEX_RECURSIVE_BIT;
    154 		break;
    155 	default:
    156 		__cpu_simple_lock_clear(&ptm->ptm_errorcheck);
    157 		ptm->ptm_owner = NULL;
    158 		break;
    159 	}
    160 	switch (proto) {
    161 	case PTHREAD_PRIO_PROTECT:
    162 		val = (uintptr_t)ptm->ptm_owner;
    163 		val |= MUTEX_PROTECT_BIT;
    164 		ptm->ptm_owner = (void *)val;
    165 		break;
    166 
    167 	}
    168 	ptm->ptm_magic = _PT_MUTEX_MAGIC;
    169 	ptm->ptm_waiters = NULL;
    170 	ptm->ptm_recursed = 0;
    171 	ptm->ptm_ceiling = (unsigned char)ceil;
    172 
    173 	return 0;
    174 }
    175 
    176 int
    177 pthread_mutex_destroy(pthread_mutex_t *ptm)
    178 {
    179 
    180 	if (__predict_false(__uselibcstub))
    181 		return __libc_mutex_destroy_stub(ptm);
    182 
    183 	pthread__error(EINVAL, "Invalid mutex",
    184 	    ptm->ptm_magic == _PT_MUTEX_MAGIC);
    185 	pthread__error(EBUSY, "Destroying locked mutex",
    186 	    MUTEX_OWNER(ptm->ptm_owner) == 0);
    187 
    188 	ptm->ptm_magic = _PT_MUTEX_DEAD;
    189 	return 0;
    190 }
    191 
    192 int
    193 pthread_mutex_lock(pthread_mutex_t *ptm)
    194 {
    195 	pthread_t self;
    196 	void *val;
    197 
    198 	if (__predict_false(__uselibcstub))
    199 		return __libc_mutex_lock_stub(ptm);
    200 
    201 	pthread__error(EINVAL, "Invalid mutex",
    202 	    ptm->ptm_magic == _PT_MUTEX_MAGIC);
    203 
    204 	self = pthread__self();
    205 	val = atomic_cas_ptr(&ptm->ptm_owner, NULL, self);
    206 	if (__predict_true(val == NULL)) {
    207 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    208 		membar_enter();
    209 #endif
    210 		return 0;
    211 	}
    212 	return pthread__mutex_lock_slow(ptm, NULL);
    213 }
    214 
    215 int
    216 pthread_mutex_timedlock(pthread_mutex_t* ptm, const struct timespec *ts)
    217 {
    218 	pthread_t self;
    219 	void *val;
    220 
    221 	pthread__error(EINVAL, "Invalid mutex",
    222 	    ptm->ptm_magic == _PT_MUTEX_MAGIC);
    223 
    224 	self = pthread__self();
    225 	val = atomic_cas_ptr(&ptm->ptm_owner, NULL, self);
    226 	if (__predict_true(val == NULL)) {
    227 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    228 		membar_enter();
    229 #endif
    230 		return 0;
    231 	}
    232 	return pthread__mutex_lock_slow(ptm, ts);
    233 }
    234 
    235 /* We want function call overhead. */
    236 NOINLINE static void
    237 pthread__mutex_pause(void)
    238 {
    239 
    240 	pthread__smt_pause();
    241 }
    242 
    243 /*
    244  * Spin while the holder is running.  'lwpctl' gives us the true
    245  * status of the thread.
    246  */
    247 NOINLINE static void *
    248 pthread__mutex_spin(pthread_mutex_t *ptm, pthread_t owner)
    249 {
    250 	pthread_t thread;
    251 	unsigned int count, i;
    252 
    253 	for (count = 2;; owner = ptm->ptm_owner) {
    254 		thread = (pthread_t)MUTEX_OWNER(owner);
    255 		if (thread == NULL)
    256 			break;
    257 		if (thread->pt_lwpctl->lc_curcpu == LWPCTL_CPU_NONE)
    258 			break;
    259 		if (count < 128)
    260 			count += count;
    261 		for (i = count; i != 0; i--)
    262 			pthread__mutex_pause();
    263 	}
    264 
    265 	return owner;
    266 }
    267 
    268 NOINLINE static int
    269 pthread__mutex_lock_slow(pthread_mutex_t *ptm, const struct timespec *ts)
    270 {
    271 	void *waiters, *newval, *owner, *next;
    272 	pthread_t self;
    273 	int serrno;
    274 	int error;
    275 
    276 	owner = ptm->ptm_owner;
    277 	self = pthread__self();
    278 	serrno = errno;
    279 
    280 	pthread__assert(!self->pt_willpark);
    281 	pthread__assert(!self->pt_mutexwait);
    282 
    283 	/* Recursive or errorcheck? */
    284 	if (MUTEX_OWNER(owner) == (uintptr_t)self) {
    285 		if (MUTEX_RECURSIVE(owner)) {
    286 			if (ptm->ptm_recursed == INT_MAX)
    287 				return EAGAIN;
    288 			ptm->ptm_recursed++;
    289 			return 0;
    290 		}
    291 		if (__SIMPLELOCK_LOCKED_P(&ptm->ptm_errorcheck))
    292 			return EDEADLK;
    293 	}
    294 
    295 	/* priority protect */
    296 	if (MUTEX_PROTECT(owner) && _sched_protect(ptm->ptm_ceiling) == -1) {
    297 		error = errno;
    298 		errno = serrno;
    299 		return error;
    300 	}
    301 
    302 	for (;;) {
    303 		/* If it has become free, try to acquire it again. */
    304 		if (MUTEX_OWNER(owner) == 0) {
    305 			newval = (void *)((uintptr_t)self | (uintptr_t)owner);
    306 			next = atomic_cas_ptr(&ptm->ptm_owner, owner, newval);
    307 			if (__predict_false(next != owner)) {
    308 				owner = next;
    309 				continue;
    310 			}
    311 			errno = serrno;
    312 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    313 			membar_enter();
    314 #endif
    315 			return 0;
    316 		} else if (MUTEX_OWNER(owner) != (uintptr_t)self) {
    317 			/* Spin while the owner is running. */
    318 			owner = pthread__mutex_spin(ptm, owner);
    319 			if (MUTEX_OWNER(owner) == 0) {
    320 				continue;
    321 			}
    322 		}
    323 
    324 		/*
    325 		 * Nope, still held.  Add thread to the list of waiters.
    326 		 * Issue a memory barrier to ensure mutexwait/mutexnext
    327 		 * are visible before we enter the waiters list.
    328 		 */
    329 		self->pt_mutexwait = 1;
    330 		for (waiters = ptm->ptm_waiters;; waiters = next) {
    331 			self->pt_mutexnext = waiters;
    332 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    333 			membar_producer();
    334 #endif
    335 			next = atomic_cas_ptr(&ptm->ptm_waiters, waiters, self);
    336 			if (next == waiters)
    337 			    	break;
    338 		}
    339 
    340 		/*
    341 		 * Try to set the waiters bit.  If the mutex has become free
    342 		 * since entering self onto the waiters list, need to wake
    343 		 * everybody up (including self) and retry.  It's possible
    344 		 * to race with the unlocking thread, so self may have
    345 		 * already been awoken.
    346 		 */
    347 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    348 		membar_sync();
    349 #endif
    350 		next = atomic_cas_ptr(&ptm->ptm_owner, owner,
    351 		    (void *)((uintptr_t)owner | MUTEX_WAITERS_BIT));
    352 		if (next != owner) {
    353 			pthread__mutex_wakeup(self, ptm);
    354 		}
    355 
    356 		/*
    357 		 * We must not proceed until told that we are no longer
    358 		 * waiting (via pt_mutexwait being set to zero).  Otherwise
    359 		 * it is unsafe to re-enter the thread onto the waiters
    360 		 * list.
    361 		 */
    362 		do {
    363 			pthread__assert(self->pt_nwaiters <= 1);
    364 			pthread__assert(self->pt_nwaiters != 0 ||
    365 			    self->pt_waiters[0] == 0);
    366 			error = _lwp_park(CLOCK_REALTIME, TIMER_ABSTIME,
    367 			    __UNCONST(ts), self->pt_waiters[0], NULL, NULL);
    368 			self->pt_waiters[0] = 0;
    369 			self->pt_nwaiters = 0;
    370 			if (error < 0 && errno == ETIMEDOUT) {
    371 				/* Remove self from waiters list */
    372 				pthread__mutex_wakeup(self, ptm);
    373 
    374 				/*
    375 				 * Might have raced with another thread to
    376 				 * do the wakeup.  In any case there will be
    377 				 * a wakeup for sure.  Eat it and wait for
    378 				 * pt_mutexwait to clear.
    379 				 */
    380 				do {
    381 					(void)_lwp_park(CLOCK_REALTIME,
    382 					   TIMER_ABSTIME, NULL, 0, NULL, NULL);
    383 				} while (self->pt_mutexwait);
    384 
    385 				/* Priority protect */
    386 				if (MUTEX_PROTECT(owner))
    387 					(void)_sched_protect(-1);
    388 				errno = serrno;
    389 				return ETIMEDOUT;
    390 			}
    391 		} while (self->pt_mutexwait);
    392 		owner = ptm->ptm_owner;
    393 	}
    394 }
    395 
    396 int
    397 pthread_mutex_trylock(pthread_mutex_t *ptm)
    398 {
    399 	pthread_t self;
    400 	void *val, *new, *next;
    401 
    402 	if (__predict_false(__uselibcstub))
    403 		return __libc_mutex_trylock_stub(ptm);
    404 
    405 	pthread__error(EINVAL, "Invalid mutex",
    406 	    ptm->ptm_magic == _PT_MUTEX_MAGIC);
    407 
    408 	self = pthread__self();
    409 	val = atomic_cas_ptr(&ptm->ptm_owner, NULL, self);
    410 	if (__predict_true(val == NULL)) {
    411 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    412 		membar_enter();
    413 #endif
    414 		return 0;
    415 	}
    416 
    417 	if (MUTEX_RECURSIVE(val)) {
    418 		if (MUTEX_OWNER(val) == 0) {
    419 			new = (void *)((uintptr_t)self | (uintptr_t)val);
    420 			next = atomic_cas_ptr(&ptm->ptm_owner, val, new);
    421 			if (__predict_true(next == val)) {
    422 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    423 				membar_enter();
    424 #endif
    425 				return 0;
    426 			}
    427 		}
    428 		if (MUTEX_OWNER(val) == (uintptr_t)self) {
    429 			if (ptm->ptm_recursed == INT_MAX)
    430 				return EAGAIN;
    431 			ptm->ptm_recursed++;
    432 			return 0;
    433 		}
    434 	}
    435 
    436 	return EBUSY;
    437 }
    438 
    439 int
    440 pthread_mutex_unlock(pthread_mutex_t *ptm)
    441 {
    442 	pthread_t self;
    443 	void *val;
    444 	int error;
    445 
    446 	if (__predict_false(__uselibcstub))
    447 		return __libc_mutex_unlock_stub(ptm);
    448 
    449 	pthread__error(EINVAL, "Invalid mutex",
    450 	    ptm->ptm_magic == _PT_MUTEX_MAGIC);
    451 
    452 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    453 	membar_exit();
    454 #endif
    455 	error = 0;
    456 	self = pthread__self();
    457 
    458 	val = atomic_cas_ptr(&ptm->ptm_owner, self, NULL);
    459 	if (__predict_false(val != self)) {
    460 		bool weown = (MUTEX_OWNER(val) == (uintptr_t)self);
    461 		void *newval = val;
    462 		if (__SIMPLELOCK_LOCKED_P(&ptm->ptm_errorcheck)) {
    463 			if (!weown) {
    464 				error = EPERM;
    465 				newval = val;
    466 			} else {
    467 				newval = NULL;
    468 			}
    469 		} else if (MUTEX_RECURSIVE(val)) {
    470 			if (!weown) {
    471 				error = EPERM;
    472 				newval = val;
    473 			} else if (ptm->ptm_recursed) {
    474 				ptm->ptm_recursed--;
    475 				newval = val;
    476 			} else {
    477 				newval = (pthread_t)MUTEX_RECURSIVE_BIT;
    478 			}
    479 		} else {
    480 			pthread__error(EPERM,
    481 			    "Unlocking unlocked mutex", (val != NULL));
    482 			pthread__error(EPERM,
    483 			    "Unlocking mutex owned by another thread", weown);
    484 			newval = NULL;
    485 		}
    486 
    487 		/*
    488 		 * Release the mutex.  If there appear to be waiters, then
    489 		 * wake them up.
    490 		 */
    491 		if (newval != val) {
    492 			val = atomic_swap_ptr(&ptm->ptm_owner, newval);
    493 			if (__predict_false(MUTEX_PROTECT(val))) {
    494 				/* restore elevated priority */
    495 				(void)_sched_protect(-1);
    496 			}
    497 		}
    498 	}
    499 
    500 	/*
    501 	 * Finally, wake any waiters and return.
    502 	 */
    503 #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    504 	membar_enter();
    505 #endif
    506 	if (MUTEX_HAS_WAITERS(val)) {
    507 		pthread__mutex_wakeup(self, ptm);
    508 	} else if (self->pt_nwaiters > 0) {
    509 		pthread__clear_waiters(self);
    510 	}
    511 	return error;
    512 }
    513 
    514 /*
    515  * pthread__mutex_wakeup: unpark threads waiting for us
    516  *
    517  * unpark threads on the ptm->ptm_waiters list and self->pt_waiters.
    518  */
    519 
    520 static void
    521 pthread__mutex_wakeup(pthread_t self, pthread_mutex_t *ptm)
    522 {
    523 	pthread_t thread, next;
    524 
    525 	/* Take ownership of the current set of waiters. */
    526 	thread = atomic_swap_ptr(&ptm->ptm_waiters, NULL);
    527 	membar_datadep_consumer(); /* for alpha */
    528 
    529 	/*
    530 	 * Pull waiters from the queue and add to our list.  Use a memory
    531 	 * barrier to ensure that we safely read the value of pt_mutexnext
    532 	 * before 'thread' sees pt_mutexwait being cleared.
    533 	 */
    534 	while (thread != NULL) {
    535 		if (self->pt_nwaiters >= pthread__unpark_max) {
    536 			pthread__clear_waiters(self);
    537 		}
    538 		next = thread->pt_mutexnext;
    539 		self->pt_waiters[self->pt_nwaiters++] = thread->pt_lid;
    540 		membar_sync();
    541 		thread->pt_mutexwait = 0;
    542 		/* No longer safe to touch 'thread' */
    543 		thread = next;
    544 	}
    545 	pthread__clear_waiters(self);
    546 }
    547 
    548 int
    549 pthread_mutexattr_init(pthread_mutexattr_t *attr)
    550 {
    551 #if 0
    552 	if (__predict_false(__uselibcstub))
    553 		return __libc_mutexattr_init_stub(attr);
    554 #endif
    555 
    556 	attr->ptma_magic = _PT_MUTEXATTR_MAGIC;
    557 	attr->ptma_private = (void *)PTHREAD_MUTEX_DEFAULT;
    558 	return 0;
    559 }
    560 
    561 int
    562 pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
    563 {
    564 	if (__predict_false(__uselibcstub))
    565 		return __libc_mutexattr_destroy_stub(attr);
    566 
    567 	pthread__error(EINVAL, "Invalid mutex attribute",
    568 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    569 
    570 	attr->ptma_magic = _PT_MUTEXATTR_DEAD;
    571 
    572 	return 0;
    573 }
    574 
    575 int
    576 pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *typep)
    577 {
    578 
    579 	pthread__error(EINVAL, "Invalid mutex attribute",
    580 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    581 
    582 	*typep = MUTEX_GET_TYPE(attr->ptma_private);
    583 	return 0;
    584 }
    585 
    586 int
    587 pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
    588 {
    589 
    590 	if (__predict_false(__uselibcstub))
    591 		return __libc_mutexattr_settype_stub(attr, type);
    592 
    593 	pthread__error(EINVAL, "Invalid mutex attribute",
    594 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    595 
    596 	switch (type) {
    597 	case PTHREAD_MUTEX_NORMAL:
    598 	case PTHREAD_MUTEX_ERRORCHECK:
    599 	case PTHREAD_MUTEX_RECURSIVE:
    600 		MUTEX_SET_TYPE(attr->ptma_private, type);
    601 		return 0;
    602 	default:
    603 		return EINVAL;
    604 	}
    605 }
    606 
    607 int
    608 pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr, int*proto)
    609 {
    610 
    611 	pthread__error(EINVAL, "Invalid mutex attribute",
    612 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    613 
    614 	*proto = MUTEX_GET_PROTOCOL(attr->ptma_private);
    615 	return 0;
    616 }
    617 
    618 int
    619 pthread_mutexattr_setprotocol(pthread_mutexattr_t* attr, int proto)
    620 {
    621 
    622 	pthread__error(EINVAL, "Invalid mutex attribute",
    623 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    624 
    625 	switch (proto) {
    626 	case PTHREAD_PRIO_NONE:
    627 	case PTHREAD_PRIO_PROTECT:
    628 		MUTEX_SET_PROTOCOL(attr->ptma_private, proto);
    629 		return 0;
    630 	case PTHREAD_PRIO_INHERIT:
    631 		return ENOTSUP;
    632 	default:
    633 		return EINVAL;
    634 	}
    635 }
    636 
    637 int
    638 pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *attr, int *ceil)
    639 {
    640 
    641 	pthread__error(EINVAL, "Invalid mutex attribute",
    642 		attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    643 
    644 	*ceil = MUTEX_GET_CEILING(attr->ptma_private);
    645 	return 0;
    646 }
    647 
    648 int
    649 pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr, int ceil)
    650 {
    651 
    652 	pthread__error(EINVAL, "Invalid mutex attribute",
    653 		attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    654 
    655 	if (ceil & ~0xff)
    656 		return EINVAL;
    657 
    658 	MUTEX_SET_CEILING(attr->ptma_private, ceil);
    659 	return 0;
    660 }
    661 
    662 #ifdef _PTHREAD_PSHARED
    663 int
    664 pthread_mutexattr_getpshared(const pthread_mutexattr_t * __restrict attr,
    665     int * __restrict pshared)
    666 {
    667 
    668 	pthread__error(EINVAL, "Invalid mutex attribute",
    669 		attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    670 
    671 	*pshared = PTHREAD_PROCESS_PRIVATE;
    672 	return 0;
    673 }
    674 
    675 int
    676 pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
    677 {
    678 
    679 	pthread__error(EINVAL, "Invalid mutex attribute",
    680 		attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    681 
    682 	switch(pshared) {
    683 	case PTHREAD_PROCESS_PRIVATE:
    684 		return 0;
    685 	case PTHREAD_PROCESS_SHARED:
    686 		return ENOSYS;
    687 	}
    688 	return EINVAL;
    689 }
    690 #endif
    691 
    692 /*
    693  * pthread__mutex_deferwake: try to defer unparking threads in self->pt_waiters
    694  *
    695  * In order to avoid unnecessary contention on interlocking mutexes, we try
    696  * to defer waking up threads until we unlock the mutex.  The threads will
    697  * be woken up when the calling thread (self) releases a mutex.
    698  */
    699 void
    700 pthread__mutex_deferwake(pthread_t self, pthread_mutex_t *ptm)
    701 {
    702 
    703 	if (__predict_false(ptm == NULL ||
    704 	    MUTEX_OWNER(ptm->ptm_owner) != (uintptr_t)self)) {
    705 		pthread__clear_waiters(self);
    706 	}
    707 }
    708 
    709 int
    710 pthread_mutex_getprioceiling(const pthread_mutex_t *ptm, int *ceil)
    711 {
    712 
    713 	pthread__error(EINVAL, "Invalid mutex",
    714 	    ptm->ptm_magic == _PT_MUTEX_MAGIC);
    715 
    716 	*ceil = ptm->ptm_ceiling;
    717 	return 0;
    718 }
    719 
    720 int
    721 pthread_mutex_setprioceiling(pthread_mutex_t *ptm, int ceil, int *old_ceil)
    722 {
    723 	int error;
    724 
    725 	pthread__error(EINVAL, "Invalid mutex",
    726 	    ptm->ptm_magic == _PT_MUTEX_MAGIC);
    727 
    728 	error = pthread_mutex_lock(ptm);
    729 	if (error == 0) {
    730 		*old_ceil = ptm->ptm_ceiling;
    731 		/*check range*/
    732 		ptm->ptm_ceiling = ceil;
    733 		pthread_mutex_unlock(ptm);
    734 	}
    735 	return error;
    736 }
    737 
    738 int
    739 _pthread_mutex_held_np(pthread_mutex_t *ptm)
    740 {
    741 
    742 	return MUTEX_OWNER(ptm->ptm_owner) == (uintptr_t)pthread__self();
    743 }
    744 
    745 pthread_t
    746 _pthread_mutex_owner_np(pthread_mutex_t *ptm)
    747 {
    748 
    749 	return (pthread_t)MUTEX_OWNER(ptm->ptm_owner);
    750 }
    751