diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/internal/NativeDistributionCommonizerCache.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/internal/NativeDistributionCommonizerCache.kt index 732eab90e7f..5674bcef5a9 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/internal/NativeDistributionCommonizerCache.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/internal/NativeDistributionCommonizerCache.kt @@ -14,6 +14,7 @@ import java.io.File import java.io.FileOutputStream import java.io.ObjectInputStream import java.io.Serializable +import java.util.concurrent.locks.Lock import java.util.concurrent.locks.ReentrantLock import kotlin.concurrent.withLock @@ -105,12 +106,11 @@ class NativeDistributionCommonizerCache( logger.info("Native Distribution Commonization: $message") private inner class Lock { - private val reentrantLock = ReentrantLock() private val lockedOutputDirectories = mutableSetOf() fun withLock(action: () -> T): T { /* Enter intra-process wide lock */ - reentrantLock.withLock { + intraProcessLock.withLock { if (outputDirectory in lockedOutputDirectories) { /* Already acquired this directory and re-entered: We can just execute the action */ return action() @@ -137,7 +137,7 @@ class NativeDistributionCommonizerCache( } fun checkLocked(outputDirectory: File) { - check(reentrantLock.isHeldByCurrentThread) { + check(intraProcessLock.isHeldByCurrentThread) { "Expected lock to be held by current thread ${Thread.currentThread().name}" } @@ -151,4 +151,8 @@ class NativeDistributionCommonizerCache( input.defaultReadObject() lock = Lock() } -} \ No newline at end of file + + private companion object { + val intraProcessLock: ReentrantLock = ReentrantLock() + } +}