Home | History | Annotate | Line # | Download | only in libpthread
pthread_mutex.c revision 1.39
      1  1.39        ad /*	$NetBSD: pthread_mutex.c,v 1.39 2007/12/24 14:46:29 ad Exp $	*/
      2   1.2   thorpej 
      3   1.2   thorpej /*-
      4  1.25        ad  * Copyright (c) 2001, 2003, 2006, 2007 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.39        ad __RCSID("$NetBSD: pthread_mutex.c,v 1.39 2007/12/24 14:46:29 ad Exp $");
     41  1.10     lukem 
     42   1.2   thorpej #include <errno.h>
     43   1.2   thorpej #include <limits.h>
     44   1.2   thorpej #include <stdlib.h>
     45   1.6       scw #include <string.h>
     46   1.2   thorpej 
     47  1.34     skrll #include <sys/types.h>
     48  1.34     skrll #include <sys/lock.h>
     49  1.34     skrll 
     50   1.2   thorpej #include "pthread.h"
     51   1.2   thorpej #include "pthread_int.h"
     52   1.2   thorpej 
     53  1.32        ad #ifndef	PTHREAD__HAVE_ATOMIC
     54  1.32        ad 
     55  1.27        ad static int pthread_mutex_lock_slow(pthread_t, pthread_mutex_t *);
     56   1.2   thorpej 
     57  1.39        ad int		_pthread_mutex_held_np(pthread_mutex_t *);
     58  1.39        ad pthread_t	_pthread_mutex_owner_np(pthread_mutex_t *);
     59  1.39        ad 
     60  1.39        ad __weak_alias(pthread_mutex_held_np,_pthread_mutex_held_np)
     61  1.39        ad __weak_alias(pthread_mutex_owner_np,_pthread_mutex_owner_np)
     62  1.39        ad 
     63   1.2   thorpej __strong_alias(__libc_mutex_init,pthread_mutex_init)
     64   1.2   thorpej __strong_alias(__libc_mutex_lock,pthread_mutex_lock)
     65   1.2   thorpej __strong_alias(__libc_mutex_trylock,pthread_mutex_trylock)
     66   1.2   thorpej __strong_alias(__libc_mutex_unlock,pthread_mutex_unlock)
     67   1.2   thorpej __strong_alias(__libc_mutex_destroy,pthread_mutex_destroy)
     68   1.4   thorpej 
     69   1.4   thorpej __strong_alias(__libc_mutexattr_init,pthread_mutexattr_init)
     70   1.4   thorpej __strong_alias(__libc_mutexattr_destroy,pthread_mutexattr_destroy)
     71   1.5   thorpej __strong_alias(__libc_mutexattr_settype,pthread_mutexattr_settype)
     72   1.2   thorpej 
     73   1.2   thorpej __strong_alias(__libc_thr_once,pthread_once)
     74   1.2   thorpej 
     75   1.2   thorpej struct mutex_private {
     76   1.2   thorpej 	int	type;
     77   1.2   thorpej 	int	recursecount;
     78   1.2   thorpej };
     79   1.2   thorpej 
     80   1.2   thorpej static const struct mutex_private mutex_private_default = {
     81   1.2   thorpej 	PTHREAD_MUTEX_DEFAULT,
     82   1.2   thorpej 	0,
     83   1.2   thorpej };
     84   1.2   thorpej 
     85   1.2   thorpej struct mutexattr_private {
     86   1.2   thorpej 	int	type;
     87   1.2   thorpej };
     88   1.2   thorpej 
     89   1.2   thorpej static const struct mutexattr_private mutexattr_private_default = {
     90   1.2   thorpej 	PTHREAD_MUTEX_DEFAULT,
     91   1.2   thorpej };
     92   1.2   thorpej 
     93   1.2   thorpej int
     94   1.2   thorpej pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
     95   1.2   thorpej {
     96   1.2   thorpej 	struct mutexattr_private *map;
     97   1.2   thorpej 	struct mutex_private *mp;
     98   1.2   thorpej 
     99  1.14   nathanw 	pthread__error(EINVAL, "Invalid mutex attribute",
    100  1.14   nathanw 	    (attr == NULL) || (attr->ptma_magic == _PT_MUTEXATTR_MAGIC));
    101   1.2   thorpej 
    102   1.2   thorpej 	if (attr != NULL && (map = attr->ptma_private) != NULL &&
    103   1.2   thorpej 	    memcmp(map, &mutexattr_private_default, sizeof(*map)) != 0) {
    104   1.2   thorpej 		mp = malloc(sizeof(*mp));
    105   1.2   thorpej 		if (mp == NULL)
    106   1.2   thorpej 			return ENOMEM;
    107   1.2   thorpej 
    108   1.2   thorpej 		mp->type = map->type;
    109   1.2   thorpej 		mp->recursecount = 0;
    110   1.2   thorpej 	} else {
    111   1.2   thorpej 		/* LINTED cast away const */
    112   1.2   thorpej 		mp = (struct mutex_private *) &mutex_private_default;
    113   1.2   thorpej 	}
    114   1.2   thorpej 
    115   1.2   thorpej 	mutex->ptm_magic = _PT_MUTEX_MAGIC;
    116   1.2   thorpej 	mutex->ptm_owner = NULL;
    117   1.2   thorpej 	pthread_lockinit(&mutex->ptm_lock);
    118   1.2   thorpej 	pthread_lockinit(&mutex->ptm_interlock);
    119   1.2   thorpej 	PTQ_INIT(&mutex->ptm_blocked);
    120   1.2   thorpej 	mutex->ptm_private = mp;
    121   1.2   thorpej 
    122   1.2   thorpej 	return 0;
    123   1.2   thorpej }
    124   1.2   thorpej 
    125   1.2   thorpej 
    126   1.2   thorpej int
    127   1.2   thorpej pthread_mutex_destroy(pthread_mutex_t *mutex)
    128   1.2   thorpej {
    129   1.2   thorpej 
    130  1.14   nathanw 	pthread__error(EINVAL, "Invalid mutex",
    131  1.14   nathanw 	    mutex->ptm_magic == _PT_MUTEX_MAGIC);
    132  1.14   nathanw 	pthread__error(EBUSY, "Destroying locked mutex",
    133  1.34     skrll 	    __SIMPLELOCK_UNLOCKED_P(&mutex->ptm_lock));
    134   1.2   thorpej 
    135   1.2   thorpej 	mutex->ptm_magic = _PT_MUTEX_DEAD;
    136   1.2   thorpej 	if (mutex->ptm_private != NULL &&
    137   1.3  christos 	    mutex->ptm_private != (const void *)&mutex_private_default)
    138   1.2   thorpej 		free(mutex->ptm_private);
    139   1.2   thorpej 
    140   1.2   thorpej 	return 0;
    141   1.2   thorpej }
    142   1.2   thorpej 
    143   1.2   thorpej 
    144   1.2   thorpej /*
    145   1.2   thorpej  * Note regarding memory visibility: Pthreads has rules about memory
    146   1.2   thorpej  * visibility and mutexes. Very roughly: Memory a thread can see when
    147   1.2   thorpej  * it unlocks a mutex can be seen by another thread that locks the
    148   1.2   thorpej  * same mutex.
    149   1.2   thorpej  *
    150   1.2   thorpej  * A memory barrier after a lock and before an unlock will provide
    151  1.37        ad  * this behavior. This code relies on pthread__spintrylock() to issue
    152  1.37        ad  * a barrier after obtaining a lock, and on pthread__spinunlock() to
    153   1.2   thorpej  * issue a barrier before releasing a lock.
    154   1.2   thorpej  */
    155   1.2   thorpej 
    156   1.2   thorpej int
    157   1.2   thorpej pthread_mutex_lock(pthread_mutex_t *mutex)
    158   1.2   thorpej {
    159  1.27        ad 	pthread_t self;
    160   1.2   thorpej 	int error;
    161   1.2   thorpej 
    162  1.27        ad 	self = pthread__self();
    163  1.27        ad 
    164   1.2   thorpej 	/*
    165   1.2   thorpej 	 * Note that if we get the lock, we don't have to deal with any
    166   1.2   thorpej 	 * non-default lock type handling.
    167   1.2   thorpej 	 */
    168  1.37        ad 	if (__predict_false(pthread__spintrylock(self, &mutex->ptm_lock) == 0)) {
    169  1.27        ad 		error = pthread_mutex_lock_slow(self, mutex);
    170   1.2   thorpej 		if (error)
    171   1.2   thorpej 			return error;
    172   1.2   thorpej 	}
    173   1.2   thorpej 
    174   1.8   nathanw 	/*
    175  1.27        ad 	 * We have the lock!
    176   1.8   nathanw 	 */
    177  1.27        ad 	mutex->ptm_owner = self;
    178   1.2   thorpej 
    179   1.2   thorpej 	return 0;
    180   1.2   thorpej }
    181   1.2   thorpej 
    182   1.2   thorpej 
    183   1.2   thorpej static int
    184  1.27        ad pthread_mutex_lock_slow(pthread_t self, pthread_mutex_t *mutex)
    185   1.2   thorpej {
    186  1.20       chs 	extern int pthread__started;
    187  1.29        ad 	struct mutex_private *mp;
    188  1.29        ad 	sigset_t ss;
    189  1.29        ad 	int count;
    190   1.2   thorpej 
    191  1.14   nathanw 	pthread__error(EINVAL, "Invalid mutex",
    192  1.14   nathanw 	    mutex->ptm_magic == _PT_MUTEX_MAGIC);
    193  1.13   nathanw 
    194  1.29        ad 	for (;;) {
    195  1.29        ad 		/* Spin for a while. */
    196  1.29        ad 		count = pthread__nspins;
    197  1.34     skrll 		while (__SIMPLELOCK_LOCKED_P(&mutex->ptm_lock)  && --count > 0)
    198  1.29        ad 			pthread__smt_pause();
    199  1.29        ad 		if (count > 0) {
    200  1.37        ad 			if (pthread__spintrylock(self, &mutex->ptm_lock) != 0)
    201  1.29        ad 				break;
    202  1.29        ad 			continue;
    203  1.29        ad 		}
    204  1.29        ad 
    205   1.2   thorpej 		/* Okay, didn't look free. Get the interlock... */
    206  1.37        ad 		pthread__spinlock(self, &mutex->ptm_interlock);
    207  1.21       chs 
    208   1.2   thorpej 		/*
    209   1.2   thorpej 		 * The mutex_unlock routine will get the interlock
    210   1.2   thorpej 		 * before looking at the list of sleepers, so if the
    211   1.2   thorpej 		 * lock is held we can safely put ourselves on the
    212   1.2   thorpej 		 * sleep queue. If it's not held, we can try taking it
    213   1.2   thorpej 		 * again.
    214   1.2   thorpej 		 */
    215  1.18        cl 		PTQ_INSERT_HEAD(&mutex->ptm_blocked, self, pt_sleep);
    216  1.35        ad 		if (__SIMPLELOCK_UNLOCKED_P(&mutex->ptm_lock)) {
    217  1.29        ad 			PTQ_REMOVE(&mutex->ptm_blocked, self, pt_sleep);
    218  1.37        ad 			pthread__spinunlock(self, &mutex->ptm_interlock);
    219  1.29        ad 			continue;
    220  1.29        ad 		}
    221   1.2   thorpej 
    222  1.31        ad 		mp = mutex->ptm_private;
    223  1.31        ad 		if (mutex->ptm_owner == self && mp != NULL) {
    224  1.29        ad 			switch (mp->type) {
    225  1.29        ad 			case PTHREAD_MUTEX_ERRORCHECK:
    226  1.29        ad 				PTQ_REMOVE(&mutex->ptm_blocked, self, pt_sleep);
    227  1.37        ad 				pthread__spinunlock(self, &mutex->ptm_interlock);
    228  1.29        ad 				return EDEADLK;
    229  1.21       chs 
    230  1.29        ad 			case PTHREAD_MUTEX_RECURSIVE:
    231  1.21       chs 				/*
    232  1.29        ad 				 * It's safe to do this without
    233  1.29        ad 				 * holding the interlock, because
    234  1.29        ad 				 * we only modify it if we know we
    235  1.29        ad 				 * own the mutex.
    236  1.21       chs 				 */
    237  1.29        ad 				PTQ_REMOVE(&mutex->ptm_blocked, self, pt_sleep);
    238  1.37        ad 				pthread__spinunlock(self, &mutex->ptm_interlock);
    239  1.29        ad 				if (mp->recursecount == INT_MAX)
    240  1.29        ad 					return EAGAIN;
    241  1.29        ad 				mp->recursecount++;
    242  1.29        ad 				return 0;
    243  1.21       chs 			}
    244  1.29        ad 		}
    245  1.21       chs 
    246  1.29        ad 		if (pthread__started == 0) {
    247  1.29        ad 			/* The spec says we must deadlock, so... */
    248  1.29        ad 			pthread__assert(mp->type == PTHREAD_MUTEX_NORMAL);
    249  1.29        ad 			(void) sigprocmask(SIG_SETMASK, NULL, &ss);
    250  1.29        ad 			for (;;) {
    251  1.29        ad 				sigsuspend(&ss);
    252  1.29        ad 			}
    253  1.29        ad 			/*NOTREACHED*/
    254   1.2   thorpej 		}
    255  1.29        ad 
    256  1.29        ad 		/*
    257  1.29        ad 		 * Locking a mutex is not a cancellation
    258  1.29        ad 		 * point, so we don't need to do the
    259  1.29        ad 		 * test-cancellation dance. We may get woken
    260  1.29        ad 		 * up spuriously by pthread_cancel or signals,
    261  1.29        ad 		 * but it's okay since we're just going to
    262  1.29        ad 		 * retry.
    263  1.29        ad 		 */
    264  1.29        ad 		self->pt_sleeponq = 1;
    265  1.29        ad 		self->pt_sleepobj = &mutex->ptm_blocked;
    266  1.37        ad 		pthread__spinunlock(self, &mutex->ptm_interlock);
    267  1.29        ad 		(void)pthread__park(self, &mutex->ptm_interlock,
    268  1.29        ad 		    &mutex->ptm_blocked, NULL, 0, &mutex->ptm_blocked);
    269   1.2   thorpej 	}
    270   1.2   thorpej 
    271   1.2   thorpej 	return 0;
    272   1.2   thorpej }
    273   1.2   thorpej 
    274   1.2   thorpej 
    275   1.2   thorpej int
    276   1.2   thorpej pthread_mutex_trylock(pthread_mutex_t *mutex)
    277   1.2   thorpej {
    278  1.27        ad 	struct mutex_private *mp;
    279  1.27        ad 	pthread_t self;
    280   1.2   thorpej 
    281  1.14   nathanw 	pthread__error(EINVAL, "Invalid mutex",
    282  1.14   nathanw 	    mutex->ptm_magic == _PT_MUTEX_MAGIC);
    283   1.2   thorpej 
    284  1.27        ad 	self = pthread__self();
    285  1.27        ad 
    286  1.37        ad 	if (pthread__spintrylock(self, &mutex->ptm_lock) == 0) {
    287   1.2   thorpej 		/*
    288   1.2   thorpej 		 * These tests can be performed without holding the
    289   1.2   thorpej 		 * interlock because these fields are only modified
    290   1.2   thorpej 		 * if we know we own the mutex.
    291   1.2   thorpej 		 */
    292  1.31        ad 		mp = mutex->ptm_private;
    293  1.31        ad 		if (mp != NULL && mp->type == PTHREAD_MUTEX_RECURSIVE &&
    294  1.27        ad 		    mutex->ptm_owner == self) {
    295  1.13   nathanw 			if (mp->recursecount == INT_MAX)
    296  1.13   nathanw 				return EAGAIN;
    297  1.13   nathanw 			mp->recursecount++;
    298  1.13   nathanw 			return 0;
    299   1.2   thorpej 		}
    300   1.2   thorpej 
    301   1.2   thorpej 		return EBUSY;
    302   1.2   thorpej 	}
    303   1.2   thorpej 
    304  1.27        ad 	mutex->ptm_owner = self;
    305   1.2   thorpej 
    306   1.2   thorpej 	return 0;
    307   1.2   thorpej }
    308   1.2   thorpej 
    309   1.2   thorpej 
    310   1.2   thorpej int
    311   1.2   thorpej pthread_mutex_unlock(pthread_mutex_t *mutex)
    312   1.2   thorpej {
    313   1.2   thorpej 	struct mutex_private *mp;
    314  1.27        ad 	pthread_t self;
    315  1.13   nathanw 	int weown;
    316  1.13   nathanw 
    317  1.14   nathanw 	pthread__error(EINVAL, "Invalid mutex",
    318  1.14   nathanw 	    mutex->ptm_magic == _PT_MUTEX_MAGIC);
    319   1.2   thorpej 
    320   1.2   thorpej 	/*
    321   1.2   thorpej 	 * These tests can be performed without holding the
    322   1.2   thorpej 	 * interlock because these fields are only modified
    323   1.2   thorpej 	 * if we know we own the mutex.
    324   1.2   thorpej 	 */
    325  1.37        ad 	self = pthread__self();
    326  1.27        ad 	weown = (mutex->ptm_owner == self);
    327  1.31        ad 	mp = mutex->ptm_private;
    328  1.31        ad 
    329  1.31        ad 	if (mp == NULL) {
    330  1.31        ad 		if (__predict_false(!weown)) {
    331  1.31        ad 			pthread__error(EPERM, "Unlocking unlocked mutex",
    332  1.31        ad 			    (mutex->ptm_owner != 0));
    333  1.31        ad 			pthread__error(EPERM,
    334  1.31        ad 			    "Unlocking mutex owned by another thread", weown);
    335  1.31        ad 		}
    336  1.31        ad 	} else if (mp->type == PTHREAD_MUTEX_RECURSIVE) {
    337  1.13   nathanw 		if (!weown)
    338   1.2   thorpej 			return EPERM;
    339   1.2   thorpej 		if (mp->recursecount != 0) {
    340   1.2   thorpej 			mp->recursecount--;
    341   1.2   thorpej 			return 0;
    342   1.2   thorpej 		}
    343  1.31        ad 	} else if (mp->type == PTHREAD_MUTEX_ERRORCHECK) {
    344  1.13   nathanw 		if (!weown)
    345  1.13   nathanw 			return EPERM;
    346  1.15   nathanw 		if (__predict_false(!weown)) {
    347  1.15   nathanw 			pthread__error(EPERM, "Unlocking unlocked mutex",
    348  1.15   nathanw 			    (mutex->ptm_owner != 0));
    349  1.15   nathanw 			pthread__error(EPERM,
    350  1.15   nathanw 			    "Unlocking mutex owned by another thread", weown);
    351  1.15   nathanw 		}
    352   1.2   thorpej 	}
    353   1.2   thorpej 
    354   1.2   thorpej 	mutex->ptm_owner = NULL;
    355  1.37        ad 	pthread__spinunlock(self, &mutex->ptm_lock);
    356  1.27        ad 
    357   1.8   nathanw 	/*
    358   1.8   nathanw 	 * Do a double-checked locking dance to see if there are any
    359   1.8   nathanw 	 * waiters.  If we don't see any waiters, we can exit, because
    360   1.8   nathanw 	 * we've already released the lock. If we do see waiters, they
    361   1.8   nathanw 	 * were probably waiting on us... there's a slight chance that
    362   1.8   nathanw 	 * they are waiting on a different thread's ownership of the
    363   1.8   nathanw 	 * lock that happened between the unlock above and this
    364   1.8   nathanw 	 * examination of the queue; if so, no harm is done, as the
    365   1.8   nathanw 	 * waiter will loop and see that the mutex is still locked.
    366   1.8   nathanw 	 */
    367  1.37        ad 	pthread__spinlock(self, &mutex->ptm_interlock);
    368  1.27        ad 	pthread__unpark_all(self, &mutex->ptm_interlock, &mutex->ptm_blocked);
    369   1.2   thorpej 	return 0;
    370   1.2   thorpej }
    371   1.2   thorpej 
    372   1.2   thorpej int
    373   1.2   thorpej pthread_mutexattr_init(pthread_mutexattr_t *attr)
    374   1.2   thorpej {
    375   1.2   thorpej 	struct mutexattr_private *map;
    376   1.2   thorpej 
    377   1.2   thorpej 	map = malloc(sizeof(*map));
    378   1.2   thorpej 	if (map == NULL)
    379   1.2   thorpej 		return ENOMEM;
    380   1.2   thorpej 
    381   1.2   thorpej 	*map = mutexattr_private_default;
    382   1.2   thorpej 
    383   1.2   thorpej 	attr->ptma_magic = _PT_MUTEXATTR_MAGIC;
    384   1.2   thorpej 	attr->ptma_private = map;
    385   1.2   thorpej 
    386   1.2   thorpej 	return 0;
    387   1.2   thorpej }
    388   1.2   thorpej 
    389   1.2   thorpej 
    390   1.2   thorpej int
    391   1.2   thorpej pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
    392   1.2   thorpej {
    393   1.2   thorpej 
    394  1.14   nathanw 	pthread__error(EINVAL, "Invalid mutex attribute",
    395  1.14   nathanw 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    396   1.2   thorpej 
    397   1.2   thorpej 	attr->ptma_magic = _PT_MUTEXATTR_DEAD;
    398   1.2   thorpej 	if (attr->ptma_private != NULL)
    399   1.2   thorpej 		free(attr->ptma_private);
    400   1.2   thorpej 
    401   1.2   thorpej 	return 0;
    402   1.2   thorpej }
    403   1.2   thorpej 
    404   1.2   thorpej 
    405   1.2   thorpej int
    406   1.2   thorpej pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *typep)
    407   1.2   thorpej {
    408   1.2   thorpej 	struct mutexattr_private *map;
    409   1.2   thorpej 
    410  1.14   nathanw 	pthread__error(EINVAL, "Invalid mutex attribute",
    411  1.14   nathanw 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    412   1.2   thorpej 
    413   1.2   thorpej 	map = attr->ptma_private;
    414   1.2   thorpej 
    415   1.2   thorpej 	*typep = map->type;
    416   1.2   thorpej 
    417   1.2   thorpej 	return 0;
    418   1.2   thorpej }
    419   1.2   thorpej 
    420   1.2   thorpej 
    421   1.2   thorpej int
    422   1.2   thorpej pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
    423   1.2   thorpej {
    424   1.2   thorpej 	struct mutexattr_private *map;
    425   1.2   thorpej 
    426  1.14   nathanw 	pthread__error(EINVAL, "Invalid mutex attribute",
    427  1.14   nathanw 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    428  1.13   nathanw 
    429   1.2   thorpej 	map = attr->ptma_private;
    430   1.2   thorpej 
    431   1.2   thorpej 	switch (type) {
    432   1.2   thorpej 	case PTHREAD_MUTEX_NORMAL:
    433   1.2   thorpej 	case PTHREAD_MUTEX_ERRORCHECK:
    434   1.2   thorpej 	case PTHREAD_MUTEX_RECURSIVE:
    435   1.2   thorpej 		map->type = type;
    436   1.2   thorpej 		break;
    437   1.2   thorpej 
    438   1.2   thorpej 	default:
    439   1.2   thorpej 		return EINVAL;
    440   1.2   thorpej 	}
    441   1.2   thorpej 
    442   1.2   thorpej 	return 0;
    443   1.2   thorpej }
    444   1.2   thorpej 
    445   1.2   thorpej 
    446  1.19   nathanw static void
    447  1.19   nathanw once_cleanup(void *closure)
    448  1.19   nathanw {
    449  1.19   nathanw 
    450  1.19   nathanw        pthread_mutex_unlock((pthread_mutex_t *)closure);
    451  1.19   nathanw }
    452  1.19   nathanw 
    453  1.19   nathanw 
    454   1.2   thorpej int
    455   1.2   thorpej pthread_once(pthread_once_t *once_control, void (*routine)(void))
    456   1.2   thorpej {
    457   1.2   thorpej 
    458   1.2   thorpej 	if (once_control->pto_done == 0) {
    459   1.2   thorpej 		pthread_mutex_lock(&once_control->pto_mutex);
    460  1.19   nathanw 		pthread_cleanup_push(&once_cleanup, &once_control->pto_mutex);
    461   1.2   thorpej 		if (once_control->pto_done == 0) {
    462   1.2   thorpej 			routine();
    463   1.2   thorpej 			once_control->pto_done = 1;
    464   1.2   thorpej 		}
    465  1.19   nathanw 		pthread_cleanup_pop(1);
    466   1.2   thorpej 	}
    467   1.2   thorpej 
    468   1.2   thorpej 	return 0;
    469   1.2   thorpej }
    470  1.32        ad 
    471  1.33        ad int
    472  1.36        ad pthread__mutex_deferwake(pthread_t thread, pthread_mutex_t *mutex)
    473  1.33        ad {
    474  1.33        ad 
    475  1.33        ad 	return mutex->ptm_owner == thread;
    476  1.33        ad }
    477  1.33        ad 
    478  1.39        ad int
    479  1.39        ad _pthread_mutex_held_np(pthread_mutex_t *mutex)
    480  1.39        ad {
    481  1.39        ad 
    482  1.39        ad 	return mutex->ptm_owner == pthread__self();
    483  1.39        ad }
    484  1.39        ad 
    485  1.39        ad pthread_t
    486  1.39        ad _pthread_mutex_owner_np(pthread_mutex_t *mutex)
    487  1.39        ad {
    488  1.39        ad 
    489  1.39        ad 	return (pthread_t)mutex->ptm_owner;
    490  1.39        ad }
    491  1.39        ad 
    492  1.32        ad #endif	/* !PTHREAD__HAVE_ATOMIC */
    493