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