* Adjust the thread's position on a turnstile after its priority has been * changed. */
| 311 | * changed. |
| 312 | */ |
| 313 | static int |
| 314 | turnstile_adjust_thread(struct turnstile *ts, struct thread *td) |
| 315 | { |
| 316 | struct thread *td1, *td2; |
| 317 | int queue; |
| 318 | |
| 319 | THREAD_LOCK_ASSERT(td, MA_OWNED); |
| 320 | MPASS(TD_ON_LOCK(td)); |
| 321 | |
| 322 | /* |
| 323 | * This thread may not be blocked on this turnstile anymore |
| 324 | * but instead might already be woken up on another CPU |
| 325 | * that is waiting on the thread lock in turnstile_unpend() to |
| 326 | * finish waking this thread up. We can detect this case |
| 327 | * by checking to see if this thread has been given a |
| 328 | * turnstile by either turnstile_signal() or |
| 329 | * turnstile_broadcast(). In this case, treat the thread as |
| 330 | * if it was already running. |
| 331 | */ |
| 332 | if (td->td_turnstile != NULL) |
| 333 | return (0); |
| 334 | |
| 335 | /* |
| 336 | * Check if the thread needs to be moved on the blocked chain. |
| 337 | * It needs to be moved if either its priority is lower than |
| 338 | * the previous thread or higher than the next thread. |
| 339 | */ |
| 340 | THREAD_LOCKPTR_BLOCKED_ASSERT(td, &ts->ts_lock); |
| 341 | td1 = TAILQ_PREV(td, threadqueue, td_lockq); |
| 342 | td2 = TAILQ_NEXT(td, td_lockq); |
| 343 | if ((td1 != NULL && td->td_priority < td1->td_priority) || |
| 344 | (td2 != NULL && td->td_priority > td2->td_priority)) { |
| 345 | /* |
| 346 | * Remove thread from blocked chain and determine where |
| 347 | * it should be moved to. |
| 348 | */ |
| 349 | queue = td->td_tsqueue; |
| 350 | MPASS(queue == TS_EXCLUSIVE_QUEUE || queue == TS_SHARED_QUEUE); |
| 351 | mtx_lock_spin(&td_contested_lock); |
| 352 | TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq); |
| 353 | TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq) { |
| 354 | MPASS(td1->td_proc->p_magic == P_MAGIC); |
| 355 | if (td1->td_priority > td->td_priority) |
| 356 | break; |
| 357 | } |
| 358 | |
| 359 | if (td1 == NULL) |
| 360 | TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq); |
| 361 | else |
| 362 | TAILQ_INSERT_BEFORE(td1, td, td_lockq); |
| 363 | mtx_unlock_spin(&td_contested_lock); |
| 364 | if (td1 == NULL) |
| 365 | CTR3(KTR_LOCK, |
| 366 | "turnstile_adjust_thread: td %d put at tail on [%p] %s", |
| 367 | td->td_tid, ts->ts_lockobj, ts->ts_lockobj->lo_name); |
| 368 | else |
| 369 | CTR4(KTR_LOCK, |
| 370 | "turnstile_adjust_thread: td %d moved before %d on [%p] %s", |
no outgoing calls
no test coverage detected