fix connecting if system has low thread count

This commit is contained in:
Bixilon 2023-06-26 18:05:25 +02:00
parent 00407633d5
commit 4edfdd9235
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
4 changed files with 49 additions and 23 deletions

View File

@ -51,6 +51,7 @@ object RenderLoader {
} }
fun RenderContext.load(latch: AbstractLatch) { fun RenderContext.load(latch: AbstractLatch) {
val renderLatch = ParentLatch(1, latch)
setThread() setThread()
Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Creating window..." } Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Creating window..." }
val stopwatch = Stopwatch() val stopwatch = Stopwatch()
@ -73,7 +74,7 @@ object RenderLoader {
renderSystem.reset() renderSystem.reset()
// Init stage // Init stage
val initLatch = ParentLatch(1, latch) val initLatch = ParentLatch(1, renderLatch)
Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Generating font and gathering textures (after ${stopwatch.labTime()})..." } Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Generating font and gathering textures (after ${stopwatch.labTime()})..." }
textureManager.dynamicTextures.load(initLatch) textureManager.dynamicTextures.load(initLatch)
textureManager.initializeSkins(connection) textureManager.initializeSkins(connection)
@ -95,16 +96,16 @@ object RenderLoader {
// Post init stage // Post init stage
Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Preloading textures (after ${stopwatch.labTime()})..." } Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Preloading textures (after ${stopwatch.labTime()})..." }
textureManager.staticTextures.preLoad(latch) textureManager.staticTextures.preLoad(renderLatch)
Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Loading textures (after ${stopwatch.labTime()})..." } Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Loading textures (after ${stopwatch.labTime()})..." }
textureManager.staticTextures.load(latch) textureManager.staticTextures.load(renderLatch)
font.postInit(latch) font.postInit(renderLatch)
Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Post loading renderer (after ${stopwatch.labTime()})..." } Log.log(LogMessageType.RENDERING_LOADING, LogLevels.VERBOSE) { "Post loading renderer (after ${stopwatch.labTime()})..." }
shaderManager.postInit() shaderManager.postInit()
skeletalManager.postInit() skeletalManager.postInit()
renderer.postInit(latch) renderer.postInit(renderLatch)
framebufferManager.postInit() framebufferManager.postInit()
@ -134,8 +135,8 @@ object RenderLoader {
textureManager.staticTextures.activate() textureManager.staticTextures.activate()
latch.dec() // initial count from rendering renderLatch.dec() // initial count from rendering
latch.await() renderLatch.await()
Log.log(LogMessageType.RENDERING_LOADING) { "Rendering is fully prepared in ${stopwatch.totalTime()}" } Log.log(LogMessageType.RENDERING_LOADING) { "Rendering is fully prepared in ${stopwatch.totalTime()}" }
} }

View File

