Home | History | Annotate | Line # | Download | only in libpthread
pthread_mutex.c revision 1.43
      1 /*	$NetBSD: pthread_mutex.c,v 1.43 2008/01/25 02:12:10 rafal 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.43 2008/01/25 02:12:10 rafal 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 static void once_cleanup(void *);
     58 
     59 int		_pthread_mutex_held_np(pthread_mutex_t *);
     60 pthread_t	_pthread_mutex_owner_np(pthread_mutex_t *);
     61 
     62 __weak_alias(pthread_mutex_held_np,_pthread_mutex_held_np)
     63 __weak_alias(pthread_mutex_owner_np,_pthread_mutex_owner_np)
     64 
     65 __strong_alias(__libc_mutex_init,pthread_mutex_init)
     66 __strong_alias(__libc_mutex_lock,pthread_mutex_lock)
     67 __strong_alias(__libc_mutex_trylock,pthread_mutex_trylock)
     68 __strong_alias(__libc_mutex_unlock,pthread_mutex_unlock)
     69 __strong_alias(__libc_mutex_destroy,pthread_mutex_destroy)
     70 
     71 __strong_alias(__libc_mutexattr_init,pthread_mutexattr_init)
     72 __strong_alias(__libc_mutexattr_destroy,pthread_mutexattr_destroy)
     73 __strong_alias(__libc_mutexattr_settype,pthread_mutexattr_settype)
     74 
     75 __strong_alias(__libc_thr_once,pthread_once)
     76 
     77 struct mutex_private {
     78 	int	type;
     79 	int	recursecount;
     80 };
     81 
     82 static const struct mutex_private mutex_private_default = {
     83 	PTHREAD_MUTEX_DEFAULT,
     84 	0,
     85 };
     86 
     87 struct mutexattr_private {
     88 	int	type;
     89 };
     90 
     91 static const struct mutexattr_private mutexattr_private_default = {
     92 	PTHREAD_MUTEX_DEFAULT,
     93 };
     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 	    __SIMPLELOCK_UNLOCKED_P(&mutex->ptm_lock));
    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__spintrylock() to issue
    154  * a barrier after obtaining a lock, and on pthread__spinunlock() to
    155  * issue a barrier before releasing a lock.
    156  */
    157 
    158 int
    159 pthread_mutex_lock(pthread_mutex_t *mutex)
    160 {
    161 	pthread_t self;
    162 	int error;
    163 
    164 	self = pthread__self();
    165 
    166 	/*
    167 	 * Note that if we get the lock, we don't have to deal with any
    168 	 * non-default lock type handling.
    169 	 */
    170 	if (__predict_false(pthread__spintrylock(self, &mutex->ptm_lock) == 0)) {
    171 		error = pthread_mutex_lock_slow(self, mutex);
    172 		if (error)
    173 			return error;
    174 	}
    175 
    176 	/*
    177 	 * We have the lock!
    178 	 */
    179 	mutex->ptm_owner = self;
    180 
    181 	return 0;
    182 }
    183 
    184 
    185 static int
    186 pthread_mutex_lock_slow(pthread_t self, pthread_mutex_t *mutex)
    187 {
    188 	struct mutex_private *mp;
    189 	int count;
    190 
    191 	pthread__error(EINVAL, "Invalid mutex",
    192 	    mutex->ptm_magic == _PT_MUTEX_MAGIC);
    193 
    194 	for (;;) {
    195 		/* Spin for a while. */
    196 		count = pthread__nspins;
    197 		while (__SIMPLELOCK_LOCKED_P(&mutex->ptm_lock)  && --count > 0)
    198 			pthread__smt_pause();
    199 		if (count > 0) {
    200 			if (pthread__spintrylock(self, &mutex->ptm_lock) != 0)
    201 				break;
    202 			continue;
    203 		}
    204 
    205 		/* Okay, didn't look free. Get the interlock... */
    206 		pthread__spinlock(self, &mutex->ptm_interlock);
    207 
    208 		/*
    209 		 * The mutex_unlock routine will get the interlock
    210 		 * before looking at the list of sleepers, so if the
    211 		 * lock is held we can safely put ourselves on the
    212 		 * sleep queue. If it's not held, we can try taking it
    213 		 * again.
    214 		 */
    215 		PTQ_INSERT_HEAD(&mutex->ptm_blocked, self, pt_sleep);
    216 		if (__SIMPLELOCK_UNLOCKED_P(&mutex->ptm_lock)) {
    217 			PTQ_REMOVE(&mutex->ptm_blocked, self, pt_sleep);
    218 			pthread__spinunlock(self, &mutex->ptm_interlock);
    219 			continue;
    220 		}
    221 
    222 		mp = mutex->ptm_private;
    223 		if (mutex->ptm_owner == self && mp != NULL) {
    224 			switch (mp->type) {
    225 			case PTHREAD_MUTEX_ERRORCHECK:
    226 				PTQ_REMOVE(&mutex->ptm_blocked, self, pt_sleep);
    227 				pthread__spinunlock(self, &mutex->ptm_interlock);
    228 				return EDEADLK;
    229 
    230 			case PTHREAD_MUTEX_RECURSIVE:
    231 				/*
    232 				 * It's safe to do this without
    233 				 * holding the interlock, because
    234 				 * we only modify it if we know we
    235 				 * own the mutex.
    236 				 */
    237 				PTQ_REMOVE(&mutex->ptm_blocked, self, pt_sleep);
    238 				pthread__spinunlock(self, &mutex->ptm_interlock);
    239 				if (mp->recursecount == INT_MAX)
    240 					return EAGAIN;
    241 				mp->recursecount++;
    242 				return 0;
    243 			}
    244 		}
    245 
    246 		/*
    247 		 * Locking a mutex is not a cancellation
    248 		 * point, so we don't need to do the
    249 		 * test-cancellation dance. We may get woken
    250 		 * up spuriously by pthread_cancel or signals,
    251 		 * but it's okay since we're just going to
    252 		 * retry.
    253 		 */
    254 		self->pt_sleeponq = 1;
    255 		self->pt_sleepobj = &mutex->ptm_blocked;
    256 		pthread__spinunlock(self, &mutex->ptm_interlock);
    257 		(void)pthread__park(self, &mutex->ptm_interlock,
    258 		    &mutex->ptm_blocked, NULL, 0, &mutex->ptm_blocked);
    259 	}
    260 
    261 	return 0;
    262 }
    263 
    264 
    265 int
    266 pthread_mutex_trylock(pthread_mutex_t *mutex)
    267 {
    268 	struct mutex_private *mp;
    269 	pthread_t self;
    270 
    271 	pthread__error(EINVAL, "Invalid mutex",
    272 	    mutex->ptm_magic == _PT_MUTEX_MAGIC);
    273 
    274 	self = pthread__self();
    275 
    276 	if (pthread__spintrylock(self, &mutex->ptm_lock) == 0) {
    277 		/*
    278 		 * These tests can be performed without holding the
    279 		 * interlock because these fields are only modified
    280 		 * if we know we own the mutex.
    281 		 */
    282 		mp = mutex->ptm_private;
    283 		if (mp != NULL && mp->type == PTHREAD_MUTEX_RECURSIVE &&
    284 		    mutex->ptm_owner == self) {
    285 			if (mp->recursecount == INT_MAX)
    286 				return EAGAIN;
    287 			mp->recursecount++;
    288 			return 0;
    289 		}
    290 
    291 		return EBUSY;
    292 	}
    293 
    294 	mutex->ptm_owner = self;
    295 
    296 	return 0;
    297 }
    298 
    299 
    300 int
    301 pthread_mutex_unlock(pthread_mutex_t *mutex)
    302 {
    303 	struct mutex_private *mp;
    304 	pthread_t self;
    305 	int weown;
    306 
    307 	pthread__error(EINVAL, "Invalid mutex",
    308 	    mutex->ptm_magic == _PT_MUTEX_MAGIC);
    309 
    310 	/*
    311 	 * These tests can be performed without holding the
    312 	 * interlock because these fields are only modified
    313 	 * if we know we own the mutex.
    314 	 */
    315 	self = pthread__self();
    316 	weown = (mutex->ptm_owner == self);
    317 	mp = mutex->ptm_private;
    318 
    319 	if (mp == NULL) {
    320 		if (__predict_false(!weown)) {
    321 			pthread__error(EPERM, "Unlocking unlocked mutex",
    322 			    (mutex->ptm_owner != 0));
    323 			pthread__error(EPERM,
    324 			    "Unlocking mutex owned by another thread", weown);
    325 		}
    326 	} else if (mp->type == PTHREAD_MUTEX_RECURSIVE) {
    327 		if (!weown)
    328 			return EPERM;
    329 		if (mp->recursecount != 0) {
    330 			mp->recursecount--;
    331 			return 0;
    332 		}
    333 	} else if (mp->type == PTHREAD_MUTEX_ERRORCHECK) {
    334 		if (!weown)
    335 			return EPERM;
    336 		if (__predict_false(!weown)) {
    337 			pthread__error(EPERM, "Unlocking unlocked mutex",
    338 			    (mutex->ptm_owner != 0));
    339 			pthread__error(EPERM,
    340 			    "Unlocking mutex owned by another thread", weown);
    341 		}
    342 	}
    343 
    344 	mutex->ptm_owner = NULL;
    345 	pthread__spinunlock(self, &mutex->ptm_lock);
    346 
    347 	/*
    348 	 * Do a double-checked locking dance to see if there are any
    349 	 * waiters.  If we don't see any waiters, we can exit, because
    350 	 * we've already released the lock. If we do see waiters, they
    351 	 * were probably waiting on us... there's a slight chance that
    352 	 * they are waiting on a different thread's ownership of the
    353 	 * lock that happened between the unlock above and this
    354 	 * examination of the queue; if so, no harm is done, as the
    355 	 * waiter will loop and see that the mutex is still locked.
    356 	 */
    357 	pthread__spinlock(self, &mutex->ptm_interlock);
    358 	pthread__unpark_all(self, &mutex->ptm_interlock, &mutex->ptm_blocked);
    359 	return 0;
    360 }
    361 
    362 int
    363 pthread_mutexattr_init(pthread_mutexattr_t *attr)
    364 {
    365 	struct mutexattr_private *map;
    366 
    367 	map = malloc(sizeof(*map));
    368 	if (map == NULL)
    369 		return ENOMEM;
    370 
    371 	*map = mutexattr_private_default;
    372 
    373 	attr->ptma_magic = _PT_MUTEXATTR_MAGIC;
    374 	attr->ptma_private = map;
    375 
    376 	return 0;
    377 }
    378 
    379 
    380 int
    381 pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
    382 {
    383 
    384 	pthread__error(EINVAL, "Invalid mutex attribute",
    385 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    386 
    387 	attr->ptma_magic = _PT_MUTEXATTR_DEAD;
    388 	if (attr->ptma_private != NULL)
    389 		free(attr->ptma_private);
    390 
    391 	return 0;
    392 }
    393 
    394 
    395 int
    396 pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *typep)
    397 {
    398 	struct mutexattr_private *map;
    399 
    400 	pthread__error(EINVAL, "Invalid mutex attribute",
    401 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    402 
    403 	map = attr->ptma_private;
    404 
    405 	*typep = map->type;
    406 
    407 	return 0;
    408 }
    409 
    410 
    411 int
    412 pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
    413 {
    414 	struct mutexattr_private *map;
    415 
    416 	pthread__error(EINVAL, "Invalid mutex attribute",
    417 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
    418 
    419 	map = attr->ptma_private;
    420 
    421 	switch (type) {
    422 	case PTHREAD_MUTEX_NORMAL:
    423 	case PTHREAD_MUTEX_ERRORCHECK:
    424 	case PTHREAD_MUTEX_RECURSIVE:
    425 		map->type = type;
    426 		break;
    427 
    428 	default:
    429 		return EINVAL;
    430 	}
    431 
    432 	return 0;
    433 }
    434 
    435 
    436 static void
    437 once_cleanup(void *closure)
    438 {
    439 
    440        pthread_mutex_unlock((pthread_mutex_t *)closure);
    441 }
    442 
    443 
    444 int
    445 pthread_once(pthread_once_t *once_control, void (*routine)(void))
    446 {
    447 
    448 	if (once_control->pto_done == 0) {
    449 		pthread_mutex_lock(&once_control->pto_mutex);
    450 		pthread_cleanup_push(&once_cleanup, &once_control->pto_mutex);
    451 		if (once_control->pto_done == 0) {
    452 			routine();
    453 			once_control->pto_done = 1;
    454 		}
    455 		pthread_cleanup_pop(1);
    456 	}
    457 
    458 	return 0;
    459 }
    460 
    461 int
    462 pthread__mutex_deferwake(pthread_t thread, pthread_mutex_t *mutex)
    463 {
    464 
    465 	return mutex->ptm_owner == thread;
    466 }
    467 
    468 int
    469 _pthread_mutex_held_np(pthread_mutex_t *mutex)
    470 {
    471 
    472 	return mutex->ptm_owner == pthread__self();
    473 }
    474 
    475 pthread_t
    476 _pthread_mutex_owner_np(pthread_mutex_t *mutex)
    477 {
    478 
    479 	return (pthread_t)mutex->ptm_owner;
    480 }
    481 
    482 #endif	/* !PTHREAD__HAVE_ATOMIC */
    483