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