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