kern_synch.c revision 1.153 1 /* $NetBSD: kern_synch.c,v 1.153 2005/11/01 09:07:53 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Charles M. Hannum.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
40 */
41
42 /*-
43 * Copyright (c) 1982, 1986, 1990, 1991, 1993
44 * The Regents of the University of California. All rights reserved.
45 * (c) UNIX System Laboratories, Inc.
46 * All or some portions of this file are derived from material licensed
47 * to the University of California by American Telephone and Telegraph
48 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
49 * the permission of UNIX System Laboratories, Inc.
50 *
51 * Redistribution and use in source and binary forms, with or without
52 * modification, are permitted provided that the following conditions
53 * are met:
54 * 1. Redistributions of source code must retain the above copyright
55 * notice, this list of conditions and the following disclaimer.
56 * 2. Redistributions in binary form must reproduce the above copyright
57 * notice, this list of conditions and the following disclaimer in the
58 * documentation and/or other materials provided with the distribution.
59 * 3. Neither the name of the University nor the names of its contributors
60 * may be used to endorse or promote products derived from this software
61 * without specific prior written permission.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * SUCH DAMAGE.
74 *
75 * @(#)kern_synch.c 8.9 (Berkeley) 5/19/95
76 */
77
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: kern_synch.c,v 1.153 2005/11/01 09:07:53 yamt Exp $");
80
81 #include "opt_ddb.h"
82 #include "opt_ktrace.h"
83 #include "opt_kstack.h"
84 #include "opt_lockdebug.h"
85 #include "opt_multiprocessor.h"
86 #include "opt_perfctrs.h"
87
88 #include <sys/param.h>
89 #include <sys/systm.h>
90 #include <sys/callout.h>
91 #include <sys/proc.h>
92 #include <sys/kernel.h>
93 #include <sys/buf.h>
94 #if defined(PERFCTRS)
95 #include <sys/pmc.h>
96 #endif
97 #include <sys/signalvar.h>
98 #include <sys/resourcevar.h>
99 #include <sys/sched.h>
100 #include <sys/sa.h>
101 #include <sys/savar.h>
102
103 #include <uvm/uvm_extern.h>
104
105 #ifdef KTRACE
106 #include <sys/ktrace.h>
107 #endif
108
109 #include <machine/cpu.h>
110
111 int lbolt; /* once a second sleep address */
112 int rrticks; /* number of hardclock ticks per roundrobin() */
113
114 /*
115 * Sleep queues.
116 *
117 * We're only looking at 7 bits of the address; everything is
118 * aligned to 4, lots of things are aligned to greater powers
119 * of 2. Shift right by 8, i.e. drop the bottom 256 worth.
120 */
121 #define SLPQUE_TABLESIZE 128
122 #define SLPQUE_LOOKUP(x) (((u_long)(x) >> 8) & (SLPQUE_TABLESIZE - 1))
123
124 #define SLPQUE(ident) (&sched_slpque[SLPQUE_LOOKUP(ident)])
125
126 /*
127 * The global scheduler state.
128 */
129 struct prochd sched_qs[RUNQUE_NQS]; /* run queues */
130 __volatile u_int32_t sched_whichqs; /* bitmap of non-empty queues */
131 struct slpque sched_slpque[SLPQUE_TABLESIZE]; /* sleep queues */
132
133 struct simplelock sched_lock = SIMPLELOCK_INITIALIZER;
134
135 void schedcpu(void *);
136 void updatepri(struct lwp *);
137 void endtsleep(void *);
138
139 __inline void sa_awaken(struct lwp *);
140 __inline void awaken(struct lwp *);
141
142 struct callout schedcpu_ch = CALLOUT_INITIALIZER_SETFUNC(schedcpu, NULL);
143
144
145
146 /*
147 * Force switch among equal priority processes every 100ms.
148 * Called from hardclock every hz/10 == rrticks hardclock ticks.
149 */
150 /* ARGSUSED */
151 void
152 roundrobin(struct cpu_info *ci)
153 {
154 struct schedstate_percpu *spc = &ci->ci_schedstate;
155
156 spc->spc_rrticks = rrticks;
157
158 if (curlwp != NULL) {
159 if (spc->spc_flags & SPCF_SEENRR) {
160 /*
161 * The process has already been through a roundrobin
162 * without switching and may be hogging the CPU.
163 * Indicate that the process should yield.
164 */
165 spc->spc_flags |= SPCF_SHOULDYIELD;
166 } else
167 spc->spc_flags |= SPCF_SEENRR;
168 }
169 need_resched(curcpu());
170 }
171
172 #define PPQ (128 / RUNQUE_NQS) /* priorities per queue */
173 #define NICE_WEIGHT 2 /* priorities per nice level */
174
175 #define ESTCPU_SHIFT 11
176 #define ESTCPU_MAX ((NICE_WEIGHT * PRIO_MAX - PPQ) << ESTCPU_SHIFT)
177 #define ESTCPULIM(e) min((e), ESTCPU_MAX)
178
179 /*
180 * Constants for digital decay and forget:
181 * 90% of (p_estcpu) usage in 5 * loadav time
182 * 95% of (p_pctcpu) usage in 60 seconds (load insensitive)
183 * Note that, as ps(1) mentions, this can let percentages
184 * total over 100% (I've seen 137.9% for 3 processes).
185 *
186 * Note that hardclock updates p_estcpu and p_cpticks independently.
187 *
188 * We wish to decay away 90% of p_estcpu in (5 * loadavg) seconds.
189 * That is, the system wants to compute a value of decay such
190 * that the following for loop:
191 * for (i = 0; i < (5 * loadavg); i++)
192 * p_estcpu *= decay;
193 * will compute
194 * p_estcpu *= 0.1;
195 * for all values of loadavg:
196 *
197 * Mathematically this loop can be expressed by saying:
198 * decay ** (5 * loadavg) ~= .1
199 *
200 * The system computes decay as:
201 * decay = (2 * loadavg) / (2 * loadavg + 1)
202 *
203 * We wish to prove that the system's computation of decay
204 * will always fulfill the equation:
205 * decay ** (5 * loadavg) ~= .1
206 *
207 * If we compute b as:
208 * b = 2 * loadavg
209 * then
210 * decay = b / (b + 1)
211 *
212 * We now need to prove two things:
213 * 1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1)
214 * 2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg)
215 *
216 * Facts:
217 * For x close to zero, exp(x) =~ 1 + x, since
218 * exp(x) = 0! + x**1/1! + x**2/2! + ... .
219 * therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b.
220 * For x close to zero, ln(1+x) =~ x, since
221 * ln(1+x) = x - x**2/2 + x**3/3 - ... -1 < x < 1
222 * therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1).
223 * ln(.1) =~ -2.30
224 *
225 * Proof of (1):
226 * Solve (factor)**(power) =~ .1 given power (5*loadav):
227 * solving for factor,
228 * ln(factor) =~ (-2.30/5*loadav), or
229 * factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) =
230 * exp(-1/b) =~ (b-1)/b =~ b/(b+1). QED
231 *
232 * Proof of (2):
233 * Solve (factor)**(power) =~ .1 given factor == (b/(b+1)):
234 * solving for power,
235 * power*ln(b/(b+1)) =~ -2.30, or
236 * power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav. QED
237 *
238 * Actual power values for the implemented algorithm are as follows:
239 * loadav: 1 2 3 4
240 * power: 5.68 10.32 14.94 19.55
241 */
242
243 /* calculations for digital decay to forget 90% of usage in 5*loadav sec */
244 #define loadfactor(loadav) (2 * (loadav))
245
246 static fixpt_t
247 decay_cpu(fixpt_t loadfac, fixpt_t estcpu)
248 {
249
250 if (estcpu == 0) {
251 return 0;
252 }
253
254 #if !defined(_LP64)
255 /* avoid 64bit arithmetics. */
256 #define FIXPT_MAX ((fixpt_t)((UINTMAX_C(1) << sizeof(fixpt_t) * CHAR_BIT) - 1))
257 if (__predict_true(loadfac <= FIXPT_MAX / ESTCPU_MAX)) {
258 return estcpu * loadfac / (loadfac + FSCALE);
259 }
260 #endif /* !defined(_LP64) */
261
262 return (uint64_t)estcpu * loadfac / (loadfac + FSCALE);
263 }
264
265 /* decay 95% of `p_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */
266 fixpt_t ccpu = 0.95122942450071400909 * FSCALE; /* exp(-1/20) */
267
268 /*
269 * If `ccpu' is not equal to `exp(-1/20)' and you still want to use the
270 * faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below
271 * and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT).
272 *
273 * To estimate CCPU_SHIFT for exp(-1/20), the following formula was used:
274 * 1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits).
275 *
276 * If you dont want to bother with the faster/more-accurate formula, you
277 * can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate
278 * (more general) method of calculating the %age of CPU used by a process.
279 */
280 #define CCPU_SHIFT 11
281
282 /*
283 * Recompute process priorities, every hz ticks.
284 */
285 /* ARGSUSED */
286 void
287 schedcpu(void *arg)
288 {
289 fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
290 struct lwp *l;
291 struct proc *p;
292 int s, minslp;
293 int clkhz;
294
295 proclist_lock_read();
296 PROCLIST_FOREACH(p, &allproc) {
297 /*
298 * Increment time in/out of memory and sleep time
299 * (if sleeping). We ignore overflow; with 16-bit int's
300 * (remember them?) overflow takes 45 days.
301 */
302 minslp = 2;
303 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
304 l->l_swtime++;
305 if (l->l_stat == LSSLEEP || l->l_stat == LSSTOP ||
306 l->l_stat == LSSUSPENDED) {
307 l->l_slptime++;
308 minslp = min(minslp, l->l_slptime);
309 } else
310 minslp = 0;
311 }
312 p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT;
313 /*
314 * If the process has slept the entire second,
315 * stop recalculating its priority until it wakes up.
316 */
317 if (minslp > 1)
318 continue;
319 s = splstatclock(); /* prevent state changes */
320 /*
321 * p_pctcpu is only for ps.
322 */
323 clkhz = stathz != 0 ? stathz : hz;
324 #if (FSHIFT >= CCPU_SHIFT)
325 p->p_pctcpu += (clkhz == 100)?
326 ((fixpt_t) p->p_cpticks) << (FSHIFT - CCPU_SHIFT):
327 100 * (((fixpt_t) p->p_cpticks)
328 << (FSHIFT - CCPU_SHIFT)) / clkhz;
329 #else
330 p->p_pctcpu += ((FSCALE - ccpu) *
331 (p->p_cpticks * FSCALE / clkhz)) >> FSHIFT;
332 #endif
333 p->p_cpticks = 0;
334 p->p_estcpu = decay_cpu(loadfac, p->p_estcpu);
335 splx(s); /* Done with the process CPU ticks update */
336 SCHED_LOCK(s);
337 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
338 if (l->l_slptime > 1)
339 continue;
340 resetpriority(l);
341 if (l->l_priority >= PUSER) {
342 if (l->l_stat == LSRUN &&
343 (l->l_flag & L_INMEM) &&
344 (l->l_priority / PPQ) != (l->l_usrpri / PPQ)) {
345 remrunqueue(l);
346 l->l_priority = l->l_usrpri;
347 setrunqueue(l);
348 } else
349 l->l_priority = l->l_usrpri;
350 }
351 }
352 SCHED_UNLOCK(s);
353 }
354 proclist_unlock_read();
355 uvm_meter();
356 wakeup((caddr_t)&lbolt);
357 callout_schedule(&schedcpu_ch, hz);
358 }
359
360 /*
361 * Recalculate the priority of a process after it has slept for a while.
362 * For all load averages >= 1 and max p_estcpu of 255, sleeping for at
363 * least six times the loadfactor will decay p_estcpu to less than
364 * (1 << ESTCPU_SHIFT).
365 */
366 void
367 updatepri(struct lwp *l)
368 {
369 struct proc *p = l->l_proc;
370 fixpt_t newcpu;
371 fixpt_t loadfac;
372
373 SCHED_ASSERT_LOCKED();
374
375 newcpu = p->p_estcpu;
376 loadfac = loadfactor(averunnable.ldavg[0]);
377
378 if (l->l_slptime > 5 * loadfac)
379 p->p_estcpu = 0; /* XXX NJWLWP */
380 else {
381 l->l_slptime--; /* the first time was done in schedcpu */
382 while (newcpu && --l->l_slptime)
383 newcpu = decay_cpu(loadfac, newcpu);
384 p->p_estcpu = newcpu;
385 }
386 resetpriority(l);
387 }
388
389 /*
390 * During autoconfiguration or after a panic, a sleep will simply
391 * lower the priority briefly to allow interrupts, then return.
392 * The priority to be used (safepri) is machine-dependent, thus this
393 * value is initialized and maintained in the machine-dependent layers.
394 * This priority will typically be 0, or the lowest priority
395 * that is safe for use on the interrupt stack; it can be made
396 * higher to block network software interrupts after panics.
397 */
398 int safepri;
399
400 /*
401 * General sleep call. Suspends the current process until a wakeup is
402 * performed on the specified identifier. The process will then be made
403 * runnable with the specified priority. Sleeps at most timo/hz seconds
404 * (0 means no timeout). If pri includes PCATCH flag, signals are checked
405 * before and after sleeping, else signals are not checked. Returns 0 if
406 * awakened, EWOULDBLOCK if the timeout expires. If PCATCH is set and a
407 * signal needs to be delivered, ERESTART is returned if the current system
408 * call should be restarted if possible, and EINTR is returned if the system
409 * call should be interrupted by the signal (return EINTR).
410 *
411 * The interlock is held until the scheduler_slock is acquired. The
412 * interlock will be locked before returning back to the caller
413 * unless the PNORELOCK flag is specified, in which case the
414 * interlock will always be unlocked upon return.
415 */
416 int
417 ltsleep(__volatile const void *ident, int priority, const char *wmesg, int timo,
418 __volatile struct simplelock *interlock)
419 {
420 struct lwp *l = curlwp;
421 struct proc *p = l ? l->l_proc : NULL;
422 struct slpque *qp;
423 struct sadata_upcall *sau;
424 int sig, s;
425 int catch = priority & PCATCH;
426 int relock = (priority & PNORELOCK) == 0;
427 int exiterr = (priority & PNOEXITERR) == 0;
428
429 /*
430 * XXXSMP
431 * This is probably bogus. Figure out what the right
432 * thing to do here really is.
433 * Note that not sleeping if ltsleep is called with curlwp == NULL
434 * in the shutdown case is disgusting but partly necessary given
435 * how shutdown (barely) works.
436 */
437 if (cold || (doing_shutdown && (panicstr || (l == NULL)))) {
438 /*
439 * After a panic, or during autoconfiguration,
440 * just give interrupts a chance, then just return;
441 * don't run any other procs or panic below,
442 * in case this is the idle process and already asleep.
443 */
444 s = splhigh();
445 splx(safepri);
446 splx(s);
447 if (interlock != NULL && relock == 0)
448 simple_unlock(interlock);
449 return (0);
450 }
451
452 KASSERT(p != NULL);
453 LOCK_ASSERT(interlock == NULL || simple_lock_held(interlock));
454
455 #ifdef KTRACE
456 if (KTRPOINT(p, KTR_CSW))
457 ktrcsw(p, 1, 0);
458 #endif
459
460 /*
461 * XXX We need to allocate the sadata_upcall structure here,
462 * XXX since we can't sleep while waiting for memory inside
463 * XXX sa_upcall(). It would be nice if we could safely
464 * XXX allocate the sadata_upcall structure on the stack, here.
465 */
466 if (l->l_flag & L_SA) {
467 sau = sadata_upcall_alloc(0);
468 } else {
469 sau = NULL;
470 }
471
472 SCHED_LOCK(s);
473
474 #ifdef DIAGNOSTIC
475 if (ident == NULL)
476 panic("ltsleep: ident == NULL");
477 if (l->l_stat != LSONPROC)
478 panic("ltsleep: l_stat %d != LSONPROC", l->l_stat);
479 if (l->l_back != NULL)
480 panic("ltsleep: p_back != NULL");
481 #endif
482
483 l->l_wchan = ident;
484 l->l_wmesg = wmesg;
485 l->l_slptime = 0;
486 l->l_priority = priority & PRIMASK;
487
488 qp = SLPQUE(ident);
489 if (qp->sq_head == 0)
490 qp->sq_head = l;
491 else {
492 *qp->sq_tailp = l;
493 }
494 *(qp->sq_tailp = &l->l_forw) = 0;
495
496 if (timo)
497 callout_reset(&l->l_tsleep_ch, timo, endtsleep, l);
498
499 /*
500 * We can now release the interlock; the scheduler_slock
501 * is held, so a thread can't get in to do wakeup() before
502 * we do the switch.
503 *
504 * XXX We leave the code block here, after inserting ourselves
505 * on the sleep queue, because we might want a more clever
506 * data structure for the sleep queues at some point.
507 */
508 if (interlock != NULL)
509 simple_unlock(interlock);
510
511 /*
512 * We put ourselves on the sleep queue and start our timeout
513 * before calling CURSIG, as we could stop there, and a wakeup
514 * or a SIGCONT (or both) could occur while we were stopped.
515 * A SIGCONT would cause us to be marked as SSLEEP
516 * without resuming us, thus we must be ready for sleep
517 * when CURSIG is called. If the wakeup happens while we're
518 * stopped, p->p_wchan will be 0 upon return from CURSIG.
519 */
520 if (catch) {
521 l->l_flag |= L_SINTR;
522 if (((sig = CURSIG(l)) != 0) ||
523 ((p->p_flag & P_WEXIT) && p->p_nlwps > 1)) {
524 if (l->l_wchan != NULL)
525 unsleep(l);
526 l->l_stat = LSONPROC;
527 SCHED_UNLOCK(s);
528 goto resume;
529 }
530 if (l->l_wchan == NULL) {
531 catch = 0;
532 SCHED_UNLOCK(s);
533 goto resume;
534 }
535 } else
536 sig = 0;
537 l->l_stat = LSSLEEP;
538 p->p_nrlwps--;
539 p->p_stats->p_ru.ru_nvcsw++;
540 SCHED_ASSERT_LOCKED();
541 if (l->l_flag & L_SA)
542 sa_switch(l, sau, SA_UPCALL_BLOCKED);
543 else
544 mi_switch(l, NULL);
545
546 #if defined(DDB) && !defined(GPROF)
547 /* handy breakpoint location after process "wakes" */
548 __asm(".globl bpendtsleep\nbpendtsleep:");
549 #endif
550 /*
551 * p->p_nrlwps is incremented by whoever made us runnable again,
552 * either setrunnable() or awaken().
553 */
554
555 SCHED_ASSERT_UNLOCKED();
556 splx(s);
557
558 resume:
559 KDASSERT(l->l_cpu != NULL);
560 KDASSERT(l->l_cpu == curcpu());
561 l->l_cpu->ci_schedstate.spc_curpriority = l->l_usrpri;
562
563 l->l_flag &= ~L_SINTR;
564 if (l->l_flag & L_TIMEOUT) {
565 l->l_flag &= ~(L_TIMEOUT|L_CANCELLED);
566 if (sig == 0) {
567 #ifdef KTRACE
568 if (KTRPOINT(p, KTR_CSW))
569 ktrcsw(p, 0, 0);
570 #endif
571 if (relock && interlock != NULL)
572 simple_lock(interlock);
573 return (EWOULDBLOCK);
574 }
575 } else if (timo)
576 callout_stop(&l->l_tsleep_ch);
577
578 if (catch) {
579 const int cancelled = l->l_flag & L_CANCELLED;
580 l->l_flag &= ~L_CANCELLED;
581 if (sig != 0 || (sig = CURSIG(l)) != 0 || cancelled) {
582 #ifdef KTRACE
583 if (KTRPOINT(p, KTR_CSW))
584 ktrcsw(p, 0, 0);
585 #endif
586 if (relock && interlock != NULL)
587 simple_lock(interlock);
588 /*
589 * If this sleep was canceled, don't let the syscall
590 * restart.
591 */
592 if (cancelled ||
593 (SIGACTION(p, sig).sa_flags & SA_RESTART) == 0)
594 return (EINTR);
595 return (ERESTART);
596 }
597 }
598
599 #ifdef KTRACE
600 if (KTRPOINT(p, KTR_CSW))
601 ktrcsw(p, 0, 0);
602 #endif
603 if (relock && interlock != NULL)
604 simple_lock(interlock);
605
606 /* XXXNJW this is very much a kluge.
607 * revisit. a better way of preventing looping/hanging syscalls like
608 * wait4() and _lwp_wait() from wedging an exiting process
609 * would be preferred.
610 */
611 if (catch && ((p->p_flag & P_WEXIT) && p->p_nlwps > 1 && exiterr))
612 return (EINTR);
613 return (0);
614 }
615
616 /*
617 * Implement timeout for tsleep.
618 * If process hasn't been awakened (wchan non-zero),
619 * set timeout flag and undo the sleep. If proc
620 * is stopped, just unsleep so it will remain stopped.
621 */
622 void
623 endtsleep(void *arg)
624 {
625 struct lwp *l;
626 int s;
627
628 l = (struct lwp *)arg;
629 SCHED_LOCK(s);
630 if (l->l_wchan) {
631 if (l->l_stat == LSSLEEP)
632 setrunnable(l);
633 else
634 unsleep(l);
635 l->l_flag |= L_TIMEOUT;
636 }
637 SCHED_UNLOCK(s);
638 }
639
640 /*
641 * Remove a process from its wait queue
642 */
643 void
644 unsleep(struct lwp *l)
645 {
646 struct slpque *qp;
647 struct lwp **hp;
648
649 SCHED_ASSERT_LOCKED();
650
651 if (l->l_wchan) {
652 hp = &(qp = SLPQUE(l->l_wchan))->sq_head;
653 while (*hp != l)
654 hp = &(*hp)->l_forw;
655 *hp = l->l_forw;
656 if (qp->sq_tailp == &l->l_forw)
657 qp->sq_tailp = hp;
658 l->l_wchan = 0;
659 }
660 }
661
662 __inline void
663 sa_awaken(struct lwp *l)
664 {
665
666 SCHED_ASSERT_LOCKED();
667
668 if (l == l->l_savp->savp_lwp && l->l_flag & L_SA_YIELD)
669 l->l_flag &= ~L_SA_IDLE;
670 }
671
672 /*
673 * Optimized-for-wakeup() version of setrunnable().
674 */
675 __inline void
676 awaken(struct lwp *l)
677 {
678
679 SCHED_ASSERT_LOCKED();
680
681 if (l->l_proc->p_sa)
682 sa_awaken(l);
683
684 if (l->l_slptime > 1)
685 updatepri(l);
686 l->l_slptime = 0;
687 l->l_stat = LSRUN;
688 l->l_proc->p_nrlwps++;
689 /*
690 * Since curpriority is a user priority, p->p_priority
691 * is always better than curpriority on the last CPU on
692 * which it ran.
693 *
694 * XXXSMP See affinity comment in resched_proc().
695 */
696 if (l->l_flag & L_INMEM) {
697 setrunqueue(l);
698 KASSERT(l->l_cpu != NULL);
699 need_resched(l->l_cpu);
700 } else
701 sched_wakeup(&proc0);
702 }
703
704 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
705 void
706 sched_unlock_idle(void)
707 {
708
709 simple_unlock(&sched_lock);
710 }
711
712 void
713 sched_lock_idle(void)
714 {
715
716 simple_lock(&sched_lock);
717 }
718 #endif /* MULTIPROCESSOR || LOCKDEBUG */
719
720 /*
721 * Make all processes sleeping on the specified identifier runnable.
722 */
723
724 void
725 wakeup(__volatile const void *ident)
726 {
727 int s;
728
729 SCHED_ASSERT_UNLOCKED();
730
731 SCHED_LOCK(s);
732 sched_wakeup(ident);
733 SCHED_UNLOCK(s);
734 }
735
736 void
737 sched_wakeup(__volatile const void *ident)
738 {
739 struct slpque *qp;
740 struct lwp *l, **q;
741
742 SCHED_ASSERT_LOCKED();
743
744 qp = SLPQUE(ident);
745 restart:
746 for (q = &qp->sq_head; (l = *q) != NULL; ) {
747 #ifdef DIAGNOSTIC
748 if (l->l_back || (l->l_stat != LSSLEEP &&
749 l->l_stat != LSSTOP && l->l_stat != LSSUSPENDED))
750 panic("wakeup");
751 #endif
752 if (l->l_wchan == ident) {
753 l->l_wchan = 0;
754 *q = l->l_forw;
755 if (qp->sq_tailp == &l->l_forw)
756 qp->sq_tailp = q;
757 if (l->l_stat == LSSLEEP) {
758 awaken(l);
759 goto restart;
760 }
761 } else
762 q = &l->l_forw;
763 }
764 }
765
766 /*
767 * Make the highest priority process first in line on the specified
768 * identifier runnable.
769 */
770 void
771 wakeup_one(__volatile const void *ident)
772 {
773 struct slpque *qp;
774 struct lwp *l, **q;
775 struct lwp *best_sleepp, **best_sleepq;
776 struct lwp *best_stopp, **best_stopq;
777 int s;
778
779 best_sleepp = best_stopp = NULL;
780 best_sleepq = best_stopq = NULL;
781
782 SCHED_LOCK(s);
783
784 qp = SLPQUE(ident);
785
786 for (q = &qp->sq_head; (l = *q) != NULL; q = &l->l_forw) {
787 #ifdef DIAGNOSTIC
788 if (l->l_back || (l->l_stat != LSSLEEP &&
789 l->l_stat != LSSTOP && l->l_stat != LSSUSPENDED))
790 panic("wakeup_one");
791 #endif
792 if (l->l_wchan == ident) {
793 if (l->l_stat == LSSLEEP) {
794 if (best_sleepp == NULL ||
795 l->l_priority < best_sleepp->l_priority) {
796 best_sleepp = l;
797 best_sleepq = q;
798 }
799 } else {
800 if (best_stopp == NULL ||
801 l->l_priority < best_stopp->l_priority) {
802 best_stopp = l;
803 best_stopq = q;
804 }
805 }
806 }
807 }
808
809 /*
810 * Consider any SSLEEP process higher than the highest priority SSTOP
811 * process.
812 */
813 if (best_sleepp != NULL) {
814 l = best_sleepp;
815 q = best_sleepq;
816 } else {
817 l = best_stopp;
818 q = best_stopq;
819 }
820
821 if (l != NULL) {
822 l->l_wchan = NULL;
823 *q = l->l_forw;
824 if (qp->sq_tailp == &l->l_forw)
825 qp->sq_tailp = q;
826 if (l->l_stat == LSSLEEP)
827 awaken(l);
828 }
829 SCHED_UNLOCK(s);
830 }
831
832 /*
833 * General yield call. Puts the current process back on its run queue and
834 * performs a voluntary context switch. Should only be called when the
835 * current process explicitly requests it (eg sched_yield(2) in compat code).
836 */
837 void
838 yield(void)
839 {
840 struct lwp *l = curlwp;
841 int s;
842
843 SCHED_LOCK(s);
844 l->l_priority = l->l_usrpri;
845 l->l_stat = LSRUN;
846 setrunqueue(l);
847 l->l_proc->p_stats->p_ru.ru_nvcsw++;
848 mi_switch(l, NULL);
849 SCHED_ASSERT_UNLOCKED();
850 splx(s);
851 }
852
853 /*
854 * General preemption call. Puts the current process back on its run queue
855 * and performs an involuntary context switch. If a process is supplied,
856 * we switch to that process. Otherwise, we use the normal process selection
857 * criteria.
858 */
859
860 void
861 preempt(int more)
862 {
863 struct lwp *l = curlwp;
864 int r, s;
865
866 SCHED_LOCK(s);
867 l->l_priority = l->l_usrpri;
868 l->l_stat = LSRUN;
869 setrunqueue(l);
870 l->l_proc->p_stats->p_ru.ru_nivcsw++;
871 r = mi_switch(l, NULL);
872 SCHED_ASSERT_UNLOCKED();
873 splx(s);
874 if ((l->l_flag & L_SA) != 0 && r != 0 && more == 0)
875 sa_preempt(l);
876 }
877
878 /*
879 * The machine independent parts of context switch.
880 * Must be called at splsched() (no higher!) and with
881 * the sched_lock held.
882 * Switch to "new" if non-NULL, otherwise let cpu_switch choose
883 * the next lwp.
884 *
885 * Returns 1 if another process was actually run.
886 */
887 int
888 mi_switch(struct lwp *l, struct lwp *newl)
889 {
890 struct schedstate_percpu *spc;
891 struct rlimit *rlim;
892 long s, u;
893 struct timeval tv;
894 int hold_count;
895 struct proc *p = l->l_proc;
896 int retval;
897
898 SCHED_ASSERT_LOCKED();
899
900 /*
901 * Release the kernel_lock, as we are about to yield the CPU.
902 * The scheduler lock is still held until cpu_switch()
903 * selects a new process and removes it from the run queue.
904 */
905 hold_count = KERNEL_LOCK_RELEASE_ALL();
906
907 KDASSERT(l->l_cpu != NULL);
908 KDASSERT(l->l_cpu == curcpu());
909
910 spc = &l->l_cpu->ci_schedstate;
911
912 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC)
913 spinlock_switchcheck();
914 #endif
915 #ifdef LOCKDEBUG
916 simple_lock_switchcheck();
917 #endif
918
919 /*
920 * Compute the amount of time during which the current
921 * process was running.
922 */
923 microtime(&tv);
924 u = p->p_rtime.tv_usec +
925 (tv.tv_usec - spc->spc_runtime.tv_usec);
926 s = p->p_rtime.tv_sec + (tv.tv_sec - spc->spc_runtime.tv_sec);
927 if (u < 0) {
928 u += 1000000;
929 s--;
930 } else if (u >= 1000000) {
931 u -= 1000000;
932 s++;
933 }
934 p->p_rtime.tv_usec = u;
935 p->p_rtime.tv_sec = s;
936
937 /*
938 * Check if the process exceeds its CPU resource allocation.
939 * If over max, kill it. In any case, if it has run for more
940 * than 10 minutes, reduce priority to give others a chance.
941 */
942 rlim = &p->p_rlimit[RLIMIT_CPU];
943 if (s >= rlim->rlim_cur) {
944 /*
945 * XXXSMP: we're inside the scheduler lock perimeter;
946 * use sched_psignal.
947 */
948 if (s >= rlim->rlim_max)
949 sched_psignal(p, SIGKILL);
950 else {
951 sched_psignal(p, SIGXCPU);
952 if (rlim->rlim_cur < rlim->rlim_max)
953 rlim->rlim_cur += 5;
954 }
955 }
956 if (autonicetime && s > autonicetime && p->p_ucred->cr_uid &&
957 p->p_nice == NZERO) {
958 p->p_nice = autoniceval + NZERO;
959 resetpriority(l);
960 }
961
962 /*
963 * Process is about to yield the CPU; clear the appropriate
964 * scheduling flags.
965 */
966 spc->spc_flags &= ~SPCF_SWITCHCLEAR;
967
968 #ifdef KSTACK_CHECK_MAGIC
969 kstack_check_magic(l);
970 #endif
971
972 /*
973 * If we are using h/w performance counters, save context.
974 */
975 #if PERFCTRS
976 if (PMC_ENABLED(p))
977 pmc_save_context(p);
978 #endif
979
980 /*
981 * Switch to the new current process. When we
982 * run again, we'll return back here.
983 */
984 uvmexp.swtch++;
985 if (newl == NULL) {
986 retval = cpu_switch(l, NULL);
987 } else {
988 remrunqueue(newl);
989 cpu_switchto(l, newl);
990 retval = 0;
991 }
992
993 /*
994 * If we are using h/w performance counters, restore context.
995 */
996 #if PERFCTRS
997 if (PMC_ENABLED(p))
998 pmc_restore_context(p);
999 #endif
1000
1001 /*
1002 * Make sure that MD code released the scheduler lock before
1003 * resuming us.
1004 */
1005 SCHED_ASSERT_UNLOCKED();
1006
1007 /*
1008 * We're running again; record our new start time. We might
1009 * be running on a new CPU now, so don't use the cache'd
1010 * schedstate_percpu pointer.
1011 */
1012 KDASSERT(l->l_cpu != NULL);
1013 KDASSERT(l->l_cpu == curcpu());
1014 microtime(&l->l_cpu->ci_schedstate.spc_runtime);
1015
1016 /*
1017 * Reacquire the kernel_lock now. We do this after we've
1018 * released the scheduler lock to avoid deadlock, and before
1019 * we reacquire the interlock.
1020 */
1021 KERNEL_LOCK_ACQUIRE_COUNT(hold_count);
1022
1023 return retval;
1024 }
1025
1026 /*
1027 * Initialize the (doubly-linked) run queues
1028 * to be empty.
1029 */
1030 void
1031 rqinit()
1032 {
1033 int i;
1034
1035 for (i = 0; i < RUNQUE_NQS; i++)
1036 sched_qs[i].ph_link = sched_qs[i].ph_rlink =
1037 (struct lwp *)&sched_qs[i];
1038 }
1039
1040 static __inline void
1041 resched_proc(struct lwp *l, u_char pri)
1042 {
1043 struct cpu_info *ci;
1044
1045 /*
1046 * XXXSMP
1047 * Since l->l_cpu persists across a context switch,
1048 * this gives us *very weak* processor affinity, in
1049 * that we notify the CPU on which the process last
1050 * ran that it should try to switch.
1051 *
1052 * This does not guarantee that the process will run on
1053 * that processor next, because another processor might
1054 * grab it the next time it performs a context switch.
1055 *
1056 * This also does not handle the case where its last
1057 * CPU is running a higher-priority process, but every
1058 * other CPU is running a lower-priority process. There
1059 * are ways to handle this situation, but they're not
1060 * currently very pretty, and we also need to weigh the
1061 * cost of moving a process from one CPU to another.
1062 *
1063 * XXXSMP
1064 * There is also the issue of locking the other CPU's
1065 * sched state, which we currently do not do.
1066 */
1067 ci = (l->l_cpu != NULL) ? l->l_cpu : curcpu();
1068 if (pri < ci->ci_schedstate.spc_curpriority)
1069 need_resched(ci);
1070 }
1071
1072 /*
1073 * Change process state to be runnable,
1074 * placing it on the run queue if it is in memory,
1075 * and awakening the swapper if it isn't in memory.
1076 */
1077 void
1078 setrunnable(struct lwp *l)
1079 {
1080 struct proc *p = l->l_proc;
1081
1082 SCHED_ASSERT_LOCKED();
1083
1084 switch (l->l_stat) {
1085 case 0:
1086 case LSRUN:
1087 case LSONPROC:
1088 case LSZOMB:
1089 case LSDEAD:
1090 default:
1091 panic("setrunnable: lwp %p state was %d", l, l->l_stat);
1092 case LSSTOP:
1093 /*
1094 * If we're being traced (possibly because someone attached us
1095 * while we were stopped), check for a signal from the debugger.
1096 */
1097 if ((p->p_flag & P_TRACED) != 0 && p->p_xstat != 0) {
1098 sigaddset(&p->p_sigctx.ps_siglist, p->p_xstat);
1099 CHECKSIGS(p);
1100 }
1101 case LSSLEEP:
1102 unsleep(l); /* e.g. when sending signals */
1103 break;
1104
1105 case LSIDL:
1106 break;
1107 case LSSUSPENDED:
1108 break;
1109 }
1110
1111 if (l->l_proc->p_sa)
1112 sa_awaken(l);
1113
1114 l->l_stat = LSRUN;
1115 p->p_nrlwps++;
1116
1117 if (l->l_flag & L_INMEM)
1118 setrunqueue(l);
1119
1120 if (l->l_slptime > 1)
1121 updatepri(l);
1122 l->l_slptime = 0;
1123 if ((l->l_flag & L_INMEM) == 0)
1124 sched_wakeup((caddr_t)&proc0);
1125 else
1126 resched_proc(l, l->l_priority);
1127 }
1128
1129 /*
1130 * Compute the priority of a process when running in user mode.
1131 * Arrange to reschedule if the resulting priority is better
1132 * than that of the current process.
1133 */
1134 void
1135 resetpriority(struct lwp *l)
1136 {
1137 unsigned int newpriority;
1138 struct proc *p = l->l_proc;
1139
1140 SCHED_ASSERT_LOCKED();
1141
1142 newpriority = PUSER + (p->p_estcpu >> ESTCPU_SHIFT) +
1143 NICE_WEIGHT * (p->p_nice - NZERO);
1144 newpriority = min(newpriority, MAXPRI);
1145 l->l_usrpri = newpriority;
1146 resched_proc(l, l->l_usrpri);
1147 }
1148
1149 /*
1150 * Recompute priority for all LWPs in a process.
1151 */
1152 void
1153 resetprocpriority(struct proc *p)
1154 {
1155 struct lwp *l;
1156
1157 LIST_FOREACH(l, &p->p_lwps, l_sibling)
1158 resetpriority(l);
1159 }
1160
1161 /*
1162 * We adjust the priority of the current process. The priority of a process
1163 * gets worse as it accumulates CPU time. The CPU usage estimator (p_estcpu)
1164 * is increased here. The formula for computing priorities (in kern_synch.c)
1165 * will compute a different value each time p_estcpu increases. This can
1166 * cause a switch, but unless the priority crosses a PPQ boundary the actual
1167 * queue will not change. The CPU usage estimator ramps up quite quickly
1168 * when the process is running (linearly), and decays away exponentially, at
1169 * a rate which is proportionally slower when the system is busy. The basic
1170 * principle is that the system will 90% forget that the process used a lot
1171 * of CPU time in 5 * loadav seconds. This causes the system to favor
1172 * processes which haven't run much recently, and to round-robin among other
1173 * processes.
1174 */
1175
1176 void
1177 schedclock(struct lwp *l)
1178 {
1179 struct proc *p = l->l_proc;
1180 int s;
1181
1182 p->p_estcpu = ESTCPULIM(p->p_estcpu + (1 << ESTCPU_SHIFT));
1183 SCHED_LOCK(s);
1184 resetpriority(l);
1185 SCHED_UNLOCK(s);
1186
1187 if (l->l_priority >= PUSER)
1188 l->l_priority = l->l_usrpri;
1189 }
1190
1191 void
1192 suspendsched()
1193 {
1194 struct lwp *l;
1195 int s;
1196
1197 /*
1198 * Convert all non-P_SYSTEM LSSLEEP or LSRUN processes to
1199 * LSSUSPENDED.
1200 */
1201 proclist_lock_read();
1202 SCHED_LOCK(s);
1203 LIST_FOREACH(l, &alllwp, l_list) {
1204 if ((l->l_proc->p_flag & P_SYSTEM) != 0)
1205 continue;
1206
1207 switch (l->l_stat) {
1208 case LSRUN:
1209 l->l_proc->p_nrlwps--;
1210 if ((l->l_flag & L_INMEM) != 0)
1211 remrunqueue(l);
1212 /* FALLTHROUGH */
1213 case LSSLEEP:
1214 l->l_stat = LSSUSPENDED;
1215 break;
1216 case LSONPROC:
1217 /*
1218 * XXX SMP: we need to deal with processes on
1219 * others CPU !
1220 */
1221 break;
1222 default:
1223 break;
1224 }
1225 }
1226 SCHED_UNLOCK(s);
1227 proclist_unlock_read();
1228 }
1229
1230 /*
1231 * scheduler_fork_hook:
1232 *
1233 * Inherit the parent's scheduler history.
1234 */
1235 void
1236 scheduler_fork_hook(struct proc *parent, struct proc *child)
1237 {
1238
1239 child->p_estcpu = parent->p_estcpu;
1240 }
1241
1242 /*
1243 * scheduler_wait_hook:
1244 *
1245 * Chargeback parents for the sins of their children.
1246 */
1247 void
1248 scheduler_wait_hook(struct proc *parent, struct proc *child)
1249 {
1250
1251 /* XXX Only if parent != init?? */
1252 parent->p_estcpu = ESTCPULIM(parent->p_estcpu + child->p_estcpu);
1253 }
1254
1255 /*
1256 * Low-level routines to access the run queue. Optimised assembler
1257 * routines can override these.
1258 */
1259
1260 #ifndef __HAVE_MD_RUNQUEUE
1261
1262 /*
1263 * On some architectures, it's faster to use a MSB ordering for the priorites
1264 * than the traditional LSB ordering.
1265 */
1266 #ifdef __HAVE_BIGENDIAN_BITOPS
1267 #define RQMASK(n) (0x80000000 >> (n))
1268 #else
1269 #define RQMASK(n) (0x00000001 << (n))
1270 #endif
1271
1272 /*
1273 * The primitives that manipulate the run queues. whichqs tells which
1274 * of the 32 queues qs have processes in them. Setrunqueue puts processes
1275 * into queues, remrunqueue removes them from queues. The running process is
1276 * on no queue, other processes are on a queue related to p->p_priority,
1277 * divided by 4 actually to shrink the 0-127 range of priorities into the 32
1278 * available queues.
1279 */
1280
1281 #ifdef RQDEBUG
1282 static void
1283 checkrunqueue(int whichq, struct lwp *l)
1284 {
1285 const struct prochd * const rq = &sched_qs[whichq];
1286 struct lwp *l2;
1287 int found = 0;
1288 int die = 0;
1289 int empty = 1;
1290 for (l2 = rq->ph_link; l2 != (void*) rq; l2 = l2->l_forw) {
1291 if (l2->l_stat != LSRUN) {
1292 printf("checkrunqueue[%d]: lwp %p state (%d) "
1293 " != LSRUN\n", whichq, l2, l2->l_stat);
1294 }
1295 if (l2->l_back->l_forw != l2) {
1296 printf("checkrunqueue[%d]: lwp %p back-qptr (%p) "
1297 "corrupt %p\n", whichq, l2, l2->l_back,
1298 l2->l_back->l_forw);
1299 die = 1;
1300 }
1301 if (l2->l_forw->l_back != l2) {
1302 printf("checkrunqueue[%d]: lwp %p forw-qptr (%p) "
1303 "corrupt %p\n", whichq, l2, l2->l_forw,
1304 l2->l_forw->l_back);
1305 die = 1;
1306 }
1307 if (l2 == l)
1308 found = 1;
1309 empty = 0;
1310 }
1311 if (empty && (sched_whichqs & RQMASK(whichq)) != 0) {
1312 printf("checkrunqueue[%d]: bit set for empty run-queue %p\n",
1313 whichq, rq);
1314 die = 1;
1315 } else if (!empty && (sched_whichqs & RQMASK(whichq)) == 0) {
1316 printf("checkrunqueue[%d]: bit clear for non-empty "
1317 "run-queue %p\n", whichq, rq);
1318 die = 1;
1319 }
1320 if (l != NULL && (sched_whichqs & RQMASK(whichq)) == 0) {
1321 printf("checkrunqueue[%d]: bit clear for active lwp %p\n",
1322 whichq, l);
1323 die = 1;
1324 }
1325 if (l != NULL && empty) {
1326 printf("checkrunqueue[%d]: empty run-queue %p with "
1327 "active lwp %p\n", whichq, rq, l);
1328 die = 1;
1329 }
1330 if (l != NULL && !found) {
1331 printf("checkrunqueue[%d]: lwp %p not in runqueue %p!",
1332 whichq, l, rq);
1333 die = 1;
1334 }
1335 if (die)
1336 panic("checkrunqueue: inconsistency found");
1337 }
1338 #endif /* RQDEBUG */
1339
1340 void
1341 setrunqueue(struct lwp *l)
1342 {
1343 struct prochd *rq;
1344 struct lwp *prev;
1345 const int whichq = l->l_priority / PPQ;
1346
1347 #ifdef RQDEBUG
1348 checkrunqueue(whichq, NULL);
1349 #endif
1350 #ifdef DIAGNOSTIC
1351 if (l->l_back != NULL || l->l_wchan != NULL || l->l_stat != LSRUN)
1352 panic("setrunqueue");
1353 #endif
1354 sched_whichqs |= RQMASK(whichq);
1355 rq = &sched_qs[whichq];
1356 prev = rq->ph_rlink;
1357 l->l_forw = (struct lwp *)rq;
1358 rq->ph_rlink = l;
1359 prev->l_forw = l;
1360 l->l_back = prev;
1361 #ifdef RQDEBUG
1362 checkrunqueue(whichq, l);
1363 #endif
1364 }
1365
1366 void
1367 remrunqueue(struct lwp *l)
1368 {
1369 struct lwp *prev, *next;
1370 const int whichq = l->l_priority / PPQ;
1371 #ifdef RQDEBUG
1372 checkrunqueue(whichq, l);
1373 #endif
1374 #ifdef DIAGNOSTIC
1375 if (((sched_whichqs & RQMASK(whichq)) == 0))
1376 panic("remrunqueue: bit %d not set", whichq);
1377 #endif
1378 prev = l->l_back;
1379 l->l_back = NULL;
1380 next = l->l_forw;
1381 prev->l_forw = next;
1382 next->l_back = prev;
1383 if (prev == next)
1384 sched_whichqs &= ~RQMASK(whichq);
1385 #ifdef RQDEBUG
1386 checkrunqueue(whichq, NULL);
1387 #endif
1388 }
1389
1390 #undef RQMASK
1391 #endif /* !defined(__HAVE_MD_RUNQUEUE) */
1392