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