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 *
| 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 $
| 26 * $FreeBSD: head/lib/libkse/thread/thr_pspinlock.c 156611 2006-03-13 00:59:51Z deischen $
|
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
| 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
|
| 40LT10_COMPAT_PRIVATE(_pthread_spin_init); 41LT10_COMPAT_DEFAULT(pthread_spin_init); 42LT10_COMPAT_PRIVATE(_pthread_spin_destroy); 43LT10_COMPAT_DEFAULT(pthread_spin_destroy); 44LT10_COMPAT_PRIVATE(_pthread_spin_trylock); 45LT10_COMPAT_DEFAULT(pthread_spin_trylock); 46LT10_COMPAT_PRIVATE(_pthread_spin_lock); 47LT10_COMPAT_DEFAULT(pthread_spin_lock); 48LT10_COMPAT_PRIVATE(_pthread_spin_unlock); 49LT10_COMPAT_DEFAULT(pthread_spin_unlock); 50
|
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
| 51__weak_reference(_pthread_spin_init, pthread_spin_init); 52__weak_reference(_pthread_spin_destroy, pthread_spin_destroy); 53__weak_reference(_pthread_spin_trylock, pthread_spin_trylock); 54__weak_reference(_pthread_spin_lock, pthread_spin_lock); 55__weak_reference(_pthread_spin_unlock, pthread_spin_unlock); 56 57int 58_pthread_spin_init(pthread_spinlock_t *lock, int pshared) 59{ 60 struct pthread_spinlock *lck; 61 int ret; 62 63 if (lock == NULL || pshared != PTHREAD_PROCESS_PRIVATE) 64 ret = EINVAL; 65 else if ((lck = malloc(sizeof(struct pthread_spinlock))) == NULL) 66 ret = ENOMEM; 67 else { 68 lck->s_lock = 0; 69 lck->s_owner= NULL; 70 *lock = lck; 71 ret = 0; 72 } 73 74 return (ret); 75} 76 77int 78_pthread_spin_destroy(pthread_spinlock_t *lock) 79{ 80 int ret; 81 82 if (lock == NULL || *lock == NULL) 83 ret = EINVAL; 84 else if ((*lock)->s_owner != NULL) 85 ret = EBUSY; 86 else { 87 free(*lock); 88 *lock = NULL; 89 ret = 0; 90 } 91 92 return (ret); 93} 94 95int 96_pthread_spin_trylock(pthread_spinlock_t *lock) 97{ 98 struct pthread_spinlock *lck; 99 struct pthread *self = _pthread_self(); 100 int oldval, ret; 101 102 if (lock == NULL || (lck = *lock) == NULL) 103 ret = EINVAL; 104 else if (lck->s_owner == self) 105 ret = EDEADLK; 106 else if (lck->s_lock != 0) 107 ret = EBUSY; 108 else { 109 atomic_swap_int((int *)&(lck)->s_lock, 1, &oldval); 110 if (oldval) 111 ret = EBUSY; 112 else { 113 lck->s_owner = _pthread_self(); 114 ret = 0; 115 } 116 } 117 return (ret); 118} 119 120int 121_pthread_spin_lock(pthread_spinlock_t *lock) 122{ 123 struct pthread_spinlock *lck; 124 struct pthread *self = _pthread_self(); 125 int count, oldval, ret; 126 127 if (lock == NULL || (lck = *lock) == NULL) 128 ret = EINVAL; 129 else if (lck->s_owner == self) 130 ret = EDEADLK; 131 else { 132 do { 133 count = SPIN_COUNT; 134 while (lck->s_lock) { 135#ifdef __i386__ 136 /* tell cpu we are spinning */ 137 __asm __volatile("pause"); 138#endif 139 if (--count <= 0) { 140 count = SPIN_COUNT; 141 _pthread_yield(); 142 } 143 } 144 atomic_swap_int((int *)&(lck)->s_lock, 1, &oldval); 145 } while (oldval); 146 147 lck->s_owner = self; 148 ret = 0; 149 } 150 151 return (ret); 152} 153 154int 155_pthread_spin_unlock(pthread_spinlock_t *lock) 156{ 157 struct pthread_spinlock *lck; 158 int ret; 159 160 if (lock == NULL || (lck = *lock) == NULL) 161 ret = EINVAL; 162 else { 163 if (lck->s_owner != _pthread_self()) 164 ret = EPERM; 165 else { 166 lck->s_owner = NULL; 167 atomic_swap_int((int *)&lck->s_lock, 0, &ret); 168 ret = 0; 169 } 170 } 171 172 return (ret); 173} 174
|