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 #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 * Threads in system processes don't process signals. This is 318 * true both for standard threads of system processes and for 319 * interrupt threads which have borrowed their pinned thread's LWP. 320 */ 321 if (lwp == NULL || (p->p_flag & SSYS)) { 322 cv_wait(cvp, mp); 323 return (rval); 324 } 325 ASSERT(t->t_intr == NULL); 326 327 ASSERT(curthread->t_schedflag & TS_DONT_SWAP); 328 cancel_pending = schedctl_cancel_pending(); 329 lwp->lwp_asleep = 1; 330 lwp->lwp_sysabort = 0; 331 thread_lock(t); 332 cv_block_sig(t, (condvar_impl_t *)cvp); 333 thread_unlock_nopreempt(t); 334 mutex_exit(mp); 335 if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending) 336 setrun(t); 337 /* ASSERT(no locks are held) */ 338 swtch(); 339 signalled = (t->t_schedflag & TS_SIGNALLED); 340 t->t_flag &= ~T_WAKEABLE; 341 mutex_enter(mp); 342 if (ISSIG_PENDING(t, lwp, p)) { 343 mutex_exit(mp); 344 if (issig(FORREAL)) 345 rval = 0; 346 mutex_enter(mp); 347 } 348 if (lwp->lwp_sysabort || MUSTRETURN(p, t)) 349 rval = 0; 350 if (rval != 0 && cancel_pending) { 351 schedctl_cancel_eintr(); 352 rval = 0; 353 } 354 lwp->lwp_asleep = 0; 355 lwp->lwp_sysabort = 0; 356 if (rval == 0 && signalled) /* avoid consuming the cv_signal() */ 357 cv_signal(cvp); 358 return (rval); 359 } 360 361 static clock_t 362 cv_timedwait_sig_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim, 363 hrtime_t res, int flag) 364 { 365 kthread_t *t = curthread; 366 proc_t *p = ttoproc(t); 367 klwp_t *lwp = ttolwp(t); 368 int cancel_pending = 0; 369 callout_id_t id; 370 clock_t rval = 1; 371 hrtime_t limit; 372 int signalled = 0; 373 374 if (panicstr) 375 return (rval); 376 ASSERT(!quiesce_active); 377 378 /* 379 * Threads in system processes don't process signals. This is 380 * true both for standard threads of system processes and for 381 * interrupt threads which have borrowed their pinned thread's LWP. 382 */ 383 if (lwp == NULL || (p->p_flag & SSYS)) 384 return (cv_timedwait_hires(cvp, mp, tim, res, flag)); 385 ASSERT(t->t_intr == NULL); 386 387 /* 388 * If tim is less than or equal to current hrtime, then the timeout 389 * has already occured. So just check to see if there is a signal 390 * pending. If so return 0 indicating that there is a signal pending. 391 * Else return -1 indicating that the timeout occured. No need to 392 * wait on anything. 393 */ 394 limit = (flag & CALLOUT_FLAG_ABSOLUTE) ? gethrtime() : 0; 395 if (tim <= limit) { 396 lwp->lwp_asleep = 1; 397 lwp->lwp_sysabort = 0; 398 rval = -1; 399 goto out; 400 } 401 402 /* 403 * Set the timeout and wait. 404 */ 405 cancel_pending = schedctl_cancel_pending(); 406 mutex_enter(&t->t_wait_mutex); 407 id = timeout_generic(CALLOUT_REALTIME, (void (*)(void *))cv_wakeup, t, 408 tim, res, flag); 409 lwp->lwp_asleep = 1; 410 lwp->lwp_sysabort = 0; 411 thread_lock(t); 412 cv_block_sig(t, (condvar_impl_t *)cvp); 413 thread_unlock_nopreempt(t); 414 mutex_exit(&t->t_wait_mutex); 415 mutex_exit(mp); 416 if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending) 417 setrun(t); 418 /* ASSERT(no locks are held) */ 419 swtch(); 420 signalled = (t->t_schedflag & TS_SIGNALLED); 421 t->t_flag &= ~T_WAKEABLE; 422 423 /* 424 * Untimeout the thread. untimeout() returns -1 if the timeout has 425 * occured or the time remaining. If the time remaining is zero, 426 * the timeout has occured between when we were awoken and 427 * we called untimeout. We will treat this as if the timeout 428 * has occured and set rval to -1. 429 */ 430 rval = untimeout_default(id, 0); 431 mutex_enter(mp); 432 if (rval <= 0) 433 rval = -1; 434 435 /* 436 * Check to see if a signal is pending. If so, regardless of whether 437 * or not we were awoken due to the signal, the signal is now pending 438 * and a return of 0 has the highest priority. 439 */ 440 out: 441 if (ISSIG_PENDING(t, lwp, p)) { 442 mutex_exit(mp); 443 if (issig(FORREAL)) 444 rval = 0; 445 mutex_enter(mp); 446 } 447 if (lwp->lwp_sysabort || MUSTRETURN(p, t)) 448 rval = 0; 449 if (rval != 0 && cancel_pending) { 450 schedctl_cancel_eintr(); 451 rval = 0; 452 } 453 lwp->lwp_asleep = 0; 454 lwp->lwp_sysabort = 0; 455 if (rval <= 0 && signalled) /* avoid consuming the cv_signal() */ 456 cv_signal(cvp); 457 return (rval); 458 } 459 460 /* 461 * Returns: 462 * Function result in order of precedence: 463 * 0 if a signal was received 464 * -1 if timeout occured 465 * >0 if awakened via cv_signal() or cv_broadcast(). 466 * (returns time remaining) 467 * 468 * cv_timedwait_sig() is now part of the DDI. 469 * 470 * This function is now just a wrapper for cv_timedwait_sig_hires(). 471 */ 472 clock_t 473 cv_timedwait_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t tim) 474 { 475 hrtime_t hrtim; 476 477 hrtim = TICK_TO_NSEC(tim - ddi_get_lbolt()); 478 return (cv_timedwait_sig_hires(cvp, mp, hrtim, nsec_per_tick, 0)); 479 } 480 481 /* 482 * Same as cv_timedwait_sig() except that the third argument is a relative 483 * timeout value, as opposed to an absolute one. There is also a fourth 484 * argument that specifies how accurately the timeout must be implemented. 485 */ 486 clock_t 487 cv_reltimedwait_sig(kcondvar_t *cvp, kmutex_t *mp, clock_t delta, 488 time_res_t res) 489 { 490 hrtime_t exp = 0; 491 492 ASSERT(TIME_RES_VALID(res)); 493 494 if (delta > 0) { 495 if ((exp = TICK_TO_NSEC(delta)) < 0) 496 exp = CY_INFINITY; 497 } 498 499 return (cv_timedwait_sig_hires(cvp, mp, exp, time_res[res], 0)); 500 } 501 502 /* 503 * Like cv_wait_sig_swap but allows the caller to indicate (with a 504 * non-NULL sigret) that they will take care of signalling the cv 505 * after wakeup, if necessary. This is a vile hack that should only 506 * be used when no other option is available; almost all callers 507 * should just use cv_wait_sig_swap (which takes care of the cv_signal 508 * stuff automatically) instead. 509 */ 510 int 511 cv_wait_sig_swap_core(kcondvar_t *cvp, kmutex_t *mp, int *sigret) 512 { 513 kthread_t *t = curthread; 514 proc_t *p = ttoproc(t); 515 klwp_t *lwp = ttolwp(t); 516 int cancel_pending; 517 int rval = 1; 518 int signalled = 0; 519 520 if (panicstr) 521 return (rval); 522 523 /* 524 * Threads in system processes don't process signals. This is 525 * true both for standard threads of system processes and for 526 * interrupt threads which have borrowed their pinned thread's LWP. 527 */ 528 if (lwp == NULL || (p->p_flag & SSYS)) { 529 cv_wait(cvp, mp); 530 return (rval); 531 } 532 ASSERT(t->t_intr == NULL); 533 534 cancel_pending = schedctl_cancel_pending(); 535 lwp->lwp_asleep = 1; 536 lwp->lwp_sysabort = 0; 537 thread_lock(t); 538 t->t_kpri_req = 0; /* don't need kernel priority */ 539 cv_block_sig(t, (condvar_impl_t *)cvp); 540 /* I can be swapped now */ 541 curthread->t_schedflag &= ~TS_DONT_SWAP; 542 thread_unlock_nopreempt(t); 543 mutex_exit(mp); 544 if (ISSIG(t, JUSTLOOKING) || MUSTRETURN(p, t) || cancel_pending) 545 setrun(t); 546 /* ASSERT(no locks are held) */ 547 swtch(); 548 signalled = (t->t_schedflag & TS_SIGNALLED); 549 t->t_flag &= ~T_WAKEABLE; 550 /* TS_DONT_SWAP set by disp() */ 551 ASSERT(curthread->t_schedflag & TS_DONT_SWAP); 552 mutex_enter(mp); 553 if (ISSIG_PENDING(t, lwp, p)) { 554 mutex_exit(mp); 555 if (issig(FORREAL)) 556 rval = 0; 557 mutex_enter(mp); 558 } 559 if (lwp->lwp_sysabort || MUSTRETURN(p, t)) 560 rval = 0; 561 if (rval != 0 && cancel_pending) { 562 schedctl_cancel_eintr(); 563 rval = 0; 564 } 565 lwp->lwp_asleep = 0; 566 lwp->lwp_sysabort = 0; 567 if (rval == 0) { 568 if (sigret != NULL) 569 *sigret = signalled; /* just tell the caller */ 570 else if (signalled) 571 cv_signal(cvp); /* avoid consuming the cv_signal() */ 572 } 573 return (rval); 574 } 575 576 /* 577 * Same as cv_wait_sig but the thread can be swapped out while waiting. 578 * This should only be used when we know we aren't holding any locks. 579 */ 580 int 581 cv_wait_sig_swap(kcondvar_t *cvp, kmutex_t *mp) 582 { 583 return (cv_wait_sig_swap_core(cvp, mp, NULL)); 584 } 585 586 void 587 cv_signal(kcondvar_t *cvp) 588 { 589 condvar_impl_t *cp = (condvar_impl_t *)cvp; 590 591 /* make sure the cv_waiters field looks sane */ 592 ASSERT(cp->cv_waiters <= CV_MAX_WAITERS); 593 if (cp->cv_waiters > 0) { 594 sleepq_head_t *sqh = SQHASH(cp); 595 disp_lock_enter(&sqh->sq_lock); 596 ASSERT(CPU_ON_INTR(CPU) == 0); 597 if (cp->cv_waiters & CV_WAITERS_MASK) { 598 kthread_t *t; 599 cp->cv_waiters--; 600 t = sleepq_wakeone_chan(&sqh->sq_queue, cp); 601 /* 602 * If cv_waiters is non-zero (and less than 603 * CV_MAX_WAITERS) there should be a thread 604 * in the queue. 605 */ 606 ASSERT(t != NULL); 607 } else if (sleepq_wakeone_chan(&sqh->sq_queue, cp) == NULL) { 608 cp->cv_waiters = 0; 609 } 610 disp_lock_exit(&sqh->sq_lock); 611 } 612 } 613 614 void 615 cv_broadcast(kcondvar_t *cvp) 616 { 617 condvar_impl_t *cp = (condvar_impl_t *)cvp; 618 619 /* make sure the cv_waiters field looks sane */ 620 ASSERT(cp->cv_waiters <= CV_MAX_WAITERS); 621 if (cp->cv_waiters > 0) { 622 sleepq_head_t *sqh = SQHASH(cp); 623 disp_lock_enter(&sqh->sq_lock); 624 ASSERT(CPU_ON_INTR(CPU) == 0); 625 sleepq_wakeall_chan(&sqh->sq_queue, cp); 626 cp->cv_waiters = 0; 627 disp_lock_exit(&sqh->sq_lock); 628 } 629 } 630 631 /* 632 * Same as cv_wait(), but wakes up (after wakeup_time milliseconds) to check 633 * for requests to stop, like cv_wait_sig() but without dealing with signals. 634 * This is a horrible kludge. It is evil. It is vile. It is swill. 635 * If your code has to call this function then your code is the same. 636 */ 637 void 638 cv_wait_stop(kcondvar_t *cvp, kmutex_t *mp, int wakeup_time) 639 { 640 kthread_t *t = curthread; 641 klwp_t *lwp = ttolwp(t); 642 proc_t *p = ttoproc(t); 643 callout_id_t id; 644 clock_t tim; 645 646 if (panicstr) 647 return; 648 649 /* 650 * Threads in system processes don't process signals. This is 651 * true both for standard threads of system processes and for 652 * interrupt threads which have borrowed their pinned thread's LWP. 653 */ 654 if (lwp == NULL || (p->p_flag & SSYS)) { 655 cv_wait(cvp, mp); 656 return; 657 } 658 ASSERT(t->t_intr == NULL); 659 660 /* 661 * Wakeup in wakeup_time milliseconds, i.e., human time. 662 */ 663 tim = ddi_get_lbolt() + MSEC_TO_TICK(wakeup_time); 664 mutex_enter(&t->t_wait_mutex); 665 id = realtime_timeout_default((void (*)(void *))cv_wakeup, t, 666 tim - ddi_get_lbolt()); 667 thread_lock(t); /* lock the thread */ 668 cv_block((condvar_impl_t *)cvp); 669 thread_unlock_nopreempt(t); 670 mutex_exit(&t->t_wait_mutex); 671 mutex_exit(mp); 672 /* ASSERT(no locks are held); */ 673 swtch(); 674 (void) untimeout_default(id, 0); 675 676 /* 677 * Check for reasons to stop, if lwp_nostop is not true. 678 * See issig_forreal() for explanations of the various stops. 679 */ 680 mutex_enter(&p->p_lock); 681 while (lwp->lwp_nostop == 0 && !(p->p_flag & SEXITLWPS)) { 682 /* 683 * Hold the lwp here for watchpoint manipulation. 684 */ 685 if (t->t_proc_flag & TP_PAUSE) { 686 stop(PR_SUSPENDED, SUSPEND_PAUSE); 687 continue; 688 } 689 /* 690 * System checkpoint. 691 */ 692 if (t->t_proc_flag & TP_CHKPT) { 693 stop(PR_CHECKPOINT, 0); 694 continue; 695 } 696 /* 697 * Honor fork1(), watchpoint activity (remapping a page), 698 * and lwp_suspend() requests. 699 */ 700 if ((p->p_flag & (SHOLDFORK1|SHOLDWATCH)) || 701 (t->t_proc_flag & TP_HOLDLWP)) { 702 stop(PR_SUSPENDED, SUSPEND_NORMAL); 703 continue; 704 } 705 /* 706 * Honor /proc requested stop. 707 */ 708 if (t->t_proc_flag & TP_PRSTOP) { 709 stop(PR_REQUESTED, 0); 710 } 711 /* 712 * If some lwp in the process has already stopped 713 * showing PR_JOBCONTROL, stop in sympathy with it. 714 */ 715 if (p->p_stopsig && t != p->p_agenttp) { 716 stop(PR_JOBCONTROL, p->p_stopsig); 717 continue; 718 } 719 break; 720 } 721 mutex_exit(&p->p_lock); 722 mutex_enter(mp); 723 } 724 725 /* 726 * Like cv_timedwait_sig(), but takes an absolute hires future time 727 * rather than a future time in clock ticks. Will not return showing 728 * that a timeout occurred until the future time is passed. 729 * If 'when' is a NULL pointer, no timeout will occur. 730 * Returns: 731 * Function result in order of precedence: 732 * 0 if a signal was received 733 * -1 if timeout occured 734 * >0 if awakened via cv_signal() or cv_broadcast() 735 * or by a spurious wakeup. 736 * (might return time remaining) 737 * As a special test, if someone abruptly resets the system time 738 * (but not through adjtime(2); drifting of the clock is allowed and 739 * expected [see timespectohz_adj()]), then we force a return of -1 740 * so the caller can return a premature timeout to the calling process 741 * so it can reevaluate the situation in light of the new system time. 742 * (The system clock has been reset if timecheck != timechanged.) 743 */ 744 int 745 cv_waituntil_sig(kcondvar_t *cvp, kmutex_t *mp, 746 timestruc_t *when, int timecheck) 747 { 748 timestruc_t now; 749 timestruc_t delta; 750 hrtime_t interval; 751 int rval; 752 753 if (when == NULL) 754 return (cv_wait_sig_swap(cvp, mp)); 755 756 gethrestime(&now); 757 delta = *when; 758 timespecsub(&delta, &now); 759 if (delta.tv_sec < 0 || (delta.tv_sec == 0 && delta.tv_nsec == 0)) { 760 /* 761 * We have already reached the absolute future time. 762 * Call cv_timedwait_sig() just to check for signals. 763 * We will return immediately with either 0 or -1. 764 */ 765 rval = cv_timedwait_sig_hires(cvp, mp, 0, 1, 0); 766 } else { 767 if (timecheck == timechanged) { 768 /* 769 * Make sure that the interval is atleast one tick. 770 * This is to prevent a user from flooding the system 771 * with very small, high resolution timers. 772 */ 773 interval = ts2hrt(&delta); 774 if (interval < nsec_per_tick) 775 interval = nsec_per_tick; 776 rval = cv_timedwait_sig_hires(cvp, mp, interval, 1, 777 CALLOUT_FLAG_HRESTIME); 778 } else { 779 /* 780 * Someone reset the system time; 781 * just force an immediate timeout. 782 */ 783 rval = -1; 784 } 785 if (rval == -1 && timecheck == timechanged) { 786 /* 787 * Even though cv_timedwait_sig() returned showing a 788 * timeout, the future time may not have passed yet. 789 * If not, change rval to indicate a normal wakeup. 790 */ 791 gethrestime(&now); 792 delta = *when; 793 timespecsub(&delta, &now); 794 if (delta.tv_sec > 0 || (delta.tv_sec == 0 && 795 delta.tv_nsec > 0)) 796 rval = 1; 797 } 798 } 799 return (rval); 800 } 801