Replace synchronized method with usage of ReentrantLock
authorLanius Trolling <lanius@laniustrolling.dev>
Sat, 10 Feb 2024 14:27:05 +0000 (09:27 -0500)
committerLanius Trolling <lanius@laniustrolling.dev>
Sat, 10 Feb 2024 14:28:03 +0000 (09:28 -0500)
src/jvmMain/kotlin/info/mechyrdia/lore/fonts.kt

index 74207fc701279089ad9c176a6f8e3be3ffd74242..e4d8fdaeeaa66673d3ef63619fee6905a924fa79 100644 (file)
@@ -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
+                               }
                        }
                }
        }