xref: /illumos-gate/usr/src/uts/common/os/condvar.c (revision 4eaa471005973e11a6110b69fe990530b3b95a38)
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 
193 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
194 	thread_lock(curthread);			/* lock the thread */
195 	cv_block((condvar_impl_t *)cvp);
196 	thread_unlock_nopreempt(curthread);	/* unlock the waiters field */
197 	mutex_exit(mp);
198 	swtch();
199 	mutex_enter(mp);
200 }
201 
202 static void
203 cv_wakeup(void *arg)
204 {
205 	kthread_t *t = arg;
206 
207 	/*
208 	 * This mutex is acquired and released in order to make sure that
209 	 * the wakeup does not happen before the block itself happens.
210 	 */
211 	mutex_enter(&t->t_wait_mutex);
212 	mutex_exit(&t->t_wait_mutex);
213 	setrun(t);
214 }
215 
216 /*
217  * Same as cv_wait except the thread will unblock at 'tim'
218  * (an absolute time) if it hasn't already unblocked.
219  *
220  * Returns the amount of time left from the original 'tim' value
221  * when it was unblocked.
222  */
223 clock_t
224 cv_timedwait(kcondvar_t *cvp, kmutex_t *mp, clock_t tim)
225 {
226 	hrtime_t hrtim;
227 
228 	if (tim <= lbolt)
229 		return (-1);
230 
231 	hrtim = TICK_TO_NSEC(tim - lbolt);
232 	return (cv_timedwait_hires(cvp, mp, hrtim, nsec_per_tick, 0));
233 }
234 
235 clock_t
236 cv_timedwait_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
237     hrtime_t res, int flag)
238 {
239 	kthread_t *t = curthread;
240 	callout_id_t id;
241 	clock_t timeleft;
242 	hrtime_t limit;
243 	int signalled;
244 
245 	if (panicstr)
246 		return (-1);
247 
248 	limit = (flag & CALLOUT_FLAG_ABSOLUTE) ? gethrtime() : 0;
249 	if (tim <= limit)
250 		return (-1);
251 	mutex_enter(&t->t_wait_mutex);
252 	id = timeout_generic(CALLOUT_REALTIME, (void (*)(void *))cv_wakeup, t,
253 	    tim, res, flag);
254 	thread_lock(t);		/* lock the thread */
255 	cv_block((condvar_impl_t *)cvp);
256 	thread_unlock_nopreempt(t);
257 	mutex_exit(&t->t_wait_mutex);
258 	mutex_exit(mp);
259 	swtch();
260 	signalled = (t->t_schedflag & TS_SIGNALLED);
261 	/*
262 	 * Get the time left. untimeout() returns -1 if the timeout has
263 	 * occured or the time remaining.  If the time remaining is zero,
264 	 * the timeout has occured between when we were awoken and
265 	 * we called untimeout.  We will treat this as if the timeout
266 	 * has occured and set timeleft to -1.
267 	 */
268 	timeleft = untimeout_default(id, 0);
269 	mutex_enter(mp);
270 	if (timeleft <= 0) {
271 		timeleft = -1;
272 		if (signalled)	/* avoid consuming the cv_signal() */
273 			cv_signal(cvp);
274 	}
275 	return (timeleft);
276 }
277 
278 int
279 cv_wait_sig(kcondvar_t *cvp, kmutex_t *mp)
280 {
281 	kthread_t *t = curthread;
282 	proc_t *p = ttoproc(t);
283 	klwp_t *lwp = ttolwp(t);
284 	int cancel_pending;
285 	int rval = 1;
286 	int signalled = 0;
287 
288 	if (panicstr)
289 		return (rval);
290 
291 	/*
292 	 * The check for t_intr is to catch an interrupt thread
293 	 * that has not yet unpinned the thread underneath.
294 	 */
295 	if (lwp == NULL || t->t_intr) {
296 		cv_wait(cvp, mp);
297 		return (rval);
298 	}
299 
300 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
301 	cancel_pending = schedctl_cancel_pending();
302 	lwp->lwp_asleep = 1;
303 	lwp->lwp_sysabort = 0;
304 	thread_lock(t);
305 	cv_block_sig(t, (condvar_impl_t *)cvp);
306 	thread_unlock_nopreempt(t);
307 	mutex_exit(mp);
308 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
309 		setrun(t);
310 	/* ASSERT(no locks are held) */
311 	swtch();
312 	signalled = (t->t_schedflag & TS_SIGNALLED);
313 	t->t_flag &= ~T_WAKEABLE;
314 	mutex_enter(mp);
315 	if (ISSIG_PENDING(t, lwp, p)) {
316 		mutex_exit(mp);
317 		if (issig(FORREAL))
318 			rval = 0;
319 		mutex_enter(mp);
320 	}
321 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
322 		rval = 0;
323 	if (rval != 0 && cancel_pending) {
324 		schedctl_cancel_eintr();
325 		rval = 0;
326 	}
327 	lwp->lwp_asleep = 0;
328 	lwp->lwp_sysabort = 0;
329 	if (rval == 0 && signalled)	/* avoid consuming the cv_signal() */
330 		cv_signal(cvp);
331 	return (rval);
332 }
333 
334 static clock_t
335 cv_timedwait_sig_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim,
336     hrtime_t res, int flag)
337 {
338 	kthread_t *t = curthread;
339 	proc_t *p = ttoproc(t);
340 	klwp_t *lwp = ttolwp(t);
341 	int cancel_pending = 0;
342 	callout_id_t id;
343 	clock_t rval = 1;
344 	hrtime_t limit;
345 	int signalled = 0;
346 
347 	if (panicstr)
348 		return (rval);
349 
350 	/*
351 	 * If there is no lwp, then we don't need to wait for a signal.
352 	 * The check for t_intr is to catch an interrupt thread
353 	 * that has not yet unpinned the thread underneath.
354 	 */
355 	if (lwp == NULL || t->t_intr)
356 		return (cv_timedwait_hires(cvp, mp, tim, res, flag));
357 
358 	/*
359 	 * If tim is less than or equal to current hrtime, then the timeout
360 	 * has already occured.  So just check to see if there is a signal
361 	 * pending.  If so return 0 indicating that there is a signal pending.
362 	 * Else return -1 indicating that the timeout occured. No need to
363 	 * wait on anything.
364 	 */
365 	limit = (flag & CALLOUT_FLAG_ABSOLUTE) ? gethrtime() : 0;
366 	if (tim <= limit) {
367 		lwp->lwp_asleep = 1;
368 		lwp->lwp_sysabort = 0;
369 		rval = -1;
370 		goto out;
371 	}
372 
373 	/*
374 	 * Set the timeout and wait.
375 	 */
376 	cancel_pending = schedctl_cancel_pending();
377 	mutex_enter(&t->t_wait_mutex);
378 	id = timeout_generic(CALLOUT_REALTIME, (void (*)(void *))cv_wakeup, t,
379 	    tim, res, flag);
380 	lwp->lwp_asleep = 1;
381 	lwp->lwp_sysabort = 0;
382 	thread_lock(t);
383 	cv_block_sig(t, (condvar_impl_t *)cvp);
384 	thread_unlock_nopreempt(t);
385 	mutex_exit(&t->t_wait_mutex);
386 	mutex_exit(mp);
387 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
388 		setrun(t);
389 	/* ASSERT(no locks are held) */
390 	swtch();
391 	signalled = (t->t_schedflag & TS_SIGNALLED);
392 	t->t_flag &= ~T_WAKEABLE;
393 
394 	/*
395 	 * Untimeout the thread.  untimeout() returns -1 if the timeout has
396 	 * occured or the time remaining.  If the time remaining is zero,
397 	 * the timeout has occured between when we were awoken and
398 	 * we called untimeout.  We will treat this as if the timeout
399 	 * has occured and set rval to -1.
400 	 */
401 	rval = untimeout_default(id, 0);
402 	mutex_enter(mp);
403 	if (rval <= 0)
404 		rval = -1;
405 
406 	/*
407 	 * Check to see if a signal is pending.  If so, regardless of whether
408 	 * or not we were awoken due to the signal, the signal is now pending
409 	 * and a return of 0 has the highest priority.
410 	 */
411 out:
412 	if (ISSIG_PENDING(t, lwp, p)) {
413 		mutex_exit(mp);
414 		if (issig(FORREAL))
415 			rval = 0;
416 		mutex_enter(mp);
417 	}
418 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
419 		rval = 0;
420 	if (rval != 0 && cancel_pending) {
421 		schedctl_cancel_eintr();
422 		rval = 0;
423 	}
424 	lwp->lwp_asleep = 0;
425 	lwp->lwp_sysabort = 0;
426 	if (rval <= 0 && signalled)	/* avoid consuming the cv_signal() */
427 		cv_signal(cvp);
428 	return (rval);
429 }
430 
431 /*
432  * Returns:
433  * 	Function result in order of precedence:
434  *		 0 if a signal was received
435  *		-1 if timeout occured
436  *		>0 if awakened via cv_signal() or cv_broadcast().
437  *		   (returns time remaining)
438  *
439  * cv_timedwait_sig() is now part of the DDI.
440  *
441  * This function is now just a wrapper for cv_timedwait_sig_hires().
442  */
443 clock_t
444 cv_timedwait_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t tim)
445 {
446 	hrtime_t hrtim;
447 
448 	hrtim = TICK_TO_NSEC(tim - lbolt);
449 	return (cv_timedwait_sig_hires(cvp, mp, hrtim, nsec_per_tick, 0));
450 }
451 
452 /*
453  * Like cv_wait_sig_swap but allows the caller to indicate (with a
454  * non-NULL sigret) that they will take care of signalling the cv
455  * after wakeup, if necessary.  This is a vile hack that should only
456  * be used when no other option is available; almost all callers
457  * should just use cv_wait_sig_swap (which takes care of the cv_signal
458  * stuff automatically) instead.
459  */
460 int
461 cv_wait_sig_swap_core(kcondvar_t *cvp, kmutex_t *mp, int *sigret)
462 {
463 	kthread_t *t = curthread;
464 	proc_t *p = ttoproc(t);
465 	klwp_t *lwp = ttolwp(t);
466 	int cancel_pending;
467 	int rval = 1;
468 	int signalled = 0;
469 
470 	if (panicstr)
471 		return (rval);
472 
473 	/*
474 	 * The check for t_intr is to catch an interrupt thread
475 	 * that has not yet unpinned the thread underneath.
476 	 */
477 	if (lwp == NULL || t->t_intr) {
478 		cv_wait(cvp, mp);
479 		return (rval);
480 	}
481 
482 	cancel_pending = schedctl_cancel_pending();
483 	lwp->lwp_asleep = 1;
484 	lwp->lwp_sysabort = 0;
485 	thread_lock(t);
486 	t->t_kpri_req = 0;	/* don't need kernel priority */
487 	cv_block_sig(t, (condvar_impl_t *)cvp);
488 	/* I can be swapped now */
489 	curthread->t_schedflag &= ~TS_DONT_SWAP;
490 	thread_unlock_nopreempt(t);
491 	mutex_exit(mp);
492 	if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending)
493 		setrun(t);
494 	/* ASSERT(no locks are held) */
495 	swtch();
496 	signalled = (t->t_schedflag & TS_SIGNALLED);
497 	t->t_flag &= ~T_WAKEABLE;
498 	/* TS_DONT_SWAP set by disp() */
499 	ASSERT(curthread->t_schedflag & TS_DONT_SWAP);
500 	mutex_enter(mp);
501 	if (ISSIG_PENDING(t, lwp, p)) {
502 		mutex_exit(mp);
503 		if (issig(FORREAL))
504 			rval = 0;
505 		mutex_enter(mp);
506 	}
507 	if (lwp->lwp_sysabort || MUSTRETURN(p, t))
508 		rval = 0;
509 	if (rval != 0 && cancel_pending) {
510 		schedctl_cancel_eintr();
511 		rval = 0;
512 	}
513 	lwp->lwp_asleep = 0;
514 	lwp->lwp_sysabort = 0;
515 	if (rval == 0) {
516 		if (sigret != NULL)
517 			*sigret = signalled;	/* just tell the caller */
518 		else if (signalled)
519 			cv_signal(cvp);	/* avoid consuming the cv_signal() */
520 	}
521 	return (rval);
522 }
523 
524 /*
525  * Same as cv_wait_sig but the thread can be swapped out while waiting.
526  * This should only be used when we know we aren't holding any locks.
527  */
528 int
529 cv_wait_sig_swap(kcondvar_t *cvp, kmutex_t *mp)
530 {
531 	return (cv_wait_sig_swap_core(cvp, mp, NULL));
532 }
533 
534 void
535 cv_signal(kcondvar_t *cvp)
536 {
537 	condvar_impl_t *cp = (condvar_impl_t *)cvp;
538 
539 	/* make sure the cv_waiters field looks sane */
540 	ASSERT(cp->cv_waiters <= CV_MAX_WAITERS);
541 	if (cp->cv_waiters > 0) {
542 		sleepq_head_t *sqh = SQHASH(cp);
543 		disp_lock_enter(&sqh->sq_lock);
544 		ASSERT(CPU_ON_INTR(CPU) == 0);
545 		if (cp->cv_waiters & CV_WAITERS_MASK) {
546 			kthread_t *t;
547 			cp->cv_waiters--;
548 			t = sleepq_wakeone_chan(&sqh->sq_queue, cp);
549 			/*
550 			 * If cv_waiters is non-zero (and less than
551 			 * CV_MAX_WAITERS) there should be a thread
552 			 * in the queue.
553 			 */
554 			ASSERT(t != NULL);
555 		} else if (sleepq_wakeone_chan(&sqh->sq_queue, cp) == NULL) {
556 			cp->cv_waiters = 0;
557 		}
558 		disp_lock_exit(&sqh->sq_lock);
559 	}
560 }
561 
562 void
563 cv_broadcast(kcondvar_t *cvp)
564 {
565 	condvar_impl_t *cp = (condvar_impl_t *)cvp;
566 
567 	/* make sure the cv_waiters field looks sane */
568 	ASSERT(cp->cv_waiters <= CV_MAX_WAITERS);
569 	if (cp->cv_waiters > 0) {
570 		sleepq_head_t *sqh = SQHASH(cp);
571 		disp_lock_enter(&sqh->sq_lock);
572 		ASSERT(CPU_ON_INTR(CPU) == 0);
573 		sleepq_wakeall_chan(&sqh->sq_queue, cp);
574 		cp->cv_waiters = 0;
575 		disp_lock_exit(&sqh->sq_lock);
576 	}
577 }
578 
579 /*
580  * Same as cv_wait(), but wakes up (after wakeup_time milliseconds) to check
581  * for requests to stop, like cv_wait_sig() but without dealing with signals.
582  * This is a horrible kludge.  It is evil.  It is vile.  It is swill.
583  * If your code has to call this function then your code is the same.
584  */
585 void
586 cv_wait_stop(kcondvar_t *cvp, kmutex_t *mp, int wakeup_time)
587 {
588 	kthread_t *t = curthread;
589 	klwp_t *lwp = ttolwp(t);
590 	proc_t *p = ttoproc(t);
591 	callout_id_t id;
592 	clock_t tim;
593 
594 	if (panicstr)
595 		return;
596 
597 	/*
598 	 * If there is no lwp, then we don't need to eventually stop it
599 	 * The check for t_intr is to catch an interrupt thread
600 	 * that has not yet unpinned the thread underneath.
601 	 */
602 	if (lwp == NULL || t->t_intr) {
603 		cv_wait(cvp, mp);
604 		return;
605 	}
606 
607 	/*
608 	 * Wakeup in wakeup_time milliseconds, i.e., human time.
609 	 */
610 	tim = lbolt + MSEC_TO_TICK(wakeup_time);
611 	mutex_enter(&t->t_wait_mutex);
612 	id = realtime_timeout_default((void (*)(void *))cv_wakeup, t,
613 	    tim - lbolt);
614 	thread_lock(t);			/* lock the thread */
615 	cv_block((condvar_impl_t *)cvp);
616 	thread_unlock_nopreempt(t);
617 	mutex_exit(&t->t_wait_mutex);
618 	mutex_exit(mp);
619 	/* ASSERT(no locks are held); */
620 	swtch();
621 	(void) untimeout_default(id, 0);
622 
623 	/*
624 	 * Check for reasons to stop, if lwp_nostop is not true.
625 	 * See issig_forreal() for explanations of the various stops.
626 	 */
627 	mutex_enter(&p->p_lock);
628 	while (lwp->lwp_nostop == 0 && !(p->p_flag & SEXITLWPS)) {
629 		/*
630 		 * Hold the lwp here for watchpoint manipulation.
631 		 */
632 		if (t->t_proc_flag & TP_PAUSE) {
633 			stop(PR_SUSPENDED, SUSPEND_PAUSE);
634 			continue;
635 		}
636 		/*
637 		 * System checkpoint.
638 		 */
639 		if (t->t_proc_flag & TP_CHKPT) {
640 			stop(PR_CHECKPOINT, 0);
641 			continue;
642 		}
643 		/*
644 		 * Honor fork1(), watchpoint activity (remapping a page),
645 		 * and lwp_suspend() requests.
646 		 */
647 		if ((p->p_flag & (SHOLDFORK1|SHOLDWATCH)) ||
648 		    (t->t_proc_flag & TP_HOLDLWP)) {
649 			stop(PR_SUSPENDED, SUSPEND_NORMAL);
650 			continue;
651 		}
652 		/*
653 		 * Honor /proc requested stop.
654 		 */
655 		if (t->t_proc_flag & TP_PRSTOP) {
656 			stop(PR_REQUESTED, 0);
657 		}
658 		/*
659 		 * If some lwp in the process has already stopped
660 		 * showing PR_JOBCONTROL, stop in sympathy with it.
661 		 */
662 		if (p->p_stopsig && t != p->p_agenttp) {
663 			stop(PR_JOBCONTROL, p->p_stopsig);
664 			continue;
665 		}
666 		break;
667 	}
668 	mutex_exit(&p->p_lock);
669 	mutex_enter(mp);
670 }
671 
672 /*
673  * Like cv_timedwait_sig(), but takes an absolute hires future time
674  * rather than a future time in clock ticks.  Will not return showing
675  * that a timeout occurred until the future time is passed.
676  * If 'when' is a NULL pointer, no timeout will occur.
677  * Returns:
678  * 	Function result in order of precedence:
679  *		 0 if a signal was received
680  *		-1 if timeout occured
681  *	        >0 if awakened via cv_signal() or cv_broadcast()
682  *		   or by a spurious wakeup.
683  *		   (might return time remaining)
684  * As a special test, if someone abruptly resets the system time
685  * (but not through adjtime(2); drifting of the clock is allowed and
686  * expected [see timespectohz_adj()]), then we force a return of -1
687  * so the caller can return a premature timeout to the calling process
688  * so it can reevaluate the situation in light of the new system time.
689  * (The system clock has been reset if timecheck != timechanged.)
690  */
691 int
692 cv_waituntil_sig(kcondvar_t *cvp, kmutex_t *mp,
693 	timestruc_t *when, int timecheck)
694 {
695 	timestruc_t now;
696 	timestruc_t delta;
697 	hrtime_t interval;
698 	int rval;
699 
700 	if (when == NULL)
701 		return (cv_wait_sig_swap(cvp, mp));
702 
703 	gethrestime(&now);
704 	delta = *when;
705 	timespecsub(&delta, &now);
706 	if (delta.tv_sec < 0 || (delta.tv_sec == 0 && delta.tv_nsec == 0)) {
707 		/*
708 		 * We have already reached the absolute future time.
709 		 * Call cv_timedwait_sig() just to check for signals.
710 		 * We will return immediately with either 0 or -1.
711 		 */
712 		rval = cv_timedwait_sig_hires(cvp, mp, 0, 1, 0);
713 	} else {
714 		if (timecheck == timechanged) {
715 			/*
716 			 * Make sure that the interval is atleast one tick.
717 			 * This is to prevent a user from flooding the system
718 			 * with very small, high resolution timers.
719 			 */
720 			interval = ts2hrt(&delta);
721 			if (interval < nsec_per_tick)
722 				interval = nsec_per_tick;
723 			rval = cv_timedwait_sig_hires(cvp, mp, interval, 1,
724 			    CALLOUT_FLAG_HRESTIME);
725 		} else {
726 			/*
727 			 * Someone reset the system time;
728 			 * just force an immediate timeout.
729 			 */
730 			rval = -1;
731 		}
732 		if (rval == -1 && timecheck == timechanged) {
733 			/*
734 			 * Even though cv_timedwait_sig() returned showing a
735 			 * timeout, the future time may not have passed yet.
736 			 * If not, change rval to indicate a normal wakeup.
737 			 */
738 			gethrestime(&now);
739 			delta = *when;
740 			timespecsub(&delta, &now);
741 			if (delta.tv_sec > 0 || (delta.tv_sec == 0 &&
742 			    delta.tv_nsec > 0))
743 				rval = 1;
744 		}
745 	}
746 	return (rval);
747 }
748