Home | History | Annotate | Line # | Download | only in libpthread
pthread_lock.c revision 1.17
      1 /*	$NetBSD: pthread_lock.c,v 1.17 2007/03/02 17:34:21 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.17 2007/03/02 17:34:21 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 #ifdef PTHREAD_SA
    199 		if (thread->pt_next != NULL) {
    200 			PTHREADD_ADD(PTHREADD_SPINPREEMPT);
    201 			pthread__assert(thread->pt_blockgen == thread->pt_unblockgen);
    202 			pthread__switch(thread, thread->pt_next);
    203 		}
    204 #else
    205 		/* XXXLWP far from ideal */
    206 		sched_yield();
    207 #endif
    208 		/* try again */
    209 		count = pthread__nspins;
    210 	SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
    211 		thread, thread->pt_spinlocks));
    212 		++thread->pt_spinlocks;
    213 	} while (/*CONSTCOND*/1);
    214 
    215 	PTHREADD_ADD(PTHREADD_SPINLOCKS);
    216 	/* Got it! We're out of here. */
    217 }
    218 
    219 
    220 int
    221 pthread_spintrylock(pthread_t thread, pthread_spin_t *lock)
    222 {
    223 	int ret;
    224 
    225 	SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
    226 		thread, thread->pt_spinlocks));
    227 	++thread->pt_spinlocks;
    228 
    229 	ret = pthread__simple_lock_try(lock);
    230 
    231 #ifdef PTHREAD_SA
    232 	if (ret == 0) {
    233 	SDPRINTF(("(pthread_spintrylock %p) decrementing spinlock from %d\n",
    234 		thread, thread->pt_spinlocks));
    235 		--thread->pt_spinlocks;
    236 		/* See above. */
    237 		if (thread->pt_next != NULL) {
    238 			PTHREADD_ADD(PTHREADD_SPINPREEMPT);
    239 			pthread__assert(thread->pt_blockgen == thread->pt_unblockgen);
    240 			pthread__switch(thread, thread->pt_next);
    241 		}
    242 	}
    243 #endif
    244 
    245 	return ret;
    246 }
    247 
    248 
    249 void
    250 pthread_spinunlock(pthread_t thread, pthread_spin_t *lock)
    251 {
    252 
    253 	pthread__simple_unlock(lock);
    254 	SDPRINTF(("(pthread_spinunlock %p) decrementing spinlock %p (count %d)\n",
    255 		thread, lock, thread->pt_spinlocks));
    256 	--thread->pt_spinlocks;
    257 #ifdef PTHREAD_SPIN_DEBUG
    258 	pthread__assert(thread->pt_spinlocks >= 0);
    259 #endif
    260 	PTHREADD_ADD(PTHREADD_SPINUNLOCKS);
    261 
    262 #ifdef PTHREAD_SA
    263 	/*
    264 	 * If we were preempted while holding a spinlock, the
    265 	 * scheduler will notice this and continue us. To be good
    266 	 * citzens, we must now get out of here if that was our
    267 	 * last spinlock.
    268 	 * XXX when will we ever have more than one?
    269 	 */
    270 
    271 	if ((thread->pt_spinlocks == 0) && (thread->pt_next != NULL)) {
    272 		PTHREADD_ADD(PTHREADD_SPINPREEMPT);
    273 		/* pthread__assert(thread->pt_blockgen == thread->pt_unblockgen); */
    274 		pthread__switch(thread, thread->pt_next);
    275 	}
    276 #endif
    277 }
    278 
    279 
    280 /*
    281  * Public (POSIX-specified) spinlocks.
    282  * These don't interact with the spin-preemption code, nor do they
    283  * perform any adaptive sleeping.
    284  */
    285 
    286 int
    287 pthread_spin_init(pthread_spinlock_t *lock, int pshared)
    288 {
    289 
    290 #ifdef ERRORCHECK
    291 	if ((lock == NULL) ||
    292 	    ((pshared != PTHREAD_PROCESS_PRIVATE) &&
    293 		(pshared != PTHREAD_PROCESS_SHARED)))
    294 		return EINVAL;
    295 #endif
    296 	lock->pts_magic = _PT_SPINLOCK_MAGIC;
    297 	/*
    298 	 * We don't actually use the pshared flag for anything;
    299 	 * CPU simple locks have all the process-shared properties
    300 	 * that we want anyway.
    301 	 */
    302 	lock->pts_flags = pshared;
    303 	pthread_lockinit(&lock->pts_spin);
    304 
    305 	return 0;
    306 }
    307 
    308 int
    309 pthread_spin_destroy(pthread_spinlock_t *lock)
    310 {
    311 
    312 #ifdef ERRORCHECK
    313 	if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
    314 		return EINVAL;
    315 
    316 	if (lock->pts_spin != __SIMPLELOCK_UNLOCKED)
    317 		return EBUSY;
    318 #endif
    319 
    320 	lock->pts_magic = _PT_SPINLOCK_DEAD;
    321 
    322 	return 0;
    323 }
    324 
    325 int
    326 pthread_spin_lock(pthread_spinlock_t *lock)
    327 {
    328 
    329 #ifdef ERRORCHECK
    330 	if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
    331 		return EINVAL;
    332 #endif
    333 
    334 	while (pthread__simple_lock_try(&lock->pts_spin) == 0) {
    335 		smt_pause();
    336 	}
    337 
    338 	return 0;
    339 }
    340 
    341 int
    342 pthread_spin_trylock(pthread_spinlock_t *lock)
    343 {
    344 
    345 #ifdef ERRORCHECK
    346 	if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
    347 		return EINVAL;
    348 #endif
    349 
    350 	if (pthread__simple_lock_try(&lock->pts_spin) == 0)
    351 		return EBUSY;
    352 
    353 	return 0;
    354 }
    355 
    356 int
    357 pthread_spin_unlock(pthread_spinlock_t *lock)
    358 {
    359 
    360 #ifdef ERRORCHECK
    361 	if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
    362 		return EINVAL;
    363 #endif
    364 
    365 	pthread__simple_unlock(&lock->pts_spin);
    366 
    367 	return 0;
    368 }
    369