@ -1,6 +1,6 @@
/* /*
* Minosoft * Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger * Copyright (C) 2020-2023 Moritz Zwerger
* *
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* *
@ -32,9 +32,10 @@ class Rendering(private val connection: PlayConnection) {
fun start(latch: AbstractLatch, render: Boolean = true, audio: Boolean = true) { fun start(latch: AbstractLatch, render: Boolean = true, audio: Boolean = true) {
Log.log(LogMessageType.RENDERING_GENERAL, LogLevels.INFO) { "Hello LWJGL ${Version.getVersion()}!" } Log.log(LogMessageType.RENDERING_GENERAL, LogLevels.INFO) { "Hello LWJGL ${Version.getVersion()}!" }
latch.inc() val loading = ParentLatch(2, latch)
if (audio) startAudioPlayerThread(latch) if (audio) startAudioPlayerThread(loading)
if (render) startRenderWindowThread(latch) if (render) startRenderWindowThread(loading)
latch.dec()
} }
private fun startAudioPlayerThread(latch: AbstractLatch) { private fun startAudioPlayerThread(latch: AbstractLatch) {
@ -46,6 +47,7 @@ class Rendering(private val connection: PlayConnection) {
try { try {
Thread.currentThread().priority = Thread.MAX_PRIORITY Thread.currentThread().priority = Thread.MAX_PRIORITY
audioPlayer.init(audioLatch) audioPlayer.init(audioLatch)
latch.dec() // initial count
audioPlayer.startLoop() audioPlayer.startLoop()
audioPlayer.exit() audioPlayer.exit()
} catch (exception: Throwable) { } catch (exception: Throwable) {
@ -70,6 +72,7 @@ class Rendering(private val connection: PlayConnection) {
Thread.currentThread().priority = Thread.MAX_PRIORITY Thread.currentThread().priority = Thread.MAX_PRIORITY
CONTEXT_MAP[Thread.currentThread()] = context CONTEXT_MAP[Thread.currentThread()] = context
context.load(latch) context.load(latch)
latch.dec()
val loop = RenderLoop(context) val loop = RenderLoop(context)
context.awaitPlaying() context.awaitPlaying()
loop.startLoop() loop.startLoop()

View File

@ -20,6 +20,7 @@ import de.bixilon.kutil.concurrent.worker.task.TaskWorker
import de.bixilon.kutil.concurrent.worker.task.WorkerTask import de.bixilon.kutil.concurrent.worker.task.WorkerTask
import de.bixilon.kutil.latch.AbstractLatch import de.bixilon.kutil.latch.AbstractLatch
import de.bixilon.kutil.latch.AbstractLatch.Companion.child import de.bixilon.kutil.latch.AbstractLatch.Companion.child
import de.bixilon.kutil.latch.CallbackLatch
import de.bixilon.kutil.observer.DataObserver.Companion.observe import de.bixilon.kutil.observer.DataObserver.Companion.observe
import de.bixilon.kutil.observer.DataObserver.Companion.observed import de.bixilon.kutil.observer.DataObserver.Companion.observed
import de.bixilon.kutil.reflection.ReflectionUtil.forceSet import de.bixilon.kutil.reflection.ReflectionUtil.forceSet
@ -67,6 +68,7 @@ import de.bixilon.minosoft.terminal.RunConfiguration
import de.bixilon.minosoft.terminal.cli.CLI import de.bixilon.minosoft.terminal.cli.CLI
import de.bixilon.minosoft.util.KUtil import de.bixilon.minosoft.util.KUtil
import de.bixilon.minosoft.util.KUtil.startInit import de.bixilon.minosoft.util.KUtil.startInit
import de.bixilon.minosoft.util.KUtil.waitIfLess
import de.bixilon.minosoft.util.logging.Log import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels import de.bixilon.minosoft.util.logging.LogLevels
import de.bixilon.minosoft.util.logging.LogMessageType import de.bixilon.minosoft.util.logging.LogMessageType
@ -177,7 +179,6 @@ class PlayConnection(
fun connect(latch: AbstractLatch? = null) { fun connect(latch: AbstractLatch? = null) {
val count = latch?.count ?: 0
check(!wasConnected) { "Connection was already connected!" } check(!wasConnected) { "Connection was already connected!" }
try { try {
state = PlayConnectionStates.WAITING_MODS state = PlayConnectionStates.WAITING_MODS
@ -219,17 +220,12 @@ class PlayConnection(
camera.init() camera.init()
if (!RunConfiguration.DISABLE_RENDERING) {
val rendering = Rendering(this) if (RunConfiguration.DISABLE_RENDERING) {
this.rendering = rendering establish(latch)
val renderLatch = latch.child(0) } else {
rendering.start(renderLatch) establishRendering(latch)
renderLatch.awaitWithChange()
} }
latch?.dec() // remove initial value
Log.log(LogMessageType.NETWORK_STATUS, level = LogLevels.INFO) { "Connecting to server: $address" }
network.connect(address, profiles.other.nativeNetwork)
state = PlayConnectionStates.ESTABLISHING
} catch (exception: Throwable) { } catch (exception: Throwable) {
Log.log(LogMessageType.VERSION_LOADING, level = LogLevels.FATAL) { exception } Log.log(LogMessageType.VERSION_LOADING, level = LogLevels.FATAL) { exception }
if (this::assetsManager.isInitialized) { if (this::assetsManager.isInitialized) {
@ -238,7 +234,25 @@ class PlayConnection(
error = exception error = exception
retry = false retry = false
} }
latch?.count = count }
private fun establish(latch: AbstractLatch?) {
latch?.dec() // remove initial value
Log.log(LogMessageType.NETWORK_STATUS, level = LogLevels.INFO) { "Connecting to server: $address" }
network.connect(address, profiles.other.nativeNetwork)
state = PlayConnectionStates.ESTABLISHING
}
private fun establishRendering(latch: AbstractLatch?) {
val rendering = Rendering(this)
this.rendering = rendering
val renderLatch = CallbackLatch(1, latch)
rendering.start(renderLatch)
renderLatch.waitIfLess(2)
renderLatch += latch@{
if (renderLatch.count > 0) return@latch
establish(latch)
}
} }
override fun disconnect() { override fun disconnect() {

View File

@ -28,6 +28,7 @@ import de.bixilon.kutil.collections.CollectionUtil.synchronizedSetOf
import de.bixilon.kutil.collections.CollectionUtil.toSynchronizedSet import de.bixilon.kutil.collections.CollectionUtil.toSynchronizedSet
import de.bixilon.kutil.concurrent.pool.DefaultThreadPool import de.bixilon.kutil.concurrent.pool.DefaultThreadPool
import de.bixilon.kutil.concurrent.schedule.TaskScheduler import de.bixilon.kutil.concurrent.schedule.TaskScheduler
import de.bixilon.kutil.latch.AbstractLatch
import de.bixilon.kutil.primitive.BooleanUtil.decide import de.bixilon.kutil.primitive.BooleanUtil.decide
import de.bixilon.kutil.primitive.DoubleUtil import de.bixilon.kutil.primitive.DoubleUtil
import de.bixilon.kutil.primitive.DoubleUtil.matches import de.bixilon.kutil.primitive.DoubleUtil.matches
@ -342,4 +343,11 @@ object KUtil {
return table return table
} }
@Deprecated("kutil 1.24")
fun AbstractLatch.waitIfLess(value: Int, timeout: Long = 0L) = synchronized(notify) {
while (this.count < value) {
waitForChange(timeout)
}
}
} }