Home | History | Annotate | Line # | Download | only in libpthread
pthread_mutex.c revision 1.23
      1 /*	$NetBSD: pthread_mutex.c,v 1.23 2006/12/23 05:14:47 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001, 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Nathan J. Williams, and by Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __RCSID("$NetBSD: pthread_mutex.c,v 1.23 2006/12/23 05:14:47 ad Exp $");
     41 
     42 #include <errno.h>
     43 #include <limits.h>
     44 #include <stdlib.h>
     45 #include <string.h>
     46 
     47 #include "pthread.h"
     48 #include "pthread_int.h"
     49 
     50 static int pthread_mutex_lock_slow(pthread_mutex_t *);
     51 
     52 __strong_alias(__libc_mutex_init,pthread_mutex_init)
     53 __strong_alias(__libc_mutex_lock,pthread_mutex_lock)
     54 __strong_alias(__libc_mutex_trylock,pthread_mutex_trylock)
     55 __strong_alias(__libc_mutex_unlock,pthread_mutex_unlock)
     56 __strong_alias(__libc_mutex_destroy,pthread_mutex_destroy)
     57 
     58 __strong_alias(__libc_mutexattr_init,pthread_mutexattr_init)
     59 __strong_alias(__libc_mutexattr_destroy,pthread_mutexattr_destroy)
     60 __strong_alias(__libc_mutexattr_settype,pthread_mutexattr_settype)
     61 
     62 __strong_alias(__libc_thr_once,pthread_once)
     63 
     64 struct mutex_private {
     65 	int	type;
     66 	int	recursecount;
     67 };
     68 
     69 static const struct mutex_private mutex_private_default = {
     70 	PTHREAD_MUTEX_DEFAULT,
     71 	0,
     72 };
     73 
     74 struct mutexattr_private {
     75 	int	type;
     76 };
     77 
     78 static const struct mutexattr_private mutexattr_private_default = {
     79 	PTHREAD_MUTEX_DEFAULT,
     80 };
     81 
     82 /*
     83  * If the mutex does not already have private data (i.e. was statically
     84  * initialized), then give it the default.
     85  */
     86 #define	GET_MUTEX_PRIVATE(mutex, mp)					\
     87 do {									\
     88 	if (__predict_false((mp = (mutex)->ptm_private) == NULL)) {	\
     89 		/* LINTED cast away const */				\
     90 		mp = ((mutex)->ptm_private =				\
     91 		    (void *)&mutex_private_default);			\
     92 	}								\
     93 } while (/*CONSTCOND*/0)
     94 
     95 int
     96 pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
     97 {
     98 	struct mutexattr_private *map;
     99 	struct mutex_private *mp;
    100 
    101 	pthread__error(EINVAL, "Invalid mutex attribute",
    102 	    (attr == NULL) || (attr->ptma_magic == _PT_MUTEXATTR_MAGIC));
    103 
    104 	if (attr != NULL && (map = attr->ptma_private) != NULL &&
    105 	    memcmp(map, &mutexattr_private_default, sizeof(*map)) != 0) {
    106 		mp = malloc(sizeof(*mp));
    107 		if (mp == NULL)
    108 			return ENOMEM;
    109 
    110 		mp->type = map->type;
    111 		mp->recursecount = 0;
    112 	} else {
    113 		/* LINTED cast away const */
    114 		mp = (struct mutex_private *) &mutex_private_default;
    115 	}
    116 
    117 	mutex->ptm_magic = _PT_MUTEX_MAGIC;
    118 	mutex->ptm_owner = NULL;
    119 	pthread_lockinit(&mutex->ptm_lock);
    120 	pthread_lockinit(&mutex->ptm_interlock);
    121 	PTQ_INIT(&mutex->ptm_blocked);
    122 	mutex->ptm_private = mp;
    123 
    124 	return 0;
    125 }
    126 
    127 
    128 int
    129 pthread_mutex_destroy(pthread_mutex_t *mutex)
    130 {
    131 
    132 	pthread__error(EINVAL, "Invalid mutex",
    133 	    mutex->ptm_magic == _PT_MUTEX_MAGIC);
    134 	pthread__error(EBUSY, "Destroying locked mutex",
    135 	    mutex->ptm_lock == __SIMPLELOCK_UNLOCKED);
    136 
    137 	mutex->ptm_magic = _PT_MUTEX_DEAD;
    138 	if (mutex->ptm_private != NULL &&
    139 	    mutex->ptm_private != (const void *)&mutex_private_default)
    140 		free(mutex->ptm_private);
    141 
    142 	return 0;
    143 }
    144 
    145 
    146 /*
    147  * Note regarding memory visibility: Pthreads has rules about memory
    148  * visibility and mutexes. Very roughly: Memory a thread can see when
    149  * it unlocks a mutex can be seen by another thread that locks the
    150  * same mutex.
    151  *
    152  * A memory barrier after a lock and before an unlock will provide
    153  * this behavior. This code relies on pthread__simple_lock_try() to issue
    154  * a barrier after obtaining a lock, and on pthread__simple_unlock() to
    155  * issue a barrier before releasing a lock.
    156  */
    157 
    158 int
    159 pthread_mutex_lock(pthread_mutex_t *mutex)
    160 {
    161 	int error;
    162 
    163 	PTHREADD_ADD(PTHREADD_MUTEX_LOCK);
    164 	/*
    165 	 * Note that if we get the lock, we don't have to deal with any
    166 	 * non-default lock type handling.
    167 	 */
    168 	if (__predict_false(pthread__simple_lock_try(&mutex->ptm_lock) == 0)) {
    169 		error = pthread_mutex_lock_slow(mutex);
    170 		if (error)
    171 			return error;
    172 	}
    173 
    174 	/* We have the lock! */
    175 	/*
    176 	 * Identifying ourselves may be slow, and this assignment is
    177 	 * only needed for (a) debugging identity of the owning thread
    178 	 * and (b) handling errorcheck and recursive mutexes. It's
    179 	 * better to just stash our stack pointer here and let those
    180 	 * slow exception cases compute the stack->thread mapping.
    181 	 */
    182 	mutex->ptm_owner = (pthread_t)pthread__sp();
    183 
    184 	return 0;
    185 }
    186 
    187 
    188 static int
    189 pthread_mutex_lock_slow(pthread_mutex_t *mutex)
    190 {
    191 	pthread_t self;
    192 	extern int pthread__started;
    193 
    194 	pthread__error(EINVAL, "Invalid mutex",
    195 	    mutex->ptm_magic == _PT_MUTEX_MAGIC);
    196 
    197 	self = pthread__self();
    198 
    199 	PTHREADD_ADD(PTHREADD_MUTEX_LOCK_SLOW);
    200 	while (/*CONSTCOND*/1) {
    201 		if (pthread__simple_lock_try(&mutex->ptm_lock))
    202 			break; /* got it! */
    203 
    204 		/* Okay, didn't look free. Get the interlock... */
    205 		pthread_spinlock(self, &mutex->ptm_interlock);
    206 
    207 		/*
    208 		 * The mutex_unlock routine will get the interlock
    209 		 * before looking at the list of sleepers, so if the
    210 		 * lock is held we can safely put ourselves on the
    211 		 * sleep queue. If it's not held, we can try taking it
    212 		 * again.
    213 		 */
    214 		PTQ_INSERT_HEAD(&mutex->ptm_blocked, self, pt_sleep);
    215 		if (mutex->ptm_lock == __SIMPLELOCK_LOCKED) {
    216 			struct mutex_private *mp;
    217 
    218 			GET_MUTEX_PRIVATE(mutex, mp);
    219 
    220 			if (pthread__id(mutex->ptm_owner) == self) {
    221 				switch (mp->type) {
    222 				case PTHREAD_MUTEX_ERRORCHECK:
    223 					PTQ_REMOVE(&mutex->ptm_blocked, self,
    224 					    pt_sleep);
    225 					pthread_spinunlock(self,
    226 					    &mutex->ptm_interlock);
    227 					return EDEADLK;
    228 
    229 				case PTHREAD_MUTEX_RECURSIVE:
    230 					/*
    231 					 * It's safe to do this without
    232 					 * holding the interlock, because
    233 					 * we only modify it if we know we
    234 					 * own the mutex.
    235 					 */
    236 					PTQ_REMOVE(&mutex->ptm_blocked, self,
    237 					    pt_sleep);
    238 					pthread_spinunlock(self,
    239 					    &mutex->ptm_interlock);
    240 					if (mp->recursecount == INT_MAX)
    241 						return EAGAIN;
    242 					mp->recursecount++;
    243 					return 0;
    244 				}
    245 			}
    246 
    247 			if (pthread__started == 0) {
    248 				sigset_t ss;
    249 
    250 				/*
    251 				 * The spec says we must deadlock, so...
    252 				 */
    253 				pthread__assert(mp->type ==
    254 						PTHREAD_MUTEX_NORMAL);
    255 				(void) sigprocmask(SIG_SETMASK, NULL, &ss);
    256 				for (;;) {
    257 					sigsuspend(&ss);
    258 				}
    259 				/*NOTREACHED*/
    260 			}
    261 
    262 			/*
    263 			 * Locking a mutex is not a cancellation
    264 			 * point, so we don't need to do the
    265 			 * test-cancellation dance. We may get woken
    266 			 * up spuriously by pthread_cancel or signals,
    267 			 * but it's okay since we're just going to
    268 			 * retry.
    269 			 */
    270 #ifdef PTHREAD_SA
    271 			pthread_spinlock(self, &self->pt_statelock);
    272 			self->pt_state = PT_STATE_BLOCKED_QUEUE;
    273 			self->pt_sleepobj = mutex;
    274 			self->pt_sleepq = &mutex->ptm_blocked;
    275 			self->pt_sleeplock = &mutex->ptm_interlock;
    276 			pthread_spinunlock(self, &self->pt_statelock);
    277 
    278 			pthread__block(self, &mutex->ptm_interlock);
    279 			/* interlock is not held when we return */
    280 #else	/* PTHREAD_SA */
    281 			(void)pthread__park(self, &mutex->ptm_interlock,
    282 			    mutex, NULL, NULL, 0);
    283 			pthread_spinunlock(self, &mutex->ptm_interlock);
    284 #endif	/* PTHREAD_SA */
    285 		} else {
    286 			PTQ_REMOVE(&mutex->ptm_blocked, self, pt_sleep);
    287 			pthread_spinunlock(self, &mutex->ptm_interlock);
    288 		}
    289 		/* Go around for another try. */
    290 	}
    291 
    292 	return 0;
    293 }
    294 
    295 
    296 int
    297 pthread_mutex_trylock(pthread_mutex_t *mutex)
    298 {
    299 
    300 	pthread__error(EINVAL, "Invalid mutex",
    301 	    mutex->ptm_magic == _PT_MUTEX_MAGIC);
    302 
    303 	PTHREADD_ADD(PTHREADD_MUTEX_TRYLOCK);
    304 	if (pthread__simple_lock_try(&mutex->ptm_lock) == 0) {
    305 		struct mutex_private *mp;
    306 
    307 		GET_MUTEX_PRIVATE(mutex, mp);
    308 
    309 		/*
    310 		 * These tests can be performed without holding the
    311 		 * interlock because these fields are only modified
    312 		 * if we know we own the mutex.
    313 		 */
    314 		if ((mp->type == PTHREAD_MUTEX_RECURSIVE) &&
    315 		    (pthread__id(mutex->ptm_owner) == pthread__self())) {
    316 			if (mp->recursecount == INT_MAX)
    317 				return EAGAIN;
    318 			mp->recursecount++;
    319 			return 0;
    320 		}
    321 
    322 		return EBUSY;
    323 	}
    324 
    325 	/* see comment at the end of pthread_mutex_lock() */
    326 	mutex->ptm_owner = (pthread_t)pthread__sp();
    327 
    328 	return 0;
    329 }
    330 
    331 
    332 int
    333 pthread_mutex_unlock(pthread_mutex_t *mutex)
    334 {
    335 	struct mutex_private *mp;
    336 	pthread_t self, blocked;
    337 	int weown;
    338 
    339 	pthread__error(EINVAL, "Invalid mutex",
    340 	    mutex->ptm_magic == _PT_MUTEX_MAGIC);
    341 
    342 	PTHREADD_ADD(PTHREADD_MUTEX_UNLOCK);
    343 
    344 	GET_MUTEX_PRIVATE(mutex, mp);
    345 
    346 	self = pthread_self();
    347 	/*
    348 	 * These tests can be performed without holding the
    349 	 * interlock because these fields are only modified
    350 	 * if we know we own the mutex.
    351 	 */
    352 	weown = (pthread__id(mutex->ptm_owner) == self);
    353 	switch (mp->type) {
    354 	case PTHREAD_MUTEX_RECURSIVE:
    355 		if (!weown)
    356 			return EPERM;
    357 		if (mp->recursecount != 0) {
    358 			mp->recursecount--;
    359 			return 0;
    360 		}
    361 		break;
    362 	case PTHREAD_MUTEX_ERRORCHECK:
    363 		if (!weown)
    364 			return EPERM;
    365 		/*FALLTHROUGH*/
    366 	default:
    367 		if (__predict_false(!weown)) {
    368 			pthread__error(EPERM, "Unlocking unlocked mutex",
    369 			    (mutex->ptm_owner != 0));
    370 			pthread__error(EPERM,
    371 			    "Unlocking mutex owned by another thread", weown);
    372 		}
    373 		break;
    374 	}
    375 
    376 	mutex->ptm_owner = NULL;
    377 	pthread__simple_unlock(&mutex->ptm_lock);
    378 	/*
    379 	 * Do a double-checked locking dance to see if there are any
    380 	 * waiters.  If we don't see any waiters, we can exit, because
    381 	 * we've already released the lock. If we do see waiters, they
    382 	 * were probably waiting on us... there's a slight chance that
    383 	 * they are waiting on a different thread's ownership of the
    384 	 * lock that happened between the unlock above and this
    385 	 * examination of the queue; if so, no harm is done, as the
    386 	 * waiter will loop and see that the mutex is still locked.
    387 	 */
    388 	pthread_spinlock(self, &mutex->ptm_interlock);
    389 	if ((blocked = PTQ_FIRST(&mutex->ptm_blocked)) != NULL) {
    390 		PTQ_REMOVE(&mutex->ptm_blocked, blocked, pt_sleep);
    391 		PTHREADD_ADD(PTHREADD_MUTEX_UNLOCK_UNBLOCK);
    392 #ifdef PTHREAD_SA
    393 		/* Give the head of the blocked queue another try. */
    394 		pthread__sched(self, blocked);
    395 		pthread_spinunlock(self, &mutex->ptm_interlock);
    396 #else	/* PTHREAD_SA */
    397 		pthread__unpark(self, &mutex->ptm_interlock, mutex, blocked);
    398 #endif	/* PTHREAD_SA */
    399 	} else
    400 		pthread_spinunlock(self, &mutex->ptm_interlock);
    401 
    402 	return 0;
    403 }
    404 
    405 int
    406 pthread_mutexattr_init(pthread_mutexattr_t *attr)
    407 {
    408 	struct mutexattr_private *map;
    409 
    410 	map = malloc(sizeof(*map));
    411 	if (map == NULL)
    412 		return ENOMEM;
    413 
    414 	*map = mutexattr_private_default;
    415 
    416 	attr->ptma_magic = _PT_MUTEXATTR_MAGIC;
    417 	attr->ptma_private = map;
    418 
    419 	return 0;
    420 }
    421 
    422 
    423 int
    424 pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
    425 {
    426 
    427 	pthread__error(EINVAL, "Invalid mutex attribute",
    428 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    429 
    430 	attr->ptma_magic = _PT_MUTEXATTR_DEAD;
    431 	if (attr->ptma_private != NULL)
    432 		free(attr->ptma_private);
    433 
    434 	return 0;
    435 }
    436 
    437 
    438 int
    439 pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *typep)
    440 {
    441 	struct mutexattr_private *map;
    442 
    443 	pthread__error(EINVAL, "Invalid mutex attribute",
    444 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    445 
    446 	map = attr->ptma_private;
    447 
    448 	*typep = map->type;
    449 
    450 	return 0;
    451 }
    452 
    453 
    454 int
    455 pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
    456 {
    457 	struct mutexattr_private *map;
    458 
    459 	pthread__error(EINVAL, "Invalid mutex attribute",
    460 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    461 
    462 	map = attr->ptma_private;
    463 
    464 	switch (type) {
    465 	case PTHREAD_MUTEX_NORMAL:
    466 	case PTHREAD_MUTEX_ERRORCHECK:
    467 	case PTHREAD_MUTEX_RECURSIVE:
    468 		map->type = type;
    469 		break;
    470 
    471 	default:
    472 		return EINVAL;
    473 	}
    474 
    475 	return 0;
    476 }
    477 
    478 
    479 static void
    480 once_cleanup(void *closure)
    481 {
    482 
    483        pthread_mutex_unlock((pthread_mutex_t *)closure);
    484 }
    485 
    486 
    487 int
    488 pthread_once(pthread_once_t *once_control, void (*routine)(void))
    489 {
    490 
    491 	if (once_control->pto_done == 0) {
    492 		pthread_mutex_lock(&once_control->pto_mutex);
    493 		pthread_cleanup_push(&once_cleanup, &once_control->pto_mutex);
    494 		if (once_control->pto_done == 0) {
    495 			routine();
    496 			once_control->pto_done = 1;
    497 		}
    498 		pthread_cleanup_pop(1);
    499 	}
    500 
    501 	return 0;
    502 }
    503