]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/libkse/thread/thr_pspinlock.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / lib / libkse / thread / thr_pspinlock.c
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$
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 LT10_COMPAT_PRIVATE(_pthread_spin_init);
41 LT10_COMPAT_DEFAULT(pthread_spin_init);
42 LT10_COMPAT_PRIVATE(_pthread_spin_destroy);
43 LT10_COMPAT_DEFAULT(pthread_spin_destroy);
44 LT10_COMPAT_PRIVATE(_pthread_spin_trylock);
45 LT10_COMPAT_DEFAULT(pthread_spin_trylock);
46 LT10_COMPAT_PRIVATE(_pthread_spin_lock);
47 LT10_COMPAT_DEFAULT(pthread_spin_lock);
48 LT10_COMPAT_PRIVATE(_pthread_spin_unlock);
49 LT10_COMPAT_DEFAULT(pthread_spin_unlock);
50
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
57 int
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
77 int
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
95 int
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
120 int
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
154 int
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