mirror of https://github.com/sbt/sbt.git
Cleanup user thread task submission
I found this code difficult to reason about so I refactored it so that it was easier for me to understand.
This commit is contained in:
parent
600628e8e9
commit
2b45183d09
|
|
@ -31,31 +31,33 @@ private[sbt] class UserThread(val channel: CommandChannel) extends AutoCloseable
|
||||||
private[sbt] def reset(state: State): Unit = if (!isClosed.get) {
|
private[sbt] def reset(state: State): Unit = if (!isClosed.get) {
|
||||||
uiThread.synchronized {
|
uiThread.synchronized {
|
||||||
val task = channel.makeUIThread(state)
|
val task = channel.makeUIThread(state)
|
||||||
def submit(): Thread = {
|
def submit(): Unit = {
|
||||||
val thread: Thread = new Thread(s"sbt-$name-ui-thread") {
|
val thread: Thread = new Thread(s"sbt-$name-ui-thread") {
|
||||||
setDaemon(true)
|
setDaemon(true)
|
||||||
override def run(): Unit =
|
override def run(): Unit =
|
||||||
try task.run()
|
try task.run()
|
||||||
finally uiThread.get match {
|
finally {
|
||||||
case (_, t) if t == this => uiThread.set(null)
|
uiThread.getAndSet(null) match {
|
||||||
case _ =>
|
case prev @ (_, th) if th != this => uiThread.set(prev)
|
||||||
|
case _ =>
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
uiThread.getAndSet((task, thread)) match {
|
uiThread.getAndSet((task, thread)) match {
|
||||||
case null => thread.start()
|
case null => thread.start()
|
||||||
case (task, t) if t.getClass != task.getClass =>
|
case (prevTask, prevThread) if prevTask.getClass != task.getClass =>
|
||||||
stopThreadImpl()
|
prevTask.close()
|
||||||
|
prevThread.joinFor(1.second)
|
||||||
thread.start()
|
thread.start()
|
||||||
case t => uiThread.set(t)
|
case t => uiThread.set(t)
|
||||||
}
|
}
|
||||||
thread
|
|
||||||
}
|
}
|
||||||
uiThread.get match {
|
uiThread.get match {
|
||||||
case null => uiThread.set((task, submit()))
|
case null => submit()
|
||||||
case (t, _) if t.getClass == task.getClass =>
|
case (prevTask, _) if prevTask.getClass == task.getClass =>
|
||||||
case (t, thread) =>
|
case (t, thread) =>
|
||||||
stopThreadImpl()
|
stopThreadImpl()
|
||||||
uiThread.set((task, submit()))
|
submit()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Option(lastProgressEvent.get).foreach(onProgressEvent)
|
Option(lastProgressEvent.get).foreach(onProgressEvent)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue