From 4ff09bea2bd3bba1d062de37d39c3895e3e62ccd Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sun, 4 Feb 2024 18:03:18 +0700 Subject: [PATCH] sigh --- .../kstarbound/client/StarboundClient.kt | 2 +- .../kstarbound/util/ExecutionSpinner.kt | 49 ++++++++++++------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/client/StarboundClient.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/client/StarboundClient.kt index f1584272..c738e6c5 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/client/StarboundClient.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/client/StarboundClient.kt @@ -681,7 +681,7 @@ class StarboundClient private constructor(val clientID: Int) : Closeable { blendFunc = BlendFunc.MULTIPLY_WITH_ALPHA } - val spinner = ExecutionSpinner(mailbox, ::renderFrame, Starbound.TICK_TIME_ADVANCE_NANOS, true) + val spinner = ExecutionSpinner(mailbox, ::renderFrame, Starbound.TICK_TIME_ADVANCE_NANOS) val settings = ClientSettings() val viewportCells: ICellAccess = object : ICellAccess { diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/util/ExecutionSpinner.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/util/ExecutionSpinner.kt index 9eb31759..0aa1d01e 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/util/ExecutionSpinner.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/util/ExecutionSpinner.kt @@ -1,11 +1,16 @@ package ru.dbotthepony.kstarbound.util +import org.apache.logging.log4j.LogManager import ru.dbotthepony.kommons.util.MailboxExecutorService import ru.dbotthepony.kstarbound.Starbound import java.util.concurrent.locks.LockSupport import java.util.function.BooleanSupplier -class ExecutionSpinner(private val executor: MailboxExecutorService, private val spinner: BooleanSupplier, private val timeBetweenFrames: Long, val precise: Boolean = false) : Runnable { +class ExecutionSpinner(private val executor: MailboxExecutorService, private val spinner: BooleanSupplier, private val timeBetweenFrames: Long) : Runnable { + init { + Companion + } + private var lastRender = System.nanoTime() private var frameRenderTime = 0L private val frameRenderTimes = LongArray(60) { 1L } @@ -46,23 +51,10 @@ class ExecutionSpinner(private val executor: MailboxExecutorService, private val fun spin(): Boolean { var diff = timeUntilNextFrame() - if (precise) { - while (diff > 1_500_000L) { - executor.executeQueuedTasks() - diff = timeUntilNextFrame() - if (diff > 1_500_000L) LockSupport.parkNanos(diff - 400_000L) - } - - while (diff > 0L) { - executor.executeQueuedTasks() - diff = timeUntilNextFrame() - } - } else { - while (diff > 0L) { - executor.executeQueuedTasks() - diff = timeUntilNextFrame() - if (diff > 400_000L) LockSupport.parkNanos(diff) - } + while (diff > 0L) { + executor.executeQueuedTasks() + diff = timeUntilNextFrame() + if (diff > 400_000L) LockSupport.parkNanos(100_000L) } val mark = System.nanoTime() @@ -78,4 +70,25 @@ class ExecutionSpinner(private val executor: MailboxExecutorService, private val override fun run() { while (spin()) {} } + + companion object { + private val LOGGER = LogManager.getLogger() + + init { + val thread = object : Thread("Fix God Damn Windows Process Scheduler For Once Please") { + override fun run() { + while (true) { + try { + sleep(Int.MAX_VALUE.toLong()) + } catch (err: InterruptedException) { + LOGGER.error("Timer hack thread was interrupted, ignoring.") + } + } + } + } + + thread.isDaemon = true + thread.start() + } + } }