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