From 6e0f5e8212bdf1dcf27912fd537d4fc8e221cdc8 Mon Sep 17 00:00:00 2001 From: Sebastian Sellmair Date: Fri, 9 Dec 2022 12:09:16 +0100 Subject: [PATCH] [Gradle] NativeDistributionCommonizerCache: Share intraProcessLock statically KT-52172 --- .../internal/NativeDistributionCommonizerCache.kt | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) 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() + } +}