Skip to content

Commit

Permalink
chore: pick another random thread
Browse files Browse the repository at this point in the history
When checking for a blocked thread, randomly pick a thread other than
self at once, rather than checking after the fact.
  • Loading branch information
João Abecasis authored and biochimia committed Dec 10, 2024
1 parent 728a555 commit 189e412
Showing 1 changed file with 2 additions and 7 deletions.
9 changes: 2 additions & 7 deletions core/jvm/src/main/scala/cats/effect/unsafe/WorkerThread.scala
Original file line number Diff line number Diff line change
Expand Up @@ -550,13 +550,8 @@ private[effect] final class WorkerThread[P <: AnyRef](
// TODO prefetch pool.workerThread or Thread.State.BLOCKED ?
// TODO check that branch elimination makes it free when off
val idx = index
var otherIdx = random.nextInt(pool.getWorkerThreads.length)
if (otherIdx == idx) {
otherIdx =
(idx + Math.max(1, random.nextInt(pool.getWorkerThreads.length - 1))) % pool
.getWorkerThreads
.length
}
val threadCount = pool.getWorkerThreadCount()
val otherIdx = (idx + random.nextInt(threadCount - 1)) % threadCount
val thread = pool.getWorkerThreads(otherIdx)
val state = thread.getState()
val parked = thread.parked
Expand Down

0 comments on commit 189e412

Please sign in to comment.