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