Skip to content

Commit 3865ba6

Browse files
committed
Replace CancellationThread by java.util.Timer
1 parent 8f7b607 commit 3865ba6

File tree

2 files changed

+21
-39
lines changed

2 files changed

+21
-39
lines changed

language-server/src/dotty/tools/languageserver/worksheet/CancellationThread.scala

Lines changed: 0 additions & 29 deletions
This file was deleted.

language-server/src/dotty/tools/languageserver/worksheet/Evaluator.scala

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import dotty.tools.dotc.core.Contexts.Context
44

55
import java.io.{File, PrintStream, IOException}
66
import java.lang.ProcessBuilder.Redirect
7+
import java.util.concurrent.CancellationException
8+
import java.util.{Timer, TimerTask}
79

810
import org.eclipse.lsp4j.jsonrpc.CancelChecker
911

@@ -32,7 +34,7 @@ private object Evaluator {
3234
def get(cancelChecker: CancelChecker)(implicit ctx: Context): Option[Evaluator] = synchronized {
3335
val classpath = ctx.settings.classpath.value
3436
previousEvaluator match {
35-
case Some(cp, evaluator) if evaluator.isAlive() && cp == classpath =>
37+
case Some(cp, evaluator) if evaluator.isAlive && cp == classpath =>
3638
evaluator.reset(cancelChecker)
3739
Some(evaluator)
3840
case _ =>
@@ -53,7 +55,7 @@ private object Evaluator {
5355
*/
5456
private class Evaluator private (javaExec: String,
5557
userClasspath: String,
56-
cancelChecker: CancelChecker) {
58+
private var cancelChecker: CancelChecker) {
5759
private val process =
5860
new ProcessBuilder(
5961
javaExec,
@@ -70,13 +72,24 @@ private class Evaluator private (javaExec: String,
7072
// Messages coming out of the REPL
7173
private val processOutput = new InputStreamConsumer(process.getInputStream())
7274

73-
// The thread that monitors cancellation
74-
private val cancellationThread = new CancellationThread(cancelChecker, this)
75-
cancellationThread.start()
75+
// A timer that monitors cancellation
76+
private val cancellationTimer = new Timer()
77+
locally {
78+
val task = new TimerTask {
79+
def run(): Unit =
80+
if (!isAlive)
81+
cancellationTimer.cancel()
82+
else
83+
try cancelChecker.checkCanceled()
84+
catch { case _: CancellationException => exit() }
85+
}
86+
val checkCancelledDelayMs = 50L
87+
cancellationTimer.schedule(task, checkCancelledDelayMs, checkCancelledDelayMs)
88+
}
7689

7790

7891
/** Is the process that runs the REPL still alive? */
79-
def isAlive(): Boolean = process.isAlive()
92+
def isAlive: Boolean = process.isAlive
8093

8194
/**
8295
* Submit `command` to the REPL, wait for the result.
@@ -97,16 +110,14 @@ private class Evaluator private (javaExec: String,
97110
* Reset the REPL to its initial state, update the cancel checker.
98111
*/
99112
def reset(cancelChecker: CancelChecker): Unit = {
100-
cancellationThread.setCancelChecker(cancelChecker)
113+
this.cancelChecker = cancelChecker
101114
eval(":reset")
102115
}
103116

104117
/** Terminate this JVM. */
105118
def exit(): Unit = {
106119
process.destroyForcibly()
107120
Evaluator.previousEvaluator = None
108-
cancellationThread.interrupt()
121+
cancellationTimer.cancel()
109122
}
110-
111123
}
112-

0 commit comments

Comments
 (0)