From 7ff8bc21b6548bd2c034230228a99b6808da3cd4 Mon Sep 17 00:00:00 2001 From: Lanius Trolling Date: Sat, 10 Feb 2024 09:27:05 -0500 Subject: [PATCH] Replace synchronized method with usage of ReentrantLock --- .../kotlin/info/mechyrdia/lore/fonts.kt | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/jvmMain/kotlin/info/mechyrdia/lore/fonts.kt b/src/jvmMain/kotlin/info/mechyrdia/lore/fonts.kt index 74207fc..e4d8fda 100644 --- a/src/jvmMain/kotlin/info/mechyrdia/lore/fonts.kt +++ b/src/jvmMain/kotlin/info/mechyrdia/lore/fonts.kt @@ -11,6 +11,8 @@ import java.awt.geom.GeneralPath import java.awt.geom.PathIterator import java.awt.image.BufferedImage import java.io.File +import java.util.concurrent.locks.ReentrantLock +import kotlin.concurrent.withLock import kotlin.properties.ReadOnlyProperty import kotlin.reflect.KProperty import kotlin.text.toCharArray @@ -41,18 +43,20 @@ object MechyrdiaSansFont { .deriveFont(DEFAULT_FONT_SIZE) } - @Synchronized + private val getValueLock = ReentrantLock() + override fun getValue(thisRef: Any?, property: KProperty<*>): Font { - val font = loadedFont - val lastMod = fontFile.lastModified() - - if (font == null || lastLoaded < lastMod) - return loadFont().also { - loadedFont = it - lastLoaded = lastMod - } - - return font + return getValueLock.withLock { + val font = loadedFont + val lastMod = fontFile.lastModified() + + if (font == null || lastLoaded < lastMod) + loadFont().also { + loadedFont = it + lastLoaded = lastMod + } + else font + } } } } -- 2.25.1