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