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