IJPL-162718 use write on background

GitOrigin-RevId: 788275571e9a112efa1ed23d56ebadb3ee417992
This commit is contained in:
Vladimir Krivosheev
2024-10-07 07:01:53 +02:00
committed by intellij-monorepo-bot
parent 17b5c9688b
commit 27a2f65317
5 changed files with 25 additions and 16 deletions

View File

@@ -1,9 +1,8 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.workspaceModel.ide.impl.legacyBridge.module
import com.intellij.openapi.application.backgroundWriteAction
import com.intellij.openapi.application.readAction
import com.intellij.openapi.application.writeAction
import com.intellij.openapi.components.service
import com.intellij.openapi.components.serviceAsync
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.diagnostic.logger
@@ -82,7 +81,7 @@ private class ModuleBridgeLoaderService : ProjectServiceContainerInitializedList
loadedFromCache = workspaceModel.loadedFromCache)
}
val globalWorkspaceModel = serviceAsync<GlobalWorkspaceModel>()
writeAction {
backgroundWriteAction {
globalWorkspaceModel.applyStateToProject(project)
}
}
@@ -110,7 +109,7 @@ private class ModuleBridgeLoaderService : ProjectServiceContainerInitializedList
launch { serviceAsync<ProjectJdkTable>() }
project.serviceAsync<ProjectRootManager>() as ProjectRootManagerBridge
}
writeAction {
backgroundWriteAction {
projectRootManager.setupTrackedLibrariesAndJdks()
}
}

View File

@@ -1,7 +1,9 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.workspaceModel.ide.impl.jps.serialization
import com.intellij.openapi.application.*
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.PathManager
import com.intellij.openapi.application.backgroundWriteAction
import com.intellij.openapi.components.impl.stores.stateStore
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.roots.OrderRootType
@@ -11,16 +13,22 @@ import com.intellij.platform.workspace.jps.JpsGlobalFileEntitySource
import com.intellij.platform.workspace.jps.entities.LibraryEntity
import com.intellij.platform.workspace.jps.entities.SdkEntity
import com.intellij.platform.workspace.jps.serialization.impl.*
import com.intellij.platform.workspace.storage.*
import com.intellij.platform.workspace.storage.EntityStorage
import com.intellij.platform.workspace.storage.MutableEntityStorage
import com.intellij.platform.workspace.storage.VersionedEntityStorage
import com.intellij.platform.workspace.storage.WorkspaceEntity
import com.intellij.platform.workspace.storage.url.VirtualFileUrl
import com.intellij.platform.workspace.storage.url.VirtualFileUrlManager
import com.intellij.workspaceModel.ide.*
import com.intellij.workspaceModel.ide.JpsGlobalModelLoadedListener
import com.intellij.workspaceModel.ide.JpsGlobalModelSynchronizer
import com.intellij.workspaceModel.ide.impl.GlobalWorkspaceModel
import com.intellij.workspaceModel.ide.impl.jpsMetrics
import com.intellij.workspaceModel.ide.impl.legacyBridge.sdk.SdkBridgeImpl.Companion.sdkMap
import com.intellij.workspaceModel.ide.legacyBridge.GlobalEntityBridgeAndEventHandler
import io.opentelemetry.api.metrics.Meter
import kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.jdom.Element
import org.jetbrains.annotations.ApiStatus
import org.jetbrains.annotations.TestOnly
@@ -151,7 +159,7 @@ class JpsGlobalModelSynchronizerImpl(private val coroutineScope: CoroutineScope)
// We don't need to initialize bridges one more time at delay loading. Otherwise, we will get the new instance of bridge in the mappings
loadGlobalEntitiesToEmptyStorage(mutableStorage, globalWorkspaceModel.entityStorage, initializeBridges = false)
writeAction {
backgroundWriteAction {
globalWorkspaceModel.updateModel("Sync global entities with state") { builder ->
builder.replaceBySource({ it is JpsGlobalFileEntitySource }, mutableStorage)
}

View File

@@ -8,7 +8,7 @@ import com.intellij.diagnostic.StartUpMeasurer.startActivity
import com.intellij.ide.highlighter.ModuleFileType
import com.intellij.ide.highlighter.ProjectFileType
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.writeAction
import com.intellij.openapi.application.backgroundWriteAction
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.components.serviceIfCreated
@@ -217,7 +217,7 @@ class JpsProjectModelSynchronizer(private val project: Project) : Disposable {
for (i in 1..retryCount) {
LOG.info("Attempt $i: $description")
val calculationResult = calculateChanges()
val isSuccessful = writeAction { applyLoadedChanges(calculationResult) }
val isSuccessful = backgroundWriteAction { applyLoadedChanges(calculationResult) }
if (isSuccessful) {
LOG.info("Attempt $i: Changes were successfully applied")
return true
@@ -227,10 +227,12 @@ class JpsProjectModelSynchronizer(private val project: Project) : Disposable {
}
val isSuccessful = applyChangesWithRetry(2)
if (isSuccessful) return
if (isSuccessful) {
return
}
// Fallback strategy after the two unsuccessful attempts to apply the changes
writeAction {
backgroundWriteAction {
LOG.info("Fallback strategy after the unsuccessful attempts to apply the changes from BGT")
val calculationResult = calculateChanges()
applyLoadedChanges(calculationResult)

View File

@@ -374,7 +374,7 @@ open class ProjectRootManagerImpl(val project: Project,
LOG.debug("Run write action for extension.projectSdkChanged(sdk)")
val extensions = EP_NAME.getExtensions(project)
writeAction {
backgroundWriteAction {
for (extension in extensions) {
extension.projectSdkChanged(sdk)
}

View File

@@ -2,7 +2,7 @@
package com.intellij.workspaceModel.ide.impl
import com.intellij.openapi.application.WriteAction
import com.intellij.openapi.application.writeAction
import com.intellij.openapi.application.backgroundWriteAction
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Ref
import com.intellij.platform.backend.workspace.WorkspaceEntityLifecycleSupporter
@@ -33,7 +33,7 @@ object WorkspaceEntityLifecycleSupporterUtils {
WorkspaceEntityLifecycleSupporter.EP_NAME.forEachExtensionSafe { provider ->
ensureInitialized(project, provider, snapshot, builderRef)
}
builderRef.get()?.also { builder -> writeAction { writeBuilder(workspaceModel, builder) } }
builderRef.get()?.also { builder -> backgroundWriteAction { writeBuilder(workspaceModel, builder) } }
}