fix particle tick crashing, handle TimeWorker exceptions gracefully

This commit is contained in:
Bixilon 2021-11-29 13:29:21 +01:00
parent 0b0ae3dd44
commit 31c1d1d7da
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 14 additions and 7 deletions

View File

@ -162,10 +162,13 @@ class ParticleRenderer(
translucentMesh = ParticleMesh(renderWindow, translucentMesh.data)
particlesLock.acquire()
particlesLock.lock()
particleQueueLock.acquire()
particles += particleQueue
particleQueueLock.release()
particlesLock.unlock()
particlesLock.acquire()
val time = KUtil.time
for (particle in particles) {

View File

@ -14,7 +14,7 @@ object TimeWorker {
while (true) {
val currentTime = KUtil.time
for (task in TASKS.toSynchronizedSet()) {
if (task.getsExecuted) {
if (task.executing) {
continue
}
if (currentTime - task.lastExecution <= task.interval) {
@ -24,7 +24,7 @@ object TimeWorker {
if (!task.lock.tryLock(100L, TimeUnit.MILLISECONDS)) {
return@execute
}
if (task.getsExecuted) {
if (task.executing) {
task.lock.unlock()
return@execute
}
@ -32,10 +32,14 @@ object TimeWorker {
task.lock.unlock()
return@execute
}
task.getsExecuted = true
task.runnable.run()
task.executing = true
try {
task.runnable.run()
} catch (exception: Exception) {
exception.printStackTrace()
}
task.lastExecution = currentTime
task.getsExecuted = false
task.executing = false
task.lock.unlock()
}
if (task.runOnce) {

View File

@ -22,6 +22,6 @@ data class TimeWorkerTask(
val runnable: Runnable,
) {
val lock = ReentrantLock()
var getsExecuted: Boolean = false
var executing: Boolean = false
var lastExecution: Long = 0L
}