From 56a26113cdd2eb92832d0e05e6ae400742d3d170 Mon Sep 17 00:00:00 2001 From: Georgy Bronnikov Date: Thu, 19 Nov 2020 21:39:47 +0300 Subject: [PATCH] IR: threadLocal To be used for per-file state in global structures during parallel lowering. --- .../org/jetbrains/kotlin/utils/threadLocal.kt | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 compiler/util/src/org/jetbrains/kotlin/utils/threadLocal.kt diff --git a/compiler/util/src/org/jetbrains/kotlin/utils/threadLocal.kt b/compiler/util/src/org/jetbrains/kotlin/utils/threadLocal.kt new file mode 100644 index 00000000000..078bbcdb1ae --- /dev/null +++ b/compiler/util/src/org/jetbrains/kotlin/utils/threadLocal.kt @@ -0,0 +1,26 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.utils + +import java.util.concurrent.ConcurrentHashMap +import kotlin.properties.ReadWriteProperty +import kotlin.reflect.KProperty + +fun threadLocal(initializer: () -> T): ReadWriteProperty = ThreadLocalDelegate(initializer) + +private class ThreadLocalDelegate(private val initializer: () -> T) : ReadWriteProperty { + private val map = ConcurrentHashMap() + + override operator fun getValue(thisRef: Any?, property: KProperty<*>): T { + return map.getOrPut(Thread.currentThread()) { + initializer() + } + } + + override operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { + map[Thread.currentThread()] = value + } +} \ No newline at end of file