pthread_lock.c revision 1.19 1 1.19 ad /* $NetBSD: pthread_lock.c,v 1.19 2007/03/05 23:30:17 ad Exp $ */
2 1.2 thorpej
3 1.2 thorpej /*-
4 1.19 ad * Copyright (c) 2001, 2006, 2007 The NetBSD Foundation, Inc.
5 1.2 thorpej * All rights reserved.
6 1.2 thorpej *
7 1.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.19 ad * by Nathan J. Williams and Andrew Doran.
9 1.2 thorpej *
10 1.2 thorpej * Redistribution and use in source and binary forms, with or without
11 1.2 thorpej * modification, are permitted provided that the following conditions
12 1.2 thorpej * are met:
13 1.2 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.2 thorpej * notice, this list of conditions and the following disclaimer.
15 1.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.2 thorpej * documentation and/or other materials provided with the distribution.
18 1.2 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.2 thorpej * must display the following acknowledgement:
20 1.2 thorpej * This product includes software developed by the NetBSD
21 1.2 thorpej * Foundation, Inc. and its contributors.
22 1.2 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.2 thorpej * contributors may be used to endorse or promote products derived
24 1.2 thorpej * from this software without specific prior written permission.
25 1.2 thorpej *
26 1.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.2 thorpej */
38 1.6 lukem
39 1.6 lukem #include <sys/cdefs.h>
40 1.19 ad __RCSID("$NetBSD: pthread_lock.c,v 1.19 2007/03/05 23:30:17 ad Exp $");
41 1.2 thorpej
42 1.12 he #include <sys/types.h>
43 1.11 cl #include <sys/lock.h>
44 1.2 thorpej #include <sys/ras.h>
45 1.2 thorpej
46 1.2 thorpej #include <errno.h>
47 1.2 thorpej #include <unistd.h>
48 1.19 ad #include <stdio.h>
49 1.2 thorpej
50 1.2 thorpej #include "pthread.h"
51 1.2 thorpej #include "pthread_int.h"
52 1.2 thorpej
53 1.5 nathanw #ifdef PTHREAD_SPIN_DEBUG_PRINT
54 1.2 thorpej #define SDPRINTF(x) DPRINTF(x)
55 1.2 thorpej #else
56 1.2 thorpej #define SDPRINTF(x)
57 1.2 thorpej #endif
58 1.2 thorpej
59 1.17 ad /* This does not belong here. */
60 1.17 ad #if defined(i386) || defined(__x86_64__)
61 1.17 ad #define smt_pause() __asm __volatile("rep; nop" ::: "memory")
62 1.17 ad #else
63 1.17 ad #define smt_pause() /* nothing */
64 1.17 ad #endif
65 1.17 ad
66 1.16 ad extern int pthread__nspins;
67 1.2 thorpej
68 1.10 thorpej RAS_DECL(pthread__lock);
69 1.2 thorpej
70 1.2 thorpej static void
71 1.2 thorpej pthread__ras_simple_lock_init(__cpu_simple_lock_t *alp)
72 1.2 thorpej {
73 1.2 thorpej
74 1.2 thorpej *alp = __SIMPLELOCK_UNLOCKED;
75 1.2 thorpej }
76 1.2 thorpej
77 1.2 thorpej static int
78 1.2 thorpej pthread__ras_simple_lock_try(__cpu_simple_lock_t *alp)
79 1.2 thorpej {
80 1.2 thorpej __cpu_simple_lock_t old;
81 1.2 thorpej
82 1.10 thorpej RAS_START(pthread__lock);
83 1.2 thorpej old = *alp;
84 1.2 thorpej *alp = __SIMPLELOCK_LOCKED;
85 1.10 thorpej RAS_END(pthread__lock);
86 1.2 thorpej
87 1.2 thorpej return (old == __SIMPLELOCK_UNLOCKED);
88 1.2 thorpej }
89 1.2 thorpej
90 1.2 thorpej static void
91 1.2 thorpej pthread__ras_simple_unlock(__cpu_simple_lock_t *alp)
92 1.2 thorpej {
93 1.2 thorpej
94 1.2 thorpej *alp = __SIMPLELOCK_UNLOCKED;
95 1.2 thorpej }
96 1.2 thorpej
97 1.2 thorpej static const struct pthread_lock_ops pthread__lock_ops_ras = {
98 1.2 thorpej pthread__ras_simple_lock_init,
99 1.2 thorpej pthread__ras_simple_lock_try,
100 1.2 thorpej pthread__ras_simple_unlock,
101 1.2 thorpej };
102 1.2 thorpej
103 1.2 thorpej static void
104 1.2 thorpej pthread__atomic_simple_lock_init(__cpu_simple_lock_t *alp)
105 1.2 thorpej {
106 1.2 thorpej
107 1.2 thorpej __cpu_simple_lock_init(alp);
108 1.2 thorpej }
109 1.2 thorpej
110 1.2 thorpej static int
111 1.2 thorpej pthread__atomic_simple_lock_try(__cpu_simple_lock_t *alp)
112 1.2 thorpej {
113 1.2 thorpej
114 1.2 thorpej return (__cpu_simple_lock_try(alp));
115 1.2 thorpej }
116 1.2 thorpej
117 1.2 thorpej static void
118 1.2 thorpej pthread__atomic_simple_unlock(__cpu_simple_lock_t *alp)
119 1.2 thorpej {
120 1.2 thorpej
121 1.2 thorpej __cpu_simple_unlock(alp);
122 1.2 thorpej }
123 1.2 thorpej
124 1.2 thorpej static const struct pthread_lock_ops pthread__lock_ops_atomic = {
125 1.2 thorpej pthread__atomic_simple_lock_init,
126 1.2 thorpej pthread__atomic_simple_lock_try,
127 1.2 thorpej pthread__atomic_simple_unlock,
128 1.2 thorpej };
129 1.2 thorpej
130 1.2 thorpej /*
131 1.2 thorpej * We default to pointing to the RAS primitives; we might need to use
132 1.2 thorpej * locks early, but before main() starts. This is safe, since no other
133 1.2 thorpej * threads will be active for the process, so atomicity will not be
134 1.2 thorpej * required.
135 1.2 thorpej */
136 1.2 thorpej const struct pthread_lock_ops *pthread__lock_ops = &pthread__lock_ops_ras;
137 1.2 thorpej
138 1.2 thorpej /*
139 1.2 thorpej * Initialize the locking primitives. On uniprocessors, we always
140 1.2 thorpej * use Restartable Atomic Sequences if they are available. Otherwise,
141 1.2 thorpej * we fall back onto machine-dependent atomic lock primitives.
142 1.2 thorpej */
143 1.2 thorpej void
144 1.11 cl pthread__lockprim_init(int ncpu)
145 1.2 thorpej {
146 1.2 thorpej
147 1.10 thorpej if (ncpu == 1 && rasctl(RAS_ADDR(pthread__lock),
148 1.19 ad RAS_SIZE(pthread__lock), RAS_INSTALL) == 0) {
149 1.2 thorpej pthread__lock_ops = &pthread__lock_ops_ras;
150 1.2 thorpej return;
151 1.2 thorpej }
152 1.2 thorpej
153 1.2 thorpej pthread__lock_ops = &pthread__lock_ops_atomic;
154 1.2 thorpej }
155 1.2 thorpej
156 1.2 thorpej void
157 1.2 thorpej pthread_lockinit(pthread_spin_t *lock)
158 1.2 thorpej {
159 1.2 thorpej
160 1.2 thorpej pthread__simple_lock_init(lock);
161 1.2 thorpej }
162 1.2 thorpej
163 1.2 thorpej void
164 1.2 thorpej pthread_spinlock(pthread_t thread, pthread_spin_t *lock)
165 1.2 thorpej {
166 1.2 thorpej int count, ret;
167 1.2 thorpej
168 1.16 ad count = pthread__nspins;
169 1.19 ad SDPRINTF(("(pthread_spinlock %p) spinlock %p (count %d)\n",
170 1.19 ad thread, lock, thread->pt_spinlocks));
171 1.2 thorpej #ifdef PTHREAD_SPIN_DEBUG
172 1.5 nathanw pthread__assert(thread->pt_spinlocks >= 0);
173 1.2 thorpej #endif
174 1.19 ad
175 1.19 ad thread->pt_spinlocks++;
176 1.19 ad if (__predict_true(pthread__simple_lock_try(lock))) {
177 1.19 ad PTHREADD_ADD(PTHREADD_SPINLOCKS);
178 1.19 ad return;
179 1.19 ad }
180 1.2 thorpej
181 1.2 thorpej do {
182 1.19 ad while ((ret = pthread__simple_lock_try(lock)) == 0 &&
183 1.19 ad --count) {
184 1.17 ad smt_pause();
185 1.17 ad }
186 1.2 thorpej
187 1.2 thorpej if (ret == 1)
188 1.2 thorpej break;
189 1.2 thorpej
190 1.19 ad SDPRINTF(("(pthread_spinlock %p) retrying spinlock %p "
191 1.19 ad "(count %d)\n", thread, lock,
192 1.19 ad thread->pt_spinlocks));
193 1.19 ad thread->pt_spinlocks--;
194 1.19 ad
195 1.16 ad /* XXXLWP far from ideal */
196 1.15 ad sched_yield();
197 1.16 ad count = pthread__nspins;
198 1.19 ad thread->pt_spinlocks++;
199 1.19 ad } while (/*CONSTCOND*/ 1);
200 1.2 thorpej
201 1.2 thorpej PTHREADD_ADD(PTHREADD_SPINLOCKS);
202 1.2 thorpej }
203 1.2 thorpej
204 1.2 thorpej int
205 1.2 thorpej pthread_spintrylock(pthread_t thread, pthread_spin_t *lock)
206 1.2 thorpej {
207 1.2 thorpej int ret;
208 1.2 thorpej
209 1.19 ad SDPRINTF(("(pthread_spintrylock %p) spinlock %p (count %d)\n",
210 1.19 ad thread, lock, thread->pt_spinlocks));
211 1.18 ad
212 1.19 ad thread->pt_spinlocks++;
213 1.2 thorpej ret = pthread__simple_lock_try(lock);
214 1.18 ad if (!ret)
215 1.19 ad thread->pt_spinlocks--;
216 1.19 ad
217 1.2 thorpej return ret;
218 1.2 thorpej }
219 1.2 thorpej
220 1.2 thorpej void
221 1.2 thorpej pthread_spinunlock(pthread_t thread, pthread_spin_t *lock)
222 1.2 thorpej {
223 1.2 thorpej
224 1.19 ad SDPRINTF(("(pthread_spinunlock %p) spinlock %p (count %d)\n",
225 1.19 ad thread, lock, thread->pt_spinlocks));
226 1.19 ad
227 1.2 thorpej pthread__simple_unlock(lock);
228 1.19 ad thread->pt_spinlocks--;
229 1.2 thorpej #ifdef PTHREAD_SPIN_DEBUG
230 1.5 nathanw pthread__assert(thread->pt_spinlocks >= 0);
231 1.2 thorpej #endif
232 1.2 thorpej PTHREADD_ADD(PTHREADD_SPINUNLOCKS);
233 1.2 thorpej }
234 1.2 thorpej
235 1.2 thorpej
236 1.2 thorpej /*
237 1.2 thorpej * Public (POSIX-specified) spinlocks.
238 1.2 thorpej */
239 1.2 thorpej int
240 1.2 thorpej pthread_spin_init(pthread_spinlock_t *lock, int pshared)
241 1.2 thorpej {
242 1.2 thorpej
243 1.2 thorpej #ifdef ERRORCHECK
244 1.19 ad if (lock == NULL || (pshared != PTHREAD_PROCESS_PRIVATE &&
245 1.19 ad pshared != PTHREAD_PROCESS_SHARED))
246 1.2 thorpej return EINVAL;
247 1.2 thorpej #endif
248 1.2 thorpej lock->pts_magic = _PT_SPINLOCK_MAGIC;
249 1.19 ad
250 1.2 thorpej /*
251 1.2 thorpej * We don't actually use the pshared flag for anything;
252 1.9 wiz * CPU simple locks have all the process-shared properties
253 1.2 thorpej * that we want anyway.
254 1.2 thorpej */
255 1.2 thorpej lock->pts_flags = pshared;
256 1.2 thorpej pthread_lockinit(&lock->pts_spin);
257 1.2 thorpej
258 1.2 thorpej return 0;
259 1.2 thorpej }
260 1.2 thorpej
261 1.2 thorpej int
262 1.2 thorpej pthread_spin_destroy(pthread_spinlock_t *lock)
263 1.2 thorpej {
264 1.2 thorpej
265 1.2 thorpej #ifdef ERRORCHECK
266 1.19 ad if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
267 1.2 thorpej return EINVAL;
268 1.2 thorpej if (lock->pts_spin != __SIMPLELOCK_UNLOCKED)
269 1.2 thorpej return EBUSY;
270 1.2 thorpej #endif
271 1.2 thorpej
272 1.2 thorpej lock->pts_magic = _PT_SPINLOCK_DEAD;
273 1.2 thorpej
274 1.2 thorpej return 0;
275 1.2 thorpej }
276 1.2 thorpej
277 1.2 thorpej int
278 1.2 thorpej pthread_spin_lock(pthread_spinlock_t *lock)
279 1.2 thorpej {
280 1.2 thorpej
281 1.2 thorpej #ifdef ERRORCHECK
282 1.19 ad if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
283 1.2 thorpej return EINVAL;
284 1.2 thorpej #endif
285 1.2 thorpej
286 1.17 ad while (pthread__simple_lock_try(&lock->pts_spin) == 0) {
287 1.17 ad smt_pause();
288 1.17 ad }
289 1.2 thorpej
290 1.2 thorpej return 0;
291 1.2 thorpej }
292 1.2 thorpej
293 1.2 thorpej int
294 1.2 thorpej pthread_spin_trylock(pthread_spinlock_t *lock)
295 1.2 thorpej {
296 1.2 thorpej
297 1.2 thorpej #ifdef ERRORCHECK
298 1.19 ad if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
299 1.2 thorpej return EINVAL;
300 1.2 thorpej #endif
301 1.2 thorpej
302 1.2 thorpej if (pthread__simple_lock_try(&lock->pts_spin) == 0)
303 1.2 thorpej return EBUSY;
304 1.2 thorpej
305 1.2 thorpej return 0;
306 1.2 thorpej }
307 1.2 thorpej
308 1.2 thorpej int
309 1.2 thorpej pthread_spin_unlock(pthread_spinlock_t *lock)
310 1.2 thorpej {
311 1.2 thorpej
312 1.2 thorpej #ifdef ERRORCHECK
313 1.19 ad if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
314 1.2 thorpej return EINVAL;
315 1.2 thorpej #endif
316 1.2 thorpej
317 1.2 thorpej pthread__simple_unlock(&lock->pts_spin);
318 1.2 thorpej
319 1.2 thorpej return 0;
320 1.2 thorpej }
321