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