[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.FileOutputStream
import java.io.ObjectInputStream import java.io.ObjectInputStream
import java.io.Serializable import java.io.Serializable
import java.util.concurrent.locks.Lock
import java.util.concurrent.locks.ReentrantLock import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.withLock import kotlin.concurrent.withLock
@@ -105,12 +106,11 @@ class NativeDistributionCommonizerCache(
logger.info("Native Distribution Commonization: $message") logger.info("Native Distribution Commonization: $message")
private inner class Lock { private inner class Lock {
private val reentrantLock = ReentrantLock()
private val lockedOutputDirectories = mutableSetOf<File>() private val lockedOutputDirectories = mutableSetOf<File>()
fun <T> withLock(action: () -> T): T { fun <T> withLock(action: () -> T): T {
/* Enter intra-process wide lock */ /* Enter intra-process wide lock */
reentrantLock.withLock { intraProcessLock.withLock {
if (outputDirectory in lockedOutputDirectories) { if (outputDirectory in lockedOutputDirectories) {
/* Already acquired this directory and re-entered: We can just execute the action */ /* Already acquired this directory and re-entered: We can just execute the action */
return action() return action()
@@ -137,7 +137,7 @@ class NativeDistributionCommonizerCache(
} }
fun checkLocked(outputDirectory: File) { fun checkLocked(outputDirectory: File) {
check(reentrantLock.isHeldByCurrentThread) { check(intraProcessLock.isHeldByCurrentThread) {
"Expected lock to be held by current thread ${Thread.currentThread().name}" "Expected lock to be held by current thread ${Thread.currentThread().name}"
} }
@@ -151,4 +151,8 @@ class NativeDistributionCommonizerCache(
input.defaultReadObject() input.defaultReadObject()
lock = Lock() lock = Lock()
} }
private companion object {
val intraProcessLock: ReentrantLock = ReentrantLock()
}
} }