4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/thread.h>
29 #include <sys/class.h>
30 #include <sys/debug.h>
31 #include <sys/cpuvar.h>
32 #include <sys/waitq.h>
33 #include <sys/cmn_err.h>
35 #include <sys/dtrace.h>
40 * Wait queue implementation.
44 waitq_init(waitq_t
*wq
)
46 DISP_LOCK_INIT(&wq
->wq_lock
);
49 wq
->wq_blocked
= B_TRUE
;
53 waitq_fini(waitq_t
*wq
)
55 ASSERT(wq
->wq_count
== 0);
56 ASSERT(wq
->wq_first
== NULL
);
57 ASSERT(wq
->wq_blocked
== B_TRUE
);
58 ASSERT(!DISP_LOCK_HELD(&wq
->wq_lock
));
60 DISP_LOCK_DESTROY(&wq
->wq_lock
);
64 * Operations on waitq_t structures.
66 * A wait queue is a singly linked NULL-terminated list with doubly
67 * linked circular sublists. The singly linked list is in descending
68 * priority order and FIFO for threads of the same priority. It links
69 * through the t_link field of the thread structure. The doubly linked
70 * sublists link threads of the same priority. They use the t_priforw
71 * and t_priback fields of the thread structure.
73 * Graphically (with priorities in parens):
75 * ________________ _______ _______
79 * t1(60)-->t2(60)-->t3(60)-->t4(50)-->t5(50)-->t6(30)-->t7(0)-->t8(0)
82 * \______/ \______/ \_______/ \__/ \_______/
84 * There are three interesting operations on a waitq list: inserting
85 * a thread into the proper position according to priority; removing a
86 * thread given a pointer to it; and walking the list, possibly
87 * removing threads along the way. This design allows all three
88 * operations to be performed efficiently and easily.
90 * To insert a thread, traverse the list looking for the sublist of
91 * the same priority as the thread (or one of a lower priority,
92 * meaning there are no other threads in the list of the same
93 * priority). This can be done without touching all threads in the
94 * list by following the links between the first threads in each
95 * sublist. Given a thread t that is the head of a sublist (the first
96 * thread of that priority found when following the t_link pointers),
97 * t->t_priback->t_link points to the head of the next sublist. It's
98 * important to do this since a waitq may contain thousands of
101 * Removing a thread from the list is also efficient. First, the
102 * t_waitq field contains a pointer to the waitq on which a thread
103 * is waiting (or NULL if it's not on a waitq). This is used to
104 * determine if the given thread is on the given waitq without
105 * searching the list. Assuming it is, if it's not the head of a
106 * sublist, just remove it from the sublist and use the t_priback
107 * pointer to find the thread that points to it with t_link. If it is
108 * the head of a sublist, search for it by walking the sublist heads,
109 * similar to searching for a given priority level when inserting a
112 * To walk the list, simply follow the t_link pointers. Removing
113 * threads along the way can be done easily if the code maintains a
114 * pointer to the t_link field that pointed to the thread being
119 waitq_link(waitq_t
*wq
, kthread_t
*t
)
124 pri_t tpri
, next_pri
, last_pri
= -1;
126 ASSERT(DISP_LOCK_HELD(&wq
->wq_lock
));
130 while ((next_tp
= *tpp
) != NULL
) {
131 next_pri
= DISP_PRIO(next_tp
);
134 last_tp
= next_tp
->t_priback
;
136 tpp
= &last_tp
->t_link
;
140 if (last_pri
== tpri
) {
141 /* last_tp points to the last thread of this priority */
142 t
->t_priback
= last_tp
;
143 t
->t_priforw
= last_tp
->t_priforw
;
144 last_tp
->t_priforw
->t_priback
= t
;
145 last_tp
->t_priforw
= t
;
147 t
->t_priback
= t
->t_priforw
= t
;
154 waitq_unlink(waitq_t
*wq
, kthread_t
*t
)
159 ASSERT(THREAD_LOCK_HELD(t
));
160 ASSERT(DISP_LOCK_HELD(&wq
->wq_lock
));
161 ASSERT(t
->t_waitq
== wq
);
163 ptl
= &t
->t_priback
->t_link
;
165 * Is it the head of a priority sublist? If so, need to walk
166 * the priorities to find the t_link pointer that points to it.
170 * Find the right priority level.
172 ptl
= &t
->t_waitq
->wq_first
;
173 while ((nt
= *ptl
) != t
)
174 ptl
= &nt
->t_priback
->t_link
;
177 * Remove thread from the t_link list.
182 * Take it off the priority sublist if there's more than one
185 if (t
->t_priforw
!= t
) {
186 t
->t_priback
->t_priforw
= t
->t_priforw
;
187 t
->t_priforw
->t_priback
= t
->t_priback
;
198 * Put specified thread to specified wait queue without dropping thread's lock.
199 * Returns 1 if thread was successfully placed on project's wait queue, or
200 * 0 if wait queue is blocked.
203 waitq_enqueue(waitq_t
*wq
, kthread_t
*t
)
205 ASSERT(THREAD_LOCK_HELD(t
));
206 ASSERT(t
->t_sleepq
== NULL
);
207 ASSERT(t
->t_waitq
== NULL
);
208 ASSERT(t
->t_link
== NULL
);
210 disp_lock_enter_high(&wq
->wq_lock
);
213 * Can't enqueue anything on a blocked wait queue
215 if (wq
->wq_blocked
) {
216 disp_lock_exit_high(&wq
->wq_lock
);
221 * Mark the time when thread is placed on wait queue. The microstate
222 * accounting code uses this timestamp to determine wait times.
224 t
->t_waitrq
= gethrtime_unscaled();
227 * Mark thread as not swappable. If necessary, it will get
228 * swapped out when it returns to the userland.
230 t
->t_schedflag
|= TS_DONT_SWAP
;
231 DTRACE_SCHED1(cpucaps__sleep
, kthread_t
*, t
);
234 THREAD_WAIT(t
, &wq
->wq_lock
);
239 * Change thread's priority while on the wait queue.
240 * Dequeue and equeue it again so that it gets placed in the right place.
243 waitq_change_pri(kthread_t
*t
, pri_t new_pri
)
245 waitq_t
*wq
= t
->t_waitq
;
247 ASSERT(THREAD_LOCK_HELD(t
));
248 ASSERT(ISWAITING(t
));
257 waitq_dequeue(waitq_t
*wq
, kthread_t
*t
)
259 ASSERT(THREAD_LOCK_HELD(t
));
260 ASSERT(t
->t_waitq
== wq
);
261 ASSERT(ISWAITING(t
));
264 DTRACE_SCHED1(cpucaps__wakeup
, kthread_t
*, t
);
267 * Change thread to transition state and drop the wait queue lock. The
268 * thread will remain locked since its t_lockp points to the
271 THREAD_TRANSITION(t
);
275 * Return True iff there are any threads on the specified wait queue.
276 * The check is done **without holding any locks**.
279 waitq_isempty(waitq_t
*wq
)
281 return (wq
->wq_count
== 0);
285 * Take thread off its wait queue and make it runnable.
286 * Returns with thread lock held.
289 waitq_setrun(kthread_t
*t
)
291 waitq_t
*wq
= t
->t_waitq
;
293 ASSERT(THREAD_LOCK_HELD(t
));
295 ASSERT(ISWAITING(t
));
297 panic("waitq_setrun: thread %p is not on waitq", (void *)t
);
298 waitq_dequeue(wq
, t
);
303 * Take the first thread off the wait queue and return pointer to it.
306 waitq_takeone(waitq_t
*wq
)
310 disp_lock_enter(&wq
->wq_lock
);
312 * waitq_dequeue drops wait queue lock but leaves the CPU at high PIL.
314 if ((t
= wq
->wq_first
) != NULL
)
315 waitq_dequeue(wq
, wq
->wq_first
);
317 disp_lock_exit(&wq
->wq_lock
);
322 * Take the first thread off the wait queue and make it runnable.
323 * Return the pointer to the thread or NULL if waitq is empty
326 waitq_runfirst(waitq_t
*wq
)
330 t
= waitq_takeone(wq
);
333 * t should have transition lock held.
334 * CL_SETRUN() will replace it with dispq lock and keep it held.
335 * thread_unlock() will drop dispq lock and restore PIL.
337 ASSERT(THREAD_LOCK_HELD(t
));
345 * Take the first thread off the wait queue and make it runnable.
348 waitq_runone(waitq_t
*wq
)
350 (void) waitq_runfirst(wq
);
354 * Take all threads off the wait queue and make them runnable.
357 waitq_runall(waitq_t
*wq
)
359 while (waitq_runfirst(wq
) != NULL
)
364 * Prevent any new threads from entering wait queue and make all threads
365 * currently on the wait queue runnable. After waitq_block() completion, no
366 * threads should ever appear on the wait queue untill it is unblocked.
369 waitq_block(waitq_t
*wq
)
371 ASSERT(!wq
->wq_blocked
);
372 disp_lock_enter(&wq
->wq_lock
);
373 wq
->wq_blocked
= B_TRUE
;
374 disp_lock_exit(&wq
->wq_lock
);
376 ASSERT(waitq_isempty(wq
));
380 * Allow threads to be placed on the wait queue.
383 waitq_unblock(waitq_t
*wq
)
385 disp_lock_enter(&wq
->wq_lock
);
387 ASSERT(waitq_isempty(wq
));
388 ASSERT(wq
->wq_blocked
);
390 wq
->wq_blocked
= B_FALSE
;
392 disp_lock_exit(&wq
->wq_lock
);