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