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