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