fixed worldgen and threadexecutor so that they will actually wait for the thread termination

This commit is contained in:
minjaesong
2019-11-16 02:41:25 +09:00
parent 7939ff3690
commit e71c56cf0d
9 changed files with 74 additions and 94 deletions

View File

@@ -9,7 +9,7 @@ import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.glutils.FrameBuffer
import com.jme3.math.FastMath
import net.torvald.terrarum.langpack.Lang
import net.torvald.util.HistoryArray
import net.torvald.util.CircularArray
/**
* Created by minjaesong on 2017-07-13.
@@ -20,10 +20,10 @@ object LoadScreen : ScreenAdapter() {
private lateinit var screenLoadingThread: Thread
private val messages = HistoryArray<String>(20)
private val messages = CircularArray<String>(20, true)
fun addMessage(msg: String) {
messages.add(msg)
messages.appendHead(msg)
}
@@ -238,9 +238,9 @@ object LoadScreen : ScreenAdapter() {
// log messages
it.color = messageForegroundColour
for (i in 0 until messages.elemCount) {
messages.forEachIndexed { i, s ->
AppLoader.fontGame.draw(it,
messages[i] ?: "",
s,
AppLoader.getTvSafeGraphicsWidth() + 16f,
80f + (messages.size - i - 1) * AppLoader.fontGame.lineHeight
)
@@ -263,9 +263,9 @@ object LoadScreen : ScreenAdapter() {
// log messages
it.color = messageForegroundColour
for (i in 0 until messages.elemCount) {
messages.forEachIndexed { i, s ->
AppLoader.fontGame.draw(it,
messages[i] ?: "",
s,
AppLoader.getTvSafeGraphicsWidth() + 16f,
80f + (messages.size - i - 1) * AppLoader.fontGame.lineHeight
)

View File

@@ -1,6 +1,7 @@
package net.torvald.terrarum.concurrent
import java.util.concurrent.Executors
import java.util.concurrent.Future
import java.util.concurrent.TimeUnit
import kotlin.math.absoluteValue
@@ -11,12 +12,27 @@ typealias ThreadableFun = (Int) -> Unit
object ThreadExecutor {
val threadCount = Runtime.getRuntime().availableProcessors() // not using (logicalCores + 1) method; it's often better idea to reserve one extra thread for other jobs in the app
private val executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())
private var executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())
fun submit(t: Runnable) = executor.submit(t)
fun submit(f: RunnableFun) = executor.submit { f() }
private fun checkShutdown() {
if (!executor.isShutdown) return
if (executor.isShutdown&& !executor.isTerminated)
throw IllegalStateException("Thread pool is still running")
executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())
}
fun submit(t: Runnable): Future<*> {
checkShutdown()
return executor.submit(t)
}
fun submit(f: RunnableFun): Future<*> {
checkShutdown()
return executor.submit { f() }
}
fun join() {
executor.shutdown() // thread status of completed ones will be WAIT instead of TERMINATED without this line...
executor.awaitTermination(24L, TimeUnit.HOURS)
}

View File

@@ -12,8 +12,9 @@ internal object Version : ConsoleCommand {
override fun execute(args: Array<String>) {
Echo("${AppLoader.GAME_NAME} ${AppLoader.getVERSION_STRING()}")
Echo("Polyglot language pack version ${Lang.POLYGLOT_VERSION}")
Echo("GL_VERSION: ${Terrarum.GL_VERSION}")
Echo("Java version: ${System.getProperty("java.version")}")
Echo("Polyglot language pack version: ${Lang.POLYGLOT_VERSION}")
Echo("GL version: ${Terrarum.GL_VERSION}")
Echo("Renderer: ${Gdx.graphics.glVersion.rendererString}, ${Gdx.graphics.glVersion.vendorString}")
}

View File

@@ -1,10 +1,12 @@
package net.torvald.terrarum.modulebasegame
import com.badlogic.gdx.ScreenAdapter
import com.badlogic.gdx.graphics.Pixmap
import com.badlogic.gdx.graphics.Texture
import net.torvald.terrarum.AppLoader
import net.torvald.terrarum.IngameInstance
import net.torvald.terrarum.gameworld.GameWorld
import net.torvald.util.HistoryArray
import net.torvald.util.CircularArray
import kotlin.math.roundToInt
/**
@@ -25,7 +27,25 @@ class WorldgenLoadScreen(private var world: GameWorld, private var screenToLoad:
private lateinit var screenLoadingThread: Thread
private lateinit var previewPixmap: Pixmap
private lateinit var previewTexture: Texture
private val messages = HistoryArray<String>(20)
private val messages = CircularArray<String>(20, true) // this many texts will be shown at once
override fun show() {
previewPixmap = Pixmap(previewWidth, previewHeight, Pixmap.Format.RGBA8888)
previewTexture = Texture(1, 1, Pixmap.Format.RGBA8888)
}
override fun render(delta: Float) {
previewTexture.dispose()
previewTexture = Texture(previewPixmap)
//
}
override fun dispose() {
previewPixmap.dispose()
previewTexture.dispose()
}
}

View File

@@ -3,11 +3,8 @@ package net.torvald.terrarum.modulebasegame.worldgenerator
import com.sudoplay.joise.Joise
import com.sudoplay.joise.module.*
import net.torvald.terrarum.AppLoader.printdbg
import net.torvald.terrarum.LoadScreen
import net.torvald.terrarum.blockproperties.Block
import net.torvald.terrarum.concurrent.ThreadExecutor
import net.torvald.terrarum.concurrent.ThreadParallel
import net.torvald.terrarum.concurrent.mapToThreadPoolDirectly
import net.torvald.terrarum.gameworld.GameWorld
import java.util.concurrent.Future
import kotlin.math.cos
@@ -31,7 +28,7 @@ class Terragen(world: GameWorld, seed: Long, params: Any) : Gen(world, seed, par
// single-threaded impl because I couldn't resolve multithread memory corruption issue...
genFuture = ThreadExecutor.submit {
for (x in 0 until world.width) {
//printdbg(this, "Tile draw for y=$y")
printdbg(this, "Tile draw for x=$x")
for (y in 0 until world.height) {
val sampleTheta = (x.toDouble() / world.width) * TWO_PI
val sampleOffset = world.width / 8.0
@@ -45,6 +42,8 @@ class Terragen(world: GameWorld, seed: Long, params: Any) : Gen(world, seed, par
}
}
ThreadExecutor.join()
printdbg(this, "Waking up Worldgen")
//Worldgen.wake()
}

View File

@@ -1,6 +1,5 @@
package net.torvald.terrarum.modulebasegame.worldgenerator
import net.torvald.terrarum.AppLoader
import net.torvald.terrarum.AppLoader.printdbg
import net.torvald.terrarum.LoadScreen
import net.torvald.terrarum.gameworld.GameWorld

View File

@@ -10,7 +10,7 @@ import net.torvald.terrarum.console.Authenticator
import net.torvald.terrarum.console.CommandInterpreter
import net.torvald.terrarum.fillRect
import net.torvald.terrarum.langpack.Lang
import net.torvald.util.HistoryArray
import net.torvald.util.CircularArray
/**
@@ -30,7 +30,7 @@ class ConsoleWindow : UICanvas() {
private var messagesCount: Int = 0
private var commandInputPool: StringBuilder? = null
private var commandHistory = HistoryArray<String>(COMMAND_HISTORY_MAX)
private var commandHistory = CircularArray<String>(COMMAND_HISTORY_MAX, true)
private val LINE_HEIGHT = 20
private val MESSAGES_DISPLAY_COUNT = 11
@@ -83,7 +83,7 @@ class ConsoleWindow : UICanvas() {
override fun keyDown(key: Int): Boolean {
// history
if (key == Input.Keys.UP && historyIndex < commandHistory.history.size)
if (key == Input.Keys.UP && historyIndex < commandHistory.elemCount)
historyIndex++
else if (key == Input.Keys.DOWN && historyIndex >= 0)
historyIndex--
@@ -92,7 +92,7 @@ class ConsoleWindow : UICanvas() {
// execute
if (key == Input.Keys.ENTER && commandInputPool!!.isNotEmpty()) {
commandHistory.add(commandInputPool!!.toString())
commandHistory.appendHead(commandInputPool!!.toString())
executeCommand()
commandInputPool = StringBuilder()
}
@@ -105,7 +105,7 @@ class ConsoleWindow : UICanvas() {
// create new stringbuilder
commandInputPool = StringBuilder()
if (historyIndex >= 0) // just leave blank if index is -1
commandInputPool!!.append(commandHistory[historyIndex] ?: "")
commandInputPool!!.append(commandHistory[historyIndex])
}
// delete input
else if (key == Input.Keys.BACKSPACE) {
@@ -179,7 +179,7 @@ class ConsoleWindow : UICanvas() {
messageDisplayPos = 0
messagesCount = 0
inputCursorPos = 0
commandHistory = HistoryArray<String>(COMMAND_HISTORY_MAX)
commandHistory = CircularArray<String>(COMMAND_HISTORY_MAX, true)
commandInputPool = StringBuilder()
if (Authenticator.b()) {