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