Home | History | Annotate | Line # | Download | only in libpthread
pthread_lock.c revision 1.1.2.7
      1 /*	$NetBSD: pthread_lock.c,v 1.1.2.7 2002/02/08 22:26:55 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 		    (thread->pt_type != PT_THREAD_UPCALL)) {
    102 			PTHREADD_ADD(PTHREADD_SPINPREEMPT);
    103 			pthread__switch(thread, thread->pt_next);
    104 		}
    105 		/* try again */
    106 		count = nspins;
    107 	SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
    108 		thread, thread->pt_spinlocks));
    109 		++thread->pt_spinlocks;
    110 	} while (/*CONSTCOND*/1);
    111 
    112 	PTHREADD_ADD(PTHREADD_SPINLOCKS);
    113 	/* Got it! We're out of here. */
    114 }
    115 
    116 
    117 int
    118 pthread_spintrylock(pthread_t thread, pthread_spin_t *lock)
    119 {
    120 	int ret;
    121 
    122 	SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
    123 		thread, thread->pt_spinlocks));
    124 	++thread->pt_spinlocks;
    125 
    126 	ret = __cpu_simple_lock_try(lock);
    127 
    128 	if (ret == 0) {
    129 	SDPRINTF(("(pthread_spintrylock %p) decrementing spinlock from %d\n",
    130 		thread, thread->pt_spinlocks));
    131 		--thread->pt_spinlocks;
    132 		/* See above. */
    133 		if ((thread->pt_next != NULL) &&
    134 		    (thread->pt_type != PT_THREAD_UPCALL)) {
    135 			PTHREADD_ADD(PTHREADD_SPINPREEMPT);
    136 			pthread__switch(thread, thread->pt_next);
    137 		}
    138 	}
    139 
    140 	return ret;
    141 }
    142 
    143 
    144 void
    145 pthread_spinunlock(pthread_t thread, pthread_spin_t *lock)
    146 {
    147 	__cpu_simple_unlock(lock);
    148 	SDPRINTF(("(pthread_spinunlock %p) decrementing spinlock from %d\n",
    149 		thread, thread->pt_spinlocks));
    150 	--thread->pt_spinlocks;
    151 
    152 	PTHREADD_ADD(PTHREADD_SPINUNLOCKS);
    153 
    154 	/* If we were preempted while holding a spinlock, the
    155 	 * scheduler will notice this and continue us. To be good
    156 	 * citzens, we must now get out of here if that was our
    157 	 * last spinlock.
    158 	 * XXX when will we ever have more than one?
    159 	 */
    160 
    161 	if ((thread->pt_spinlocks == 0) && (thread->pt_next != NULL)
    162 		&& (thread->pt_type != PT_THREAD_UPCALL)) {
    163 		PTHREADD_ADD(PTHREADD_SPINPREEMPT);
    164 		pthread__switch(thread, thread->pt_next);
    165 	}
    166 }
    167 
    168