kern_synch.c revision 1.155 1 /* $NetBSD: kern_synch.c,v 1.155 2005/12/15 13:43:49 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.155 2005/12/15 13:43:49 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 << ESTCPU_SHIFT),
363 * sleeping for at least eight times the loadfactor will decay p_estcpu to
364 * less than (1 << ESTCPU_SHIFT).
365 *
366 * note that our ESTCPU_MAX is actually much smaller than (255 << ESTCPU_SHIFT).
367 */
368 void
369 updatepri(struct lwp *l)
370 {
371 struct proc *p = l->l_proc;
372 fixpt_t newcpu;
373 fixpt_t loadfac;
374
375 SCHED_ASSERT_LOCKED();
376
377 newcpu = p->p_estcpu;
378 loadfac = loadfactor(averunnable.ldavg[0]);
379
380 if ((l->l_slptime << FSHIFT) >= 8 * loadfac)
381 p->p_estcpu = 0; /* XXX NJWLWP */
382 else {
383 l->l_slptime--; /* the first time was done in schedcpu */
384 while (newcpu && --l->l_slptime)
385 newcpu = decay_cpu(loadfac, newcpu);
386 p->p_estcpu = newcpu;
387 }
388 resetpriority(l);
389 }
390
391 /*
392 * During autoconfiguration or after a panic, a sleep will simply
393 * lower the priority briefly to allow interrupts, then return.
394 * The priority to be used (safepri) is machine-dependent, thus this
395 * value is initialized and maintained in the machine-dependent layers.
396 * This priority will typically be 0, or the lowest priority
397 * that is safe for use on the interrupt stack; it can be made
398 * higher to block network software interrupts after panics.
399 */
400 int safepri;
401
402 /*
403 * General sleep call. Suspends the current process until a wakeup is
404 * performed on the specified identifier. The process will then be made
405 * runnable with the specified priority. Sleeps at most timo/hz seconds
406 * (0 means no timeout). If pri includes PCATCH flag, signals are checked
407 * before and after sleeping, else signals are not checked. Returns 0 if
408 * awakened, EWOULDBLOCK if the timeout expires. If PCATCH is set and a
409 * signal needs to be delivered, ERESTART is returned if the current system
410 * call should be restarted if possible, and EINTR is returned if the system
411 * call should be interrupted by the signal (return EINTR).
412 *
413 * The interlock is held until the scheduler_slock is acquired. The
414 * interlock will be locked before returning back to the caller
415 * unless the PNORELOCK flag is specified, in which case the
416 * interlock will always be unlocked upon return.
417 */
418 int
419 ltsleep(__volatile const void *ident, int priority, const char *wmesg, int timo,
420 __volatile struct simplelock *interlock)
421 {
422 struct lwp *l = curlwp;
423 struct proc *p = l ? l->l_proc : NULL;
424 struct slpque *qp;
425 struct sadata_upcall *sau;
426 int sig, s;
427 int catch = priority & PCATCH;
428 int relock = (priority & PNORELOCK) == 0;
429 int exiterr = (priority & PNOEXITERR) == 0;
430
431 /*
432 * XXXSMP
433 * This is probably bogus. Figure out what the right
434 * thing to do here really is.
435 * Note that not sleeping if ltsleep is called with curlwp == NULL
436 * in the shutdown case is disgusting but partly necessary given
437 * how shutdown (barely) works.
438 */
439 if (cold || (doing_shutdown && (panicstr || (l == NULL)))) {
440 /*
441 * After a panic, or during autoconfiguration,
442 * just give interrupts a chance, then just return;
443 * don't run any other procs or panic below,
444 * in case this is the idle process and already asleep.
445 */
446 s = splhigh();
447 splx(safepri);
448 splx(s);
449 if (interlock != NULL && relock == 0)
450 simple_unlock(interlock);
451 return (0);
452 }
453
454 KASSERT(p != NULL);
455 LOCK_ASSERT(interlock == NULL || simple_lock_held(interlock));
456
457 #ifdef KTRACE
458 if (KTRPOINT(p, KTR_CSW))
459 ktrcsw(l, 1, 0);
460 #endif
461
462 /*
463 * XXX We need to allocate the sadata_upcall structure here,
464 * XXX since we can't sleep while waiting for memory inside
465 * XXX sa_upcall(). It would be nice if we could safely
466 * XXX allocate the sadata_upcall structure on the stack, here.
467 */
468 if (l->l_flag & L_SA) {
469 sau = sadata_upcall_alloc(0);
470 } else {
471 sau = NULL;
472 }
473
474 SCHED_LOCK(s);
475
476 #ifdef DIAGNOSTIC
477 if (ident == NULL)
478 panic("ltsleep: ident == NULL");
479 if (l->l_stat != LSONPROC)
480 panic("ltsleep: l_stat %d != LSONPROC", l->l_stat);
481 if (l->l_back != NULL)
482 panic("ltsleep: p_back != NULL");
483 #endif
484
485 l->l_wchan = ident;
486 l->l_wmesg = wmesg;
487 l->l_slptime = 0;
488 l->l_priority = priority & PRIMASK;
489
490 qp = SLPQUE(ident);
491 if (qp->sq_head == 0)
492 qp->sq_head = l;
493 else {
494 *qp->sq_tailp = l;
495 }
496 *(qp->sq_tailp = &l->l_forw) = 0;
497
498 if (timo)
499 callout_reset(&l->l_tsleep_ch, timo, endtsleep, l);
500
501 /*
502 * We can now release the interlock; the scheduler_slock
503 * is held, so a thread can't get in to do wakeup() before
504 * we do the switch.
505 *
506 * XXX We leave the code block here, after inserting ourselves
507 * on the sleep queue, because we might want a more clever
508 * data structure for the sleep queues at some point.
509 */
510 if (interlock != NULL)
511 simple_unlock(interlock);
512
513 /*
514 * We put ourselves on the sleep queue and start our timeout
515 * before calling CURSIG, as we could stop there, and a wakeup
516 * or a SIGCONT (or both) could occur while we were stopped.
517 * A SIGCONT would cause us to be marked as SSLEEP
518 * without resuming us, thus we must be ready for sleep
519 * when CURSIG is called. If the wakeup happens while we're
520 * stopped, p->p_wchan will be 0 upon return from CURSIG.
521 */
522 if (catch) {
523 l->l_flag |= L_SINTR;
524 if (((sig = CURSIG(l)) != 0) ||
525 ((p->p_flag & P_WEXIT) && p->p_nlwps > 1)) {
526 if (l->l_wchan != NULL)
527 unsleep(l);
528 l->l_stat = LSONPROC;
529 SCHED_UNLOCK(s);
530 goto resume;
531 }
532 if (l->l_wchan == NULL) {
533 catch = 0;
534 SCHED_UNLOCK(s);
535 goto resume;
536 }
537 } else
538 sig = 0;
539 l->l_stat = LSSLEEP;
540 p->p_nrlwps--;
541 p->p_stats->p_ru.ru_nvcsw++;
542 SCHED_ASSERT_LOCKED();
543 if (l->l_flag & L_SA)
544 sa_switch(l, sau, SA_UPCALL_BLOCKED);
545 else
546 mi_switch(l, NULL);
547
548 #if defined(DDB) && !defined(GPROF)
549 /* handy breakpoint location after process "wakes" */
550 __asm(".globl bpendtsleep\nbpendtsleep:");
551 #endif
552 /*
553 * p->p_nrlwps is incremented by whoever made us runnable again,
554 * either setrunnable() or awaken().
555 */
556
557 SCHED_ASSERT_UNLOCKED();
558 splx(s);
559
560 resume:
561 KDASSERT(l->l_cpu != NULL);
562 KDASSERT(l->l_cpu == curcpu());
563 l->l_cpu->ci_schedstate.spc_curpriority = l->l_usrpri;
564
565 l->l_flag &= ~L_SINTR;
566 if (l->l_flag & L_TIMEOUT) {
567 l->l_flag &= ~(L_TIMEOUT|L_CANCELLED);
568 if (sig == 0) {
569 #ifdef KTRACE
570 if (KTRPOINT(p, KTR_CSW))
571 ktrcsw(l, 0, 0);
572 #endif
573 if (relock && interlock != NULL)
574 simple_lock(interlock);
575 return (EWOULDBLOCK);
576 }
577 } else if (timo)
578 callout_stop(&l->l_tsleep_ch);
579
580 if (catch) {
581 const int cancelled = l->l_flag & L_CANCELLED;
582 l->l_flag &= ~L_CANCELLED;
583 if (sig != 0 || (sig = CURSIG(l)) != 0 || cancelled) {
584 #ifdef KTRACE
585 if (KTRPOINT(p, KTR_CSW))
586 ktrcsw(l, 0, 0);
587 #endif
588 if (relock && interlock != NULL)
589 simple_lock(interlock);
590 /*
591 * If this sleep was canceled, don't let the syscall
592 * restart.
593 */
594 if (cancelled ||
595 (SIGACTION(p, sig).sa_flags & SA_RESTART) == 0)
596 return (EINTR);
597 return (ERESTART);
598 }
599 }
600
601 #ifdef KTRACE
602 if (KTRPOINT(p, KTR_CSW))
603 ktrcsw(l, 0, 0);
604 #endif
605 if (relock && interlock != NULL)
606 simple_lock(interlock);
607
608 /* XXXNJW this is very much a kluge.
609 * revisit. a better way of preventing looping/hanging syscalls like
610 * wait4() and _lwp_wait() from wedging an exiting process
611 * would be preferred.
612 */
613 if (catch && ((p->p_flag & P_WEXIT) && p->p_nlwps > 1 && exiterr))
614 return (EINTR);
615 return (0);
616 }
617
618 /*
619 * Implement timeout for tsleep.
620 * If process hasn't been awakened (wchan non-zero),
621 * set timeout flag and undo the sleep. If proc
622 * is stopped, just unsleep so it will remain stopped.
623 */
624 void
625 endtsleep(void *arg)
626 {
627 struct lwp *l;
628 int s;
629
630 l = (struct lwp *)arg;
631 SCHED_LOCK(s);
632 if (l->l_wchan) {
633 if (l->l_stat == LSSLEEP)
634 setrunnable(l);
635 else
636 unsleep(l);
637 l->l_flag |= L_TIMEOUT;
638 }
639 SCHED_UNLOCK(s);
640 }
641
642 /*
643 * Remove a process from its wait queue
644 */
645 void
646 unsleep(struct lwp *l)
647 {
648 struct slpque *qp;
649 struct lwp **hp;
650
651 SCHED_ASSERT_LOCKED();
652
653 if (l->l_wchan) {
654 hp = &(qp = SLPQUE(l->l_wchan))->sq_head;
655 while (*hp != l)
656 hp = &(*hp)->l_forw;
657 *hp = l->l_forw;
658 if (qp->sq_tailp == &l->l_forw)
659 qp->sq_tailp = hp;
660 l->l_wchan = 0;
661 }
662 }
663
664 __inline void
665 sa_awaken(struct lwp *l)
666 {
667
668 SCHED_ASSERT_LOCKED();
669
670 if (l == l->l_savp->savp_lwp && l->l_flag & L_SA_YIELD)
671 l->l_flag &= ~L_SA_IDLE;
672 }
673
674 /*
675 * Optimized-for-wakeup() version of setrunnable().
676 */
677 __inline void
678 awaken(struct lwp *l)
679 {
680
681 SCHED_ASSERT_LOCKED();
682
683 if (l->l_proc->p_sa)
684 sa_awaken(l);
685
686 if (l->l_slptime > 1)
687 updatepri(l);
688 l->l_slptime = 0;
689 l->l_stat = LSRUN;
690 l->l_proc->p_nrlwps++;
691 /*
692 * Since curpriority is a user priority, p->p_priority
693 * is always better than curpriority on the last CPU on
694 * which it ran.
695 *
696 * XXXSMP See affinity comment in resched_proc().
697 */
698 if (l->l_flag & L_INMEM) {
699 setrunqueue(l);
700 KASSERT(l->l_cpu != NULL);
701 need_resched(l->l_cpu);
702 } else
703 sched_wakeup(&proc0);
704 }
705
706 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
707 void
708 sched_unlock_idle(void)
709 {
710
711 simple_unlock(&sched_lock);
712 }
713
714 void
715 sched_lock_idle(void)
716 {
717
718 simple_lock(&sched_lock);
719 }
720 #endif /* MULTIPROCESSOR || LOCKDEBUG */
721
722 /*
723 * Make all processes sleeping on the specified identifier runnable.
724 */
725
726 void
727 wakeup(__volatile const void *ident)
728 {
729 int s;
730
731 SCHED_ASSERT_UNLOCKED();
732
733 SCHED_LOCK(s);
734 sched_wakeup(ident);
735 SCHED_UNLOCK(s);
736 }
737
738 void
739 sched_wakeup(__volatile const void *ident)
740 {
741 struct slpque *qp;
742 struct lwp *l, **q;
743
744 SCHED_ASSERT_LOCKED();
745
746 qp = SLPQUE(ident);
747 restart:
748 for (q = &qp->sq_head; (l = *q) != NULL; ) {
749 #ifdef DIAGNOSTIC
750 if (l->l_back || (l->l_stat != LSSLEEP &&
751 l->l_stat != LSSTOP && l->l_stat != LSSUSPENDED))
752 panic("wakeup");
753 #endif
754 if (l->l_wchan == ident) {
755 l->l_wchan = 0;
756 *q = l->l_forw;
757 if (qp->sq_tailp == &l->l_forw)
758 qp->sq_tailp = q;
759 if (l->l_stat == LSSLEEP) {
760 awaken(l);
761 goto restart;
762 }
763 } else
764 q = &l->l_forw;
765 }
766 }
767
768 /*
769 * Make the highest priority process first in line on the specified
770 * identifier runnable.
771 */
772 void
773 wakeup_one(__volatile const void *ident)
774 {
775 struct slpque *qp;
776 struct lwp *l, **q;
777 struct lwp *best_sleepp, **best_sleepq;
778 struct lwp *best_stopp, **best_stopq;
779 int s;
780
781 best_sleepp = best_stopp = NULL;
782 best_sleepq = best_stopq = NULL;
783
784 SCHED_LOCK(s);
785
786 qp = SLPQUE(ident);
787
788 for (q = &qp->sq_head; (l = *q) != NULL; q = &l->l_forw) {
789 #ifdef DIAGNOSTIC
790 if (l->l_back || (l->l_stat != LSSLEEP &&
791 l->l_stat != LSSTOP && l->l_stat != LSSUSPENDED))
792 panic("wakeup_one");
793 #endif
794 if (l->l_wchan == ident) {
795 if (l->l_stat == LSSLEEP) {
796 if (best_sleepp == NULL ||
797 l->l_priority < best_sleepp->l_priority) {
798 best_sleepp = l;
799 best_sleepq = q;
800 }
801 } else {
802 if (best_stopp == NULL ||
803 l->l_priority < best_stopp->l_priority) {
804 best_stopp = l;
805 best_stopq = q;
806 }
807 }
808 }
809 }
810
811 /*
812 * Consider any SSLEEP process higher than the highest priority SSTOP
813 * process.
814 */
815 if (best_sleepp != NULL) {
816 l = best_sleepp;
817 q = best_sleepq;
818 } else {
819 l = best_stopp;
820 q = best_stopq;
821 }
822
823 if (l != NULL) {
824 l->l_wchan = NULL;
825 *q = l->l_forw;
826 if (qp->sq_tailp == &l->l_forw)
827 qp->sq_tailp = q;
828 if (l->l_stat == LSSLEEP)
829 awaken(l);
830 }
831 SCHED_UNLOCK(s);
832 }
833
834 /*
835 * General yield call. Puts the current process back on its run queue and
836 * performs a voluntary context switch. Should only be called when the
837 * current process explicitly requests it (eg sched_yield(2) in compat code).
838 */
839 void
840 yield(void)
841 {
842 struct lwp *l = curlwp;
843 int s;
844
845 SCHED_LOCK(s);
846 l->l_priority = l->l_usrpri;
847 l->l_stat = LSRUN;
848 setrunqueue(l);
849 l->l_proc->p_stats->p_ru.ru_nvcsw++;
850 mi_switch(l, NULL);
851 SCHED_ASSERT_UNLOCKED();
852 splx(s);
853 }
854
855 /*
856 * General preemption call. Puts the current process back on its run queue
857 * and performs an involuntary context switch. If a process is supplied,
858 * we switch to that process. Otherwise, we use the normal process selection
859 * criteria.
860 */
861
862 void
863 preempt(int more)
864 {
865 struct lwp *l = curlwp;
866 int r, s;
867
868 SCHED_LOCK(s);
869 l->l_priority = l->l_usrpri;
870 l->l_stat = LSRUN;
871 setrunqueue(l);
872 l->l_proc->p_stats->p_ru.ru_nivcsw++;
873 r = mi_switch(l, NULL);
874 SCHED_ASSERT_UNLOCKED();
875 splx(s);
876 if ((l->l_flag & L_SA) != 0 && r != 0 && more == 0)
877 sa_preempt(l);
878 }
879
880 /*
881 * The machine independent parts of context switch.
882 * Must be called at splsched() (no higher!) and with
883 * the sched_lock held.
884 * Switch to "new" if non-NULL, otherwise let cpu_switch choose
885 * the next lwp.
886 *
887 * Returns 1 if another process was actually run.
888 */
889 int
890 mi_switch(struct lwp *l, struct lwp *newl)
891 {
892 struct schedstate_percpu *spc;
893 struct rlimit *rlim;
894 long s, u;
895 struct timeval tv;
896 int hold_count;
897 struct proc *p = l->l_proc;
898 int retval;
899
900 SCHED_ASSERT_LOCKED();
901
902 /*
903 * Release the kernel_lock, as we are about to yield the CPU.
904 * The scheduler lock is still held until cpu_switch()
905 * selects a new process and removes it from the run queue.
906 */
907 hold_count = KERNEL_LOCK_RELEASE_ALL();
908
909 KDASSERT(l->l_cpu != NULL);
910 KDASSERT(l->l_cpu == curcpu());
911
912 spc = &l->l_cpu->ci_schedstate;
913
914 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC)
915 spinlock_switchcheck();
916 #endif
917 #ifdef LOCKDEBUG
918 simple_lock_switchcheck();
919 #endif
920
921 /*
922 * Compute the amount of time during which the current
923 * process was running.
924 */
925 microtime(&tv);
926 u = p->p_rtime.tv_usec +
927 (tv.tv_usec - spc->spc_runtime.tv_usec);
928 s = p->p_rtime.tv_sec + (tv.tv_sec - spc->spc_runtime.tv_sec);
929 if (u < 0) {
930 u += 1000000;
931 s--;
932 } else if (u >= 1000000) {
933 u -= 1000000;
934 s++;
935 }
936 p->p_rtime.tv_usec = u;
937 p->p_rtime.tv_sec = s;
938
939 /*
940 * Check if the process exceeds its CPU resource allocation.
941 * If over max, kill it. In any case, if it has run for more
942 * than 10 minutes, reduce priority to give others a chance.
943 */
944 rlim = &p->p_rlimit[RLIMIT_CPU];
945 if (s >= rlim->rlim_cur) {
946 /*
947 * XXXSMP: we're inside the scheduler lock perimeter;
948 * use sched_psignal.
949 */
950 if (s >= rlim->rlim_max)
951 sched_psignal(p, SIGKILL);
952 else {
953 sched_psignal(p, SIGXCPU);
954 if (rlim->rlim_cur < rlim->rlim_max)
955 rlim->rlim_cur += 5;
956 }
957 }
958 if (autonicetime && s > autonicetime && p->p_ucred->cr_uid &&
959 p->p_nice == NZERO) {
960 p->p_nice = autoniceval + NZERO;
961 resetpriority(l);
962 }
963
964 /*
965 * Process is about to yield the CPU; clear the appropriate
966 * scheduling flags.
967 */
968 spc->spc_flags &= ~SPCF_SWITCHCLEAR;
969
970 #ifdef KSTACK_CHECK_MAGIC
971 kstack_check_magic(l);
972 #endif
973
974 /*
975 * If we are using h/w performance counters, save context.
976 */
977 #if PERFCTRS
978 if (PMC_ENABLED(p))
979 pmc_save_context(p);
980 #endif
981
982 /*
983 * Switch to the new current process. When we
984 * run again, we'll return back here.
985 */
986 uvmexp.swtch++;
987 if (newl == NULL) {
988 retval = cpu_switch(l, NULL);
989 } else {
990 remrunqueue(newl);
991 cpu_switchto(l, newl);
992 retval = 0;
993 }
994
995 /*
996 * If we are using h/w performance counters, restore context.
997 */
998 #if PERFCTRS
999 if (PMC_ENABLED(p))
1000 pmc_restore_context(p);
1001 #endif
1002
1003 /*
1004 * Make sure that MD code released the scheduler lock before
1005 * resuming us.
1006 */
1007 SCHED_ASSERT_UNLOCKED();
1008
1009 /*
1010 * We're running again; record our new start time. We might
1011 * be running on a new CPU now, so don't use the cache'd
1012 * schedstate_percpu pointer.
1013 */
1014 KDASSERT(l->l_cpu != NULL);
1015 KDASSERT(l->l_cpu == curcpu());
1016 microtime(&l->l_cpu->ci_schedstate.spc_runtime);
1017
1018 /*
1019 * Reacquire the kernel_lock now. We do this after we've
1020 * released the scheduler lock to avoid deadlock, and before
1021 * we reacquire the interlock.
1022 */
1023 KERNEL_LOCK_ACQUIRE_COUNT(hold_count);
1024
1025 return retval;
1026 }
1027
1028 /*
1029 * Initialize the (doubly-linked) run queues
1030 * to be empty.
1031 */
1032 void
1033 rqinit()
1034 {
1035 int i;
1036
1037 for (i = 0; i < RUNQUE_NQS; i++)
1038 sched_qs[i].ph_link = sched_qs[i].ph_rlink =
1039 (struct lwp *)&sched_qs[i];
1040 }
1041
1042 static __inline void
1043 resched_proc(struct lwp *l, u_char pri)
1044 {
1045 struct cpu_info *ci;
1046
1047 /*
1048 * XXXSMP
1049 * Since l->l_cpu persists across a context switch,
1050 * this gives us *very weak* processor affinity, in
1051 * that we notify the CPU on which the process last
1052 * ran that it should try to switch.
1053 *
1054 * This does not guarantee that the process will run on
1055 * that processor next, because another processor might
1056 * grab it the next time it performs a context switch.
1057 *
1058 * This also does not handle the case where its last
1059 * CPU is running a higher-priority process, but every
1060 * other CPU is running a lower-priority process. There
1061 * are ways to handle this situation, but they're not
1062 * currently very pretty, and we also need to weigh the
1063 * cost of moving a process from one CPU to another.
1064 *
1065 * XXXSMP
1066 * There is also the issue of locking the other CPU's
1067 * sched state, which we currently do not do.
1068 */
1069 ci = (l->l_cpu != NULL) ? l->l_cpu : curcpu();
1070 if (pri < ci->ci_schedstate.spc_curpriority)
1071 need_resched(ci);
1072 }
1073
1074 /*
1075 * Change process state to be runnable,
1076 * placing it on the run queue if it is in memory,
1077 * and awakening the swapper if it isn't in memory.
1078 */
1079 void
1080 setrunnable(struct lwp *l)
1081 {
1082 struct proc *p = l->l_proc;
1083
1084 SCHED_ASSERT_LOCKED();
1085
1086 switch (l->l_stat) {
1087 case 0:
1088 case LSRUN:
1089 case LSONPROC:
1090 case LSZOMB:
1091 case LSDEAD:
1092 default:
1093 panic("setrunnable: lwp %p state was %d", l, l->l_stat);
1094 case LSSTOP:
1095 /*
1096 * If we're being traced (possibly because someone attached us
1097 * while we were stopped), check for a signal from the debugger.
1098 */
1099 if ((p->p_flag & P_TRACED) != 0 && p->p_xstat != 0) {
1100 sigaddset(&p->p_sigctx.ps_siglist, p->p_xstat);
1101 CHECKSIGS(p);
1102 }
1103 case LSSLEEP:
1104 unsleep(l); /* e.g. when sending signals */
1105 break;
1106
1107 case LSIDL:
1108 break;
1109 case LSSUSPENDED:
1110 break;
1111 }
1112
1113 if (l->l_proc->p_sa)
1114 sa_awaken(l);
1115
1116 l->l_stat = LSRUN;
1117 p->p_nrlwps++;
1118
1119 if (l->l_flag & L_INMEM)
1120 setrunqueue(l);
1121
1122 if (l->l_slptime > 1)
1123 updatepri(l);
1124 l->l_slptime = 0;
1125 if ((l->l_flag & L_INMEM) == 0)
1126 sched_wakeup((caddr_t)&proc0);
1127 else
1128 resched_proc(l, l->l_priority);
1129 }
1130
1131 /*
1132 * Compute the priority of a process when running in user mode.
1133 * Arrange to reschedule if the resulting priority is better
1134 * than that of the current process.
1135 */
1136 void
1137 resetpriority(struct lwp *l)
1138 {
1139 unsigned int newpriority;
1140 struct proc *p = l->l_proc;
1141
1142 SCHED_ASSERT_LOCKED();
1143
1144 newpriority = PUSER + (p->p_estcpu >> ESTCPU_SHIFT) +
1145 NICE_WEIGHT * (p->p_nice - NZERO);
1146 newpriority = min(newpriority, MAXPRI);
1147 l->l_usrpri = newpriority;
1148 resched_proc(l, l->l_usrpri);
1149 }
1150
1151 /*
1152 * Recompute priority for all LWPs in a process.
1153 */
1154 void
1155 resetprocpriority(struct proc *p)
1156 {
1157 struct lwp *l;
1158
1159 LIST_FOREACH(l, &p->p_lwps, l_sibling)
1160 resetpriority(l);
1161 }
1162
1163 /*
1164 * We adjust the priority of the current process. The priority of a process
1165 * gets worse as it accumulates CPU time. The CPU usage estimator (p_estcpu)
1166 * is increased here. The formula for computing priorities (in kern_synch.c)
1167 * will compute a different value each time p_estcpu increases. This can
1168 * cause a switch, but unless the priority crosses a PPQ boundary the actual
1169 * queue will not change. The CPU usage estimator ramps up quite quickly
1170 * when the process is running (linearly), and decays away exponentially, at
1171 * a rate which is proportionally slower when the system is busy. The basic
1172 * principle is that the system will 90% forget that the process used a lot
1173 * of CPU time in 5 * loadav seconds. This causes the system to favor
1174 * processes which haven't run much recently, and to round-robin among other
1175 * processes.
1176 */
1177
1178 void
1179 schedclock(struct lwp *l)
1180 {
1181 struct proc *p = l->l_proc;
1182 int s;
1183
1184 p->p_estcpu = ESTCPULIM(p->p_estcpu + (1 << ESTCPU_SHIFT));
1185 SCHED_LOCK(s);
1186 resetpriority(l);
1187 SCHED_UNLOCK(s);
1188
1189 if (l->l_priority >= PUSER)
1190 l->l_priority = l->l_usrpri;
1191 }
1192
1193 void
1194 suspendsched()
1195 {
1196 struct lwp *l;
1197 int s;
1198
1199 /*
1200 * Convert all non-P_SYSTEM LSSLEEP or LSRUN processes to
1201 * LSSUSPENDED.
1202 */
1203 proclist_lock_read();
1204 SCHED_LOCK(s);
1205 LIST_FOREACH(l, &alllwp, l_list) {
1206 if ((l->l_proc->p_flag & P_SYSTEM) != 0)
1207 continue;
1208
1209 switch (l->l_stat) {
1210 case LSRUN:
1211 l->l_proc->p_nrlwps--;
1212 if ((l->l_flag & L_INMEM) != 0)
1213 remrunqueue(l);
1214 /* FALLTHROUGH */
1215 case LSSLEEP:
1216 l->l_stat = LSSUSPENDED;
1217 break;
1218 case LSONPROC:
1219 /*
1220 * XXX SMP: we need to deal with processes on
1221 * others CPU !
1222 */
1223 break;
1224 default:
1225 break;
1226 }
1227 }
1228 SCHED_UNLOCK(s);
1229 proclist_unlock_read();
1230 }
1231
1232 /*
1233 * scheduler_fork_hook:
1234 *
1235 * Inherit the parent's scheduler history.
1236 */
1237 void
1238 scheduler_fork_hook(struct proc *parent, struct proc *child)
1239 {
1240
1241 child->p_estcpu = parent->p_estcpu;
1242 }
1243
1244 /*
1245 * scheduler_wait_hook:
1246 *
1247 * Chargeback parents for the sins of their children.
1248 */
1249 void
1250 scheduler_wait_hook(struct proc *parent, struct proc *child)
1251 {
1252
1253 /* XXX Only if parent != init?? */
1254 parent->p_estcpu = ESTCPULIM(parent->p_estcpu + child->p_estcpu);
1255 }
1256
1257 /*
1258 * Low-level routines to access the run queue. Optimised assembler
1259 * routines can override these.
1260 */
1261
1262 #ifndef __HAVE_MD_RUNQUEUE
1263
1264 /*
1265 * On some architectures, it's faster to use a MSB ordering for the priorites
1266 * than the traditional LSB ordering.
1267 */
1268 #ifdef __HAVE_BIGENDIAN_BITOPS
1269 #define RQMASK(n) (0x80000000 >> (n))
1270 #else
1271 #define RQMASK(n) (0x00000001 << (n))
1272 #endif
1273
1274 /*
1275 * The primitives that manipulate the run queues. whichqs tells which
1276 * of the 32 queues qs have processes in them. Setrunqueue puts processes
1277 * into queues, remrunqueue removes them from queues. The running process is
1278 * on no queue, other processes are on a queue related to p->p_priority,
1279 * divided by 4 actually to shrink the 0-127 range of priorities into the 32
1280 * available queues.
1281 */
1282
1283 #ifdef RQDEBUG
1284 static void
1285 checkrunqueue(int whichq, struct lwp *l)
1286 {
1287 const struct prochd * const rq = &sched_qs[whichq];
1288 struct lwp *l2;
1289 int found = 0;
1290 int die = 0;
1291 int empty = 1;
1292 for (l2 = rq->ph_link; l2 != (void*) rq; l2 = l2->l_forw) {
1293 if (l2->l_stat != LSRUN) {
1294 printf("checkrunqueue[%d]: lwp %p state (%d) "
1295 " != LSRUN\n", whichq, l2, l2->l_stat);
1296 }
1297 if (l2->l_back->l_forw != l2) {
1298 printf("checkrunqueue[%d]: lwp %p back-qptr (%p) "
1299 "corrupt %p\n", whichq, l2, l2->l_back,
1300 l2->l_back->l_forw);
1301 die = 1;
1302 }
1303 if (l2->l_forw->l_back != l2) {
1304 printf("checkrunqueue[%d]: lwp %p forw-qptr (%p) "
1305 "corrupt %p\n", whichq, l2, l2->l_forw,
1306 l2->l_forw->l_back);
1307 die = 1;
1308 }
1309 if (l2 == l)
1310 found = 1;
1311 empty = 0;
1312 }
1313 if (empty && (sched_whichqs & RQMASK(whichq)) != 0) {
1314 printf("checkrunqueue[%d]: bit set for empty run-queue %p\n",
1315 whichq, rq);
1316 die = 1;
1317 } else if (!empty && (sched_whichqs & RQMASK(whichq)) == 0) {
1318 printf("checkrunqueue[%d]: bit clear for non-empty "
1319 "run-queue %p\n", whichq, rq);
1320 die = 1;
1321 }
1322 if (l != NULL && (sched_whichqs & RQMASK(whichq)) == 0) {
1323 printf("checkrunqueue[%d]: bit clear for active lwp %p\n",
1324 whichq, l);
1325 die = 1;
1326 }
1327 if (l != NULL && empty) {
1328 printf("checkrunqueue[%d]: empty run-queue %p with "
1329 "active lwp %p\n", whichq, rq, l);
1330 die = 1;
1331 }
1332 if (l != NULL && !found) {
1333 printf("checkrunqueue[%d]: lwp %p not in runqueue %p!",
1334 whichq, l, rq);
1335 die = 1;
1336 }
1337 if (die)
1338 panic("checkrunqueue: inconsistency found");
1339 }
1340 #endif /* RQDEBUG */
1341
1342 void
1343 setrunqueue(struct lwp *l)
1344 {
1345 struct prochd *rq;
1346 struct lwp *prev;
1347 const int whichq = l->l_priority / PPQ;
1348
1349 #ifdef RQDEBUG
1350 checkrunqueue(whichq, NULL);
1351 #endif
1352 #ifdef DIAGNOSTIC
1353 if (l->l_back != NULL || l->l_wchan != NULL || l->l_stat != LSRUN)
1354 panic("setrunqueue");
1355 #endif
1356 sched_whichqs |= RQMASK(whichq);
1357 rq = &sched_qs[whichq];
1358 prev = rq->ph_rlink;
1359 l->l_forw = (struct lwp *)rq;
1360 rq->ph_rlink = l;
1361 prev->l_forw = l;
1362 l->l_back = prev;
1363 #ifdef RQDEBUG
1364 checkrunqueue(whichq, l);
1365 #endif
1366 }
1367
1368 void
1369 remrunqueue(struct lwp *l)
1370 {
1371 struct lwp *prev, *next;
1372 const int whichq = l->l_priority / PPQ;
1373 #ifdef RQDEBUG
1374 checkrunqueue(whichq, l);
1375 #endif
1376 #ifdef DIAGNOSTIC
1377 if (((sched_whichqs & RQMASK(whichq)) == 0))
1378 panic("remrunqueue: bit %d not set", whichq);
1379 #endif
1380 prev = l->l_back;
1381 l->l_back = NULL;
1382 next = l->l_forw;
1383 prev->l_forw = next;
1384 next->l_back = prev;
1385 if (prev == next)
1386 sched_whichqs &= ~RQMASK(whichq);
1387 #ifdef RQDEBUG
1388 checkrunqueue(whichq, NULL);
1389 #endif
1390 }
1391
1392 #undef RQMASK
1393 #endif /* !defined(__HAVE_MD_RUNQUEUE) */
1394