xref: /onnv-gate/usr/src/uts/common/os/waitq.c (revision 7632:91aa3d8541b5)
13792Sakolb /*
23792Sakolb  * CDDL HEADER START
33792Sakolb  *
43792Sakolb  * The contents of this file are subject to the terms of the
53792Sakolb  * Common Development and Distribution License (the "License").
63792Sakolb  * You may not use this file except in compliance with the License.
73792Sakolb  *
83792Sakolb  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93792Sakolb  * or http://www.opensolaris.org/os/licensing.
103792Sakolb  * See the License for the specific language governing permissions
113792Sakolb  * and limitations under the License.
123792Sakolb  *
133792Sakolb  * When distributing Covered Code, include this CDDL HEADER in each
143792Sakolb  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153792Sakolb  * If applicable, add the following below this CDDL HEADER, with the
163792Sakolb  * fields enclosed by brackets "[]" replaced with your own identifying
173792Sakolb  * information: Portions Copyright [yyyy] [name of copyright owner]
183792Sakolb  *
193792Sakolb  * CDDL HEADER END
203792Sakolb  */
213792Sakolb /*
22*7632SNick.Todd@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
233792Sakolb  * Use is subject to license terms.
243792Sakolb  */
253792Sakolb 
263792Sakolb #include <sys/param.h>
273792Sakolb #include <sys/systm.h>
283792Sakolb #include <sys/thread.h>
293792Sakolb #include <sys/class.h>
303792Sakolb #include <sys/debug.h>
313792Sakolb #include <sys/cpuvar.h>
323792Sakolb #include <sys/waitq.h>
333792Sakolb #include <sys/cmn_err.h>
343792Sakolb #include <sys/time.h>
353792Sakolb #include <sys/dtrace.h>
363792Sakolb #include <sys/sdt.h>
373792Sakolb #include <sys/zone.h>
383792Sakolb 
393792Sakolb /*
403792Sakolb  * Wait queue implementation.
413792Sakolb  */
423792Sakolb 
433792Sakolb void
waitq_init(waitq_t * wq)443792Sakolb waitq_init(waitq_t *wq)
453792Sakolb {
463792Sakolb 	DISP_LOCK_INIT(&wq->wq_lock);
473792Sakolb 	wq->wq_first = NULL;
483792Sakolb 	wq->wq_count = 0;
493792Sakolb 	wq->wq_blocked = B_TRUE;
503792Sakolb }
513792Sakolb 
523792Sakolb void
waitq_fini(waitq_t * wq)533792Sakolb waitq_fini(waitq_t *wq)
543792Sakolb {
553792Sakolb 	ASSERT(wq->wq_count == 0);
563792Sakolb 	ASSERT(wq->wq_first == NULL);
573792Sakolb 	ASSERT(wq->wq_blocked == B_TRUE);
583792Sakolb 	ASSERT(!DISP_LOCK_HELD(&wq->wq_lock));
593792Sakolb 
603792Sakolb 	DISP_LOCK_DESTROY(&wq->wq_lock);
613792Sakolb }
623792Sakolb 
633792Sakolb /*
643792Sakolb  * Operations on waitq_t structures.
653792Sakolb  *
663792Sakolb  * A wait queue is a singly linked NULL-terminated list with doubly
673792Sakolb  * linked circular sublists.  The singly linked list is in descending
683792Sakolb  * priority order and FIFO for threads of the same priority.  It links
693792Sakolb  * through the t_link field of the thread structure.  The doubly linked
703792Sakolb  * sublists link threads of the same priority.  They use the t_priforw
713792Sakolb  * and t_priback fields of the thread structure.
723792Sakolb  *
733792Sakolb  * Graphically (with priorities in parens):
743792Sakolb  *
753792Sakolb  *         ________________           _______                   _______
763792Sakolb  *        /                \         /       \                 /       \
773792Sakolb  *        |                |         |       |                 |       |
783792Sakolb  *        v                v         v       v                 v       v
793792Sakolb  *     t1(60)-->t2(60)-->t3(60)-->t4(50)-->t5(50)-->t6(30)-->t7(0)-->t8(0)
803792Sakolb  *        ^      ^  ^      ^         ^       ^       ^  ^      ^       ^
813792Sakolb  *        |      |  |      |         |       |       |  |      |       |
823792Sakolb  *        \______/  \______/         \_______/       \__/      \_______/
833792Sakolb  *
843792Sakolb  * There are three interesting operations on a waitq list: inserting
853792Sakolb  * a thread into the proper position according to priority; removing a
863792Sakolb  * thread given a pointer to it; and walking the list, possibly
873792Sakolb  * removing threads along the way.  This design allows all three
883792Sakolb  * operations to be performed efficiently and easily.
893792Sakolb  *
903792Sakolb  * To insert a thread, traverse the list looking for the sublist of
913792Sakolb  * the same priority as the thread (or one of a lower priority,
923792Sakolb  * meaning there are no other threads in the list of the same
933792Sakolb  * priority).  This can be done without touching all threads in the
943792Sakolb  * list by following the links between the first threads in each
953792Sakolb  * sublist.  Given a thread t that is the head of a sublist (the first
963792Sakolb  * thread of that priority found when following the t_link pointers),
973792Sakolb  * t->t_priback->t_link points to the head of the next sublist.  It's
983792Sakolb  * important to do this since a waitq may contain thousands of
993792Sakolb  * threads.
1003792Sakolb  *
1013792Sakolb  * Removing a thread from the list is also efficient.  First, the
1023792Sakolb  * t_waitq field contains a pointer to the waitq on which a thread
1033792Sakolb  * is waiting (or NULL if it's not on a waitq).  This is used to
1043792Sakolb  * determine if the given thread is on the given waitq without
1053792Sakolb  * searching the list.  Assuming it is, if it's not the head of a
1063792Sakolb  * sublist, just remove it from the sublist and use the t_priback
1073792Sakolb  * pointer to find the thread that points to it with t_link.  If it is
1083792Sakolb  * the head of a sublist, search for it by walking the sublist heads,
1093792Sakolb  * similar to searching for a given priority level when inserting a
1103792Sakolb  * thread.
1113792Sakolb  *
1123792Sakolb  * To walk the list, simply follow the t_link pointers.  Removing
1133792Sakolb  * threads along the way can be done easily if the code maintains a
1143792Sakolb  * pointer to the t_link field that pointed to the thread being
1153792Sakolb  * removed.
1163792Sakolb  */
1173792Sakolb 
1183792Sakolb static void
waitq_link(waitq_t * wq,kthread_t * t)1193792Sakolb waitq_link(waitq_t *wq, kthread_t *t)
1203792Sakolb {
1213792Sakolb 	kthread_t *next_tp;
1223792Sakolb 	kthread_t *last_tp;
1233792Sakolb 	kthread_t **tpp;
1243792Sakolb 	pri_t tpri, next_pri, last_pri = -1;
1253792Sakolb 
1263792Sakolb 	ASSERT(DISP_LOCK_HELD(&wq->wq_lock));
1273792Sakolb 
1283792Sakolb 	tpri = DISP_PRIO(t);
1293792Sakolb 	tpp = &wq->wq_first;
1303792Sakolb 	while ((next_tp = *tpp) != NULL) {
1313792Sakolb 		next_pri = DISP_PRIO(next_tp);
1323792Sakolb 		if (tpri > next_pri)
1333792Sakolb 			break;
1343792Sakolb 		last_tp = next_tp->t_priback;
1353792Sakolb 		last_pri = next_pri;
1363792Sakolb 		tpp = &last_tp->t_link;
1373792Sakolb 	}
1383792Sakolb 	*tpp = t;
1393792Sakolb 	t->t_link = next_tp;
1403792Sakolb 	if (last_pri == tpri) {
1413792Sakolb 		/* last_tp points to the last thread of this priority */
1423792Sakolb 		t->t_priback = last_tp;
1433792Sakolb 		t->t_priforw = last_tp->t_priforw;
1443792Sakolb 		last_tp->t_priforw->t_priback = t;
1453792Sakolb 		last_tp->t_priforw = t;
1463792Sakolb 	} else {
1473792Sakolb 		t->t_priback = t->t_priforw = t;
1483792Sakolb 	}
1493792Sakolb 	wq->wq_count++;
1503792Sakolb 	t->t_waitq = wq;
1513792Sakolb }
1523792Sakolb 
1533792Sakolb static void
waitq_unlink(waitq_t * wq,kthread_t * t)1543792Sakolb waitq_unlink(waitq_t *wq, kthread_t *t)
1553792Sakolb {
1563792Sakolb 	kthread_t *nt;
1573792Sakolb 	kthread_t **ptl;
1583792Sakolb 
1593792Sakolb 	ASSERT(THREAD_LOCK_HELD(t));
1603792Sakolb 	ASSERT(DISP_LOCK_HELD(&wq->wq_lock));
1613792Sakolb 	ASSERT(t->t_waitq == wq);
1623792Sakolb 
1633792Sakolb 	ptl = &t->t_priback->t_link;
1643792Sakolb 	/*
1653792Sakolb 	 * Is it the head of a priority sublist?  If so, need to walk
1663792Sakolb 	 * the priorities to find the t_link pointer that points to it.
1673792Sakolb 	 */
1683792Sakolb 	if (*ptl != t) {
1693792Sakolb 		/*
1703792Sakolb 		 * Find the right priority level.
1713792Sakolb 		 */
1723792Sakolb 		ptl = &t->t_waitq->wq_first;
1733792Sakolb 		while ((nt = *ptl) != t)
1743792Sakolb 			ptl = &nt->t_priback->t_link;
1753792Sakolb 	}
1763792Sakolb 	/*
1773792Sakolb 	 * Remove thread from the t_link list.
1783792Sakolb 	 */
1793792Sakolb 	*ptl = t->t_link;
1803792Sakolb 
1813792Sakolb 	/*
1823792Sakolb 	 * Take it off the priority sublist if there's more than one
1833792Sakolb 	 * thread there.
1843792Sakolb 	 */
1853792Sakolb 	if (t->t_priforw != t) {
1863792Sakolb 		t->t_priback->t_priforw = t->t_priforw;
1873792Sakolb 		t->t_priforw->t_priback = t->t_priback;
1883792Sakolb 	}
1893792Sakolb 	t->t_link = NULL;
1903792Sakolb 
1913792Sakolb 	wq->wq_count--;
1923792Sakolb 	t->t_waitq = NULL;
1933792Sakolb 	t->t_priforw = NULL;
1943792Sakolb 	t->t_priback = NULL;
1953792Sakolb }
1963792Sakolb 
1973792Sakolb /*
1983792Sakolb  * Put specified thread to specified wait queue without dropping thread's lock.
1993792Sakolb  * Returns 1 if thread was successfully placed on project's wait queue, or
2003792Sakolb  * 0 if wait queue is blocked.
2013792Sakolb  */
2023792Sakolb int
waitq_enqueue(waitq_t * wq,kthread_t * t)2033792Sakolb waitq_enqueue(waitq_t *wq, kthread_t *t)
2043792Sakolb {
2053792Sakolb 	ASSERT(THREAD_LOCK_HELD(t));
2063792Sakolb 	ASSERT(t->t_sleepq == NULL);
2073792Sakolb 	ASSERT(t->t_waitq == NULL);
2083792Sakolb 	ASSERT(t->t_link == NULL);
2093792Sakolb 
2103792Sakolb 	disp_lock_enter_high(&wq->wq_lock);
2113792Sakolb 
2123792Sakolb 	/*
2133792Sakolb 	 * Can't enqueue anything on a blocked wait queue
2143792Sakolb 	 */
2153792Sakolb 	if (wq->wq_blocked) {
2163792Sakolb 		disp_lock_exit_high(&wq->wq_lock);
2173792Sakolb 		return (0);
2183792Sakolb 	}
2193792Sakolb 
2203792Sakolb 	/*
2213792Sakolb 	 * Mark the time when thread is placed on wait queue. The microstate
2223792Sakolb 	 * accounting code uses this timestamp to determine wait times.
2233792Sakolb 	 */
2243792Sakolb 	t->t_waitrq = gethrtime_unscaled();
2253792Sakolb 
2263792Sakolb 	/*
2273792Sakolb 	 * Mark thread as not swappable.  If necessary, it will get
2283792Sakolb 	 * swapped out when it returns to the userland.
2293792Sakolb 	 */
2303792Sakolb 	t->t_schedflag |= TS_DONT_SWAP;
2313792Sakolb 	DTRACE_SCHED1(cpucaps__sleep, kthread_t *, t);
2323792Sakolb 	waitq_link(wq, t);
2333792Sakolb 
2343792Sakolb 	THREAD_WAIT(t, &wq->wq_lock);
2353792Sakolb 	return (1);
2363792Sakolb }
2373792Sakolb 
2383792Sakolb /*
2393792Sakolb  * Change thread's priority while on the wait queue.
2403792Sakolb  * Dequeue and equeue it again so that it gets placed in the right place.
2413792Sakolb  */
2423792Sakolb void
waitq_change_pri(kthread_t * t,pri_t new_pri)2433792Sakolb waitq_change_pri(kthread_t *t, pri_t new_pri)
2443792Sakolb {
2453792Sakolb 	waitq_t *wq = t->t_waitq;
2463792Sakolb 
2473792Sakolb 	ASSERT(THREAD_LOCK_HELD(t));
2483792Sakolb 	ASSERT(ISWAITING(t));
2493792Sakolb 	ASSERT(wq != NULL);
2503792Sakolb 
2513792Sakolb 	waitq_unlink(wq, t);
2523792Sakolb 	t->t_pri = new_pri;
2533792Sakolb 	waitq_link(wq, t);
2543792Sakolb }
2553792Sakolb 
2563792Sakolb static void
waitq_dequeue(waitq_t * wq,kthread_t * t)2573792Sakolb waitq_dequeue(waitq_t *wq, kthread_t *t)
2583792Sakolb {
2593792Sakolb 	ASSERT(THREAD_LOCK_HELD(t));
2603792Sakolb 	ASSERT(t->t_waitq == wq);
2613792Sakolb 	ASSERT(ISWAITING(t));
2623792Sakolb 
2633792Sakolb 	waitq_unlink(wq, t);
2643792Sakolb 	DTRACE_SCHED1(cpucaps__wakeup, kthread_t *, t);
2653792Sakolb 
2663792Sakolb 	/*
2674994Sakolb 	 * Change thread to transition state and drop the wait queue lock. The
2684994Sakolb 	 * thread will remain locked since its t_lockp points to the
2694994Sakolb 	 * transition_lock.
2703792Sakolb 	 */
2714994Sakolb 	THREAD_TRANSITION(t);
2723792Sakolb }
2733792Sakolb 
2743792Sakolb /*
2753792Sakolb  * Return True iff there are any threads on the specified wait queue.
2763792Sakolb  * The check is done **without holding any locks**.
2773792Sakolb  */
2783792Sakolb boolean_t
waitq_isempty(waitq_t * wq)2793792Sakolb waitq_isempty(waitq_t *wq)
2803792Sakolb {
2813792Sakolb 	return (wq->wq_count == 0);
2823792Sakolb }
2833792Sakolb 
2843792Sakolb /*
2853792Sakolb  * Take thread off its wait queue and make it runnable.
2863792Sakolb  * Returns with thread lock held.
2873792Sakolb  */
2883792Sakolb void
waitq_setrun(kthread_t * t)2893792Sakolb waitq_setrun(kthread_t *t)
2903792Sakolb {
2913792Sakolb 	waitq_t *wq = t->t_waitq;
2923792Sakolb 
2933792Sakolb 	ASSERT(THREAD_LOCK_HELD(t));
2943792Sakolb 
2953792Sakolb 	ASSERT(ISWAITING(t));
2963792Sakolb 	if (wq == NULL)
297*7632SNick.Todd@Sun.COM 		panic("waitq_setrun: thread %p is not on waitq", (void *)t);
2983792Sakolb 	waitq_dequeue(wq, t);
2993792Sakolb 	CL_SETRUN(t);
3003792Sakolb }
3013792Sakolb 
3023792Sakolb /*
3033792Sakolb  * Take the first thread off the wait queue and return pointer to it.
3043792Sakolb  */
3053792Sakolb static kthread_t *
waitq_takeone(waitq_t * wq)3063792Sakolb waitq_takeone(waitq_t *wq)
3073792Sakolb {
3083792Sakolb 	kthread_t *t;
3093792Sakolb 
3103792Sakolb 	disp_lock_enter(&wq->wq_lock);
3114994Sakolb 	/*
3124994Sakolb 	 * waitq_dequeue drops wait queue lock but leaves the CPU at high PIL.
3134994Sakolb 	 */
3143792Sakolb 	if ((t = wq->wq_first) != NULL)
3153792Sakolb 		waitq_dequeue(wq, wq->wq_first);
3164994Sakolb 	else
3174994Sakolb 		disp_lock_exit(&wq->wq_lock);
3183792Sakolb 	return (t);
3193792Sakolb }
3203792Sakolb 
3213792Sakolb /*
3223792Sakolb  * Take the first thread off the wait queue and make it runnable.
3233792Sakolb  * Return the pointer to the thread or NULL if waitq is empty
3243792Sakolb  */
3253792Sakolb static kthread_t *
waitq_runfirst(waitq_t * wq)3263792Sakolb waitq_runfirst(waitq_t *wq)
3273792Sakolb {
3283792Sakolb 	kthread_t *t;
3293792Sakolb 
3303792Sakolb 	t = waitq_takeone(wq);
3313792Sakolb 	if (t != NULL) {
3324994Sakolb 		/*
3334994Sakolb 		 * t should have transition lock held.
3344994Sakolb 		 * CL_SETRUN() will replace it with dispq lock and keep it held.
3354994Sakolb 		 * thread_unlock() will drop dispq lock and restore PIL.
3364994Sakolb 		 */
3374994Sakolb 		ASSERT(THREAD_LOCK_HELD(t));
3383792Sakolb 		CL_SETRUN(t);
3394994Sakolb 		thread_unlock(t);
3403792Sakolb 	}
3413792Sakolb 	return (t);
3423792Sakolb }
3433792Sakolb 
3443792Sakolb /*
3453792Sakolb  * Take the first thread off the wait queue and make it runnable.
3463792Sakolb  */
3473792Sakolb void
waitq_runone(waitq_t * wq)3483792Sakolb waitq_runone(waitq_t *wq)
3493792Sakolb {
3503792Sakolb 	(void) waitq_runfirst(wq);
3513792Sakolb }
3523792Sakolb 
3533792Sakolb /*
3543792Sakolb  * Take all threads off the wait queue and make them runnable.
3553792Sakolb  */
3563792Sakolb static void
waitq_runall(waitq_t * wq)3573792Sakolb waitq_runall(waitq_t *wq)
3583792Sakolb {
3593792Sakolb 	while (waitq_runfirst(wq) != NULL)
3603792Sakolb 		;
3613792Sakolb }
3623792Sakolb 
3633792Sakolb /*
3643792Sakolb  * Prevent any new threads from entering wait queue and make all threads
3653792Sakolb  * currently on the wait queue runnable. After waitq_block() completion, no
3663792Sakolb  * threads should ever appear on the wait queue untill it is unblocked.
3673792Sakolb  */
3683792Sakolb void
waitq_block(waitq_t * wq)3693792Sakolb waitq_block(waitq_t *wq)
3703792Sakolb {
3713792Sakolb 	ASSERT(!wq->wq_blocked);
3723792Sakolb 	disp_lock_enter(&wq->wq_lock);
3733792Sakolb 	wq->wq_blocked = B_TRUE;
3743792Sakolb 	disp_lock_exit(&wq->wq_lock);
3753792Sakolb 	waitq_runall(wq);
3763792Sakolb 	ASSERT(waitq_isempty(wq));
3773792Sakolb }
3783792Sakolb 
3793792Sakolb /*
3803792Sakolb  * Allow threads to be placed on the wait queue.
3813792Sakolb  */
3823792Sakolb void
waitq_unblock(waitq_t * wq)3833792Sakolb waitq_unblock(waitq_t *wq)
3843792Sakolb {
3853792Sakolb 	disp_lock_enter(&wq->wq_lock);
3863792Sakolb 
3873792Sakolb 	ASSERT(waitq_isempty(wq));
3883792Sakolb 	ASSERT(wq->wq_blocked);
3893792Sakolb 
3903792Sakolb 	wq->wq_blocked = B_FALSE;
3913792Sakolb 
3923792Sakolb 	disp_lock_exit(&wq->wq_lock);
3933792Sakolb }
394