Skip to content

Commit 4c93351

Browse files
committed
[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). #KT-65634 Fixed
1 parent b4d5b45 commit 4c93351

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CacheStorage.kt

+11-7
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ internal class CacheStorage(private val generationState: NativeGenerationState)
1414
private val outputFiles = generationState.outputFiles
1515

1616
companion object {
17-
fun renameOutput(outputFiles: OutputFiles) {
18-
// For caches the output file is a directory. It might be created by someone else,
19-
// we have to delete it in order for the next renaming operation to succeed.
20-
val tempDirectoryForRemoval = File(outputFiles.mainFileName + "-to-remove")
21-
if (outputFiles.mainFile.exists && !outputFiles.mainFile.renameTo(tempDirectoryForRemoval))
22-
return
23-
tempDirectoryForRemoval.deleteRecursively()
17+
fun renameOutput(outputFiles: OutputFiles, overwrite: Boolean) {
18+
if (outputFiles.mainFile.exists) {
19+
if (!overwrite)
20+
return
21+
// For caches the output file is a directory. It might be already created,
22+
// we have to delete it in order for the next renaming operation to succeed.
23+
val tempDirectoryForRemoval = File(outputFiles.mainFileName + "-to-remove")
24+
if (!outputFiles.mainFile.renameTo(tempDirectoryForRemoval))
25+
return
26+
tempDirectoryForRemoval.deleteRecursively()
27+
}
2428
if (!outputFiles.tempCacheDirectory!!.renameTo(outputFiles.mainFile))
2529
outputFiles.tempCacheDirectory.deleteRecursively()
2630
}

kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/CacheBuilding.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ internal val SaveAdditionalCacheInfoPhase = createSimpleNamedCompilerPhase<Nativ
4444
internal val FinalizeCachePhase = createSimpleNamedCompilerPhase<PhaseContext, OutputFiles>(
4545
name = "FinalizeCache",
4646
description = "Finalize cache (rename temp to the final dist)"
47-
) { _, outputFiles ->
47+
) { context, outputFiles ->
4848
// TODO: Explicit parameter
49-
CacheStorage.renameOutput(outputFiles)
49+
CacheStorage.renameOutput(outputFiles, overwrite = context.config.producePerFileCache)
5050
}

0 commit comments

Comments
 (0)