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