2009-10-22 01:11:57 +04:00
|
|
|
/* $NetBSD: kern_sleepq.c,v 1.37 2009/10/21 21:12:06 rmind Exp $ */
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
/*-
|
2009-03-21 16:11:14 +03:00
|
|
|
* Copyright (c) 2006, 2007, 2008, 2009 The NetBSD Foundation, Inc.
|
2007-02-10 00:55:00 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Andrew Doran.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sleep queue implementation, used by turnstiles and general sleep/wakeup
|
|
|
|
* interfaces.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2009-10-22 01:11:57 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: kern_sleepq.c,v 1.37 2009/10/21 21:12:06 rmind Exp $");
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
2007-05-17 18:51:11 +04:00
|
|
|
#include <sys/cpu.h>
|
2007-02-10 00:55:00 +03:00
|
|
|
#include <sys/pool.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/resourcevar.h>
|
2008-10-15 10:51:17 +04:00
|
|
|
#include <sys/sa.h>
|
|
|
|
#include <sys/savar.h>
|
2007-02-10 00:55:00 +03:00
|
|
|
#include <sys/sched.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/sleepq.h>
|
|
|
|
#include <sys/ktrace.h>
|
|
|
|
|
2007-02-15 23:21:13 +03:00
|
|
|
#include <uvm/uvm_extern.h>
|
|
|
|
|
2008-10-15 10:51:17 +04:00
|
|
|
#include "opt_sa.h"
|
|
|
|
|
2007-03-29 21:34:39 +04:00
|
|
|
int sleepq_sigtoerror(lwp_t *, int);
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
/* General purpose sleep table, used by ltsleep() and condition variables. */
|
|
|
|
sleeptab_t sleeptab;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sleeptab_init:
|
|
|
|
*
|
|
|
|
* Initialize a sleep table.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sleeptab_init(sleeptab_t *st)
|
|
|
|
{
|
|
|
|
sleepq_t *sq;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < SLEEPTAB_HASH_SIZE; i++) {
|
|
|
|
sq = &st->st_queues[i].st_queue;
|
2009-03-21 16:11:14 +03:00
|
|
|
st->st_queues[i].st_mutex =
|
|
|
|
mutex_obj_alloc(MUTEX_DEFAULT, IPL_SCHED);
|
2008-05-26 16:08:38 +04:00
|
|
|
sleepq_init(sq);
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sleepq_init:
|
|
|
|
*
|
|
|
|
* Prepare a sleep queue for use.
|
|
|
|
*/
|
|
|
|
void
|
2008-05-26 16:08:38 +04:00
|
|
|
sleepq_init(sleepq_t *sq)
|
2007-02-10 00:55:00 +03:00
|
|
|
{
|
|
|
|
|
2008-05-26 16:08:38 +04:00
|
|
|
TAILQ_INIT(sq);
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sleepq_remove:
|
|
|
|
*
|
2009-10-22 01:11:57 +04:00
|
|
|
* Remove an LWP from a sleep queue and wake it up.
|
2007-02-10 00:55:00 +03:00
|
|
|
*/
|
2009-10-22 01:11:57 +04:00
|
|
|
void
|
2007-03-29 21:34:39 +04:00
|
|
|
sleepq_remove(sleepq_t *sq, lwp_t *l)
|
2007-02-10 00:55:00 +03:00
|
|
|
{
|
2007-05-17 18:51:11 +04:00
|
|
|
struct schedstate_percpu *spc;
|
2007-02-10 00:55:00 +03:00
|
|
|
struct cpu_info *ci;
|
|
|
|
|
2008-05-26 16:08:38 +04:00
|
|
|
KASSERT(lwp_locked(l, NULL));
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2008-05-26 16:08:38 +04:00
|
|
|
TAILQ_REMOVE(sq, l, l_sleepchain);
|
2007-02-10 00:55:00 +03:00
|
|
|
l->l_syncobj = &sched_syncobj;
|
|
|
|
l->l_wchan = NULL;
|
|
|
|
l->l_sleepq = NULL;
|
2007-02-18 01:31:36 +03:00
|
|
|
l->l_flag &= ~LW_SINTR;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2007-05-17 18:51:11 +04:00
|
|
|
ci = l->l_cpu;
|
|
|
|
spc = &ci->ci_schedstate;
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
/*
|
|
|
|
* If not sleeping, the LWP must have been suspended. Let whoever
|
|
|
|
* holds it stopped set it running again.
|
|
|
|
*/
|
|
|
|
if (l->l_stat != LSSLEEP) {
|
2007-10-13 04:13:05 +04:00
|
|
|
KASSERT(l->l_stat == LSSTOP || l->l_stat == LSSUSPENDED);
|
2008-02-14 17:26:57 +03:00
|
|
|
lwp_setlock(l, spc->spc_lwplock);
|
2009-10-22 01:11:57 +04:00
|
|
|
return;
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the LWP is still on the CPU, mark it as LSONPROC. It may be
|
|
|
|
* about to call mi_switch(), in which case it will yield.
|
|
|
|
*/
|
2008-06-01 01:26:01 +04:00
|
|
|
if ((l->l_pflag & LP_RUNNING) != 0) {
|
2007-02-10 00:55:00 +03:00
|
|
|
l->l_stat = LSONPROC;
|
|
|
|
l->l_slptime = 0;
|
2008-02-14 17:26:57 +03:00
|
|
|
lwp_setlock(l, spc->spc_lwplock);
|
2009-10-22 01:11:57 +04:00
|
|
|
return;
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
2008-05-19 16:48:54 +04:00
|
|
|
/* Update sleep time delta, call the wake-up handler of scheduler */
|
|
|
|
l->l_slpticksum += (hardclock_ticks - l->l_slpticks);
|
2007-10-13 04:13:05 +04:00
|
|
|
sched_wakeup(l);
|
2008-05-19 16:48:54 +04:00
|
|
|
|
|
|
|
/* Look for a CPU to wake up */
|
|
|
|
l->l_cpu = sched_takecpu(l);
|
2007-10-13 04:13:05 +04:00
|
|
|
ci = l->l_cpu;
|
|
|
|
spc = &ci->ci_schedstate;
|
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
/*
|
2007-10-14 18:23:12 +04:00
|
|
|
* Set it running.
|
2007-02-10 00:55:00 +03:00
|
|
|
*/
|
2007-05-17 18:51:11 +04:00
|
|
|
spc_lock(ci);
|
|
|
|
lwp_setlock(l, spc->spc_mutex);
|
2008-10-15 10:51:17 +04:00
|
|
|
#ifdef KERN_SA
|
|
|
|
if (l->l_proc->p_sa != NULL)
|
|
|
|
sa_awaken(l);
|
|
|
|
#endif /* KERN_SA */
|
2007-05-17 18:51:11 +04:00
|
|
|
sched_setrunnable(l);
|
2007-02-10 00:55:00 +03:00
|
|
|
l->l_stat = LSRUN;
|
|
|
|
l->l_slptime = 0;
|
2009-10-22 01:11:57 +04:00
|
|
|
sched_enqueue(l, false);
|
2007-05-17 18:51:11 +04:00
|
|
|
spc_unlock(ci);
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sleepq_insert:
|
|
|
|
*
|
|
|
|
* Insert an LWP into the sleep queue, optionally sorting by priority.
|
|
|
|
*/
|
|
|
|
inline void
|
2007-03-29 21:34:39 +04:00
|
|
|
sleepq_insert(sleepq_t *sq, lwp_t *l, syncobj_t *sobj)
|
2007-02-10 00:55:00 +03:00
|
|
|
{
|
2007-03-29 21:34:39 +04:00
|
|
|
lwp_t *l2;
|
2007-02-26 12:20:52 +03:00
|
|
|
const int pri = lwp_eprio(l);
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
if ((sobj->sobj_flag & SOBJ_SLEEPQ_SORTED) != 0) {
|
2008-05-26 16:08:38 +04:00
|
|
|
TAILQ_FOREACH(l2, sq, l_sleepchain) {
|
2007-11-06 03:42:39 +03:00
|
|
|
if (lwp_eprio(l2) < pri) {
|
2007-02-10 00:55:00 +03:00
|
|
|
TAILQ_INSERT_BEFORE(l2, l, l_sleepchain);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-07 03:58:56 +04:00
|
|
|
if ((sobj->sobj_flag & SOBJ_SLEEPQ_LIFO) != 0)
|
2008-05-26 16:08:38 +04:00
|
|
|
TAILQ_INSERT_HEAD(sq, l, l_sleepchain);
|
2007-09-07 03:58:56 +04:00
|
|
|
else
|
2008-05-26 16:08:38 +04:00
|
|
|
TAILQ_INSERT_TAIL(sq, l, l_sleepchain);
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
2007-05-17 18:51:11 +04:00
|
|
|
/*
|
|
|
|
* sleepq_enqueue:
|
|
|
|
*
|
|
|
|
* Enter an LWP into the sleep queue and prepare for sleep. The sleep
|
|
|
|
* queue must already be locked, and any interlock (such as the kernel
|
|
|
|
* lock) must have be released (see sleeptab_lookup(), sleepq_enter()).
|
|
|
|
*/
|
2007-02-10 00:55:00 +03:00
|
|
|
void
|
2007-11-06 03:42:39 +03:00
|
|
|
sleepq_enqueue(sleepq_t *sq, wchan_t wchan, const char *wmesg, syncobj_t *sobj)
|
2007-02-10 00:55:00 +03:00
|
|
|
{
|
2007-03-29 21:34:39 +04:00
|
|
|
lwp_t *l = curlwp;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2008-05-26 16:08:38 +04:00
|
|
|
KASSERT(lwp_locked(l, NULL));
|
2007-02-10 00:55:00 +03:00
|
|
|
KASSERT(l->l_stat == LSONPROC);
|
|
|
|
KASSERT(l->l_wchan == NULL && l->l_sleepq == NULL);
|
|
|
|
|
|
|
|
l->l_syncobj = sobj;
|
|
|
|
l->l_wchan = wchan;
|
|
|
|
l->l_sleepq = sq;
|
|
|
|
l->l_wmesg = wmesg;
|
|
|
|
l->l_slptime = 0;
|
|
|
|
l->l_stat = LSSLEEP;
|
|
|
|
l->l_sleeperr = 0;
|
|
|
|
|
2007-02-26 12:20:52 +03:00
|
|
|
sleepq_insert(sq, l, sobj);
|
2008-05-19 16:48:54 +04:00
|
|
|
|
|
|
|
/* Save the time when thread has slept */
|
|
|
|
l->l_slpticks = hardclock_ticks;
|
2007-10-09 23:00:13 +04:00
|
|
|
sched_slept(l);
|
2007-02-26 12:20:52 +03:00
|
|
|
}
|
|
|
|
|
2007-05-17 18:51:11 +04:00
|
|
|
/*
|
|
|
|
* sleepq_block:
|
|
|
|
*
|
|
|
|
* After any intermediate step such as releasing an interlock, switch.
|
|
|
|
* sleepq_block() may return early under exceptional conditions, for
|
|
|
|
* example if the LWP's containing process is exiting.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sleepq_block(int timo, bool catch)
|
2007-02-26 12:20:52 +03:00
|
|
|
{
|
2007-07-10 00:51:58 +04:00
|
|
|
int error = 0, sig;
|
2007-05-17 18:51:11 +04:00
|
|
|
struct proc *p;
|
2007-03-29 21:34:39 +04:00
|
|
|
lwp_t *l = curlwp;
|
2007-08-02 03:30:54 +04:00
|
|
|
bool early = false;
|
2008-08-11 06:36:25 +04:00
|
|
|
int biglocks = l->l_biglocks;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2007-08-15 16:07:23 +04:00
|
|
|
ktrcsw(1, 0);
|
2007-02-15 23:21:13 +03:00
|
|
|
|
2007-02-10 00:55:00 +03:00
|
|
|
/*
|
|
|
|
* If sleeping interruptably, check for pending signals, exits or
|
|
|
|
* core dump events.
|
|
|
|
*/
|
|
|
|
if (catch) {
|
2007-02-18 01:31:36 +03:00
|
|
|
l->l_flag |= LW_SINTR;
|
|
|
|
if ((l->l_flag & (LW_CANCELLED|LW_WEXIT|LW_WCORE)) != 0) {
|
|
|
|
l->l_flag &= ~LW_CANCELLED;
|
2007-09-07 03:58:56 +04:00
|
|
|
error = EINTR;
|
|
|
|
early = true;
|
|
|
|
} else if ((l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0))
|
2007-08-02 03:30:54 +04:00
|
|
|
early = true;
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
2007-08-31 19:27:18 +04:00
|
|
|
if (early) {
|
|
|
|
/* lwp_unsleep() will release the lock */
|
2008-03-17 19:54:51 +03:00
|
|
|
lwp_unsleep(l, true);
|
2007-08-31 19:27:18 +04:00
|
|
|
} else {
|
2007-08-02 03:30:54 +04:00
|
|
|
if (timo)
|
2007-09-07 03:58:56 +04:00
|
|
|
callout_schedule(&l->l_timeout_ch, timo);
|
2008-10-15 10:51:17 +04:00
|
|
|
|
|
|
|
#ifdef KERN_SA
|
|
|
|
if (((l->l_flag & LW_SA) != 0) && (~l->l_pflag & LP_SA_NOBLOCK))
|
|
|
|
sa_switch(l);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
mi_switch(l);
|
2007-08-02 03:30:54 +04:00
|
|
|
|
|
|
|
/* The LWP and sleep queue are now unlocked. */
|
|
|
|
if (timo) {
|
|
|
|
/*
|
|
|
|
* Even if the callout appears to have fired, we need to
|
|
|
|
* stop it in order to synchronise with other CPUs.
|
|
|
|
*/
|
2008-04-22 16:04:22 +04:00
|
|
|
if (callout_halt(&l->l_timeout_ch, NULL))
|
2007-08-02 03:30:54 +04:00
|
|
|
error = EWOULDBLOCK;
|
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
2007-05-17 18:51:11 +04:00
|
|
|
if (catch && error == 0) {
|
2007-02-10 00:55:00 +03:00
|
|
|
p = l->l_proc;
|
2007-02-18 01:31:36 +03:00
|
|
|
if ((l->l_flag & (LW_CANCELLED | LW_WEXIT | LW_WCORE)) != 0)
|
2007-02-10 00:55:00 +03:00
|
|
|
error = EINTR;
|
2007-02-18 01:31:36 +03:00
|
|
|
else if ((l->l_flag & LW_PENDSIG) != 0) {
|
2008-06-17 13:11:25 +04:00
|
|
|
/*
|
|
|
|
* Acquiring p_lock may cause us to recurse
|
|
|
|
* through the sleep path and back into this
|
|
|
|
* routine, but is safe because LWPs sleeping
|
|
|
|
* on locks are non-interruptable. We will
|
|
|
|
* not recurse again.
|
|
|
|
*/
|
2008-04-24 22:39:20 +04:00
|
|
|
mutex_enter(p->p_lock);
|
2007-02-10 00:55:00 +03:00
|
|
|
if ((sig = issignal(l)) != 0)
|
|
|
|
error = sleepq_sigtoerror(l, sig);
|
2008-04-24 22:39:20 +04:00
|
|
|
mutex_exit(p->p_lock);
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-15 16:07:23 +04:00
|
|
|
ktrcsw(0, 0);
|
2008-08-11 06:36:25 +04:00
|
|
|
if (__predict_false(biglocks != 0)) {
|
|
|
|
KERNEL_LOCK(biglocks, NULL);
|
2008-05-26 16:08:38 +04:00
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sleepq_wake:
|
|
|
|
*
|
|
|
|
* Wake zero or more LWPs blocked on a single wait channel.
|
|
|
|
*/
|
2007-03-29 21:34:39 +04:00
|
|
|
lwp_t *
|
2008-05-26 16:08:38 +04:00
|
|
|
sleepq_wake(sleepq_t *sq, wchan_t wchan, u_int expected, kmutex_t *mp)
|
2007-02-10 00:55:00 +03:00
|
|
|
{
|
2007-03-29 21:34:39 +04:00
|
|
|
lwp_t *l, *next;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2008-05-26 16:08:38 +04:00
|
|
|
KASSERT(mutex_owned(mp));
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2008-05-26 16:08:38 +04:00
|
|
|
for (l = TAILQ_FIRST(sq); l != NULL; l = next) {
|
2007-02-10 00:55:00 +03:00
|
|
|
KASSERT(l->l_sleepq == sq);
|
2008-05-26 16:08:38 +04:00
|
|
|
KASSERT(l->l_mutex == mp);
|
2007-02-10 00:55:00 +03:00
|
|
|
next = TAILQ_NEXT(l, l_sleepchain);
|
|
|
|
if (l->l_wchan != wchan)
|
|
|
|
continue;
|
2009-10-22 01:11:57 +04:00
|
|
|
sleepq_remove(sq, l);
|
2007-02-10 00:55:00 +03:00
|
|
|
if (--expected == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-05-26 16:08:38 +04:00
|
|
|
mutex_spin_exit(mp);
|
2007-03-29 21:34:39 +04:00
|
|
|
return l;
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sleepq_unsleep:
|
|
|
|
*
|
|
|
|
* Remove an LWP from its sleep queue and set it runnable again.
|
|
|
|
* sleepq_unsleep() is called with the LWP's mutex held, and will
|
|
|
|
* always release it.
|
|
|
|
*/
|
2009-10-22 01:11:57 +04:00
|
|
|
void
|
2008-03-17 19:54:51 +03:00
|
|
|
sleepq_unsleep(lwp_t *l, bool cleanup)
|
2007-02-10 00:55:00 +03:00
|
|
|
{
|
|
|
|
sleepq_t *sq = l->l_sleepq;
|
2008-05-26 16:08:38 +04:00
|
|
|
kmutex_t *mp = l->l_mutex;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2008-05-26 16:08:38 +04:00
|
|
|
KASSERT(lwp_locked(l, mp));
|
2007-02-10 00:55:00 +03:00
|
|
|
KASSERT(l->l_wchan != NULL);
|
|
|
|
|
2009-10-22 01:11:57 +04:00
|
|
|
sleepq_remove(sq, l);
|
2008-03-17 19:54:51 +03:00
|
|
|
if (cleanup) {
|
2008-05-26 16:08:38 +04:00
|
|
|
mutex_spin_exit(mp);
|
2008-03-17 19:54:51 +03:00
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sleepq_timeout:
|
|
|
|
*
|
|
|
|
* Entered via the callout(9) subsystem to time out an LWP that is on a
|
|
|
|
* sleep queue.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sleepq_timeout(void *arg)
|
|
|
|
{
|
2007-03-29 21:34:39 +04:00
|
|
|
lwp_t *l = arg;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Lock the LWP. Assuming it's still on the sleep queue, its
|
|
|
|
* current mutex will also be the sleep queue mutex.
|
|
|
|
*/
|
|
|
|
lwp_lock(l);
|
|
|
|
|
|
|
|
if (l->l_wchan == NULL) {
|
|
|
|
/* Somebody beat us to it. */
|
|
|
|
lwp_unlock(l);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-03-17 19:54:51 +03:00
|
|
|
lwp_unsleep(l, true);
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sleepq_sigtoerror:
|
|
|
|
*
|
|
|
|
* Given a signal number, interpret and return an error code.
|
|
|
|
*/
|
|
|
|
int
|
2007-03-29 21:34:39 +04:00
|
|
|
sleepq_sigtoerror(lwp_t *l, int sig)
|
2007-02-10 00:55:00 +03:00
|
|
|
{
|
|
|
|
struct proc *p = l->l_proc;
|
|
|
|
int error;
|
|
|
|
|
2008-04-24 22:39:20 +04:00
|
|
|
KASSERT(mutex_owned(p->p_lock));
|
2007-02-10 00:55:00 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If this sleep was canceled, don't let the syscall restart.
|
|
|
|
*/
|
|
|
|
if ((SIGACTION(p, sig).sa_flags & SA_RESTART) == 0)
|
|
|
|
error = EINTR;
|
|
|
|
else
|
|
|
|
error = ERESTART;
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sleepq_abort:
|
|
|
|
*
|
|
|
|
* After a panic or during autoconfiguration, lower the interrupt
|
|
|
|
* priority level to give pending interrupts a chance to run, and
|
|
|
|
* then return. Called if sleepq_dontsleep() returns non-zero, and
|
|
|
|
* always returns zero.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sleepq_abort(kmutex_t *mtx, int unlock)
|
|
|
|
{
|
|
|
|
extern int safepri;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
s = splhigh();
|
|
|
|
splx(safepri);
|
|
|
|
splx(s);
|
|
|
|
if (mtx != NULL && unlock != 0)
|
|
|
|
mutex_exit(mtx);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sleepq_changepri:
|
|
|
|
*
|
|
|
|
* Adjust the priority of an LWP residing on a sleepq. This method
|
|
|
|
* will only alter the user priority; the effective priority is
|
|
|
|
* assumed to have been fixed at the time of insertion into the queue.
|
|
|
|
*/
|
|
|
|
void
|
2007-03-29 21:34:39 +04:00
|
|
|
sleepq_changepri(lwp_t *l, pri_t pri)
|
2007-02-10 00:55:00 +03:00
|
|
|
{
|
2007-11-06 03:42:39 +03:00
|
|
|
sleepq_t *sq = l->l_sleepq;
|
|
|
|
pri_t opri;
|
|
|
|
|
2008-05-26 16:08:38 +04:00
|
|
|
KASSERT(lwp_locked(l, NULL));
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2007-11-06 03:42:39 +03:00
|
|
|
opri = lwp_eprio(l);
|
|
|
|
l->l_priority = pri;
|
2008-06-16 14:02:15 +04:00
|
|
|
|
|
|
|
if (lwp_eprio(l) == opri) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((l->l_syncobj->sobj_flag & SOBJ_SLEEPQ_SORTED) == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't let the sleep queue become empty, even briefly.
|
|
|
|
* cv_signal() and cv_broadcast() inspect it without the
|
|
|
|
* sleep queue lock held and need to see a non-empty queue
|
|
|
|
* head if there are waiters.
|
|
|
|
*/
|
|
|
|
if (TAILQ_FIRST(sq) == l && TAILQ_NEXT(l, l_sleepchain) == NULL) {
|
|
|
|
return;
|
2007-11-06 03:42:39 +03:00
|
|
|
}
|
2008-06-16 14:02:15 +04:00
|
|
|
TAILQ_REMOVE(sq, l, l_sleepchain);
|
|
|
|
sleepq_insert(sq, l, l->l_syncobj);
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
2007-02-26 12:20:52 +03:00
|
|
|
|
|
|
|
void
|
2007-03-29 21:34:39 +04:00
|
|
|
sleepq_lendpri(lwp_t *l, pri_t pri)
|
2007-02-26 12:20:52 +03:00
|
|
|
{
|
|
|
|
sleepq_t *sq = l->l_sleepq;
|
2007-02-27 18:07:28 +03:00
|
|
|
pri_t opri;
|
2007-02-26 12:20:52 +03:00
|
|
|
|
2008-05-26 16:08:38 +04:00
|
|
|
KASSERT(lwp_locked(l, NULL));
|
2007-02-26 12:20:52 +03:00
|
|
|
|
|
|
|
opri = lwp_eprio(l);
|
|
|
|
l->l_inheritedprio = pri;
|
|
|
|
|
2008-06-16 14:02:15 +04:00
|
|
|
if (lwp_eprio(l) == opri) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((l->l_syncobj->sobj_flag & SOBJ_SLEEPQ_SORTED) == 0) {
|
|
|
|
return;
|
2007-02-26 12:20:52 +03:00
|
|
|
}
|
2008-06-16 14:02:15 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't let the sleep queue become empty, even briefly.
|
|
|
|
* cv_signal() and cv_broadcast() inspect it without the
|
|
|
|
* sleep queue lock held and need to see a non-empty queue
|
|
|
|
* head if there are waiters.
|
|
|
|
*/
|
|
|
|
if (TAILQ_FIRST(sq) == l && TAILQ_NEXT(l, l_sleepchain) == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
TAILQ_REMOVE(sq, l, l_sleepchain);
|
|
|
|
sleepq_insert(sq, l, l->l_syncobj);
|
2007-02-26 12:20:52 +03:00
|
|
|
}
|