Skip to content

Commit 027e003

Browse files
committed
Rename AsyncHelper to ThreadPoolFactory, remove unused implicits
1 parent 466421d commit 027e003

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

compiler/src/dotty/tools/dotc/profile/AsyncHelper.scala renamed to compiler/src/dotty/tools/dotc/profile/ThreadPoolFactory.scala

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,49 @@ import java.util.concurrent.atomic.AtomicInteger
99
import dotty.tools.dotc.core.Phases.Phase
1010
import dotty.tools.dotc.core.Contexts._
1111

12-
sealed trait AsyncHelper {
13-
14-
def newUnboundedQueueFixedThreadPool
15-
(nThreads: Int,
16-
shortId: String, priority : Int = Thread.NORM_PRIORITY) : ThreadPoolExecutor
17-
def newBoundedQueueFixedThreadPool
18-
(nThreads: Int, maxQueueSize: Int, rejectHandler: RejectedExecutionHandler,
19-
shortId: String, priority : Int = Thread.NORM_PRIORITY) : ThreadPoolExecutor
12+
sealed trait ThreadPoolFactory {
13+
14+
def newUnboundedQueueFixedThreadPool(
15+
nThreads: Int,
16+
shortId: String,
17+
priority : Int = Thread.NORM_PRIORITY) : ThreadPoolExecutor
18+
19+
def newBoundedQueueFixedThreadPool(
20+
nThreads: Int,
21+
maxQueueSize: Int,
22+
rejectHandler: RejectedExecutionHandler,
23+
shortId: String,
24+
priority : Int = Thread.NORM_PRIORITY) : ThreadPoolExecutor
2025
}
2126

2227

23-
object AsyncHelper {
24-
def apply(phase: Phase)(using Context): AsyncHelper = ctx.profiler match {
25-
case NoOpProfiler => new BasicAsyncHelper(phase)
26-
case r: RealProfiler => new ProfilingAsyncHelper(phase, r)
28+
object ThreadPoolFactory {
29+
def apply(phase: Phase)(using Context): ThreadPoolFactory = ctx.profiler match {
30+
case NoOpProfiler => new BasicThreadPoolFactory(phase)
31+
case r: RealProfiler => new ProfilingThreadPoolFactory(phase, r)
2732
}
2833

29-
private abstract class BaseAsyncHelper(phase: Phase)(using Context) extends AsyncHelper {
34+
private abstract class BaseThreadPoolFactory(phase: Phase) extends ThreadPoolFactory {
3035
val baseGroup = new ThreadGroup(s"dotc-${phase.phaseName}")
36+
3137
private def childGroup(name: String) = new ThreadGroup(baseGroup, name)
3238

33-
protected def wrapRunnable(r: Runnable, shortId:String): Runnable
39+
// Invoked when a new `Worker` is created, see `CommonThreadFactory.newThread`
40+
protected def wrapWorker(worker: Runnable, shortId:String): Runnable = worker
3441

35-
protected class CommonThreadFactory(shortId: String,
36-
daemon: Boolean = true,
37-
priority: Int) extends ThreadFactory {
42+
protected final class CommonThreadFactory(
43+
shortId: String,
44+
daemon: Boolean = true,
45+
priority: Int) extends ThreadFactory {
3846
private val group: ThreadGroup = childGroup(shortId)
3947
private val threadNumber: AtomicInteger = new AtomicInteger(1)
4048
private val namePrefix = s"${baseGroup.getName}-$shortId-"
4149

42-
override def newThread(r: Runnable): Thread = {
43-
val wrapped = wrapRunnable(r, shortId)
50+
// Invoked by the `ThreadPoolExecutor` when creating a new worker thread. The argument
51+
// runnable is the `Worker` (which extends `Runnable`). Its `run` method gets tasks from
52+
// the thread pool and executes them (on the thread created here).
53+
override def newThread(worker: Runnable): Thread = {
54+
val wrapped = wrapWorker(worker, shortId)
4455
val t: Thread = new Thread(group, wrapped, namePrefix + threadNumber.getAndIncrement, 0)
4556
if (t.isDaemon != daemon) t.setDaemon(daemon)
4657
if (t.getPriority != priority) t.setPriority(priority)
@@ -49,7 +60,7 @@ object AsyncHelper {
4960
}
5061
}
5162

52-
private final class BasicAsyncHelper(phase: Phase)(using Context) extends BaseAsyncHelper(phase) {
63+
private final class BasicThreadPoolFactory(phase: Phase) extends BaseThreadPoolFactory(phase) {
5364

5465
override def newUnboundedQueueFixedThreadPool(nThreads: Int, shortId: String, priority: Int): ThreadPoolExecutor = {
5566
val threadFactory = new CommonThreadFactory(shortId, priority = priority)
@@ -62,11 +73,9 @@ object AsyncHelper {
6273
//like Executors.newFixedThreadPool
6374
new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue[Runnable](maxQueueSize), threadFactory, rejectHandler)
6475
}
65-
66-
override protected def wrapRunnable(r: Runnable, shortId:String): Runnable = r
6776
}
6877

69-
private class ProfilingAsyncHelper(phase: Phase, private val profiler: RealProfiler)(using Context) extends BaseAsyncHelper(phase) {
78+
private class ProfilingThreadPoolFactory(phase: Phase, private val profiler: RealProfiler) extends BaseThreadPoolFactory(phase) {
7079

7180
override def newUnboundedQueueFixedThreadPool(nThreads: Int, shortId: String, priority: Int): ThreadPoolExecutor = {
7281
val threadFactory = new CommonThreadFactory(shortId, priority = priority)
@@ -80,13 +89,13 @@ object AsyncHelper {
8089
new SinglePhaseInstrumentedThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue[Runnable](maxQueueSize), threadFactory, rejectHandler)
8190
}
8291

83-
override protected def wrapRunnable(r: Runnable, shortId:String): Runnable = {
92+
override protected def wrapWorker(worker: Runnable, shortId: String): Runnable = {
8493
() =>
8594
val data = new ThreadProfileData
8695
localData.set(data)
8796

8897
val profileStart = profiler.snapThread(0)
89-
try r.run finally {
98+
try worker.run finally {
9099
val snap = profiler.snapThread(data.idleNs)
91100
val threadRange = ProfileRange(profileStart, snap, phase, shortId, data.taskCount, Thread.currentThread())
92101
profiler.completeBackground(threadRange)

0 commit comments

Comments
 (0)