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