kern_turnstile.c revision 1.1.18.4 1 1.1.18.4 yamt /* $NetBSD: kern_turnstile.c,v 1.1.18.4 2007/12/07 17:32:54 yamt Exp $ */
2 1.1.18.1 yamt
3 1.1.18.1 yamt /*-
4 1.1.18.1 yamt * Copyright (c) 2002, 2006, 2007 The NetBSD Foundation, Inc.
5 1.1.18.1 yamt * All rights reserved.
6 1.1.18.1 yamt *
7 1.1.18.1 yamt * This code is derived from software contributed to The NetBSD Foundation
8 1.1.18.1 yamt * by Jason R. Thorpe and Andrew Doran.
9 1.1.18.1 yamt *
10 1.1.18.1 yamt * Redistribution and use in source and binary forms, with or without
11 1.1.18.1 yamt * modification, are permitted provided that the following conditions
12 1.1.18.1 yamt * are met:
13 1.1.18.1 yamt * 1. Redistributions of source code must retain the above copyright
14 1.1.18.1 yamt * notice, this list of conditions and the following disclaimer.
15 1.1.18.1 yamt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.18.1 yamt * notice, this list of conditions and the following disclaimer in the
17 1.1.18.1 yamt * documentation and/or other materials provided with the distribution.
18 1.1.18.1 yamt * 3. All advertising materials mentioning features or use of this software
19 1.1.18.1 yamt * must display the following acknowledgement:
20 1.1.18.1 yamt * This product includes software developed by the NetBSD
21 1.1.18.1 yamt * Foundation, Inc. and its contributors.
22 1.1.18.1 yamt * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.18.1 yamt * contributors may be used to endorse or promote products derived
24 1.1.18.1 yamt * from this software without specific prior written permission.
25 1.1.18.1 yamt *
26 1.1.18.1 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.18.1 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.18.1 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.18.1 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.18.1 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.18.1 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.18.1 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.18.1 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.18.1 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.18.1 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.18.1 yamt * POSSIBILITY OF SUCH DAMAGE.
37 1.1.18.1 yamt */
38 1.1.18.1 yamt
39 1.1.18.1 yamt /*
40 1.1.18.1 yamt * Turnstiles are described in detail in:
41 1.1.18.1 yamt *
42 1.1.18.1 yamt * Solaris Internals: Core Kernel Architecture, Jim Mauro and
43 1.1.18.1 yamt * Richard McDougall.
44 1.1.18.1 yamt *
45 1.1.18.1 yamt * Turnstiles are kept in a hash table. There are likely to be many more
46 1.1.18.1 yamt * synchronisation objects than there are threads. Since a thread can block
47 1.1.18.1 yamt * on only one lock at a time, we only need one turnstile per thread, and
48 1.1.18.1 yamt * so they are allocated at thread creation time.
49 1.1.18.1 yamt *
50 1.1.18.1 yamt * When a thread decides it needs to block on a lock, it looks up the
51 1.1.18.1 yamt * active turnstile for that lock. If no active turnstile exists, then
52 1.1.18.1 yamt * the process lends its turnstile to the lock. If there is already an
53 1.1.18.1 yamt * active turnstile for the lock, the thread places its turnstile on a
54 1.1.18.1 yamt * list of free turnstiles, and references the active one instead.
55 1.1.18.1 yamt *
56 1.1.18.1 yamt * The act of looking up the turnstile acquires an interlock on the sleep
57 1.1.18.1 yamt * queue. If a thread decides it doesn't need to block after all, then this
58 1.1.18.1 yamt * interlock must be released by explicitly aborting the turnstile
59 1.1.18.1 yamt * operation.
60 1.1.18.1 yamt *
61 1.1.18.1 yamt * When a thread is awakened, it needs to get its turnstile back. If there
62 1.1.18.1 yamt * are still other threads waiting in the active turnstile, the the thread
63 1.1.18.1 yamt * grabs a free turnstile off the free list. Otherwise, it can take back
64 1.1.18.1 yamt * the active turnstile from the lock (thus deactivating the turnstile).
65 1.1.18.1 yamt *
66 1.1.18.2 yamt * Turnstiles are the place to do priority inheritence.
67 1.1.18.1 yamt */
68 1.1.18.1 yamt
69 1.1.18.1 yamt #include <sys/cdefs.h>
70 1.1.18.4 yamt __KERNEL_RCSID(0, "$NetBSD: kern_turnstile.c,v 1.1.18.4 2007/12/07 17:32:54 yamt Exp $");
71 1.1.18.1 yamt
72 1.1.18.1 yamt #include <sys/param.h>
73 1.1.18.1 yamt #include <sys/lock.h>
74 1.1.18.2 yamt #include <sys/lockdebug.h>
75 1.1.18.1 yamt #include <sys/pool.h>
76 1.1.18.1 yamt #include <sys/proc.h>
77 1.1.18.1 yamt #include <sys/sleepq.h>
78 1.1.18.1 yamt #include <sys/systm.h>
79 1.1.18.1 yamt
80 1.1.18.1 yamt #include <uvm/uvm_extern.h>
81 1.1.18.1 yamt
82 1.1.18.1 yamt #define TS_HASH_SIZE 64
83 1.1.18.1 yamt #define TS_HASH_MASK (TS_HASH_SIZE - 1)
84 1.1.18.1 yamt #define TS_HASH(obj) (((uintptr_t)(obj) >> 3) & TS_HASH_MASK)
85 1.1.18.1 yamt
86 1.1.18.1 yamt tschain_t turnstile_tab[TS_HASH_SIZE];
87 1.1.18.3 yamt pool_cache_t turnstile_cache;
88 1.1.18.1 yamt
89 1.1.18.1 yamt int turnstile_ctor(void *, void *, int);
90 1.1.18.1 yamt
91 1.1.18.1 yamt extern turnstile_t turnstile0;
92 1.1.18.1 yamt
93 1.1.18.1 yamt /*
94 1.1.18.1 yamt * turnstile_init:
95 1.1.18.1 yamt *
96 1.1.18.1 yamt * Initialize the turnstile mechanism.
97 1.1.18.1 yamt */
98 1.1.18.1 yamt void
99 1.1.18.1 yamt turnstile_init(void)
100 1.1.18.1 yamt {
101 1.1.18.1 yamt tschain_t *tc;
102 1.1.18.1 yamt int i;
103 1.1.18.1 yamt
104 1.1.18.1 yamt for (i = 0; i < TS_HASH_SIZE; i++) {
105 1.1.18.1 yamt tc = &turnstile_tab[i];
106 1.1.18.1 yamt LIST_INIT(&tc->tc_chain);
107 1.1.18.4 yamt mutex_init(&tc->tc_mutex, MUTEX_DEFAULT, IPL_SCHED);
108 1.1.18.1 yamt }
109 1.1.18.1 yamt
110 1.1.18.3 yamt turnstile_cache = pool_cache_init(sizeof(turnstile_t), 0, 0, 0,
111 1.1.18.3 yamt "tstilepl", NULL, IPL_NONE, turnstile_ctor, NULL, NULL);
112 1.1.18.3 yamt KASSERT(turnstile_cache != NULL);
113 1.1.18.1 yamt
114 1.1.18.1 yamt (void)turnstile_ctor(NULL, &turnstile0, 0);
115 1.1.18.1 yamt }
116 1.1.18.1 yamt
117 1.1.18.1 yamt /*
118 1.1.18.1 yamt * turnstile_ctor:
119 1.1.18.1 yamt *
120 1.1.18.1 yamt * Constructor for turnstiles.
121 1.1.18.1 yamt */
122 1.1.18.1 yamt int
123 1.1.18.1 yamt turnstile_ctor(void *arg, void *obj, int flags)
124 1.1.18.1 yamt {
125 1.1.18.1 yamt turnstile_t *ts = obj;
126 1.1.18.1 yamt
127 1.1.18.1 yamt memset(ts, 0, sizeof(*ts));
128 1.1.18.1 yamt sleepq_init(&ts->ts_sleepq[TS_READER_Q], NULL);
129 1.1.18.1 yamt sleepq_init(&ts->ts_sleepq[TS_WRITER_Q], NULL);
130 1.1.18.1 yamt return (0);
131 1.1.18.1 yamt }
132 1.1.18.1 yamt
133 1.1.18.1 yamt /*
134 1.1.18.1 yamt * turnstile_remove:
135 1.1.18.1 yamt *
136 1.1.18.1 yamt * Remove an LWP from a turnstile sleep queue and wake it.
137 1.1.18.1 yamt */
138 1.1.18.2 yamt static inline void
139 1.1.18.2 yamt turnstile_remove(turnstile_t *ts, lwp_t *l, sleepq_t *sq)
140 1.1.18.1 yamt {
141 1.1.18.1 yamt turnstile_t *nts;
142 1.1.18.1 yamt
143 1.1.18.1 yamt KASSERT(l->l_ts == ts);
144 1.1.18.1 yamt
145 1.1.18.1 yamt /*
146 1.1.18.1 yamt * This process is no longer using the active turnstile.
147 1.1.18.1 yamt * Find an inactive one on the free list to give to it.
148 1.1.18.1 yamt */
149 1.1.18.1 yamt if ((nts = ts->ts_free) != NULL) {
150 1.1.18.1 yamt KASSERT(TS_ALL_WAITERS(ts) > 1);
151 1.1.18.1 yamt l->l_ts = nts;
152 1.1.18.1 yamt ts->ts_free = nts->ts_free;
153 1.1.18.1 yamt nts->ts_free = NULL;
154 1.1.18.1 yamt } else {
155 1.1.18.1 yamt /*
156 1.1.18.1 yamt * If the free list is empty, this is the last
157 1.1.18.1 yamt * waiter.
158 1.1.18.1 yamt */
159 1.1.18.1 yamt KASSERT(TS_ALL_WAITERS(ts) == 1);
160 1.1.18.1 yamt LIST_REMOVE(ts, ts_chain);
161 1.1.18.1 yamt }
162 1.1.18.1 yamt
163 1.1.18.2 yamt (void)sleepq_remove(sq, l);
164 1.1.18.1 yamt }
165 1.1.18.1 yamt
166 1.1.18.1 yamt /*
167 1.1.18.1 yamt * turnstile_lookup:
168 1.1.18.1 yamt *
169 1.1.18.1 yamt * Look up the turnstile for the specified lock. This acquires and
170 1.1.18.1 yamt * holds the turnstile chain lock (sleep queue interlock).
171 1.1.18.1 yamt */
172 1.1.18.1 yamt turnstile_t *
173 1.1.18.1 yamt turnstile_lookup(wchan_t obj)
174 1.1.18.1 yamt {
175 1.1.18.1 yamt turnstile_t *ts;
176 1.1.18.1 yamt tschain_t *tc;
177 1.1.18.1 yamt
178 1.1.18.1 yamt tc = &turnstile_tab[TS_HASH(obj)];
179 1.1.18.2 yamt mutex_spin_enter(&tc->tc_mutex);
180 1.1.18.1 yamt
181 1.1.18.1 yamt LIST_FOREACH(ts, &tc->tc_chain, ts_chain)
182 1.1.18.1 yamt if (ts->ts_obj == obj)
183 1.1.18.1 yamt return (ts);
184 1.1.18.1 yamt
185 1.1.18.1 yamt /*
186 1.1.18.1 yamt * No turnstile yet for this lock. No problem, turnstile_block()
187 1.1.18.1 yamt * handles this by fetching the turnstile from the blocking thread.
188 1.1.18.1 yamt */
189 1.1.18.1 yamt return (NULL);
190 1.1.18.1 yamt }
191 1.1.18.1 yamt
192 1.1.18.1 yamt /*
193 1.1.18.1 yamt * turnstile_exit:
194 1.1.18.1 yamt *
195 1.1.18.1 yamt * Abort a turnstile operation.
196 1.1.18.1 yamt */
197 1.1.18.1 yamt void
198 1.1.18.1 yamt turnstile_exit(wchan_t obj)
199 1.1.18.1 yamt {
200 1.1.18.1 yamt tschain_t *tc;
201 1.1.18.1 yamt
202 1.1.18.1 yamt tc = &turnstile_tab[TS_HASH(obj)];
203 1.1.18.2 yamt mutex_spin_exit(&tc->tc_mutex);
204 1.1.18.1 yamt }
205 1.1.18.1 yamt
206 1.1.18.1 yamt /*
207 1.1.18.1 yamt * turnstile_block:
208 1.1.18.1 yamt *
209 1.1.18.1 yamt * Enter an object into the turnstile chain and prepare the current
210 1.1.18.1 yamt * LWP for sleep.
211 1.1.18.1 yamt */
212 1.1.18.1 yamt void
213 1.1.18.2 yamt turnstile_block(turnstile_t *ts, int q, wchan_t obj, syncobj_t *sobj)
214 1.1.18.1 yamt {
215 1.1.18.2 yamt lwp_t *l;
216 1.1.18.2 yamt lwp_t *cur; /* cached curlwp */
217 1.1.18.2 yamt lwp_t *owner;
218 1.1.18.1 yamt turnstile_t *ots;
219 1.1.18.1 yamt tschain_t *tc;
220 1.1.18.1 yamt sleepq_t *sq;
221 1.1.18.2 yamt pri_t prio;
222 1.1.18.1 yamt
223 1.1.18.1 yamt tc = &turnstile_tab[TS_HASH(obj)];
224 1.1.18.2 yamt l = cur = curlwp;
225 1.1.18.1 yamt
226 1.1.18.1 yamt KASSERT(q == TS_READER_Q || q == TS_WRITER_Q);
227 1.1.18.2 yamt KASSERT(mutex_owned(&tc->tc_mutex));
228 1.1.18.1 yamt KASSERT(l != NULL && l->l_ts != NULL);
229 1.1.18.1 yamt
230 1.1.18.1 yamt if (ts == NULL) {
231 1.1.18.1 yamt /*
232 1.1.18.1 yamt * We are the first thread to wait for this object;
233 1.1.18.1 yamt * lend our turnstile to it.
234 1.1.18.1 yamt */
235 1.1.18.1 yamt ts = l->l_ts;
236 1.1.18.1 yamt KASSERT(TS_ALL_WAITERS(ts) == 0);
237 1.1.18.1 yamt KASSERT(TAILQ_EMPTY(&ts->ts_sleepq[TS_READER_Q].sq_queue) &&
238 1.1.18.1 yamt TAILQ_EMPTY(&ts->ts_sleepq[TS_WRITER_Q].sq_queue));
239 1.1.18.1 yamt ts->ts_obj = obj;
240 1.1.18.2 yamt ts->ts_inheritor = NULL;
241 1.1.18.2 yamt ts->ts_sleepq[TS_READER_Q].sq_mutex = &tc->tc_mutex;
242 1.1.18.2 yamt ts->ts_sleepq[TS_WRITER_Q].sq_mutex = &tc->tc_mutex;
243 1.1.18.1 yamt LIST_INSERT_HEAD(&tc->tc_chain, ts, ts_chain);
244 1.1.18.1 yamt } else {
245 1.1.18.1 yamt /*
246 1.1.18.1 yamt * Object already has a turnstile. Put our turnstile
247 1.1.18.1 yamt * onto the free list, and reference the existing
248 1.1.18.1 yamt * turnstile instead.
249 1.1.18.1 yamt */
250 1.1.18.1 yamt ots = l->l_ts;
251 1.1.18.1 yamt ots->ts_free = ts->ts_free;
252 1.1.18.1 yamt ts->ts_free = ots;
253 1.1.18.1 yamt l->l_ts = ts;
254 1.1.18.1 yamt
255 1.1.18.2 yamt KASSERT(ts->ts_obj == obj);
256 1.1.18.1 yamt KASSERT(TS_ALL_WAITERS(ts) != 0);
257 1.1.18.1 yamt KASSERT(!TAILQ_EMPTY(&ts->ts_sleepq[TS_READER_Q].sq_queue) ||
258 1.1.18.1 yamt !TAILQ_EMPTY(&ts->ts_sleepq[TS_WRITER_Q].sq_queue));
259 1.1.18.1 yamt }
260 1.1.18.1 yamt
261 1.1.18.1 yamt sq = &ts->ts_sleepq[q];
262 1.1.18.1 yamt sleepq_enter(sq, l);
263 1.1.18.2 yamt LOCKDEBUG_BARRIER(&tc->tc_mutex, 1);
264 1.1.18.3 yamt l->l_kpriority = true;
265 1.1.18.3 yamt sleepq_enqueue(sq, obj, "tstile", sobj);
266 1.1.18.2 yamt
267 1.1.18.2 yamt /*
268 1.1.18.2 yamt * lend our priority to lwps on the blocking chain.
269 1.1.18.2 yamt */
270 1.1.18.3 yamt prio = lwp_eprio(l);
271 1.1.18.2 yamt for (;;) {
272 1.1.18.2 yamt bool dolock;
273 1.1.18.2 yamt
274 1.1.18.2 yamt if (l->l_wchan == NULL)
275 1.1.18.2 yamt break;
276 1.1.18.2 yamt
277 1.1.18.2 yamt owner = (*l->l_syncobj->sobj_owner)(l->l_wchan);
278 1.1.18.2 yamt if (owner == NULL)
279 1.1.18.2 yamt break;
280 1.1.18.2 yamt
281 1.1.18.2 yamt KASSERT(l != owner);
282 1.1.18.2 yamt KASSERT(cur != owner);
283 1.1.18.2 yamt
284 1.1.18.2 yamt if (l->l_mutex != owner->l_mutex)
285 1.1.18.2 yamt dolock = true;
286 1.1.18.2 yamt else
287 1.1.18.2 yamt dolock = false;
288 1.1.18.2 yamt if (dolock && !lwp_trylock(owner)) {
289 1.1.18.2 yamt /*
290 1.1.18.2 yamt * restart from curlwp.
291 1.1.18.2 yamt */
292 1.1.18.2 yamt lwp_unlock(l);
293 1.1.18.2 yamt l = cur;
294 1.1.18.2 yamt lwp_lock(l);
295 1.1.18.2 yamt prio = lwp_eprio(l);
296 1.1.18.2 yamt continue;
297 1.1.18.2 yamt }
298 1.1.18.3 yamt if (prio <= lwp_eprio(owner)) {
299 1.1.18.2 yamt if (dolock)
300 1.1.18.2 yamt lwp_unlock(owner);
301 1.1.18.2 yamt break;
302 1.1.18.2 yamt }
303 1.1.18.2 yamt ts = l->l_ts;
304 1.1.18.2 yamt KASSERT(ts->ts_inheritor == owner || ts->ts_inheritor == NULL);
305 1.1.18.2 yamt if (ts->ts_inheritor == NULL) {
306 1.1.18.2 yamt ts->ts_inheritor = owner;
307 1.1.18.2 yamt ts->ts_eprio = prio;
308 1.1.18.2 yamt SLIST_INSERT_HEAD(&owner->l_pi_lenders, ts, ts_pichain);
309 1.1.18.2 yamt lwp_lendpri(owner, prio);
310 1.1.18.3 yamt } else if (prio > ts->ts_eprio) {
311 1.1.18.2 yamt ts->ts_eprio = prio;
312 1.1.18.2 yamt lwp_lendpri(owner, prio);
313 1.1.18.2 yamt }
314 1.1.18.2 yamt if (dolock)
315 1.1.18.2 yamt lwp_unlock(l);
316 1.1.18.2 yamt l = owner;
317 1.1.18.2 yamt }
318 1.1.18.2 yamt LOCKDEBUG_BARRIER(l->l_mutex, 1);
319 1.1.18.2 yamt if (cur->l_mutex != l->l_mutex) {
320 1.1.18.2 yamt lwp_unlock(l);
321 1.1.18.2 yamt lwp_lock(cur);
322 1.1.18.2 yamt }
323 1.1.18.2 yamt LOCKDEBUG_BARRIER(cur->l_mutex, 1);
324 1.1.18.2 yamt
325 1.1.18.2 yamt sleepq_block(0, false);
326 1.1.18.1 yamt }
327 1.1.18.1 yamt
328 1.1.18.1 yamt /*
329 1.1.18.1 yamt * turnstile_wakeup:
330 1.1.18.1 yamt *
331 1.1.18.1 yamt * Wake up the specified number of threads that are blocked
332 1.1.18.1 yamt * in a turnstile.
333 1.1.18.1 yamt */
334 1.1.18.1 yamt void
335 1.1.18.2 yamt turnstile_wakeup(turnstile_t *ts, int q, int count, lwp_t *nl)
336 1.1.18.1 yamt {
337 1.1.18.1 yamt sleepq_t *sq;
338 1.1.18.1 yamt tschain_t *tc;
339 1.1.18.2 yamt lwp_t *l;
340 1.1.18.1 yamt
341 1.1.18.1 yamt tc = &turnstile_tab[TS_HASH(ts->ts_obj)];
342 1.1.18.1 yamt sq = &ts->ts_sleepq[q];
343 1.1.18.1 yamt
344 1.1.18.1 yamt KASSERT(q == TS_READER_Q || q == TS_WRITER_Q);
345 1.1.18.1 yamt KASSERT(count > 0 && count <= TS_WAITERS(ts, q));
346 1.1.18.2 yamt KASSERT(mutex_owned(&tc->tc_mutex) && sq->sq_mutex == &tc->tc_mutex);
347 1.1.18.2 yamt KASSERT(ts->ts_inheritor == curlwp || ts->ts_inheritor == NULL);
348 1.1.18.2 yamt
349 1.1.18.2 yamt /*
350 1.1.18.2 yamt * restore inherited priority if necessary.
351 1.1.18.2 yamt */
352 1.1.18.2 yamt
353 1.1.18.2 yamt if (ts->ts_inheritor != NULL) {
354 1.1.18.2 yamt turnstile_t *iter;
355 1.1.18.2 yamt turnstile_t *next;
356 1.1.18.2 yamt turnstile_t *prev = NULL;
357 1.1.18.2 yamt pri_t prio;
358 1.1.18.2 yamt bool dolock;
359 1.1.18.2 yamt
360 1.1.18.2 yamt ts->ts_inheritor = NULL;
361 1.1.18.2 yamt l = curlwp;
362 1.1.18.2 yamt
363 1.1.18.2 yamt dolock = l->l_mutex == &l->l_cpu->ci_schedstate.spc_lwplock;
364 1.1.18.2 yamt if (dolock) {
365 1.1.18.2 yamt lwp_lock(l);
366 1.1.18.2 yamt }
367 1.1.18.2 yamt
368 1.1.18.2 yamt /*
369 1.1.18.2 yamt * the following loop does two things.
370 1.1.18.2 yamt *
371 1.1.18.2 yamt * - remove ts from the list.
372 1.1.18.2 yamt *
373 1.1.18.2 yamt * - from the rest of the list, find the highest priority.
374 1.1.18.2 yamt */
375 1.1.18.2 yamt
376 1.1.18.3 yamt prio = -1;
377 1.1.18.2 yamt KASSERT(!SLIST_EMPTY(&l->l_pi_lenders));
378 1.1.18.2 yamt for (iter = SLIST_FIRST(&l->l_pi_lenders);
379 1.1.18.2 yamt iter != NULL; iter = next) {
380 1.1.18.3 yamt KASSERT(lwp_eprio(l) >= ts->ts_eprio);
381 1.1.18.2 yamt next = SLIST_NEXT(iter, ts_pichain);
382 1.1.18.2 yamt if (iter == ts) {
383 1.1.18.2 yamt if (prev == NULL) {
384 1.1.18.2 yamt SLIST_REMOVE_HEAD(&l->l_pi_lenders,
385 1.1.18.2 yamt ts_pichain);
386 1.1.18.2 yamt } else {
387 1.1.18.2 yamt SLIST_REMOVE_AFTER(prev, ts_pichain);
388 1.1.18.2 yamt }
389 1.1.18.3 yamt } else if (prio < iter->ts_eprio) {
390 1.1.18.2 yamt prio = iter->ts_eprio;
391 1.1.18.2 yamt }
392 1.1.18.2 yamt prev = iter;
393 1.1.18.2 yamt }
394 1.1.18.2 yamt
395 1.1.18.2 yamt lwp_lendpri(l, prio);
396 1.1.18.2 yamt
397 1.1.18.2 yamt if (dolock) {
398 1.1.18.2 yamt lwp_unlock(l);
399 1.1.18.2 yamt }
400 1.1.18.2 yamt }
401 1.1.18.1 yamt
402 1.1.18.1 yamt if (nl != NULL) {
403 1.1.18.1 yamt #if defined(DEBUG) || defined(LOCKDEBUG)
404 1.1.18.1 yamt TAILQ_FOREACH(l, &sq->sq_queue, l_sleepchain) {
405 1.1.18.1 yamt if (l == nl)
406 1.1.18.1 yamt break;
407 1.1.18.1 yamt }
408 1.1.18.1 yamt if (l == NULL)
409 1.1.18.1 yamt panic("turnstile_wakeup: nl not on sleepq");
410 1.1.18.1 yamt #endif
411 1.1.18.2 yamt turnstile_remove(ts, nl, sq);
412 1.1.18.1 yamt } else {
413 1.1.18.1 yamt while (count-- > 0) {
414 1.1.18.1 yamt l = TAILQ_FIRST(&sq->sq_queue);
415 1.1.18.1 yamt KASSERT(l != NULL);
416 1.1.18.2 yamt turnstile_remove(ts, l, sq);
417 1.1.18.1 yamt }
418 1.1.18.1 yamt }
419 1.1.18.2 yamt mutex_spin_exit(&tc->tc_mutex);
420 1.1.18.1 yamt }
421 1.1.18.1 yamt
422 1.1.18.1 yamt /*
423 1.1.18.1 yamt * turnstile_unsleep:
424 1.1.18.1 yamt *
425 1.1.18.1 yamt * Remove an LWP from the turnstile. This is called when the LWP has
426 1.1.18.1 yamt * not been awoken normally but instead interrupted: for example, if it
427 1.1.18.1 yamt * has received a signal. It's not a valid action for turnstiles,
428 1.1.18.1 yamt * since LWPs blocking on a turnstile are not interruptable.
429 1.1.18.1 yamt */
430 1.1.18.1 yamt void
431 1.1.18.2 yamt turnstile_unsleep(lwp_t *l)
432 1.1.18.1 yamt {
433 1.1.18.1 yamt
434 1.1.18.1 yamt lwp_unlock(l);
435 1.1.18.1 yamt panic("turnstile_unsleep");
436 1.1.18.1 yamt }
437 1.1.18.1 yamt
438 1.1.18.1 yamt /*
439 1.1.18.1 yamt * turnstile_changepri:
440 1.1.18.1 yamt *
441 1.1.18.2 yamt * Adjust the priority of an LWP residing on a turnstile.
442 1.1.18.1 yamt */
443 1.1.18.1 yamt void
444 1.1.18.2 yamt turnstile_changepri(lwp_t *l, pri_t pri)
445 1.1.18.1 yamt {
446 1.1.18.1 yamt
447 1.1.18.2 yamt /* XXX priority inheritance */
448 1.1.18.2 yamt sleepq_changepri(l, pri);
449 1.1.18.1 yamt }
450 1.1.18.1 yamt
451 1.1.18.1 yamt #if defined(LOCKDEBUG)
452 1.1.18.1 yamt /*
453 1.1.18.1 yamt * turnstile_print:
454 1.1.18.1 yamt *
455 1.1.18.1 yamt * Given the address of a lock object, print the contents of a
456 1.1.18.1 yamt * turnstile.
457 1.1.18.1 yamt */
458 1.1.18.1 yamt void
459 1.1.18.1 yamt turnstile_print(volatile void *obj, void (*pr)(const char *, ...))
460 1.1.18.1 yamt {
461 1.1.18.1 yamt turnstile_t *ts;
462 1.1.18.1 yamt tschain_t *tc;
463 1.1.18.1 yamt sleepq_t *rsq, *wsq;
464 1.1.18.2 yamt lwp_t *l;
465 1.1.18.1 yamt
466 1.1.18.1 yamt tc = &turnstile_tab[TS_HASH(obj)];
467 1.1.18.1 yamt
468 1.1.18.1 yamt LIST_FOREACH(ts, &tc->tc_chain, ts_chain)
469 1.1.18.1 yamt if (ts->ts_obj == obj)
470 1.1.18.1 yamt break;
471 1.1.18.1 yamt
472 1.1.18.2 yamt (*pr)("Turnstile chain at %p.\n", tc);
473 1.1.18.1 yamt if (ts == NULL) {
474 1.1.18.1 yamt (*pr)("=> No active turnstile for this lock.\n");
475 1.1.18.1 yamt return;
476 1.1.18.1 yamt }
477 1.1.18.1 yamt
478 1.1.18.1 yamt rsq = &ts->ts_sleepq[TS_READER_Q];
479 1.1.18.1 yamt wsq = &ts->ts_sleepq[TS_WRITER_Q];
480 1.1.18.1 yamt
481 1.1.18.1 yamt (*pr)("=> Turnstile at %p (wrq=%p, rdq=%p).\n", ts, rsq, wsq);
482 1.1.18.1 yamt
483 1.1.18.1 yamt (*pr)("=> %d waiting readers:", rsq->sq_waiters);
484 1.1.18.1 yamt TAILQ_FOREACH(l, &rsq->sq_queue, l_sleepchain) {
485 1.1.18.1 yamt (*pr)(" %p", l);
486 1.1.18.1 yamt }
487 1.1.18.1 yamt (*pr)("\n");
488 1.1.18.1 yamt
489 1.1.18.1 yamt (*pr)("=> %d waiting writers:", wsq->sq_waiters);
490 1.1.18.1 yamt TAILQ_FOREACH(l, &wsq->sq_queue, l_sleepchain) {
491 1.1.18.1 yamt (*pr)(" %p", l);
492 1.1.18.1 yamt }
493 1.1.18.1 yamt (*pr)("\n");
494 1.1.18.1 yamt }
495 1.1.18.1 yamt #endif /* LOCKDEBUG */
496