exception on the vm should exit the app

This commit is contained in:
minjaesong
2023-01-03 01:52:38 +09:00
parent a066975ce6
commit ff23b13ba0
4 changed files with 29 additions and 8 deletions

View File

@@ -1,12 +1,12 @@
package net.torvald.tsvm package net.torvald.tsvm
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.utils.GdxRuntimeException import com.badlogic.gdx.utils.GdxRuntimeException
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import net.torvald.tsvm.peripheral.GraphicsAdapter import net.torvald.tsvm.peripheral.GraphicsAdapter
import net.torvald.tsvm.peripheral.ReferenceGraphicsAdapter2
/** /**
* Created by minjaesong on 2022-12-15. * Created by minjaesong on 2022-12-15.
@@ -21,7 +21,7 @@ object VMSetupBroker {
* @param vmRunners Hashmap on the host of VMs that holds the instances of the VMRunners for the given VM. Key: Int(VM's identifier), value: [net.torvald.tsvm.VMRunner] * @param vmRunners Hashmap on the host of VMs that holds the instances of the VMRunners for the given VM. Key: Int(VM's identifier), value: [net.torvald.tsvm.VMRunner]
* @param coroutineJobs Hashmap on the host of VMs that holds the coroutine-job object for the currently running VM-instance. Key: Int(VM's identifier), value: [kotlinx.coroutines.Job] * @param coroutineJobs Hashmap on the host of VMs that holds the coroutine-job object for the currently running VM-instance. Key: Int(VM's identifier), value: [kotlinx.coroutines.Job]
*/ */
fun initVMenv(vm: VM, gpu: GraphicsAdapter, vmRunners: HashMap<Int, VMRunner>, coroutineJobs: HashMap<Int, Job>) { fun initVMenv(vm: VM, gpu: GraphicsAdapter, vmRunners: HashMap<Int, VMRunner>, coroutineJobs: HashMap<Int, Job>, whatToDoOnVmException: (Throwable) -> Unit) {
vm.init() vm.init()
try { try {
@@ -37,7 +37,14 @@ object VMSetupBroker {
vm.poke(-90L, 0) vm.poke(-90L, 0)
vmRunners[vm.id] = VMRunnerFactory(vm.assetsDir, vm, "js") vmRunners[vm.id] = VMRunnerFactory(vm.assetsDir, vm, "js")
coroutineJobs[vm.id] = GlobalScope.launch { vmRunners[vm.id]?.executeCommand(vm.roms[0]!!.readAll()) } coroutineJobs[vm.id] = GlobalScope.launch {
try {
vmRunners[vm.id]?.executeCommand(vm.roms[0]!!.readAll())
}
catch (e: Throwable) {
whatToDoOnVmException(e)
}
}
} }
/** /**

View File

@@ -1,13 +1,13 @@
package net.torvald.tsvm.peripheral package net.torvald.tsvm.peripheral
import com.badlogic.gdx.Gdx import com.badlogic.gdx.Gdx
import com.badlogic.gdx.audio.AudioDevice
import com.badlogic.gdx.backends.lwjgl3.audio.OpenALLwjgl3Audio import com.badlogic.gdx.backends.lwjgl3.audio.OpenALLwjgl3Audio
import com.badlogic.gdx.utils.Queue import com.badlogic.gdx.utils.Queue
import net.torvald.UnsafeHelper import net.torvald.UnsafeHelper
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.toUint import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.toUint
import net.torvald.tsvm.ThreeFiveMiniUfloat import net.torvald.tsvm.ThreeFiveMiniUfloat
import net.torvald.tsvm.VM import net.torvald.tsvm.VM
import kotlin.system.exitProcess
private fun Boolean.toInt() = if (this) 1 else 0 private fun Boolean.toInt() = if (this) 1 else 0
@@ -36,6 +36,11 @@ class AudioAdapter(val vm: VM) : PeriBase {
private val renderThreads = Array(4) { Thread(getRenderFun(it)) } private val renderThreads = Array(4) { Thread(getRenderFun(it)) }
private val writeQueueingThreads = Array(4) { Thread(getQueueingFun(it)) } private val writeQueueingThreads = Array(4) { Thread(getQueueingFun(it)) }
private val threadExceptionHandler = Thread.UncaughtExceptionHandler { thread, throwable ->
throwable.printStackTrace()
exitProcess(1)
}
private fun getRenderFun(pheadNum: Int): () -> Unit = { while (true) { private fun getRenderFun(pheadNum: Int): () -> Unit = { while (true) {
render(playheads[pheadNum]) render(playheads[pheadNum])
Thread.sleep(1) Thread.sleep(1)
@@ -91,8 +96,8 @@ class AudioAdapter(val vm: VM) : PeriBase {
// printdbg("AudioAdapter latency: ${audioDevice.latency}") // printdbg("AudioAdapter latency: ${audioDevice.latency}")
renderThreads.forEach { it.start() } renderThreads.forEach { it.uncaughtExceptionHandler = threadExceptionHandler; it.start() }
writeQueueingThreads.forEach { it.start() } writeQueueingThreads.forEach { it.uncaughtExceptionHandler = threadExceptionHandler; it.start() }
} }

View File

@@ -91,7 +91,13 @@ public class TsvmEmulator {
appConfig.setWindowedMode(WIDTH, HEIGHT); appConfig.setWindowedMode(WIDTH, HEIGHT);
new Lwjgl3Application(new VMEmuExecutableWrapper(VIEWPORT_W, VIEWPORT_H, PANELS_X, PANELS_Y,"assets/"), appConfig); try {
new Lwjgl3Application(new VMEmuExecutableWrapper(VIEWPORT_W, VIEWPORT_H, PANELS_X, PANELS_Y, "assets/"), appConfig);
}
catch (Throwable e) {
e.printStackTrace();
if (Gdx.app != null) Gdx.app.exit();
}
} }
private static void getDefaultDirectory() { private static void getDefaultDirectory() {

View File

@@ -194,7 +194,10 @@ class VMEmuExecutable(val windowWidth: Int, val windowHeight: Int, var panelsX:
internal fun initVMenv(vm: VM) { internal fun initVMenv(vm: VM) {
val gpu = ReferenceGraphicsAdapter2("./assets", vm) val gpu = ReferenceGraphicsAdapter2("./assets", vm)
VMSetupBroker.initVMenv(vm, gpu, vmRunners, coroutineJobs) VMSetupBroker.initVMenv(vm, gpu, vmRunners, coroutineJobs) {
it.printStackTrace()
VMSetupBroker.killVMenv(vm, vmRunners, coroutineJobs)
}
} }
internal fun killVMenv(vm: VM) { internal fun killVMenv(vm: VM) {