kern_turnstile.c revision 1.1.2.7 1 1.1.2.7 thorpej /* $NetBSD: kern_turnstile.c,v 1.1.2.7 2002/03/16 20:57:42 thorpej Exp $ */
2 1.1.2.1 thorpej
3 1.1.2.1 thorpej /*-
4 1.1.2.1 thorpej * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.1.2.1 thorpej * All rights reserved.
6 1.1.2.1 thorpej *
7 1.1.2.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.1 thorpej * by Jason R. Thorpe.
9 1.1.2.1 thorpej *
10 1.1.2.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1.2.1 thorpej * modification, are permitted provided that the following conditions
12 1.1.2.1 thorpej * are met:
13 1.1.2.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1.2.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1.2.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1.2.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1.2.1 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.1.2.1 thorpej * must display the following acknowledgement:
20 1.1.2.1 thorpej * This product includes software developed by the NetBSD
21 1.1.2.1 thorpej * Foundation, Inc. and its contributors.
22 1.1.2.1 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.2.1 thorpej * contributors may be used to endorse or promote products derived
24 1.1.2.1 thorpej * from this software without specific prior written permission.
25 1.1.2.1 thorpej *
26 1.1.2.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.2.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.2.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.2.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.2.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.2.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.2.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.2.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.2.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.2.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.2.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.1.2.1 thorpej */
38 1.1.2.1 thorpej
39 1.1.2.1 thorpej /*
40 1.1.2.1 thorpej * Turnsiles are specialized sleep queues for use by locks. Turnstiles
41 1.1.2.1 thorpej * are described in detail in:
42 1.1.2.1 thorpej *
43 1.1.2.1 thorpej * Solaris Internals: Core Kernel Architecture, Jim Mauro and
44 1.1.2.1 thorpej * Richard McDougall.
45 1.1.2.1 thorpej *
46 1.1.2.3 thorpej * Turnstiles are kept in a hash table. There are likely to be many more
47 1.1.2.3 thorpej * lock objects than there are threads. Since a thread can block on only
48 1.1.2.3 thorpej * one lock at a time, we only need one turnstile per thread, and so they
49 1.1.2.3 thorpej * are allocated at thread creation time.
50 1.1.2.1 thorpej *
51 1.1.2.1 thorpej * When a thread decides it needs to block on a lock, it looks up the
52 1.1.2.1 thorpej * active turnstile for that lock. If no active turnstile exists, then
53 1.1.2.1 thorpej * the process lends its turnstile to the lock. If there is already
54 1.1.2.1 thorpej * an active turnstile for the lock, the thread places its turnstile on
55 1.1.2.1 thorpej * a list of free turnstiles, and references the active one instead.
56 1.1.2.1 thorpej *
57 1.1.2.1 thorpej * The act of looking up the turnstile acquires an interlock on the sleep
58 1.1.2.1 thorpej * queue. If a thread decides it doesn't need to block after all, then
59 1.1.2.1 thorpej * this interlock must be released by explicitly aborting the turnstile
60 1.1.2.1 thorpej * operation.
61 1.1.2.1 thorpej *
62 1.1.2.1 thorpej * When a thread is awakened, it needs to get its turnstile back. If
63 1.1.2.1 thorpej * there are still other threads waiting in the active turnstile, the
64 1.1.2.1 thorpej * the thread grabs a free turnstile off the free list. Otherwise, it
65 1.1.2.1 thorpej * can take back the active turnstile from the lock (thus deactivating
66 1.1.2.1 thorpej * the turnstile).
67 1.1.2.1 thorpej *
68 1.1.2.1 thorpej * Turnstiles are the place to do priority inheritence. However, we do
69 1.1.2.1 thorpej * not currently implement that.
70 1.1.2.1 thorpej *
71 1.1.2.1 thorpej * We also do not differentiate between the reader and writer queues,
72 1.1.2.1 thorpej * although we currently provide for it in the API so that we can add
73 1.1.2.1 thorpej * support for it later.
74 1.1.2.1 thorpej *
75 1.1.2.1 thorpej * XXX We currently have to interlock with the sched_lock. The locking
76 1.1.2.1 thorpej * order is:
77 1.1.2.1 thorpej *
78 1.1.2.1 thorpej * turnstile chain -> sched_lock
79 1.1.2.1 thorpej */
80 1.1.2.1 thorpej
81 1.1.2.1 thorpej #include <sys/cdefs.h>
82 1.1.2.7 thorpej __KERNEL_RCSID(0, "$NetBSD: kern_turnstile.c,v 1.1.2.7 2002/03/16 20:57:42 thorpej Exp $");
83 1.1.2.1 thorpej
84 1.1.2.1 thorpej #include <sys/param.h>
85 1.1.2.5 thorpej #include <sys/simplelock.h>
86 1.1.2.1 thorpej #include <sys/pool.h>
87 1.1.2.1 thorpej #include <sys/proc.h>
88 1.1.2.1 thorpej #include <sys/resourcevar.h>
89 1.1.2.1 thorpej #include <sys/sched.h>
90 1.1.2.1 thorpej #include <sys/systm.h>
91 1.1.2.1 thorpej
92 1.1.2.1 thorpej /*
93 1.1.2.1 thorpej * Turnstile hash -- shift the lock object to eliminate the zero bits
94 1.1.2.1 thorpej * of the address, and mask it off with the turnstile table's size.
95 1.1.2.1 thorpej */
96 1.1.2.1 thorpej #if LONG_BIT == 64
97 1.1.2.1 thorpej #define TURNSTILE_HASH_SHIFT 3
98 1.1.2.1 thorpej #elif LONG_BIT == 32
99 1.1.2.1 thorpej #define TURNSTILE_HASH_SHIFT 2
100 1.1.2.1 thorpej #else
101 1.1.2.1 thorpej #error "Don't know how big your pointers are."
102 1.1.2.1 thorpej #endif
103 1.1.2.1 thorpej
104 1.1.2.1 thorpej #define TURNSTILE_HASH_SIZE 64 /* XXXJRT tune */
105 1.1.2.1 thorpej #define TURNSTILE_HASH_MASK (TURNSTILE_HASH_SIZE - 1)
106 1.1.2.1 thorpej
107 1.1.2.1 thorpej #define TURNSTILE_HASH(obj) \
108 1.1.2.1 thorpej ((((u_long)(obj)) >> TURNSTILE_HASH_SHIFT) & TURNSTILE_HASH_MASK)
109 1.1.2.1 thorpej
110 1.1.2.1 thorpej struct turnstile_chain {
111 1.1.2.1 thorpej __cpu_simple_lock_t tc_lock; /* lock on hash chain */
112 1.1.2.3 thorpej int tc_oldspl; /* saved spl of lock holder
113 1.1.2.3 thorpej (only valid while tc_lock held) */
114 1.1.2.1 thorpej LIST_HEAD(, turnstile) tc_chain;/* turnstile chain */
115 1.1.2.1 thorpej } turnstile_table[TURNSTILE_HASH_SIZE];
116 1.1.2.1 thorpej
117 1.1.2.1 thorpej #define TURNSTILE_CHAIN(obj) \
118 1.1.2.1 thorpej &turnstile_table[TURNSTILE_HASH(obj)]
119 1.1.2.1 thorpej
120 1.1.2.1 thorpej #define TURNSTILE_CHAIN_LOCK(tc) \
121 1.1.2.1 thorpej do { \
122 1.1.2.1 thorpej int _s_ = splsched(); \
123 1.1.2.1 thorpej __cpu_simple_lock(&(tc)->tc_lock); \
124 1.1.2.1 thorpej (tc)->tc_oldspl = _s_; \
125 1.1.2.1 thorpej } while (/*CONSTCOND*/0)
126 1.1.2.1 thorpej
127 1.1.2.1 thorpej #define TURNSTILE_CHAIN_UNLOCK(tc) \
128 1.1.2.1 thorpej do { \
129 1.1.2.2 thorpej int _s_ = (tc)->tc_oldspl; \
130 1.1.2.1 thorpej __cpu_simple_unlock(&(tc)->tc_lock); \
131 1.1.2.2 thorpej splx(_s_); \
132 1.1.2.1 thorpej } while (/*CONSTCOND*/0)
133 1.1.2.1 thorpej
134 1.1.2.1 thorpej static const char turnstile_wmesg[] = "tstile";
135 1.1.2.1 thorpej
136 1.1.2.1 thorpej struct pool turnstile_pool;
137 1.1.2.3 thorpej struct pool_cache turnstile_cache;
138 1.1.2.3 thorpej
139 1.1.2.3 thorpej int turnstile_ctor(void *, void *, int);
140 1.1.2.1 thorpej
141 1.1.2.1 thorpej /*
142 1.1.2.1 thorpej * turnstile_init:
143 1.1.2.1 thorpej *
144 1.1.2.1 thorpej * Initialize the turnstile mechanism.
145 1.1.2.1 thorpej */
146 1.1.2.1 thorpej void
147 1.1.2.1 thorpej turnstile_init(void)
148 1.1.2.1 thorpej {
149 1.1.2.1 thorpej struct turnstile_chain *tc;
150 1.1.2.1 thorpej int i;
151 1.1.2.1 thorpej
152 1.1.2.1 thorpej for (i = 0; i < TURNSTILE_HASH_SIZE; i++) {
153 1.1.2.1 thorpej tc = &turnstile_table[i];
154 1.1.2.1 thorpej __cpu_simple_lock_init(&tc->tc_lock);
155 1.1.2.1 thorpej LIST_INIT(&tc->tc_chain);
156 1.1.2.1 thorpej }
157 1.1.2.1 thorpej
158 1.1.2.1 thorpej pool_init(&turnstile_pool, sizeof(struct turnstile), 0, 0, 0,
159 1.1.2.1 thorpej "tspool", &pool_allocator_nointr);
160 1.1.2.3 thorpej pool_cache_init(&turnstile_cache, &turnstile_pool,
161 1.1.2.3 thorpej turnstile_ctor, NULL, NULL);
162 1.1.2.3 thorpej }
163 1.1.2.3 thorpej
164 1.1.2.3 thorpej /*
165 1.1.2.3 thorpej * turnstile_ctor:
166 1.1.2.3 thorpej *
167 1.1.2.3 thorpej * Constructor for turnstiles.
168 1.1.2.3 thorpej */
169 1.1.2.3 thorpej int
170 1.1.2.3 thorpej turnstile_ctor(void *arg, void *obj, int flags)
171 1.1.2.3 thorpej {
172 1.1.2.3 thorpej struct turnstile *ts = obj;
173 1.1.2.3 thorpej
174 1.1.2.3 thorpej memset(ts, 0, sizeof(*ts));
175 1.1.2.3 thorpej return (0);
176 1.1.2.1 thorpej }
177 1.1.2.1 thorpej
178 1.1.2.1 thorpej static void
179 1.1.2.6 thorpej turnstile_remque(struct turnstile *ts, struct proc *p,
180 1.1.2.6 thorpej struct turnstile_sleepq *tsq)
181 1.1.2.1 thorpej {
182 1.1.2.6 thorpej struct proc **q = &tsq->tsq_q.sq_head;
183 1.1.2.1 thorpej struct turnstile *nts;
184 1.1.2.1 thorpej
185 1.1.2.1 thorpej KASSERT(p->p_ts == ts);
186 1.1.2.1 thorpej
187 1.1.2.1 thorpej /*
188 1.1.2.1 thorpej * This process is no longer using the active turnstile.
189 1.1.2.1 thorpej * Find an inactive one on the free list to give to it.
190 1.1.2.1 thorpej */
191 1.1.2.3 thorpej if ((nts = ts->ts_free) != NULL) {
192 1.1.2.6 thorpej KASSERT(TS_WAITERS(ts) > 1);
193 1.1.2.1 thorpej p->p_ts = nts;
194 1.1.2.1 thorpej ts->ts_free = nts->ts_free;
195 1.1.2.1 thorpej nts->ts_free = NULL;
196 1.1.2.1 thorpej } else {
197 1.1.2.1 thorpej /*
198 1.1.2.1 thorpej * If the free list is empty, this is the last
199 1.1.2.1 thorpej * waiter.
200 1.1.2.1 thorpej */
201 1.1.2.6 thorpej KASSERT(TS_WAITERS(ts) == 1);
202 1.1.2.1 thorpej LIST_REMOVE(ts, ts_chain);
203 1.1.2.1 thorpej }
204 1.1.2.1 thorpej
205 1.1.2.6 thorpej tsq->tsq_waiters--;
206 1.1.2.1 thorpej
207 1.1.2.1 thorpej *q = p->p_forw;
208 1.1.2.6 thorpej if (tsq->tsq_q.sq_tailp == &p->p_forw)
209 1.1.2.6 thorpej tsq->tsq_q.sq_tailp = q;
210 1.1.2.3 thorpej
211 1.1.2.6 thorpej KASSERT(ts->ts_sleepq[TS_READER_Q].tsq_waiters != 0 ||
212 1.1.2.6 thorpej ts->ts_sleepq[TS_READER_Q].tsq_q.sq_head == NULL);
213 1.1.2.6 thorpej KASSERT(ts->ts_sleepq[TS_WRITER_Q].tsq_waiters != 0 ||
214 1.1.2.6 thorpej ts->ts_sleepq[TS_WRITER_Q].tsq_q.sq_head == NULL);
215 1.1.2.6 thorpej
216 1.1.2.6 thorpej KASSERT(ts->ts_sleepq[TS_READER_Q].tsq_waiters == 0 ||
217 1.1.2.6 thorpej ts->ts_sleepq[TS_READER_Q].tsq_q.sq_head != NULL);
218 1.1.2.6 thorpej KASSERT(ts->ts_sleepq[TS_WRITER_Q].tsq_waiters == 0 ||
219 1.1.2.6 thorpej ts->ts_sleepq[TS_WRITER_Q].tsq_q.sq_head != NULL);
220 1.1.2.1 thorpej }
221 1.1.2.1 thorpej
222 1.1.2.1 thorpej /*
223 1.1.2.1 thorpej * turnstile_lookup:
224 1.1.2.1 thorpej *
225 1.1.2.1 thorpej * Look up the turnstile for the specified lock object. This
226 1.1.2.1 thorpej * acquires and holds the turnstile chain lock (sleep queue
227 1.1.2.1 thorpej * interlock).
228 1.1.2.1 thorpej */
229 1.1.2.1 thorpej struct turnstile *
230 1.1.2.1 thorpej turnstile_lookup(void *lp)
231 1.1.2.1 thorpej {
232 1.1.2.1 thorpej struct turnstile_chain *tc = TURNSTILE_CHAIN(lp);
233 1.1.2.1 thorpej struct turnstile *ts;
234 1.1.2.1 thorpej
235 1.1.2.1 thorpej TURNSTILE_CHAIN_LOCK(tc);
236 1.1.2.1 thorpej
237 1.1.2.1 thorpej LIST_FOREACH(ts, &tc->tc_chain, ts_chain)
238 1.1.2.1 thorpej if (ts->ts_obj == lp)
239 1.1.2.1 thorpej return (ts);
240 1.1.2.1 thorpej
241 1.1.2.1 thorpej /*
242 1.1.2.1 thorpej * No turnstile yet for this lock. No problem, turnstile_block()
243 1.1.2.1 thorpej * handle this by fetching the turnstile from the blocking thread.
244 1.1.2.1 thorpej */
245 1.1.2.1 thorpej return (NULL);
246 1.1.2.1 thorpej }
247 1.1.2.1 thorpej
248 1.1.2.1 thorpej /*
249 1.1.2.1 thorpej * turnstile_exit:
250 1.1.2.1 thorpej *
251 1.1.2.1 thorpej * Abort a turnstile operation.
252 1.1.2.1 thorpej */
253 1.1.2.1 thorpej void
254 1.1.2.1 thorpej turnstile_exit(void *lp)
255 1.1.2.1 thorpej {
256 1.1.2.1 thorpej struct turnstile_chain *tc = TURNSTILE_CHAIN(lp);
257 1.1.2.1 thorpej
258 1.1.2.1 thorpej TURNSTILE_CHAIN_UNLOCK(tc);
259 1.1.2.1 thorpej }
260 1.1.2.1 thorpej
261 1.1.2.1 thorpej /*
262 1.1.2.1 thorpej * turnstile_block:
263 1.1.2.1 thorpej *
264 1.1.2.1 thorpej * Block a thread on a lock object.
265 1.1.2.1 thorpej */
266 1.1.2.1 thorpej int
267 1.1.2.4 thorpej turnstile_block(struct turnstile *ts, int rw, int pri, void *lp)
268 1.1.2.1 thorpej {
269 1.1.2.1 thorpej struct turnstile_chain *tc = TURNSTILE_CHAIN(lp);
270 1.1.2.1 thorpej struct proc *p = curproc;
271 1.1.2.1 thorpej struct turnstile *ots;
272 1.1.2.6 thorpej struct turnstile_sleepq *tsq;
273 1.1.2.1 thorpej struct slpque *qp;
274 1.1.2.1 thorpej int s;
275 1.1.2.1 thorpej
276 1.1.2.1 thorpej KASSERT(p->p_ts != NULL);
277 1.1.2.6 thorpej KASSERT(rw == TS_READER_Q || rw == TS_WRITER_Q);
278 1.1.2.1 thorpej
279 1.1.2.1 thorpej if (ts == NULL) {
280 1.1.2.1 thorpej /*
281 1.1.2.1 thorpej * We are the first thread to wait for this lock;
282 1.1.2.1 thorpej * lend our turnstile to it.
283 1.1.2.1 thorpej */
284 1.1.2.1 thorpej ts = p->p_ts;
285 1.1.2.6 thorpej KASSERT(TS_WAITERS(ts) == 0);
286 1.1.2.6 thorpej KASSERT(ts->ts_sleepq[TS_READER_Q].tsq_q.sq_head == NULL &&
287 1.1.2.6 thorpej ts->ts_sleepq[TS_WRITER_Q].tsq_q.sq_head == NULL);
288 1.1.2.1 thorpej ts->ts_obj = lp;
289 1.1.2.1 thorpej LIST_INSERT_HEAD(&tc->tc_chain, ts, ts_chain);
290 1.1.2.1 thorpej } else {
291 1.1.2.1 thorpej /*
292 1.1.2.1 thorpej * Lock already has a turnstile. Put our turnstile
293 1.1.2.1 thorpej * onto the free list, and reference the existing
294 1.1.2.1 thorpej * turnstile instead.
295 1.1.2.1 thorpej */
296 1.1.2.1 thorpej ots = p->p_ts;
297 1.1.2.1 thorpej ots->ts_free = ts->ts_free;
298 1.1.2.1 thorpej ts->ts_free = ots;
299 1.1.2.1 thorpej p->p_ts = ts;
300 1.1.2.1 thorpej }
301 1.1.2.1 thorpej
302 1.1.2.1 thorpej #ifdef DIAGNOSTIC
303 1.1.2.1 thorpej if (p->p_stat != SONPROC)
304 1.1.2.1 thorpej panic("turnstile_block: p_stat %d != SONPROC", p->p_stat);
305 1.1.2.1 thorpej if (p->p_back != NULL)
306 1.1.2.1 thorpej panic("turnstile_block: p_back != NULL");
307 1.1.2.1 thorpej #endif
308 1.1.2.1 thorpej
309 1.1.2.1 thorpej #ifdef KTRACE
310 1.1.2.1 thorpej if (KTRPOINT(p, KTR_CSW))
311 1.1.2.1 thorpej ktrcsw(p, 1, 0);
312 1.1.2.1 thorpej #endif
313 1.1.2.1 thorpej
314 1.1.2.1 thorpej /* XXXJRT PCATCH? */
315 1.1.2.1 thorpej
316 1.1.2.1 thorpej p->p_wchan = lp;
317 1.1.2.1 thorpej p->p_wmesg = turnstile_wmesg;
318 1.1.2.1 thorpej p->p_slptime = 0;
319 1.1.2.4 thorpej p->p_priority = pri & PRIMASK;
320 1.1.2.1 thorpej
321 1.1.2.6 thorpej tsq = &ts->ts_sleepq[rw];
322 1.1.2.6 thorpej qp = &tsq->tsq_q;
323 1.1.2.6 thorpej
324 1.1.2.6 thorpej tsq->tsq_waiters++;
325 1.1.2.1 thorpej
326 1.1.2.1 thorpej if (qp->sq_head == NULL)
327 1.1.2.1 thorpej qp->sq_head = p;
328 1.1.2.1 thorpej else
329 1.1.2.1 thorpej *qp->sq_tailp = p;
330 1.1.2.1 thorpej *(qp->sq_tailp = &p->p_forw) = NULL;
331 1.1.2.1 thorpej
332 1.1.2.1 thorpej p->p_stat = SSLEEP;
333 1.1.2.1 thorpej p->p_stats->p_ru.ru_nvcsw++;
334 1.1.2.1 thorpej
335 1.1.2.1 thorpej /*
336 1.1.2.1 thorpej * XXX We currently need to interlock with sched_lock.
337 1.1.2.1 thorpej * Note we're already at splsched().
338 1.1.2.1 thorpej */
339 1.1.2.1 thorpej _SCHED_LOCK;
340 1.1.2.1 thorpej
341 1.1.2.1 thorpej /*
342 1.1.2.1 thorpej * We can now release the turnstile chain interlock; the
343 1.1.2.1 thorpej * scheduler lock is held, so a thread can't get in to
344 1.1.2.1 thorpej * do a turnstile_wakeup() before we do the switch.
345 1.1.2.1 thorpej *
346 1.1.2.1 thorpej * Note: we need to remember our old spl which is currently
347 1.1.2.1 thorpej * stored in the turnstile chain, because we have to stay
348 1.1.2.1 thorpej * st splsched while the sched_lock is held.
349 1.1.2.1 thorpej */
350 1.1.2.1 thorpej s = tc->tc_oldspl;
351 1.1.2.1 thorpej __cpu_simple_unlock(&tc->tc_lock);
352 1.1.2.1 thorpej
353 1.1.2.1 thorpej mi_switch(p);
354 1.1.2.1 thorpej
355 1.1.2.1 thorpej SCHED_ASSERT_UNLOCKED();
356 1.1.2.1 thorpej splx(s);
357 1.1.2.1 thorpej
358 1.1.2.1 thorpej /*
359 1.1.2.1 thorpej * We are now back to the base spl level we were at when the
360 1.1.2.1 thorpej * caller called turnstile_lookup().
361 1.1.2.1 thorpej */
362 1.1.2.1 thorpej
363 1.1.2.1 thorpej KDASSERT(p->p_cpu != NULL);
364 1.1.2.1 thorpej KDASSERT(p->p_cpu == curcpu());
365 1.1.2.1 thorpej p->p_cpu->ci_schedstate.spc_curpriority = p->p_usrpri;
366 1.1.2.1 thorpej
367 1.1.2.1 thorpej KDASSERT((p->p_flag & (P_SINTR|P_TIMEOUT)) == 0);
368 1.1.2.1 thorpej
369 1.1.2.1 thorpej #ifdef KTRACE
370 1.1.2.1 thorpej if (KTRPOINT(p, KTR_CSW))
371 1.1.2.1 thorpej ktrcsw(p, 0, 0);
372 1.1.2.1 thorpej #endif
373 1.1.2.1 thorpej
374 1.1.2.1 thorpej return (0);
375 1.1.2.1 thorpej }
376 1.1.2.1 thorpej
377 1.1.2.1 thorpej /*
378 1.1.2.1 thorpej * turnstile_wakeup:
379 1.1.2.1 thorpej *
380 1.1.2.1 thorpej * Wake up the specified number of threads that are blocked
381 1.1.2.1 thorpej * in a turnstile.
382 1.1.2.1 thorpej */
383 1.1.2.1 thorpej void
384 1.1.2.7 thorpej turnstile_wakeup(struct turnstile *ts, int rw, int count,
385 1.1.2.7 thorpej struct proc *nextproc)
386 1.1.2.1 thorpej {
387 1.1.2.1 thorpej struct turnstile_chain *tc = TURNSTILE_CHAIN(ts->ts_obj);
388 1.1.2.6 thorpej struct turnstile_sleepq *tsq;
389 1.1.2.1 thorpej struct proc *p;
390 1.1.2.1 thorpej
391 1.1.2.6 thorpej KASSERT(rw == TS_READER_Q || rw == TS_WRITER_Q);
392 1.1.2.6 thorpej
393 1.1.2.6 thorpej tsq = &ts->ts_sleepq[rw];
394 1.1.2.6 thorpej
395 1.1.2.7 thorpej /*
396 1.1.2.7 thorpej * If nextproc != NULL, then we're being asked to do direct
397 1.1.2.7 thorpej * handoff to a pre-select waiter. Therefore, the count must
398 1.1.2.7 thorpej * be 1.
399 1.1.2.7 thorpej */
400 1.1.2.7 thorpej KASSERT(nextproc == NULL || count == 1);
401 1.1.2.7 thorpej
402 1.1.2.1 thorpej /* XXX We currently interlock with sched_lock. */
403 1.1.2.1 thorpej _SCHED_LOCK;
404 1.1.2.1 thorpej
405 1.1.2.7 thorpej if (nextproc != NULL) {
406 1.1.2.7 thorpej #ifdef DEBUG
407 1.1.2.7 thorpej for (p = tsq->tsq_q.sq_head; p != NULL;
408 1.1.2.7 thorpej p = p->p_forw) {
409 1.1.2.7 thorpej if (p == nextproc)
410 1.1.2.7 thorpej break;
411 1.1.2.7 thorpej }
412 1.1.2.7 thorpej if (p == NULL)
413 1.1.2.7 thorpej panic("turnstile_wakeup: nextproc not on sleepq");
414 1.1.2.7 thorpej #endif
415 1.1.2.7 thorpej turnstile_remque(ts, nextproc, tsq);
416 1.1.2.7 thorpej
417 1.1.2.7 thorpej nextproc->p_wchan = NULL;
418 1.1.2.7 thorpej
419 1.1.2.7 thorpej if (nextproc->p_stat == SSLEEP)
420 1.1.2.7 thorpej awaken(nextproc);
421 1.1.2.7 thorpej } else {
422 1.1.2.7 thorpej while (count-- > 0) {
423 1.1.2.7 thorpej p = tsq->tsq_q.sq_head;
424 1.1.2.1 thorpej
425 1.1.2.7 thorpej KASSERT(p != NULL);
426 1.1.2.1 thorpej
427 1.1.2.7 thorpej turnstile_remque(ts, p, tsq);
428 1.1.2.1 thorpej
429 1.1.2.7 thorpej p->p_wchan = NULL;
430 1.1.2.1 thorpej
431 1.1.2.7 thorpej if (p->p_stat == SSLEEP)
432 1.1.2.7 thorpej awaken(p);
433 1.1.2.7 thorpej }
434 1.1.2.1 thorpej }
435 1.1.2.1 thorpej
436 1.1.2.1 thorpej _SCHED_UNLOCK;
437 1.1.2.1 thorpej
438 1.1.2.1 thorpej TURNSTILE_CHAIN_UNLOCK(tc);
439 1.1.2.1 thorpej }
440