pthread_lock.c revision 1.1.2.14 1 1.1.2.14 thorpej /* $NetBSD: pthread_lock.c,v 1.1.2.14 2002/12/30 22:24:34 thorpej 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.14 thorpej #include <sys/param.h>
40 1.1.2.14 thorpej #include <sys/ras.h>
41 1.1.2.14 thorpej #include <sys/sysctl.h>
42 1.1.2.14 thorpej
43 1.1.2.6 nathanw #include <assert.h>
44 1.1.2.8 nathanw #include <errno.h>
45 1.1.2.12 nathanw #include <unistd.h>
46 1.1.2.6 nathanw
47 1.1.2.1 nathanw #include "pthread.h"
48 1.1.2.1 nathanw #include "pthread_int.h"
49 1.1.2.1 nathanw
50 1.1.2.12 nathanw #undef PTHREAD_SPIN_DEBUG
51 1.1.2.6 nathanw
52 1.1.2.12 nathanw #ifdef PTHREAD_SPIN_DEBUG
53 1.1.2.6 nathanw #define SDPRINTF(x) DPRINTF(x)
54 1.1.2.6 nathanw #else
55 1.1.2.6 nathanw #define SDPRINTF(x)
56 1.1.2.6 nathanw #endif
57 1.1.2.6 nathanw
58 1.1.2.1 nathanw /* How many times to try before checking whether we've been continued. */
59 1.1.2.10 nathanw #define NSPINS 1 /* no point in actually spinning until MP works */
60 1.1.2.1 nathanw
61 1.1.2.1 nathanw static int nspins = NSPINS;
62 1.1.2.1 nathanw
63 1.1.2.14 thorpej extern char pthread__lock_ras_start[], pthread__lock_ras_end[];
64 1.1.2.14 thorpej
65 1.1.2.14 thorpej static void
66 1.1.2.14 thorpej pthread__ras_simple_lock_init(__cpu_simple_lock_t *alp)
67 1.1.2.14 thorpej {
68 1.1.2.14 thorpej
69 1.1.2.14 thorpej *alp = __SIMPLELOCK_UNLOCKED;
70 1.1.2.14 thorpej }
71 1.1.2.14 thorpej
72 1.1.2.14 thorpej static int
73 1.1.2.14 thorpej pthread__ras_simple_lock_try(__cpu_simple_lock_t *alp)
74 1.1.2.14 thorpej {
75 1.1.2.14 thorpej __cpu_simple_lock_t old;
76 1.1.2.14 thorpej
77 1.1.2.14 thorpej /* This is the atomic sequence. */
78 1.1.2.14 thorpej __asm __volatile("pthread__lock_ras_start:");
79 1.1.2.14 thorpej old = *alp;
80 1.1.2.14 thorpej *alp = __SIMPLELOCK_LOCKED;
81 1.1.2.14 thorpej __asm __volatile("pthread__lock_ras_end:");
82 1.1.2.14 thorpej
83 1.1.2.14 thorpej return (old == __SIMPLELOCK_UNLOCKED);
84 1.1.2.14 thorpej }
85 1.1.2.14 thorpej
86 1.1.2.14 thorpej static void
87 1.1.2.14 thorpej pthread__ras_simple_unlock(__cpu_simple_lock_t *alp)
88 1.1.2.14 thorpej {
89 1.1.2.14 thorpej
90 1.1.2.14 thorpej *alp = __SIMPLELOCK_UNLOCKED;
91 1.1.2.14 thorpej }
92 1.1.2.14 thorpej
93 1.1.2.14 thorpej static const struct pthread_lock_ops pthread__lock_ops_ras = {
94 1.1.2.14 thorpej pthread__ras_simple_lock_init,
95 1.1.2.14 thorpej pthread__ras_simple_lock_try,
96 1.1.2.14 thorpej pthread__ras_simple_unlock,
97 1.1.2.14 thorpej };
98 1.1.2.14 thorpej
99 1.1.2.14 thorpej static void
100 1.1.2.14 thorpej pthread__atomic_simple_lock_init(__cpu_simple_lock_t *alp)
101 1.1.2.14 thorpej {
102 1.1.2.14 thorpej
103 1.1.2.14 thorpej __cpu_simple_lock_init(alp);
104 1.1.2.14 thorpej }
105 1.1.2.14 thorpej
106 1.1.2.14 thorpej static int
107 1.1.2.14 thorpej pthread__atomic_simple_lock_try(__cpu_simple_lock_t *alp)
108 1.1.2.14 thorpej {
109 1.1.2.14 thorpej
110 1.1.2.14 thorpej return (__cpu_simple_lock_try(alp));
111 1.1.2.14 thorpej }
112 1.1.2.14 thorpej
113 1.1.2.14 thorpej static void
114 1.1.2.14 thorpej pthread__atomic_simple_unlock(__cpu_simple_lock_t *alp)
115 1.1.2.14 thorpej {
116 1.1.2.14 thorpej
117 1.1.2.14 thorpej __cpu_simple_unlock(alp);
118 1.1.2.14 thorpej }
119 1.1.2.14 thorpej
120 1.1.2.14 thorpej static const struct pthread_lock_ops pthread__lock_ops_atomic = {
121 1.1.2.14 thorpej pthread__atomic_simple_lock_init,
122 1.1.2.14 thorpej pthread__atomic_simple_lock_try,
123 1.1.2.14 thorpej pthread__atomic_simple_unlock,
124 1.1.2.14 thorpej };
125 1.1.2.14 thorpej
126 1.1.2.14 thorpej /*
127 1.1.2.14 thorpej * We default to pointing to the RAS primitives; we might need to use
128 1.1.2.14 thorpej * locks early, but before main() starts. This is safe, since no other
129 1.1.2.14 thorpej * threads will be active for the process, so atomicity will not be
130 1.1.2.14 thorpej * required.
131 1.1.2.14 thorpej */
132 1.1.2.14 thorpej const struct pthread_lock_ops *pthread__lock_ops = &pthread__lock_ops_ras;
133 1.1.2.14 thorpej
134 1.1.2.14 thorpej /*
135 1.1.2.14 thorpej * Initialize the locking primitives. On uniprocessors, we always
136 1.1.2.14 thorpej * use Restartable Atomic Sequences if they are available. Otherwise,
137 1.1.2.14 thorpej * we fall back onto machine-dependent atomic lock primitives.
138 1.1.2.14 thorpej */
139 1.1.2.14 thorpej void
140 1.1.2.14 thorpej pthread__lockprim_init(void)
141 1.1.2.14 thorpej {
142 1.1.2.14 thorpej int mib[2];
143 1.1.2.14 thorpej size_t len;
144 1.1.2.14 thorpej int ncpu;
145 1.1.2.14 thorpej
146 1.1.2.14 thorpej mib[0] = CTL_HW;
147 1.1.2.14 thorpej mib[1] = HW_NCPU;
148 1.1.2.14 thorpej
149 1.1.2.14 thorpej len = sizeof(ncpu);
150 1.1.2.14 thorpej sysctl(mib, 2, &ncpu, &len, NULL, 0);
151 1.1.2.14 thorpej
152 1.1.2.14 thorpej if (ncpu == 1 &&
153 1.1.2.14 thorpej rasctl(pthread__lock_ras_start,
154 1.1.2.14 thorpej (caddr_t)pthread__lock_ras_end -
155 1.1.2.14 thorpej (caddr_t)pthread__lock_ras_start, RAS_INSTALL) == 0) {
156 1.1.2.14 thorpej pthread__lock_ops = &pthread__lock_ops_ras;
157 1.1.2.14 thorpej return;
158 1.1.2.14 thorpej }
159 1.1.2.14 thorpej
160 1.1.2.14 thorpej pthread__lock_ops = &pthread__lock_ops_atomic;
161 1.1.2.14 thorpej }
162 1.1.2.14 thorpej
163 1.1.2.1 nathanw void
164 1.1.2.6 nathanw pthread_lockinit(pthread_spin_t *lock)
165 1.1.2.2 nathanw {
166 1.1.2.2 nathanw
167 1.1.2.14 thorpej pthread__simple_lock_init(lock);
168 1.1.2.2 nathanw }
169 1.1.2.2 nathanw
170 1.1.2.2 nathanw void
171 1.1.2.6 nathanw pthread_spinlock(pthread_t thread, pthread_spin_t *lock)
172 1.1.2.1 nathanw {
173 1.1.2.1 nathanw int count, ret;
174 1.1.2.1 nathanw
175 1.1.2.1 nathanw count = nspins;
176 1.1.2.13 nathanw SDPRINTF(("(pthread_spinlock %p) incrementing spinlock %p (count %d)\n",
177 1.1.2.13 nathanw thread, lock, thread->pt_spinlocks));
178 1.1.2.12 nathanw #ifdef PTHREAD_SPIN_DEBUG
179 1.1.2.12 nathanw if(!(thread->pt_spinlocks >= 0)) {
180 1.1.2.12 nathanw (void)kill(getpid(), SIGABRT);
181 1.1.2.12 nathanw _exit(1);
182 1.1.2.12 nathanw }
183 1.1.2.12 nathanw #endif
184 1.1.2.1 nathanw ++thread->pt_spinlocks;
185 1.1.2.1 nathanw
186 1.1.2.1 nathanw do {
187 1.1.2.14 thorpej while (((ret = pthread__simple_lock_try(lock)) == 0) && --count)
188 1.1.2.1 nathanw ;
189 1.1.2.1 nathanw
190 1.1.2.1 nathanw if (ret == 1)
191 1.1.2.1 nathanw break;
192 1.1.2.6 nathanw
193 1.1.2.13 nathanw SDPRINTF(("(pthread_spinlock %p) decrementing spinlock %p (count %d)\n",
194 1.1.2.13 nathanw thread, lock, thread->pt_spinlocks));
195 1.1.2.1 nathanw --thread->pt_spinlocks;
196 1.1.2.1 nathanw
197 1.1.2.9 nathanw /*
198 1.1.2.9 nathanw * We may be preempted while spinning. If so, we will
199 1.1.2.1 nathanw * be restarted here if thread->pt_spinlocks is
200 1.1.2.1 nathanw * nonzero, which can happen if:
201 1.1.2.1 nathanw * a) we just got the lock
202 1.1.2.1 nathanw * b) we haven't yet decremented the lock count.
203 1.1.2.1 nathanw * If we're at this point, (b) applies. Therefore,
204 1.1.2.1 nathanw * check if we're being continued, and if so, bail.
205 1.1.2.1 nathanw * (in case (a), we should let the code finish and
206 1.1.2.1 nathanw * we will bail out in pthread_spinunlock()).
207 1.1.2.1 nathanw */
208 1.1.2.10 nathanw if (thread->pt_next != NULL) {
209 1.1.2.1 nathanw PTHREADD_ADD(PTHREADD_SPINPREEMPT);
210 1.1.2.5 nathanw pthread__switch(thread, thread->pt_next);
211 1.1.2.1 nathanw }
212 1.1.2.1 nathanw /* try again */
213 1.1.2.1 nathanw count = nspins;
214 1.1.2.6 nathanw SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
215 1.1.2.6 nathanw thread, thread->pt_spinlocks));
216 1.1.2.1 nathanw ++thread->pt_spinlocks;
217 1.1.2.1 nathanw } while (/*CONSTCOND*/1);
218 1.1.2.1 nathanw
219 1.1.2.1 nathanw PTHREADD_ADD(PTHREADD_SPINLOCKS);
220 1.1.2.1 nathanw /* Got it! We're out of here. */
221 1.1.2.1 nathanw }
222 1.1.2.1 nathanw
223 1.1.2.1 nathanw
224 1.1.2.1 nathanw int
225 1.1.2.6 nathanw pthread_spintrylock(pthread_t thread, pthread_spin_t *lock)
226 1.1.2.1 nathanw {
227 1.1.2.1 nathanw int ret;
228 1.1.2.1 nathanw
229 1.1.2.6 nathanw SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
230 1.1.2.6 nathanw thread, thread->pt_spinlocks));
231 1.1.2.1 nathanw ++thread->pt_spinlocks;
232 1.1.2.1 nathanw
233 1.1.2.14 thorpej ret = pthread__simple_lock_try(lock);
234 1.1.2.1 nathanw
235 1.1.2.1 nathanw if (ret == 0) {
236 1.1.2.6 nathanw SDPRINTF(("(pthread_spintrylock %p) decrementing spinlock from %d\n",
237 1.1.2.6 nathanw thread, thread->pt_spinlocks));
238 1.1.2.1 nathanw --thread->pt_spinlocks;
239 1.1.2.1 nathanw /* See above. */
240 1.1.2.10 nathanw if (thread->pt_next != NULL) {
241 1.1.2.1 nathanw PTHREADD_ADD(PTHREADD_SPINPREEMPT);
242 1.1.2.5 nathanw pthread__switch(thread, thread->pt_next);
243 1.1.2.1 nathanw }
244 1.1.2.1 nathanw }
245 1.1.2.1 nathanw
246 1.1.2.1 nathanw return ret;
247 1.1.2.1 nathanw }
248 1.1.2.1 nathanw
249 1.1.2.1 nathanw
250 1.1.2.1 nathanw void
251 1.1.2.6 nathanw pthread_spinunlock(pthread_t thread, pthread_spin_t *lock)
252 1.1.2.1 nathanw {
253 1.1.2.9 nathanw
254 1.1.2.14 thorpej pthread__simple_unlock(lock);
255 1.1.2.13 nathanw SDPRINTF(("(pthread_spinunlock %p) decrementing spinlock %p (count %d)\n",
256 1.1.2.13 nathanw thread, lock, thread->pt_spinlocks));
257 1.1.2.1 nathanw --thread->pt_spinlocks;
258 1.1.2.12 nathanw #ifdef PTHREAD_SPIN_DEBUG
259 1.1.2.12 nathanw if (!(thread->pt_spinlocks >= 0)) {
260 1.1.2.12 nathanw (void)kill(getpid(), SIGABRT);
261 1.1.2.12 nathanw _exit(1);
262 1.1.2.12 nathanw }
263 1.1.2.12 nathanw #endif
264 1.1.2.1 nathanw PTHREADD_ADD(PTHREADD_SPINUNLOCKS);
265 1.1.2.1 nathanw
266 1.1.2.9 nathanw /*
267 1.1.2.9 nathanw * If we were preempted while holding a spinlock, the
268 1.1.2.1 nathanw * scheduler will notice this and continue us. To be good
269 1.1.2.1 nathanw * citzens, we must now get out of here if that was our
270 1.1.2.1 nathanw * last spinlock.
271 1.1.2.1 nathanw * XXX when will we ever have more than one?
272 1.1.2.1 nathanw */
273 1.1.2.1 nathanw
274 1.1.2.10 nathanw if ((thread->pt_spinlocks == 0) && (thread->pt_next != NULL)) {
275 1.1.2.1 nathanw PTHREADD_ADD(PTHREADD_SPINPREEMPT);
276 1.1.2.5 nathanw pthread__switch(thread, thread->pt_next);
277 1.1.2.1 nathanw }
278 1.1.2.8 nathanw }
279 1.1.2.8 nathanw
280 1.1.2.8 nathanw
281 1.1.2.8 nathanw /*
282 1.1.2.8 nathanw * Public (POSIX-specified) spinlocks.
283 1.1.2.8 nathanw * These don't interact with the spin-preemption code, nor do they
284 1.1.2.8 nathanw * perform any adaptive sleeping.
285 1.1.2.8 nathanw */
286 1.1.2.8 nathanw
287 1.1.2.8 nathanw int
288 1.1.2.8 nathanw pthread_spin_init(pthread_spinlock_t *lock, int pshared)
289 1.1.2.8 nathanw {
290 1.1.2.8 nathanw
291 1.1.2.8 nathanw #ifdef ERRORCHECK
292 1.1.2.8 nathanw if ((lock == NULL) ||
293 1.1.2.8 nathanw ((pshared != PTHREAD_PROCESS_PRIVATE) &&
294 1.1.2.8 nathanw (pshared != PTHREAD_PROCESS_SHARED)))
295 1.1.2.8 nathanw return EINVAL;
296 1.1.2.8 nathanw #endif
297 1.1.2.8 nathanw lock->pts_magic = _PT_SPINLOCK_MAGIC;
298 1.1.2.9 nathanw /*
299 1.1.2.9 nathanw * We don't actually use the pshared flag for anything;
300 1.1.2.8 nathanw * cpu simple locks have all the process-shared properties
301 1.1.2.8 nathanw * that we want anyway.
302 1.1.2.8 nathanw */
303 1.1.2.8 nathanw lock->pts_flags = pshared;
304 1.1.2.8 nathanw pthread_lockinit(&lock->pts_spin);
305 1.1.2.8 nathanw
306 1.1.2.8 nathanw return 0;
307 1.1.2.8 nathanw }
308 1.1.2.8 nathanw
309 1.1.2.8 nathanw int
310 1.1.2.8 nathanw pthread_spin_destroy(pthread_spinlock_t *lock)
311 1.1.2.8 nathanw {
312 1.1.2.8 nathanw
313 1.1.2.8 nathanw #ifdef ERRORCHECK
314 1.1.2.8 nathanw if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
315 1.1.2.8 nathanw return EINVAL;
316 1.1.2.8 nathanw
317 1.1.2.8 nathanw if (lock->pts_spin != __SIMPLELOCK_UNLOCKED)
318 1.1.2.8 nathanw return EBUSY;
319 1.1.2.8 nathanw #endif
320 1.1.2.8 nathanw
321 1.1.2.8 nathanw lock->pts_magic = _PT_SPINLOCK_DEAD;
322 1.1.2.8 nathanw
323 1.1.2.8 nathanw return 0;
324 1.1.2.8 nathanw }
325 1.1.2.8 nathanw
326 1.1.2.8 nathanw int
327 1.1.2.8 nathanw pthread_spin_lock(pthread_spinlock_t *lock)
328 1.1.2.8 nathanw {
329 1.1.2.8 nathanw
330 1.1.2.8 nathanw #ifdef ERRORCHECK
331 1.1.2.8 nathanw if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
332 1.1.2.8 nathanw return EINVAL;
333 1.1.2.8 nathanw #endif
334 1.1.2.8 nathanw
335 1.1.2.14 thorpej while (pthread__simple_lock_try(&lock->pts_spin) == 0)
336 1.1.2.14 thorpej /* spin */ ;
337 1.1.2.8 nathanw
338 1.1.2.8 nathanw return 0;
339 1.1.2.8 nathanw }
340 1.1.2.8 nathanw
341 1.1.2.8 nathanw int
342 1.1.2.8 nathanw pthread_spin_trylock(pthread_spinlock_t *lock)
343 1.1.2.8 nathanw {
344 1.1.2.8 nathanw
345 1.1.2.8 nathanw #ifdef ERRORCHECK
346 1.1.2.8 nathanw if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
347 1.1.2.8 nathanw return EINVAL;
348 1.1.2.8 nathanw #endif
349 1.1.2.8 nathanw
350 1.1.2.14 thorpej if (pthread__simple_lock_try(&lock->pts_spin) == 0)
351 1.1.2.8 nathanw return EBUSY;
352 1.1.2.8 nathanw
353 1.1.2.8 nathanw return 0;
354 1.1.2.8 nathanw }
355 1.1.2.8 nathanw
356 1.1.2.8 nathanw int
357 1.1.2.8 nathanw pthread_spin_unlock(pthread_spinlock_t *lock)
358 1.1.2.8 nathanw {
359 1.1.2.8 nathanw
360 1.1.2.8 nathanw #ifdef ERRORCHECK
361 1.1.2.8 nathanw if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
362 1.1.2.8 nathanw return EINVAL;
363 1.1.2.8 nathanw #endif
364 1.1.2.8 nathanw
365 1.1.2.14 thorpej pthread__simple_unlock(&lock->pts_spin);
366 1.1.2.8 nathanw
367 1.1.2.8 nathanw return 0;
368 1.1.2.1 nathanw }
369