godot.util.ThreadLocalDelegates.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of godot-runtime Show documentation
Show all versions of godot-runtime Show documentation
Contains runtime classes and core types to make the jvm interact with cpp
package godot.util
import kotlin.reflect.KProperty
class ThreadLocalLazyDelegate(private val provider: () -> T) : Lazy {
private val threadLocal = ThreadLocal.withInitial { lazy(LazyThreadSafetyMode.NONE, provider) }
override val value get() = threadLocal.get().value
override fun isInitialized(): Boolean = threadLocal.get().isInitialized()
}
class ThreadLocalDelegate(val provider: () -> T) {
private val threadLocal = ThreadLocal.withInitial { provider() }
operator fun getValue(thisRef: Any?, property: KProperty<*>): T = threadLocal.get()
}
fun threadLocalLazy(provider: () -> T) = ThreadLocalLazyDelegate(provider)
fun threadLocal(provider: () -> T) = ThreadLocalDelegate(provider)