From 8cc9272018d6216b45e564fb637232d447ff6d15 Mon Sep 17 00:00:00 2001 From: Igor Chevdar Date: Mon, 8 Jan 2024 15:30:55 +0200 Subject: [PATCH] [K/N] Fixed a data race during monolithic caches creation For per-file caches the data race is still there, but since per-file caches are only used for incremental compilation, it's ok to leave it as is for now (we cope with the data race by using a separate folder for each Gradle task). --- .../kotlin/backend/konan/CacheStorage.kt | 18 +++++++++++------- .../konan/driver/phases/CacheBuilding.kt | 4 ++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CacheStorage.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CacheStorage.kt index 7c110b44a97..776cc66f6f0 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CacheStorage.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CacheStorage.kt @@ -14,13 +14,17 @@ internal class CacheStorage(private val generationState: NativeGenerationState) private val outputFiles = generationState.outputFiles companion object { - fun renameOutput(outputFiles: OutputFiles) { - // For caches the output file is a directory. It might be created by someone else, - // we have to delete it in order for the next renaming operation to succeed. - val tempDirectoryForRemoval = File(outputFiles.mainFileName + "-to-remove") - if (outputFiles.mainFile.exists && !outputFiles.mainFile.renameTo(tempDirectoryForRemoval)) - return - tempDirectoryForRemoval.deleteRecursively() + fun renameOutput(outputFiles: OutputFiles, overwrite: Boolean) { + if (outputFiles.mainFile.exists) { + if (!overwrite) + return + // For caches the output file is a directory. It might be already created, + // we have to delete it in order for the next renaming operation to succeed. + val tempDirectoryForRemoval = File(outputFiles.mainFileName + "-to-remove") + if (!outputFiles.mainFile.renameTo(tempDirectoryForRemoval)) + return + tempDirectoryForRemoval.deleteRecursively() + } if (!outputFiles.tempCacheDirectory!!.renameTo(outputFiles.mainFile)) outputFiles.tempCacheDirectory.deleteRecursively() } diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/CacheBuilding.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/CacheBuilding.kt index e9141c42685..1ffb250b880 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/CacheBuilding.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/CacheBuilding.kt @@ -45,7 +45,7 @@ internal val SaveAdditionalCacheInfoPhase = createSimpleNamedCompilerPhase( name = "FinalizeCache", description = "Finalize cache (rename temp to the final dist)" -) { _, outputFiles -> +) { context, outputFiles -> // TODO: Explicit parameter - CacheStorage.renameOutput(outputFiles) + CacheStorage.renameOutput(outputFiles, overwrite = context.config.producePerFileCache) } \ No newline at end of file