1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <sys/param.h>
30*0Sstevel@tonic-gate #include <sys/systm.h>
31*0Sstevel@tonic-gate #include <sys/thread.h>
32*0Sstevel@tonic-gate #include <sys/proc.h>
33*0Sstevel@tonic-gate #include <sys/debug.h>
34*0Sstevel@tonic-gate #include <sys/cpuvar.h>
35*0Sstevel@tonic-gate #include <sys/sleepq.h>
36*0Sstevel@tonic-gate #include <sys/sdt.h>
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gate /*
39*0Sstevel@tonic-gate * Operations on sleepq_t structures.
40*0Sstevel@tonic-gate *
41*0Sstevel@tonic-gate * A sleep queue is a singly linked NULL-terminated list with doubly
42*0Sstevel@tonic-gate * linked circular sublists. The singly linked list is in descending
43*0Sstevel@tonic-gate * priority order and FIFO for threads of the same priority. It links
44*0Sstevel@tonic-gate * through the t_link field of the thread structure. The doubly linked
45*0Sstevel@tonic-gate * sublists link threads of the same priority. They use the t_priforw
46*0Sstevel@tonic-gate * and t_priback fields of the thread structure.
47*0Sstevel@tonic-gate *
48*0Sstevel@tonic-gate * Graphically (with priorities in parens):
49*0Sstevel@tonic-gate *
50*0Sstevel@tonic-gate * ________________ _______ _______
51*0Sstevel@tonic-gate * / \ / \ / \
52*0Sstevel@tonic-gate * | | | | | |
53*0Sstevel@tonic-gate * v v v v v v
54*0Sstevel@tonic-gate * t1(60)-->t2(60)-->t3(60)-->t4(50)-->t5(50)-->t6(30)-->t7(0)-->t8(0)
55*0Sstevel@tonic-gate * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
56*0Sstevel@tonic-gate * | | | | | | | | | |
57*0Sstevel@tonic-gate * \______/ \______/ \_______/ \__/ \_______/
58*0Sstevel@tonic-gate *
59*0Sstevel@tonic-gate * There are three interesting operations on a sleepq list: inserting
60*0Sstevel@tonic-gate * a thread into the proper position according to priority; removing a
61*0Sstevel@tonic-gate * thread given a pointer to it; and walking the list, possibly
62*0Sstevel@tonic-gate * removing threads along the way. This design allows all three
63*0Sstevel@tonic-gate * operations to be performed efficiently and easily.
64*0Sstevel@tonic-gate *
65*0Sstevel@tonic-gate * To insert a thread, traverse the list looking for the sublist of
66*0Sstevel@tonic-gate * the same priority as the thread (or one of a lower priority,
67*0Sstevel@tonic-gate * meaning there are no other threads in the list of the same
68*0Sstevel@tonic-gate * priority). This can be done without touching all threads in the
69*0Sstevel@tonic-gate * list by following the links between the first threads in each
70*0Sstevel@tonic-gate * sublist. Given a thread t that is the head of a sublist (the first
71*0Sstevel@tonic-gate * thread of that priority found when following the t_link pointers),
72*0Sstevel@tonic-gate * t->t_priback->t_link points to the head of the next sublist. It's
73*0Sstevel@tonic-gate * important to do this since a sleepq may contain thousands of
74*0Sstevel@tonic-gate * threads.
75*0Sstevel@tonic-gate *
76*0Sstevel@tonic-gate * Removing a thread from the list is also efficient. First, the
77*0Sstevel@tonic-gate * t_sleepq field contains a pointer to the sleepq on which a thread
78*0Sstevel@tonic-gate * is waiting (or NULL if it's not on a sleepq). This is used to
79*0Sstevel@tonic-gate * determine if the given thread is on the given sleepq without
80*0Sstevel@tonic-gate * searching the list. Assuming it is, if it's not the head of a
81*0Sstevel@tonic-gate * sublist, just remove it from the sublist and use the t_priback
82*0Sstevel@tonic-gate * pointer to find the thread that points to it with t_link. If it is
83*0Sstevel@tonic-gate * the head of a sublist, search for it by walking the sublist heads,
84*0Sstevel@tonic-gate * similar to searching for a given priority level when inserting a
85*0Sstevel@tonic-gate * thread.
86*0Sstevel@tonic-gate *
87*0Sstevel@tonic-gate * To walk the list, simply follow the t_link pointers. Removing
88*0Sstevel@tonic-gate * threads along the way can be done easily if the code maintains a
89*0Sstevel@tonic-gate * pointer to the t_link field that pointed to the thread being
90*0Sstevel@tonic-gate * removed.
91*0Sstevel@tonic-gate */
92*0Sstevel@tonic-gate
93*0Sstevel@tonic-gate sleepq_head_t sleepq_head[NSLEEPQ];
94*0Sstevel@tonic-gate
95*0Sstevel@tonic-gate /*
96*0Sstevel@tonic-gate * Common code to unlink a thread from the queue. tpp is a pointer to
97*0Sstevel@tonic-gate * the t_link pointer that points to tp.
98*0Sstevel@tonic-gate */
99*0Sstevel@tonic-gate void
sleepq_unlink(kthread_t ** tpp,kthread_t * tp)100*0Sstevel@tonic-gate sleepq_unlink(kthread_t **tpp, kthread_t *tp)
101*0Sstevel@tonic-gate {
102*0Sstevel@tonic-gate ASSERT(*tpp == tp);
103*0Sstevel@tonic-gate ASSERT(tp->t_sleepq != NULL);
104*0Sstevel@tonic-gate
105*0Sstevel@tonic-gate /* remove it from the t_link list */
106*0Sstevel@tonic-gate *tpp = tp->t_link;
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate /*
109*0Sstevel@tonic-gate * Take it off the priority sublist if there's more than one
110*0Sstevel@tonic-gate * thread there.
111*0Sstevel@tonic-gate */
112*0Sstevel@tonic-gate if (tp->t_priforw != tp) {
113*0Sstevel@tonic-gate tp->t_priback->t_priforw = tp->t_priforw;
114*0Sstevel@tonic-gate tp->t_priforw->t_priback = tp->t_priback;
115*0Sstevel@tonic-gate }
116*0Sstevel@tonic-gate
117*0Sstevel@tonic-gate /* Clear out the link junk */
118*0Sstevel@tonic-gate tp->t_link = NULL;
119*0Sstevel@tonic-gate tp->t_sleepq = NULL;
120*0Sstevel@tonic-gate tp->t_priforw = NULL;
121*0Sstevel@tonic-gate tp->t_priback = NULL;
122*0Sstevel@tonic-gate }
123*0Sstevel@tonic-gate
124*0Sstevel@tonic-gate /*
125*0Sstevel@tonic-gate * Insert thread t into sleep queue spq in dispatch priority order.
126*0Sstevel@tonic-gate * For lwp_rwlock_t queueing, we must queue writers ahead of readers
127*0Sstevel@tonic-gate * of the same priority. We do this by making writers appear to have
128*0Sstevel@tonic-gate * a half point higher priority for purposes of priority comparisions.
129*0Sstevel@tonic-gate */
130*0Sstevel@tonic-gate #define CMP_PRIO(t) ((DISP_PRIO(t) << 1) + (t)->t_writer)
131*0Sstevel@tonic-gate void
sleepq_insert(sleepq_t * spq,kthread_t * t)132*0Sstevel@tonic-gate sleepq_insert(sleepq_t *spq, kthread_t *t)
133*0Sstevel@tonic-gate {
134*0Sstevel@tonic-gate kthread_t *next_tp;
135*0Sstevel@tonic-gate kthread_t *last_tp;
136*0Sstevel@tonic-gate kthread_t **tpp;
137*0Sstevel@tonic-gate pri_t tpri, next_pri, last_pri = -1;
138*0Sstevel@tonic-gate
139*0Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t)); /* holding the lock on the sleepq */
140*0Sstevel@tonic-gate ASSERT(t->t_sleepq == NULL); /* not already on a sleep queue */
141*0Sstevel@tonic-gate
142*0Sstevel@tonic-gate tpri = CMP_PRIO(t);
143*0Sstevel@tonic-gate tpp = &spq->sq_first;
144*0Sstevel@tonic-gate while ((next_tp = *tpp) != NULL) {
145*0Sstevel@tonic-gate next_pri = CMP_PRIO(next_tp);
146*0Sstevel@tonic-gate if (tpri > next_pri)
147*0Sstevel@tonic-gate break;
148*0Sstevel@tonic-gate last_tp = next_tp->t_priback;
149*0Sstevel@tonic-gate last_pri = next_pri;
150*0Sstevel@tonic-gate tpp = &last_tp->t_link;
151*0Sstevel@tonic-gate }
152*0Sstevel@tonic-gate *tpp = t;
153*0Sstevel@tonic-gate t->t_link = next_tp;
154*0Sstevel@tonic-gate if (last_pri == tpri) {
155*0Sstevel@tonic-gate /* last_tp points to the last thread of this priority */
156*0Sstevel@tonic-gate t->t_priback = last_tp;
157*0Sstevel@tonic-gate t->t_priforw = last_tp->t_priforw;
158*0Sstevel@tonic-gate last_tp->t_priforw->t_priback = t;
159*0Sstevel@tonic-gate last_tp->t_priforw = t;
160*0Sstevel@tonic-gate } else {
161*0Sstevel@tonic-gate t->t_priback = t->t_priforw = t;
162*0Sstevel@tonic-gate }
163*0Sstevel@tonic-gate t->t_sleepq = spq;
164*0Sstevel@tonic-gate }
165*0Sstevel@tonic-gate
166*0Sstevel@tonic-gate
167*0Sstevel@tonic-gate /*
168*0Sstevel@tonic-gate * Yank a particular thread out of sleep queue and wake it up.
169*0Sstevel@tonic-gate */
170*0Sstevel@tonic-gate void
sleepq_unsleep(kthread_t * t)171*0Sstevel@tonic-gate sleepq_unsleep(kthread_t *t)
172*0Sstevel@tonic-gate {
173*0Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t)); /* thread locked via sleepq */
174*0Sstevel@tonic-gate
175*0Sstevel@tonic-gate /* remove it from queue */
176*0Sstevel@tonic-gate sleepq_dequeue(t);
177*0Sstevel@tonic-gate
178*0Sstevel@tonic-gate /* wake it up */
179*0Sstevel@tonic-gate t->t_sobj_ops = NULL;
180*0Sstevel@tonic-gate t->t_wchan = NULL;
181*0Sstevel@tonic-gate t->t_wchan0 = NULL;
182*0Sstevel@tonic-gate ASSERT(t->t_state == TS_SLEEP);
183*0Sstevel@tonic-gate /*
184*0Sstevel@tonic-gate * Change thread to transition state without
185*0Sstevel@tonic-gate * dropping the sleep queue lock.
186*0Sstevel@tonic-gate */
187*0Sstevel@tonic-gate THREAD_TRANSITION_NOLOCK(t);
188*0Sstevel@tonic-gate }
189*0Sstevel@tonic-gate
190*0Sstevel@tonic-gate /*
191*0Sstevel@tonic-gate * Yank a particular thread out of sleep queue but don't wake it up.
192*0Sstevel@tonic-gate */
193*0Sstevel@tonic-gate void
sleepq_dequeue(kthread_t * t)194*0Sstevel@tonic-gate sleepq_dequeue(kthread_t *t)
195*0Sstevel@tonic-gate {
196*0Sstevel@tonic-gate kthread_t *nt;
197*0Sstevel@tonic-gate kthread_t **ptl;
198*0Sstevel@tonic-gate
199*0Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t)); /* thread locked via sleepq */
200*0Sstevel@tonic-gate ASSERT(t->t_sleepq != NULL);
201*0Sstevel@tonic-gate
202*0Sstevel@tonic-gate ptl = &t->t_priback->t_link;
203*0Sstevel@tonic-gate /*
204*0Sstevel@tonic-gate * Is it the head of a priority sublist? If so, need to walk
205*0Sstevel@tonic-gate * the priorities to find the t_link pointer that points to it.
206*0Sstevel@tonic-gate */
207*0Sstevel@tonic-gate if (*ptl != t) {
208*0Sstevel@tonic-gate /*
209*0Sstevel@tonic-gate * Find the right priority level.
210*0Sstevel@tonic-gate */
211*0Sstevel@tonic-gate ptl = &t->t_sleepq->sq_first;
212*0Sstevel@tonic-gate while ((nt = *ptl) != t)
213*0Sstevel@tonic-gate ptl = &nt->t_priback->t_link;
214*0Sstevel@tonic-gate }
215*0Sstevel@tonic-gate sleepq_unlink(ptl, t);
216*0Sstevel@tonic-gate }
217*0Sstevel@tonic-gate
218*0Sstevel@tonic-gate kthread_t *
sleepq_wakeone_chan(sleepq_t * spq,void * chan)219*0Sstevel@tonic-gate sleepq_wakeone_chan(sleepq_t *spq, void *chan)
220*0Sstevel@tonic-gate {
221*0Sstevel@tonic-gate kthread_t *tp;
222*0Sstevel@tonic-gate kthread_t **tpp;
223*0Sstevel@tonic-gate
224*0Sstevel@tonic-gate tpp = &spq->sq_first;
225*0Sstevel@tonic-gate while ((tp = *tpp) != NULL) {
226*0Sstevel@tonic-gate if (tp->t_wchan == chan) {
227*0Sstevel@tonic-gate ASSERT(tp->t_wchan0 == NULL);
228*0Sstevel@tonic-gate sleepq_unlink(tpp, tp);
229*0Sstevel@tonic-gate DTRACE_SCHED1(wakeup, kthread_t *, tp);
230*0Sstevel@tonic-gate tp->t_wchan = NULL;
231*0Sstevel@tonic-gate tp->t_sobj_ops = NULL;
232*0Sstevel@tonic-gate /*
233*0Sstevel@tonic-gate * Let the target thread know it was cv_signal()ed.
234*0Sstevel@tonic-gate * This assumes that cv_signal() is the only
235*0Sstevel@tonic-gate * caller of sleepq_wakeone_chan(). If this
236*0Sstevel@tonic-gate * becomes false, this code must be revised.
237*0Sstevel@tonic-gate */
238*0Sstevel@tonic-gate tp->t_schedflag |= TS_SIGNALLED;
239*0Sstevel@tonic-gate ASSERT(tp->t_state == TS_SLEEP);
240*0Sstevel@tonic-gate CL_WAKEUP(tp);
241*0Sstevel@tonic-gate thread_unlock_high(tp); /* drop runq lock */
242*0Sstevel@tonic-gate return (tp);
243*0Sstevel@tonic-gate }
244*0Sstevel@tonic-gate tpp = &tp->t_link;
245*0Sstevel@tonic-gate }
246*0Sstevel@tonic-gate return (NULL);
247*0Sstevel@tonic-gate }
248*0Sstevel@tonic-gate
249*0Sstevel@tonic-gate void
sleepq_wakeall_chan(sleepq_t * spq,void * chan)250*0Sstevel@tonic-gate sleepq_wakeall_chan(sleepq_t *spq, void *chan)
251*0Sstevel@tonic-gate {
252*0Sstevel@tonic-gate kthread_t *tp;
253*0Sstevel@tonic-gate kthread_t **tpp;
254*0Sstevel@tonic-gate
255*0Sstevel@tonic-gate tpp = &spq->sq_first;
256*0Sstevel@tonic-gate while ((tp = *tpp) != NULL) {
257*0Sstevel@tonic-gate if (tp->t_wchan == chan) {
258*0Sstevel@tonic-gate ASSERT(tp->t_wchan0 == NULL);
259*0Sstevel@tonic-gate sleepq_unlink(tpp, tp);
260*0Sstevel@tonic-gate DTRACE_SCHED1(wakeup, kthread_t *, tp);
261*0Sstevel@tonic-gate tp->t_wchan = NULL;
262*0Sstevel@tonic-gate tp->t_sobj_ops = NULL;
263*0Sstevel@tonic-gate ASSERT(tp->t_state == TS_SLEEP);
264*0Sstevel@tonic-gate CL_WAKEUP(tp);
265*0Sstevel@tonic-gate thread_unlock_high(tp); /* drop runq lock */
266*0Sstevel@tonic-gate continue;
267*0Sstevel@tonic-gate }
268*0Sstevel@tonic-gate tpp = &tp->t_link;
269*0Sstevel@tonic-gate }
270*0Sstevel@tonic-gate }
271