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