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