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