Currently this is mostly done in mi_switch. Unfortunately some of its consumers want thread lock to remain held after return, making it infeasible to handle it there. Even if a flag argument or new variant was introduced it would still mean some callers manipulate the list with the lock.
Therefore I decided to move the manipulation to a place prior to taking it. Reaping is synchronized with a new state (TDS_UNUSED) which maintains the current semantics, but also retains the pessimization of having to deal with it on context switch. Fixing the last part may be a little delicate so I'm leaving it as an exercise for the reader -- most of the benefit is already in place with the patch below.
I added a simple probe to get an idea how often it waits in the first place during poudriere -j 104. 15 minutes of counting spin counts reveals:
> value ------------- Distribution ------------- count
> -1 | 0
> 0 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 2699722
> 1 | 1105
> 2 | 2765
> 4 | 4566
> 8 | 7344
> 16 | 9568
> 32 | 3118
> 64 | 45
> 128 | 19
> 256 | 3
> 512 | 0
> 1024 | 0
> 2048 | 1
> 4096 | 0
>