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