Home | History | Annotate | Line # | Download | only in libpthread
pthread_lock.c revision 1.3
      1  1.3  christos /*	$NetBSD: pthread_lock.c,v 1.3 2003/01/18 18:45:55 christos Exp $	*/
      2  1.2   thorpej 
      3  1.2   thorpej /*-
      4  1.2   thorpej  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  1.2   thorpej  * All rights reserved.
      6  1.2   thorpej  *
      7  1.2   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2   thorpej  * by Nathan J. Williams.
      9  1.2   thorpej  *
     10  1.2   thorpej  * Redistribution and use in source and binary forms, with or without
     11  1.2   thorpej  * modification, are permitted provided that the following conditions
     12  1.2   thorpej  * are met:
     13  1.2   thorpej  * 1. Redistributions of source code must retain the above copyright
     14  1.2   thorpej  *    notice, this list of conditions and the following disclaimer.
     15  1.2   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17  1.2   thorpej  *    documentation and/or other materials provided with the distribution.
     18  1.2   thorpej  * 3. All advertising materials mentioning features or use of this software
     19  1.2   thorpej  *    must display the following acknowledgement:
     20  1.2   thorpej  *        This product includes software developed by the NetBSD
     21  1.2   thorpej  *        Foundation, Inc. and its contributors.
     22  1.2   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.2   thorpej  *    contributors may be used to endorse or promote products derived
     24  1.2   thorpej  *    from this software without specific prior written permission.
     25  1.2   thorpej  *
     26  1.2   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.2   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.2   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.2   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.2   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.2   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.2   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.2   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.2   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.2   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.2   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37  1.2   thorpej  */
     38  1.2   thorpej 
     39  1.2   thorpej #include <sys/param.h>
     40  1.2   thorpej #include <sys/ras.h>
     41  1.2   thorpej #include <sys/sysctl.h>
     42  1.2   thorpej 
     43  1.2   thorpej #include <assert.h>
     44  1.2   thorpej #include <errno.h>
     45  1.2   thorpej #include <unistd.h>
     46  1.2   thorpej 
     47  1.2   thorpej #include "pthread.h"
     48  1.2   thorpej #include "pthread_int.h"
     49  1.2   thorpej 
     50  1.2   thorpej #undef PTHREAD_SPIN_DEBUG
     51  1.2   thorpej 
     52  1.2   thorpej #ifdef PTHREAD_SPIN_DEBUG
     53  1.2   thorpej #define SDPRINTF(x) DPRINTF(x)
     54  1.2   thorpej #else
     55  1.2   thorpej #define SDPRINTF(x)
     56  1.2   thorpej #endif
     57  1.2   thorpej 
     58  1.2   thorpej /* How many times to try before checking whether we've been continued. */
     59  1.2   thorpej #define NSPINS 1	/* no point in actually spinning until MP works */
     60  1.2   thorpej 
     61  1.2   thorpej static int nspins = NSPINS;
     62  1.2   thorpej 
     63  1.2   thorpej extern char pthread__lock_ras_start[], pthread__lock_ras_end[];
     64  1.2   thorpej 
     65  1.2   thorpej static void
     66  1.2   thorpej pthread__ras_simple_lock_init(__cpu_simple_lock_t *alp)
     67  1.2   thorpej {
     68  1.2   thorpej 
     69  1.2   thorpej 	*alp = __SIMPLELOCK_UNLOCKED;
     70  1.2   thorpej }
     71  1.2   thorpej 
     72  1.2   thorpej static int
     73  1.2   thorpej pthread__ras_simple_lock_try(__cpu_simple_lock_t *alp)
     74  1.2   thorpej {
     75  1.2   thorpej 	__cpu_simple_lock_t old;
     76  1.2   thorpej 
     77  1.2   thorpej 	/* This is the atomic sequence. */
     78  1.2   thorpej 	__asm __volatile("pthread__lock_ras_start:");
     79  1.2   thorpej 	old = *alp;
     80  1.2   thorpej 	*alp = __SIMPLELOCK_LOCKED;
     81  1.2   thorpej 	__asm __volatile("pthread__lock_ras_end:");
     82  1.2   thorpej 
     83  1.2   thorpej 	return (old == __SIMPLELOCK_UNLOCKED);
     84  1.2   thorpej }
     85  1.2   thorpej 
     86  1.2   thorpej static void
     87  1.2   thorpej pthread__ras_simple_unlock(__cpu_simple_lock_t *alp)
     88  1.2   thorpej {
     89  1.2   thorpej 
     90  1.2   thorpej 	*alp = __SIMPLELOCK_UNLOCKED;
     91  1.2   thorpej }
     92  1.2   thorpej 
     93  1.2   thorpej static const struct pthread_lock_ops pthread__lock_ops_ras = {
     94  1.2   thorpej 	pthread__ras_simple_lock_init,
     95  1.2   thorpej 	pthread__ras_simple_lock_try,
     96  1.2   thorpej 	pthread__ras_simple_unlock,
     97  1.2   thorpej };
     98  1.2   thorpej 
     99  1.2   thorpej static void
    100  1.2   thorpej pthread__atomic_simple_lock_init(__cpu_simple_lock_t *alp)
    101  1.2   thorpej {
    102  1.2   thorpej 
    103  1.2   thorpej 	__cpu_simple_lock_init(alp);
    104  1.2   thorpej }
    105  1.2   thorpej 
    106  1.2   thorpej static int
    107  1.2   thorpej pthread__atomic_simple_lock_try(__cpu_simple_lock_t *alp)
    108  1.2   thorpej {
    109  1.2   thorpej 
    110  1.2   thorpej 	return (__cpu_simple_lock_try(alp));
    111  1.2   thorpej }
    112  1.2   thorpej 
    113  1.2   thorpej static void
    114  1.2   thorpej pthread__atomic_simple_unlock(__cpu_simple_lock_t *alp)
    115  1.2   thorpej {
    116  1.2   thorpej 
    117  1.2   thorpej 	__cpu_simple_unlock(alp);
    118  1.2   thorpej }
    119  1.2   thorpej 
    120  1.2   thorpej static const struct pthread_lock_ops pthread__lock_ops_atomic = {
    121  1.2   thorpej 	pthread__atomic_simple_lock_init,
    122  1.2   thorpej 	pthread__atomic_simple_lock_try,
    123  1.2   thorpej 	pthread__atomic_simple_unlock,
    124  1.2   thorpej };
    125  1.2   thorpej 
    126  1.2   thorpej /*
    127  1.2   thorpej  * We default to pointing to the RAS primitives; we might need to use
    128  1.2   thorpej  * locks early, but before main() starts.  This is safe, since no other
    129  1.2   thorpej  * threads will be active for the process, so atomicity will not be
    130  1.2   thorpej  * required.
    131  1.2   thorpej  */
    132  1.2   thorpej const struct pthread_lock_ops *pthread__lock_ops = &pthread__lock_ops_ras;
    133  1.2   thorpej 
    134  1.2   thorpej /*
    135  1.2   thorpej  * Initialize the locking primitives.  On uniprocessors, we always
    136  1.2   thorpej  * use Restartable Atomic Sequences if they are available.  Otherwise,
    137  1.2   thorpej  * we fall back onto machine-dependent atomic lock primitives.
    138  1.2   thorpej  */
    139  1.2   thorpej void
    140  1.2   thorpej pthread__lockprim_init(void)
    141  1.2   thorpej {
    142  1.2   thorpej 	int mib[2];
    143  1.2   thorpej 	size_t len;
    144  1.2   thorpej 	int ncpu;
    145  1.2   thorpej 
    146  1.2   thorpej 	mib[0] = CTL_HW;
    147  1.2   thorpej 	mib[1] = HW_NCPU;
    148  1.2   thorpej 
    149  1.2   thorpej 	len = sizeof(ncpu);
    150  1.2   thorpej 	sysctl(mib, 2, &ncpu, &len, NULL, 0);
    151  1.2   thorpej 
    152  1.3  christos 	if (ncpu == 1 && rasctl(pthread__lock_ras_start,
    153  1.3  christos 	    (size_t)(pthread__lock_ras_end - pthread__lock_ras_start),
    154  1.3  christos 	    RAS_INSTALL) == 0) {
    155  1.2   thorpej 		pthread__lock_ops = &pthread__lock_ops_ras;
    156  1.2   thorpej 		return;
    157  1.2   thorpej 	}
    158  1.2   thorpej 
    159  1.2   thorpej 	pthread__lock_ops = &pthread__lock_ops_atomic;
    160  1.2   thorpej }
    161  1.2   thorpej 
    162  1.2   thorpej void
    163  1.2   thorpej pthread_lockinit(pthread_spin_t *lock)
    164  1.2   thorpej {
    165  1.2   thorpej 
    166  1.2   thorpej 	pthread__simple_lock_init(lock);
    167  1.2   thorpej }
    168  1.2   thorpej 
    169  1.2   thorpej void
    170  1.2   thorpej pthread_spinlock(pthread_t thread, pthread_spin_t *lock)
    171  1.2   thorpej {
    172  1.2   thorpej 	int count, ret;
    173  1.2   thorpej 
    174  1.2   thorpej 	count = nspins;
    175  1.2   thorpej 	SDPRINTF(("(pthread_spinlock %p) incrementing spinlock %p (count %d)\n",
    176  1.2   thorpej 		thread, lock, thread->pt_spinlocks));
    177  1.2   thorpej #ifdef PTHREAD_SPIN_DEBUG
    178  1.2   thorpej 	if(!(thread->pt_spinlocks >= 0)) {
    179  1.2   thorpej 		(void)kill(getpid(), SIGABRT);
    180  1.2   thorpej 		_exit(1);
    181  1.2   thorpej 	}
    182  1.2   thorpej #endif
    183  1.2   thorpej 	++thread->pt_spinlocks;
    184  1.2   thorpej 
    185  1.2   thorpej 	do {
    186  1.2   thorpej 		while (((ret = pthread__simple_lock_try(lock)) == 0) && --count)
    187  1.2   thorpej 			;
    188  1.2   thorpej 
    189  1.2   thorpej 		if (ret == 1)
    190  1.2   thorpej 			break;
    191  1.2   thorpej 
    192  1.2   thorpej 	SDPRINTF(("(pthread_spinlock %p) decrementing spinlock %p (count %d)\n",
    193  1.2   thorpej 		thread, lock, thread->pt_spinlocks));
    194  1.2   thorpej 		--thread->pt_spinlocks;
    195  1.2   thorpej 
    196  1.2   thorpej 		/*
    197  1.2   thorpej 		 * We may be preempted while spinning. If so, we will
    198  1.2   thorpej 		 * be restarted here if thread->pt_spinlocks is
    199  1.2   thorpej 		 * nonzero, which can happen if:
    200  1.2   thorpej 		 * a) we just got the lock
    201  1.2   thorpej 		 * b) we haven't yet decremented the lock count.
    202  1.2   thorpej 		 * If we're at this point, (b) applies. Therefore,
    203  1.2   thorpej 		 * check if we're being continued, and if so, bail.
    204  1.2   thorpej 		 * (in case (a), we should let the code finish and
    205  1.2   thorpej 		 * we will bail out in pthread_spinunlock()).
    206  1.2   thorpej 		 */
    207  1.2   thorpej 		if (thread->pt_next != NULL) {
    208  1.2   thorpej 			PTHREADD_ADD(PTHREADD_SPINPREEMPT);
    209  1.2   thorpej 			pthread__switch(thread, thread->pt_next);
    210  1.2   thorpej 		}
    211  1.2   thorpej 		/* try again */
    212  1.2   thorpej 		count = nspins;
    213  1.2   thorpej 	SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
    214  1.2   thorpej 		thread, thread->pt_spinlocks));
    215  1.2   thorpej 		++thread->pt_spinlocks;
    216  1.2   thorpej 	} while (/*CONSTCOND*/1);
    217  1.2   thorpej 
    218  1.2   thorpej 	PTHREADD_ADD(PTHREADD_SPINLOCKS);
    219  1.2   thorpej 	/* Got it! We're out of here. */
    220  1.2   thorpej }
    221  1.2   thorpej 
    222  1.2   thorpej 
    223  1.2   thorpej int
    224  1.2   thorpej pthread_spintrylock(pthread_t thread, pthread_spin_t *lock)
    225  1.2   thorpej {
    226  1.2   thorpej 	int ret;
    227  1.2   thorpej 
    228  1.2   thorpej 	SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
    229  1.2   thorpej 		thread, thread->pt_spinlocks));
    230  1.2   thorpej 	++thread->pt_spinlocks;
    231  1.2   thorpej 
    232  1.2   thorpej 	ret = pthread__simple_lock_try(lock);
    233  1.2   thorpej 
    234  1.2   thorpej 	if (ret == 0) {
    235  1.2   thorpej 	SDPRINTF(("(pthread_spintrylock %p) decrementing spinlock from %d\n",
    236  1.2   thorpej 		thread, thread->pt_spinlocks));
    237  1.2   thorpej 		--thread->pt_spinlocks;
    238  1.2   thorpej 		/* See above. */
    239  1.2   thorpej 		if (thread->pt_next != NULL) {
    240  1.2   thorpej 			PTHREADD_ADD(PTHREADD_SPINPREEMPT);
    241  1.2   thorpej 			pthread__switch(thread, thread->pt_next);
    242  1.2   thorpej 		}
    243  1.2   thorpej 	}
    244  1.2   thorpej 
    245  1.2   thorpej 	return ret;
    246  1.2   thorpej }
    247  1.2   thorpej 
    248  1.2   thorpej 
    249  1.2   thorpej void
    250  1.2   thorpej pthread_spinunlock(pthread_t thread, pthread_spin_t *lock)
    251  1.2   thorpej {
    252  1.2   thorpej 
    253  1.2   thorpej 	pthread__simple_unlock(lock);
    254  1.2   thorpej 	SDPRINTF(("(pthread_spinunlock %p) decrementing spinlock %p (count %d)\n",
    255  1.2   thorpej 		thread, lock, thread->pt_spinlocks));
    256  1.2   thorpej 	--thread->pt_spinlocks;
    257  1.2   thorpej #ifdef PTHREAD_SPIN_DEBUG
    258  1.2   thorpej 	if (!(thread->pt_spinlocks >= 0)) {
    259  1.2   thorpej 		(void)kill(getpid(), SIGABRT);
    260  1.2   thorpej 		_exit(1);
    261  1.2   thorpej 	}
    262  1.2   thorpej #endif
    263  1.2   thorpej 	PTHREADD_ADD(PTHREADD_SPINUNLOCKS);
    264  1.2   thorpej 
    265  1.2   thorpej 	/*
    266  1.2   thorpej 	 * If we were preempted while holding a spinlock, the
    267  1.2   thorpej 	 * scheduler will notice this and continue us. To be good
    268  1.2   thorpej 	 * citzens, we must now get out of here if that was our
    269  1.2   thorpej 	 * last spinlock.
    270  1.2   thorpej 	 * XXX when will we ever have more than one?
    271  1.2   thorpej 	 */
    272  1.2   thorpej 
    273  1.2   thorpej 	if ((thread->pt_spinlocks == 0) && (thread->pt_next != NULL)) {
    274  1.2   thorpej 		PTHREADD_ADD(PTHREADD_SPINPREEMPT);
    275  1.2   thorpej 		pthread__switch(thread, thread->pt_next);
    276  1.2   thorpej 	}
    277  1.2   thorpej }
    278  1.2   thorpej 
    279  1.2   thorpej 
    280  1.2   thorpej /*
    281  1.2   thorpej  * Public (POSIX-specified) spinlocks.
    282  1.2   thorpej  * These don't interact with the spin-preemption code, nor do they
    283  1.2   thorpej  * perform any adaptive sleeping.
    284  1.2   thorpej  */
    285  1.2   thorpej 
    286  1.2   thorpej int
    287  1.2   thorpej pthread_spin_init(pthread_spinlock_t *lock, int pshared)
    288  1.2   thorpej {
    289  1.2   thorpej 
    290  1.2   thorpej #ifdef ERRORCHECK
    291  1.2   thorpej 	if ((lock == NULL) ||
    292  1.2   thorpej 	    ((pshared != PTHREAD_PROCESS_PRIVATE) &&
    293  1.2   thorpej 		(pshared != PTHREAD_PROCESS_SHARED)))
    294  1.2   thorpej 		return EINVAL;
    295  1.2   thorpej #endif
    296  1.2   thorpej 	lock->pts_magic = _PT_SPINLOCK_MAGIC;
    297  1.2   thorpej 	/*
    298  1.2   thorpej 	 * We don't actually use the pshared flag for anything;
    299  1.2   thorpej 	 * cpu simple locks have all the process-shared properties
    300  1.2   thorpej 	 * that we want anyway.
    301  1.2   thorpej 	 */
    302  1.2   thorpej 	lock->pts_flags = pshared;
    303  1.2   thorpej 	pthread_lockinit(&lock->pts_spin);
    304  1.2   thorpej 
    305  1.2   thorpej 	return 0;
    306  1.2   thorpej }
    307  1.2   thorpej 
    308  1.2   thorpej int
    309  1.2   thorpej pthread_spin_destroy(pthread_spinlock_t *lock)
    310  1.2   thorpej {
    311  1.2   thorpej 
    312  1.2   thorpej #ifdef ERRORCHECK
    313  1.2   thorpej 	if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
    314  1.2   thorpej 		return EINVAL;
    315  1.2   thorpej 
    316  1.2   thorpej 	if (lock->pts_spin != __SIMPLELOCK_UNLOCKED)
    317  1.2   thorpej 		return EBUSY;
    318  1.2   thorpej #endif
    319  1.2   thorpej 
    320  1.2   thorpej 	lock->pts_magic = _PT_SPINLOCK_DEAD;
    321  1.2   thorpej 
    322  1.2   thorpej 	return 0;
    323  1.2   thorpej }
    324  1.2   thorpej 
    325  1.2   thorpej int
    326  1.2   thorpej pthread_spin_lock(pthread_spinlock_t *lock)
    327  1.2   thorpej {
    328  1.2   thorpej 
    329  1.2   thorpej #ifdef ERRORCHECK
    330  1.2   thorpej 	if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
    331  1.2   thorpej 		return EINVAL;
    332  1.2   thorpej #endif
    333  1.2   thorpej 
    334  1.2   thorpej 	while (pthread__simple_lock_try(&lock->pts_spin) == 0)
    335  1.2   thorpej 		/* spin */ ;
    336  1.2   thorpej 
    337  1.2   thorpej 	return 0;
    338  1.2   thorpej }
    339  1.2   thorpej 
    340  1.2   thorpej int
    341  1.2   thorpej pthread_spin_trylock(pthread_spinlock_t *lock)
    342  1.2   thorpej {
    343  1.2   thorpej 
    344  1.2   thorpej #ifdef ERRORCHECK
    345  1.2   thorpej 	if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
    346  1.2   thorpej 		return EINVAL;
    347  1.2   thorpej #endif
    348  1.2   thorpej 
    349  1.2   thorpej 	if (pthread__simple_lock_try(&lock->pts_spin) == 0)
    350  1.2   thorpej 		return EBUSY;
    351  1.2   thorpej 
    352  1.2   thorpej 	return 0;
    353  1.2   thorpej }
    354  1.2   thorpej 
    355  1.2   thorpej int
    356  1.2   thorpej pthread_spin_unlock(pthread_spinlock_t *lock)
    357  1.2   thorpej {
    358  1.2   thorpej 
    359  1.2   thorpej #ifdef ERRORCHECK
    360  1.2   thorpej 	if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
    361  1.2   thorpej 		return EINVAL;
    362  1.2   thorpej #endif
    363  1.2   thorpej 
    364  1.2   thorpej 	pthread__simple_unlock(&lock->pts_spin);
    365  1.2   thorpej 
    366  1.2   thorpej 	return 0;
    367  1.2   thorpej }
    368