xref: /titanic_50/usr/src/uts/common/os/condvar.c (revision 427a21b56415a2c5048627d491e6e8da8d953bf1)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /* Copyright (c) 2011 by Delphix. All rights reserved. */
28 
29 #include <sys/thread.h>
30 #include <sys/proc.h>
31 #include <sys/debug.h>
32 #include <sys/cmn_err.h>
33 #include <sys/systm.h>
34 #include <sys/sobject.h>
35 #include <sys/sleepq.h>
36 #include <sys/cpuvar.h>
37 #include <sys/condvar.h>
38 #include <sys/condvar_impl.h>
39 #include <sys/schedctl.h>
40 #include <sys/procfs.h>
41 #include <sys/sdt.h>
42 #include <sys/callo.h>
43 
44 clock_t cv_timedwait_hires(kcondvar_t *, kmutex_t *, hrtime_t, hrtime_t, int);
45 
46 /*
47  * CV_MAX_WAITERS is the maximum number of waiters we track; once
48  * the number becomes higher than that, we look at the sleepq to
49  * see whether there are *really* any waiters.
50  */
51 #define	CV_MAX_WAITERS		1024		/* must be power of 2 */
52 #define	CV_WAITERS_MASK		(CV_MAX_WAITERS - 1)
53 
54 /*
55  * Threads don't "own" condition variables.
56  */
57 /* ARGSUSED */
58 static kthread_t *
59 cv_owner(void *cvp)
60 {
61 	return (NULL);
62 }
63 
64 /*
65  * Unsleep a thread that's blocked on a condition variable.
66  */
67 static void
68 cv_unsleep(kthread_t *t)
69 {
70 	condvar_impl_t *cvp = (condvar_impl_t *)t->t_wchan;
71 	sleepq_head_t *sqh = SQHASH(cvp);
72 
73 	ASSERT(THREAD_LOCK_HELD(t));
74 
75 	if (cvp == NULL)
76 		panic("cv_unsleep: thread %p not on sleepq %p",
77 		    (void *)t, (void *)sqh);
78 	DTRACE_SCHED1(wakeup, kthread_t *, t);
79 	sleepq_unsleep(t);
80 	if (cvp->cv_waiters != CV_MAX_WAITERS)
81 		cvp->cv_waiters--;
82 	disp_lock_exit_high(&sqh->sq_lock);
83 	CL_SETRUN(t);
84 }
85 
86 /*
87  * Change the priority of a thread that's blocked on a condition variable.
88  */
89 static void
90 cv_change_pri(kthread_t *t, pri_t pri, pri_t *t_prip)
91 {
92 	condvar_impl_t *cvp = (condvar_impl_t *)t->t_wchan;
93 	sleepq_t *sqp = t->t_sleepq;
94 
95 	ASSERT(THREAD_LOCK_HELD(t));
96 	ASSERT(&SQHASH(cvp)->sq_queue == sqp);
97 
98 	if (cvp == NULL)
99 		panic("cv_change_pri: %p not on sleep queue", (void *)t);
100 	sleepq_dequeue(t);
101 	*t_prip = pri;
102 	sleepq_insert(sqp, t);
103 }
104 
105 /*
106  * The sobj_ops vector exports a set of functions needed when a thread
107  * is asleep on a synchronization object of this type.
108  */
109 static sobj_ops_t cv_sobj_ops = {
110 	SOBJ_CV, cv_owner, cv_unsleep, cv_change_pri
111 };
112 
113 /* ARGSUSED */
114 void
115 cv_init(kcondvar_t *cvp, char *name, kcv_type_t type, void *arg)
116 {
117 	((condvar_impl_t *)cvp)->cv_waiters = 0;
118 }
119 
120 /*
121  * cv_destroy is not currently needed, but is part of the DDI.
122  * This is in case cv_init ever needs to allocate something for a cv.
123  */
124 /* ARGSUSED */
125 void
126 cv_destroy(kcondvar_t *cvp)
127 {
128 	ASSERT((((condvar_impl_t *)cvp)->cv_waiters & CV_WAITERS_MASK) == 0);
129 }
130 
131 /*
132  * The cv_block() function blocks a thread on a condition variable
133  * by putting it in a hashed sleep queue associated with the
134  * synchronization object.
135  *
136  * Threads are taken off the hashed sleep queues via calls to
137  * cv_signal(), cv_broadcast(), or cv_unsleep().
138  */
139 static void
140 cv_block(condvar_impl_t *cvp)
141 {
142 	kthread_t *t = curthread;
143 	klwp_t *lwp = ttolwp(t);
144 	sleepq_head_t *sqh;
145 
146 	ASSERT(THREAD_LOCK_HELD(t));
147 	ASSERT(t != CPU->cpu_idle_thread);
148 	ASSERT(CPU_ON_INTR(CPU) == 0);
149 	ASSERT(t->t_wchan0 == NULL && t->t_wchan == NULL);
150 	ASSERT(t->t_state == TS_ONPROC);
151 
152 	t->t_schedflag &= ~TS_SIGNALLED;
153 	CL_SLEEP(t);			/* assign kernel priority */
154 	t->t_wchan = (caddr_t)cvp;
155 	t->t_sobj_ops = &cv_sobj_ops;
156 	DTRACE_SCHED(sleep);
157 
158 	/*
159 	 * The check for t_intr is to avoid doing the
160 	 * account for an interrupt thread on the still-pinned
161 	 * lwp's statistics.
162 	 */
163 	if (lwp != NULL && t->t_intr == NULL) {
164 		lwp->lwp_ru.nvcsw++;
165 		(void) new_mstate(t, LMS_SLEEP);
166 	}
167 
168 	sqh = SQHASH(cvp);
169 	disp_lock_enter_high(&sqh->sq_lock);
170 	if (cvp->cv_waiters < CV_MAX_WAITERS)
171 		cvp->cv_waiters++;
172 	ASSERT(cvp->cv_waiters <= CV_MAX_WAITERS);
173 	THREAD_SLEEP(t, &sqh->sq_lock);
174 	sleepq_insert(&sqh->sq_queue, t);
175 	/*
176 	 * THREAD_SLEEP() moves curthread->t_lockp to point to the
177 	 * lock sqh->sq_lock. This lock is later released by the caller
178 	 * when it calls thread_unlock() on curthread.
179 	 */
180 }
181 
182 #define	cv_block_sig(t, cvp)	\
183 	{ (t)->t_flag |= T_WAKEABLE; cv_block(cvp); }
184 
185 /*
186  * Block on the indicated condition variable and release the
187  * associated kmutex while blocked.
188  */
189 void
190 cv_wait(kcondvar_t *cvp, kmutex_t *mp)
191 {
192 	if (panicstr)
193 		return;
194 	ASSERT(!quiesce_active);
195 
196 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
197 	thread_lock(curthread);			/* lock the thread */
198 	cv_block((condvar_impl_t *)cvp);
199 	thread_unlock_nopreempt(curthread);	/* unlock the waiters field */
200 	mutex_exit(mp);
201 	swtch();
202 	mutex_enter(mp);
203 }
204 
205 static void
206 cv_wakeup(void *arg)
207 {
208 	kthread_t *t = arg;
209 
210 	/*
211 	 * This mutex is acquired and released in order to make sure that
212 	 * the wakeup does not happen before the block itself happens.
213 	 */
214 	mutex_enter(&t->t_wait_mutex);
215 	mutex_exit(&t->t_wait_mutex);
216 	setrun(t);
217 }
218 
219 /*
220  * Same as cv_wait except the thread will unblock at 'tim'
221  * (an absolute time) if it hasn't already unblocked.
222  *
223  * Returns the amount of time left from the original 'tim' value
224  * when it was unblocked.
225  */
226 clock_t
227 cv_timedwait(kcondvar_t *cvp, kmutex_t *mp, clock_t tim)
228 {
229 	hrtime_t hrtim;
230 	clock_t now = ddi_get_lbolt();
231 
232 	if (tim <= now)
233 		return (-1);
234 
235 	hrtim = TICK_TO_NSEC(tim - now);
236 	return (cv_timedwait_hires(cvp, mp, hrtim, nsec_per_tick, 0));
237 }
238 
239 /*
240  * Same as cv_timedwait() except that the third argument is a relative
241  * timeout value, as opposed to an absolute one. There is also a fourth
242  * argument that specifies how accurately the timeout must be implemented.
243  */
244 clock_t
245 cv_reltimedwait(kcondvar_t *cvp, kmutex_t *mp, clock_t delta, time_res_t res)
246 {
247 	hrtime_t exp;
248 
249 	ASSERT(TIME_RES_VALID(res));
250 
251 	if (delta <= 0)
252 		return (-1);
253 
254 	if ((exp = TICK_TO_NSEC(delta)) < 0)
255 		exp = CY_INFINITY;
256 
257 	return (cv_timedwait_hires(cvp, mp, exp, time_res[res], 0));
258 }
259 
260 clock_t
261 cv_timedwait_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
262     hrtime_t res, int flag)
263 {
264 	kthread_t *t = curthread;
265 	callout_id_t id;
266 	clock_t timeleft;
267 	hrtime_t limit;
268 	int signalled;
269 
270 	if (panicstr)
271 		return (-1);
272 	ASSERT(!quiesce_active);
273 
274 	limit = (flag & CALLOUT_FLAG_ABSOLUTE) ? gethrtime() : 0;
275 	if (tim <= limit)
276 		return (-1);
277 	mutex_enter(&t->t_wait_mutex);
278 	id = timeout_generic(CALLOUT_REALTIME, (void (*)(void *))cv_wakeup, t,
279 	    tim, res, flag);
280 	thread_lock(t);		/* lock the thread */
281 	cv_block((condvar_impl_t *)cvp);
282 	thread_unlock_nopreempt(t);
283 	mutex_exit(&t->t_wait_mutex);
284 	mutex_exit(mp);
285 	swtch();
286 	signalled = (t->t_schedflag & TS_SIGNALLED);
287 	/*
288 	 * Get the time left. untimeout() returns -1 if the timeout has
289 	 * occured or the time remaining.  If the time remaining is zero,
290 	 * the timeout has occured between when we were awoken and
291 	 * we called untimeout.  We will treat this as if the timeout
292 	 * has occured and set timeleft to -1.
293 	 */
294 	timeleft = untimeout_default(id, 0);
295 	mutex_enter(mp);
296 	if (timeleft <= 0) {
297 		timeleft = -1;
298 		if (signalled)	/* avoid consuming the cv_signal() */
299 			cv_signal(cvp);
300 	}
301 	return (timeleft);
302 }
303 
304 int
305 cv_wait_sig(kcondvar_t *cvp, kmutex_t *mp)
306 {
307 	kthread_t *t = curthread;
308 	proc_t *p = ttoproc(t);
309 	klwp_t *lwp = ttolwp(t);
310 	int cancel_pending;
311 	int rval = 1;
312 	int signalled = 0;
313 
314 	if (panicstr)
315 		return (rval);
316 	ASSERT(!quiesce_active);
317 
318 	/*
319 	 * Threads in system processes don't process signals.  This is
320 	 * true both for standard threads of system processes and for
321 	 * interrupt threads which have borrowed their pinned thread's LWP.
322 	 */
323 	if (lwp == NULL || (p->p_flag & SSYS)) {
324 		cv_wait(cvp, mp);
325 		return (rval);
326 	}
327 	ASSERT(t->t_intr == NULL);
328 
329 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
330 	cancel_pending = schedctl_cancel_pending();
331 	lwp->lwp_asleep = 1;
332 	lwp->lwp_sysabort = 0;
333 	thread_lock(t);
334 	cv_block_sig(t, (condvar_impl_t *)cvp);
335 	thread_unlock_nopreempt(t);
336 	mutex_exit(mp);
337 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
338 		setrun(t);
339 	/* ASSERT(no locks are held) */
340 	swtch();
341 	signalled = (t->t_schedflag & TS_SIGNALLED);
342 	t->t_flag &= ~T_WAKEABLE;
343 	mutex_enter(mp);
344 	if (ISSIG_PENDING(t, lwp, p)) {
345 		mutex_exit(mp);
346 		if (issig(FORREAL))
347 			rval = 0;
348 		mutex_enter(mp);
349 	}
350 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
351 		rval = 0;
352 	if (rval != 0 && cancel_pending) {
353 		schedctl_cancel_eintr();
354 		rval = 0;
355 	}
356 	lwp->lwp_asleep = 0;
357 	lwp->lwp_sysabort = 0;
358 	if (rval == 0 && signalled)	/* avoid consuming the cv_signal() */
359 		cv_signal(cvp);
360 	return (rval);
361 }
362 
363 static clock_t
364 cv_timedwait_sig_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
365     hrtime_t res, int flag)
366 {
367 	kthread_t *t = curthread;
368 	proc_t *p = ttoproc(t);
369 	klwp_t *lwp = ttolwp(t);
370 	int cancel_pending = 0;
371 	callout_id_t id;
372 	clock_t rval = 1;
373 	hrtime_t limit;
374 	int signalled = 0;
375 
376 	if (panicstr)
377 		return (rval);
378 	ASSERT(!quiesce_active);
379 
380 	/*
381 	 * Threads in system processes don't process signals.  This is
382 	 * true both for standard threads of system processes and for
383 	 * interrupt threads which have borrowed their pinned thread's LWP.
384 	 */
385 	if (lwp == NULL || (p->p_flag & SSYS))
386 		return (cv_timedwait_hires(cvp, mp, tim, res, flag));
387 	ASSERT(t->t_intr == NULL);
388 
389 	/*
390 	 * If tim is less than or equal to current hrtime, then the timeout
391 	 * has already occured.  So just check to see if there is a signal
392 	 * pending.  If so return 0 indicating that there is a signal pending.
393 	 * Else return -1 indicating that the timeout occured. No need to
394 	 * wait on anything.
395 	 */
396 	limit = (flag & CALLOUT_FLAG_ABSOLUTE) ? gethrtime() : 0;
397 	if (tim <= limit) {
398 		lwp->lwp_asleep = 1;
399 		lwp->lwp_sysabort = 0;
400 		rval = -1;
401 		goto out;
402 	}
403 
404 	/*
405 	 * Set the timeout and wait.
406 	 */
407 	cancel_pending = schedctl_cancel_pending();
408 	mutex_enter(&t->t_wait_mutex);
409 	id = timeout_generic(CALLOUT_REALTIME, (void (*)(void *))cv_wakeup, t,
410 	    tim, res, flag);
411 	lwp->lwp_asleep = 1;
412 	lwp->lwp_sysabort = 0;
413 	thread_lock(t);
414 	cv_block_sig(t, (condvar_impl_t *)cvp);
415 	thread_unlock_nopreempt(t);
416 	mutex_exit(&t->t_wait_mutex);
417 	mutex_exit(mp);
418 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
419 		setrun(t);
420 	/* ASSERT(no locks are held) */
421 	swtch();
422 	signalled = (t->t_schedflag & TS_SIGNALLED);
423 	t->t_flag &= ~T_WAKEABLE;
424 
425 	/*
426 	 * Untimeout the thread.  untimeout() returns -1 if the timeout has
427 	 * occured or the time remaining.  If the time remaining is zero,
428 	 * the timeout has occured between when we were awoken and
429 	 * we called untimeout.  We will treat this as if the timeout
430 	 * has occured and set rval to -1.
431 	 */
432 	rval = untimeout_default(id, 0);
433 	mutex_enter(mp);
434 	if (rval <= 0)
435 		rval = -1;
436 
437 	/*
438 	 * Check to see if a signal is pending.  If so, regardless of whether
439 	 * or not we were awoken due to the signal, the signal is now pending
440 	 * and a return of 0 has the highest priority.
441 	 */
442 out:
443 	if (ISSIG_PENDING(t, lwp, p)) {
444 		mutex_exit(mp);
445 		if (issig(FORREAL))
446 			rval = 0;
447 		mutex_enter(mp);
448 	}
449 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
450 		rval = 0;
451 	if (rval != 0 && cancel_pending) {
452 		schedctl_cancel_eintr();
453 		rval = 0;
454 	}
455 	lwp->lwp_asleep = 0;
456 	lwp->lwp_sysabort = 0;
457 	if (rval <= 0 && signalled)	/* avoid consuming the cv_signal() */
458 		cv_signal(cvp);
459 	return (rval);
460 }
461 
462 /*
463  * Returns:
464  * 	Function result in order of precedence:
465  *		 0 if a signal was received
466  *		-1 if timeout occured
467  *		>0 if awakened via cv_signal() or cv_broadcast().
468  *		   (returns time remaining)
469  *
470  * cv_timedwait_sig() is now part of the DDI.
471  *
472  * This function is now just a wrapper for cv_timedwait_sig_hires().
473  */
474 clock_t
475 cv_timedwait_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t tim)
476 {
477 	hrtime_t hrtim;
478 
479 	hrtim = TICK_TO_NSEC(tim - ddi_get_lbolt());
480 	return (cv_timedwait_sig_hires(cvp, mp, hrtim, nsec_per_tick, 0));
481 }
482 
483 /*
484  * Same as cv_timedwait_sig() except that the third argument is a relative
485  * timeout value, as opposed to an absolute one. There is also a fourth
486  * argument that specifies how accurately the timeout must be implemented.
487  */
488 clock_t
489 cv_reltimedwait_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t delta,
490     time_res_t res)
491 {
492 	hrtime_t exp = 0;
493 
494 	ASSERT(TIME_RES_VALID(res));
495 
496 	if (delta > 0) {
497 		if ((exp = TICK_TO_NSEC(delta)) < 0)
498 			exp = CY_INFINITY;
499 	}
500 
501 	return (cv_timedwait_sig_hires(cvp, mp, exp, time_res[res], 0));
502 }
503 
504 /*
505  * Same as cv_reltimedwait_sig() except that the timeout is optional. If
506  * there is no timeout then the function will block until woken up
507  * or interrupted.
508  */
509 clock_t
510 cv_relwaituntil_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t *delta,
511     time_res_t res)
512 {
513 	/*
514 	 * If there is no timeout specified wait indefinitely for a
515 	 * signal or a wakeup.
516 	 */
517 	if (delta == NULL) {
518 		return (cv_wait_sig_swap(cvp, mp));
519 	}
520 
521 	/*
522 	 * cv_reltimedwait_sig will wait for the relative timeout
523 	 * specified by delta.
524 	 */
525 	return (cv_reltimedwait_sig(cvp, mp, *delta, res));
526 }
527 
528 /*
529  * Like cv_wait_sig_swap but allows the caller to indicate (with a
530  * non-NULL sigret) that they will take care of signalling the cv
531  * after wakeup, if necessary.  This is a vile hack that should only
532  * be used when no other option is available; almost all callers
533  * should just use cv_wait_sig_swap (which takes care of the cv_signal
534  * stuff automatically) instead.
535  */
536 int
537 cv_wait_sig_swap_core(kcondvar_t *cvp, kmutex_t *mp, int *sigret)
538 {
539 	kthread_t *t = curthread;
540 	proc_t *p = ttoproc(t);
541 	klwp_t *lwp = ttolwp(t);
542 	int cancel_pending;
543 	int rval = 1;
544 	int signalled = 0;
545 
546 	if (panicstr)
547 		return (rval);
548 
549 	/*
550 	 * Threads in system processes don't process signals.  This is
551 	 * true both for standard threads of system processes and for
552 	 * interrupt threads which have borrowed their pinned thread's LWP.
553 	 */
554 	if (lwp == NULL || (p->p_flag & SSYS)) {
555 		cv_wait(cvp, mp);
556 		return (rval);
557 	}
558 	ASSERT(t->t_intr == NULL);
559 
560 	cancel_pending = schedctl_cancel_pending();
561 	lwp->lwp_asleep = 1;
562 	lwp->lwp_sysabort = 0;
563 	thread_lock(t);
564 	t->t_kpri_req = 0;	/* don't need kernel priority */
565 	cv_block_sig(t, (condvar_impl_t *)cvp);
566 	/* I can be swapped now */
567 	curthread->t_schedflag &= ~TS_DONT_SWAP;
568 	thread_unlock_nopreempt(t);
569 	mutex_exit(mp);
570 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
571 		setrun(t);
572 	/* ASSERT(no locks are held) */
573 	swtch();
574 	signalled = (t->t_schedflag & TS_SIGNALLED);
575 	t->t_flag &= ~T_WAKEABLE;
576 	/* TS_DONT_SWAP set by disp() */
577 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
578 	mutex_enter(mp);
579 	if (ISSIG_PENDING(t, lwp, p)) {
580 		mutex_exit(mp);
581 		if (issig(FORREAL))
582 			rval = 0;
583 		mutex_enter(mp);
584 	}
585 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
586 		rval = 0;
587 	if (rval != 0 && cancel_pending) {
588 		schedctl_cancel_eintr();
589 		rval = 0;
590 	}
591 	lwp->lwp_asleep = 0;
592 	lwp->lwp_sysabort = 0;
593 	if (rval == 0) {
594 		if (sigret != NULL)
595 			*sigret = signalled;	/* just tell the caller */
596 		else if (signalled)
597 			cv_signal(cvp);	/* avoid consuming the cv_signal() */
598 	}
599 	return (rval);
600 }
601 
602 /*
603  * Same as cv_wait_sig but the thread can be swapped out while waiting.
604  * This should only be used when we know we aren't holding any locks.
605  */
606 int
607 cv_wait_sig_swap(kcondvar_t *cvp, kmutex_t *mp)
608 {
609 	return (cv_wait_sig_swap_core(cvp, mp, NULL));
610 }
611 
612 void
613 cv_signal(kcondvar_t *cvp)
614 {
615 	condvar_impl_t *cp = (condvar_impl_t *)cvp;
616 
617 	/* make sure the cv_waiters field looks sane */
618 	ASSERT(cp->cv_waiters <= CV_MAX_WAITERS);
619 	if (cp->cv_waiters > 0) {
620 		sleepq_head_t *sqh = SQHASH(cp);
621 		disp_lock_enter(&sqh->sq_lock);
622 		ASSERT(CPU_ON_INTR(CPU) == 0);
623 		if (cp->cv_waiters & CV_WAITERS_MASK) {
624 			kthread_t *t;
625 			cp->cv_waiters--;
626 			t = sleepq_wakeone_chan(&sqh->sq_queue, cp);
627 			/*
628 			 * If cv_waiters is non-zero (and less than
629 			 * CV_MAX_WAITERS) there should be a thread
630 			 * in the queue.
631 			 */
632 			ASSERT(t != NULL);
633 		} else if (sleepq_wakeone_chan(&sqh->sq_queue, cp) == NULL) {
634 			cp->cv_waiters = 0;
635 		}
636 		disp_lock_exit(&sqh->sq_lock);
637 	}
638 }
639 
640 void
641 cv_broadcast(kcondvar_t *cvp)
642 {
643 	condvar_impl_t *cp = (condvar_impl_t *)cvp;
644 
645 	/* make sure the cv_waiters field looks sane */
646 	ASSERT(cp->cv_waiters <= CV_MAX_WAITERS);
647 	if (cp->cv_waiters > 0) {
648 		sleepq_head_t *sqh = SQHASH(cp);
649 		disp_lock_enter(&sqh->sq_lock);
650 		ASSERT(CPU_ON_INTR(CPU) == 0);
651 		sleepq_wakeall_chan(&sqh->sq_queue, cp);
652 		cp->cv_waiters = 0;
653 		disp_lock_exit(&sqh->sq_lock);
654 	}
655 }
656 
657 /*
658  * Same as cv_wait(), but wakes up (after wakeup_time milliseconds) to check
659  * for requests to stop, like cv_wait_sig() but without dealing with signals.
660  * This is a horrible kludge.  It is evil.  It is vile.  It is swill.
661  * If your code has to call this function then your code is the same.
662  */
663 void
664 cv_wait_stop(kcondvar_t *cvp, kmutex_t *mp, int wakeup_time)
665 {
666 	kthread_t *t = curthread;
667 	klwp_t *lwp = ttolwp(t);
668 	proc_t *p = ttoproc(t);
669 	callout_id_t id;
670 	clock_t tim;
671 
672 	if (panicstr)
673 		return;
674 
675 	/*
676 	 * Threads in system processes don't process signals.  This is
677 	 * true both for standard threads of system processes and for
678 	 * interrupt threads which have borrowed their pinned thread's LWP.
679 	 */
680 	if (lwp == NULL || (p->p_flag & SSYS)) {
681 		cv_wait(cvp, mp);
682 		return;
683 	}
684 	ASSERT(t->t_intr == NULL);
685 
686 	/*
687 	 * Wakeup in wakeup_time milliseconds, i.e., human time.
688 	 */
689 	tim = ddi_get_lbolt() + MSEC_TO_TICK(wakeup_time);
690 	mutex_enter(&t->t_wait_mutex);
691 	id = realtime_timeout_default((void (*)(void *))cv_wakeup, t,
692 	    tim - ddi_get_lbolt());
693 	thread_lock(t);			/* lock the thread */
694 	cv_block((condvar_impl_t *)cvp);
695 	thread_unlock_nopreempt(t);
696 	mutex_exit(&t->t_wait_mutex);
697 	mutex_exit(mp);
698 	/* ASSERT(no locks are held); */
699 	swtch();
700 	(void) untimeout_default(id, 0);
701 
702 	/*
703 	 * Check for reasons to stop, if lwp_nostop is not true.
704 	 * See issig_forreal() for explanations of the various stops.
705 	 */
706 	mutex_enter(&p->p_lock);
707 	while (lwp->lwp_nostop == 0 && !(p->p_flag & SEXITLWPS)) {
708 		/*
709 		 * Hold the lwp here for watchpoint manipulation.
710 		 */
711 		if (t->t_proc_flag & TP_PAUSE) {
712 			stop(PR_SUSPENDED, SUSPEND_PAUSE);
713 			continue;
714 		}
715 		/*
716 		 * System checkpoint.
717 		 */
718 		if (t->t_proc_flag & TP_CHKPT) {
719 			stop(PR_CHECKPOINT, 0);
720 			continue;
721 		}
722 		/*
723 		 * Honor fork1(), watchpoint activity (remapping a page),
724 		 * and lwp_suspend() requests.
725 		 */
726 		if ((p->p_flag & (SHOLDFORK1|SHOLDWATCH)) ||
727 		    (t->t_proc_flag & TP_HOLDLWP)) {
728 			stop(PR_SUSPENDED, SUSPEND_NORMAL);
729 			continue;
730 		}
731 		/*
732 		 * Honor /proc requested stop.
733 		 */
734 		if (t->t_proc_flag & TP_PRSTOP) {
735 			stop(PR_REQUESTED, 0);
736 		}
737 		/*
738 		 * If some lwp in the process has already stopped
739 		 * showing PR_JOBCONTROL, stop in sympathy with it.
740 		 */
741 		if (p->p_stopsig && t != p->p_agenttp) {
742 			stop(PR_JOBCONTROL, p->p_stopsig);
743 			continue;
744 		}
745 		break;
746 	}
747 	mutex_exit(&p->p_lock);
748 	mutex_enter(mp);
749 }
750 
751 /*
752  * Like cv_timedwait_sig(), but takes an absolute hires future time
753  * rather than a future time in clock ticks.  Will not return showing
754  * that a timeout occurred until the future time is passed.
755  * If 'when' is a NULL pointer, no timeout will occur.
756  * Returns:
757  * 	Function result in order of precedence:
758  *		 0 if a signal was received
759  *		-1 if timeout occured
760  *	        >0 if awakened via cv_signal() or cv_broadcast()
761  *		   or by a spurious wakeup.
762  *		   (might return time remaining)
763  * As a special test, if someone abruptly resets the system time
764  * (but not through adjtime(2); drifting of the clock is allowed and
765  * expected [see timespectohz_adj()]), then we force a return of -1
766  * so the caller can return a premature timeout to the calling process
767  * so it can reevaluate the situation in light of the new system time.
768  * (The system clock has been reset if timecheck != timechanged.)
769  */
770 int
771 cv_waituntil_sig(kcondvar_t *cvp, kmutex_t *mp,
772 	timestruc_t *when, int timecheck)
773 {
774 	timestruc_t now;
775 	timestruc_t delta;
776 	hrtime_t interval;
777 	int rval;
778 
779 	if (when == NULL)
780 		return (cv_wait_sig_swap(cvp, mp));
781 
782 	gethrestime(&now);
783 	delta = *when;
784 	timespecsub(&delta, &now);
785 	if (delta.tv_sec < 0 || (delta.tv_sec == 0 && delta.tv_nsec == 0)) {
786 		/*
787 		 * We have already reached the absolute future time.
788 		 * Call cv_timedwait_sig() just to check for signals.
789 		 * We will return immediately with either 0 or -1.
790 		 */
791 		rval = cv_timedwait_sig_hires(cvp, mp, 0, 1, 0);
792 	} else {
793 		if (timecheck == timechanged) {
794 			/*
795 			 * Make sure that the interval is atleast one tick.
796 			 * This is to prevent a user from flooding the system
797 			 * with very small, high resolution timers.
798 			 */
799 			interval = ts2hrt(&delta);
800 			if (interval < nsec_per_tick)
801 				interval = nsec_per_tick;
802 			rval = cv_timedwait_sig_hires(cvp, mp, interval, 1,
803 			    CALLOUT_FLAG_HRESTIME);
804 		} else {
805 			/*
806 			 * Someone reset the system time;
807 			 * just force an immediate timeout.
808 			 */
809 			rval = -1;
810 		}
811 		if (rval == -1 && timecheck == timechanged) {
812 			/*
813 			 * Even though cv_timedwait_sig() returned showing a
814 			 * timeout, the future time may not have passed yet.
815 			 * If not, change rval to indicate a normal wakeup.
816 			 */
817 			gethrestime(&now);
818 			delta = *when;
819 			timespecsub(&delta, &now);
820 			if (delta.tv_sec > 0 || (delta.tv_sec == 0 &&
821 			    delta.tv_nsec > 0))
822 				rval = 1;
823 		}
824 	}
825 	return (rval);
826 }
827