1994-05-24 10:09:53 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1982, 1986, 1990, 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
* (c) UNIX System Laboratories, Inc.
|
|
|
|
* All or some portions of this file are derived from material licensed
|
|
|
|
* to the University of California by American Telephone and Telegraph
|
|
|
|
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
|
|
|
|
* the permission of UNIX System Laboratories, Inc.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
|
|
|
|
*
|
1996-03-11 05:48:57 +00:00
|
|
|
* @(#)kern_synch.c 8.9 (Berkeley) 5/19/95
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
|
2003-06-11 00:56:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1996-01-03 21:42:35 +00:00
|
|
|
#include "opt_ktrace.h"
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2001-03-28 11:52:56 +00:00
|
|
|
#include <sys/condvar.h>
|
2004-07-10 21:36:01 +00:00
|
|
|
#include <sys/kdb.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/kernel.h>
|
2000-09-07 01:33:02 +00:00
|
|
|
#include <sys/ktr.h>
|
2000-12-06 00:33:58 +00:00
|
|
|
#include <sys/lock.h>
|
2000-10-20 07:52:10 +00:00
|
|
|
#include <sys/mutex.h>
|
2001-03-28 11:52:56 +00:00
|
|
|
#include <sys/proc.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/resourcevar.h>
|
2002-10-12 05:32:24 +00:00
|
|
|
#include <sys/sched.h>
|
2001-03-28 11:52:56 +00:00
|
|
|
#include <sys/signalvar.h>
|
Switch the sleep/wakeup and condition variable implementations to use the
sleep queue interface:
- Sleep queues attempt to merge some of the benefits of both sleep queues
and condition variables. Having sleep qeueus in a hash table avoids
having to allocate a queue head for each wait channel. Thus, struct cv
has shrunk down to just a single char * pointer now. However, the
hash table does not hold threads directly, but queue heads. This means
that once you have located a queue in the hash bucket, you no longer have
to walk the rest of the hash chain looking for threads. Instead, you have
a list of all the threads sleeping on that wait channel.
- Outside of the sleepq code and the sleep/cv code the kernel no longer
differentiates between cv's and sleep/wakeup. For example, calls to
abortsleep() and cv_abort() are replaced with a call to sleepq_abort().
Thus, the TDF_CVWAITQ flag is removed. Also, calls to unsleep() and
cv_waitq_remove() have been replaced with calls to sleepq_remove().
- The sched_sleep() function no longer accepts a priority argument as
sleep's no longer inherently bump the priority. Instead, this is soley
a propery of msleep() which explicitly calls sched_prio() before
blocking.
- The TDF_ONSLEEPQ flag has been dropped as it was never used. The
associated TDF_SET_ONSLEEPQ and TDF_CLR_ON_SLEEPQ macros have also been
dropped and replaced with a single explicit clearing of td_wchan.
TD_SET_ONSLEEPQ() would really have only made sense if it had taken
the wait channel and message as arguments anyway. Now that that only
happens in one place, a macro would be overkill.
2004-02-27 18:52:44 +00:00
|
|
|
#include <sys/sleepqueue.h>
|
2001-04-27 19:28:25 +00:00
|
|
|
#include <sys/smp.h>
|
2001-03-28 11:52:56 +00:00
|
|
|
#include <sys/sx.h>
|
1997-08-08 22:48:57 +00:00
|
|
|
#include <sys/sysctl.h>
|
2000-12-02 05:41:30 +00:00
|
|
|
#include <sys/sysproto.h>
|
2001-03-28 11:52:56 +00:00
|
|
|
#include <sys/vmmeter.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#ifdef KTRACE
|
1998-03-28 10:33:27 +00:00
|
|
|
#include <sys/uio.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/ktrace.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <machine/cpu.h>
|
|
|
|
|
2004-01-25 07:49:45 +00:00
|
|
|
static void synch_setup(void *dummy);
|
|
|
|
SYSINIT(synch_setup, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, synch_setup, NULL)
|
1995-08-28 09:19:25 +00:00
|
|
|
|
1999-02-22 16:57:48 +00:00
|
|
|
int hogticks;
|
1999-03-03 18:15:29 +00:00
|
|
|
int lbolt;
|
2007-02-23 16:22:09 +00:00
|
|
|
static int pause_wchan;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2004-02-01 05:37:36 +00:00
|
|
|
static struct callout loadav_callout;
|
2002-11-21 08:57:08 +00:00
|
|
|
static struct callout lbolt_callout;
|
2000-11-27 22:52:31 +00:00
|
|
|
|
2001-10-20 13:10:43 +00:00
|
|
|
struct loadavg averunnable =
|
|
|
|
{ {0, 0, 0}, FSCALE }; /* load average, of runnable procs */
|
|
|
|
/*
|
|
|
|
* Constants for averages over 1, 5, and 15 minutes
|
|
|
|
* when sampling at 5 second intervals.
|
|
|
|
*/
|
|
|
|
static fixpt_t cexp[3] = {
|
|
|
|
0.9200444146293232 * FSCALE, /* exp(-1/12) */
|
|
|
|
0.9834714538216174 * FSCALE, /* exp(-1/60) */
|
|
|
|
0.9944598480048967 * FSCALE, /* exp(-1/180) */
|
|
|
|
};
|
|
|
|
|
2002-11-21 08:57:08 +00:00
|
|
|
/* kernel uses `FSCALE', userland (SHOULD) use kern.fscale */
|
|
|
|
static int fscale __unused = FSCALE;
|
|
|
|
SYSCTL_INT(_kern, OID_AUTO, fscale, CTLFLAG_RD, 0, FSCALE, "");
|
|
|
|
|
2004-02-01 05:37:36 +00:00
|
|
|
static void loadav(void *arg);
|
2002-11-21 08:57:08 +00:00
|
|
|
static void lboltcb(void *arg);
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1996-07-31 09:26:54 +00:00
|
|
|
void
|
1999-03-03 18:15:29 +00:00
|
|
|
sleepinit(void)
|
1996-07-31 09:26:54 +00:00
|
|
|
{
|
|
|
|
|
2002-10-12 05:32:24 +00:00
|
|
|
hogticks = (hz / 10) * 2; /* Default only. */
|
Switch the sleep/wakeup and condition variable implementations to use the
sleep queue interface:
- Sleep queues attempt to merge some of the benefits of both sleep queues
and condition variables. Having sleep qeueus in a hash table avoids
having to allocate a queue head for each wait channel. Thus, struct cv
has shrunk down to just a single char * pointer now. However, the
hash table does not hold threads directly, but queue heads. This means
that once you have located a queue in the hash bucket, you no longer have
to walk the rest of the hash chain looking for threads. Instead, you have
a list of all the threads sleeping on that wait channel.
- Outside of the sleepq code and the sleep/cv code the kernel no longer
differentiates between cv's and sleep/wakeup. For example, calls to
abortsleep() and cv_abort() are replaced with a call to sleepq_abort().
Thus, the TDF_CVWAITQ flag is removed. Also, calls to unsleep() and
cv_waitq_remove() have been replaced with calls to sleepq_remove().
- The sched_sleep() function no longer accepts a priority argument as
sleep's no longer inherently bump the priority. Instead, this is soley
a propery of msleep() which explicitly calls sched_prio() before
blocking.
- The TDF_ONSLEEPQ flag has been dropped as it was never used. The
associated TDF_SET_ONSLEEPQ and TDF_CLR_ON_SLEEPQ macros have also been
dropped and replaced with a single explicit clearing of td_wchan.
TD_SET_ONSLEEPQ() would really have only made sense if it had taken
the wait channel and message as arguments anyway. Now that that only
happens in one place, a macro would be overkill.
2004-02-27 18:52:44 +00:00
|
|
|
init_sleepqueues();
|
1996-07-31 09:26:54 +00:00
|
|
|
}
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
2006-02-22 20:46:10 +00:00
|
|
|
* General sleep call. Suspends the current thread until a wakeup is
|
|
|
|
* performed on the specified identifier. The thread will then be made
|
1994-05-24 10:09:53 +00:00
|
|
|
* runnable with the specified priority. Sleeps at most timo/hz seconds
|
|
|
|
* (0 means no timeout). If pri includes PCATCH flag, signals are checked
|
|
|
|
* before and after sleeping, else signals are not checked. Returns 0 if
|
|
|
|
* awakened, EWOULDBLOCK if the timeout expires. If PCATCH is set and a
|
|
|
|
* signal needs to be delivered, ERESTART is returned if the current system
|
|
|
|
* call should be restarted if possible, and EINTR is returned if the system
|
|
|
|
* call should be interrupted by the signal (return EINTR).
|
2000-09-11 00:20:02 +00:00
|
|
|
*
|
2006-02-22 20:46:10 +00:00
|
|
|
* The mutex argument is unlocked before the caller is suspended, and
|
|
|
|
* re-locked before msleep returns. If priority includes the PDROP
|
|
|
|
* flag the mutex is not re-locked before returning.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
int
|
2000-09-11 00:20:02 +00:00
|
|
|
msleep(ident, mtx, priority, wmesg, timo)
|
1994-05-24 10:09:53 +00:00
|
|
|
void *ident;
|
2000-09-14 20:15:16 +00:00
|
|
|
struct mtx *mtx;
|
1994-05-24 10:09:53 +00:00
|
|
|
int priority, timo;
|
1997-11-21 11:37:03 +00:00
|
|
|
const char *wmesg;
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
Switch the sleep/wakeup and condition variable implementations to use the
sleep queue interface:
- Sleep queues attempt to merge some of the benefits of both sleep queues
and condition variables. Having sleep qeueus in a hash table avoids
having to allocate a queue head for each wait channel. Thus, struct cv
has shrunk down to just a single char * pointer now. However, the
hash table does not hold threads directly, but queue heads. This means
that once you have located a queue in the hash bucket, you no longer have
to walk the rest of the hash chain looking for threads. Instead, you have
a list of all the threads sleeping on that wait channel.
- Outside of the sleepq code and the sleep/cv code the kernel no longer
differentiates between cv's and sleep/wakeup. For example, calls to
abortsleep() and cv_abort() are replaced with a call to sleepq_abort().
Thus, the TDF_CVWAITQ flag is removed. Also, calls to unsleep() and
cv_waitq_remove() have been replaced with calls to sleepq_remove().
- The sched_sleep() function no longer accepts a priority argument as
sleep's no longer inherently bump the priority. Instead, this is soley
a propery of msleep() which explicitly calls sched_prio() before
blocking.
- The TDF_ONSLEEPQ flag has been dropped as it was never used. The
associated TDF_SET_ONSLEEPQ and TDF_CLR_ON_SLEEPQ macros have also been
dropped and replaced with a single explicit clearing of td_wchan.
TD_SET_ONSLEEPQ() would really have only made sense if it had taken
the wait channel and message as arguments anyway. Now that that only
happens in one place, a macro would be overkill.
2004-02-27 18:52:44 +00:00
|
|
|
struct thread *td;
|
|
|
|
struct proc *p;
|
2006-11-30 08:27:38 +00:00
|
|
|
int catch, rval, flags, pri;
|
2000-09-11 00:20:02 +00:00
|
|
|
WITNESS_SAVE_DECL(mtx);
|
1994-05-24 10:09:53 +00:00
|
|
|
|
Switch the sleep/wakeup and condition variable implementations to use the
sleep queue interface:
- Sleep queues attempt to merge some of the benefits of both sleep queues
and condition variables. Having sleep qeueus in a hash table avoids
having to allocate a queue head for each wait channel. Thus, struct cv
has shrunk down to just a single char * pointer now. However, the
hash table does not hold threads directly, but queue heads. This means
that once you have located a queue in the hash bucket, you no longer have
to walk the rest of the hash chain looking for threads. Instead, you have
a list of all the threads sleeping on that wait channel.
- Outside of the sleepq code and the sleep/cv code the kernel no longer
differentiates between cv's and sleep/wakeup. For example, calls to
abortsleep() and cv_abort() are replaced with a call to sleepq_abort().
Thus, the TDF_CVWAITQ flag is removed. Also, calls to unsleep() and
cv_waitq_remove() have been replaced with calls to sleepq_remove().
- The sched_sleep() function no longer accepts a priority argument as
sleep's no longer inherently bump the priority. Instead, this is soley
a propery of msleep() which explicitly calls sched_prio() before
blocking.
- The TDF_ONSLEEPQ flag has been dropped as it was never used. The
associated TDF_SET_ONSLEEPQ and TDF_CLR_ON_SLEEPQ macros have also been
dropped and replaced with a single explicit clearing of td_wchan.
TD_SET_ONSLEEPQ() would really have only made sense if it had taken
the wait channel and message as arguments anyway. Now that that only
happens in one place, a macro would be overkill.
2004-02-27 18:52:44 +00:00
|
|
|
td = curthread;
|
|
|
|
p = td->td_proc;
|
1994-05-24 10:09:53 +00:00
|
|
|
#ifdef KTRACE
|
2002-06-07 05:39:16 +00:00
|
|
|
if (KTRPOINT(td, KTR_CSW))
|
|
|
|
ktrcsw(1, 0);
|
1994-05-24 10:09:53 +00:00
|
|
|
#endif
|
Switch the sleep/wakeup and condition variable implementations to use the
sleep queue interface:
- Sleep queues attempt to merge some of the benefits of both sleep queues
and condition variables. Having sleep qeueus in a hash table avoids
having to allocate a queue head for each wait channel. Thus, struct cv
has shrunk down to just a single char * pointer now. However, the
hash table does not hold threads directly, but queue heads. This means
that once you have located a queue in the hash bucket, you no longer have
to walk the rest of the hash chain looking for threads. Instead, you have
a list of all the threads sleeping on that wait channel.
- Outside of the sleepq code and the sleep/cv code the kernel no longer
differentiates between cv's and sleep/wakeup. For example, calls to
abortsleep() and cv_abort() are replaced with a call to sleepq_abort().
Thus, the TDF_CVWAITQ flag is removed. Also, calls to unsleep() and
cv_waitq_remove() have been replaced with calls to sleepq_remove().
- The sched_sleep() function no longer accepts a priority argument as
sleep's no longer inherently bump the priority. Instead, this is soley
a propery of msleep() which explicitly calls sched_prio() before
blocking.
- The TDF_ONSLEEPQ flag has been dropped as it was never used. The
associated TDF_SET_ONSLEEPQ and TDF_CLR_ON_SLEEPQ macros have also been
dropped and replaced with a single explicit clearing of td_wchan.
TD_SET_ONSLEEPQ() would really have only made sense if it had taken
the wait channel and message as arguments anyway. Now that that only
happens in one place, a macro would be overkill.
2004-02-27 18:52:44 +00:00
|
|
|
WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, mtx == NULL ? NULL :
|
|
|
|
&mtx->mtx_object, "Sleeping on \"%s\"", wmesg);
|
2006-11-15 20:44:07 +00:00
|
|
|
KASSERT(timo != 0 || mtx_owned(&Giant) || mtx != NULL ||
|
|
|
|
ident == &lbolt, ("sleeping without a mutex"));
|
Switch the sleep/wakeup and condition variable implementations to use the
sleep queue interface:
- Sleep queues attempt to merge some of the benefits of both sleep queues
and condition variables. Having sleep qeueus in a hash table avoids
having to allocate a queue head for each wait channel. Thus, struct cv
has shrunk down to just a single char * pointer now. However, the
hash table does not hold threads directly, but queue heads. This means
that once you have located a queue in the hash bucket, you no longer have
to walk the rest of the hash chain looking for threads. Instead, you have
a list of all the threads sleeping on that wait channel.
- Outside of the sleepq code and the sleep/cv code the kernel no longer
differentiates between cv's and sleep/wakeup. For example, calls to
abortsleep() and cv_abort() are replaced with a call to sleepq_abort().
Thus, the TDF_CVWAITQ flag is removed. Also, calls to unsleep() and
cv_waitq_remove() have been replaced with calls to sleepq_remove().
- The sched_sleep() function no longer accepts a priority argument as
sleep's no longer inherently bump the priority. Instead, this is soley
a propery of msleep() which explicitly calls sched_prio() before
blocking.
- The TDF_ONSLEEPQ flag has been dropped as it was never used. The
associated TDF_SET_ONSLEEPQ and TDF_CLR_ON_SLEEPQ macros have also been
dropped and replaced with a single explicit clearing of td_wchan.
TD_SET_ONSLEEPQ() would really have only made sense if it had taken
the wait channel and message as arguments anyway. Now that that only
happens in one place, a macro would be overkill.
2004-02-27 18:52:44 +00:00
|
|
|
KASSERT(p != NULL, ("msleep1"));
|
|
|
|
KASSERT(ident != NULL && TD_IS_RUNNING(td), ("msleep"));
|
|
|
|
|
|
|
|
if (cold) {
|
|
|
|
/*
|
|
|
|
* During autoconfiguration, just return;
|
2004-05-14 20:51:42 +00:00
|
|
|
* don't run any other threads or panic below,
|
|
|
|
* in case this is the idle thread and already asleep.
|
Switch the sleep/wakeup and condition variable implementations to use the
sleep queue interface:
- Sleep queues attempt to merge some of the benefits of both sleep queues
and condition variables. Having sleep qeueus in a hash table avoids
having to allocate a queue head for each wait channel. Thus, struct cv
has shrunk down to just a single char * pointer now. However, the
hash table does not hold threads directly, but queue heads. This means
that once you have located a queue in the hash bucket, you no longer have
to walk the rest of the hash chain looking for threads. Instead, you have
a list of all the threads sleeping on that wait channel.
- Outside of the sleepq code and the sleep/cv code the kernel no longer
differentiates between cv's and sleep/wakeup. For example, calls to
abortsleep() and cv_abort() are replaced with a call to sleepq_abort().
Thus, the TDF_CVWAITQ flag is removed. Also, calls to unsleep() and
cv_waitq_remove() have been replaced with calls to sleepq_remove().
- The sched_sleep() function no longer accepts a priority argument as
sleep's no longer inherently bump the priority. Instead, this is soley
a propery of msleep() which explicitly calls sched_prio() before
blocking.
- The TDF_ONSLEEPQ flag has been dropped as it was never used. The
associated TDF_SET_ONSLEEPQ and TDF_CLR_ON_SLEEPQ macros have also been
dropped and replaced with a single explicit clearing of td_wchan.
TD_SET_ONSLEEPQ() would really have only made sense if it had taken
the wait channel and message as arguments anyway. Now that that only
happens in one place, a macro would be overkill.
2004-02-27 18:52:44 +00:00
|
|
|
* XXX: this used to do "s = splhigh(); splx(safepri);
|
|
|
|
* splx(s);" to give interrupts a chance, but there is
|
|
|
|
* no way to give interrupts a chance now.
|
|
|
|
*/
|
|
|
|
if (mtx != NULL && priority & PDROP)
|
|
|
|
mtx_unlock(mtx);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
catch = priority & PCATCH;
|
|
|
|
rval = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we are already on a sleep queue, then remove us from that
|
|
|
|
* sleep queue first. We have to do this to handle recursive
|
|
|
|
* sleeps.
|
|
|
|
*/
|
|
|
|
if (TD_ON_SLEEPQ(td))
|
|
|
|
sleepq_remove(td, td->td_wchan);
|
|
|
|
|
2007-02-23 16:22:09 +00:00
|
|
|
if (ident == &pause_wchan)
|
|
|
|
flags = SLEEPQ_PAUSE;
|
|
|
|
else
|
|
|
|
flags = SLEEPQ_MSLEEP;
|
2006-02-23 00:13:58 +00:00
|
|
|
if (catch)
|
|
|
|
flags |= SLEEPQ_INTERRUPTIBLE;
|
|
|
|
|
2004-10-12 18:36:20 +00:00
|
|
|
sleepq_lock(ident);
|
2004-05-14 20:51:42 +00:00
|
|
|
CTR5(KTR_PROC, "msleep: thread %p (pid %ld, %s) on %s (%p)",
|
|
|
|
(void *)td, (long)p->p_pid, p->p_comm, wmesg, ident);
|
Switch the sleep/wakeup and condition variable implementations to use the
sleep queue interface:
- Sleep queues attempt to merge some of the benefits of both sleep queues
and condition variables. Having sleep qeueus in a hash table avoids
having to allocate a queue head for each wait channel. Thus, struct cv
has shrunk down to just a single char * pointer now. However, the
hash table does not hold threads directly, but queue heads. This means
that once you have located a queue in the hash bucket, you no longer have
to walk the rest of the hash chain looking for threads. Instead, you have
a list of all the threads sleeping on that wait channel.
- Outside of the sleepq code and the sleep/cv code the kernel no longer
differentiates between cv's and sleep/wakeup. For example, calls to
abortsleep() and cv_abort() are replaced with a call to sleepq_abort().
Thus, the TDF_CVWAITQ flag is removed. Also, calls to unsleep() and
cv_waitq_remove() have been replaced with calls to sleepq_remove().
- The sched_sleep() function no longer accepts a priority argument as
sleep's no longer inherently bump the priority. Instead, this is soley
a propery of msleep() which explicitly calls sched_prio() before
blocking.
- The TDF_ONSLEEPQ flag has been dropped as it was never used. The
associated TDF_SET_ONSLEEPQ and TDF_CLR_ON_SLEEPQ macros have also been
dropped and replaced with a single explicit clearing of td_wchan.
TD_SET_ONSLEEPQ() would really have only made sense if it had taken
the wait channel and message as arguments anyway. Now that that only
happens in one place, a macro would be overkill.
2004-02-27 18:52:44 +00:00
|
|
|
|
Change the preemption code for software interrupt thread schedules and
mutex releases to not require flags for the cases when preemption is
not allowed:
The purpose of the MTX_NOSWITCH and SWI_NOSWITCH flags is to prevent
switching to a higher priority thread on mutex releease and swi schedule,
respectively when that switch is not safe. Now that the critical section
API maintains a per-thread nesting count, the kernel can easily check
whether or not it should switch without relying on flags from the
programmer. This fixes a few bugs in that all current callers of
swi_sched() used SWI_NOSWITCH, when in fact, only the ones called from
fast interrupt handlers and the swi_sched of softclock needed this flag.
Note that to ensure that swi_sched()'s in clock and fast interrupt
handlers do not switch, these handlers have to be explicitly wrapped
in critical_enter/exit pairs. Presently, just wrapping the handlers is
sufficient, but in the future with the fully preemptive kernel, the
interrupt must be EOI'd before critical_exit() is called. (critical_exit()
can switch due to a deferred preemption in a fully preemptive kernel.)
I've tested the changes to the interrupt code on i386 and alpha. I have
not tested ia64, but the interrupt code is almost identical to the alpha
code, so I expect it will work fine. PowerPC and ARM do not yet have
interrupt code in the tree so they shouldn't be broken. Sparc64 is
broken, but that's been ok'd by jake and tmm who will be fixing the
interrupt code for sparc64 shortly.
Reviewed by: peter
Tested on: i386, alpha
2002-01-05 08:47:13 +00:00
|
|
|
DROP_GIANT();
|
2000-11-29 18:32:50 +00:00
|
|
|
if (mtx != NULL) {
|
|
|
|
mtx_assert(mtx, MA_OWNED | MA_NOTRECURSED);
|
Rework the witness code to work with sx locks as well as mutexes.
- Introduce lock classes and lock objects. Each lock class specifies a
name and set of flags (or properties) shared by all locks of a given
type. Currently there are three lock classes: spin mutexes, sleep
mutexes, and sx locks. A lock object specifies properties of an
additional lock along with a lock name and all of the extra stuff needed
to make witness work with a given lock. This abstract lock stuff is
defined in sys/lock.h. The lockmgr constants, types, and prototypes have
been moved to sys/lockmgr.h. For temporary backwards compatability,
sys/lock.h includes sys/lockmgr.h.
- Replace proc->p_spinlocks with a per-CPU list, PCPU(spinlocks), of spin
locks held. By making this per-cpu, we do not have to jump through
magic hoops to deal with sched_lock changing ownership during context
switches.
- Replace proc->p_heldmtx, formerly a list of held sleep mutexes, with
proc->p_sleeplocks, which is a list of held sleep locks including sleep
mutexes and sx locks.
- Add helper macros for logging lock events via the KTR_LOCK KTR logging
level so that the log messages are consistent.
- Add some new flags that can be passed to mtx_init():
- MTX_NOWITNESS - specifies that this lock should be ignored by witness.
This is used for the mutex that blocks a sx lock for example.
- MTX_QUIET - this is not new, but you can pass this to mtx_init() now
and no events will be logged for this lock, so that one doesn't have
to change all the individual mtx_lock/unlock() operations.
- All lock objects maintain an initialized flag. Use this flag to export
a mtx_initialized() macro that can be safely called from drivers. Also,
we on longer walk the all_mtx list if MUTEX_DEBUG is defined as witness
performs the corresponding checks using the initialized flag.
- The lock order reversal messages have been improved to output slightly
more accurate file and line numbers.
2001-03-28 09:03:24 +00:00
|
|
|
WITNESS_SAVE(&mtx->mtx_object, mtx);
|
Change the preemption code for software interrupt thread schedules and
mutex releases to not require flags for the cases when preemption is
not allowed:
The purpose of the MTX_NOSWITCH and SWI_NOSWITCH flags is to prevent
switching to a higher priority thread on mutex releease and swi schedule,
respectively when that switch is not safe. Now that the critical section
API maintains a per-thread nesting count, the kernel can easily check
whether or not it should switch without relying on flags from the
programmer. This fixes a few bugs in that all current callers of
swi_sched() used SWI_NOSWITCH, when in fact, only the ones called from
fast interrupt handlers and the swi_sched of softclock needed this flag.
Note that to ensure that swi_sched()'s in clock and fast interrupt
handlers do not switch, these handlers have to be explicitly wrapped
in critical_enter/exit pairs. Presently, just wrapping the handlers is
sufficient, but in the future with the fully preemptive kernel, the
interrupt must be EOI'd before critical_exit() is called. (critical_exit()
can switch due to a deferred preemption in a fully preemptive kernel.)
I've tested the changes to the interrupt code on i386 and alpha. I have
not tested ia64, but the interrupt code is almost identical to the alpha
code, so I expect it will work fine. PowerPC and ARM do not yet have
interrupt code in the tree so they shouldn't be broken. Sparc64 is
broken, but that's been ok'd by jake and tmm who will be fixing the
interrupt code for sparc64 shortly.
Reviewed by: peter
Tested on: i386, alpha
2002-01-05 08:47:13 +00:00
|
|
|
mtx_unlock(mtx);
|
2000-11-29 18:32:50 +00:00
|
|
|
}
|
2001-09-12 08:38:13 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* We put ourselves on the sleep queue and start our timeout
|
Switch the sleep/wakeup and condition variable implementations to use the
sleep queue interface:
- Sleep queues attempt to merge some of the benefits of both sleep queues
and condition variables. Having sleep qeueus in a hash table avoids
having to allocate a queue head for each wait channel. Thus, struct cv
has shrunk down to just a single char * pointer now. However, the
hash table does not hold threads directly, but queue heads. This means
that once you have located a queue in the hash bucket, you no longer have
to walk the rest of the hash chain looking for threads. Instead, you have
a list of all the threads sleeping on that wait channel.
- Outside of the sleepq code and the sleep/cv code the kernel no longer
differentiates between cv's and sleep/wakeup. For example, calls to
abortsleep() and cv_abort() are replaced with a call to sleepq_abort().
Thus, the TDF_CVWAITQ flag is removed. Also, calls to unsleep() and
cv_waitq_remove() have been replaced with calls to sleepq_remove().
- The sched_sleep() function no longer accepts a priority argument as
sleep's no longer inherently bump the priority. Instead, this is soley
a propery of msleep() which explicitly calls sched_prio() before
blocking.
- The TDF_ONSLEEPQ flag has been dropped as it was never used. The
associated TDF_SET_ONSLEEPQ and TDF_CLR_ON_SLEEPQ macros have also been
dropped and replaced with a single explicit clearing of td_wchan.
TD_SET_ONSLEEPQ() would really have only made sense if it had taken
the wait channel and message as arguments anyway. Now that that only
happens in one place, a macro would be overkill.
2004-02-27 18:52:44 +00:00
|
|
|
* before calling thread_suspend_check, as we could stop there,
|
|
|
|
* and a wakeup or a SIGCONT (or both) could occur while we were
|
|
|
|
* stopped without resuming us. Thus, we must be ready for sleep
|
|
|
|
* when cursig() is called. If the wakeup happens while we're
|
|
|
|
* stopped, then td will no longer be on a sleep queue upon
|
|
|
|
* return from cursig().
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
2006-11-16 01:02:00 +00:00
|
|
|
sleepq_add(ident, ident == &lbolt ? NULL : &mtx->mtx_object, wmesg,
|
2006-12-16 06:54:09 +00:00
|
|
|
flags, 0);
|
Switch the sleep/wakeup and condition variable implementations to use the
sleep queue interface:
- Sleep queues attempt to merge some of the benefits of both sleep queues
and condition variables. Having sleep qeueus in a hash table avoids
having to allocate a queue head for each wait channel. Thus, struct cv
has shrunk down to just a single char * pointer now. However, the
hash table does not hold threads directly, but queue heads. This means
that once you have located a queue in the hash bucket, you no longer have
to walk the rest of the hash chain looking for threads. Instead, you have
a list of all the threads sleeping on that wait channel.
- Outside of the sleepq code and the sleep/cv code the kernel no longer
differentiates between cv's and sleep/wakeup. For example, calls to
abortsleep() and cv_abort() are replaced with a call to sleepq_abort().
Thus, the TDF_CVWAITQ flag is removed. Also, calls to unsleep() and
cv_waitq_remove() have been replaced with calls to sleepq_remove().
- The sched_sleep() function no longer accepts a priority argument as
sleep's no longer inherently bump the priority. Instead, this is soley
a propery of msleep() which explicitly calls sched_prio() before
blocking.
- The TDF_ONSLEEPQ flag has been dropped as it was never used. The
associated TDF_SET_ONSLEEPQ and TDF_CLR_ON_SLEEPQ macros have also been
dropped and replaced with a single explicit clearing of td_wchan.
TD_SET_ONSLEEPQ() would really have only made sense if it had taken
the wait channel and message as arguments anyway. Now that that only
happens in one place, a macro would be overkill.
2004-02-27 18:52:44 +00:00
|
|
|
if (timo)
|
2004-03-12 19:06:18 +00:00
|
|
|
sleepq_set_timeout(ident, timo);
|
2002-10-12 05:32:24 +00:00
|
|
|
|
|
|
|
/*
|
2006-11-30 08:27:38 +00:00
|
|
|
* Adjust this thread's priority, if necessary.
|
2002-10-12 05:32:24 +00:00
|
|
|
*/
|
2006-11-30 08:27:38 +00:00
|
|
|
pri = priority & PRIMASK;
|
|
|
|
if (pri != 0 && pri != td->td_priority) {
|
2006-04-17 18:20:38 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
2006-11-30 08:27:38 +00:00
|
|
|
sched_prio(td, pri);
|
2006-04-17 18:20:38 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
}
|
2002-10-12 05:32:24 +00:00
|
|
|
|
Switch the sleep/wakeup and condition variable implementations to use the
sleep queue interface:
- Sleep queues attempt to merge some of the benefits of both sleep queues
and condition variables. Having sleep qeueus in a hash table avoids
having to allocate a queue head for each wait channel. Thus, struct cv
has shrunk down to just a single char * pointer now. However, the
hash table does not hold threads directly, but queue heads. This means
that once you have located a queue in the hash bucket, you no longer have
to walk the rest of the hash chain looking for threads. Instead, you have
a list of all the threads sleeping on that wait channel.
- Outside of the sleepq code and the sleep/cv code the kernel no longer
differentiates between cv's and sleep/wakeup. For example, calls to
abortsleep() and cv_abort() are replaced with a call to sleepq_abort().
Thus, the TDF_CVWAITQ flag is removed. Also, calls to unsleep() and
cv_waitq_remove() have been replaced with calls to sleepq_remove().
- The sched_sleep() function no longer accepts a priority argument as
sleep's no longer inherently bump the priority. Instead, this is soley
a propery of msleep() which explicitly calls sched_prio() before
blocking.
- The TDF_ONSLEEPQ flag has been dropped as it was never used. The
associated TDF_SET_ONSLEEPQ and TDF_CLR_ON_SLEEPQ macros have also been
dropped and replaced with a single explicit clearing of td_wchan.
TD_SET_ONSLEEPQ() would really have only made sense if it had taken
the wait channel and message as arguments anyway. Now that that only
happens in one place, a macro would be overkill.
2004-02-27 18:52:44 +00:00
|
|
|
if (timo && catch)
|
Fix a long standing race between sleep queue and thread
suspension code. When a thread A is going to sleep, it calls
sleepq_catch_signals() to detect any pending signals or thread
suspension request, if nothing happens, it returns without
holding process lock or scheduler lock, this opens a race
window which allows thread B to come in and do process
suspension work, however since A is still at running state,
thread B can do nothing to A, thread A continues, and puts
itself into actually sleeping state, but B has never seen it,
and it sits there forever until B is woken up by other threads
sometimes later(this can be very long delay or never
happen). Fix this bug by forcing sleepq_catch_signals to
return with scheduler lock held.
Fix sleepq_abort() by passing it an interrupted code, previously,
it worked as wakeup_one(), and the interruption can not be
identified correctly by sleep queue code when the sleeping
thread is resumed.
Let thread_suspend_check() returns EINTR or ERESTART, so sleep
queue no longer has to use SIGSTOP as a hack to build a return
value.
Reviewed by: jhb
MFC after: 1 week
2006-02-15 23:52:01 +00:00
|
|
|
rval = sleepq_timedwait_sig(ident);
|
Switch the sleep/wakeup and condition variable implementations to use the
sleep queue interface:
- Sleep queues attempt to merge some of the benefits of both sleep queues
and condition variables. Having sleep qeueus in a hash table avoids
having to allocate a queue head for each wait channel. Thus, struct cv
has shrunk down to just a single char * pointer now. However, the
hash table does not hold threads directly, but queue heads. This means
that once you have located a queue in the hash bucket, you no longer have
to walk the rest of the hash chain looking for threads. Instead, you have
a list of all the threads sleeping on that wait channel.
- Outside of the sleepq code and the sleep/cv code the kernel no longer
differentiates between cv's and sleep/wakeup. For example, calls to
abortsleep() and cv_abort() are replaced with a call to sleepq_abort().
Thus, the TDF_CVWAITQ flag is removed. Also, calls to unsleep() and
cv_waitq_remove() have been replaced with calls to sleepq_remove().
- The sched_sleep() function no longer accepts a priority argument as
sleep's no longer inherently bump the priority. Instead, this is soley
a propery of msleep() which explicitly calls sched_prio() before
blocking.
- The TDF_ONSLEEPQ flag has been dropped as it was never used. The
associated TDF_SET_ONSLEEPQ and TDF_CLR_ON_SLEEPQ macros have also been
dropped and replaced with a single explicit clearing of td_wchan.
TD_SET_ONSLEEPQ() would really have only made sense if it had taken
the wait channel and message as arguments anyway. Now that that only
happens in one place, a macro would be overkill.
2004-02-27 18:52:44 +00:00
|
|
|
else if (timo)
|
2004-06-28 18:57:06 +00:00
|
|
|
rval = sleepq_timedwait(ident);
|
Switch the sleep/wakeup and condition variable implementations to use the
sleep queue interface:
- Sleep queues attempt to merge some of the benefits of both sleep queues
and condition variables. Having sleep qeueus in a hash table avoids
having to allocate a queue head for each wait channel. Thus, struct cv
has shrunk down to just a single char * pointer now. However, the
hash table does not hold threads directly, but queue heads. This means
that once you have located a queue in the hash bucket, you no longer have
to walk the rest of the hash chain looking for threads. Instead, you have
a list of all the threads sleeping on that wait channel.
- Outside of the sleepq code and the sleep/cv code the kernel no longer
differentiates between cv's and sleep/wakeup. For example, calls to
abortsleep() and cv_abort() are replaced with a call to sleepq_abort().
Thus, the TDF_CVWAITQ flag is removed. Also, calls to unsleep() and
cv_waitq_remove() have been replaced with calls to sleepq_remove().
- The sched_sleep() function no longer accepts a priority argument as
sleep's no longer inherently bump the priority. Instead, this is soley
a propery of msleep() which explicitly calls sched_prio() before
blocking.
- The TDF_ONSLEEPQ flag has been dropped as it was never used. The
associated TDF_SET_ONSLEEPQ and TDF_CLR_ON_SLEEPQ macros have also been
dropped and replaced with a single explicit clearing of td_wchan.
TD_SET_ONSLEEPQ() would really have only made sense if it had taken
the wait channel and message as arguments anyway. Now that that only
happens in one place, a macro would be overkill.
2004-02-27 18:52:44 +00:00
|
|
|
else if (catch)
|
|
|
|
rval = sleepq_wait_sig(ident);
|
|
|
|
else {
|
|
|
|
sleepq_wait(ident);
|
|
|
|
rval = 0;
|
2001-06-22 23:11:26 +00:00
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
#ifdef KTRACE
|
2002-06-07 05:39:16 +00:00
|
|
|
if (KTRPOINT(td, KTR_CSW))
|
|
|
|
ktrcsw(0, 0);
|
1994-05-24 10:09:53 +00:00
|
|
|
#endif
|
2002-06-07 05:39:16 +00:00
|
|
|
PICKUP_GIANT();
|
2004-03-02 14:58:33 +00:00
|
|
|
if (mtx != NULL && !(priority & PDROP)) {
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_lock(mtx);
|
Rework the witness code to work with sx locks as well as mutexes.
- Introduce lock classes and lock objects. Each lock class specifies a
name and set of flags (or properties) shared by all locks of a given
type. Currently there are three lock classes: spin mutexes, sleep
mutexes, and sx locks. A lock object specifies properties of an
additional lock along with a lock name and all of the extra stuff needed
to make witness work with a given lock. This abstract lock stuff is
defined in sys/lock.h. The lockmgr constants, types, and prototypes have
been moved to sys/lockmgr.h. For temporary backwards compatability,
sys/lock.h includes sys/lockmgr.h.
- Replace proc->p_spinlocks with a per-CPU list, PCPU(spinlocks), of spin
locks held. By making this per-cpu, we do not have to jump through
magic hoops to deal with sched_lock changing ownership during context
switches.
- Replace proc->p_heldmtx, formerly a list of held sleep mutexes, with
proc->p_sleeplocks, which is a list of held sleep locks including sleep
mutexes and sx locks.
- Add helper macros for logging lock events via the KTR_LOCK KTR logging
level so that the log messages are consistent.
- Add some new flags that can be passed to mtx_init():
- MTX_NOWITNESS - specifies that this lock should be ignored by witness.
This is used for the mutex that blocks a sx lock for example.
- MTX_QUIET - this is not new, but you can pass this to mtx_init() now
and no events will be logged for this lock, so that one doesn't have
to change all the individual mtx_lock/unlock() operations.
- All lock objects maintain an initialized flag. Use this flag to export
a mtx_initialized() macro that can be safely called from drivers. Also,
we on longer walk the all_mtx list if MUTEX_DEBUG is defined as witness
performs the corresponding checks using the initialized flag.
- The lock order reversal messages have been improved to output slightly
more accurate file and line numbers.
2001-03-28 09:03:24 +00:00
|
|
|
WITNESS_RESTORE(&mtx->mtx_object, mtx);
|
2000-09-11 00:20:02 +00:00
|
|
|
}
|
2000-09-07 01:33:02 +00:00
|
|
|
return (rval);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
2005-12-29 20:57:45 +00:00
|
|
|
int
|
|
|
|
msleep_spin(ident, mtx, wmesg, timo)
|
|
|
|
void *ident;
|
|
|
|
struct mtx *mtx;
|
|
|
|
const char *wmesg;
|
|
|
|
int timo;
|
|
|
|
{
|
|
|
|
struct thread *td;
|
|
|
|
struct proc *p;
|
|
|
|
int rval;
|
|
|
|
WITNESS_SAVE_DECL(mtx);
|
|
|
|
|
|
|
|
td = curthread;
|
|
|
|
p = td->td_proc;
|
|
|
|
KASSERT(mtx != NULL, ("sleeping without a mutex"));
|
|
|
|
KASSERT(p != NULL, ("msleep1"));
|
|
|
|
KASSERT(ident != NULL && TD_IS_RUNNING(td), ("msleep"));
|
|
|
|
|
|
|
|
if (cold) {
|
|
|
|
/*
|
|
|
|
* During autoconfiguration, just return;
|
|
|
|
* don't run any other threads or panic below,
|
|
|
|
* in case this is the idle thread and already asleep.
|
|
|
|
* XXX: this used to do "s = splhigh(); splx(safepri);
|
|
|
|
* splx(s);" to give interrupts a chance, but there is
|
|
|
|
* no way to give interrupts a chance now.
|
|
|
|
*/
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
sleepq_lock(ident);
|
|
|
|
CTR5(KTR_PROC, "msleep_spin: thread %p (pid %ld, %s) on %s (%p)",
|
|
|
|
(void *)td, (long)p->p_pid, p->p_comm, wmesg, ident);
|
|
|
|
|
|
|
|
DROP_GIANT();
|
|
|
|
mtx_assert(mtx, MA_OWNED | MA_NOTRECURSED);
|
|
|
|
WITNESS_SAVE(&mtx->mtx_object, mtx);
|
|
|
|
mtx_unlock_spin(mtx);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We put ourselves on the sleep queue and start our timeout.
|
|
|
|
*/
|
2006-12-16 06:54:09 +00:00
|
|
|
sleepq_add(ident, &mtx->mtx_object, wmesg, SLEEPQ_MSLEEP, 0);
|
2005-12-29 20:57:45 +00:00
|
|
|
if (timo)
|
|
|
|
sleepq_set_timeout(ident, timo);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Can't call ktrace with any spin locks held so it can lock the
|
|
|
|
* ktrace_mtx lock, and WITNESS_WARN considers it an error to hold
|
|
|
|
* any spin lock. Thus, we have to drop the sleepq spin lock while
|
|
|
|
* we handle those requests. This is safe since we have placed our
|
|
|
|
* thread on the sleep queue already.
|
|
|
|
*/
|
|
|
|
#ifdef KTRACE
|
|
|
|
if (KTRPOINT(td, KTR_CSW)) {
|
|
|
|
sleepq_release(ident);
|
|
|
|
ktrcsw(1, 0);
|
|
|
|
sleepq_lock(ident);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef WITNESS
|
|
|
|
sleepq_release(ident);
|
|
|
|
WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "Sleeping on \"%s\"",
|
|
|
|
wmesg);
|
|
|
|
sleepq_lock(ident);
|
|
|
|
#endif
|
|
|
|
if (timo)
|
|
|
|
rval = sleepq_timedwait(ident);
|
|
|
|
else {
|
|
|
|
sleepq_wait(ident);
|
|
|
|
rval = 0;
|
|
|
|
}
|
|
|
|
#ifdef KTRACE
|
|
|
|
if (KTRPOINT(td, KTR_CSW))
|
|
|
|
ktrcsw(0, 0);
|
|
|
|
#endif
|
|
|
|
PICKUP_GIANT();
|
|
|
|
mtx_lock_spin(mtx);
|
|
|
|
WITNESS_RESTORE(&mtx->mtx_object, mtx);
|
|
|
|
return (rval);
|
|
|
|
}
|
|
|
|
|
2007-02-23 16:22:09 +00:00
|
|
|
/*
|
|
|
|
* pause() is like tsleep() except that the intention is to not be
|
|
|
|
* explicitly woken up by another thread. Instead, the current thread
|
|
|
|
* simply wishes to sleep until the timeout expires. It is
|
|
|
|
* implemented using a dummy wait channel.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pause(wmesg, timo)
|
|
|
|
const char *wmesg;
|
|
|
|
int timo;
|
|
|
|
{
|
|
|
|
|
|
|
|
KASSERT(timo != 0, ("pause: timeout required"));
|
|
|
|
return (tsleep(&pause_wchan, 0, wmesg, timo));
|
|
|
|
}
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
2004-05-14 20:51:42 +00:00
|
|
|
* Make all threads sleeping on the specified identifier runnable.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
void
|
2001-07-06 01:16:43 +00:00
|
|
|
wakeup(ident)
|
1994-05-24 10:09:53 +00:00
|
|
|
register void *ident;
|
|
|
|
{
|
|
|
|
|
2004-10-12 18:36:20 +00:00
|
|
|
sleepq_lock(ident);
|
2006-12-16 06:54:09 +00:00
|
|
|
sleepq_broadcast(ident, SLEEPQ_MSLEEP, -1, 0);
|
1996-07-31 09:26:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-05-14 20:51:42 +00:00
|
|
|
* Make a thread sleeping on the specified identifier runnable.
|
|
|
|
* May wake more than one thread if a target thread is currently
|
2001-07-06 01:16:43 +00:00
|
|
|
* swapped out.
|
1996-07-31 09:26:54 +00:00
|
|
|
*/
|
|
|
|
void
|
2001-07-06 01:16:43 +00:00
|
|
|
wakeup_one(ident)
|
1996-07-31 09:26:54 +00:00
|
|
|
register void *ident;
|
|
|
|
{
|
|
|
|
|
2004-10-12 18:36:20 +00:00
|
|
|
sleepq_lock(ident);
|
2006-12-16 06:54:09 +00:00
|
|
|
sleepq_signal(ident, SLEEPQ_MSLEEP, -1, 0);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-05-14 20:51:42 +00:00
|
|
|
* The machine independent parts of context switching.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
void
|
2004-07-02 19:09:50 +00:00
|
|
|
mi_switch(int flags, struct thread *newtd)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2006-02-07 21:22:02 +00:00
|
|
|
uint64_t new_switchtime;
|
2003-04-02 23:53:30 +00:00
|
|
|
struct thread *td;
|
|
|
|
struct proc *p;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2001-01-24 11:10:55 +00:00
|
|
|
mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
|
2003-04-02 23:53:30 +00:00
|
|
|
td = curthread; /* XXX */
|
|
|
|
p = td->td_proc; /* XXX */
|
2002-09-11 08:13:56 +00:00
|
|
|
KASSERT(!TD_ON_RUNQ(td), ("mi_switch: called by old code"));
|
2001-10-23 17:52:49 +00:00
|
|
|
#ifdef INVARIANTS
|
2003-05-05 21:12:36 +00:00
|
|
|
if (!TD_ON_LOCK(td) && !TD_IS_RUNNING(td))
|
2001-10-23 17:52:49 +00:00
|
|
|
mtx_assert(&Giant, MA_NOTOWNED);
|
|
|
|
#endif
|
2004-07-02 20:21:44 +00:00
|
|
|
KASSERT(td->td_critnest == 1 || (td->td_critnest == 2 &&
|
2005-04-08 03:37:53 +00:00
|
|
|
(td->td_owepreempt) && (flags & SW_INVOL) != 0 &&
|
2005-03-31 20:36:44 +00:00
|
|
|
newtd == NULL) || panicstr,
|
2002-07-17 02:46:13 +00:00
|
|
|
("mi_switch: switch in a critical section"));
|
2004-01-25 03:54:52 +00:00
|
|
|
KASSERT((flags & (SW_INVOL | SW_VOL)) != 0,
|
|
|
|
("mi_switch: switch must be voluntary or involuntary"));
|
2004-07-16 21:04:55 +00:00
|
|
|
KASSERT(newtd != curthread, ("mi_switch: preempting back to ourself"));
|
2000-09-07 01:33:02 +00:00
|
|
|
|
2006-06-03 20:49:44 +00:00
|
|
|
/*
|
|
|
|
* Don't perform context switches from the debugger.
|
|
|
|
*/
|
|
|
|
if (kdb_active) {
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
kdb_backtrace();
|
|
|
|
kdb_reenter();
|
|
|
|
panic("%s: did not reenter debugger", __func__);
|
|
|
|
}
|
|
|
|
|
2004-01-25 03:54:52 +00:00
|
|
|
if (flags & SW_VOL)
|
|
|
|
p->p_stats->p_ru.ru_nvcsw++;
|
|
|
|
else
|
|
|
|
p->p_stats->p_ru.ru_nivcsw++;
|
2004-07-02 20:21:44 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* Compute the amount of time during which the current
|
|
|
|
* process was running, and add that to its total so far.
|
|
|
|
*/
|
2006-02-07 21:22:02 +00:00
|
|
|
new_switchtime = cpu_ticks();
|
|
|
|
p->p_rux.rux_runtime += (new_switchtime - PCPU_GET(switchtime));
|
2006-02-11 09:33:07 +00:00
|
|
|
p->p_rux.rux_uticks += td->td_uticks;
|
|
|
|
td->td_uticks = 0;
|
|
|
|
p->p_rux.rux_iticks += td->td_iticks;
|
|
|
|
td->td_iticks = 0;
|
|
|
|
p->p_rux.rux_sticks += td->td_sticks;
|
|
|
|
td->td_sticks = 0;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2003-10-05 09:35:08 +00:00
|
|
|
td->td_generation++; /* bump preempt-detect counter */
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
2002-10-01 14:10:08 +00:00
|
|
|
* Check if the process exceeds its cpu resource allocation. If
|
2005-11-28 19:09:08 +00:00
|
|
|
* it reaches the max, arrange to kill the process in ast().
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
2002-10-09 17:17:24 +00:00
|
|
|
if (p->p_cpulimit != RLIM_INFINITY &&
|
2006-02-07 21:22:02 +00:00
|
|
|
p->p_rux.rux_runtime >= p->p_cpulimit * cpu_tickrate()) {
|
2002-09-30 21:13:54 +00:00
|
|
|
p->p_sflag |= PS_XCPU;
|
2003-02-17 09:55:10 +00:00
|
|
|
td->td_flags |= TDF_ASTPENDING;
|
2002-10-01 14:10:08 +00:00
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
/*
|
2002-08-01 18:45:10 +00:00
|
|
|
* Finish up stats for outgoing thread.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
cnt.v_swtch++;
|
2001-01-10 04:43:51 +00:00
|
|
|
PCPU_SET(switchtime, new_switchtime);
|
2003-10-29 15:23:09 +00:00
|
|
|
PCPU_SET(switchticks, ticks);
|
2004-08-09 18:21:12 +00:00
|
|
|
CTR4(KTR_PROC, "mi_switch: old thread %p (kse %p, pid %ld, %s)",
|
2004-09-05 02:09:54 +00:00
|
|
|
(void *)td, td->td_sched, (long)p->p_pid, p->p_comm);
|
2004-12-26 00:14:21 +00:00
|
|
|
#if (KTR_COMPILE & KTR_SCHED) != 0
|
|
|
|
if (td == PCPU_GET(idlethread))
|
|
|
|
CTR3(KTR_SCHED, "mi_switch: %p(%s) prio %d idle",
|
|
|
|
td, td->td_proc->p_comm, td->td_priority);
|
|
|
|
else if (newtd != NULL)
|
|
|
|
CTR5(KTR_SCHED,
|
|
|
|
"mi_switch: %p(%s) prio %d preempted by %p(%s)",
|
|
|
|
td, td->td_proc->p_comm, td->td_priority, newtd,
|
|
|
|
newtd->td_proc->p_comm);
|
|
|
|
else
|
|
|
|
CTR6(KTR_SCHED,
|
|
|
|
"mi_switch: %p(%s) prio %d inhibit %d wmesg %s lock %s",
|
|
|
|
td, td->td_proc->p_comm, td->td_priority,
|
|
|
|
td->td_inhibitors, td->td_wmesg, td->td_lockname);
|
2007-01-03 02:38:41 +00:00
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* We call thread_switchout after the KTR_SCHED prints above so kse
|
|
|
|
* selecting a new thread to run does not show up as a preemption.
|
|
|
|
*/
|
|
|
|
#ifdef KSE
|
|
|
|
if ((flags & SW_VOL) && (td->td_proc->p_flag & P_SA))
|
|
|
|
newtd = thread_switchout(td, flags, newtd);
|
2004-12-26 00:14:21 +00:00
|
|
|
#endif
|
2004-09-10 21:04:38 +00:00
|
|
|
sched_switch(td, newtd, flags);
|
2004-12-26 00:14:21 +00:00
|
|
|
CTR3(KTR_SCHED, "mi_switch: running %p(%s) prio %d",
|
|
|
|
td, td->td_proc->p_comm, td->td_priority);
|
2002-10-12 05:32:24 +00:00
|
|
|
|
2004-08-09 18:21:12 +00:00
|
|
|
CTR4(KTR_PROC, "mi_switch: new thread %p (kse %p, pid %ld, %s)",
|
2004-09-05 02:09:54 +00:00
|
|
|
(void *)td, td->td_sched, (long)p->p_pid, p->p_comm);
|
2003-10-29 15:23:09 +00:00
|
|
|
|
2002-12-10 02:33:45 +00:00
|
|
|
/*
|
|
|
|
* If the last thread was exiting, finish cleaning it up.
|
|
|
|
*/
|
|
|
|
if ((td = PCPU_GET(deadthread))) {
|
|
|
|
PCPU_SET(deadthread, NULL);
|
|
|
|
thread_stash(td);
|
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Change process state to be runnable,
|
|
|
|
* placing it on the run queue if it is in memory,
|
|
|
|
* and awakening the swapper if it isn't in memory.
|
|
|
|
*/
|
|
|
|
void
|
2001-09-12 08:38:13 +00:00
|
|
|
setrunnable(struct thread *td)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2003-08-15 21:29:06 +00:00
|
|
|
struct proc *p;
|
2001-09-21 19:16:12 +00:00
|
|
|
|
2003-08-15 21:29:06 +00:00
|
|
|
p = td->td_proc;
|
2002-07-03 09:15:20 +00:00
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
switch (p->p_state) {
|
|
|
|
case PRS_ZOMBIE:
|
2001-09-21 19:16:12 +00:00
|
|
|
panic("setrunnable(1)");
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
default:
|
|
|
|
break;
|
2001-09-12 08:38:13 +00:00
|
|
|
}
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
switch (td->td_state) {
|
|
|
|
case TDS_RUNNING:
|
2002-09-11 08:13:56 +00:00
|
|
|
case TDS_RUNQ:
|
|
|
|
return;
|
|
|
|
case TDS_INHIBITED:
|
|
|
|
/*
|
|
|
|
* If we are only inhibited because we are swapped out
|
|
|
|
* then arange to swap in this process. Otherwise just return.
|
|
|
|
*/
|
|
|
|
if (td->td_inhibitors != TDI_SWAPPED)
|
|
|
|
return;
|
2003-05-31 20:13:58 +00:00
|
|
|
/* XXX: intentional fall-through ? */
|
2002-09-11 08:13:56 +00:00
|
|
|
case TDS_CAN_RUN:
|
|
|
|
break;
|
1994-05-24 10:09:53 +00:00
|
|
|
default:
|
2002-09-11 08:13:56 +00:00
|
|
|
printf("state is 0x%x", td->td_state);
|
2001-09-12 08:38:13 +00:00
|
|
|
panic("setrunnable(2)");
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
2001-01-24 11:10:55 +00:00
|
|
|
if ((p->p_sflag & PS_INMEM) == 0) {
|
2002-07-30 06:54:05 +00:00
|
|
|
if ((p->p_sflag & PS_SWAPPINGIN) == 0) {
|
|
|
|
p->p_sflag |= PS_SWAPINREQ;
|
2004-08-04 20:24:40 +00:00
|
|
|
/*
|
2004-10-16 06:38:22 +00:00
|
|
|
* due to a LOR between sched_lock and
|
2005-05-23 23:01:53 +00:00
|
|
|
* the sleepqueue chain locks, use
|
|
|
|
* lower level scheduling functions.
|
2004-08-04 20:24:40 +00:00
|
|
|
*/
|
2005-05-23 23:01:53 +00:00
|
|
|
kick_proc0();
|
2002-07-30 06:54:05 +00:00
|
|
|
}
|
2002-10-12 05:32:24 +00:00
|
|
|
} else
|
|
|
|
sched_wakeup(td);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
1997-11-25 07:07:48 +00:00
|
|
|
|
2001-10-20 13:10:43 +00:00
|
|
|
/*
|
|
|
|
* Compute a tenex style load average of a quantity on
|
|
|
|
* 1, 5 and 15 minute intervals.
|
|
|
|
* XXXKSE Needs complete rewrite when correct info is available.
|
|
|
|
* Completely Bogus.. only works with 1:1 (but compiles ok now :-)
|
|
|
|
*/
|
|
|
|
static void
|
2004-02-01 05:37:36 +00:00
|
|
|
loadav(void *arg)
|
2001-10-20 13:10:43 +00:00
|
|
|
{
|
|
|
|
int i, nrun;
|
2001-10-20 16:07:17 +00:00
|
|
|
struct loadavg *avg;
|
2001-10-20 13:10:43 +00:00
|
|
|
|
2004-02-01 02:51:33 +00:00
|
|
|
nrun = sched_load();
|
2001-10-20 16:07:17 +00:00
|
|
|
avg = &averunnable;
|
2004-02-01 02:51:33 +00:00
|
|
|
|
2001-10-20 13:10:43 +00:00
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
|
|
|
|
nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
|
2001-10-20 16:07:17 +00:00
|
|
|
|
2004-02-01 05:37:36 +00:00
|
|
|
/*
|
|
|
|
* Schedule the next update to occur after 5 seconds, but add a
|
|
|
|
* random variation to avoid synchronisation with processes that
|
|
|
|
* run at regular intervals.
|
|
|
|
*/
|
|
|
|
callout_reset(&loadav_callout, hz * 4 + (int)(random() % (hz * 2 + 1)),
|
|
|
|
loadav, NULL);
|
2001-10-20 13:10:43 +00:00
|
|
|
}
|
|
|
|
|
2002-11-21 08:57:08 +00:00
|
|
|
static void
|
|
|
|
lboltcb(void *arg)
|
|
|
|
{
|
|
|
|
wakeup(&lbolt);
|
|
|
|
callout_reset(&lbolt_callout, hz, lboltcb, NULL);
|
|
|
|
}
|
|
|
|
|
1997-11-25 07:07:48 +00:00
|
|
|
/* ARGSUSED */
|
|
|
|
static void
|
2004-01-25 07:49:45 +00:00
|
|
|
synch_setup(dummy)
|
1997-11-25 07:07:48 +00:00
|
|
|
void *dummy;
|
|
|
|
{
|
2004-03-08 22:01:19 +00:00
|
|
|
callout_init(&loadav_callout, CALLOUT_MPSAFE);
|
2003-08-19 17:51:11 +00:00
|
|
|
callout_init(&lbolt_callout, CALLOUT_MPSAFE);
|
2000-11-27 22:52:31 +00:00
|
|
|
|
1997-11-25 07:07:48 +00:00
|
|
|
/* Kick off timeout driven events by calling first time. */
|
2004-02-01 05:37:36 +00:00
|
|
|
loadav(NULL);
|
2002-11-21 08:57:08 +00:00
|
|
|
lboltcb(NULL);
|
1997-11-25 07:07:48 +00:00
|
|
|
}
|
|
|
|
|
2000-12-02 05:41:30 +00:00
|
|
|
/*
|
|
|
|
* General purpose yield system call
|
|
|
|
*/
|
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
yield(struct thread *td, struct yield_args *uap)
|
2000-12-02 05:41:30 +00:00
|
|
|
{
|
2001-09-01 03:54:09 +00:00
|
|
|
mtx_assert(&Giant, MA_NOTOWNED);
|
2006-06-15 06:41:57 +00:00
|
|
|
(void)uap;
|
|
|
|
sched_relinquish(td);
|
2000-12-02 05:41:30 +00:00
|
|
|
return (0);
|
|
|
|
}
|