pthread_lock.c revision 1.23 1 /* $NetBSD: pthread_lock.c,v 1.23 2007/08/16 13:54:17 ad 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.23 2007/08/16 13:54:17 ad 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 void
75 pthread__simple_lock_init(__cpu_simple_lock_t *alp)
76 {
77
78 if (pthread__atomic) {
79 __cpu_simple_lock_init(alp);
80 return;
81 }
82
83 *alp = __SIMPLELOCK_UNLOCKED;
84 }
85
86 int
87 pthread__simple_lock_try(__cpu_simple_lock_t *alp)
88 {
89 __cpu_simple_lock_t old;
90
91 if (pthread__atomic)
92 return __cpu_simple_lock_try(alp);
93
94 RAS_START(pthread__lock);
95 old = *alp;
96 *alp = __SIMPLELOCK_LOCKED;
97 RAS_END(pthread__lock);
98
99 return old == __SIMPLELOCK_UNLOCKED;
100 }
101
102 inline void
103 pthread__simple_unlock(__cpu_simple_lock_t *alp)
104 {
105
106 #ifdef PTHREAD__CHEAP_UNLOCK
107 __cpu_simple_unlock(alp);
108 #else
109 if (pthread__atomic) {
110 __cpu_simple_unlock(alp);
111 return;
112 }
113 *alp = __SIMPLELOCK_UNLOCKED;
114 #endif
115 }
116
117 void
118 pthread_spinlock(pthread_spin_t *lock)
119 {
120 #ifdef PTHREAD_SPIN_DEBUG
121 pthread_t thread = pthread__self();
122
123 SDPRINTF(("(pthread_spinlock %p) spinlock %p (count %d)\n",
124 thread, lock, thread->pt_spinlocks));
125 pthread__assert(thread->pt_spinlocks >= 0);
126 thread->pt_spinlocks++;
127 #endif
128
129 if (pthread__atomic) {
130 if (__predict_false(!__cpu_simple_lock_try(lock))) {
131 pthread_spinlock_slow(lock);
132 return;
133 }
134 } else {
135 __cpu_simple_lock_t old;
136
137 RAS_START(pthread__lock2);
138 old = *lock;
139 *lock = __SIMPLELOCK_LOCKED;
140 RAS_END(pthread__lock2);
141
142 if (__predict_false(old != __SIMPLELOCK_UNLOCKED)) {
143 pthread_spinlock_slow(lock);
144 return;
145 }
146 }
147
148 PTHREADD_ADD(PTHREADD_SPINLOCKS);
149 }
150
151 /*
152 * Prevent this routine from being inlined. The common case is no
153 * contention and it's better to not burden the instruction decoder.
154 */
155 #if __GNUC_PREREQ__(3, 0)
156 __attribute ((noinline))
157 #endif
158 static void
159 pthread_spinlock_slow(pthread_spin_t *lock)
160 {
161 int count;
162 #ifdef PTHREAD_SPIN_DEBUG
163 pthread_t thread = pthread__self();
164 #endif
165
166 do {
167 count = pthread__nspins;
168 while (*lock == __SIMPLELOCK_LOCKED && --count > 0)
169 pthread__smt_pause();
170 if (count > 0) {
171 if (pthread__simple_lock_try(lock))
172 break;
173 continue;
174 }
175
176 #ifdef PTHREAD_SPIN_DEBUG
177 SDPRINTF(("(pthread_spinlock %p) retrying spinlock %p "
178 "(count %d)\n", thread, lock,
179 thread->pt_spinlocks));
180 thread->pt_spinlocks--;
181 /* XXXLWP far from ideal */
182 sched_yield();
183 thread->pt_spinlocks++;
184 #else
185 /* XXXLWP far from ideal */
186 sched_yield();
187 #endif
188 } while (/*CONSTCOND*/ 1);
189
190 PTHREADD_ADD(PTHREADD_SPINLOCKS);
191 }
192
193 int
194 pthread_spintrylock(pthread_spin_t *lock)
195 {
196 #ifdef PTHREAD_SPIN_DEBUG
197 pthread_t thread = pthread__self();
198 int ret;
199
200 SDPRINTF(("(pthread_spintrylock %p) spinlock %p (count %d)\n",
201 thread, lock, thread->pt_spinlocks));
202 thread->pt_spinlocks++;
203 ret = pthread__simple_lock_try(lock);
204 if (!ret)
205 thread->pt_spinlocks--;
206 return ret;
207 #else
208 return pthread__simple_lock_try(lock);
209 #endif
210 }
211
212 void
213 pthread_spinunlock(pthread_spin_t *lock)
214 {
215 #ifdef PTHREAD_SPIN_DEBUG
216 pthread_t thread = pthread__self();
217
218 SDPRINTF(("(pthread_spinunlock %p) spinlock %p (count %d)\n",
219 thread, lock, thread->pt_spinlocks));
220
221 pthread__simple_unlock(lock);
222 thread->pt_spinlocks--;
223 pthread__assert(thread->pt_spinlocks >= 0);
224 PTHREADD_ADD(PTHREADD_SPINUNLOCKS);
225 #else
226 pthread__simple_unlock(lock);
227 #endif
228 }
229
230 /*
231 * Initialize the locking primitives. On uniprocessors, we always
232 * use Restartable Atomic Sequences if they are available. Otherwise,
233 * we fall back onto machine-dependent atomic lock primitives.
234 */
235 void
236 pthread__lockprim_init(void)
237 {
238 char *p;
239
240 if ((p = getenv("PTHREAD_NSPINS")) != NULL)
241 pthread__nspins = atoi(p);
242 else if (pthread__concurrency != 1)
243 pthread__nspins = PTHREAD__NSPINS;
244 else
245 pthread__nspins = 1;
246
247 if (pthread__concurrency != 1) {
248 pthread__atomic = 1;
249 return;
250 }
251
252 if (rasctl(RAS_ADDR(pthread__lock), RAS_SIZE(pthread__lock),
253 RAS_INSTALL) != 0) {
254 pthread__atomic = 1;
255 return;
256 }
257
258 if (rasctl(RAS_ADDR(pthread__lock2), RAS_SIZE(pthread__lock2),
259 RAS_INSTALL) != 0) {
260 pthread__atomic = 1;
261 return;
262 }
263 }
264
265 void
266 pthread_lockinit(pthread_spin_t *lock)
267 {
268
269 pthread__simple_lock_init(lock);
270 }
271