Home | History | Annotate | Line # | Download | only in libpthread
pthread_lock.c revision 1.21.2.2
      1  1.21.2.2  ad /*	$NetBSD: pthread_lock.c,v 1.21.2.2 2007/08/04 13:37:50 ad Exp $	*/
      2  1.21.2.2  ad 
      3  1.21.2.2  ad /*-
      4  1.21.2.2  ad  * Copyright (c) 2001, 2006, 2007 The NetBSD Foundation, Inc.
      5  1.21.2.2  ad  * All rights reserved.
      6  1.21.2.2  ad  *
      7  1.21.2.2  ad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.21.2.2  ad  * by Nathan J. Williams and Andrew Doran.
      9  1.21.2.2  ad  *
     10  1.21.2.2  ad  * Redistribution and use in source and binary forms, with or without
     11  1.21.2.2  ad  * modification, are permitted provided that the following conditions
     12  1.21.2.2  ad  * are met:
     13  1.21.2.2  ad  * 1. Redistributions of source code must retain the above copyright
     14  1.21.2.2  ad  *    notice, this list of conditions and the following disclaimer.
     15  1.21.2.2  ad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.21.2.2  ad  *    notice, this list of conditions and the following disclaimer in the
     17  1.21.2.2  ad  *    documentation and/or other materials provided with the distribution.
     18  1.21.2.2  ad  * 3. All advertising materials mentioning features or use of this software
     19  1.21.2.2  ad  *    must display the following acknowledgement:
     20  1.21.2.2  ad  *        This product includes software developed by the NetBSD
     21  1.21.2.2  ad  *        Foundation, Inc. and its contributors.
     22  1.21.2.2  ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.21.2.2  ad  *    contributors may be used to endorse or promote products derived
     24  1.21.2.2  ad  *    from this software without specific prior written permission.
     25  1.21.2.2  ad  *
     26  1.21.2.2  ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.21.2.2  ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.21.2.2  ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.21.2.2  ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.21.2.2  ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.21.2.2  ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.21.2.2  ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.21.2.2  ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.21.2.2  ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.21.2.2  ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.21.2.2  ad  * POSSIBILITY OF SUCH DAMAGE.
     37  1.21.2.2  ad  */
     38  1.21.2.2  ad 
     39  1.21.2.2  ad #include <sys/cdefs.h>
     40  1.21.2.2  ad __RCSID("$NetBSD: pthread_lock.c,v 1.21.2.2 2007/08/04 13:37:50 ad Exp $");
     41  1.21.2.2  ad 
     42  1.21.2.2  ad #include <sys/types.h>
     43  1.21.2.2  ad #include <sys/lock.h>
     44  1.21.2.2  ad #include <sys/ras.h>
     45  1.21.2.2  ad 
     46  1.21.2.2  ad #include <errno.h>
     47  1.21.2.2  ad #include <unistd.h>
     48  1.21.2.2  ad #include <stdio.h>
     49  1.21.2.2  ad 
     50  1.21.2.2  ad #include "pthread.h"
     51  1.21.2.2  ad #include "pthread_int.h"
     52  1.21.2.2  ad 
     53  1.21.2.2  ad #ifdef PTHREAD_SPIN_DEBUG_PRINT
     54  1.21.2.2  ad #define SDPRINTF(x) DPRINTF(x)
     55  1.21.2.2  ad #else
     56  1.21.2.2  ad #define SDPRINTF(x)
     57  1.21.2.2  ad #endif
     58  1.21.2.2  ad 
     59  1.21.2.2  ad static int pthread__atomic;
     60  1.21.2.2  ad 
     61  1.21.2.2  ad RAS_DECL(pthread__lock);
     62  1.21.2.2  ad 
     63  1.21.2.2  ad void
     64  1.21.2.2  ad pthread__simple_lock_init(__cpu_simple_lock_t *alp)
     65  1.21.2.2  ad {
     66  1.21.2.2  ad 
     67  1.21.2.2  ad 	if (pthread__atomic) {
     68  1.21.2.2  ad 		__cpu_simple_lock_init(alp);
     69  1.21.2.2  ad 		return;
     70  1.21.2.2  ad 	}
     71  1.21.2.2  ad 
     72  1.21.2.2  ad 	*alp = __SIMPLELOCK_UNLOCKED;
     73  1.21.2.2  ad }
     74  1.21.2.2  ad 
     75  1.21.2.2  ad int
     76  1.21.2.2  ad pthread__simple_lock_try(__cpu_simple_lock_t *alp)
     77  1.21.2.2  ad {
     78  1.21.2.2  ad 	__cpu_simple_lock_t old;
     79  1.21.2.2  ad 
     80  1.21.2.2  ad 	if (pthread__atomic)
     81  1.21.2.2  ad 		return __cpu_simple_lock_try(alp);
     82  1.21.2.2  ad 
     83  1.21.2.2  ad 	RAS_START(pthread__lock);
     84  1.21.2.2  ad 	old = *alp;
     85  1.21.2.2  ad 	*alp = __SIMPLELOCK_LOCKED;
     86  1.21.2.2  ad 	RAS_END(pthread__lock);
     87  1.21.2.2  ad 
     88  1.21.2.2  ad 	return old == __SIMPLELOCK_UNLOCKED;
     89  1.21.2.2  ad }
     90  1.21.2.2  ad 
     91  1.21.2.2  ad inline void
     92  1.21.2.2  ad pthread__simple_unlock(__cpu_simple_lock_t *alp)
     93  1.21.2.2  ad {
     94  1.21.2.2  ad 
     95  1.21.2.2  ad 	if (pthread__atomic) {
     96  1.21.2.2  ad 		__cpu_simple_unlock(alp);
     97  1.21.2.2  ad 		return;
     98  1.21.2.2  ad 	}
     99  1.21.2.2  ad 
    100  1.21.2.2  ad 	*alp = __SIMPLELOCK_UNLOCKED;
    101  1.21.2.2  ad }
    102  1.21.2.2  ad 
    103  1.21.2.2  ad /*
    104  1.21.2.2  ad  * Initialize the locking primitives.  On uniprocessors, we always
    105  1.21.2.2  ad  * use Restartable Atomic Sequences if they are available.  Otherwise,
    106  1.21.2.2  ad  * we fall back onto machine-dependent atomic lock primitives.
    107  1.21.2.2  ad  */
    108  1.21.2.2  ad void
    109  1.21.2.2  ad pthread__lockprim_init(int ncpu)
    110  1.21.2.2  ad {
    111  1.21.2.2  ad 
    112  1.21.2.2  ad 	if (ncpu != 1) {
    113  1.21.2.2  ad 		pthread__atomic = 1;
    114  1.21.2.2  ad 		return;
    115  1.21.2.2  ad 	}
    116  1.21.2.2  ad 
    117  1.21.2.2  ad 	if (rasctl(RAS_ADDR(pthread__lock), RAS_SIZE(pthread__lock),
    118  1.21.2.2  ad 	    RAS_INSTALL) != 0) {
    119  1.21.2.2  ad 	    	pthread__atomic = 1;
    120  1.21.2.2  ad 	    	return;
    121  1.21.2.2  ad 	}
    122  1.21.2.2  ad }
    123  1.21.2.2  ad 
    124  1.21.2.2  ad void
    125  1.21.2.2  ad pthread_lockinit(pthread_spin_t *lock)
    126  1.21.2.2  ad {
    127  1.21.2.2  ad 
    128  1.21.2.2  ad 	pthread__simple_lock_init(lock);
    129  1.21.2.2  ad }
    130  1.21.2.2  ad 
    131  1.21.2.2  ad void
    132  1.21.2.2  ad pthread_spinlock(pthread_t thread, pthread_spin_t *lock)
    133  1.21.2.2  ad {
    134  1.21.2.2  ad 	int count;
    135  1.21.2.2  ad 
    136  1.21.2.2  ad 	SDPRINTF(("(pthread_spinlock %p) spinlock %p (count %d)\n",
    137  1.21.2.2  ad 	    thread, lock, thread->pt_spinlocks));
    138  1.21.2.2  ad #ifdef PTHREAD_SPIN_DEBUG
    139  1.21.2.2  ad 	pthread__assert(thread->pt_spinlocks >= 0);
    140  1.21.2.2  ad #endif
    141  1.21.2.2  ad 
    142  1.21.2.2  ad 	thread->pt_spinlocks++;
    143  1.21.2.2  ad 	if (__predict_true(pthread__simple_lock_try(lock))) {
    144  1.21.2.2  ad 		PTHREADD_ADD(PTHREADD_SPINLOCKS);
    145  1.21.2.2  ad 		return;
    146  1.21.2.2  ad 	}
    147  1.21.2.2  ad 
    148  1.21.2.2  ad 	do {
    149  1.21.2.2  ad 		count = pthread__nspins;
    150  1.21.2.2  ad 		while (*lock == __SIMPLELOCK_LOCKED && --count > 0)
    151  1.21.2.2  ad 			pthread__smt_pause();
    152  1.21.2.2  ad 		if (count > 0) {
    153  1.21.2.2  ad 			if (pthread__simple_lock_try(lock))
    154  1.21.2.2  ad 				break;
    155  1.21.2.2  ad 			continue;
    156  1.21.2.2  ad 		}
    157  1.21.2.2  ad 
    158  1.21.2.2  ad 		SDPRINTF(("(pthread_spinlock %p) retrying spinlock %p "
    159  1.21.2.2  ad 		    "(count %d)\n", thread, lock,
    160  1.21.2.2  ad 		    thread->pt_spinlocks));
    161  1.21.2.2  ad 		thread->pt_spinlocks--;
    162  1.21.2.2  ad 
    163  1.21.2.2  ad 		/* XXXLWP far from ideal */
    164  1.21.2.2  ad 		sched_yield();
    165  1.21.2.2  ad 		thread->pt_spinlocks++;
    166  1.21.2.2  ad 	} while (/*CONSTCOND*/ 1);
    167  1.21.2.2  ad 
    168  1.21.2.2  ad 	PTHREADD_ADD(PTHREADD_SPINLOCKS);
    169  1.21.2.2  ad }
    170  1.21.2.2  ad 
    171  1.21.2.2  ad int
    172  1.21.2.2  ad pthread_spintrylock(pthread_t thread, pthread_spin_t *lock)
    173  1.21.2.2  ad {
    174  1.21.2.2  ad 	int ret;
    175  1.21.2.2  ad 
    176  1.21.2.2  ad 	SDPRINTF(("(pthread_spintrylock %p) spinlock %p (count %d)\n",
    177  1.21.2.2  ad 	    thread, lock, thread->pt_spinlocks));
    178  1.21.2.2  ad 
    179  1.21.2.2  ad 	thread->pt_spinlocks++;
    180  1.21.2.2  ad 	ret = pthread__simple_lock_try(lock);
    181  1.21.2.2  ad 	if (!ret)
    182  1.21.2.2  ad 		thread->pt_spinlocks--;
    183  1.21.2.2  ad 
    184  1.21.2.2  ad 	return ret;
    185  1.21.2.2  ad }
    186  1.21.2.2  ad 
    187  1.21.2.2  ad void
    188  1.21.2.2  ad pthread_spinunlock(pthread_t thread, pthread_spin_t *lock)
    189  1.21.2.2  ad {
    190  1.21.2.2  ad 
    191  1.21.2.2  ad 	SDPRINTF(("(pthread_spinunlock %p) spinlock %p (count %d)\n",
    192  1.21.2.2  ad 	    thread, lock, thread->pt_spinlocks));
    193  1.21.2.2  ad 
    194  1.21.2.2  ad 	pthread__simple_unlock(lock);
    195  1.21.2.2  ad 	thread->pt_spinlocks--;
    196  1.21.2.2  ad #ifdef PTHREAD_SPIN_DEBUG
    197  1.21.2.2  ad 	pthread__assert(thread->pt_spinlocks >= 0);
    198  1.21.2.2  ad #endif
    199  1.21.2.2  ad 	PTHREADD_ADD(PTHREADD_SPINUNLOCKS);
    200  1.21.2.2  ad }
    201  1.21.2.2  ad 
    202  1.21.2.2  ad 
    203  1.21.2.2  ad /*
    204  1.21.2.2  ad  * Public (POSIX-specified) spinlocks.
    205  1.21.2.2  ad  */
    206  1.21.2.2  ad int
    207  1.21.2.2  ad pthread_spin_init(pthread_spinlock_t *lock, int pshared)
    208  1.21.2.2  ad {
    209  1.21.2.2  ad 
    210  1.21.2.2  ad #ifdef ERRORCHECK
    211  1.21.2.2  ad 	if (lock == NULL || (pshared != PTHREAD_PROCESS_PRIVATE &&
    212  1.21.2.2  ad 	    pshared != PTHREAD_PROCESS_SHARED))
    213  1.21.2.2  ad 		return EINVAL;
    214  1.21.2.2  ad #endif
    215  1.21.2.2  ad 	lock->pts_magic = _PT_SPINLOCK_MAGIC;
    216  1.21.2.2  ad 
    217  1.21.2.2  ad 	/*
    218  1.21.2.2  ad 	 * We don't actually use the pshared flag for anything;
    219  1.21.2.2  ad 	 * CPU simple locks have all the process-shared properties
    220  1.21.2.2  ad 	 * that we want anyway.
    221  1.21.2.2  ad 	 */
    222  1.21.2.2  ad 	lock->pts_flags = pshared;
    223  1.21.2.2  ad 	pthread_lockinit(&lock->pts_spin);
    224  1.21.2.2  ad 
    225  1.21.2.2  ad 	return 0;
    226  1.21.2.2  ad }
    227  1.21.2.2  ad 
    228  1.21.2.2  ad int
    229  1.21.2.2  ad pthread_spin_destroy(pthread_spinlock_t *lock)
    230  1.21.2.2  ad {
    231  1.21.2.2  ad 
    232  1.21.2.2  ad #ifdef ERRORCHECK
    233  1.21.2.2  ad 	if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
    234  1.21.2.2  ad 		return EINVAL;
    235  1.21.2.2  ad 	if (lock->pts_spin != __SIMPLELOCK_UNLOCKED)
    236  1.21.2.2  ad 		return EBUSY;
    237  1.21.2.2  ad #endif
    238  1.21.2.2  ad 
    239  1.21.2.2  ad 	lock->pts_magic = _PT_SPINLOCK_DEAD;
    240  1.21.2.2  ad 
    241  1.21.2.2  ad 	return 0;
    242  1.21.2.2  ad }
    243  1.21.2.2  ad 
    244  1.21.2.2  ad int
    245  1.21.2.2  ad pthread_spin_lock(pthread_spinlock_t *lock)
    246  1.21.2.2  ad {
    247  1.21.2.2  ad 
    248  1.21.2.2  ad #ifdef ERRORCHECK
    249  1.21.2.2  ad 	if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
    250  1.21.2.2  ad 		return EINVAL;
    251  1.21.2.2  ad #endif
    252  1.21.2.2  ad 
    253  1.21.2.2  ad 	while (pthread__simple_lock_try(&lock->pts_spin) == 0) {
    254  1.21.2.2  ad 		pthread__smt_pause();
    255  1.21.2.2  ad 	}
    256  1.21.2.2  ad 
    257  1.21.2.2  ad 	return 0;
    258  1.21.2.2  ad }
    259  1.21.2.2  ad 
    260  1.21.2.2  ad int
    261  1.21.2.2  ad pthread_spin_trylock(pthread_spinlock_t *lock)
    262  1.21.2.2  ad {
    263  1.21.2.2  ad 
    264  1.21.2.2  ad #ifdef ERRORCHECK
    265  1.21.2.2  ad 	if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
    266  1.21.2.2  ad 		return EINVAL;
    267  1.21.2.2  ad #endif
    268  1.21.2.2  ad 
    269  1.21.2.2  ad 	if (pthread__simple_lock_try(&lock->pts_spin) == 0)
    270  1.21.2.2  ad 		return EBUSY;
    271  1.21.2.2  ad 
    272  1.21.2.2  ad 	return 0;
    273  1.21.2.2  ad }
    274  1.21.2.2  ad 
    275  1.21.2.2  ad int
    276  1.21.2.2  ad pthread_spin_unlock(pthread_spinlock_t *lock)
    277  1.21.2.2  ad {
    278  1.21.2.2  ad 
    279  1.21.2.2  ad #ifdef ERRORCHECK
    280  1.21.2.2  ad 	if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
    281  1.21.2.2  ad 		return EINVAL;
    282  1.21.2.2  ad #endif
    283  1.21.2.2  ad 
    284  1.21.2.2  ad 	pthread__simple_unlock(&lock->pts_spin);
    285  1.21.2.2  ad 
    286  1.21.2.2  ad 	return 0;
    287  1.21.2.2  ad }
    288