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