1 /* $NetBSD: kern_sleepq.c,v 1.36 2009/03/21 13:11:14 ad Exp $ */
4 * Copyright (c) 2006, 2007, 2008, 2009 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Sleep queue implementation, used by turnstiles and general sleep/wakeup
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: kern_sleepq.c,v 1.36 2009/03/21 13:11:14 ad Exp $");
40 #include <sys/param.h>
41 #include <sys/kernel.h>
45 #include <sys/resourcevar.h>
47 #include <sys/savar.h>
48 #include <sys/sched.h>
49 #include <sys/systm.h>
50 #include <sys/sleepq.h>
51 #include <sys/ktrace.h>
53 #include <uvm/uvm_extern.h>
57 int sleepq_sigtoerror(lwp_t
*, int);
59 /* General purpose sleep table, used by ltsleep() and condition variables. */
65 * Initialize a sleep table.
68 sleeptab_init(sleeptab_t
*st
)
73 for (i
= 0; i
< SLEEPTAB_HASH_SIZE
; i
++) {
74 sq
= &st
->st_queues
[i
].st_queue
;
75 st
->st_queues
[i
].st_mutex
=
76 mutex_obj_alloc(MUTEX_DEFAULT
, IPL_SCHED
);
84 * Prepare a sleep queue for use.
87 sleepq_init(sleepq_t
*sq
)
96 * Remove an LWP from a sleep queue and wake it up.
99 sleepq_remove(sleepq_t
*sq
, lwp_t
*l
)
101 struct schedstate_percpu
*spc
;
104 KASSERT(lwp_locked(l
, NULL
));
106 TAILQ_REMOVE(sq
, l
, l_sleepchain
);
107 l
->l_syncobj
= &sched_syncobj
;
110 l
->l_flag
&= ~LW_SINTR
;
113 spc
= &ci
->ci_schedstate
;
116 * If not sleeping, the LWP must have been suspended. Let whoever
117 * holds it stopped set it running again.
119 if (l
->l_stat
!= LSSLEEP
) {
120 KASSERT(l
->l_stat
== LSSTOP
|| l
->l_stat
== LSSUSPENDED
);
121 lwp_setlock(l
, spc
->spc_lwplock
);
126 * If the LWP is still on the CPU, mark it as LSONPROC. It may be
127 * about to call mi_switch(), in which case it will yield.
129 if ((l
->l_pflag
& LP_RUNNING
) != 0) {
130 l
->l_stat
= LSONPROC
;
132 lwp_setlock(l
, spc
->spc_lwplock
);
136 /* Update sleep time delta, call the wake-up handler of scheduler */
137 l
->l_slpticksum
+= (hardclock_ticks
- l
->l_slpticks
);
140 /* Look for a CPU to wake up */
141 l
->l_cpu
= sched_takecpu(l
);
143 spc
= &ci
->ci_schedstate
;
149 lwp_setlock(l
, spc
->spc_mutex
);
151 if (l
->l_proc
->p_sa
!= NULL
)
154 sched_setrunnable(l
);
157 sched_enqueue(l
, false);
164 * Insert an LWP into the sleep queue, optionally sorting by priority.
167 sleepq_insert(sleepq_t
*sq
, lwp_t
*l
, syncobj_t
*sobj
)
170 const int pri
= lwp_eprio(l
);
172 if ((sobj
->sobj_flag
& SOBJ_SLEEPQ_SORTED
) != 0) {
173 TAILQ_FOREACH(l2
, sq
, l_sleepchain
) {
174 if (lwp_eprio(l2
) < pri
) {
175 TAILQ_INSERT_BEFORE(l2
, l
, l_sleepchain
);
181 if ((sobj
->sobj_flag
& SOBJ_SLEEPQ_LIFO
) != 0)
182 TAILQ_INSERT_HEAD(sq
, l
, l_sleepchain
);
184 TAILQ_INSERT_TAIL(sq
, l
, l_sleepchain
);
190 * Enter an LWP into the sleep queue and prepare for sleep. The sleep
191 * queue must already be locked, and any interlock (such as the kernel
192 * lock) must have be released (see sleeptab_lookup(), sleepq_enter()).
195 sleepq_enqueue(sleepq_t
*sq
, wchan_t wchan
, const char *wmesg
, syncobj_t
*sobj
)
199 KASSERT(lwp_locked(l
, NULL
));
200 KASSERT(l
->l_stat
== LSONPROC
);
201 KASSERT(l
->l_wchan
== NULL
&& l
->l_sleepq
== NULL
);
211 sleepq_insert(sq
, l
, sobj
);
213 /* Save the time when thread has slept */
214 l
->l_slpticks
= hardclock_ticks
;
221 * After any intermediate step such as releasing an interlock, switch.
222 * sleepq_block() may return early under exceptional conditions, for
223 * example if the LWP's containing process is exiting.
226 sleepq_block(int timo
, bool catch)
232 int biglocks
= l
->l_biglocks
;
237 * If sleeping interruptably, check for pending signals, exits or
241 l
->l_flag
|= LW_SINTR
;
242 if ((l
->l_flag
& (LW_CANCELLED
|LW_WEXIT
|LW_WCORE
)) != 0) {
243 l
->l_flag
&= ~LW_CANCELLED
;
246 } else if ((l
->l_flag
& LW_PENDSIG
) != 0 && sigispending(l
, 0))
251 /* lwp_unsleep() will release the lock */
252 lwp_unsleep(l
, true);
255 callout_schedule(&l
->l_timeout_ch
, timo
);
258 if (((l
->l_flag
& LW_SA
) != 0) && (~l
->l_pflag
& LP_SA_NOBLOCK
))
264 /* The LWP and sleep queue are now unlocked. */
267 * Even if the callout appears to have fired, we need to
268 * stop it in order to synchronise with other CPUs.
270 if (callout_halt(&l
->l_timeout_ch
, NULL
))
275 if (catch && error
== 0) {
277 if ((l
->l_flag
& (LW_CANCELLED
| LW_WEXIT
| LW_WCORE
)) != 0)
279 else if ((l
->l_flag
& LW_PENDSIG
) != 0) {
281 * Acquiring p_lock may cause us to recurse
282 * through the sleep path and back into this
283 * routine, but is safe because LWPs sleeping
284 * on locks are non-interruptable. We will
287 mutex_enter(p
->p_lock
);
288 if ((sig
= issignal(l
)) != 0)
289 error
= sleepq_sigtoerror(l
, sig
);
290 mutex_exit(p
->p_lock
);
295 if (__predict_false(biglocks
!= 0)) {
296 KERNEL_LOCK(biglocks
, NULL
);
304 * Wake zero or more LWPs blocked on a single wait channel.
307 sleepq_wake(sleepq_t
*sq
, wchan_t wchan
, u_int expected
, kmutex_t
*mp
)
311 KASSERT(mutex_owned(mp
));
313 for (l
= TAILQ_FIRST(sq
); l
!= NULL
; l
= next
) {
314 KASSERT(l
->l_sleepq
== sq
);
315 KASSERT(l
->l_mutex
== mp
);
316 next
= TAILQ_NEXT(l
, l_sleepchain
);
317 if (l
->l_wchan
!= wchan
)
319 sleepq_remove(sq
, l
);
331 * Remove an LWP from its sleep queue and set it runnable again.
332 * sleepq_unsleep() is called with the LWP's mutex held, and will
336 sleepq_unsleep(lwp_t
*l
, bool cleanup
)
338 sleepq_t
*sq
= l
->l_sleepq
;
339 kmutex_t
*mp
= l
->l_mutex
;
341 KASSERT(lwp_locked(l
, mp
));
342 KASSERT(l
->l_wchan
!= NULL
);
344 sleepq_remove(sq
, l
);
353 * Entered via the callout(9) subsystem to time out an LWP that is on a
357 sleepq_timeout(void *arg
)
362 * Lock the LWP. Assuming it's still on the sleep queue, its
363 * current mutex will also be the sleep queue mutex.
367 if (l
->l_wchan
== NULL
) {
368 /* Somebody beat us to it. */
373 lwp_unsleep(l
, true);
379 * Given a signal number, interpret and return an error code.
382 sleepq_sigtoerror(lwp_t
*l
, int sig
)
384 struct proc
*p
= l
->l_proc
;
387 KASSERT(mutex_owned(p
->p_lock
));
390 * If this sleep was canceled, don't let the syscall restart.
392 if ((SIGACTION(p
, sig
).sa_flags
& SA_RESTART
) == 0)
403 * After a panic or during autoconfiguration, lower the interrupt
404 * priority level to give pending interrupts a chance to run, and
405 * then return. Called if sleepq_dontsleep() returns non-zero, and
406 * always returns zero.
409 sleepq_abort(kmutex_t
*mtx
, int unlock
)
417 if (mtx
!= NULL
&& unlock
!= 0)
426 * Adjust the priority of an LWP residing on a sleepq. This method
427 * will only alter the user priority; the effective priority is
428 * assumed to have been fixed at the time of insertion into the queue.
431 sleepq_changepri(lwp_t
*l
, pri_t pri
)
433 sleepq_t
*sq
= l
->l_sleepq
;
436 KASSERT(lwp_locked(l
, NULL
));
441 if (lwp_eprio(l
) == opri
) {
444 if ((l
->l_syncobj
->sobj_flag
& SOBJ_SLEEPQ_SORTED
) == 0) {
449 * Don't let the sleep queue become empty, even briefly.
450 * cv_signal() and cv_broadcast() inspect it without the
451 * sleep queue lock held and need to see a non-empty queue
452 * head if there are waiters.
454 if (TAILQ_FIRST(sq
) == l
&& TAILQ_NEXT(l
, l_sleepchain
) == NULL
) {
457 TAILQ_REMOVE(sq
, l
, l_sleepchain
);
458 sleepq_insert(sq
, l
, l
->l_syncobj
);
462 sleepq_lendpri(lwp_t
*l
, pri_t pri
)
464 sleepq_t
*sq
= l
->l_sleepq
;
467 KASSERT(lwp_locked(l
, NULL
));
470 l
->l_inheritedprio
= pri
;
472 if (lwp_eprio(l
) == opri
) {
475 if ((l
->l_syncobj
->sobj_flag
& SOBJ_SLEEPQ_SORTED
) == 0) {
480 * Don't let the sleep queue become empty, even briefly.
481 * cv_signal() and cv_broadcast() inspect it without the
482 * sleep queue lock held and need to see a non-empty queue
483 * head if there are waiters.
485 if (TAILQ_FIRST(sq
) == l
&& TAILQ_NEXT(l
, l_sleepchain
) == NULL
) {
488 TAILQ_REMOVE(sq
, l
, l_sleepchain
);
489 sleepq_insert(sq
, l
, l
->l_syncobj
);