Home | History | Annotate | Line # | Download | only in libpthread
pthread_mutex.c revision 1.1.2.3
      1  1.1.2.3  nathanw /*	$NetBSD: pthread_mutex.c,v 1.1.2.3 2001/07/13 02:42:38 nathanw Exp $	*/
      2  1.1.2.3  nathanw 
      3  1.1.2.3  nathanw /*-
      4  1.1.2.3  nathanw  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  1.1.2.3  nathanw  * All rights reserved.
      6  1.1.2.3  nathanw  *
      7  1.1.2.3  nathanw  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1.2.3  nathanw  * by Nathan J. Williams.
      9  1.1.2.3  nathanw  *
     10  1.1.2.3  nathanw  * Redistribution and use in source and binary forms, with or without
     11  1.1.2.3  nathanw  * modification, are permitted provided that the following conditions
     12  1.1.2.3  nathanw  * are met:
     13  1.1.2.3  nathanw  * 1. Redistributions of source code must retain the above copyright
     14  1.1.2.3  nathanw  *    notice, this list of conditions and the following disclaimer.
     15  1.1.2.3  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1.2.3  nathanw  *    notice, this list of conditions and the following disclaimer in the
     17  1.1.2.3  nathanw  *    documentation and/or other materials provided with the distribution.
     18  1.1.2.3  nathanw  * 3. All advertising materials mentioning features or use of this software
     19  1.1.2.3  nathanw  *    must display the following acknowledgement:
     20  1.1.2.3  nathanw  *        This product includes software developed by the NetBSD
     21  1.1.2.3  nathanw  *        Foundation, Inc. and its contributors.
     22  1.1.2.3  nathanw  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1.2.3  nathanw  *    contributors may be used to endorse or promote products derived
     24  1.1.2.3  nathanw  *    from this software without specific prior written permission.
     25  1.1.2.3  nathanw  *
     26  1.1.2.3  nathanw  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1.2.3  nathanw  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1.2.3  nathanw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1.2.3  nathanw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1.2.3  nathanw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1.2.3  nathanw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1.2.3  nathanw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1.2.3  nathanw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1.2.3  nathanw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1.2.3  nathanw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1.2.3  nathanw  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1.2.3  nathanw  */
     38  1.1.2.1  nathanw 
     39  1.1.2.1  nathanw #include <assert.h>
     40  1.1.2.1  nathanw #include <errno.h>
     41  1.1.2.1  nathanw #include <signal.h>
     42  1.1.2.1  nathanw #include <stdlib.h>
     43  1.1.2.1  nathanw #include <ucontext.h>
     44  1.1.2.1  nathanw #include <sys/queue.h>
     45  1.1.2.1  nathanw 
     46  1.1.2.1  nathanw #include "pthread.h"
     47  1.1.2.1  nathanw #include "pthread_int.h"
     48  1.1.2.1  nathanw 
     49  1.1.2.1  nathanw 
     50  1.1.2.1  nathanw int
     51  1.1.2.1  nathanw pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
     52  1.1.2.1  nathanw {
     53  1.1.2.1  nathanw 
     54  1.1.2.1  nathanw 	assert(mutex != NULL);
     55  1.1.2.1  nathanw 
     56  1.1.2.1  nathanw 	/* XXX No mutex attr support yet. */
     57  1.1.2.1  nathanw 	if (attr != NULL)
     58  1.1.2.1  nathanw 		return EINVAL;
     59  1.1.2.1  nathanw 
     60  1.1.2.1  nathanw 	/* Allocate. */
     61  1.1.2.1  nathanw 
     62  1.1.2.2  nathanw 	mutex->ptm_magic = PT_MUTEX_MAGIC;
     63  1.1.2.2  nathanw 	mutex->ptm_owner = NULL;
     64  1.1.2.2  nathanw 	pthread_lockinit(&mutex->ptm_lock);
     65  1.1.2.2  nathanw 	pthread_lockinit(&mutex->ptm_interlock);
     66  1.1.2.2  nathanw 	PTQ_INIT(&mutex->ptm_blocked);
     67  1.1.2.1  nathanw 
     68  1.1.2.1  nathanw 	return 0;
     69  1.1.2.1  nathanw }
     70  1.1.2.1  nathanw 
     71  1.1.2.1  nathanw 
     72  1.1.2.1  nathanw int
     73  1.1.2.1  nathanw pthread_mutex_destroy(pthread_mutex_t *mutex)
     74  1.1.2.1  nathanw {
     75  1.1.2.1  nathanw 
     76  1.1.2.2  nathanw 	assert(mutex != NULL);
     77  1.1.2.2  nathanw 	assert(mutex->ptm_lock == __SIMPLELOCK_UNLOCKED);
     78  1.1.2.1  nathanw 
     79  1.1.2.2  nathanw 	mutex->ptm_magic = PT_MUTEX_DEAD;
     80  1.1.2.1  nathanw 
     81  1.1.2.1  nathanw 	return 0;
     82  1.1.2.1  nathanw }
     83  1.1.2.1  nathanw 
     84  1.1.2.1  nathanw 
     85  1.1.2.2  nathanw /*
     86  1.1.2.2  nathanw  * Note regarding memory visibility: Pthreads has rules about memory
     87  1.1.2.2  nathanw  * visibility and mutexes. Very roughly: Memory a thread can see when
     88  1.1.2.2  nathanw  * it unlocks a mutex can be seen by another thread that locks the
     89  1.1.2.2  nathanw  * same mutex.
     90  1.1.2.2  nathanw  *
     91  1.1.2.2  nathanw  * A memory barrier after a lock and before an unlock will provide
     92  1.1.2.2  nathanw  * this behavior. This code relies on __cpu_simple_lock_try() to issue
     93  1.1.2.2  nathanw  * a barrier after obtaining a lock, and on __cpu_simple_unlock() to
     94  1.1.2.2  nathanw  * issue a barrier before releasing a lock.
     95  1.1.2.2  nathanw  */
     96  1.1.2.2  nathanw 
     97  1.1.2.1  nathanw int
     98  1.1.2.1  nathanw pthread_mutex_lock(pthread_mutex_t *mutex)
     99  1.1.2.1  nathanw {
    100  1.1.2.1  nathanw 	pthread_t self;
    101  1.1.2.2  nathanw #ifdef ERRORCHECK
    102  1.1.2.2  nathanw 	if ((mutex == NULL) || (mutex->ptm_magic != PT_MUTEX_MAGIC))
    103  1.1.2.1  nathanw 		return EINVAL;
    104  1.1.2.2  nathanw #endif
    105  1.1.2.1  nathanw 
    106  1.1.2.2  nathanw 	while (/*CONSTCOND*/1) {
    107  1.1.2.2  nathanw 		if (__cpu_simple_lock_try(&mutex->ptm_lock))
    108  1.1.2.2  nathanw 		    break; /* got it! */
    109  1.1.2.1  nathanw 
    110  1.1.2.2  nathanw 		self = pthread__self();
    111  1.1.2.2  nathanw 		/* Okay, didn't look free. Get the interlock... */
    112  1.1.2.2  nathanw 		pthread_spinlock(self, &mutex->ptm_interlock);
    113  1.1.2.2  nathanw 		/* The mutex_unlock routine will get the interlock
    114  1.1.2.2  nathanw 		 * before looking at the list of sleepers, so if the
    115  1.1.2.2  nathanw 		 * lock is held we can safely put ourselves on the
    116  1.1.2.2  nathanw 		 * sleep queue. If it's not held, we can try taking it
    117  1.1.2.2  nathanw 		 * again.
    118  1.1.2.2  nathanw 		 */
    119  1.1.2.2  nathanw 		if (mutex->ptm_lock == __SIMPLELOCK_LOCKED) {
    120  1.1.2.2  nathanw 			PTQ_INSERT_TAIL(&mutex->ptm_blocked, self, pt_sleep);
    121  1.1.2.2  nathanw 			self->pt_state = PT_STATE_BLOCKED;
    122  1.1.2.2  nathanw 			pthread__block(self, &mutex->ptm_interlock);
    123  1.1.2.2  nathanw 			/* interlock is not held when we return */
    124  1.1.2.2  nathanw 		} else {
    125  1.1.2.2  nathanw 			pthread_spinunlock(self, &mutex->ptm_interlock);
    126  1.1.2.2  nathanw 		}
    127  1.1.2.2  nathanw 		/* Go around for another try. */
    128  1.1.2.1  nathanw 	}
    129  1.1.2.1  nathanw 
    130  1.1.2.2  nathanw 	/* We have the lock! */
    131  1.1.2.2  nathanw #ifdef ERRORCHECK
    132  1.1.2.2  nathanw 	mutex->ptm_owner = self;
    133  1.1.2.2  nathanw #endif
    134  1.1.2.1  nathanw 	return 0;
    135  1.1.2.1  nathanw }
    136  1.1.2.1  nathanw 
    137  1.1.2.1  nathanw 
    138  1.1.2.1  nathanw int
    139  1.1.2.1  nathanw pthread_mutex_trylock(pthread_mutex_t *mutex)
    140  1.1.2.1  nathanw {
    141  1.1.2.1  nathanw 
    142  1.1.2.2  nathanw #ifdef ERRORCHECK
    143  1.1.2.2  nathanw 	if ((mutex == NULL) || (mutex->ptm_magic != PT_MUTEX_MAGIC))
    144  1.1.2.1  nathanw 		return EINVAL;
    145  1.1.2.2  nathanw #endif
    146  1.1.2.1  nathanw 
    147  1.1.2.2  nathanw 	if (__cpu_simple_lock_try(&mutex->ptm_lock) == 0)
    148  1.1.2.1  nathanw 		return EBUSY;
    149  1.1.2.1  nathanw 
    150  1.1.2.2  nathanw #ifdef ERRORCHECK
    151  1.1.2.2  nathanw 	mutex->ptm_owner = pthread__self();
    152  1.1.2.2  nathanw #endif
    153  1.1.2.1  nathanw 	return 0;
    154  1.1.2.1  nathanw }
    155  1.1.2.1  nathanw 
    156  1.1.2.1  nathanw 
    157  1.1.2.1  nathanw int
    158  1.1.2.1  nathanw pthread_mutex_unlock(pthread_mutex_t *mutex)
    159  1.1.2.1  nathanw {
    160  1.1.2.1  nathanw 	pthread_t self, blocked;
    161  1.1.2.2  nathanw 	struct pt_queue_t blockedq, nullq = PTQ_HEAD_INITIALIZER;
    162  1.1.2.1  nathanw 
    163  1.1.2.2  nathanw #ifdef ERRORCHECK
    164  1.1.2.2  nathanw 	if ((mutex == NULL) || (mutex->ptm_magic != PT_MUTEX_MAGIC))
    165  1.1.2.1  nathanw 		return EINVAL;
    166  1.1.2.1  nathanw 
    167  1.1.2.2  nathanw 	if (mutex->ptm_lock != __SIMPLELOCK_LOCKED)
    168  1.1.2.1  nathanw 		return EPERM; /* Not exactly the right error. */
    169  1.1.2.1  nathanw 
    170  1.1.2.1  nathanw 	/* One is only permitted to unlock one's own mutexes. */
    171  1.1.2.2  nathanw 	if (mutex->ptm_owner != self)
    172  1.1.2.1  nathanw 		return EPERM;
    173  1.1.2.2  nathanw #endif
    174  1.1.2.1  nathanw 
    175  1.1.2.2  nathanw 	self = pthread__self();
    176  1.1.2.2  nathanw 	pthread_spinlock(self, &mutex->ptm_interlock);
    177  1.1.2.2  nathanw        	blockedq = mutex->ptm_blocked;
    178  1.1.2.2  nathanw 	mutex->ptm_blocked = nullq;
    179  1.1.2.2  nathanw #ifdef ERRORCHECK
    180  1.1.2.2  nathanw 	mutex->ptm_owner = NULL;
    181  1.1.2.2  nathanw #endif
    182  1.1.2.2  nathanw 	__cpu_simple_unlock(&mutex->ptm_lock);
    183  1.1.2.2  nathanw 	pthread_spinunlock(self, &mutex->ptm_interlock);
    184  1.1.2.1  nathanw 
    185  1.1.2.2  nathanw 	/* Give everyone on the sleep queue another chance at the lock. */
    186  1.1.2.2  nathanw 	PTQ_FOREACH(blocked, &blockedq, pt_sleep)
    187  1.1.2.1  nathanw 		pthread__sched(self, blocked);
    188  1.1.2.1  nathanw 
    189  1.1.2.1  nathanw 	return 0;
    190  1.1.2.1  nathanw }
    191