Should fix potential NPE in worker thread when arch is re-evaluated, closes #1070.

May also fix other related problems of the architecture previously potentially changing mid-execution.
This commit is contained in:
Florian Nücke 2015-04-15 19:29:31 +02:00
parent 85a07e4a3c
commit 8b81e44481

View File

@ -117,19 +117,17 @@ class Machine(val host: MachineHost) extends prefab.ManagedEnvironment with mach
} }
case _ => 0 case _ => 0
})) }))
val oldArchitecture = architecture var newArchitecture: Architecture = null
architecture = null
components.find { components.find {
case stack: ItemStack => Option(Driver.driverFor(stack, host.getClass)) match { case stack: ItemStack => Option(Driver.driverFor(stack, host.getClass)) match {
case Some(driver: Processor) if driver.slot(stack) == Slot.CPU => case Some(driver: Processor) if driver.slot(stack) == Slot.CPU =>
Option(driver.architecture(stack)) match { Option(driver.architecture(stack)) match {
case Some(clazz) => case Some(clazz) =>
if (oldArchitecture == null || oldArchitecture.getClass != clazz) { if (architecture == null || architecture.getClass != clazz) {
architecture = clazz.getConstructor(classOf[machine.Machine]).newInstance(this) newArchitecture = clazz.getConstructor(classOf[machine.Machine]).newInstance(this)
if (node.network != null) architecture.onConnect()
} }
else { else {
architecture = oldArchitecture newArchitecture = architecture
} }
true true
case _ => false case _ => false
@ -138,6 +136,12 @@ class Machine(val host: MachineHost) extends prefab.ManagedEnvironment with mach
} }
case _ => false case _ => false
} }
// This needs to operate synchronized against the worker thread, to avoid the
// architecture changing while it is currently being executed.
if (newArchitecture != architecture) this.synchronized {
architecture = newArchitecture
if (architecture != null && node.network != null) architecture.onConnect()
}
hasMemory = Option(architecture).fold(false)(_.recomputeMemory(components)) hasMemory = Option(architecture).fold(false)(_.recomputeMemory(components))
} }