Home | History | Annotate | Line # | Download | only in libpthread
pthread_lock.c revision 1.1.2.9
      1 /*	$NetBSD: pthread_lock.c,v 1.1.2.9 2002/10/16 18:34:40 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 <assert.h>
     40 #include <errno.h>
     41 
     42 #include "pthread.h"
     43 #include "pthread_int.h"
     44 
     45 #undef PTHREAD_SA_DEBUG
     46 
     47 #ifdef PTHREAD_SA_DEBUG
     48 #define SDPRINTF(x) DPRINTF(x)
     49 #else
     50 #define SDPRINTF(x)
     51 #endif
     52 
     53 /* How many times to try before checking whether we've been continued. */
     54 #define NSPINS 20	/* XXX arbitrary */
     55 
     56 static int nspins = NSPINS;
     57 
     58 void
     59 pthread_lockinit(pthread_spin_t *lock)
     60 {
     61 
     62 	__cpu_simple_lock_init(lock);
     63 }
     64 
     65 void
     66 pthread_spinlock(pthread_t thread, pthread_spin_t *lock)
     67 {
     68 	int count, ret;
     69 
     70 	count = nspins;
     71 	SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
     72 		thread, thread->pt_spinlocks));
     73 	++thread->pt_spinlocks;
     74 
     75 	do {
     76 		while (((ret = __cpu_simple_lock_try(lock)) == 0) && --count)
     77 			;
     78 
     79 		if (ret == 1)
     80 			break;
     81 
     82 		/*
     83 		 * As long as this is uniprocessor, encountering a
     84 		 * locked spinlock is a bug.
     85 		 */
     86 		assert (ret == 1);
     87 
     88 	SDPRINTF(("(pthread_spinlock %p) decrementing spinlock from %d\n",
     89 		thread, thread->pt_spinlocks));
     90 		--thread->pt_spinlocks;
     91 
     92 		/*
     93 		 * We may be preempted while spinning. If so, we will
     94 		 * be restarted here if thread->pt_spinlocks is
     95 		 * nonzero, which can happen if:
     96 		 * a) we just got the lock
     97 		 * b) we haven't yet decremented the lock count.
     98 		 * If we're at this point, (b) applies. Therefore,
     99 		 * check if we're being continued, and if so, bail.
    100 		 * (in case (a), we should let the code finish and
    101 		 * we will bail out in pthread_spinunlock()).
    102 		 */
    103 		if ((thread->pt_next != NULL) &&
    104 		    (thread->pt_type != PT_THREAD_UPCALL)) {
    105 			PTHREADD_ADD(PTHREADD_SPINPREEMPT);
    106 			pthread__switch(thread, thread->pt_next);
    107 		}
    108 		/* try again */
    109 		count = nspins;
    110 	SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
    111 		thread, thread->pt_spinlocks));
    112 		++thread->pt_spinlocks;
    113 	} while (/*CONSTCOND*/1);
    114 
    115 	PTHREADD_ADD(PTHREADD_SPINLOCKS);
    116 	/* Got it! We're out of here. */
    117 }
    118 
    119 
    120 int
    121 pthread_spintrylock(pthread_t thread, pthread_spin_t *lock)
    122 {
    123 	int ret;
    124 
    125 	SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
    126 		thread, thread->pt_spinlocks));
    127 	++thread->pt_spinlocks;
    128 
    129 	ret = __cpu_simple_lock_try(lock);
    130 
    131 	if (ret == 0) {
    132 	SDPRINTF(("(pthread_spintrylock %p) decrementing spinlock from %d\n",
    133 		thread, thread->pt_spinlocks));
    134 		--thread->pt_spinlocks;
    135 		/* See above. */
    136 		if ((thread->pt_next != NULL) &&
    137 		    (thread->pt_type != PT_THREAD_UPCALL)) {
    138 			PTHREADD_ADD(PTHREADD_SPINPREEMPT);
    139 			pthread__switch(thread, thread->pt_next);
    140 		}
    141 	}
    142 
    143 	return ret;
    144 }
    145 
    146 
    147 void
    148 pthread_spinunlock(pthread_t thread, pthread_spin_t *lock)
    149 {
    150 
    151 	__cpu_simple_unlock(lock);
    152 	SDPRINTF(("(pthread_spinunlock %p) decrementing spinlock from %d\n",
    153 		thread, thread->pt_spinlocks));
    154 	--thread->pt_spinlocks;
    155 
    156 	PTHREADD_ADD(PTHREADD_SPINUNLOCKS);
    157 
    158 	/*
    159 	 * If we were preempted while holding a spinlock, the
    160 	 * scheduler will notice this and continue us. To be good
    161 	 * citzens, we must now get out of here if that was our
    162 	 * last spinlock.
    163 	 * XXX when will we ever have more than one?
    164 	 */
    165 
    166 	if ((thread->pt_spinlocks == 0) && (thread->pt_next != NULL)
    167 		&& (thread->pt_type != PT_THREAD_UPCALL)) {
    168 		PTHREADD_ADD(PTHREADD_SPINPREEMPT);
    169 		pthread__switch(thread, thread->pt_next);
    170 	}
    171 }
    172 
    173 
    174 /*
    175  * Public (POSIX-specified) spinlocks.
    176  * These don't interact with the spin-preemption code, nor do they
    177  * perform any adaptive sleeping.
    178  */
    179 
    180 int
    181 pthread_spin_init(pthread_spinlock_t *lock, int pshared)
    182 {
    183 
    184 #ifdef ERRORCHECK
    185 	if ((lock == NULL) ||
    186 	    ((pshared != PTHREAD_PROCESS_PRIVATE) &&
    187 		(pshared != PTHREAD_PROCESS_SHARED)))
    188 		return EINVAL;
    189 #endif
    190 	lock->pts_magic = _PT_SPINLOCK_MAGIC;
    191 	/*
    192 	 * We don't actually use the pshared flag for anything;
    193 	 * cpu simple locks have all the process-shared properties
    194 	 * that we want anyway.
    195 	 */
    196 	lock->pts_flags = pshared;
    197 	pthread_lockinit(&lock->pts_spin);
    198 
    199 	return 0;
    200 }
    201 
    202 int
    203 pthread_spin_destroy(pthread_spinlock_t *lock)
    204 {
    205 
    206 #ifdef ERRORCHECK
    207 	if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
    208 		return EINVAL;
    209 
    210 	if (lock->pts_spin != __SIMPLELOCK_UNLOCKED)
    211 		return EBUSY;
    212 #endif
    213 
    214 	lock->pts_magic = _PT_SPINLOCK_DEAD;
    215 
    216 	return 0;
    217 }
    218 
    219 int
    220 pthread_spin_lock(pthread_spinlock_t *lock)
    221 {
    222 
    223 #ifdef ERRORCHECK
    224 	if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
    225 		return EINVAL;
    226 #endif
    227 
    228 	__cpu_simple_lock(&lock->pts_spin);
    229 
    230 	return 0;
    231 }
    232 
    233 int
    234 pthread_spin_trylock(pthread_spinlock_t *lock)
    235 {
    236 
    237 #ifdef ERRORCHECK
    238 	if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
    239 		return EINVAL;
    240 #endif
    241 
    242 	if (__cpu_simple_lock_try(&lock->pts_spin) == 0)
    243 		return EBUSY;
    244 
    245 	return 0;
    246 }
    247 
    248 int
    249 pthread_spin_unlock(pthread_spinlock_t *lock)
    250 {
    251 
    252 #ifdef ERRORCHECK
    253 	if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
    254 		return EINVAL;
    255 #endif
    256 
    257 	__cpu_simple_unlock(&lock->pts_spin);
    258 
    259 	return 0;
    260 }
    261 
    262