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