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