File: kern/subr_sleepqueue. File revision: r300109 Routine: sleepq_add. Repeated initialization of sq to td->td_sleepqueue in 327 and 347 lines with no other intervening assignments of other sleepqueue to sq. Probably, compiler optimizes this out.
td_sleepqueue in thread structure marked as (k) - accessed only by curthread, thus no need to repeatedly reassign same value to sq.
(In reply to Bulat from comment #1) Earlier assignment is performed only under invariants, i.e. in the debugging kernel. Later one is for the normal control flow. So yes, for the invariants-enabled kernel the second assignment is excessive, but there is no point of optimizing for INVARIANTS, which performs lot of algorithmically unneeded work to make more robust checks.
If the INVARIANTS block didn't have to define a variable we probably wouldn't do it twice. We could fix it but it would have to be a bit uglier: #ifdef INVARIANTS int i; #endif sq = td->td_sleepqueue; #ifdef INVARIANTS for (i = 0; ...) { } ... sq->sq_lock = lock; #endif ... If our style permitted C99-type 'for (int i' that would be a simple way to solve it, but it doesn't.