pthread_lock.c revision 1.1.2.10 1 1.1.2.10 nathanw /* $NetBSD: pthread_lock.c,v 1.1.2.10 2002/10/22 01:38:17 nathanw Exp $ */
2 1.1.2.3 nathanw
3 1.1.2.3 nathanw /*-
4 1.1.2.3 nathanw * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1.2.3 nathanw * All rights reserved.
6 1.1.2.3 nathanw *
7 1.1.2.3 nathanw * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.3 nathanw * by Nathan J. Williams.
9 1.1.2.3 nathanw *
10 1.1.2.3 nathanw * Redistribution and use in source and binary forms, with or without
11 1.1.2.3 nathanw * modification, are permitted provided that the following conditions
12 1.1.2.3 nathanw * are met:
13 1.1.2.3 nathanw * 1. Redistributions of source code must retain the above copyright
14 1.1.2.3 nathanw * notice, this list of conditions and the following disclaimer.
15 1.1.2.3 nathanw * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.3 nathanw * notice, this list of conditions and the following disclaimer in the
17 1.1.2.3 nathanw * documentation and/or other materials provided with the distribution.
18 1.1.2.3 nathanw * 3. All advertising materials mentioning features or use of this software
19 1.1.2.3 nathanw * must display the following acknowledgement:
20 1.1.2.3 nathanw * This product includes software developed by the NetBSD
21 1.1.2.3 nathanw * Foundation, Inc. and its contributors.
22 1.1.2.3 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.2.3 nathanw * contributors may be used to endorse or promote products derived
24 1.1.2.3 nathanw * from this software without specific prior written permission.
25 1.1.2.3 nathanw *
26 1.1.2.3 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.2.3 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.2.3 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.2.3 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.2.3 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.2.3 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.2.3 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.2.3 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.2.3 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.2.3 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.2.3 nathanw * POSSIBILITY OF SUCH DAMAGE.
37 1.1.2.3 nathanw */
38 1.1.2.1 nathanw
39 1.1.2.6 nathanw #include <assert.h>
40 1.1.2.8 nathanw #include <errno.h>
41 1.1.2.6 nathanw
42 1.1.2.1 nathanw #include "pthread.h"
43 1.1.2.1 nathanw #include "pthread_int.h"
44 1.1.2.1 nathanw
45 1.1.2.6 nathanw #undef PTHREAD_SA_DEBUG
46 1.1.2.6 nathanw
47 1.1.2.6 nathanw #ifdef PTHREAD_SA_DEBUG
48 1.1.2.6 nathanw #define SDPRINTF(x) DPRINTF(x)
49 1.1.2.6 nathanw #else
50 1.1.2.6 nathanw #define SDPRINTF(x)
51 1.1.2.6 nathanw #endif
52 1.1.2.6 nathanw
53 1.1.2.1 nathanw /* How many times to try before checking whether we've been continued. */
54 1.1.2.10 nathanw #define NSPINS 1 /* no point in actually spinning until MP works */
55 1.1.2.1 nathanw
56 1.1.2.1 nathanw static int nspins = NSPINS;
57 1.1.2.1 nathanw
58 1.1.2.1 nathanw void
59 1.1.2.6 nathanw pthread_lockinit(pthread_spin_t *lock)
60 1.1.2.2 nathanw {
61 1.1.2.2 nathanw
62 1.1.2.2 nathanw __cpu_simple_lock_init(lock);
63 1.1.2.2 nathanw }
64 1.1.2.2 nathanw
65 1.1.2.2 nathanw void
66 1.1.2.6 nathanw pthread_spinlock(pthread_t thread, pthread_spin_t *lock)
67 1.1.2.1 nathanw {
68 1.1.2.1 nathanw int count, ret;
69 1.1.2.1 nathanw
70 1.1.2.1 nathanw count = nspins;
71 1.1.2.6 nathanw SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
72 1.1.2.6 nathanw thread, thread->pt_spinlocks));
73 1.1.2.1 nathanw ++thread->pt_spinlocks;
74 1.1.2.1 nathanw
75 1.1.2.1 nathanw do {
76 1.1.2.1 nathanw while (((ret = __cpu_simple_lock_try(lock)) == 0) && --count)
77 1.1.2.1 nathanw ;
78 1.1.2.1 nathanw
79 1.1.2.1 nathanw if (ret == 1)
80 1.1.2.1 nathanw break;
81 1.1.2.6 nathanw
82 1.1.2.6 nathanw SDPRINTF(("(pthread_spinlock %p) decrementing spinlock from %d\n",
83 1.1.2.6 nathanw thread, thread->pt_spinlocks));
84 1.1.2.1 nathanw --thread->pt_spinlocks;
85 1.1.2.1 nathanw
86 1.1.2.9 nathanw /*
87 1.1.2.9 nathanw * We may be preempted while spinning. If so, we will
88 1.1.2.1 nathanw * be restarted here if thread->pt_spinlocks is
89 1.1.2.1 nathanw * nonzero, which can happen if:
90 1.1.2.1 nathanw * a) we just got the lock
91 1.1.2.1 nathanw * b) we haven't yet decremented the lock count.
92 1.1.2.1 nathanw * If we're at this point, (b) applies. Therefore,
93 1.1.2.1 nathanw * check if we're being continued, and if so, bail.
94 1.1.2.1 nathanw * (in case (a), we should let the code finish and
95 1.1.2.1 nathanw * we will bail out in pthread_spinunlock()).
96 1.1.2.1 nathanw */
97 1.1.2.10 nathanw if (thread->pt_next != NULL) {
98 1.1.2.1 nathanw PTHREADD_ADD(PTHREADD_SPINPREEMPT);
99 1.1.2.5 nathanw pthread__switch(thread, thread->pt_next);
100 1.1.2.1 nathanw }
101 1.1.2.1 nathanw /* try again */
102 1.1.2.1 nathanw count = nspins;
103 1.1.2.6 nathanw SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
104 1.1.2.6 nathanw thread, thread->pt_spinlocks));
105 1.1.2.1 nathanw ++thread->pt_spinlocks;
106 1.1.2.1 nathanw } while (/*CONSTCOND*/1);
107 1.1.2.1 nathanw
108 1.1.2.1 nathanw PTHREADD_ADD(PTHREADD_SPINLOCKS);
109 1.1.2.1 nathanw /* Got it! We're out of here. */
110 1.1.2.1 nathanw }
111 1.1.2.1 nathanw
112 1.1.2.1 nathanw
113 1.1.2.1 nathanw int
114 1.1.2.6 nathanw pthread_spintrylock(pthread_t thread, pthread_spin_t *lock)
115 1.1.2.1 nathanw {
116 1.1.2.1 nathanw int ret;
117 1.1.2.1 nathanw
118 1.1.2.6 nathanw SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
119 1.1.2.6 nathanw thread, thread->pt_spinlocks));
120 1.1.2.1 nathanw ++thread->pt_spinlocks;
121 1.1.2.1 nathanw
122 1.1.2.1 nathanw ret = __cpu_simple_lock_try(lock);
123 1.1.2.1 nathanw
124 1.1.2.1 nathanw if (ret == 0) {
125 1.1.2.6 nathanw SDPRINTF(("(pthread_spintrylock %p) decrementing spinlock from %d\n",
126 1.1.2.6 nathanw thread, thread->pt_spinlocks));
127 1.1.2.1 nathanw --thread->pt_spinlocks;
128 1.1.2.1 nathanw /* See above. */
129 1.1.2.10 nathanw if (thread->pt_next != NULL) {
130 1.1.2.1 nathanw PTHREADD_ADD(PTHREADD_SPINPREEMPT);
131 1.1.2.5 nathanw pthread__switch(thread, thread->pt_next);
132 1.1.2.1 nathanw }
133 1.1.2.1 nathanw }
134 1.1.2.1 nathanw
135 1.1.2.1 nathanw return ret;
136 1.1.2.1 nathanw }
137 1.1.2.1 nathanw
138 1.1.2.1 nathanw
139 1.1.2.1 nathanw void
140 1.1.2.6 nathanw pthread_spinunlock(pthread_t thread, pthread_spin_t *lock)
141 1.1.2.1 nathanw {
142 1.1.2.9 nathanw
143 1.1.2.1 nathanw __cpu_simple_unlock(lock);
144 1.1.2.6 nathanw SDPRINTF(("(pthread_spinunlock %p) decrementing spinlock from %d\n",
145 1.1.2.6 nathanw thread, thread->pt_spinlocks));
146 1.1.2.1 nathanw --thread->pt_spinlocks;
147 1.1.2.1 nathanw
148 1.1.2.1 nathanw PTHREADD_ADD(PTHREADD_SPINUNLOCKS);
149 1.1.2.1 nathanw
150 1.1.2.9 nathanw /*
151 1.1.2.9 nathanw * If we were preempted while holding a spinlock, the
152 1.1.2.1 nathanw * scheduler will notice this and continue us. To be good
153 1.1.2.1 nathanw * citzens, we must now get out of here if that was our
154 1.1.2.1 nathanw * last spinlock.
155 1.1.2.1 nathanw * XXX when will we ever have more than one?
156 1.1.2.1 nathanw */
157 1.1.2.1 nathanw
158 1.1.2.10 nathanw if ((thread->pt_spinlocks == 0) && (thread->pt_next != NULL)) {
159 1.1.2.1 nathanw PTHREADD_ADD(PTHREADD_SPINPREEMPT);
160 1.1.2.5 nathanw pthread__switch(thread, thread->pt_next);
161 1.1.2.1 nathanw }
162 1.1.2.8 nathanw }
163 1.1.2.8 nathanw
164 1.1.2.8 nathanw
165 1.1.2.8 nathanw /*
166 1.1.2.8 nathanw * Public (POSIX-specified) spinlocks.
167 1.1.2.8 nathanw * These don't interact with the spin-preemption code, nor do they
168 1.1.2.8 nathanw * perform any adaptive sleeping.
169 1.1.2.8 nathanw */
170 1.1.2.8 nathanw
171 1.1.2.8 nathanw int
172 1.1.2.8 nathanw pthread_spin_init(pthread_spinlock_t *lock, int pshared)
173 1.1.2.8 nathanw {
174 1.1.2.8 nathanw
175 1.1.2.8 nathanw #ifdef ERRORCHECK
176 1.1.2.8 nathanw if ((lock == NULL) ||
177 1.1.2.8 nathanw ((pshared != PTHREAD_PROCESS_PRIVATE) &&
178 1.1.2.8 nathanw (pshared != PTHREAD_PROCESS_SHARED)))
179 1.1.2.8 nathanw return EINVAL;
180 1.1.2.8 nathanw #endif
181 1.1.2.8 nathanw lock->pts_magic = _PT_SPINLOCK_MAGIC;
182 1.1.2.9 nathanw /*
183 1.1.2.9 nathanw * We don't actually use the pshared flag for anything;
184 1.1.2.8 nathanw * cpu simple locks have all the process-shared properties
185 1.1.2.8 nathanw * that we want anyway.
186 1.1.2.8 nathanw */
187 1.1.2.8 nathanw lock->pts_flags = pshared;
188 1.1.2.8 nathanw pthread_lockinit(&lock->pts_spin);
189 1.1.2.8 nathanw
190 1.1.2.8 nathanw return 0;
191 1.1.2.8 nathanw }
192 1.1.2.8 nathanw
193 1.1.2.8 nathanw int
194 1.1.2.8 nathanw pthread_spin_destroy(pthread_spinlock_t *lock)
195 1.1.2.8 nathanw {
196 1.1.2.8 nathanw
197 1.1.2.8 nathanw #ifdef ERRORCHECK
198 1.1.2.8 nathanw if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
199 1.1.2.8 nathanw return EINVAL;
200 1.1.2.8 nathanw
201 1.1.2.8 nathanw if (lock->pts_spin != __SIMPLELOCK_UNLOCKED)
202 1.1.2.8 nathanw return EBUSY;
203 1.1.2.8 nathanw #endif
204 1.1.2.8 nathanw
205 1.1.2.8 nathanw lock->pts_magic = _PT_SPINLOCK_DEAD;
206 1.1.2.8 nathanw
207 1.1.2.8 nathanw return 0;
208 1.1.2.8 nathanw }
209 1.1.2.8 nathanw
210 1.1.2.8 nathanw int
211 1.1.2.8 nathanw pthread_spin_lock(pthread_spinlock_t *lock)
212 1.1.2.8 nathanw {
213 1.1.2.8 nathanw
214 1.1.2.8 nathanw #ifdef ERRORCHECK
215 1.1.2.8 nathanw if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
216 1.1.2.8 nathanw return EINVAL;
217 1.1.2.8 nathanw #endif
218 1.1.2.8 nathanw
219 1.1.2.8 nathanw __cpu_simple_lock(&lock->pts_spin);
220 1.1.2.8 nathanw
221 1.1.2.8 nathanw return 0;
222 1.1.2.8 nathanw }
223 1.1.2.8 nathanw
224 1.1.2.8 nathanw int
225 1.1.2.8 nathanw pthread_spin_trylock(pthread_spinlock_t *lock)
226 1.1.2.8 nathanw {
227 1.1.2.8 nathanw
228 1.1.2.8 nathanw #ifdef ERRORCHECK
229 1.1.2.8 nathanw if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
230 1.1.2.8 nathanw return EINVAL;
231 1.1.2.8 nathanw #endif
232 1.1.2.8 nathanw
233 1.1.2.8 nathanw if (__cpu_simple_lock_try(&lock->pts_spin) == 0)
234 1.1.2.8 nathanw return EBUSY;
235 1.1.2.8 nathanw
236 1.1.2.8 nathanw return 0;
237 1.1.2.8 nathanw }
238 1.1.2.8 nathanw
239 1.1.2.8 nathanw int
240 1.1.2.8 nathanw pthread_spin_unlock(pthread_spinlock_t *lock)
241 1.1.2.8 nathanw {
242 1.1.2.8 nathanw
243 1.1.2.8 nathanw #ifdef ERRORCHECK
244 1.1.2.8 nathanw if ((lock == NULL) || (lock->pts_magic != _PT_SPINLOCK_MAGIC))
245 1.1.2.8 nathanw return EINVAL;
246 1.1.2.8 nathanw #endif
247 1.1.2.8 nathanw
248 1.1.2.8 nathanw __cpu_simple_unlock(&lock->pts_spin);
249 1.1.2.8 nathanw
250 1.1.2.8 nathanw return 0;
251 1.1.2.1 nathanw }
252 1.1.2.1 nathanw
253