Home | History | Annotate | Line # | Download | only in libpthread
pthread_lock.c revision 1.1.2.6
      1 /*	$NetBSD: pthread_lock.c,v 1.1.2.6 2001/12/30 02:17:08 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 
     41 #include "pthread.h"
     42 #include "pthread_int.h"
     43 
     44 #undef PTHREAD_SA_DEBUG
     45 
     46 #ifdef PTHREAD_SA_DEBUG
     47 #define SDPRINTF(x) DPRINTF(x)
     48 #else
     49 #define SDPRINTF(x)
     50 #endif
     51 
     52 /* How many times to try before checking whether we've been continued. */
     53 #define NSPINS 20	/* XXX arbitrary */
     54 
     55 static int nspins = NSPINS;
     56 
     57 void
     58 pthread_lockinit(pthread_spin_t *lock)
     59 {
     60 
     61 	__cpu_simple_lock_init(lock);
     62 }
     63 
     64 void
     65 pthread_spinlock(pthread_t thread, pthread_spin_t *lock)
     66 {
     67 	int count, ret;
     68 
     69 	count = nspins;
     70 	SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
     71 		thread, thread->pt_spinlocks));
     72 	++thread->pt_spinlocks;
     73 
     74 	do {
     75 		while (((ret = __cpu_simple_lock_try(lock)) == 0) && --count)
     76 			;
     77 
     78 		if (ret == 1)
     79 			break;
     80 
     81 		/* As long as this is uniprocessor, encountering a
     82 		 * locked spinlock is a bug.
     83 		 */
     84 		assert (ret == 1);
     85 
     86 	SDPRINTF(("(pthread_spinlock %p) decrementing spinlock from %d\n",
     87 		thread, thread->pt_spinlocks));
     88 		--thread->pt_spinlocks;
     89 
     90 		/* We may be preempted while spinning. If so, we will
     91 		 * be restarted here if thread->pt_spinlocks is
     92 		 * nonzero, which can happen if:
     93 		 * a) we just got the lock
     94 		 * b) we haven't yet decremented the lock count.
     95 		 * If we're at this point, (b) applies. Therefore,
     96 		 * check if we're being continued, and if so, bail.
     97 		 * (in case (a), we should let the code finish and
     98 		 * we will bail out in pthread_spinunlock()).
     99 		 */
    100 		if (thread->pt_next != NULL) {
    101 			PTHREADD_ADD(PTHREADD_SPINPREEMPT);
    102 			pthread__switch(thread, thread->pt_next);
    103 		}
    104 		/* try again */
    105 		count = nspins;
    106 	SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
    107 		thread, thread->pt_spinlocks));
    108 		++thread->pt_spinlocks;
    109 	} while (/*CONSTCOND*/1);
    110 
    111 	PTHREADD_ADD(PTHREADD_SPINLOCKS);
    112 	/* Got it! We're out of here. */
    113 }
    114 
    115 
    116 int
    117 pthread_spintrylock(pthread_t thread, pthread_spin_t *lock)
    118 {
    119 	int ret;
    120 
    121 	SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
    122 		thread, thread->pt_spinlocks));
    123 	++thread->pt_spinlocks;
    124 
    125 	ret = __cpu_simple_lock_try(lock);
    126 
    127 	if (ret == 0) {
    128 	SDPRINTF(("(pthread_spintrylock %p) decrementing spinlock from %d\n",
    129 		thread, thread->pt_spinlocks));
    130 		--thread->pt_spinlocks;
    131 		/* See above. */
    132 		if (thread->pt_next != NULL) {
    133 			PTHREADD_ADD(PTHREADD_SPINPREEMPT);
    134 			pthread__switch(thread, thread->pt_next);
    135 		}
    136 	}
    137 
    138 	return ret;
    139 }
    140 
    141 
    142 void
    143 pthread_spinunlock(pthread_t thread, pthread_spin_t *lock)
    144 {
    145 	__cpu_simple_unlock(lock);
    146 	SDPRINTF(("(pthread_spinunlock %p) decrementing spinlock from %d\n",
    147 		thread, thread->pt_spinlocks));
    148 	--thread->pt_spinlocks;
    149 
    150 	PTHREADD_ADD(PTHREADD_SPINUNLOCKS);
    151 
    152 	/* If we were preempted while holding a spinlock, the
    153 	 * scheduler will notice this and continue us. To be good
    154 	 * citzens, we must now get out of here if that was our
    155 	 * last spinlock.
    156 	 * XXX when will we ever have more than one?
    157 	 */
    158 
    159 	if ((thread->pt_spinlocks == 0) && (thread->pt_next != NULL)) {
    160 		PTHREADD_ADD(PTHREADD_SPINPREEMPT);
    161 		pthread__switch(thread, thread->pt_next);
    162 	}
    163 }
    164 
    165