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