pthread_lock.c revision 1.1.2.12 1 1.1.2.12 nathanw /* $NetBSD: pthread_lock.c,v 1.1.2.12 2002/11/14 04:10:08 nathanw Exp $ */
2 1.1.2.3 nathanw
3 1.1.2.3 nathanw /*-
4 1.1.2.3 nathanw * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1.2.3 nathanw * All rights reserved.
6 1.1.2.3 nathanw *
7 1.1.2.3 nathanw * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.3 nathanw * by Nathan J. Williams.
9 1.1.2.3 nathanw *
10 1.1.2.3 nathanw * Redistribution and use in source and binary forms, with or without
11 1.1.2.3 nathanw * modification, are permitted provided that the following conditions
12 1.1.2.3 nathanw * are met:
13 1.1.2.3 nathanw * 1. Redistributions of source code must retain the above copyright
14 1.1.2.3 nathanw * notice, this list of conditions and the following disclaimer.
15 1.1.2.3 nathanw * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.3 nathanw * notice, this list of conditions and the following disclaimer in the
17 1.1.2.3 nathanw * documentation and/or other materials provided with the distribution.
18 1.1.2.3 nathanw * 3. All advertising materials mentioning features or use of this software
19 1.1.2.3 nathanw * must display the following acknowledgement:
20 1.1.2.3 nathanw * This product includes software developed by the NetBSD
21 1.1.2.3 nathanw * Foundation, Inc. and its contributors.
22 1.1.2.3 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.2.3 nathanw * contributors may be used to endorse or promote products derived
24 1.1.2.3 nathanw * from this software without specific prior written permission.
25 1.1.2.3 nathanw *
26 1.1.2.3 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.2.3 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.2.3 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.2.3 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.2.3 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.2.3 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.2.3 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.2.3 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.2.3 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.2.3 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.2.3 nathanw * POSSIBILITY OF SUCH DAMAGE.
37 1.1.2.3 nathanw */
38 1.1.2.1 nathanw
39 1.1.2.6 nathanw #include <assert.h>
40 1.1.2.8 nathanw #include <errno.h>
41 1.1.2.12 nathanw #include <unistd.h>
42 1.1.2.6 nathanw
43 1.1.2.1 nathanw #include "pthread.h"
44 1.1.2.1 nathanw #include "pthread_int.h"
45 1.1.2.1 nathanw
46 1.1.2.12 nathanw #undef PTHREAD_SPIN_DEBUG
47 1.1.2.6 nathanw
48 1.1.2.12 nathanw #ifdef PTHREAD_SPIN_DEBUG
49 1.1.2.6 nathanw #define SDPRINTF(x) DPRINTF(x)
50 1.1.2.6 nathanw #else
51 1.1.2.6 nathanw #define SDPRINTF(x)
52 1.1.2.6 nathanw #endif
53 1.1.2.6 nathanw
54 1.1.2.1 nathanw /* How many times to try before checking whether we've been continued. */
55 1.1.2.10 nathanw #define NSPINS 1 /* no point in actually spinning until MP works */
56 1.1.2.1 nathanw
57 1.1.2.1 nathanw static int nspins = NSPINS;
58 1.1.2.1 nathanw
59 1.1.2.1 nathanw void
60 1.1.2.6 nathanw pthread_lockinit(pthread_spin_t *lock)
61 1.1.2.2 nathanw {
62 1.1.2.2 nathanw
63 1.1.2.2 nathanw __cpu_simple_lock_init(lock);
64 1.1.2.2 nathanw }
65 1.1.2.2 nathanw
66 1.1.2.2 nathanw void
67 1.1.2.6 nathanw pthread_spinlock(pthread_t thread, pthread_spin_t *lock)
68 1.1.2.1 nathanw {
69 1.1.2.1 nathanw int count, ret;
70 1.1.2.1 nathanw
71 1.1.2.1 nathanw count = nspins;
72 1.1.2.6 nathanw SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
73 1.1.2.6 nathanw thread, thread->pt_spinlocks));
74 1.1.2.12 nathanw #ifdef PTHREAD_SPIN_DEBUG
75 1.1.2.12 nathanw if(!(thread->pt_spinlocks >= 0)) {
76 1.1.2.12 nathanw (void)kill(getpid(), SIGABRT);
77 1.1.2.12 nathanw _exit(1);
78 1.1.2.12 nathanw }
79 1.1.2.12 nathanw #endif
80 1.1.2.1 nathanw ++thread->pt_spinlocks;
81 1.1.2.1 nathanw
82 1.1.2.1 nathanw do {
83 1.1.2.1 nathanw while (((ret = __cpu_simple_lock_try(lock)) == 0) && --count)
84 1.1.2.1 nathanw ;
85 1.1.2.1 nathanw
86 1.1.2.1 nathanw if (ret == 1)
87 1.1.2.1 nathanw break;
88 1.1.2.6 nathanw
89 1.1.2.6 nathanw SDPRINTF(("(pthread_spinlock %p) decrementing spinlock from %d\n",
90 1.1.2.6 nathanw thread, thread->pt_spinlocks));
91 1.1.2.1 nathanw --thread->pt_spinlocks;
92 1.1.2.1 nathanw
93 1.1.2.9 nathanw /*
94 1.1.2.9 nathanw * We may be preempted while spinning. If so, we will
95 1.1.2.1 nathanw * be restarted here if thread->pt_spinlocks is
96 1.1.2.1 nathanw * nonzero, which can happen if:
97 1.1.2.1 nathanw * a) we just got the lock
98 1.1.2.1 nathanw * b) we haven't yet decremented the lock count.
99 1.1.2.1 nathanw * If we're at this point, (b) applies. Therefore,
100 1.1.2.1 nathanw * check if we're being continued, and if so, bail.
101 1.1.2.1 nathanw * (in case (a), we should let the code finish and
102 1.1.2.1 nathanw * we will bail out in pthread_spinunlock()).
103 1.1.2.1 nathanw */
104 1.1.2.10 nathanw if (thread->pt_next != NULL) {
105 1.1.2.1 nathanw PTHREADD_ADD(PTHREADD_SPINPREEMPT);
106 1.1.2.5 nathanw pthread__switch(thread, thread->pt_next);
107 1.1.2.1 nathanw }
108 1.1.2.1 nathanw /* try again */
109 1.1.2.1 nathanw count = nspins;
110 1.1.2.6 nathanw SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
111 1.1.2.6 nathanw thread, thread->pt_spinlocks));
112 1.1.2.1 nathanw ++thread->pt_spinlocks;
113 1.1.2.1 nathanw } while (/*CONSTCOND*/1);
114 1.1.2.1 nathanw
115 1.1.2.1 nathanw PTHREADD_ADD(PTHREADD_SPINLOCKS);
116 1.1.2.1 nathanw /* Got it! We're out of here. */
117 1.1.2.1 nathanw }
118 1.1.2.1 nathanw
119 1.1.2.1 nathanw
120 1.1.2.1 nathanw int
121 1.1.2.6 nathanw pthread_spintrylock(pthread_t thread, pthread_spin_t *lock)
122 1.1.2.1 nathanw {
123 1.1.2.1 nathanw int ret;
124 1.1.2.1 nathanw
125 1.1.2.6 nathanw SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
126 1.1.2.6 nathanw thread, thread->pt_spinlocks));
127 1.1.2.1 nathanw ++thread->pt_spinlocks;
128 1.1.2.1 nathanw
129 1.1.2.1 nathanw ret = __cpu_simple_lock_try(lock);
130 1.1.2.1 nathanw
131 1.1.2.1 nathanw if (ret == 0) {
132 1.1.2.6 nathanw SDPRINTF(("(pthread_spintrylock %p) decrementing spinlock from %d\n",
133 1.1.2.6 nathanw thread, thread->pt_spinlocks));
134 1.1.2.1 nathanw --thread->pt_spinlocks;
135 1.1.2.1 nathanw /* See above. */
136 1.1.2.10 nathanw if (thread->pt_next != NULL) {
137 1.1.2.1 nathanw PTHREADD_ADD(PTHREADD_SPINPREEMPT);
138 1.1.2.5 nathanw pthread__switch(thread, thread->pt_next);
139 1.1.2.1 nathanw }
140 1.1.2.1 nathanw }
141 1.1.2.1 nathanw
142 1.1.2.1 nathanw return ret;
143 1.1.2.1 nathanw }
144 1.1.2.1 nathanw
145 1.1.2.1 nathanw
146 1.1.2.1 nathanw void
147 1.1.2.6 nathanw pthread_spinunlock(pthread_t thread, pthread_spin_t *lock)
148 1.1.2.1 nathanw {
149 1.1.2.9 nathanw
150 1.1.2.1 nathanw __cpu_simple_unlock(lock);
151 1.1.2.6 nathanw SDPRINTF(("(pthread_spinunlock %p) decrementing spinlock from %d\n",
152 1.1.2.6 nathanw thread, thread->pt_spinlocks));
153 1.1.2.1 nathanw --thread->pt_spinlocks;
154 1.1.2.12 nathanw #ifdef PTHREAD_SPIN_DEBUG
155 1.1.2.12 nathanw if (!(thread->pt_spinlocks >= 0)) {
156 1.1.2.12 nathanw (void)kill(getpid(), SIGABRT);
157 1.1.2.12 nathanw _exit(1);
158 1.1.2.12 nathanw }
159 1.1.2.12 nathanw #endif
160 1.1.2.1 nathanw PTHREADD_ADD(PTHREADD_SPINUNLOCKS);
161 1.1.2.1 nathanw
162 1.1.2.9 nathanw /*
163 1.1.2.9 nathanw * If we were preempted while holding a spinlock, the
164 1.1.2.1 nathanw * scheduler will notice this and continue us. To be good
165 1.1.2.1 nathanw * citzens, we must now get out of here if that was our
166 1.1.2.1 nathanw * last spinlock.
167 1.1.2.1 nathanw * XXX when will we ever have more than one?
168 1.1.2.1 nathanw */
169 1.1.2.1 nathanw
170 1.1.2.10 nathanw if ((thread->pt_spinlocks == 0) && (thread->pt_next != NULL)) {
171 1.1.2.1 nathanw PTHREADD_ADD(PTHREADD_SPINPREEMPT);
172 1.1.2.5 nathanw pthread__switch(thread, thread->pt_next);
173 1.1.2.1 nathanw }
174 1.1.2.8 nathanw }
175 1.1.2.8 nathanw
176 1.1.2.8 nathanw
177 1.1.2.8 nathanw /*
178 1.1.2.8 nathanw * Public (POSIX-specified) spinlocks.
179 1.1.2.8 nathanw * These don't interact with the spin-preemption code, nor do they
180 1.1.2.8 nathanw * perform any adaptive sleeping.
181 1.1.2.8 nathanw */
182 1.1.2.8 nathanw
183 1.1.2.8 nathanw int
184 1.1.2.8 nathanw pthread_spin_init(pthread_spinlock_t *lock, int pshared)
185 1.1.2.8 nathanw {
186 1.1.2.8 nathanw
187 1.1.2.8 nathanw #ifdef ERRORCHECK
188 1.1.2.8 nathanw if ((lock == NULL) ||
189 1.1.2.8 nathanw ((pshared != PTHREAD_PROCESS_PRIVATE) &&
190 1.1.2.8 nathanw (pshared != PTHREAD_PROCESS_SHARED)))
191 1.1.2.8 nathanw return EINVAL;
192 1.1.2.8 nathanw #endif
193 1.1.2.8 nathanw lock->pts_magic = _PT_SPINLOCK_MAGIC;
194 1.1.2.9 nathanw /*
195 1.1.2.9 nathanw * We don't actually use the pshared flag for anything;
196 1.1.2.8 nathanw * cpu simple locks have all the process-shared properties
197 1.1.2.8 nathanw * that we want anyway.
198 1.1.2.8 nathanw */
199 1.1.2.8 nathanw lock->pts_flags = pshared;
200 1.1.2.8 nathanw pthread_lockinit(&lock->pts_spin);
201 1.1.2.8 nathanw
202 1.1.2.8 nathanw return 0;
203 1.1.2.8 nathanw }
204 1.1.2.8 nathanw
205 1.1.2.8 nathanw int
206 1.1.2.8 nathanw pthread_spin_destroy(pthread_spinlock_t *lock)
207 1.1.2.8 nathanw {
208 1.1.2.8 nathanw
209 1.1.2.8 nathanw #ifdef ERRORCHECK
210 1.1.2.8 nathanw if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
211 1.1.2.8 nathanw return EINVAL;
212 1.1.2.8 nathanw
213 1.1.2.8 nathanw if (lock->pts_spin != __SIMPLELOCK_UNLOCKED)
214 1.1.2.8 nathanw return EBUSY;
215 1.1.2.8 nathanw #endif
216 1.1.2.8 nathanw
217 1.1.2.8 nathanw lock->pts_magic = _PT_SPINLOCK_DEAD;
218 1.1.2.8 nathanw
219 1.1.2.8 nathanw return 0;
220 1.1.2.8 nathanw }
221 1.1.2.8 nathanw
222 1.1.2.8 nathanw int
223 1.1.2.8 nathanw pthread_spin_lock(pthread_spinlock_t *lock)
224 1.1.2.8 nathanw {
225 1.1.2.8 nathanw
226 1.1.2.8 nathanw #ifdef ERRORCHECK
227 1.1.2.8 nathanw if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
228 1.1.2.8 nathanw return EINVAL;
229 1.1.2.8 nathanw #endif
230 1.1.2.8 nathanw
231 1.1.2.8 nathanw __cpu_simple_lock(&lock->pts_spin);
232 1.1.2.8 nathanw
233 1.1.2.8 nathanw return 0;
234 1.1.2.8 nathanw }
235 1.1.2.8 nathanw
236 1.1.2.8 nathanw int
237 1.1.2.8 nathanw pthread_spin_trylock(pthread_spinlock_t *lock)
238 1.1.2.8 nathanw {
239 1.1.2.8 nathanw
240 1.1.2.8 nathanw #ifdef ERRORCHECK
241 1.1.2.8 nathanw if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
242 1.1.2.8 nathanw return EINVAL;
243 1.1.2.8 nathanw #endif
244 1.1.2.8 nathanw
245 1.1.2.8 nathanw if (__cpu_simple_lock_try(&lock->pts_spin) == 0)
246 1.1.2.8 nathanw return EBUSY;
247 1.1.2.8 nathanw
248 1.1.2.8 nathanw return 0;
249 1.1.2.8 nathanw }
250 1.1.2.8 nathanw
251 1.1.2.8 nathanw int
252 1.1.2.8 nathanw pthread_spin_unlock(pthread_spinlock_t *lock)
253 1.1.2.8 nathanw {
254 1.1.2.8 nathanw
255 1.1.2.8 nathanw #ifdef ERRORCHECK
256 1.1.2.8 nathanw if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
257 1.1.2.8 nathanw return EINVAL;
258 1.1.2.8 nathanw #endif
259 1.1.2.8 nathanw
260 1.1.2.8 nathanw __cpu_simple_unlock(&lock->pts_spin);
261 1.1.2.8 nathanw
262 1.1.2.8 nathanw return 0;
263 1.1.2.1 nathanw }
264