Home | History | Annotate | Line # | Download | only in libpthread
pthread_spin.c revision 1.5
      1 /*	$NetBSD: pthread_spin.c,v 1.5 2008/04/28 20:23:01 martin Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001, 2006, 2007 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 and Andrew Doran.
      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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Public (POSIX-specified) spinlocks.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __RCSID("$NetBSD: pthread_spin.c,v 1.5 2008/04/28 20:23:01 martin Exp $");
     38 
     39 #include <sys/types.h>
     40 #include <sys/ras.h>
     41 
     42 #include <machine/lock.h>
     43 
     44 #include <errno.h>
     45 #include <unistd.h>
     46 #include <stdio.h>
     47 #include <stdlib.h>
     48 
     49 #include "pthread.h"
     50 #include "pthread_int.h"
     51 
     52 int
     53 pthread_spin_init(pthread_spinlock_t *lock, int pshared)
     54 {
     55 
     56 #ifdef ERRORCHECK
     57 	if (lock == NULL || (pshared != PTHREAD_PROCESS_PRIVATE &&
     58 	    pshared != PTHREAD_PROCESS_SHARED))
     59 		return EINVAL;
     60 #endif
     61 	lock->pts_magic = _PT_SPINLOCK_MAGIC;
     62 
     63 	/*
     64 	 * We don't actually use the pshared flag for anything;
     65 	 * CPU simple locks have all the process-shared properties
     66 	 * that we want anyway.
     67 	 */
     68 	lock->pts_flags = pshared;
     69 	pthread_lockinit(&lock->pts_spin);
     70 
     71 	return 0;
     72 }
     73 
     74 int
     75 pthread_spin_destroy(pthread_spinlock_t *lock)
     76 {
     77 
     78 #ifdef ERRORCHECK
     79 	if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
     80 		return EINVAL;
     81 	if (!__SIMPLELOCK_UNLOCKED_P(&lock->pts_spin))
     82 		return EBUSY;
     83 #endif
     84 
     85 	lock->pts_magic = _PT_SPINLOCK_DEAD;
     86 
     87 	return 0;
     88 }
     89 
     90 int
     91 pthread_spin_lock(pthread_spinlock_t *lock)
     92 {
     93 	pthread_t self;
     94 
     95 #ifdef ERRORCHECK
     96 	if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
     97 		return EINVAL;
     98 #endif
     99 
    100 	self = pthread__self();
    101 	while (pthread__spintrylock(self, &lock->pts_spin) == 0) {
    102 		pthread__smt_pause();
    103 	}
    104 
    105 	return 0;
    106 }
    107 
    108 int
    109 pthread_spin_trylock(pthread_spinlock_t *lock)
    110 {
    111 	pthread_t self;
    112 
    113 #ifdef ERRORCHECK
    114 	if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
    115 		return EINVAL;
    116 #endif
    117 
    118 	self = pthread__self();
    119 	if (pthread__spintrylock(self, &lock->pts_spin) == 0)
    120 		return EBUSY;
    121 	return 0;
    122 }
    123 
    124 int
    125 pthread_spin_unlock(pthread_spinlock_t *lock)
    126 {
    127 	pthread_t self;
    128 
    129 #ifdef ERRORCHECK
    130 	if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
    131 		return EINVAL;
    132 #endif
    133 
    134 	self = pthread__self();
    135 	pthread__spinunlock(self, &lock->pts_spin);
    136 
    137 	return 0;
    138 }
    139