pthread_lock.c revision 1.28 1 /* $NetBSD: pthread_lock.c,v 1.28 2007/09/17 13:25:59 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2001, 2006, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams and Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * libpthread internal spinlock routines.
41 */
42
43 #include <sys/cdefs.h>
44 __RCSID("$NetBSD: pthread_lock.c,v 1.28 2007/09/17 13:25:59 skrll Exp $");
45
46 #include <sys/types.h>
47 #include <sys/lock.h>
48 #include <sys/ras.h>
49
50 #include <errno.h>
51 #include <unistd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54
55 #include "pthread.h"
56 #include "pthread_int.h"
57
58 /* How many times to try acquiring spin locks on MP systems. */
59 #define PTHREAD__NSPINS 1024
60
61 #ifdef PTHREAD_SPIN_DEBUG_PRINT
62 #define SDPRINTF(x) DPRINTF(x)
63 #else
64 #define SDPRINTF(x)
65 #endif
66
67 static void pthread_spinlock_slow(pthread_spin_t *);
68
69 static int pthread__atomic;
70
71 RAS_DECL(pthread__lock);
72 RAS_DECL(pthread__lock2);
73
74 int
75 pthread__simple_locked_p(__cpu_simple_lock_t *alp)
76 {
77 return __SIMPLELOCK_LOCKED_P(alp);
78 }
79
80 void
81 pthread__simple_lock_init(__cpu_simple_lock_t *alp)
82 {
83
84 if (pthread__atomic) {
85 __cpu_simple_lock_init(alp);
86 return;
87 }
88
89 __cpu_simple_lock_clear(alp);
90 }
91
92 int
93 pthread__simple_lock_try(__cpu_simple_lock_t *alp)
94 {
95 int locked;
96
97 if (pthread__atomic)
98 return __cpu_simple_lock_try(alp);
99
100 RAS_START(pthread__lock);
101 locked = __SIMPLELOCK_LOCKED_P(alp);
102 __cpu_simple_lock_set(alp);
103 RAS_END(pthread__lock);
104
105 return !locked;
106 }
107
108 inline void
109 pthread__simple_unlock(__cpu_simple_lock_t *alp)
110 {
111
112 #ifdef PTHREAD__CHEAP_UNLOCK
113 __cpu_simple_unlock(alp);
114 #else
115 if (pthread__atomic) {
116 __cpu_simple_unlock(alp);
117 return;
118 }
119
120 __cpu_simple_lock_clear(alp);
121 #endif
122 }
123
124 void
125 pthread_spinlock(pthread_spin_t *lock)
126 {
127 #ifdef PTHREAD_SPIN_DEBUG
128 pthread_t thread = pthread__self();
129
130 SDPRINTF(("(pthread_spinlock %p) spinlock %p (count %d)\n",
131 thread, lock, thread->pt_spinlocks));
132 pthread__assert(thread->pt_spinlocks >= 0);
133 thread->pt_spinlocks++;
134 PTHREADD_ADD(PTHREADD_SPINLOCKS);
135 #endif
136
137 if (pthread__atomic) {
138 if (__predict_true(__cpu_simple_lock_try(lock)))
139 return;
140 } else {
141 int locked;
142
143 RAS_START(pthread__lock2);
144 locked = __SIMPLELOCK_LOCKED_P(lock);
145 __cpu_simple_lock_set(lock);
146 RAS_END(pthread__lock2);
147
148 if (__predict_true(!locked))
149 return;
150 }
151
152 pthread_spinlock_slow(lock);
153 }
154
155 /*
156 * Prevent this routine from being inlined. The common case is no
157 * contention and it's better to not burden the instruction decoder.
158 */
159 #if __GNUC_PREREQ__(3, 0)
160 __attribute ((noinline))
161 #endif
162 static void
163 pthread_spinlock_slow(pthread_spin_t *lock)
164 {
165 int count;
166 #ifdef PTHREAD_SPIN_DEBUG
167 pthread_t thread = pthread__self();
168 #endif
169
170 do {
171 count = pthread__nspins;
172 while (pthread__simple_locked_p(lock) && --count > 0)
173 pthread__smt_pause();
174 if (count > 0) {
175 if (pthread__simple_lock_try(lock))
176 break;
177 continue;
178 }
179
180 #ifdef PTHREAD_SPIN_DEBUG
181 SDPRINTF(("(pthread_spinlock %p) retrying spinlock %p "
182 "(count %d)\n", thread, lock,
183 thread->pt_spinlocks));
184 thread->pt_spinlocks--;
185 /* XXXLWP far from ideal */
186 sched_yield();
187 thread->pt_spinlocks++;
188 #else
189 /* XXXLWP far from ideal */
190 sched_yield();
191 #endif
192 } while (/*CONSTCOND*/ 1);
193 }
194
195 int
196 pthread_spintrylock(pthread_spin_t *lock)
197 {
198 #ifdef PTHREAD_SPIN_DEBUG
199 pthread_t thread = pthread__self();
200 int ret;
201
202 SDPRINTF(("(pthread_spintrylock %p) spinlock %p (count %d)\n",
203 thread, lock, thread->pt_spinlocks));
204 thread->pt_spinlocks++;
205 ret = pthread__simple_lock_try(lock);
206 if (!ret)
207 thread->pt_spinlocks--;
208 return ret;
209 #else
210 return pthread__simple_lock_try(lock);
211 #endif
212 }
213
214 void
215 pthread_spinunlock(pthread_spin_t *lock)
216 {
217 #ifdef PTHREAD_SPIN_DEBUG
218 pthread_t thread = pthread__self();
219
220 SDPRINTF(("(pthread_spinunlock %p) spinlock %p (count %d)\n",
221 thread, lock, thread->pt_spinlocks));
222
223 pthread__simple_unlock(lock);
224 thread->pt_spinlocks--;
225 pthread__assert(thread->pt_spinlocks >= 0);
226 PTHREADD_ADD(PTHREADD_SPINUNLOCKS);
227 #else
228 pthread__simple_unlock(lock);
229 #endif
230 }
231
232 /*
233 * Initialize the locking primitives. On uniprocessors, we always
234 * use Restartable Atomic Sequences if they are available. Otherwise,
235 * we fall back onto machine-dependent atomic lock primitives.
236 */
237 void
238 pthread__lockprim_init(void)
239 {
240 char *p;
241
242 if ((p = getenv("PTHREAD_NSPINS")) != NULL)
243 pthread__nspins = atoi(p);
244 else if (pthread__concurrency != 1)
245 pthread__nspins = PTHREAD__NSPINS;
246 else
247 pthread__nspins = 1;
248
249 if (pthread__concurrency != 1) {
250 pthread__atomic = 1;
251 return;
252 }
253
254 if (rasctl(RAS_ADDR(pthread__lock), RAS_SIZE(pthread__lock),
255 RAS_INSTALL) != 0) {
256 pthread__atomic = 1;
257 return;
258 }
259
260 if (rasctl(RAS_ADDR(pthread__lock2), RAS_SIZE(pthread__lock2),
261 RAS_INSTALL) != 0) {
262 pthread__atomic = 1;
263 return;
264 }
265 }
266
267 void
268 pthread_lockinit(pthread_spin_t *lock)
269 {
270
271 pthread__simple_lock_init(lock);
272 }
273