Home | History | Annotate | Line # | Download | only in libpthread
pthread_lock.c revision 1.18
      1 /*	$NetBSD: pthread_lock.c,v 1.18 2007/03/02 18:53:52 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 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.
      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.18 2007/03/02 18:53:52 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 
     49 #include "pthread.h"
     50 #include "pthread_int.h"
     51 
     52 #ifdef PTHREAD_SPIN_DEBUG_PRINT
     53 #define SDPRINTF(x) DPRINTF(x)
     54 #else
     55 #define SDPRINTF(x)
     56 #endif
     57 
     58 /* This does not belong here. */
     59 #if defined(i386) || defined(__x86_64__)
     60 #define	smt_pause()	__asm __volatile("rep; nop" ::: "memory")
     61 #else
     62 #define	smt_pause()	/* nothing */
     63 #endif
     64 
     65 extern int pthread__nspins;
     66 
     67 RAS_DECL(pthread__lock);
     68 
     69 static void
     70 pthread__ras_simple_lock_init(__cpu_simple_lock_t *alp)
     71 {
     72 
     73 	*alp = __SIMPLELOCK_UNLOCKED;
     74 }
     75 
     76 static int
     77 pthread__ras_simple_lock_try(__cpu_simple_lock_t *alp)
     78 {
     79 	__cpu_simple_lock_t old;
     80 
     81 	RAS_START(pthread__lock);
     82 	old = *alp;
     83 	*alp = __SIMPLELOCK_LOCKED;
     84 	RAS_END(pthread__lock);
     85 
     86 	return (old == __SIMPLELOCK_UNLOCKED);
     87 }
     88 
     89 static void
     90 pthread__ras_simple_unlock(__cpu_simple_lock_t *alp)
     91 {
     92 
     93 	*alp = __SIMPLELOCK_UNLOCKED;
     94 }
     95 
     96 static const struct pthread_lock_ops pthread__lock_ops_ras = {
     97 	pthread__ras_simple_lock_init,
     98 	pthread__ras_simple_lock_try,
     99 	pthread__ras_simple_unlock,
    100 };
    101 
    102 static void
    103 pthread__atomic_simple_lock_init(__cpu_simple_lock_t *alp)
    104 {
    105 
    106 	__cpu_simple_lock_init(alp);
    107 }
    108 
    109 static int
    110 pthread__atomic_simple_lock_try(__cpu_simple_lock_t *alp)
    111 {
    112 
    113 	return (__cpu_simple_lock_try(alp));
    114 }
    115 
    116 static void
    117 pthread__atomic_simple_unlock(__cpu_simple_lock_t *alp)
    118 {
    119 
    120 	__cpu_simple_unlock(alp);
    121 }
    122 
    123 static const struct pthread_lock_ops pthread__lock_ops_atomic = {
    124 	pthread__atomic_simple_lock_init,
    125 	pthread__atomic_simple_lock_try,
    126 	pthread__atomic_simple_unlock,
    127 };
    128 
    129 /*
    130  * We default to pointing to the RAS primitives; we might need to use
    131  * locks early, but before main() starts.  This is safe, since no other
    132  * threads will be active for the process, so atomicity will not be
    133  * required.
    134  */
    135 const struct pthread_lock_ops *pthread__lock_ops = &pthread__lock_ops_ras;
    136 
    137 /*
    138  * Initialize the locking primitives.  On uniprocessors, we always
    139  * use Restartable Atomic Sequences if they are available.  Otherwise,
    140  * we fall back onto machine-dependent atomic lock primitives.
    141  */
    142 void
    143 pthread__lockprim_init(int ncpu)
    144 {
    145 
    146 	if (ncpu == 1 && rasctl(RAS_ADDR(pthread__lock),
    147 				RAS_SIZE(pthread__lock), RAS_INSTALL) == 0) {
    148 		pthread__lock_ops = &pthread__lock_ops_ras;
    149 		return;
    150 	}
    151 
    152 	pthread__lock_ops = &pthread__lock_ops_atomic;
    153 }
    154 
    155 void
    156 pthread_lockinit(pthread_spin_t *lock)
    157 {
    158 
    159 	pthread__simple_lock_init(lock);
    160 }
    161 
    162 void
    163 pthread_spinlock(pthread_t thread, pthread_spin_t *lock)
    164 {
    165 	int count, ret;
    166 
    167 	count = pthread__nspins;
    168 	SDPRINTF(("(pthread_spinlock %p) incrementing spinlock %p (count %d)\n",
    169 		thread, lock, thread->pt_spinlocks));
    170 #ifdef PTHREAD_SPIN_DEBUG
    171 	pthread__assert(thread->pt_spinlocks >= 0);
    172 #endif
    173 	++thread->pt_spinlocks;
    174 
    175 	do {
    176 		while (((ret = pthread__simple_lock_try(lock)) == 0) && --count) {
    177 			smt_pause();
    178 		}
    179 
    180 		if (ret == 1)
    181 			break;
    182 
    183 	SDPRINTF(("(pthread_spinlock %p) decrementing spinlock %p (count %d)\n",
    184 		thread, lock, thread->pt_spinlocks));
    185 		--thread->pt_spinlocks;
    186 
    187 		/*
    188 		 * We may be preempted while spinning. If so, we will
    189 		 * be restarted here if thread->pt_spinlocks is
    190 		 * nonzero, which can happen if:
    191 		 * a) we just got the lock
    192 		 * b) we haven't yet decremented the lock count.
    193 		 * If we're at this point, (b) applies. Therefore,
    194 		 * check if we're being continued, and if so, bail.
    195 		 * (in case (a), we should let the code finish and
    196 		 * we will bail out in pthread_spinunlock()).
    197 		 */
    198 		/* XXXLWP far from ideal */
    199 		sched_yield();
    200 		/* try again */
    201 		count = pthread__nspins;
    202 	SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
    203 		thread, thread->pt_spinlocks));
    204 		++thread->pt_spinlocks;
    205 	} while (/*CONSTCOND*/1);
    206 
    207 	PTHREADD_ADD(PTHREADD_SPINLOCKS);
    208 	/* Got it! We're out of here. */
    209 }
    210 
    211 
    212 int
    213 pthread_spintrylock(pthread_t thread, pthread_spin_t *lock)
    214 {
    215 	int ret;
    216 
    217 	SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
    218 		thread, thread->pt_spinlocks));
    219 
    220 	++thread->pt_spinlocks;
    221 	ret = pthread__simple_lock_try(lock);
    222 	if (!ret)
    223 		--thread->pt_spinlocks;
    224 	return ret;
    225 }
    226 
    227 
    228 void
    229 pthread_spinunlock(pthread_t thread, pthread_spin_t *lock)
    230 {
    231 
    232 	pthread__simple_unlock(lock);
    233 	SDPRINTF(("(pthread_spinunlock %p) decrementing spinlock %p (count %d)\n",
    234 		thread, lock, thread->pt_spinlocks));
    235 	--thread->pt_spinlocks;
    236 #ifdef PTHREAD_SPIN_DEBUG
    237 	pthread__assert(thread->pt_spinlocks >= 0);
    238 #endif
    239 	PTHREADD_ADD(PTHREADD_SPINUNLOCKS);
    240 }
    241 
    242 
    243 /*
    244  * Public (POSIX-specified) spinlocks.
    245  * These don't interact with the spin-preemption code, nor do they
    246  * perform any adaptive sleeping.
    247  */
    248 
    249 int
    250 pthread_spin_init(pthread_spinlock_t *lock, int pshared)
    251 {
    252 
    253 #ifdef ERRORCHECK
    254 	if ((lock == NULL) ||
    255 	    ((pshared != PTHREAD_PROCESS_PRIVATE) &&
    256 		(pshared != PTHREAD_PROCESS_SHARED)))
    257 		return EINVAL;
    258 #endif
    259 	lock->pts_magic = _PT_SPINLOCK_MAGIC;
    260 	/*
    261 	 * We don't actually use the pshared flag for anything;
    262 	 * CPU simple locks have all the process-shared properties
    263 	 * that we want anyway.
    264 	 */
    265 	lock->pts_flags = pshared;
    266 	pthread_lockinit(&lock->pts_spin);
    267 
    268 	return 0;
    269 }
    270 
    271 int
    272 pthread_spin_destroy(pthread_spinlock_t *lock)
    273 {
    274 
    275 #ifdef ERRORCHECK
    276 	if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
    277 		return EINVAL;
    278 
    279 	if (lock->pts_spin != __SIMPLELOCK_UNLOCKED)
    280 		return EBUSY;
    281 #endif
    282 
    283 	lock->pts_magic = _PT_SPINLOCK_DEAD;
    284 
    285 	return 0;
    286 }
    287 
    288 int
    289 pthread_spin_lock(pthread_spinlock_t *lock)
    290 {
    291 
    292 #ifdef ERRORCHECK
    293 	if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
    294 		return EINVAL;
    295 #endif
    296 
    297 	while (pthread__simple_lock_try(&lock->pts_spin) == 0) {
    298 		smt_pause();
    299 	}
    300 
    301 	return 0;
    302 }
    303 
    304 int
    305 pthread_spin_trylock(pthread_spinlock_t *lock)
    306 {
    307 
    308 #ifdef ERRORCHECK
    309 	if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
    310 		return EINVAL;
    311 #endif
    312 
    313 	if (pthread__simple_lock_try(&lock->pts_spin) == 0)
    314 		return EBUSY;
    315 
    316 	return 0;
    317 }
    318 
    319 int
    320 pthread_spin_unlock(pthread_spinlock_t *lock)
    321 {
    322 
    323 #ifdef ERRORCHECK
    324 	if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
    325 		return EINVAL;
    326 #endif
    327 
    328 	pthread__simple_unlock(&lock->pts_spin);
    329 
    330 	return 0;
    331 }
    332