[Gradle] NativeDistributionCommonizerCache: Share intraProcessLock statically

KT-52172
This commit is contained in:
Sebastian Sellmair
2022-12-09 12:09:16 +01:00
committed by Space Team
parent b872d42723
commit 6e0f5e8212
@@ -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<File>()
fun <T> 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()
}
}
private companion object {
val intraProcessLock: ReentrantLock = ReentrantLock()
}
}