thr_pspinlock.c revision 149691
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 149691 2005-09-01 15:21:23Z stefanf $ 27 */ 28 29#include <sys/types.h> 30#include <errno.h> 31#include <pthread.h> 32#include <stdint.h> 33#include <stdlib.h> 34 35#include "atomic_ops.h" 36#include "thr_private.h" 37 38#define SPIN_COUNT 10000 39 40__weak_reference(_pthread_spin_init, pthread_spin_init); 41__weak_reference(_pthread_spin_destroy, pthread_spin_destroy); 42__weak_reference(_pthread_spin_trylock, pthread_spin_trylock); 43__weak_reference(_pthread_spin_lock, pthread_spin_lock); 44__weak_reference(_pthread_spin_unlock, pthread_spin_unlock); 45 46int 47_pthread_spin_init(pthread_spinlock_t *lock, int pshared) 48{ 49 struct pthread_spinlock *lck; 50 int ret; 51 52 if (lock == NULL || pshared != PTHREAD_PROCESS_PRIVATE) 53 ret = EINVAL; 54 else if ((lck = malloc(sizeof(struct pthread_spinlock))) == NULL) 55 ret = ENOMEM; 56 else { 57 lck->s_lock = 0; 58 lck->s_owner= NULL; 59 *lock = lck; 60 ret = 0; 61 } 62 63 return (ret); 64} 65 66int 67_pthread_spin_destroy(pthread_spinlock_t *lock) 68{ 69 int ret; 70 71 if (lock == NULL || *lock == NULL) 72 ret = EINVAL; 73 else if ((*lock)->s_owner != NULL) 74 ret = EBUSY; 75 else { 76 free(*lock); 77 *lock = NULL; 78 ret = 0; 79 } 80 81 return (ret); 82} 83 84int 85_pthread_spin_trylock(pthread_spinlock_t *lock) 86{ 87 struct pthread_spinlock *lck; 88 struct pthread *self = _pthread_self(); 89 int oldval, ret; 90 91 if (lock == NULL || (lck = *lock) == NULL) 92 ret = EINVAL; 93 else if (lck->s_owner == self) 94 ret = EDEADLK; 95 else if (lck->s_lock != 0) 96 ret = EBUSY; 97 else { 98 atomic_swap_int((int *)&(lck)->s_lock, 1, &oldval); 99 if (oldval) 100 ret = EBUSY; 101 else { 102 lck->s_owner = _pthread_self(); 103 ret = 0; 104 } 105 } 106 return (ret); 107} 108 109int 110_pthread_spin_lock(pthread_spinlock_t *lock) 111{ 112 struct pthread_spinlock *lck; 113 struct pthread *self = _pthread_self(); 114 int count, oldval, ret; 115 116 if (lock == NULL || (lck = *lock) == NULL) 117 ret = EINVAL; 118 else if (lck->s_owner == self) 119 ret = EDEADLK; 120 else { 121 do { 122 count = SPIN_COUNT; 123 while (lck->s_lock) { 124#ifdef __i386__ 125 /* tell cpu we are spinning */ 126 __asm __volatile("pause"); 127#endif 128 if (--count <= 0) { 129 count = SPIN_COUNT; 130 _pthread_yield(); 131 } 132 } 133 atomic_swap_int((int *)&(lck)->s_lock, 1, &oldval); 134 } while (oldval); 135 136 lck->s_owner = self; 137 ret = 0; 138 } 139 140 return (ret); 141} 142 143int 144_pthread_spin_unlock(pthread_spinlock_t *lock) 145{ 146 struct pthread_spinlock *lck; 147 int ret; 148 149 if (lock == NULL || (lck = *lock) == NULL) 150 ret = EINVAL; 151 else { 152 if (lck->s_owner != _pthread_self()) 153 ret = EPERM; 154 else { 155 lck->s_owner = NULL; 156 atomic_swap_int((int *)&lck->s_lock, 0, &ret); 157 ret = 0; 158 } 159 } 160 161 return (ret); 162} 163 164