Home | History | Annotate | Line # | Download | only in libpthread
pthread_lock.c revision 1.3
      1 /*	$NetBSD: pthread_lock.c,v 1.3 2003/01/18 18:45:55 christos 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/param.h>
     40 #include <sys/ras.h>
     41 #include <sys/sysctl.h>
     42 
     43 #include <assert.h>
     44 #include <errno.h>
     45 #include <unistd.h>
     46 
     47 #include "pthread.h"
     48 #include "pthread_int.h"
     49 
     50 #undef PTHREAD_SPIN_DEBUG
     51 
     52 #ifdef PTHREAD_SPIN_DEBUG
     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 1	/* no point in actually spinning until MP works */
     60 
     61 static int nspins = NSPINS;
     62 
     63 extern char pthread__lock_ras_start[], pthread__lock_ras_end[];
     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 	/* This is the atomic sequence. */
     78 	__asm __volatile("pthread__lock_ras_start:");
     79 	old = *alp;
     80 	*alp = __SIMPLELOCK_LOCKED;
     81 	__asm __volatile("pthread__lock_ras_end:");
     82 
     83 	return (old == __SIMPLELOCK_UNLOCKED);
     84 }
     85 
     86 static void
     87 pthread__ras_simple_unlock(__cpu_simple_lock_t *alp)
     88 {
     89 
     90 	*alp = __SIMPLELOCK_UNLOCKED;
     91 }
     92 
     93 static const struct pthread_lock_ops pthread__lock_ops_ras = {
     94 	pthread__ras_simple_lock_init,
     95 	pthread__ras_simple_lock_try,
     96 	pthread__ras_simple_unlock,
     97 };
     98 
     99 static void
    100 pthread__atomic_simple_lock_init(__cpu_simple_lock_t *alp)
    101 {
    102 
    103 	__cpu_simple_lock_init(alp);
    104 }
    105 
    106 static int
    107 pthread__atomic_simple_lock_try(__cpu_simple_lock_t *alp)
    108 {
    109 
    110 	return (__cpu_simple_lock_try(alp));
    111 }
    112 
    113 static void
    114 pthread__atomic_simple_unlock(__cpu_simple_lock_t *alp)
    115 {
    116 
    117 	__cpu_simple_unlock(alp);
    118 }
    119 
    120 static const struct pthread_lock_ops pthread__lock_ops_atomic = {
    121 	pthread__atomic_simple_lock_init,
    122 	pthread__atomic_simple_lock_try,
    123 	pthread__atomic_simple_unlock,
    124 };
    125 
    126 /*
    127  * We default to pointing to the RAS primitives; we might need to use
    128  * locks early, but before main() starts.  This is safe, since no other
    129  * threads will be active for the process, so atomicity will not be
    130  * required.
    131  */
    132 const struct pthread_lock_ops *pthread__lock_ops = &pthread__lock_ops_ras;
    133 
    134 /*
    135  * Initialize the locking primitives.  On uniprocessors, we always
    136  * use Restartable Atomic Sequences if they are available.  Otherwise,
    137  * we fall back onto machine-dependent atomic lock primitives.
    138  */
    139 void
    140 pthread__lockprim_init(void)
    141 {
    142 	int mib[2];
    143 	size_t len;
    144 	int ncpu;
    145 
    146 	mib[0] = CTL_HW;
    147 	mib[1] = HW_NCPU;
    148 
    149 	len = sizeof(ncpu);
    150 	sysctl(mib, 2, &ncpu, &len, NULL, 0);
    151 
    152 	if (ncpu == 1 && rasctl(pthread__lock_ras_start,
    153 	    (size_t)(pthread__lock_ras_end - pthread__lock_ras_start),
    154 	    RAS_INSTALL) == 0) {
    155 		pthread__lock_ops = &pthread__lock_ops_ras;
    156 		return;
    157 	}
    158 
    159 	pthread__lock_ops = &pthread__lock_ops_atomic;
    160 }
    161 
    162 void
    163 pthread_lockinit(pthread_spin_t *lock)
    164 {
    165 
    166 	pthread__simple_lock_init(lock);
    167 }
    168 
    169 void
    170 pthread_spinlock(pthread_t thread, pthread_spin_t *lock)
    171 {
    172 	int count, ret;
    173 
    174 	count = nspins;
    175 	SDPRINTF(("(pthread_spinlock %p) incrementing spinlock %p (count %d)\n",
    176 		thread, lock, thread->pt_spinlocks));
    177 #ifdef PTHREAD_SPIN_DEBUG
    178 	if(!(thread->pt_spinlocks >= 0)) {
    179 		(void)kill(getpid(), SIGABRT);
    180 		_exit(1);
    181 	}
    182 #endif
    183 	++thread->pt_spinlocks;
    184 
    185 	do {
    186 		while (((ret = pthread__simple_lock_try(lock)) == 0) && --count)
    187 			;
    188 
    189 		if (ret == 1)
    190 			break;
    191 
    192 	SDPRINTF(("(pthread_spinlock %p) decrementing spinlock %p (count %d)\n",
    193 		thread, lock, thread->pt_spinlocks));
    194 		--thread->pt_spinlocks;
    195 
    196 		/*
    197 		 * We may be preempted while spinning. If so, we will
    198 		 * be restarted here if thread->pt_spinlocks is
    199 		 * nonzero, which can happen if:
    200 		 * a) we just got the lock
    201 		 * b) we haven't yet decremented the lock count.
    202 		 * If we're at this point, (b) applies. Therefore,
    203 		 * check if we're being continued, and if so, bail.
    204 		 * (in case (a), we should let the code finish and
    205 		 * we will bail out in pthread_spinunlock()).
    206 		 */
    207 		if (thread->pt_next != NULL) {
    208 			PTHREADD_ADD(PTHREADD_SPINPREEMPT);
    209 			pthread__switch(thread, thread->pt_next);
    210 		}
    211 		/* try again */
    212 		count = nspins;
    213 	SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
    214 		thread, thread->pt_spinlocks));
    215 		++thread->pt_spinlocks;
    216 	} while (/*CONSTCOND*/1);
    217 
    218 	PTHREADD_ADD(PTHREADD_SPINLOCKS);
    219 	/* Got it! We're out of here. */
    220 }
    221 
    222 
    223 int
    224 pthread_spintrylock(pthread_t thread, pthread_spin_t *lock)
    225 {
    226 	int ret;
    227 
    228 	SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
    229 		thread, thread->pt_spinlocks));
    230 	++thread->pt_spinlocks;
    231 
    232 	ret = pthread__simple_lock_try(lock);
    233 
    234 	if (ret == 0) {
    235 	SDPRINTF(("(pthread_spintrylock %p) decrementing spinlock from %d\n",
    236 		thread, thread->pt_spinlocks));
    237 		--thread->pt_spinlocks;
    238 		/* See above. */
    239 		if (thread->pt_next != NULL) {
    240 			PTHREADD_ADD(PTHREADD_SPINPREEMPT);
    241 			pthread__switch(thread, thread->pt_next);
    242 		}
    243 	}
    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 	if (!(thread->pt_spinlocks >= 0)) {
    259 		(void)kill(getpid(), SIGABRT);
    260 		_exit(1);
    261 	}
    262 #endif
    263 	PTHREADD_ADD(PTHREADD_SPINUNLOCKS);
    264 
    265 	/*
    266 	 * If we were preempted while holding a spinlock, the
    267 	 * scheduler will notice this and continue us. To be good
    268 	 * citzens, we must now get out of here if that was our
    269 	 * last spinlock.
    270 	 * XXX when will we ever have more than one?
    271 	 */
    272 
    273 	if ((thread->pt_spinlocks == 0) && (thread->pt_next != NULL)) {
    274 		PTHREADD_ADD(PTHREADD_SPINPREEMPT);
    275 		pthread__switch(thread, thread->pt_next);
    276 	}
    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 		/* spin */ ;
    336 
    337 	return 0;
    338 }
    339 
    340 int
    341 pthread_spin_trylock(pthread_spinlock_t *lock)
    342 {
    343 
    344 #ifdef ERRORCHECK
    345 	if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
    346 		return EINVAL;
    347 #endif
    348 
    349 	if (pthread__simple_lock_try(&lock->pts_spin) == 0)
    350 		return EBUSY;
    351 
    352 	return 0;
    353 }
    354 
    355 int
    356 pthread_spin_unlock(pthread_spinlock_t *lock)
    357 {
    358 
    359 #ifdef ERRORCHECK
    360 	if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
    361 		return EINVAL;
    362 #endif
    363 
    364 	pthread__simple_unlock(&lock->pts_spin);
    365 
    366 	return 0;
    367 }
    368