Merge pull request #5641 from eatkins/completion-threads

Name threads in completion service
This commit is contained in:
eugene yokota 2020-06-23 00:45:00 -04:00 committed by GitHub
commit 77a99d34df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 8 deletions

View File

@ -23,18 +23,25 @@ trait CompletionService[A, R] {
def take(): R
}
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.{
Callable,
CompletionService => JCompletionService,
Executor,
Executors,
ExecutorCompletionService,
Executors,
RejectedExecutionException,
CompletionService => JCompletionService
}
object CompletionService {
val poolID = new AtomicInteger(1)
def apply[A, T](poolSize: Int): (CompletionService[A, T], () => Unit) = {
val pool = Executors.newFixedThreadPool(poolSize)
val i = new AtomicInteger(1)
val id = poolID.getAndIncrement()
val pool = Executors.newFixedThreadPool(
poolSize,
(r: Runnable) => new Thread(r, s"sbt-completion-thread-$id-${i.getAndIncrement}")
)
(apply[A, T](pool), () => { pool.shutdownNow(); () })
}
def apply[A, T](x: Executor): CompletionService[A, T] =

View File

@ -7,6 +7,8 @@
package sbt
import java.util.concurrent.atomic.AtomicInteger
import sbt.internal.util.AttributeKey
/**
@ -119,6 +121,8 @@ object ConcurrentRestrictions {
private[this] def merge[A, B](m: Map[A, B], n: Map[A, B])(f: (B, B) => B): Map[A, B] =
n.foldLeft(m) { case (acc, (a, b)) => update(acc, a, b)(f) }
private[this] val poolID = new AtomicInteger(1)
/**
* Constructs a CompletionService suitable for backing task execution based on the provided restrictions on concurrent task execution.
* @return a pair, with _1 being the CompletionService and _2 a function to shutdown the service.
@ -129,11 +133,12 @@ object ConcurrentRestrictions {
tags: ConcurrentRestrictions[A],
warn: String => Unit
): (CompletionService[A, R], () => Unit) = {
val pool = Executors.newCachedThreadPool()
(completionService[A, R](pool, tags, warn), () => {
pool.shutdownNow()
()
})
val id = poolID.getAndIncrement
val i = new AtomicInteger(1)
val pool = Executors.newCachedThreadPool { r =>
new Thread(r, s"sbt-completion-service-pool-$id-${i.getAndIncrement()}")
}
(completionService[A, R](pool, tags, warn), () => { pool.shutdownNow(); () })
}
def completionService[A, R](