4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/thread.h>
33 #include <sys/debug.h>
34 #include <sys/cpuvar.h>
35 #include <sys/sleepq.h>
39 * Operations on sleepq_t structures.
41 * A sleep queue is a singly linked NULL-terminated list with doubly
42 * linked circular sublists. The singly linked list is in descending
43 * priority order and FIFO for threads of the same priority. It links
44 * through the t_link field of the thread structure. The doubly linked
45 * sublists link threads of the same priority. They use the t_priforw
46 * and t_priback fields of the thread structure.
48 * Graphically (with priorities in parens):
50 * ________________ _______ _______
54 * t1(60)-->t2(60)-->t3(60)-->t4(50)-->t5(50)-->t6(30)-->t7(0)-->t8(0)
57 * \______/ \______/ \_______/ \__/ \_______/
59 * There are three interesting operations on a sleepq list: inserting
60 * a thread into the proper position according to priority; removing a
61 * thread given a pointer to it; and walking the list, possibly
62 * removing threads along the way. This design allows all three
63 * operations to be performed efficiently and easily.
65 * To insert a thread, traverse the list looking for the sublist of
66 * the same priority as the thread (or one of a lower priority,
67 * meaning there are no other threads in the list of the same
68 * priority). This can be done without touching all threads in the
69 * list by following the links between the first threads in each
70 * sublist. Given a thread t that is the head of a sublist (the first
71 * thread of that priority found when following the t_link pointers),
72 * t->t_priback->t_link points to the head of the next sublist. It's
73 * important to do this since a sleepq may contain thousands of
76 * Removing a thread from the list is also efficient. First, the
77 * t_sleepq field contains a pointer to the sleepq on which a thread
78 * is waiting (or NULL if it's not on a sleepq). This is used to
79 * determine if the given thread is on the given sleepq without
80 * searching the list. Assuming it is, if it's not the head of a
81 * sublist, just remove it from the sublist and use the t_priback
82 * pointer to find the thread that points to it with t_link. If it is
83 * the head of a sublist, search for it by walking the sublist heads,
84 * similar to searching for a given priority level when inserting a
87 * To walk the list, simply follow the t_link pointers. Removing
88 * threads along the way can be done easily if the code maintains a
89 * pointer to the t_link field that pointed to the thread being
93 sleepq_head_t sleepq_head
[NSLEEPQ
];
96 * Common code to unlink a thread from the queue. tpp is a pointer to
97 * the t_link pointer that points to tp.
100 sleepq_unlink(kthread_t
**tpp
, kthread_t
*tp
)
103 ASSERT(tp
->t_sleepq
!= NULL
);
105 /* remove it from the t_link list */
109 * Take it off the priority sublist if there's more than one
112 if (tp
->t_priforw
!= tp
) {
113 tp
->t_priback
->t_priforw
= tp
->t_priforw
;
114 tp
->t_priforw
->t_priback
= tp
->t_priback
;
117 /* Clear out the link junk */
120 tp
->t_priforw
= NULL
;
121 tp
->t_priback
= NULL
;
125 * Insert thread t into sleep queue spq in dispatch priority order.
126 * For lwp_rwlock_t queueing, we must queue writers ahead of readers
127 * of the same priority. We do this by making writers appear to have
128 * a half point higher priority for purposes of priority comparisions.
130 #define CMP_PRIO(t) ((DISP_PRIO(t) << 1) + (t)->t_writer)
132 sleepq_insert(sleepq_t
*spq
, kthread_t
*t
)
137 pri_t tpri
, next_pri
, last_pri
= -1;
139 ASSERT(THREAD_LOCK_HELD(t
)); /* holding the lock on the sleepq */
140 ASSERT(t
->t_sleepq
== NULL
); /* not already on a sleep queue */
143 tpp
= &spq
->sq_first
;
144 while ((next_tp
= *tpp
) != NULL
) {
145 next_pri
= CMP_PRIO(next_tp
);
148 last_tp
= next_tp
->t_priback
;
150 tpp
= &last_tp
->t_link
;
154 if (last_pri
== tpri
) {
155 /* last_tp points to the last thread of this priority */
156 t
->t_priback
= last_tp
;
157 t
->t_priforw
= last_tp
->t_priforw
;
158 last_tp
->t_priforw
->t_priback
= t
;
159 last_tp
->t_priforw
= t
;
161 t
->t_priback
= t
->t_priforw
= t
;
168 * Yank a particular thread out of sleep queue and wake it up.
171 sleepq_unsleep(kthread_t
*t
)
173 ASSERT(THREAD_LOCK_HELD(t
)); /* thread locked via sleepq */
175 /* remove it from queue */
179 t
->t_sobj_ops
= NULL
;
182 ASSERT(t
->t_state
== TS_SLEEP
);
184 * Change thread to transition state without
185 * dropping the sleep queue lock.
187 THREAD_TRANSITION_NOLOCK(t
);
191 * Yank a particular thread out of sleep queue but don't wake it up.
194 sleepq_dequeue(kthread_t
*t
)
199 ASSERT(THREAD_LOCK_HELD(t
)); /* thread locked via sleepq */
200 ASSERT(t
->t_sleepq
!= NULL
);
202 ptl
= &t
->t_priback
->t_link
;
204 * Is it the head of a priority sublist? If so, need to walk
205 * the priorities to find the t_link pointer that points to it.
209 * Find the right priority level.
211 ptl
= &t
->t_sleepq
->sq_first
;
212 while ((nt
= *ptl
) != t
)
213 ptl
= &nt
->t_priback
->t_link
;
215 sleepq_unlink(ptl
, t
);
219 sleepq_wakeone_chan(sleepq_t
*spq
, void *chan
)
224 tpp
= &spq
->sq_first
;
225 while ((tp
= *tpp
) != NULL
) {
226 if (tp
->t_wchan
== chan
) {
227 ASSERT(tp
->t_wchan0
== NULL
);
228 sleepq_unlink(tpp
, tp
);
229 DTRACE_SCHED1(wakeup
, kthread_t
*, tp
);
231 tp
->t_sobj_ops
= NULL
;
233 * Let the target thread know it was cv_signal()ed.
234 * This assumes that cv_signal() is the only
235 * caller of sleepq_wakeone_chan(). If this
236 * becomes false, this code must be revised.
238 tp
->t_schedflag
|= TS_SIGNALLED
;
239 ASSERT(tp
->t_state
== TS_SLEEP
);
241 thread_unlock_high(tp
); /* drop runq lock */
250 sleepq_wakeall_chan(sleepq_t
*spq
, void *chan
)
255 tpp
= &spq
->sq_first
;
256 while ((tp
= *tpp
) != NULL
) {
257 if (tp
->t_wchan
== chan
) {
258 ASSERT(tp
->t_wchan0
== NULL
);
259 sleepq_unlink(tpp
, tp
);
260 DTRACE_SCHED1(wakeup
, kthread_t
*, tp
);
262 tp
->t_sobj_ops
= NULL
;
263 ASSERT(tp
->t_state
== TS_SLEEP
);
265 thread_unlock_high(tp
); /* drop runq lock */