Home | History | Annotate | Line # | Download | only in libpthread
pthread_lock.c revision 1.20
      1  1.20       ad /*	$NetBSD: pthread_lock.c,v 1.20 2007/03/24 18:52:00 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.6    lukem #include <sys/cdefs.h>
     40  1.20       ad __RCSID("$NetBSD: pthread_lock.c,v 1.20 2007/03/24 18:52:00 ad Exp $");
     41   1.2  thorpej 
     42  1.12       he #include <sys/types.h>
     43  1.11       cl #include <sys/lock.h>
     44   1.2  thorpej #include <sys/ras.h>
     45   1.2  thorpej 
     46   1.2  thorpej #include <errno.h>
     47   1.2  thorpej #include <unistd.h>
     48  1.19       ad #include <stdio.h>
     49   1.2  thorpej 
     50   1.2  thorpej #include "pthread.h"
     51   1.2  thorpej #include "pthread_int.h"
     52   1.2  thorpej 
     53   1.5  nathanw #ifdef PTHREAD_SPIN_DEBUG_PRINT
     54   1.2  thorpej #define SDPRINTF(x) DPRINTF(x)
     55   1.2  thorpej #else
     56   1.2  thorpej #define SDPRINTF(x)
     57   1.2  thorpej #endif
     58   1.2  thorpej 
     59  1.17       ad /* This does not belong here. */
     60  1.17       ad #if defined(i386) || defined(__x86_64__)
     61  1.17       ad #define	smt_pause()	__asm __volatile("rep; nop" ::: "memory")
     62  1.17       ad #else
     63  1.17       ad #define	smt_pause()	/* nothing */
     64  1.17       ad #endif
     65  1.17       ad 
     66  1.16       ad extern int pthread__nspins;
     67  1.20       ad static int pthread__atomic;
     68   1.2  thorpej 
     69  1.10  thorpej RAS_DECL(pthread__lock);
     70   1.2  thorpej 
     71  1.20       ad void
     72  1.20       ad pthread__simple_lock_init(__cpu_simple_lock_t *alp)
     73   1.2  thorpej {
     74   1.2  thorpej 
     75  1.20       ad 	if (pthread__atomic) {
     76  1.20       ad 		__cpu_simple_lock_init(alp);
     77  1.20       ad 		return;
     78  1.20       ad 	}
     79  1.20       ad 
     80   1.2  thorpej 	*alp = __SIMPLELOCK_UNLOCKED;
     81   1.2  thorpej }
     82   1.2  thorpej 
     83  1.20       ad int
     84  1.20       ad pthread__simple_lock_try(__cpu_simple_lock_t *alp)
     85   1.2  thorpej {
     86   1.2  thorpej 	__cpu_simple_lock_t old;
     87   1.2  thorpej 
     88  1.20       ad 	if (pthread__atomic)
     89  1.20       ad 		return __cpu_simple_lock_try(alp);
     90  1.20       ad 
     91  1.10  thorpej 	RAS_START(pthread__lock);
     92   1.2  thorpej 	old = *alp;
     93   1.2  thorpej 	*alp = __SIMPLELOCK_LOCKED;
     94  1.10  thorpej 	RAS_END(pthread__lock);
     95   1.2  thorpej 
     96  1.20       ad 	return old == __SIMPLELOCK_UNLOCKED;
     97   1.2  thorpej }
     98   1.2  thorpej 
     99  1.20       ad inline void
    100  1.20       ad pthread__simple_unlock(__cpu_simple_lock_t *alp)
    101   1.2  thorpej {
    102   1.2  thorpej 
    103  1.20       ad 	if (pthread__atomic) {
    104  1.20       ad 		__cpu_simple_unlock(alp);
    105  1.20       ad 		return;
    106  1.20       ad 	}
    107  1.20       ad 
    108   1.2  thorpej 	*alp = __SIMPLELOCK_UNLOCKED;
    109   1.2  thorpej }
    110   1.2  thorpej 
    111   1.2  thorpej /*
    112   1.2  thorpej  * Initialize the locking primitives.  On uniprocessors, we always
    113   1.2  thorpej  * use Restartable Atomic Sequences if they are available.  Otherwise,
    114   1.2  thorpej  * we fall back onto machine-dependent atomic lock primitives.
    115   1.2  thorpej  */
    116   1.2  thorpej void
    117  1.11       cl pthread__lockprim_init(int ncpu)
    118   1.2  thorpej {
    119  1.20       ad 
    120  1.20       ad 	if (ncpu != 1) {
    121  1.20       ad 		pthread__atomic = 1;
    122   1.2  thorpej 		return;
    123   1.2  thorpej 	}
    124   1.2  thorpej 
    125  1.20       ad 	if (rasctl(RAS_ADDR(pthread__lock), RAS_SIZE(pthread__lock),
    126  1.20       ad 	    RAS_INSTALL) != 0) {
    127  1.20       ad 	    	pthread__atomic = 1;
    128  1.20       ad 	    	return;
    129  1.20       ad 	}
    130   1.2  thorpej }
    131   1.2  thorpej 
    132   1.2  thorpej void
    133   1.2  thorpej pthread_lockinit(pthread_spin_t *lock)
    134   1.2  thorpej {
    135   1.2  thorpej 
    136   1.2  thorpej 	pthread__simple_lock_init(lock);
    137   1.2  thorpej }
    138   1.2  thorpej 
    139   1.2  thorpej void
    140   1.2  thorpej pthread_spinlock(pthread_t thread, pthread_spin_t *lock)
    141   1.2  thorpej {
    142   1.2  thorpej 	int count, ret;
    143   1.2  thorpej 
    144  1.16       ad 	count = pthread__nspins;
    145  1.19       ad 	SDPRINTF(("(pthread_spinlock %p) spinlock %p (count %d)\n",
    146  1.19       ad 	    thread, lock, thread->pt_spinlocks));
    147   1.2  thorpej #ifdef PTHREAD_SPIN_DEBUG
    148   1.5  nathanw 	pthread__assert(thread->pt_spinlocks >= 0);
    149   1.2  thorpej #endif
    150  1.19       ad 
    151  1.19       ad 	thread->pt_spinlocks++;
    152  1.19       ad 	if (__predict_true(pthread__simple_lock_try(lock))) {
    153  1.19       ad 		PTHREADD_ADD(PTHREADD_SPINLOCKS);
    154  1.19       ad 		return;
    155  1.19       ad 	}
    156   1.2  thorpej 
    157   1.2  thorpej 	do {
    158  1.19       ad 		while ((ret = pthread__simple_lock_try(lock)) == 0 &&
    159  1.19       ad 		    --count) {
    160  1.17       ad 			smt_pause();
    161  1.17       ad 		}
    162   1.2  thorpej 
    163   1.2  thorpej 		if (ret == 1)
    164   1.2  thorpej 			break;
    165   1.2  thorpej 
    166  1.19       ad 		SDPRINTF(("(pthread_spinlock %p) retrying spinlock %p "
    167  1.19       ad 		    "(count %d)\n", thread, lock,
    168  1.19       ad 		    thread->pt_spinlocks));
    169  1.19       ad 		thread->pt_spinlocks--;
    170  1.19       ad 
    171  1.16       ad 		/* XXXLWP far from ideal */
    172  1.15       ad 		sched_yield();
    173  1.16       ad 		count = pthread__nspins;
    174  1.19       ad 		thread->pt_spinlocks++;
    175  1.19       ad 	} while (/*CONSTCOND*/ 1);
    176   1.2  thorpej 
    177   1.2  thorpej 	PTHREADD_ADD(PTHREADD_SPINLOCKS);
    178   1.2  thorpej }
    179   1.2  thorpej 
    180   1.2  thorpej int
    181   1.2  thorpej pthread_spintrylock(pthread_t thread, pthread_spin_t *lock)
    182   1.2  thorpej {
    183   1.2  thorpej 	int ret;
    184   1.2  thorpej 
    185  1.19       ad 	SDPRINTF(("(pthread_spintrylock %p) spinlock %p (count %d)\n",
    186  1.19       ad 	    thread, lock, thread->pt_spinlocks));
    187  1.18       ad 
    188  1.19       ad 	thread->pt_spinlocks++;
    189   1.2  thorpej 	ret = pthread__simple_lock_try(lock);
    190  1.18       ad 	if (!ret)
    191  1.19       ad 		thread->pt_spinlocks--;
    192  1.19       ad 
    193   1.2  thorpej 	return ret;
    194   1.2  thorpej }
    195   1.2  thorpej 
    196   1.2  thorpej void
    197   1.2  thorpej pthread_spinunlock(pthread_t thread, pthread_spin_t *lock)
    198   1.2  thorpej {
    199   1.2  thorpej 
    200  1.19       ad 	SDPRINTF(("(pthread_spinunlock %p) spinlock %p (count %d)\n",
    201  1.19       ad 	    thread, lock, thread->pt_spinlocks));
    202  1.19       ad 
    203   1.2  thorpej 	pthread__simple_unlock(lock);
    204  1.19       ad 	thread->pt_spinlocks--;
    205   1.2  thorpej #ifdef PTHREAD_SPIN_DEBUG
    206   1.5  nathanw 	pthread__assert(thread->pt_spinlocks >= 0);
    207   1.2  thorpej #endif
    208   1.2  thorpej 	PTHREADD_ADD(PTHREADD_SPINUNLOCKS);
    209   1.2  thorpej }
    210   1.2  thorpej 
    211   1.2  thorpej 
    212   1.2  thorpej /*
    213   1.2  thorpej  * Public (POSIX-specified) spinlocks.
    214   1.2  thorpej  */
    215   1.2  thorpej int
    216   1.2  thorpej pthread_spin_init(pthread_spinlock_t *lock, int pshared)
    217   1.2  thorpej {
    218   1.2  thorpej 
    219   1.2  thorpej #ifdef ERRORCHECK
    220  1.19       ad 	if (lock == NULL || (pshared != PTHREAD_PROCESS_PRIVATE &&
    221  1.19       ad 	    pshared != PTHREAD_PROCESS_SHARED))
    222   1.2  thorpej 		return EINVAL;
    223   1.2  thorpej #endif
    224   1.2  thorpej 	lock->pts_magic = _PT_SPINLOCK_MAGIC;
    225  1.19       ad 
    226   1.2  thorpej 	/*
    227   1.2  thorpej 	 * We don't actually use the pshared flag for anything;
    228   1.9      wiz 	 * CPU simple locks have all the process-shared properties
    229   1.2  thorpej 	 * that we want anyway.
    230   1.2  thorpej 	 */
    231   1.2  thorpej 	lock->pts_flags = pshared;
    232   1.2  thorpej 	pthread_lockinit(&lock->pts_spin);
    233   1.2  thorpej 
    234   1.2  thorpej 	return 0;
    235   1.2  thorpej }
    236   1.2  thorpej 
    237   1.2  thorpej int
    238   1.2  thorpej pthread_spin_destroy(pthread_spinlock_t *lock)
    239   1.2  thorpej {
    240   1.2  thorpej 
    241   1.2  thorpej #ifdef ERRORCHECK
    242  1.19       ad 	if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
    243   1.2  thorpej 		return EINVAL;
    244   1.2  thorpej 	if (lock->pts_spin != __SIMPLELOCK_UNLOCKED)
    245   1.2  thorpej 		return EBUSY;
    246   1.2  thorpej #endif
    247   1.2  thorpej 
    248   1.2  thorpej 	lock->pts_magic = _PT_SPINLOCK_DEAD;
    249   1.2  thorpej 
    250   1.2  thorpej 	return 0;
    251   1.2  thorpej }
    252   1.2  thorpej 
    253   1.2  thorpej int
    254   1.2  thorpej pthread_spin_lock(pthread_spinlock_t *lock)
    255   1.2  thorpej {
    256   1.2  thorpej 
    257   1.2  thorpej #ifdef ERRORCHECK
    258  1.19       ad 	if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
    259   1.2  thorpej 		return EINVAL;
    260   1.2  thorpej #endif
    261   1.2  thorpej 
    262  1.17       ad 	while (pthread__simple_lock_try(&lock->pts_spin) == 0) {
    263  1.17       ad 		smt_pause();
    264  1.17       ad 	}
    265   1.2  thorpej 
    266   1.2  thorpej 	return 0;
    267   1.2  thorpej }
    268   1.2  thorpej 
    269   1.2  thorpej int
    270   1.2  thorpej pthread_spin_trylock(pthread_spinlock_t *lock)
    271   1.2  thorpej {
    272   1.2  thorpej 
    273   1.2  thorpej #ifdef ERRORCHECK
    274  1.19       ad 	if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
    275   1.2  thorpej 		return EINVAL;
    276   1.2  thorpej #endif
    277   1.2  thorpej 
    278   1.2  thorpej 	if (pthread__simple_lock_try(&lock->pts_spin) == 0)
    279   1.2  thorpej 		return EBUSY;
    280   1.2  thorpej 
    281   1.2  thorpej 	return 0;
    282   1.2  thorpej }
    283   1.2  thorpej 
    284   1.2  thorpej int
    285   1.2  thorpej pthread_spin_unlock(pthread_spinlock_t *lock)
    286   1.2  thorpej {
    287   1.2  thorpej 
    288   1.2  thorpej #ifdef ERRORCHECK
    289  1.19       ad 	if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
    290   1.2  thorpej 		return EINVAL;
    291   1.2  thorpej #endif
    292   1.2  thorpej 
    293   1.2  thorpej 	pthread__simple_unlock(&lock->pts_spin);
    294   1.2  thorpej 
    295   1.2  thorpej 	return 0;
    296   1.2  thorpej }
    297