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