Home | History | Annotate | Line # | Download | only in libpthread
pthread_mutex.c revision 1.1.2.4
      1 /*	$NetBSD: pthread_mutex.c,v 1.1.2.4 2001/07/24 21:21: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 
     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 	assert(mutex != NULL);
     51 
     52 	/* XXX No mutex attr support yet. */
     53 	if (attr != NULL)
     54 		return EINVAL;
     55 
     56 	/* Allocate. */
     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 	assert(mutex != NULL);
     73 	assert(mutex->ptm_magic == _PT_MUTEX_MAGIC);
     74 	assert(mutex->ptm_lock == __SIMPLELOCK_UNLOCKED);
     75 
     76 	mutex->ptm_magic = _PT_MUTEX_DEAD;
     77 
     78 	return 0;
     79 }
     80 
     81 
     82 /*
     83  * Note regarding memory visibility: Pthreads has rules about memory
     84  * visibility and mutexes. Very roughly: Memory a thread can see when
     85  * it unlocks a mutex can be seen by another thread that locks the
     86  * same mutex.
     87  *
     88  * A memory barrier after a lock and before an unlock will provide
     89  * this behavior. This code relies on __cpu_simple_lock_try() to issue
     90  * a barrier after obtaining a lock, and on __cpu_simple_unlock() to
     91  * issue a barrier before releasing a lock.
     92  */
     93 
     94 int
     95 pthread_mutex_lock(pthread_mutex_t *mutex)
     96 {
     97 	pthread_t self;
     98 #ifdef ERRORCHECK
     99 	if ((mutex == NULL) || (mutex->ptm_magic != _PT_MUTEX_MAGIC))
    100 		return EINVAL;
    101 #endif
    102 
    103 	while (/*CONSTCOND*/1) {
    104 		if (__cpu_simple_lock_try(&mutex->ptm_lock))
    105 		    break; /* got it! */
    106 
    107 		self = pthread__self();
    108 		/* Okay, didn't look free. Get the interlock... */
    109 		pthread_spinlock(self, &mutex->ptm_interlock);
    110 		/* The mutex_unlock routine will get the interlock
    111 		 * before looking at the list of sleepers, so if the
    112 		 * lock is held we can safely put ourselves on the
    113 		 * sleep queue. If it's not held, we can try taking it
    114 		 * again.
    115 		 */
    116 		if (mutex->ptm_lock == __SIMPLELOCK_LOCKED) {
    117 			PTQ_INSERT_TAIL(&mutex->ptm_blocked, self, pt_sleep);
    118 			self->pt_state = PT_STATE_BLOCKED;
    119 			pthread__block(self, &mutex->ptm_interlock);
    120 			/* interlock is not held when we return */
    121 		} else {
    122 			pthread_spinunlock(self, &mutex->ptm_interlock);
    123 		}
    124 		/* Go around for another try. */
    125 	}
    126 
    127 	/* We have the lock! */
    128 #ifdef ERRORCHECK
    129 	mutex->ptm_owner = self;
    130 #endif
    131 	return 0;
    132 }
    133 
    134 
    135 int
    136 pthread_mutex_trylock(pthread_mutex_t *mutex)
    137 {
    138 
    139 #ifdef ERRORCHECK
    140 	if ((mutex == NULL) || (mutex->ptm_magic != _PT_MUTEX_MAGIC))
    141 		return EINVAL;
    142 #endif
    143 
    144 	if (__cpu_simple_lock_try(&mutex->ptm_lock) == 0)
    145 		return EBUSY;
    146 
    147 #ifdef ERRORCHECK
    148 	mutex->ptm_owner = pthread__self();
    149 #endif
    150 	return 0;
    151 }
    152 
    153 
    154 int
    155 pthread_mutex_unlock(pthread_mutex_t *mutex)
    156 {
    157 	pthread_t self, blocked;
    158 
    159 #ifdef ERRORCHECK
    160 	if ((mutex == NULL) || (mutex->ptm_magic != _PT_MUTEX_MAGIC))
    161 		return EINVAL;
    162 
    163 	if (mutex->ptm_lock != __SIMPLELOCK_LOCKED)
    164 		return EPERM; /* Not exactly the right error. */
    165 
    166 	/* One is only permitted to unlock one's own mutexes. */
    167 	if (mutex->ptm_owner != self)
    168 		return EPERM;
    169 #endif
    170 
    171 	self = pthread__self();
    172 	pthread_spinlock(self, &mutex->ptm_interlock);
    173 	blocked = PTQ_FIRST(&mutex->ptm_blocked);
    174 	if (blocked)
    175 		PTQ_REMOVE(&mutex->ptm_blocked, blocked, pt_sleep);
    176 #ifdef ERRORCHECK
    177 	mutex->ptm_owner = NULL;
    178 #endif
    179 	__cpu_simple_unlock(&mutex->ptm_lock);
    180 	pthread_spinunlock(self, &mutex->ptm_interlock);
    181 
    182 	/* Give the head of the blocked queue another try. */
    183 	if (blocked)
    184 		pthread__sched(self, blocked);
    185 
    186 	return 0;
    187 }
    188