kern_synch.c revision 1.177.2.20 1 /* $NetBSD: kern_synch.c,v 1.177.2.20 2007/03/24 17:13:14 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2004, 2006, 2007 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, by Charles M. Hannum, Andrew Doran and
10 * Daniel Sieger.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 /*-
42 * Copyright (c) 1982, 1986, 1990, 1991, 1993
43 * The Regents of the University of California. All rights reserved.
44 * (c) UNIX System Laboratories, Inc.
45 * All or some portions of this file are derived from material licensed
46 * to the University of California by American Telephone and Telegraph
47 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
48 * the permission of UNIX System Laboratories, Inc.
49 *
50 * Redistribution and use in source and binary forms, with or without
51 * modification, are permitted provided that the following conditions
52 * are met:
53 * 1. Redistributions of source code must retain the above copyright
54 * notice, this list of conditions and the following disclaimer.
55 * 2. Redistributions in binary form must reproduce the above copyright
56 * notice, this list of conditions and the following disclaimer in the
57 * documentation and/or other materials provided with the distribution.
58 * 3. Neither the name of the University nor the names of its contributors
59 * may be used to endorse or promote products derived from this software
60 * without specific prior written permission.
61 *
62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * SUCH DAMAGE.
73 *
74 * @(#)kern_synch.c 8.9 (Berkeley) 5/19/95
75 */
76
77 #include <sys/cdefs.h>
78 __KERNEL_RCSID(0, "$NetBSD: kern_synch.c,v 1.177.2.20 2007/03/24 17:13:14 ad Exp $");
79
80 #include "opt_kstack.h"
81 #include "opt_lockdebug.h"
82 #include "opt_multiprocessor.h"
83 #include "opt_perfctrs.h"
84
85 #define __MUTEX_PRIVATE
86
87 #include <sys/param.h>
88 #include <sys/systm.h>
89 #include <sys/proc.h>
90 #include <sys/kernel.h>
91 #if defined(PERFCTRS)
92 #include <sys/pmc.h>
93 #endif
94 #include <sys/cpu.h>
95 #include <sys/resourcevar.h>
96 #include <sys/sched.h>
97 #include <sys/syscall_stats.h>
98 #include <sys/sleepq.h>
99 #include <sys/lockdebug.h>
100
101 #include <uvm/uvm_extern.h>
102
103 int lbolt; /* once a second sleep address */
104
105 static void sched_unsleep(struct lwp *);
106 static void sched_changepri(struct lwp *, pri_t);
107 static void sched_lendpri(struct lwp *, pri_t);
108
109 syncobj_t sleep_syncobj = {
110 SOBJ_SLEEPQ_SORTED,
111 sleepq_unsleep,
112 sleepq_changepri,
113 sleepq_lendpri,
114 syncobj_noowner,
115 };
116
117 syncobj_t sched_syncobj = {
118 SOBJ_SLEEPQ_SORTED,
119 sched_unsleep,
120 sched_changepri,
121 sched_lendpri,
122 syncobj_noowner,
123 };
124
125 /*
126 * During autoconfiguration or after a panic, a sleep will simply lower the
127 * priority briefly to allow interrupts, then return. The priority to be
128 * used (safepri) is machine-dependent, thus this value is initialized and
129 * maintained in the machine-dependent layers. This priority will typically
130 * be 0, or the lowest priority that is safe for use on the interrupt stack;
131 * it can be made higher to block network software interrupts after panics.
132 */
133 int safepri;
134
135 /*
136 * OBSOLETE INTERFACE
137 *
138 * General sleep call. Suspends the current process until a wakeup is
139 * performed on the specified identifier. The process will then be made
140 * runnable with the specified priority. Sleeps at most timo/hz seconds (0
141 * means no timeout). If pri includes PCATCH flag, signals are checked
142 * before and after sleeping, else signals are not checked. Returns 0 if
143 * awakened, EWOULDBLOCK if the timeout expires. If PCATCH is set and a
144 * signal needs to be delivered, ERESTART is returned if the current system
145 * call should be restarted if possible, and EINTR is returned if the system
146 * call should be interrupted by the signal (return EINTR).
147 *
148 * The interlock is held until we are on a sleep queue. The interlock will
149 * be locked before returning back to the caller unless the PNORELOCK flag
150 * is specified, in which case the interlock will always be unlocked upon
151 * return.
152 */
153 int
154 ltsleep(wchan_t ident, pri_t priority, const char *wmesg, int timo,
155 volatile struct simplelock *interlock)
156 {
157 struct lwp *l = curlwp;
158 sleepq_t *sq;
159 int error, catch;
160
161 if (sleepq_dontsleep(l)) {
162 (void)sleepq_abort(NULL, 0);
163 if ((priority & PNORELOCK) != 0)
164 simple_unlock(interlock);
165 return 0;
166 }
167
168 sq = sleeptab_lookup(&sleeptab, ident);
169 sleepq_enter(sq, l);
170
171 if (interlock != NULL) {
172 LOCK_ASSERT(simple_lock_held(interlock));
173 simple_unlock(interlock);
174 }
175
176 catch = priority & PCATCH;
177 sleepq_block(sq, priority & PRIMASK, ident, wmesg, timo, catch,
178 &sleep_syncobj);
179 error = sleepq_unblock(timo, catch);
180
181 if (interlock != NULL && (priority & PNORELOCK) == 0)
182 simple_lock(interlock);
183
184 return error;
185 }
186
187 int
188 mtsleep(wchan_t ident, pri_t priority, const char *wmesg, int timo,
189 kmutex_t *mtx)
190 {
191 struct lwp *l = curlwp;
192 sleepq_t *sq;
193 int error, catch;
194
195 if (sleepq_dontsleep(l)) {
196 (void)sleepq_abort(mtx, (priority & PNORELOCK) != 0);
197 return 0;
198 }
199
200 sq = sleeptab_lookup(&sleeptab, ident);
201 sleepq_enter(sq, l);
202 mutex_exit(mtx);
203
204 catch = priority & PCATCH;
205 sleepq_block(sq, priority & PRIMASK, ident, wmesg, timo, catch,
206 &sleep_syncobj);
207 error = sleepq_unblock(timo, catch);
208
209 if ((priority & PNORELOCK) == 0)
210 mutex_enter(mtx);
211
212 return error;
213 }
214
215 /*
216 * General sleep call for situations where a wake-up is not expected.
217 */
218 int
219 kpause(const char *wmesg, bool intr, int timo, kmutex_t *mtx)
220 {
221 struct lwp *l = curlwp;
222 sleepq_t *sq;
223 int error;
224
225 if (sleepq_dontsleep(l))
226 return sleepq_abort(NULL, 0);
227
228 if (mtx != NULL)
229 mutex_exit(mtx);
230 sq = sleeptab_lookup(&sleeptab, l);
231 sleepq_enter(sq, l);
232 sleepq_block(sq, sched_kpri(l), l, wmesg, timo, intr, &sleep_syncobj);
233 error = sleepq_unblock(timo, intr);
234 if (mtx != NULL)
235 mutex_enter(mtx);
236
237 return error;
238 }
239
240 /*
241 * OBSOLETE INTERFACE
242 *
243 * Make all processes sleeping on the specified identifier runnable.
244 */
245 void
246 wakeup(wchan_t ident)
247 {
248 sleepq_t *sq;
249
250 if (cold)
251 return;
252
253 sq = sleeptab_lookup(&sleeptab, ident);
254 sleepq_wake(sq, ident, (u_int)-1);
255 }
256
257 /*
258 * OBSOLETE INTERFACE
259 *
260 * Make the highest priority process first in line on the specified
261 * identifier runnable.
262 */
263 void
264 wakeup_one(wchan_t ident)
265 {
266 sleepq_t *sq;
267
268 if (cold)
269 return;
270
271 sq = sleeptab_lookup(&sleeptab, ident);
272 sleepq_wake(sq, ident, 1);
273 }
274
275
276 /*
277 * General yield call. Puts the current process back on its run queue and
278 * performs a voluntary context switch. Should only be called when the
279 * current process explicitly requests it (eg sched_yield(2) in compat code).
280 */
281 void
282 yield(void)
283 {
284 struct lwp *l = curlwp;
285
286 KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
287 lwp_lock(l);
288 if (l->l_stat == LSONPROC) {
289 KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_mutex));
290 l->l_priority = l->l_usrpri;
291 }
292 l->l_nvcsw++;
293 mi_switch(l);
294 KERNEL_LOCK(l->l_biglocks, l);
295 }
296
297 /*
298 * General preemption call. Puts the current process back on its run queue
299 * and performs an involuntary context switch.
300 */
301 void
302 preempt(void)
303 {
304 struct lwp *l = curlwp;
305
306 KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
307 lwp_lock(l);
308 if (l->l_stat == LSONPROC) {
309 KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_mutex));
310 l->l_priority = l->l_usrpri;
311 }
312 l->l_nivcsw++;
313 (void)mi_switch(l);
314 KERNEL_LOCK(l->l_biglocks, l);
315 }
316
317 /*
318 * Compute the amount of time during which the current lwp was running.
319 *
320 * - update l_rtime unless it's an idle lwp.
321 * - update spc_runtime for the next lwp.
322 */
323
324 static inline void
325 updatertime(struct lwp *l, struct schedstate_percpu *spc)
326 {
327 struct timeval tv;
328 long s, u;
329
330 if ((l->l_flag & LW_IDLE) != 0) {
331 microtime(&spc->spc_runtime);
332 return;
333 }
334
335 microtime(&tv);
336 u = l->l_rtime.tv_usec + (tv.tv_usec - spc->spc_runtime.tv_usec);
337 s = l->l_rtime.tv_sec + (tv.tv_sec - spc->spc_runtime.tv_sec);
338 if (u < 0) {
339 u += 1000000;
340 s--;
341 } else if (u >= 1000000) {
342 u -= 1000000;
343 s++;
344 }
345 l->l_rtime.tv_usec = u;
346 l->l_rtime.tv_sec = s;
347
348 spc->spc_runtime = tv;
349 }
350
351 /*
352 * The machine independent parts of context switch.
353 *
354 * Returns 1 if another process was actually run.
355 */
356 int
357 mi_switch(struct lwp *l)
358 {
359 struct schedstate_percpu *spc;
360 struct lwp *newl;
361 int retval, oldspl;
362
363 LOCK_ASSERT(lwp_locked(l, NULL));
364
365 #ifdef LOCKDEBUG
366 spinlock_switchcheck();
367 simple_lock_switchcheck();
368 #endif
369 #ifdef KSTACK_CHECK_MAGIC
370 kstack_check_magic(l);
371 #endif
372
373 /*
374 * It's safe to read the per CPU schedstate unlocked here, as all we
375 * are after is the run time and that's guarenteed to have been last
376 * updated by this CPU.
377 */
378 KDASSERT(l->l_cpu == curcpu());
379 spc = &l->l_cpu->ci_schedstate;
380
381 /* Count time spent in current system call */
382 SYSCALL_TIME_SLEEP(l);
383
384 /*
385 * XXXSMP If we are using h/w performance counters, save context.
386 */
387 #if PERFCTRS
388 if (PMC_ENABLED(l->l_proc)) {
389 pmc_save_context(l->l_proc);
390 }
391 #endif
392
393 /*
394 * If on the CPU and we have gotten this far, then we must yield.
395 */
396 KASSERT(l->l_stat != LSRUN);
397 if (l->l_stat == LSONPROC) {
398 KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_mutex));
399 l->l_stat = LSRUN;
400 if ((l->l_flag & LW_IDLE) == 0) {
401 sched_enqueue(l, true);
402 }
403 }
404
405 /*
406 * Process is about to yield the CPU; clear the appropriate
407 * scheduling flags.
408 */
409 spc->spc_flags &= ~SPCF_SWITCHCLEAR;
410
411 LOCKDEBUG_BARRIER(l->l_mutex, 1);
412
413 /*
414 * Acquire the spc_mutex if necessary.
415 */
416 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
417 if (l->l_mutex != spc->spc_mutex) {
418 mutex_enter(spc->spc_mutex);
419 }
420 #endif
421 /*
422 * Let sched_nextlwp() select the LWP to run the CPU next.
423 * If no LWP is runnable, switch to the idle LWP.
424 */
425 newl = sched_nextlwp();
426 if (newl) {
427 sched_dequeue(newl);
428 } else {
429 newl = l->l_cpu->ci_data.cpu_idlelwp;
430 KASSERT(newl != NULL);
431 }
432 KASSERT(lwp_locked(newl, spc->spc_mutex));
433 newl->l_stat = LSONPROC;
434 newl->l_cpu = l->l_cpu;
435 newl->l_flag |= LW_RUNNING;
436
437 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
438 if (l->l_mutex != spc->spc_mutex) {
439 mutex_exit(spc->spc_mutex);
440 }
441 #endif
442
443 updatertime(l, spc);
444 if (l != newl) {
445 struct lwp *prevlwp;
446
447 /* Unlocked, but for statistics only. */
448 uvmexp.swtch++;
449
450 /* Save old VM context. */
451 pmap_deactivate(l);
452
453 /* Switch to the new LWP.. */
454 l->l_flag &= ~LW_RUNNING;
455 oldspl = MUTEX_SPIN_OLDSPL(l->l_cpu);
456 prevlwp = cpu_switchto(l, newl);
457
458 /* .. we have switched. */
459 curlwp = l;
460 if (prevlwp != NULL) {
461 curcpu()->ci_mtx_oldspl = oldspl;
462 lwp_unlock(prevlwp);
463 } else {
464 splx(oldspl);
465 }
466
467 /* Restore VM context. */
468 pmap_activate(l);
469 retval = 1;
470 } else {
471 /* Nothing to do - just unlock and return. */
472 lwp_unlock(l);
473 retval = 0;
474 }
475
476 KASSERT(l == curlwp);
477 KASSERT(l->l_stat == LSONPROC);
478
479 /*
480 * XXXSMP If we are using h/w performance counters, restore context.
481 */
482 #if PERFCTRS
483 if (PMC_ENABLED(l->l_proc)) {
484 pmc_restore_context(l->l_proc);
485 }
486 #endif
487
488 /*
489 * We're running again; record our new start time. We might
490 * be running on a new CPU now, so don't use the cached
491 * schedstate_percpu pointer.
492 */
493 SYSCALL_TIME_WAKEUP(l);
494 KDASSERT(l->l_cpu == curcpu());
495 LOCKDEBUG_BARRIER(NULL, 1);
496
497 return retval;
498 }
499
500 /*
501 * Change process state to be runnable, placing it on the run queue if it is
502 * in memory, and awakening the swapper if it isn't in memory.
503 *
504 * Call with the process and LWP locked. Will return with the LWP unlocked.
505 */
506 void
507 setrunnable(struct lwp *l)
508 {
509 struct proc *p = l->l_proc;
510 sigset_t *ss;
511
512 KASSERT((l->l_flag & LW_IDLE) == 0);
513 KASSERT(mutex_owned(&p->p_smutex));
514 KASSERT(lwp_locked(l, NULL));
515
516 switch (l->l_stat) {
517 case LSSTOP:
518 /*
519 * If we're being traced (possibly because someone attached us
520 * while we were stopped), check for a signal from the debugger.
521 */
522 if ((p->p_slflag & PSL_TRACED) != 0 && p->p_xstat != 0) {
523 if ((sigprop[p->p_xstat] & SA_TOLWP) != 0)
524 ss = &l->l_sigpend.sp_set;
525 else
526 ss = &p->p_sigpend.sp_set;
527 sigaddset(ss, p->p_xstat);
528 signotify(l);
529 }
530 p->p_nrlwps++;
531 break;
532 case LSSUSPENDED:
533 l->l_flag &= ~LW_WSUSPEND;
534 p->p_nrlwps++;
535 break;
536 case LSSLEEP:
537 KASSERT(l->l_wchan != NULL);
538 break;
539 default:
540 panic("setrunnable: lwp %p state was %d", l, l->l_stat);
541 }
542
543 /*
544 * If the LWP was sleeping interruptably, then it's OK to start it
545 * again. If not, mark it as still sleeping.
546 */
547 if (l->l_wchan != NULL) {
548 l->l_stat = LSSLEEP;
549 /* lwp_unsleep() will release the lock. */
550 lwp_unsleep(l);
551 return;
552 }
553
554 LOCK_ASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_mutex));
555
556 /*
557 * If the LWP is still on the CPU, mark it as LSONPROC. It may be
558 * about to call mi_switch(), in which case it will yield.
559 */
560 if ((l->l_flag & LW_RUNNING) != 0) {
561 l->l_stat = LSONPROC;
562 l->l_slptime = 0;
563 lwp_unlock(l);
564 return;
565 }
566
567 /*
568 * Set the LWP runnable. If it's swapped out, we need to wake the swapper
569 * to bring it back in. Otherwise, enter it into a run queue.
570 */
571 sched_setrunnable(l);
572 l->l_stat = LSRUN;
573 l->l_slptime = 0;
574
575 if (l->l_flag & LW_INMEM) {
576 sched_enqueue(l, false);
577 resched_cpu(l);
578 lwp_unlock(l);
579 } else {
580 lwp_unlock(l);
581 uvm_kick_scheduler();
582 }
583 }
584
585 /*
586 * suspendsched:
587 *
588 * Convert all non-L_SYSTEM LSSLEEP or LSRUN LWPs to LSSUSPENDED.
589 */
590 void
591 suspendsched(void)
592 {
593 #ifdef MULTIPROCESSOR
594 CPU_INFO_ITERATOR cii;
595 struct cpu_info *ci;
596 #endif
597 struct lwp *l;
598 struct proc *p;
599
600 /*
601 * We do this by process in order not to violate the locking rules.
602 */
603 mutex_enter(&proclist_mutex);
604 PROCLIST_FOREACH(p, &allproc) {
605 mutex_enter(&p->p_smutex);
606
607 if ((p->p_flag & PK_SYSTEM) != 0) {
608 mutex_exit(&p->p_smutex);
609 continue;
610 }
611
612 p->p_stat = SSTOP;
613
614 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
615 if (l == curlwp)
616 continue;
617
618 lwp_lock(l);
619
620 /*
621 * Set L_WREBOOT so that the LWP will suspend itself
622 * when it tries to return to user mode. We want to
623 * try and get to get as many LWPs as possible to
624 * the user / kernel boundary, so that they will
625 * release any locks that they hold.
626 */
627 l->l_flag |= (LW_WREBOOT | LW_WSUSPEND);
628
629 if (l->l_stat == LSSLEEP &&
630 (l->l_flag & LW_SINTR) != 0) {
631 /* setrunnable() will release the lock. */
632 setrunnable(l);
633 continue;
634 }
635
636 lwp_unlock(l);
637 }
638
639 mutex_exit(&p->p_smutex);
640 }
641 mutex_exit(&proclist_mutex);
642
643 /*
644 * Kick all CPUs to make them preempt any LWPs running in user mode.
645 * They'll trap into the kernel and suspend themselves in userret().
646 */
647 #ifdef MULTIPROCESSOR
648 for (CPU_INFO_FOREACH(cii, ci))
649 cpu_need_resched(ci, 0);
650 #else
651 cpu_need_resched(curcpu(), 0);
652 #endif
653 }
654
655 /*
656 * sched_kpri:
657 *
658 * Scale a priority level to a kernel priority level, usually
659 * for an LWP that is about to sleep.
660 */
661 pri_t
662 sched_kpri(struct lwp *l)
663 {
664 /*
665 * Scale user priorities (127 -> 50) up to kernel priorities
666 * in the range (49 -> 8). Reserve the top 8 kernel priorities
667 * for high priority kthreads. Kernel priorities passed in
668 * are left "as is". XXX This is somewhat arbitrary.
669 */
670 static const uint8_t kpri_tab[] = {
671 0, 1, 2, 3, 4, 5, 6, 7,
672 8, 9, 10, 11, 12, 13, 14, 15,
673 16, 17, 18, 19, 20, 21, 22, 23,
674 24, 25, 26, 27, 28, 29, 30, 31,
675 32, 33, 34, 35, 36, 37, 38, 39,
676 40, 41, 42, 43, 44, 45, 46, 47,
677 48, 49, 8, 8, 9, 9, 10, 10,
678 11, 11, 12, 12, 13, 14, 14, 15,
679 15, 16, 16, 17, 17, 18, 18, 19,
680 20, 20, 21, 21, 22, 22, 23, 23,
681 24, 24, 25, 26, 26, 27, 27, 28,
682 28, 29, 29, 30, 30, 31, 32, 32,
683 33, 33, 34, 34, 35, 35, 36, 36,
684 37, 38, 38, 39, 39, 40, 40, 41,
685 41, 42, 42, 43, 44, 44, 45, 45,
686 46, 46, 47, 47, 48, 48, 49, 49,
687 };
688
689 return (pri_t)kpri_tab[l->l_usrpri];
690 }
691
692 /*
693 * sched_unsleep:
694 *
695 * The is called when the LWP has not been awoken normally but instead
696 * interrupted: for example, if the sleep timed out. Because of this,
697 * it's not a valid action for running or idle LWPs.
698 */
699 static void
700 sched_unsleep(struct lwp *l)
701 {
702
703 lwp_unlock(l);
704 panic("sched_unsleep");
705 }
706
707 inline void
708 resched_cpu(struct lwp *l)
709 {
710 struct cpu_info *ci;
711 const pri_t pri = lwp_eprio(l);
712
713 /*
714 * XXXSMP
715 * Since l->l_cpu persists across a context switch,
716 * this gives us *very weak* processor affinity, in
717 * that we notify the CPU on which the process last
718 * ran that it should try to switch.
719 *
720 * This does not guarantee that the process will run on
721 * that processor next, because another processor might
722 * grab it the next time it performs a context switch.
723 *
724 * This also does not handle the case where its last
725 * CPU is running a higher-priority process, but every
726 * other CPU is running a lower-priority process. There
727 * are ways to handle this situation, but they're not
728 * currently very pretty, and we also need to weigh the
729 * cost of moving a process from one CPU to another.
730 */
731 ci = (l->l_cpu != NULL) ? l->l_cpu : curcpu();
732 if (pri < ci->ci_schedstate.spc_curpriority)
733 cpu_need_resched(ci, 0);
734 }
735
736 static void
737 sched_changepri(struct lwp *l, pri_t pri)
738 {
739
740 LOCK_ASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_mutex));
741
742 l->l_usrpri = pri;
743 if (l->l_priority < PUSER)
744 return;
745
746 if (l->l_stat != LSRUN || (l->l_flag & LW_INMEM) == 0) {
747 l->l_priority = pri;
748 return;
749 }
750
751 sched_dequeue(l);
752 l->l_priority = pri;
753 sched_enqueue(l, false);
754 resched_cpu(l);
755 }
756
757 static void
758 sched_lendpri(struct lwp *l, pri_t pri)
759 {
760
761 LOCK_ASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_mutex));
762
763 if (l->l_stat != LSRUN || (l->l_flag & LW_INMEM) == 0) {
764 l->l_inheritedprio = pri;
765 return;
766 }
767
768 sched_dequeue(l);
769 l->l_inheritedprio = pri;
770 sched_enqueue(l, false);
771 resched_cpu(l);
772 }
773
774 struct lwp *
775 syncobj_noowner(wchan_t wchan)
776 {
777
778 return NULL;
779 }
780