Home | History | Annotate | Line # | Download | only in libpthread
pthread_mutex.c revision 1.55
      1  1.55     yamt /*	$NetBSD: pthread_mutex.c,v 1.55 2013/03/06 11:31:34 yamt Exp $	*/
      2   1.2  thorpej 
      3   1.2  thorpej /*-
      4  1.44       ad  * Copyright (c) 2001, 2003, 2006, 2007, 2008 The NetBSD Foundation, Inc.
      5   1.2  thorpej  * All rights reserved.
      6   1.2  thorpej  *
      7   1.2  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.27       ad  * by Nathan J. Williams, by Jason R. Thorpe, and by Andrew Doran.
      9   1.2  thorpej  *
     10   1.2  thorpej  * Redistribution and use in source and binary forms, with or without
     11   1.2  thorpej  * modification, are permitted provided that the following conditions
     12   1.2  thorpej  * are met:
     13   1.2  thorpej  * 1. Redistributions of source code must retain the above copyright
     14   1.2  thorpej  *    notice, this list of conditions and the following disclaimer.
     15   1.2  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.2  thorpej  *    notice, this list of conditions and the following disclaimer in the
     17   1.2  thorpej  *    documentation and/or other materials provided with the distribution.
     18   1.2  thorpej  *
     19   1.2  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.2  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.2  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.2  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.2  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.2  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.2  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.2  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.2  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.2  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.2  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     30   1.2  thorpej  */
     31   1.2  thorpej 
     32  1.49       ad /*
     33  1.49       ad  * To track threads waiting for mutexes to be released, we use lockless
     34  1.49       ad  * lists built on atomic operations and memory barriers.
     35  1.49       ad  *
     36  1.49       ad  * A simple spinlock would be faster and make the code easier to
     37  1.49       ad  * follow, but spinlocks are problematic in userspace.  If a thread is
     38  1.49       ad  * preempted by the kernel while holding a spinlock, any other thread
     39  1.49       ad  * attempting to acquire that spinlock will needlessly busy wait.
     40  1.49       ad  *
     41  1.49       ad  * There is no good way to know that the holding thread is no longer
     42  1.49       ad  * running, nor to request a wake-up once it has begun running again.
     43  1.49       ad  * Of more concern, threads in the SCHED_FIFO class do not have a
     44  1.49       ad  * limited time quantum and so could spin forever, preventing the
     45  1.49       ad  * thread holding the spinlock from getting CPU time: it would never
     46  1.49       ad  * be released.
     47  1.49       ad  */
     48  1.49       ad 
     49   1.2  thorpej #include <sys/cdefs.h>
     50  1.55     yamt __RCSID("$NetBSD: pthread_mutex.c,v 1.55 2013/03/06 11:31:34 yamt Exp $");
     51  1.40       ad 
     52  1.40       ad #include <sys/types.h>
     53  1.44       ad #include <sys/lwpctl.h>
     54  1.51     matt #include <sys/lock.h>
     55  1.10    lukem 
     56   1.2  thorpej #include <errno.h>
     57   1.2  thorpej #include <limits.h>
     58   1.2  thorpej #include <stdlib.h>
     59   1.6      scw #include <string.h>
     60  1.44       ad #include <stdio.h>
     61   1.2  thorpej 
     62   1.2  thorpej #include "pthread.h"
     63   1.2  thorpej #include "pthread_int.h"
     64   1.2  thorpej 
     65  1.44       ad #define	MUTEX_WAITERS_BIT		((uintptr_t)0x01)
     66  1.44       ad #define	MUTEX_RECURSIVE_BIT		((uintptr_t)0x02)
     67  1.44       ad #define	MUTEX_DEFERRED_BIT		((uintptr_t)0x04)
     68  1.44       ad #define	MUTEX_THREAD			((uintptr_t)-16L)
     69  1.44       ad 
     70  1.44       ad #define	MUTEX_HAS_WAITERS(x)		((uintptr_t)(x) & MUTEX_WAITERS_BIT)
     71  1.44       ad #define	MUTEX_RECURSIVE(x)		((uintptr_t)(x) & MUTEX_RECURSIVE_BIT)
     72  1.44       ad #define	MUTEX_OWNER(x)			((uintptr_t)(x) & MUTEX_THREAD)
     73  1.44       ad 
     74  1.44       ad #if __GNUC_PREREQ__(3, 0)
     75  1.44       ad #define	NOINLINE		__attribute ((noinline))
     76  1.44       ad #else
     77  1.44       ad #define	NOINLINE		/* nothing */
     78  1.44       ad #endif
     79  1.44       ad 
     80  1.44       ad static void	pthread__mutex_wakeup(pthread_t, pthread_mutex_t *);
     81  1.44       ad static int	pthread__mutex_lock_slow(pthread_mutex_t *);
     82  1.44       ad static int	pthread__mutex_unlock_slow(pthread_mutex_t *);
     83  1.44       ad static void	pthread__mutex_pause(void);
     84   1.2  thorpej 
     85  1.39       ad int		_pthread_mutex_held_np(pthread_mutex_t *);
     86  1.39       ad pthread_t	_pthread_mutex_owner_np(pthread_mutex_t *);
     87  1.39       ad 
     88  1.39       ad __weak_alias(pthread_mutex_held_np,_pthread_mutex_held_np)
     89  1.39       ad __weak_alias(pthread_mutex_owner_np,_pthread_mutex_owner_np)
     90  1.39       ad 
     91   1.2  thorpej __strong_alias(__libc_mutex_init,pthread_mutex_init)
     92   1.2  thorpej __strong_alias(__libc_mutex_lock,pthread_mutex_lock)
     93   1.2  thorpej __strong_alias(__libc_mutex_trylock,pthread_mutex_trylock)
     94   1.2  thorpej __strong_alias(__libc_mutex_unlock,pthread_mutex_unlock)
     95   1.2  thorpej __strong_alias(__libc_mutex_destroy,pthread_mutex_destroy)
     96   1.4  thorpej 
     97   1.4  thorpej __strong_alias(__libc_mutexattr_init,pthread_mutexattr_init)
     98   1.4  thorpej __strong_alias(__libc_mutexattr_destroy,pthread_mutexattr_destroy)
     99   1.5  thorpej __strong_alias(__libc_mutexattr_settype,pthread_mutexattr_settype)
    100   1.2  thorpej 
    101   1.2  thorpej int
    102  1.44       ad pthread_mutex_init(pthread_mutex_t *ptm, const pthread_mutexattr_t *attr)
    103   1.2  thorpej {
    104  1.44       ad 	intptr_t type;
    105   1.2  thorpej 
    106  1.44       ad 	if (attr == NULL)
    107  1.44       ad 		type = PTHREAD_MUTEX_NORMAL;
    108  1.44       ad 	else
    109  1.44       ad 		type = (intptr_t)attr->ptma_private;
    110   1.2  thorpej 
    111  1.44       ad 	switch (type) {
    112  1.44       ad 	case PTHREAD_MUTEX_ERRORCHECK:
    113  1.51     matt 		__cpu_simple_lock_set(&ptm->ptm_errorcheck);
    114  1.44       ad 		ptm->ptm_owner = NULL;
    115  1.44       ad 		break;
    116  1.44       ad 	case PTHREAD_MUTEX_RECURSIVE:
    117  1.51     matt 		__cpu_simple_lock_clear(&ptm->ptm_errorcheck);
    118  1.44       ad 		ptm->ptm_owner = (void *)MUTEX_RECURSIVE_BIT;
    119  1.44       ad 		break;
    120  1.44       ad 	default:
    121  1.51     matt 		__cpu_simple_lock_clear(&ptm->ptm_errorcheck);
    122  1.44       ad 		ptm->ptm_owner = NULL;
    123  1.44       ad 		break;
    124   1.2  thorpej 	}
    125   1.2  thorpej 
    126  1.44       ad 	ptm->ptm_magic = _PT_MUTEX_MAGIC;
    127  1.44       ad 	ptm->ptm_waiters = NULL;
    128  1.45       ad 	ptm->ptm_recursed = 0;
    129   1.2  thorpej 
    130   1.2  thorpej 	return 0;
    131   1.2  thorpej }
    132   1.2  thorpej 
    133   1.2  thorpej int
    134  1.44       ad pthread_mutex_destroy(pthread_mutex_t *ptm)
    135   1.2  thorpej {
    136   1.2  thorpej 
    137  1.14  nathanw 	pthread__error(EINVAL, "Invalid mutex",
    138  1.44       ad 	    ptm->ptm_magic == _PT_MUTEX_MAGIC);
    139  1.14  nathanw 	pthread__error(EBUSY, "Destroying locked mutex",
    140  1.44       ad 	    MUTEX_OWNER(ptm->ptm_owner) == 0);
    141   1.2  thorpej 
    142  1.44       ad 	ptm->ptm_magic = _PT_MUTEX_DEAD;
    143   1.2  thorpej 	return 0;
    144   1.2  thorpej }
    145   1.2  thorpej 
    146   1.2  thorpej int
    147  1.44       ad pthread_mutex_lock(pthread_mutex_t *ptm)
    148   1.2  thorpej {
    149  1.27       ad 	pthread_t self;
    150  1.44       ad 	void *val;
    151   1.2  thorpej 
    152  1.27       ad 	self = pthread__self();
    153  1.44       ad 	val = atomic_cas_ptr(&ptm->ptm_owner, NULL, self);
    154  1.44       ad 	if (__predict_true(val == NULL)) {
    155  1.44       ad #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    156  1.44       ad 		membar_enter();
    157  1.44       ad #endif
    158  1.44       ad 		return 0;
    159   1.2  thorpej 	}
    160  1.44       ad 	return pthread__mutex_lock_slow(ptm);
    161  1.44       ad }
    162   1.2  thorpej 
    163  1.44       ad /* We want function call overhead. */
    164  1.44       ad NOINLINE static void
    165  1.44       ad pthread__mutex_pause(void)
    166  1.44       ad {
    167   1.2  thorpej 
    168  1.44       ad 	pthread__smt_pause();
    169   1.2  thorpej }
    170   1.2  thorpej 
    171  1.44       ad /*
    172  1.44       ad  * Spin while the holder is running.  'lwpctl' gives us the true
    173  1.44       ad  * status of the thread.  pt_blocking is set by libpthread in order
    174  1.44       ad  * to cut out system call and kernel spinlock overhead on remote CPUs
    175  1.44       ad  * (could represent many thousands of clock cycles).  pt_blocking also
    176  1.44       ad  * makes this thread yield if the target is calling sched_yield().
    177  1.44       ad  */
    178  1.44       ad NOINLINE static void *
    179  1.44       ad pthread__mutex_spin(pthread_mutex_t *ptm, pthread_t owner)
    180  1.44       ad {
    181  1.44       ad 	pthread_t thread;
    182  1.44       ad 	unsigned int count, i;
    183  1.44       ad 
    184  1.44       ad 	for (count = 2;; owner = ptm->ptm_owner) {
    185  1.44       ad 		thread = (pthread_t)MUTEX_OWNER(owner);
    186  1.44       ad 		if (thread == NULL)
    187  1.44       ad 			break;
    188  1.44       ad 		if (thread->pt_lwpctl->lc_curcpu == LWPCTL_CPU_NONE ||
    189  1.44       ad 		    thread->pt_blocking)
    190  1.44       ad 			break;
    191  1.44       ad 		if (count < 128)
    192  1.44       ad 			count += count;
    193  1.44       ad 		for (i = count; i != 0; i--)
    194  1.44       ad 			pthread__mutex_pause();
    195  1.44       ad 	}
    196   1.2  thorpej 
    197  1.44       ad 	return owner;
    198  1.44       ad }
    199  1.44       ad 
    200  1.44       ad NOINLINE static int
    201  1.44       ad pthread__mutex_lock_slow(pthread_mutex_t *ptm)
    202   1.2  thorpej {
    203  1.44       ad 	void *waiters, *new, *owner, *next;
    204  1.44       ad 	pthread_t self;
    205   1.2  thorpej 
    206  1.14  nathanw 	pthread__error(EINVAL, "Invalid mutex",
    207  1.44       ad 	    ptm->ptm_magic == _PT_MUTEX_MAGIC);
    208  1.44       ad 
    209  1.44       ad 	owner = ptm->ptm_owner;
    210  1.44       ad 	self = pthread__self();
    211  1.13  nathanw 
    212  1.44       ad 	/* Recursive or errorcheck? */
    213  1.44       ad 	if (MUTEX_OWNER(owner) == (uintptr_t)self) {
    214  1.44       ad 		if (MUTEX_RECURSIVE(owner)) {
    215  1.45       ad 			if (ptm->ptm_recursed == INT_MAX)
    216  1.44       ad 				return EAGAIN;
    217  1.45       ad 			ptm->ptm_recursed++;
    218  1.44       ad 			return 0;
    219  1.29       ad 		}
    220  1.51     matt 		if (__SIMPLELOCK_LOCKED_P(&ptm->ptm_errorcheck))
    221  1.44       ad 			return EDEADLK;
    222  1.44       ad 	}
    223  1.29       ad 
    224  1.44       ad 	for (;; owner = ptm->ptm_owner) {
    225  1.44       ad 		/* Spin while the owner is running. */
    226  1.44       ad 		owner = pthread__mutex_spin(ptm, owner);
    227  1.44       ad 
    228  1.44       ad 		/* If it has become free, try to acquire it again. */
    229  1.44       ad 		if (MUTEX_OWNER(owner) == 0) {
    230  1.47       ad 			do {
    231  1.44       ad 				new = (void *)
    232  1.44       ad 				    ((uintptr_t)self | (uintptr_t)owner);
    233  1.44       ad 				next = atomic_cas_ptr(&ptm->ptm_owner, owner,
    234  1.44       ad 				    new);
    235  1.44       ad 				if (next == owner) {
    236  1.44       ad #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    237  1.44       ad 					membar_enter();
    238  1.44       ad #endif
    239  1.44       ad 					return 0;
    240  1.44       ad 				}
    241  1.47       ad 				owner = next;
    242  1.47       ad 			} while (MUTEX_OWNER(owner) == 0);
    243  1.44       ad 			/*
    244  1.44       ad 			 * We have lost the race to acquire the mutex.
    245  1.44       ad 			 * The new owner could be running on another
    246  1.44       ad 			 * CPU, in which case we should spin and avoid
    247  1.44       ad 			 * the overhead of blocking.
    248  1.44       ad 			 */
    249  1.47       ad 			continue;
    250  1.44       ad 		}
    251  1.21      chs 
    252   1.2  thorpej 		/*
    253  1.44       ad 		 * Nope, still held.  Add thread to the list of waiters.
    254  1.50       ad 		 * Issue a memory barrier to ensure mutexwait/mutexnext
    255  1.44       ad 		 * are visible before we enter the waiters list.
    256   1.2  thorpej 		 */
    257  1.50       ad 		self->pt_mutexwait = 1;
    258  1.44       ad 		for (waiters = ptm->ptm_waiters;; waiters = next) {
    259  1.50       ad 			self->pt_mutexnext = waiters;
    260  1.44       ad 			membar_producer();
    261  1.44       ad 			next = atomic_cas_ptr(&ptm->ptm_waiters, waiters, self);
    262  1.44       ad 			if (next == waiters)
    263  1.44       ad 			    	break;
    264  1.44       ad 		}
    265  1.21      chs 
    266  1.44       ad 		/*
    267  1.44       ad 		 * Set the waiters bit and block.
    268  1.44       ad 		 *
    269  1.44       ad 		 * Note that the mutex can become unlocked before we set
    270  1.44       ad 		 * the waiters bit.  If that happens it's not safe to sleep
    271  1.44       ad 		 * as we may never be awoken: we must remove the current
    272  1.44       ad 		 * thread from the waiters list and try again.
    273  1.44       ad 		 *
    274  1.44       ad 		 * Because we are doing this atomically, we can't remove
    275  1.44       ad 		 * one waiter: we must remove all waiters and awken them,
    276  1.44       ad 		 * then sleep in _lwp_park() until we have been awoken.
    277  1.44       ad 		 *
    278  1.44       ad 		 * Issue a memory barrier to ensure that we are reading
    279  1.50       ad 		 * the value of ptm_owner/pt_mutexwait after we have entered
    280  1.44       ad 		 * the waiters list (the CAS itself must be atomic).
    281  1.44       ad 		 */
    282  1.44       ad 		membar_consumer();
    283  1.44       ad 		for (owner = ptm->ptm_owner;; owner = next) {
    284  1.44       ad 			if (MUTEX_HAS_WAITERS(owner))
    285  1.44       ad 				break;
    286  1.44       ad 			if (MUTEX_OWNER(owner) == 0) {
    287  1.44       ad 				pthread__mutex_wakeup(self, ptm);
    288  1.44       ad 				break;
    289  1.44       ad 			}
    290  1.44       ad 			new = (void *)((uintptr_t)owner | MUTEX_WAITERS_BIT);
    291  1.44       ad 			next = atomic_cas_ptr(&ptm->ptm_owner, owner, new);
    292  1.44       ad 			if (next == owner) {
    293  1.21      chs 				/*
    294  1.44       ad 				 * pthread_mutex_unlock() can do a
    295  1.44       ad 				 * non-interlocked CAS.  We cannot
    296  1.44       ad 				 * know if our attempt to set the
    297  1.44       ad 				 * waiters bit has succeeded while
    298  1.44       ad 				 * the holding thread is running.
    299  1.44       ad 				 * There are many assumptions; see
    300  1.44       ad 				 * sys/kern/kern_mutex.c for details.
    301  1.44       ad 				 * In short, we must spin if we see
    302  1.44       ad 				 * that the holder is running again.
    303  1.21      chs 				 */
    304  1.44       ad 				membar_sync();
    305  1.44       ad 				next = pthread__mutex_spin(ptm, owner);
    306  1.21      chs 			}
    307  1.29       ad 		}
    308  1.21      chs 
    309  1.29       ad 		/*
    310  1.44       ad 		 * We may have been awoken by the current thread above,
    311  1.44       ad 		 * or will be awoken by the current holder of the mutex.
    312  1.44       ad 		 * The key requirement is that we must not proceed until
    313  1.50       ad 		 * told that we are no longer waiting (via pt_mutexwait
    314  1.44       ad 		 * being set to zero).  Otherwise it is unsafe to re-enter
    315  1.44       ad 		 * the thread onto the waiters list.
    316  1.29       ad 		 */
    317  1.50       ad 		while (self->pt_mutexwait) {
    318  1.44       ad 			self->pt_blocking++;
    319  1.50       ad 			(void)_lwp_park(NULL, self->pt_unpark,
    320  1.50       ad 			    __UNVOLATILE(&ptm->ptm_waiters),
    321  1.50       ad 			    __UNVOLATILE(&ptm->ptm_waiters));
    322  1.50       ad 			self->pt_unpark = 0;
    323  1.44       ad 			self->pt_blocking--;
    324  1.44       ad 			membar_sync();
    325  1.44       ad 		}
    326   1.2  thorpej 	}
    327   1.2  thorpej }
    328   1.2  thorpej 
    329   1.2  thorpej int
    330  1.44       ad pthread_mutex_trylock(pthread_mutex_t *ptm)
    331   1.2  thorpej {
    332  1.27       ad 	pthread_t self;
    333  1.46       ad 	void *val, *new, *next;
    334   1.2  thorpej 
    335  1.27       ad 	self = pthread__self();
    336  1.44       ad 	val = atomic_cas_ptr(&ptm->ptm_owner, NULL, self);
    337  1.44       ad 	if (__predict_true(val == NULL)) {
    338  1.44       ad #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    339  1.44       ad 		membar_enter();
    340  1.44       ad #endif
    341  1.44       ad 		return 0;
    342  1.44       ad 	}
    343  1.27       ad 
    344  1.46       ad 	if (MUTEX_RECURSIVE(val)) {
    345  1.46       ad 		if (MUTEX_OWNER(val) == 0) {
    346  1.46       ad 			new = (void *)((uintptr_t)self | (uintptr_t)val);
    347  1.46       ad 			next = atomic_cas_ptr(&ptm->ptm_owner, val, new);
    348  1.46       ad 			if (__predict_true(next == val)) {
    349  1.46       ad #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    350  1.46       ad 				membar_enter();
    351  1.46       ad #endif
    352  1.46       ad 				return 0;
    353  1.46       ad 			}
    354  1.46       ad 		}
    355  1.46       ad 		if (MUTEX_OWNER(val) == (uintptr_t)self) {
    356  1.46       ad 			if (ptm->ptm_recursed == INT_MAX)
    357  1.46       ad 				return EAGAIN;
    358  1.46       ad 			ptm->ptm_recursed++;
    359  1.46       ad 			return 0;
    360  1.46       ad 		}
    361   1.2  thorpej 	}
    362   1.2  thorpej 
    363  1.44       ad 	return EBUSY;
    364   1.2  thorpej }
    365   1.2  thorpej 
    366   1.2  thorpej int
    367  1.44       ad pthread_mutex_unlock(pthread_mutex_t *ptm)
    368   1.2  thorpej {
    369  1.27       ad 	pthread_t self;
    370  1.44       ad 	void *value;
    371  1.44       ad 
    372  1.44       ad 	/*
    373  1.44       ad 	 * Note this may be a non-interlocked CAS.  See lock_slow()
    374  1.44       ad 	 * above and sys/kern/kern_mutex.c for details.
    375  1.44       ad 	 */
    376  1.44       ad #ifndef PTHREAD__ATOMIC_IS_MEMBAR
    377  1.44       ad 	membar_exit();
    378  1.44       ad #endif
    379  1.44       ad 	self = pthread__self();
    380  1.44       ad 	value = atomic_cas_ptr_ni(&ptm->ptm_owner, self, NULL);
    381  1.54     matt 	if (__predict_true(value == self)) {
    382  1.54     matt 		pthread__smt_wake();
    383  1.44       ad 		return 0;
    384  1.54     matt 	}
    385  1.44       ad 	return pthread__mutex_unlock_slow(ptm);
    386  1.44       ad }
    387  1.44       ad 
    388  1.44       ad NOINLINE static int
    389  1.44       ad pthread__mutex_unlock_slow(pthread_mutex_t *ptm)
    390  1.44       ad {
    391  1.44       ad 	pthread_t self, owner, new;
    392  1.44       ad 	int weown, error, deferred;
    393  1.13  nathanw 
    394  1.14  nathanw 	pthread__error(EINVAL, "Invalid mutex",
    395  1.44       ad 	    ptm->ptm_magic == _PT_MUTEX_MAGIC);
    396  1.44       ad 
    397  1.44       ad 	self = pthread__self();
    398  1.44       ad 	owner = ptm->ptm_owner;
    399  1.44       ad 	weown = (MUTEX_OWNER(owner) == (uintptr_t)self);
    400  1.44       ad 	deferred = (int)((uintptr_t)owner & MUTEX_DEFERRED_BIT);
    401  1.44       ad 	error = 0;
    402  1.44       ad 
    403  1.51     matt 	if (__SIMPLELOCK_LOCKED_P(&ptm->ptm_errorcheck)) {
    404  1.44       ad 		if (!weown) {
    405  1.44       ad 			error = EPERM;
    406  1.44       ad 			new = owner;
    407  1.44       ad 		} else {
    408  1.44       ad 			new = NULL;
    409  1.44       ad 		}
    410  1.44       ad 	} else if (MUTEX_RECURSIVE(owner)) {
    411  1.44       ad 		if (!weown) {
    412  1.44       ad 			error = EPERM;
    413  1.44       ad 			new = owner;
    414  1.45       ad 		} else if (ptm->ptm_recursed) {
    415  1.45       ad 			ptm->ptm_recursed--;
    416  1.44       ad 			new = owner;
    417  1.44       ad 		} else {
    418  1.44       ad 			new = (pthread_t)MUTEX_RECURSIVE_BIT;
    419  1.44       ad 		}
    420  1.44       ad 	} else {
    421  1.44       ad 		pthread__error(EPERM,
    422  1.44       ad 		    "Unlocking unlocked mutex", (owner != NULL));
    423  1.44       ad 		pthread__error(EPERM,
    424  1.44       ad 		    "Unlocking mutex owned by another thread", weown);
    425  1.44       ad 		new = NULL;
    426  1.44       ad 	}
    427   1.2  thorpej 
    428   1.2  thorpej 	/*
    429  1.44       ad 	 * Release the mutex.  If there appear to be waiters, then
    430  1.44       ad 	 * wake them up.
    431   1.2  thorpej 	 */
    432  1.44       ad 	if (new != owner) {
    433  1.44       ad 		owner = atomic_swap_ptr(&ptm->ptm_owner, new);
    434  1.44       ad 		if (MUTEX_HAS_WAITERS(owner) != 0) {
    435  1.44       ad 			pthread__mutex_wakeup(self, ptm);
    436   1.2  thorpej 			return 0;
    437   1.2  thorpej 		}
    438  1.44       ad 	}
    439  1.44       ad 
    440  1.44       ad 	/*
    441  1.44       ad 	 * There were no waiters, but we may have deferred waking
    442  1.44       ad 	 * other threads until mutex unlock - we must wake them now.
    443  1.44       ad 	 */
    444  1.44       ad 	if (!deferred)
    445  1.44       ad 		return error;
    446  1.44       ad 
    447  1.44       ad 	if (self->pt_nwaiters == 1) {
    448  1.44       ad 		/*
    449  1.44       ad 		 * If the calling thread is about to block, defer
    450  1.44       ad 		 * unparking the target until _lwp_park() is called.
    451  1.44       ad 		 */
    452  1.44       ad 		if (self->pt_willpark && self->pt_unpark == 0) {
    453  1.44       ad 			self->pt_unpark = self->pt_waiters[0];
    454  1.44       ad 		} else {
    455  1.44       ad 			(void)_lwp_unpark(self->pt_waiters[0],
    456  1.45       ad 			    __UNVOLATILE(&ptm->ptm_waiters));
    457  1.15  nathanw 		}
    458  1.44       ad 	} else {
    459  1.44       ad 		(void)_lwp_unpark_all(self->pt_waiters, self->pt_nwaiters,
    460  1.45       ad 		    __UNVOLATILE(&ptm->ptm_waiters));
    461   1.2  thorpej 	}
    462  1.44       ad 	self->pt_nwaiters = 0;
    463   1.2  thorpej 
    464  1.44       ad 	return error;
    465  1.44       ad }
    466  1.44       ad 
    467  1.55     yamt /*
    468  1.55     yamt  * pthread__mutex_wakeup: unpark threads waiting for us
    469  1.55     yamt  *
    470  1.55     yamt  * unpark threads on the ptm->ptm_waiters list and self->pt_waiters.
    471  1.55     yamt  */
    472  1.55     yamt 
    473  1.44       ad static void
    474  1.44       ad pthread__mutex_wakeup(pthread_t self, pthread_mutex_t *ptm)
    475  1.44       ad {
    476  1.44       ad 	pthread_t thread, next;
    477  1.44       ad 	ssize_t n, rv;
    478  1.27       ad 
    479   1.8  nathanw 	/*
    480  1.44       ad 	 * Take ownership of the current set of waiters.  No
    481  1.44       ad 	 * need for a memory barrier following this, all loads
    482  1.44       ad 	 * are dependent upon 'thread'.
    483   1.8  nathanw 	 */
    484  1.44       ad 	thread = atomic_swap_ptr(&ptm->ptm_waiters, NULL);
    485  1.54     matt 	pthread__smt_wake();
    486  1.44       ad 
    487  1.44       ad 	for (;;) {
    488  1.44       ad 		/*
    489  1.44       ad 		 * Pull waiters from the queue and add to our list.
    490  1.44       ad 		 * Use a memory barrier to ensure that we safely
    491  1.50       ad 		 * read the value of pt_mutexnext before 'thread'
    492  1.50       ad 		 * sees pt_mutexwait being cleared.
    493  1.44       ad 		 */
    494  1.44       ad 		for (n = self->pt_nwaiters, self->pt_nwaiters = 0;
    495  1.44       ad 		    n < pthread__unpark_max && thread != NULL;
    496  1.44       ad 		    thread = next) {
    497  1.50       ad 		    	next = thread->pt_mutexnext;
    498  1.44       ad 		    	if (thread != self) {
    499  1.44       ad 				self->pt_waiters[n++] = thread->pt_lid;
    500  1.44       ad 				membar_sync();
    501  1.44       ad 			}
    502  1.50       ad 			thread->pt_mutexwait = 0;
    503  1.44       ad 			/* No longer safe to touch 'thread' */
    504  1.44       ad 		}
    505  1.44       ad 
    506  1.44       ad 		switch (n) {
    507  1.44       ad 		case 0:
    508  1.44       ad 			return;
    509  1.44       ad 		case 1:
    510  1.44       ad 			/*
    511  1.44       ad 			 * If the calling thread is about to block,
    512  1.44       ad 			 * defer unparking the target until _lwp_park()
    513  1.44       ad 			 * is called.
    514  1.44       ad 			 */
    515  1.44       ad 			if (self->pt_willpark && self->pt_unpark == 0) {
    516  1.44       ad 				self->pt_unpark = self->pt_waiters[0];
    517  1.44       ad 				return;
    518  1.44       ad 			}
    519  1.44       ad 			rv = (ssize_t)_lwp_unpark(self->pt_waiters[0],
    520  1.45       ad 			    __UNVOLATILE(&ptm->ptm_waiters));
    521  1.44       ad 			if (rv != 0 && errno != EALREADY && errno != EINTR &&
    522  1.44       ad 			    errno != ESRCH) {
    523  1.44       ad 				pthread__errorfunc(__FILE__, __LINE__,
    524  1.44       ad 				    __func__, "_lwp_unpark failed");
    525  1.44       ad 			}
    526  1.44       ad 			return;
    527  1.44       ad 		default:
    528  1.44       ad 			rv = _lwp_unpark_all(self->pt_waiters, (size_t)n,
    529  1.45       ad 			    __UNVOLATILE(&ptm->ptm_waiters));
    530  1.44       ad 			if (rv != 0 && errno != EINTR) {
    531  1.44       ad 				pthread__errorfunc(__FILE__, __LINE__,
    532  1.44       ad 				    __func__, "_lwp_unpark_all failed");
    533  1.44       ad 			}
    534  1.44       ad 			break;
    535  1.44       ad 		}
    536  1.44       ad 	}
    537   1.2  thorpej }
    538  1.55     yamt 
    539   1.2  thorpej int
    540   1.2  thorpej pthread_mutexattr_init(pthread_mutexattr_t *attr)
    541   1.2  thorpej {
    542   1.2  thorpej 
    543   1.2  thorpej 	attr->ptma_magic = _PT_MUTEXATTR_MAGIC;
    544  1.44       ad 	attr->ptma_private = (void *)PTHREAD_MUTEX_DEFAULT;
    545   1.2  thorpej 	return 0;
    546   1.2  thorpej }
    547   1.2  thorpej 
    548   1.2  thorpej int
    549   1.2  thorpej pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
    550   1.2  thorpej {
    551   1.2  thorpej 
    552  1.14  nathanw 	pthread__error(EINVAL, "Invalid mutex attribute",
    553  1.14  nathanw 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    554   1.2  thorpej 
    555   1.2  thorpej 	return 0;
    556   1.2  thorpej }
    557   1.2  thorpej 
    558   1.2  thorpej int
    559   1.2  thorpej pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *typep)
    560   1.2  thorpej {
    561   1.2  thorpej 
    562  1.14  nathanw 	pthread__error(EINVAL, "Invalid mutex attribute",
    563  1.14  nathanw 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    564   1.2  thorpej 
    565  1.44       ad 	*typep = (int)(intptr_t)attr->ptma_private;
    566   1.2  thorpej 	return 0;
    567   1.2  thorpej }
    568   1.2  thorpej 
    569   1.2  thorpej int
    570   1.2  thorpej pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
    571   1.2  thorpej {
    572   1.2  thorpej 
    573  1.14  nathanw 	pthread__error(EINVAL, "Invalid mutex attribute",
    574  1.14  nathanw 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    575  1.13  nathanw 
    576   1.2  thorpej 	switch (type) {
    577   1.2  thorpej 	case PTHREAD_MUTEX_NORMAL:
    578   1.2  thorpej 	case PTHREAD_MUTEX_ERRORCHECK:
    579   1.2  thorpej 	case PTHREAD_MUTEX_RECURSIVE:
    580  1.44       ad 		attr->ptma_private = (void *)(intptr_t)type;
    581  1.44       ad 		return 0;
    582   1.2  thorpej 	default:
    583   1.2  thorpej 		return EINVAL;
    584   1.2  thorpej 	}
    585   1.2  thorpej }
    586   1.2  thorpej 
    587  1.55     yamt /*
    588  1.55     yamt  * pthread__mutex_deferwake: try to defer unparking threads in self->pt_waiters
    589  1.55     yamt  *
    590  1.55     yamt  * In order to avoid unnecessary contention on the interlocking mutex,
    591  1.55     yamt  * we defer waking up threads until we unlock the mutex.  The threads will
    592  1.55     yamt  * be woken up when the calling thread (self) releases the first mutex with
    593  1.55     yamt  * MUTEX_DEFERRED_BIT set.  It likely be the mutex 'ptm', but no problem
    594  1.55     yamt  * even if it isn't.
    595  1.55     yamt  */
    596  1.55     yamt 
    597  1.50       ad void
    598  1.50       ad pthread__mutex_deferwake(pthread_t self, pthread_mutex_t *ptm)
    599  1.33       ad {
    600  1.33       ad 
    601  1.50       ad 	if (__predict_false(ptm == NULL ||
    602  1.50       ad 	    MUTEX_OWNER(ptm->ptm_owner) != (uintptr_t)self)) {
    603  1.50       ad 	    	(void)_lwp_unpark_all(self->pt_waiters, self->pt_nwaiters,
    604  1.50       ad 	    	    __UNVOLATILE(&ptm->ptm_waiters));
    605  1.50       ad 	    	self->pt_nwaiters = 0;
    606  1.50       ad 	} else {
    607  1.50       ad 		atomic_or_ulong((volatile unsigned long *)
    608  1.50       ad 		    (uintptr_t)&ptm->ptm_owner,
    609  1.50       ad 		    (unsigned long)MUTEX_DEFERRED_BIT);
    610  1.50       ad 	}
    611  1.33       ad }
    612  1.33       ad 
    613  1.39       ad int
    614  1.44       ad _pthread_mutex_held_np(pthread_mutex_t *ptm)
    615  1.39       ad {
    616  1.39       ad 
    617  1.44       ad 	return MUTEX_OWNER(ptm->ptm_owner) == (uintptr_t)pthread__self();
    618  1.39       ad }
    619  1.39       ad 
    620  1.39       ad pthread_t
    621  1.44       ad _pthread_mutex_owner_np(pthread_mutex_t *ptm)
    622  1.39       ad {
    623  1.39       ad 
    624  1.44       ad 	return (pthread_t)MUTEX_OWNER(ptm->ptm_owner);
    625  1.39       ad }
    626