xref: /netbsd-src/lib/libpthread/pthread_lock.c (revision 0920b4f20b78ab1ccd9f2312fbe10deaf000cbf3)
1 /*	$NetBSD: pthread_lock.c,v 1.24 2007/08/16 23:37:08 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.24 2007/08/16 23:37:08 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 	PTHREADD_ADD(PTHREADD_SPINLOCKS);
128 #endif
129 
130 	if (pthread__atomic) {
131 		if (__predict_true(__cpu_simple_lock_try(lock)))
132 			return;
133 	} else {
134 		__cpu_simple_lock_t old;
135 
136 		RAS_START(pthread__lock2);
137 		old = *lock;
138 		*lock = __SIMPLELOCK_LOCKED;
139 		RAS_END(pthread__lock2);
140 
141 		if (__predict_true(old == __SIMPLELOCK_UNLOCKED))
142 			return;
143 	}
144 
145 	pthread_spinlock_slow(lock);
146 }
147 
148 /*
149  * Prevent this routine from being inlined.  The common case is no
150  * contention and it's better to not burden the instruction decoder.
151  */
152 #if __GNUC_PREREQ__(3, 0)
153 __attribute ((noinline))
154 #endif
155 static void
156 pthread_spinlock_slow(pthread_spin_t *lock)
157 {
158 	int count;
159 #ifdef PTHREAD_SPIN_DEBUG
160 	pthread_t thread = pthread__self();
161 #endif
162 
163 	do {
164 		count = pthread__nspins;
165 		while (*lock == __SIMPLELOCK_LOCKED && --count > 0)
166 			pthread__smt_pause();
167 		if (count > 0) {
168 			if (pthread__simple_lock_try(lock))
169 				break;
170 			continue;
171 		}
172 
173 #ifdef PTHREAD_SPIN_DEBUG
174 		SDPRINTF(("(pthread_spinlock %p) retrying spinlock %p "
175 		    "(count %d)\n", thread, lock,
176 		    thread->pt_spinlocks));
177 		thread->pt_spinlocks--;
178 		/* XXXLWP far from ideal */
179 		sched_yield();
180 		thread->pt_spinlocks++;
181 #else
182 		/* XXXLWP far from ideal */
183 		sched_yield();
184 #endif
185 	} while (/*CONSTCOND*/ 1);
186 }
187 
188 int
189 pthread_spintrylock(pthread_spin_t *lock)
190 {
191 #ifdef PTHREAD_SPIN_DEBUG
192 	pthread_t thread = pthread__self();
193 	int ret;
194 
195 	SDPRINTF(("(pthread_spintrylock %p) spinlock %p (count %d)\n",
196 	    thread, lock, thread->pt_spinlocks));
197 	thread->pt_spinlocks++;
198 	ret = pthread__simple_lock_try(lock);
199 	if (!ret)
200 		thread->pt_spinlocks--;
201 	return ret;
202 #else
203 	return pthread__simple_lock_try(lock);
204 #endif
205 }
206 
207 void
208 pthread_spinunlock(pthread_spin_t *lock)
209 {
210 #ifdef PTHREAD_SPIN_DEBUG
211 	pthread_t thread = pthread__self();
212 
213 	SDPRINTF(("(pthread_spinunlock %p) spinlock %p (count %d)\n",
214 	    thread, lock, thread->pt_spinlocks));
215 
216 	pthread__simple_unlock(lock);
217 	thread->pt_spinlocks--;
218 	pthread__assert(thread->pt_spinlocks >= 0);
219 	PTHREADD_ADD(PTHREADD_SPINUNLOCKS);
220 #else
221 	pthread__simple_unlock(lock);
222 #endif
223 }
224 
225 /*
226  * Initialize the locking primitives.  On uniprocessors, we always
227  * use Restartable Atomic Sequences if they are available.  Otherwise,
228  * we fall back onto machine-dependent atomic lock primitives.
229  */
230 void
231 pthread__lockprim_init(void)
232 {
233 	char *p;
234 
235 	if ((p = getenv("PTHREAD_NSPINS")) != NULL)
236 		pthread__nspins = atoi(p);
237 	else if (pthread__concurrency != 1)
238 		pthread__nspins = PTHREAD__NSPINS;
239 	else
240 		pthread__nspins = 1;
241 
242 	if (pthread__concurrency != 1) {
243 		pthread__atomic = 1;
244 		return;
245 	}
246 
247 	if (rasctl(RAS_ADDR(pthread__lock), RAS_SIZE(pthread__lock),
248 	    RAS_INSTALL) != 0) {
249 	    	pthread__atomic = 1;
250 	    	return;
251 	}
252 
253 	if (rasctl(RAS_ADDR(pthread__lock2), RAS_SIZE(pthread__lock2),
254 	    RAS_INSTALL) != 0) {
255 	    	pthread__atomic = 1;
256 	    	return;
257 	}
258 }
259 
260 void
261 pthread_lockinit(pthread_spin_t *lock)
262 {
263 
264 	pthread__simple_lock_init(lock);
265 }
266