Don't directly depend on forkjoinpool

This commit is contained in:
DBotThePony 2023-12-01 21:06:28 +07:00
parent f4cb791125
commit bfc39a6cb6
Signed by: DBot
GPG Key ID: DCC23B5715498507
4 changed files with 26 additions and 18 deletions

View File

@ -3,16 +3,18 @@ package ru.dbotthepony.kstarbound
import org.apache.logging.log4j.LogManager
import ru.dbotthepony.kstarbound.defs.ClientConfigParameters
import ru.dbotthepony.kstarbound.defs.MovementParameters
import ru.dbotthepony.kstarbound.defs.PlayerMovementParameters
import ru.dbotthepony.kstarbound.defs.ActorMovementParameters
import ru.dbotthepony.kstarbound.util.AssetPathStack
import java.util.concurrent.ExecutorService
import java.util.concurrent.ForkJoinPool
import java.util.concurrent.ForkJoinTask
import java.util.concurrent.Future
import kotlin.reflect.KMutableProperty0
object GlobalDefaults {
private val LOGGER = LogManager.getLogger()
var playerMovementParameters = PlayerMovementParameters()
var actorMovementParameters = ActorMovementParameters()
private set
var movementParameters = MovementParameters()
@ -34,7 +36,7 @@ object GlobalDefaults {
}
}
private inline fun <reified T> load(path: String, accept: KMutableProperty0<T>, executor: ForkJoinPool): ForkJoinTask<*> {
private inline fun <reified T> load(path: String, accept: KMutableProperty0<T>, executor: ExecutorService): Future<*> {
val file = Starbound.locate(path)
if (!file.exists) {
@ -52,10 +54,10 @@ object GlobalDefaults {
}
}
fun load(executor: ForkJoinPool): List<ForkJoinTask<*>> {
val tasks = ArrayList<ForkJoinTask<*>>()
fun load(executor: ExecutorService): List<Future<*>> {
val tasks = ArrayList<Future<*>>()
tasks.add(load("/default_actor_movement.config", ::playerMovementParameters, executor))
tasks.add(load("/default_actor_movement.config", ::actorMovementParameters, executor))
tasks.add(load("/default_movement.config", ::movementParameters, executor))
tasks.add(load("/client.config", ::clientParameters, executor))

View File

@ -10,8 +10,10 @@ import ru.dbotthepony.kstarbound.util.KOptional
import ru.dbotthepony.kstarbound.util.ParallelPerform
import java.util.*
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.ExecutorService
import java.util.concurrent.ForkJoinPool
import java.util.concurrent.ForkJoinTask
import java.util.concurrent.Future
import java.util.concurrent.locks.ReentrantLock
import kotlin.collections.ArrayList
import kotlin.concurrent.withLock
@ -72,7 +74,7 @@ object RecipeRegistry {
}
}
fun load(fileTree: Map<String, List<IStarboundFile>>, executor: ForkJoinPool): List<ForkJoinTask<*>> {
fun load(fileTree: Map<String, List<IStarboundFile>>, executor: ExecutorService): List<Future<*>> {
val files = fileTree["recipe"] ?: return emptyList()
val elements = Starbound.gson.getAdapter(JsonElement::class.java)

View File

@ -35,8 +35,10 @@ import ru.dbotthepony.kstarbound.defs.tile.LiquidDefinition
import ru.dbotthepony.kstarbound.defs.tile.MaterialModifier
import ru.dbotthepony.kstarbound.defs.tile.TileDefinition
import ru.dbotthepony.kstarbound.util.AssetPathStack
import java.util.concurrent.ExecutorService
import java.util.concurrent.ForkJoinPool
import java.util.concurrent.ForkJoinTask
import java.util.concurrent.Future
object Registries {
private val LOGGER = LogManager.getLogger()
@ -96,11 +98,11 @@ object Registries {
}
private inline fun <reified T : Any> loadRegistry(
executor: ForkJoinPool,
executor: ExecutorService,
registry: Registry<T>,
files: List<IStarboundFile>,
noinline keyProvider: (T) -> Pair<String, Int?>
): List<ForkJoinTask<*>> {
): List<Future<*>> {
val adapter by lazy { Starbound.gson.getAdapter(T::class.java) }
val elementAdapter by lazy { Starbound.gson.getAdapter(JsonElement::class.java) }
@ -130,8 +132,8 @@ object Registries {
registries.forEach { it.finishLoad() }
}
fun load(fileTree: Map<String, List<IStarboundFile>>, executor: ForkJoinPool): List<ForkJoinTask<*>> {
val tasks = ArrayList<ForkJoinTask<*>>()
fun load(fileTree: Map<String, List<IStarboundFile>>, executor: ExecutorService): List<Future<*>> {
val tasks = ArrayList<Future<*>>()
tasks.addAll(loadItemDefinitions(fileTree, executor))
@ -155,7 +157,7 @@ object Registries {
return tasks
}
private fun loadItemDefinitions(files: Map<String, Collection<IStarboundFile>>, executor: ForkJoinPool): List<ForkJoinTask<*>> {
private fun loadItemDefinitions(files: Map<String, Collection<IStarboundFile>>, executor: ExecutorService): List<Future<*>> {
val fileMap = mapOf(
"item" to ItemDefinition::class.java,
"currency" to CurrencyItemDefinition::class.java,
@ -169,7 +171,7 @@ object Registries {
"back" to BackArmorItemDefinition::class.java,
)
val tasks = ArrayList<ForkJoinTask<*>>()
val tasks = ArrayList<Future<*>>()
val objects = Starbound.gson.getAdapter(JsonObject::class.java)
for ((ext, clazz) in fileMap) {
@ -195,7 +197,7 @@ object Registries {
return tasks
}
private fun loadJsonFunctions(files: Collection<IStarboundFile>, executor: ForkJoinPool): List<ForkJoinTask<*>> {
private fun loadJsonFunctions(files: Collection<IStarboundFile>, executor: ExecutorService): List<Future<*>> {
return files.map { listedFile ->
executor.submit {
try {
@ -216,7 +218,7 @@ object Registries {
}
}
private fun loadJson2Functions(files: Collection<IStarboundFile>, executor: ForkJoinPool): List<ForkJoinTask<*>> {
private fun loadJson2Functions(files: Collection<IStarboundFile>, executor: ExecutorService): List<Future<*>> {
return files.map { listedFile ->
executor.submit {
try {
@ -237,7 +239,7 @@ object Registries {
}
}
private fun loadTreasurePools(files: Collection<IStarboundFile>, executor: ForkJoinPool): List<ForkJoinTask<*>> {
private fun loadTreasurePools(files: Collection<IStarboundFile>, executor: ExecutorService): List<Future<*>> {
return files.map { listedFile ->
executor.submit {
try {

View File

@ -63,8 +63,10 @@ import java.io.*
import java.lang.ref.Cleaner
import java.text.DateFormat
import java.util.concurrent.CompletableFuture
import java.util.concurrent.Executors
import java.util.concurrent.ForkJoinPool
import java.util.concurrent.ForkJoinTask
import java.util.concurrent.Future
import java.util.concurrent.locks.LockSupport
import java.util.function.BiConsumer
import java.util.function.BinaryOperator
@ -442,8 +444,8 @@ object Starbound : ISBFileLocator {
checkMailbox()
val tasks = ArrayList<ForkJoinTask<*>>()
val pool = if (parallel) ForkJoinPool.commonPool() else ForkJoinPool(1)
val tasks = ArrayList<Future<*>>()
val pool = if (parallel) ForkJoinPool.commonPool() else Executors.newFixedThreadPool(1)
tasks.addAll(Registries.load(ext2files, pool))
tasks.addAll(RecipeRegistry.load(ext2files, pool))