Home | History | Annotate | Line # | Download | only in libpthread
pthread_mutex.c revision 1.1.2.3
      1 /*	$NetBSD: pthread_mutex.c,v 1.1.2.3 2001/07/13 02:42:38 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 <signal.h>
     42 #include <stdlib.h>
     43 #include <ucontext.h>
     44 #include <sys/queue.h>
     45 
     46 #include "pthread.h"
     47 #include "pthread_int.h"
     48 
     49 
     50 int
     51 pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
     52 {
     53 
     54 	assert(mutex != NULL);
     55 
     56 	/* XXX No mutex attr support yet. */
     57 	if (attr != NULL)
     58 		return EINVAL;
     59 
     60 	/* Allocate. */
     61 
     62 	mutex->ptm_magic = PT_MUTEX_MAGIC;
     63 	mutex->ptm_owner = NULL;
     64 	pthread_lockinit(&mutex->ptm_lock);
     65 	pthread_lockinit(&mutex->ptm_interlock);
     66 	PTQ_INIT(&mutex->ptm_blocked);
     67 
     68 	return 0;
     69 }
     70 
     71 
     72 int
     73 pthread_mutex_destroy(pthread_mutex_t *mutex)
     74 {
     75 
     76 	assert(mutex != NULL);
     77 	assert(mutex->ptm_lock == __SIMPLELOCK_UNLOCKED);
     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 	pthread_t self;
    101 #ifdef ERRORCHECK
    102 	if ((mutex == NULL) || (mutex->ptm_magic != PT_MUTEX_MAGIC))
    103 		return EINVAL;
    104 #endif
    105 
    106 	while (/*CONSTCOND*/1) {
    107 		if (__cpu_simple_lock_try(&mutex->ptm_lock))
    108 		    break; /* got it! */
    109 
    110 		self = pthread__self();
    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 			self->pt_state = PT_STATE_BLOCKED;
    122 			pthread__block(self, &mutex->ptm_interlock);
    123 			/* interlock is not held when we return */
    124 		} else {
    125 			pthread_spinunlock(self, &mutex->ptm_interlock);
    126 		}
    127 		/* Go around for another try. */
    128 	}
    129 
    130 	/* We have the lock! */
    131 #ifdef ERRORCHECK
    132 	mutex->ptm_owner = self;
    133 #endif
    134 	return 0;
    135 }
    136 
    137 
    138 int
    139 pthread_mutex_trylock(pthread_mutex_t *mutex)
    140 {
    141 
    142 #ifdef ERRORCHECK
    143 	if ((mutex == NULL) || (mutex->ptm_magic != PT_MUTEX_MAGIC))
    144 		return EINVAL;
    145 #endif
    146 
    147 	if (__cpu_simple_lock_try(&mutex->ptm_lock) == 0)
    148 		return EBUSY;
    149 
    150 #ifdef ERRORCHECK
    151 	mutex->ptm_owner = pthread__self();
    152 #endif
    153 	return 0;
    154 }
    155 
    156 
    157 int
    158 pthread_mutex_unlock(pthread_mutex_t *mutex)
    159 {
    160 	pthread_t self, blocked;
    161 	struct pt_queue_t blockedq, nullq = PTQ_HEAD_INITIALIZER;
    162 
    163 #ifdef ERRORCHECK
    164 	if ((mutex == NULL) || (mutex->ptm_magic != PT_MUTEX_MAGIC))
    165 		return EINVAL;
    166 
    167 	if (mutex->ptm_lock != __SIMPLELOCK_LOCKED)
    168 		return EPERM; /* Not exactly the right error. */
    169 
    170 	/* One is only permitted to unlock one's own mutexes. */
    171 	if (mutex->ptm_owner != self)
    172 		return EPERM;
    173 #endif
    174 
    175 	self = pthread__self();
    176 	pthread_spinlock(self, &mutex->ptm_interlock);
    177        	blockedq = mutex->ptm_blocked;
    178 	mutex->ptm_blocked = nullq;
    179 #ifdef ERRORCHECK
    180 	mutex->ptm_owner = NULL;
    181 #endif
    182 	__cpu_simple_unlock(&mutex->ptm_lock);
    183 	pthread_spinunlock(self, &mutex->ptm_interlock);
    184 
    185 	/* Give everyone on the sleep queue another chance at the lock. */
    186 	PTQ_FOREACH(blocked, &blockedq, pt_sleep)
    187 		pthread__sched(self, blocked);
    188 
    189 	return 0;
    190 }
    191