Home | History | Annotate | Line # | Download | only in libpthread
pthread_spin.c revision 1.3
      1 /*	$NetBSD: pthread_spin.c,v 1.3 2007/11/13 15:57:14 ad 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  * 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 /*
     40  * Public (POSIX-specified) spinlocks.
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __RCSID("$NetBSD: pthread_spin.c,v 1.3 2007/11/13 15:57:14 ad Exp $");
     45 
     46 #include <sys/types.h>
     47 #include <sys/lock.h>
     48 #include <sys/ras.h>
     49 
     50 #include <errno.h>
     51 #include <unistd.h>
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 
     55 #include "pthread.h"
     56 #include "pthread_int.h"
     57 
     58 int
     59 pthread_spin_init(pthread_spinlock_t *lock, int pshared)
     60 {
     61 
     62 #ifdef ERRORCHECK
     63 	if (lock == NULL || (pshared != PTHREAD_PROCESS_PRIVATE &&
     64 	    pshared != PTHREAD_PROCESS_SHARED))
     65 		return EINVAL;
     66 #endif
     67 	lock->pts_magic = _PT_SPINLOCK_MAGIC;
     68 
     69 	/*
     70 	 * We don't actually use the pshared flag for anything;
     71 	 * CPU simple locks have all the process-shared properties
     72 	 * that we want anyway.
     73 	 */
     74 	lock->pts_flags = pshared;
     75 	pthread_lockinit(&lock->pts_spin);
     76 
     77 	return 0;
     78 }
     79 
     80 int
     81 pthread_spin_destroy(pthread_spinlock_t *lock)
     82 {
     83 
     84 #ifdef ERRORCHECK
     85 	if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
     86 		return EINVAL;
     87 	if (!__SIMPLELOCK_UNLOCKED_P(&lock->pts_spin))
     88 		return EBUSY;
     89 #endif
     90 
     91 	lock->pts_magic = _PT_SPINLOCK_DEAD;
     92 
     93 	return 0;
     94 }
     95 
     96 int
     97 pthread_spin_lock(pthread_spinlock_t *lock)
     98 {
     99 	pthread_t self;
    100 
    101 #ifdef ERRORCHECK
    102 	if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
    103 		return EINVAL;
    104 #endif
    105 
    106 	self = pthread__self();
    107 	while (pthread__spintrylock(self, &lock->pts_spin) == 0) {
    108 		pthread__smt_pause();
    109 	}
    110 
    111 	return 0;
    112 }
    113 
    114 int
    115 pthread_spin_trylock(pthread_spinlock_t *lock)
    116 {
    117 	pthread_t self;
    118 
    119 #ifdef ERRORCHECK
    120 	if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
    121 		return EINVAL;
    122 #endif
    123 
    124 	self = pthread__self();
    125 	if (pthread__spintrylock(self, &lock->pts_spin) == 0)
    126 		return EBUSY;
    127 	return 0;
    128 }
    129 
    130 int
    131 pthread_spin_unlock(pthread_spinlock_t *lock)
    132 {
    133 	pthread_t self;
    134 
    135 #ifdef ERRORCHECK
    136 	if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
    137 		return EINVAL;
    138 #endif
    139 
    140 	self = pthread__self();
    141 	pthread__spinunlock(self, &lock->pts_spin);
    142 
    143 	return 0;
    144 }
    145