mirror of
https://github.com/curioustorvald/tsvm.git
synced 2026-06-21 19:54:04 +09:00
vmgui now exits on exit
This commit is contained in:
@@ -16,9 +16,9 @@ private class RenderRunnable(val playhead: AudioAdapter.Playhead) : Runnable {
|
||||
private fun printdbg(msg: Any) {
|
||||
if (AudioAdapter.DBGPRN) println("[AudioAdapter] $msg")
|
||||
}
|
||||
@Volatile private var exit = false
|
||||
override fun run() {
|
||||
while (!exit) {
|
||||
while (!Thread.currentThread().isInterrupted) {
|
||||
try {
|
||||
if (playhead.isPcmMode) {
|
||||
|
||||
val writeQueue = playhead.pcmQueue
|
||||
@@ -52,10 +52,10 @@ private class RenderRunnable(val playhead: AudioAdapter.Playhead) : Runnable {
|
||||
|
||||
Thread.sleep(1)
|
||||
}
|
||||
playhead.audioDevice.dispose()
|
||||
catch (_: InterruptedException) {
|
||||
Thread.currentThread().interrupt()
|
||||
}
|
||||
}
|
||||
fun stop() {
|
||||
exit = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,17 +63,21 @@ private class WriteQueueingRunnable(val playhead: AudioAdapter.Playhead, val pcm
|
||||
private fun printdbg(msg: Any) {
|
||||
if (AudioAdapter.DBGPRN) println("[AudioAdapter] $msg")
|
||||
}
|
||||
@Volatile private var exit = false
|
||||
|
||||
override fun run() {
|
||||
while (!exit) {
|
||||
|
||||
while (!Thread.currentThread().isInterrupted) {
|
||||
try {
|
||||
playhead.let {
|
||||
if (/*it.pcmQueue.size < it.getPcmQueueCapacity() &&*/ it.pcmUpload && it.pcmUploadLength > 0) {
|
||||
printdbg("Downloading samples ${it.pcmUploadLength}")
|
||||
|
||||
val samples = ByteArray(it.pcmUploadLength)
|
||||
UnsafeHelper.memcpyRaw(null, pcmBin.ptr, samples, UnsafeHelper.getArrayOffset(samples), it.pcmUploadLength.toLong())
|
||||
UnsafeHelper.memcpyRaw(
|
||||
null,
|
||||
pcmBin.ptr,
|
||||
samples,
|
||||
UnsafeHelper.getArrayOffset(samples),
|
||||
it.pcmUploadLength.toLong()
|
||||
)
|
||||
it.pcmQueue.addLast(samples)
|
||||
|
||||
it.pcmUploadLength = 0
|
||||
@@ -88,9 +92,10 @@ private class WriteQueueingRunnable(val playhead: AudioAdapter.Playhead, val pcm
|
||||
|
||||
Thread.sleep(1)
|
||||
}
|
||||
catch (_: InterruptedException) {
|
||||
Thread.currentThread().interrupt()
|
||||
}
|
||||
}
|
||||
fun stop() {
|
||||
exit = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,6 +130,9 @@ class AudioAdapter(val vm: VM) : PeriBase(VM.PERITYPE_SOUND) {
|
||||
private val writeQueueingRunnables: Array<WriteQueueingRunnable>
|
||||
private val writeQueueingThreads: Array<Thread>
|
||||
|
||||
private val renderThreadGroup = ThreadGroup("AudioRenderThreadGroup")
|
||||
private val writeQueueingGroup = ThreadGroup("AudioQriteQueueingThreadGroup")
|
||||
|
||||
private val threadExceptionHandler = Thread.UncaughtExceptionHandler { thread, throwable ->
|
||||
throwable.printStackTrace()
|
||||
}
|
||||
@@ -163,9 +171,9 @@ class AudioAdapter(val vm: VM) : PeriBase(VM.PERITYPE_SOUND) {
|
||||
|
||||
|
||||
renderRunnables = Array(4) { RenderRunnable(playheads[it]) }
|
||||
renderThreads = Array(4) { Thread(renderRunnables[it], "AudioRenderHead${it+1}!$hash") }
|
||||
renderThreads = Array(4) { Thread(renderThreadGroup, renderRunnables[it], "AudioRenderHead${it+1}!$hash") }
|
||||
writeQueueingRunnables = Array(4) { WriteQueueingRunnable(playheads[it], pcmBin) }
|
||||
writeQueueingThreads = Array(4) { Thread(writeQueueingRunnables[it], "AudioQueueingHead${it+1}!$hash") }
|
||||
writeQueueingThreads = Array(4) { Thread(writeQueueingGroup, writeQueueingRunnables[it], "AudioQueueingHead${it+1}!$hash") }
|
||||
|
||||
// printdbg("AudioAdapter latency: ${audioDevice.latency}")
|
||||
renderThreads.forEach { it.uncaughtExceptionHandler = threadExceptionHandler; it.start() }
|
||||
@@ -266,18 +274,24 @@ class AudioAdapter(val vm: VM) : PeriBase(VM.PERITYPE_SOUND) {
|
||||
}
|
||||
}
|
||||
|
||||
private var disposed = false
|
||||
|
||||
override fun dispose() {
|
||||
if (!disposed) {
|
||||
disposed = true
|
||||
System.err.println("Dispose AudioAdapter")
|
||||
renderRunnables.forEach { it.stop() }
|
||||
renderThreads.forEach { it.interrupt() }
|
||||
writeQueueingRunnables.forEach { it.stop() }
|
||||
writeQueueingThreads.forEach { it.interrupt() }
|
||||
renderThreadGroup.interrupt()
|
||||
writeQueueingGroup.interrupt()
|
||||
playheads.forEach { it.dispose() }
|
||||
sampleBin.destroy()
|
||||
pcmBin.destroy()
|
||||
mediaFrameBin.destroy()
|
||||
mediaDecodedBin.destroy()
|
||||
}
|
||||
else {
|
||||
System.err.println("AudioAdapter already disposed")
|
||||
}
|
||||
}
|
||||
|
||||
override fun getVM(): VM {
|
||||
return vm
|
||||
@@ -435,7 +449,7 @@ class AudioAdapter(val vm: VM) : PeriBase(VM.PERITYPE_SOUND) {
|
||||
fun dispose() {
|
||||
// audioDevice.dispose() is called by RenderRunnable.stop()
|
||||
System.err.println("AudioDevice dispose ${parent.renderThreads[index]}")
|
||||
try { audioDevice.dispose() } catch (e: GdxRuntimeException) { println(" "+ e.message) }
|
||||
try { audioDevice.dispose() } catch (e: GdxRuntimeException) { System.err.println(" "+ e.message) }
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
@@ -54,7 +54,7 @@ class VMEmuExecutableWrapper(val windowWidth: Int, val windowHeight: Int, var pa
|
||||
// println("App Dispose")
|
||||
executable.dispose()
|
||||
SQTEX.dispose()
|
||||
exitProcess(1)
|
||||
exitProcess(0)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import java.util.*
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
import java.util.concurrent.atomic.AtomicLong
|
||||
import kotlin.coroutines.*
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
|
||||
class EmulInstance(
|
||||
@@ -320,6 +321,9 @@ class VMGUI(val loaderInfo: EmulInstance, val viewportWidth: Int, val viewportHe
|
||||
crtShader.dispose()
|
||||
gpuFBO.dispose()
|
||||
vm.dispose()
|
||||
|
||||
System.err.println("VM disposed: ${vm.id}")
|
||||
exitProcess(0)
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
Reference in New Issue
Block a user