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