thr_pspinlock.c revision 174112
1/*- 2 * Copyright (c) 2003 David Xu <davidxu@freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: head/lib/libkse/thread/thr_pspinlock.c 174112 2007-11-30 17:20:29Z deischen $ 27 */ 28 29#include "namespace.h" 30#include <sys/types.h> 31#include <errno.h> 32#include <pthread.h> 33#include <stdint.h> 34#include <stdlib.h> 35#include "un-namespace.h" 36 37#include "atomic_ops.h" 38#include "thr_private.h" 39 40#define SPIN_COUNT 10000 41 42LT10_COMPAT_PRIVATE(_pthread_spin_init); 43LT10_COMPAT_DEFAULT(pthread_spin_init); 44LT10_COMPAT_PRIVATE(_pthread_spin_destroy); 45LT10_COMPAT_DEFAULT(pthread_spin_destroy); 46LT10_COMPAT_PRIVATE(_pthread_spin_trylock); 47LT10_COMPAT_DEFAULT(pthread_spin_trylock); 48LT10_COMPAT_PRIVATE(_pthread_spin_lock); 49LT10_COMPAT_DEFAULT(pthread_spin_lock); 50LT10_COMPAT_PRIVATE(_pthread_spin_unlock); 51LT10_COMPAT_DEFAULT(pthread_spin_unlock); 52 53__weak_reference(_pthread_spin_init, pthread_spin_init); 54__weak_reference(_pthread_spin_destroy, pthread_spin_destroy); 55__weak_reference(_pthread_spin_trylock, pthread_spin_trylock); 56__weak_reference(_pthread_spin_lock, pthread_spin_lock); 57__weak_reference(_pthread_spin_unlock, pthread_spin_unlock); 58 59int 60_pthread_spin_init(pthread_spinlock_t *lock, int pshared) 61{ 62 struct pthread_spinlock *lck; 63 int ret; 64 65 if (lock == NULL || pshared != PTHREAD_PROCESS_PRIVATE) 66 ret = EINVAL; 67 else if ((lck = malloc(sizeof(struct pthread_spinlock))) == NULL) 68 ret = ENOMEM; 69 else { 70 lck->s_lock = 0; 71 lck->s_owner= NULL; 72 *lock = lck; 73 ret = 0; 74 } 75 76 return (ret); 77} 78 79int 80_pthread_spin_destroy(pthread_spinlock_t *lock) 81{ 82 int ret; 83 84 if (lock == NULL || *lock == NULL) 85 ret = EINVAL; 86 else if ((*lock)->s_owner != NULL) 87 ret = EBUSY; 88 else { 89 free(*lock); 90 *lock = NULL; 91 ret = 0; 92 } 93 94 return (ret); 95} 96 97int 98_pthread_spin_trylock(pthread_spinlock_t *lock) 99{ 100 struct pthread_spinlock *lck; 101 struct pthread *self = _pthread_self(); 102 int oldval, ret; 103 104 if (lock == NULL || (lck = *lock) == NULL) 105 ret = EINVAL; 106 else if (lck->s_owner == self) 107 ret = EDEADLK; 108 else if (lck->s_lock != 0) 109 ret = EBUSY; 110 else { 111 atomic_swap_int(&(lck)->s_lock, 1, &oldval); 112 if (oldval) 113 ret = EBUSY; 114 else { 115 lck->s_owner = _pthread_self(); 116 ret = 0; 117 } 118 } 119 return (ret); 120} 121 122int 123_pthread_spin_lock(pthread_spinlock_t *lock) 124{ 125 struct pthread_spinlock *lck; 126 struct pthread *self = _pthread_self(); 127 int count, oldval, ret; 128 129 if (lock == NULL || (lck = *lock) == NULL) 130 ret = EINVAL; 131 else if (lck->s_owner == self) 132 ret = EDEADLK; 133 else { 134 do { 135 count = SPIN_COUNT; 136 while (lck->s_lock) { 137#ifdef __i386__ 138 /* tell cpu we are spinning */ 139 __asm __volatile("pause"); 140#endif 141 if (--count <= 0) { 142 count = SPIN_COUNT; 143 _pthread_yield(); 144 } 145 } 146 atomic_swap_int(&(lck)->s_lock, 1, &oldval); 147 } while (oldval); 148 149 lck->s_owner = self; 150 ret = 0; 151 } 152 153 return (ret); 154} 155 156int 157_pthread_spin_unlock(pthread_spinlock_t *lock) 158{ 159 struct pthread_spinlock *lck; 160 int ret; 161 162 if (lock == NULL || (lck = *lock) == NULL) 163 ret = EINVAL; 164 else { 165 if (lck->s_owner != _pthread_self()) 166 ret = EPERM; 167 else { 168 lck->s_owner = NULL; 169 atomic_swap_int(&lck->s_lock, 0, &ret); 170 ret = 0; 171 } 172 } 173 174 return (ret); 175} 176 177