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