diff --git a/src/main/java/de/bixilon/minosoft/data/world/chunk/light/SectionLight.kt b/src/main/java/de/bixilon/minosoft/data/world/chunk/light/SectionLight.kt index 9bc1c05bf..62942867a 100644 --- a/src/main/java/de/bixilon/minosoft/data/world/chunk/light/SectionLight.kt +++ b/src/main/java/de/bixilon/minosoft/data/world/chunk/light/SectionLight.kt @@ -226,7 +226,7 @@ class SectionLight( update = true val blocks = section.blocks - blocks.lock?.acquire() + section.chunk.lock.lock() val min = blocks.minPosition val max = blocks.maxPosition @@ -243,7 +243,7 @@ class SectionLight( } } } - blocks.lock?.release() + section.chunk.lock.unlock() section.chunk.light.sky.recalculate(section.sectionHeight) } diff --git a/src/main/java/de/bixilon/minosoft/util/DebugLock.kt b/src/main/java/de/bixilon/minosoft/util/DebugLock.kt new file mode 100644 index 000000000..afcc6db50 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/util/DebugLock.kt @@ -0,0 +1,68 @@ +/* + * Minosoft + * 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 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.util + +import de.bixilon.kutil.concurrent.lock.Lock +import de.bixilon.kutil.concurrent.lock.NobodyIsReadingException +import de.bixilon.kutil.concurrent.lock.NobodyIsWritingException + + +@Deprecated("kutil 1.25") +class DebugLock : Lock { + private val lock = Object() + override var readers = 0 + private set + override var locked = false + private set + + override fun acquire() { + synchronized(lock) { + while (locked) { + lock.wait() + } + readers++ + // Exception("acquire").printStackTrace() + lock.notifyAll() + } + } + + override fun release() { + synchronized(lock) { + if (readers <= 0) throw NobodyIsReadingException() + readers-- + // Exception("release").printStackTrace() + lock.notifyAll() + } + } + + override fun lock() { + synchronized(lock) { + while (locked || readers > 0) { + lock.wait() + } + locked = true + Exception("lock").printStackTrace() + lock.notifyAll() + } + } + + override fun unlock() { + synchronized(lock) { + if (!locked) throw NobodyIsWritingException() + locked = false + Exception("unlock").printStackTrace() + lock.notifyAll() + } + } +}