Home | History | Annotate | Line # | Download | only in libpthread
pthread_lock.c revision 1.16
      1 /*	$NetBSD: pthread_lock.c,v 1.16 2006/12/24 18:39:46 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.16 2006/12/24 18:39:46 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 extern int pthread__nspins;
     59 
     60 RAS_DECL(pthread__lock);
     61 
     62 static void
     63 pthread__ras_simple_lock_init(__cpu_simple_lock_t *alp)
     64 {
     65 
     66 	*alp = __SIMPLELOCK_UNLOCKED;
     67 }
     68 
     69 static int
     70 pthread__ras_simple_lock_try(__cpu_simple_lock_t *alp)
     71 {
     72 	__cpu_simple_lock_t old;
     73 
     74 	RAS_START(pthread__lock);
     75 	old = *alp;
     76 	*alp = __SIMPLELOCK_LOCKED;
     77 	RAS_END(pthread__lock);
     78 
     79 	return (old == __SIMPLELOCK_UNLOCKED);
     80 }
     81 
     82 static void
     83 pthread__ras_simple_unlock(__cpu_simple_lock_t *alp)
     84 {
     85 
     86 	*alp = __SIMPLELOCK_UNLOCKED;
     87 }
     88 
     89 static const struct pthread_lock_ops pthread__lock_ops_ras = {
     90 	pthread__ras_simple_lock_init,
     91 	pthread__ras_simple_lock_try,
     92 	pthread__ras_simple_unlock,
     93 };
     94 
     95 static void
     96 pthread__atomic_simple_lock_init(__cpu_simple_lock_t *alp)
     97 {
     98 
     99 	__cpu_simple_lock_init(alp);
    100 }
    101 
    102 static int
    103 pthread__atomic_simple_lock_try(__cpu_simple_lock_t *alp)
    104 {
    105 
    106 	return (__cpu_simple_lock_try(alp));
    107 }
    108 
    109 static void
    110 pthread__atomic_simple_unlock(__cpu_simple_lock_t *alp)
    111 {
    112 
    113 	__cpu_simple_unlock(alp);
    114 }
    115 
    116 static const struct pthread_lock_ops pthread__lock_ops_atomic = {
    117 	pthread__atomic_simple_lock_init,
    118 	pthread__atomic_simple_lock_try,
    119 	pthread__atomic_simple_unlock,
    120 };
    121 
    122 /*
    123  * We default to pointing to the RAS primitives; we might need to use
    124  * locks early, but before main() starts.  This is safe, since no other
    125  * threads will be active for the process, so atomicity will not be
    126  * required.
    127  */
    128 const struct pthread_lock_ops *pthread__lock_ops = &pthread__lock_ops_ras;
    129 
    130 /*
    131  * Initialize the locking primitives.  On uniprocessors, we always
    132  * use Restartable Atomic Sequences if they are available.  Otherwise,
    133  * we fall back onto machine-dependent atomic lock primitives.
    134  */
    135 void
    136 pthread__lockprim_init(int ncpu)
    137 {
    138 
    139 	if (ncpu == 1 && rasctl(RAS_ADDR(pthread__lock),
    140 				RAS_SIZE(pthread__lock), RAS_INSTALL) == 0) {
    141 		pthread__lock_ops = &pthread__lock_ops_ras;
    142 		return;
    143 	}
    144 
    145 	pthread__lock_ops = &pthread__lock_ops_atomic;
    146 }
    147 
    148 void
    149 pthread_lockinit(pthread_spin_t *lock)
    150 {
    151 
    152 	pthread__simple_lock_init(lock);
    153 }
    154 
    155 void
    156 pthread_spinlock(pthread_t thread, pthread_spin_t *lock)
    157 {
    158 	int count, ret;
    159 
    160 	count = pthread__nspins;
    161 	SDPRINTF(("(pthread_spinlock %p) incrementing spinlock %p (count %d)\n",
    162 		thread, lock, thread->pt_spinlocks));
    163 #ifdef PTHREAD_SPIN_DEBUG
    164 	pthread__assert(thread->pt_spinlocks >= 0);
    165 #endif
    166 	++thread->pt_spinlocks;
    167 
    168 	do {
    169 		while (((ret = pthread__simple_lock_try(lock)) == 0) && --count)
    170 			/* XXX For at least x86, issue 'pause' instruction */;
    171 
    172 		if (ret == 1)
    173 			break;
    174 
    175 	SDPRINTF(("(pthread_spinlock %p) decrementing spinlock %p (count %d)\n",
    176 		thread, lock, thread->pt_spinlocks));
    177 		--thread->pt_spinlocks;
    178 
    179 		/*
    180 		 * We may be preempted while spinning. If so, we will
    181 		 * be restarted here if thread->pt_spinlocks is
    182 		 * nonzero, which can happen if:
    183 		 * a) we just got the lock
    184 		 * b) we haven't yet decremented the lock count.
    185 		 * If we're at this point, (b) applies. Therefore,
    186 		 * check if we're being continued, and if so, bail.
    187 		 * (in case (a), we should let the code finish and
    188 		 * we will bail out in pthread_spinunlock()).
    189 		 */
    190 #ifdef PTHREAD_SA
    191 		if (thread->pt_next != NULL) {
    192 			PTHREADD_ADD(PTHREADD_SPINPREEMPT);
    193 			pthread__assert(thread->pt_blockgen == thread->pt_unblockgen);
    194 			pthread__switch(thread, thread->pt_next);
    195 		}
    196 #else
    197 		/* XXXLWP far from ideal */
    198 		sched_yield();
    199 #endif
    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 	++thread->pt_spinlocks;
    220 
    221 	ret = pthread__simple_lock_try(lock);
    222 
    223 #ifdef PTHREAD_SA
    224 	if (ret == 0) {
    225 	SDPRINTF(("(pthread_spintrylock %p) decrementing spinlock from %d\n",
    226 		thread, thread->pt_spinlocks));
    227 		--thread->pt_spinlocks;
    228 		/* See above. */
    229 		if (thread->pt_next != NULL) {
    230 			PTHREADD_ADD(PTHREADD_SPINPREEMPT);
    231 			pthread__assert(thread->pt_blockgen == thread->pt_unblockgen);
    232 			pthread__switch(thread, thread->pt_next);
    233 		}
    234 	}
    235 #endif
    236 
    237 	return ret;
    238 }
    239 
    240 
    241 void
    242 pthread_spinunlock(pthread_t thread, pthread_spin_t *lock)
    243 {
    244 
    245 	pthread__simple_unlock(lock);
    246 	SDPRINTF(("(pthread_spinunlock %p) decrementing spinlock %p (count %d)\n",
    247 		thread, lock, thread->pt_spinlocks));
    248 	--thread->pt_spinlocks;
    249 #ifdef PTHREAD_SPIN_DEBUG
    250 	pthread__assert(thread->pt_spinlocks >= 0);
    251 #endif
    252 	PTHREADD_ADD(PTHREADD_SPINUNLOCKS);
    253 
    254 #ifdef PTHREAD_SA
    255 	/*
    256 	 * If we were preempted while holding a spinlock, the
    257 	 * scheduler will notice this and continue us. To be good
    258 	 * citzens, we must now get out of here if that was our
    259 	 * last spinlock.
    260 	 * XXX when will we ever have more than one?
    261 	 */
    262 
    263 	if ((thread->pt_spinlocks == 0) && (thread->pt_next != NULL)) {
    264 		PTHREADD_ADD(PTHREADD_SPINPREEMPT);
    265 		/* pthread__assert(thread->pt_blockgen == thread->pt_unblockgen); */
    266 		pthread__switch(thread, thread->pt_next);
    267 	}
    268 #endif
    269 }
    270 
    271 
    272 /*
    273  * Public (POSIX-specified) spinlocks.
    274  * These don't interact with the spin-preemption code, nor do they
    275  * perform any adaptive sleeping.
    276  */
    277 
    278 int
    279 pthread_spin_init(pthread_spinlock_t *lock, int pshared)
    280 {
    281 
    282 #ifdef ERRORCHECK
    283 	if ((lock == NULL) ||
    284 	    ((pshared != PTHREAD_PROCESS_PRIVATE) &&
    285 		(pshared != PTHREAD_PROCESS_SHARED)))
    286 		return EINVAL;
    287 #endif
    288 	lock->pts_magic = _PT_SPINLOCK_MAGIC;
    289 	/*
    290 	 * We don't actually use the pshared flag for anything;
    291 	 * CPU simple locks have all the process-shared properties
    292 	 * that we want anyway.
    293 	 */
    294 	lock->pts_flags = pshared;
    295 	pthread_lockinit(&lock->pts_spin);
    296 
    297 	return 0;
    298 }
    299 
    300 int
    301 pthread_spin_destroy(pthread_spinlock_t *lock)
    302 {
    303 
    304 #ifdef ERRORCHECK
    305 	if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
    306 		return EINVAL;
    307 
    308 	if (lock->pts_spin != __SIMPLELOCK_UNLOCKED)
    309 		return EBUSY;
    310 #endif
    311 
    312 	lock->pts_magic = _PT_SPINLOCK_DEAD;
    313 
    314 	return 0;
    315 }
    316 
    317 int
    318 pthread_spin_lock(pthread_spinlock_t *lock)
    319 {
    320 
    321 #ifdef ERRORCHECK
    322 	if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
    323 		return EINVAL;
    324 #endif
    325 
    326 	while (pthread__simple_lock_try(&lock->pts_spin) == 0)
    327 		/* spin */ ;
    328 
    329 	return 0;
    330 }
    331 
    332 int
    333 pthread_spin_trylock(pthread_spinlock_t *lock)
    334 {
    335 
    336 #ifdef ERRORCHECK
    337 	if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
    338 		return EINVAL;
    339 #endif
    340 
    341 	if (pthread__simple_lock_try(&lock->pts_spin) == 0)
    342 		return EBUSY;
    343 
    344 	return 0;
    345 }
    346 
    347 int
    348 pthread_spin_unlock(pthread_spinlock_t *lock)
    349 {
    350 
    351 #ifdef ERRORCHECK
    352 	if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
    353 		return EINVAL;
    354 #endif
    355 
    356 	pthread__simple_unlock(&lock->pts_spin);
    357 
    358 	return 0;
    359 }
    360