Home | History | Annotate | Line # | Download | only in libpthread
pthread_lock.c revision 1.5
      1 /*	$NetBSD: pthread_lock.c,v 1.5 2003/02/15 04:37:04 nathanw 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 <errno.h>
     44 #include <unistd.h>
     45 
     46 #include "pthread.h"
     47 #include "pthread_int.h"
     48 
     49 #ifdef PTHREAD_SPIN_DEBUG_PRINT
     50 #define SDPRINTF(x) DPRINTF(x)
     51 #else
     52 #define SDPRINTF(x)
     53 #endif
     54 
     55 /* How many times to try before checking whether we've been continued. */
     56 #define NSPINS 1	/* no point in actually spinning until MP works */
     57 
     58 static int nspins = NSPINS;
     59 
     60 extern void pthread__lock_ras_start(void), pthread__lock_ras_end(void);
     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 	/* This is the atomic sequence. */
     75 	__asm __volatile("pthread__lock_ras_start:");
     76 	old = *alp;
     77 	*alp = __SIMPLELOCK_LOCKED;
     78 	__asm __volatile("pthread__lock_ras_end:");
     79 
     80 	return (old == __SIMPLELOCK_UNLOCKED);
     81 }
     82 
     83 static void
     84 pthread__ras_simple_unlock(__cpu_simple_lock_t *alp)
     85 {
     86 
     87 	*alp = __SIMPLELOCK_UNLOCKED;
     88 }
     89 
     90 static const struct pthread_lock_ops pthread__lock_ops_ras = {
     91 	pthread__ras_simple_lock_init,
     92 	pthread__ras_simple_lock_try,
     93 	pthread__ras_simple_unlock,
     94 };
     95 
     96 static void
     97 pthread__atomic_simple_lock_init(__cpu_simple_lock_t *alp)
     98 {
     99 
    100 	__cpu_simple_lock_init(alp);
    101 }
    102 
    103 static int
    104 pthread__atomic_simple_lock_try(__cpu_simple_lock_t *alp)
    105 {
    106 
    107 	return (__cpu_simple_lock_try(alp));
    108 }
    109 
    110 static void
    111 pthread__atomic_simple_unlock(__cpu_simple_lock_t *alp)
    112 {
    113 
    114 	__cpu_simple_unlock(alp);
    115 }
    116 
    117 static const struct pthread_lock_ops pthread__lock_ops_atomic = {
    118 	pthread__atomic_simple_lock_init,
    119 	pthread__atomic_simple_lock_try,
    120 	pthread__atomic_simple_unlock,
    121 };
    122 
    123 /*
    124  * We default to pointing to the RAS primitives; we might need to use
    125  * locks early, but before main() starts.  This is safe, since no other
    126  * threads will be active for the process, so atomicity will not be
    127  * required.
    128  */
    129 const struct pthread_lock_ops *pthread__lock_ops = &pthread__lock_ops_ras;
    130 
    131 /*
    132  * Initialize the locking primitives.  On uniprocessors, we always
    133  * use Restartable Atomic Sequences if they are available.  Otherwise,
    134  * we fall back onto machine-dependent atomic lock primitives.
    135  */
    136 void
    137 pthread__lockprim_init(void)
    138 {
    139 	int mib[2];
    140 	size_t len;
    141 	int ncpu;
    142 
    143 	mib[0] = CTL_HW;
    144 	mib[1] = HW_NCPU;
    145 
    146 	len = sizeof(ncpu);
    147 	sysctl(mib, 2, &ncpu, &len, NULL, 0);
    148 
    149 	if (ncpu == 1 && rasctl((void *)pthread__lock_ras_start,
    150 	    (size_t)((uintptr_t)pthread__lock_ras_end -
    151 	             (uintptr_t)pthread__lock_ras_start),
    152 	    RAS_INSTALL) == 0) {
    153 		pthread__lock_ops = &pthread__lock_ops_ras;
    154 		return;
    155 	}
    156 
    157 	pthread__lock_ops = &pthread__lock_ops_atomic;
    158 }
    159 
    160 void
    161 pthread_lockinit(pthread_spin_t *lock)
    162 {
    163 
    164 	pthread__simple_lock_init(lock);
    165 }
    166 
    167 void
    168 pthread_spinlock(pthread_t thread, pthread_spin_t *lock)
    169 {
    170 	int count, ret;
    171 
    172 	count = nspins;
    173 	SDPRINTF(("(pthread_spinlock %p) incrementing spinlock %p (count %d)\n",
    174 		thread, lock, thread->pt_spinlocks));
    175 #ifdef PTHREAD_SPIN_DEBUG
    176 	pthread__assert(thread->pt_spinlocks >= 0);
    177 #endif
    178 	++thread->pt_spinlocks;
    179 
    180 	do {
    181 		while (((ret = pthread__simple_lock_try(lock)) == 0) && --count)
    182 			;
    183 
    184 		if (ret == 1)
    185 			break;
    186 
    187 	SDPRINTF(("(pthread_spinlock %p) decrementing spinlock %p (count %d)\n",
    188 		thread, lock, thread->pt_spinlocks));
    189 		--thread->pt_spinlocks;
    190 
    191 		/*
    192 		 * We may be preempted while spinning. If so, we will
    193 		 * be restarted here if thread->pt_spinlocks is
    194 		 * nonzero, which can happen if:
    195 		 * a) we just got the lock
    196 		 * b) we haven't yet decremented the lock count.
    197 		 * If we're at this point, (b) applies. Therefore,
    198 		 * check if we're being continued, and if so, bail.
    199 		 * (in case (a), we should let the code finish and
    200 		 * we will bail out in pthread_spinunlock()).
    201 		 */
    202 		if (thread->pt_next != NULL) {
    203 			PTHREADD_ADD(PTHREADD_SPINPREEMPT);
    204 			pthread__switch(thread, thread->pt_next);
    205 		}
    206 		/* try again */
    207 		count = nspins;
    208 	SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
    209 		thread, thread->pt_spinlocks));
    210 		++thread->pt_spinlocks;
    211 	} while (/*CONSTCOND*/1);
    212 
    213 	PTHREADD_ADD(PTHREADD_SPINLOCKS);
    214 	/* Got it! We're out of here. */
    215 }
    216 
    217 
    218 int
    219 pthread_spintrylock(pthread_t thread, pthread_spin_t *lock)
    220 {
    221 	int ret;
    222 
    223 	SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
    224 		thread, thread->pt_spinlocks));
    225 	++thread->pt_spinlocks;
    226 
    227 	ret = pthread__simple_lock_try(lock);
    228 
    229 	if (ret == 0) {
    230 	SDPRINTF(("(pthread_spintrylock %p) decrementing spinlock from %d\n",
    231 		thread, thread->pt_spinlocks));
    232 		--thread->pt_spinlocks;
    233 		/* See above. */
    234 		if (thread->pt_next != NULL) {
    235 			PTHREADD_ADD(PTHREADD_SPINPREEMPT);
    236 			pthread__switch(thread, thread->pt_next);
    237 		}
    238 	}
    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 	/*
    258 	 * If we were preempted while holding a spinlock, the
    259 	 * scheduler will notice this and continue us. To be good
    260 	 * citzens, we must now get out of here if that was our
    261 	 * last spinlock.
    262 	 * XXX when will we ever have more than one?
    263 	 */
    264 
    265 	if ((thread->pt_spinlocks == 0) && (thread->pt_next != NULL)) {
    266 		PTHREADD_ADD(PTHREADD_SPINPREEMPT);
    267 		pthread__switch(thread, thread->pt_next);
    268 	}
    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