Home | History | Annotate | Line # | Download | only in libpthread
pthread_mutex.c revision 1.1.2.13
      1 /*	$NetBSD: pthread_mutex.c,v 1.1.2.13 2002/10/22 21:56:00 nathanw Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 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.
      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 <assert.h>
     40 #include <errno.h>
     41 #include <sys/cdefs.h>
     42 
     43 #include "pthread.h"
     44 #include "pthread_int.h"
     45 
     46 static void pthread_mutex_lock_slow(pthread_mutex_t *);
     47 static void pthread_mutex_unlock_slow(pthread_mutex_t *);
     48 
     49 int
     50 pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
     51 {
     52 
     53 #ifdef ERRORCHECK
     54 	if ((mutex == NULL) ||
     55 	    (attr && (attr->ptma_magic != _PT_MUTEXATTR_MAGIC)))
     56 		return EINVAL;
     57 #endif
     58 
     59 	mutex->ptm_magic = _PT_MUTEX_MAGIC;
     60 	mutex->ptm_owner = NULL;
     61 	pthread_lockinit(&mutex->ptm_lock);
     62 	pthread_lockinit(&mutex->ptm_interlock);
     63 	PTQ_INIT(&mutex->ptm_blocked);
     64 
     65 	return 0;
     66 }
     67 
     68 
     69 int
     70 pthread_mutex_destroy(pthread_mutex_t *mutex)
     71 {
     72 
     73 #ifdef ERRORCHECK
     74 	if ((mutex == NULL) ||
     75 	    (mutex->ptm_magic != _PT_MUTEX_MAGIC) ||
     76 	    (mutex->ptm_lock != __SIMPLELOCK_UNLOCKED))
     77 		return EINVAL;
     78 #endif
     79 
     80 	mutex->ptm_magic = _PT_MUTEX_DEAD;
     81 
     82 	return 0;
     83 }
     84 
     85 
     86 /*
     87  * Note regarding memory visibility: Pthreads has rules about memory
     88  * visibility and mutexes. Very roughly: Memory a thread can see when
     89  * it unlocks a mutex can be seen by another thread that locks the
     90  * same mutex.
     91  *
     92  * A memory barrier after a lock and before an unlock will provide
     93  * this behavior. This code relies on __cpu_simple_lock_try() to issue
     94  * a barrier after obtaining a lock, and on __cpu_simple_unlock() to
     95  * issue a barrier before releasing a lock.
     96  */
     97 
     98 int
     99 pthread_mutex_lock(pthread_mutex_t *mutex)
    100 {
    101 
    102 #ifdef ERRORCHECK
    103 	if ((mutex == NULL) || (mutex->ptm_magic != _PT_MUTEX_MAGIC))
    104 		return EINVAL;
    105 #endif
    106 
    107 	if (__predict_false(__cpu_simple_lock_try(&mutex->ptm_lock) == 0))
    108 		pthread_mutex_lock_slow(mutex);
    109 
    110 	/* We have the lock! */
    111 #ifdef ERRORCHECK
    112 	mutex->ptm_owner = (pthread_t)pthread__sp();
    113 #endif
    114 	return 0;
    115 }
    116 
    117 static void
    118 pthread_mutex_lock_slow(pthread_mutex_t *mutex)
    119 {
    120 	pthread_t self;
    121 
    122 	self = pthread__self();
    123 
    124 	while (/*CONSTCOND*/1) {
    125 		if (__cpu_simple_lock_try(&mutex->ptm_lock))
    126 		    break; /* got it! */
    127 
    128 		/* Okay, didn't look free. Get the interlock... */
    129 		pthread_spinlock(self, &mutex->ptm_interlock);
    130 		/* The mutex_unlock routine will get the interlock
    131 		 * before looking at the list of sleepers, so if the
    132 		 * lock is held we can safely put ourselves on the
    133 		 * sleep queue. If it's not held, we can try taking it
    134 		 * again.
    135 		 */
    136 		if (mutex->ptm_lock == __SIMPLELOCK_LOCKED) {
    137 			PTQ_INSERT_TAIL(&mutex->ptm_blocked, self, pt_sleep);
    138 			/* Locking a mutex is not a cancellation
    139 			 * point, so we don't need to do the
    140 			 * test-cancellation dance. We may get woken
    141 			 * up spuriously by pthread_cancel, though,
    142 			 * but it's okay since we're just going to
    143 			 * retry.
    144 			 */
    145 			pthread_spinlock(self, &self->pt_statelock);
    146 			self->pt_state = PT_STATE_BLOCKED_QUEUE;
    147 			self->pt_sleepobj = mutex;
    148 			self->pt_sleepq = &mutex->ptm_blocked;
    149 			self->pt_sleeplock = &mutex->ptm_interlock;
    150 			pthread_spinunlock(self, &self->pt_statelock);
    151 
    152 			pthread__block(self, &mutex->ptm_interlock);
    153 			/* interlock is not held when we return */
    154 		} else {
    155 			pthread_spinunlock(self, &mutex->ptm_interlock);
    156 		}
    157 		/* Go around for another try. */
    158 	}
    159 }
    160 
    161 
    162 int
    163 pthread_mutex_trylock(pthread_mutex_t *mutex)
    164 {
    165 
    166 #ifdef ERRORCHECK
    167 	if ((mutex == NULL) || (mutex->ptm_magic != _PT_MUTEX_MAGIC))
    168 		return EINVAL;
    169 #endif
    170 
    171 	if (__cpu_simple_lock_try(&mutex->ptm_lock) == 0)
    172 		return EBUSY;
    173 
    174 #ifdef ERRORCHECK
    175 	mutex->ptm_owner = (pthread_t)pthread__sp();
    176 #endif
    177 	return 0;
    178 }
    179 
    180 
    181 int
    182 pthread_mutex_unlock(pthread_mutex_t *mutex)
    183 {
    184 
    185 #ifdef ERRORCHECK
    186 	if ((mutex == NULL) || (mutex->ptm_magic != _PT_MUTEX_MAGIC))
    187 		return EINVAL;
    188 
    189 	if (mutex->ptm_lock != __SIMPLELOCK_LOCKED)
    190 		return EPERM; /* Not exactly the right error. */
    191 #endif
    192 
    193 	__cpu_simple_unlock(&mutex->ptm_lock);
    194 #ifdef ERRORCHECK
    195 	mutex->ptm_owner = NULL;
    196 #endif
    197 	if (__predict_false(!PTQ_EMPTY(&mutex->ptm_blocked)))
    198 		pthread_mutex_unlock_slow(mutex);
    199 
    200 	return 0;
    201 }
    202 
    203 static void
    204 pthread_mutex_unlock_slow(pthread_mutex_t *mutex)
    205 {
    206 	pthread_t self, blocked;
    207 
    208 	self = pthread__self();
    209 
    210 	pthread_spinlock(self, &mutex->ptm_interlock);
    211 	blocked = PTQ_FIRST(&mutex->ptm_blocked);
    212 	if (blocked)
    213 		PTQ_REMOVE(&mutex->ptm_blocked, blocked, pt_sleep);
    214 	pthread_spinunlock(self, &mutex->ptm_interlock);
    215 
    216 	/* Give the head of the blocked queue another try. */
    217 	if (blocked)
    218 		pthread__sched(self, blocked);
    219 }
    220 
    221 int
    222 pthread_mutexattr_init(pthread_mutexattr_t *attr)
    223 {
    224 
    225 #ifdef ERRORCHECK
    226 	if (attr == NULL)
    227 		return EINVAL;
    228 #endif
    229 
    230 	attr->ptma_magic = _PT_MUTEXATTR_MAGIC;
    231 
    232 	return 0;
    233 }
    234 
    235 
    236 int
    237 pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
    238 {
    239 
    240 #ifdef ERRORCHECK
    241 	if ((attr == NULL) ||
    242 	    (attr->ptma_magic != _PT_MUTEXATTR_MAGIC))
    243 		return EINVAL;
    244 #endif
    245 
    246 	attr->ptma_magic = _PT_MUTEXATTR_DEAD;
    247 
    248 	return 0;
    249 }
    250 
    251 
    252 int
    253 pthread_once(pthread_once_t *once_control, void (*routine)(void))
    254 {
    255 
    256 	if (once_control->pto_done == 0) {
    257 		pthread_mutex_lock(&once_control->pto_mutex);
    258 		if (once_control->pto_done == 0) {
    259 			routine();
    260 			once_control->pto_done = 1;
    261 		}
    262 		pthread_mutex_unlock(&once_control->pto_mutex);
    263 	}
    264 
    265 	return 0;
    266 }
    267