From 259978fa2992ea580cd4df59c95f1f1295c7af39 Mon Sep 17 00:00:00 2001 From: Philippe Kernevez Date: Mon, 21 Aug 2023 14:54:14 +0200 Subject: [PATCH] Add read barrier in background thread. --- .../lcaplugin/actions/AssessProcessAction.kt | 11 +- .../actions/AssessProcessWithDataAction.kt | 122 +++++++++--------- .../actions/sankey/SankeyGraphAction.kt | 12 +- 3 files changed, 78 insertions(+), 67 deletions(-) diff --git a/src/main/kotlin/ch/kleis/lcaplugin/actions/AssessProcessAction.kt b/src/main/kotlin/ch/kleis/lcaplugin/actions/AssessProcessAction.kt index dd6e9141a..bcd2cfcc7 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/actions/AssessProcessAction.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/actions/AssessProcessAction.kt @@ -12,6 +12,7 @@ import com.intellij.notification.NotificationType import com.intellij.openapi.actionSystem.AnAction import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.LangDataKeys +import com.intellij.openapi.application.runReadAction import com.intellij.openapi.diagnostic.Logger import com.intellij.openapi.progress.ProgressIndicator import com.intellij.openapi.progress.ProgressManager @@ -41,10 +42,12 @@ class AssessProcessAction( private var data: Pair>? = null override fun run(indicator: ProgressIndicator) { - val trace = traceSystemWithIndicator(indicator, file, processName, matchLabels) - val order = trace.getObservableOrder() - val inventory = Assessment(trace.getSystemValue(), trace.getEntryPoint()).inventory() - this.data = Pair(inventory, order) + runReadAction { + val trace = traceSystemWithIndicator(indicator, file, processName, matchLabels) + val order = trace.getObservableOrder() + val inventory = Assessment(trace.getSystemValue(), trace.getEntryPoint()).inventory() + this.data = Pair(inventory, order) + } } override fun onSuccess() { diff --git a/src/main/kotlin/ch/kleis/lcaplugin/actions/AssessProcessWithDataAction.kt b/src/main/kotlin/ch/kleis/lcaplugin/actions/AssessProcessWithDataAction.kt index b8ba8529a..901981e17 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/actions/AssessProcessWithDataAction.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/actions/AssessProcessWithDataAction.kt @@ -41,69 +41,71 @@ class AssessProcessWithDataAction( ProgressManager.getInstance().run(object : Task.Backgroundable(project, "Run with ${processName}.csv") { override fun run(indicator: ProgressIndicator) { - try { - // read - indicator.fraction = 0.0 - indicator.text = "Reading $processName.csv" - val csvFile = Path(containingDirectory.virtualFile.path, "$processName.csv").toFile() - val requests = csvFile.inputStream().use { - val requestReader = CsvRequestReader(processName, matchLabels, it) - requestReader.read() - } + runReadAction { + try { + // read + indicator.fraction = 0.0 + indicator.text = "Reading $processName.csv" + val csvFile = Path(containingDirectory.virtualFile.path, "$processName.csv").toFile() + val requests = csvFile.inputStream().use { + val requestReader = CsvRequestReader(processName, matchLabels, it) + requestReader.read() + } - // process - val symbolTable = runReadAction { - val collector = LcaFileCollector(file.project) - val parser = LcaLangAbstractParser(collector.collect(file)) - parser.load() - } - val csvProcessor = CsvProcessor(symbolTable) - val results = requests.map { request -> - ProgressManager.checkCanceled() - indicator.text = "Processing using ${request.arguments()}" - indicator.fraction = indicator.fraction + 1.0 / requests.size - csvProcessor.process(request) - } + // process + val symbolTable = runReadAction { + val collector = LcaFileCollector(file.project) + val parser = LcaLangAbstractParser(collector.collect(file)) + parser.load() + } + val csvProcessor = CsvProcessor(symbolTable) + val results = requests.map { request -> + ProgressManager.checkCanceled() + indicator.text = "Processing using ${request.arguments()}" + indicator.fraction = indicator.fraction + 1.0 / requests.size + csvProcessor.process(request) + } - // write - indicator.text = "Writing to $processName.results.csv" - indicator.fraction = 1.0 - val path = Path(containingDirectory.virtualFile.path, "$processName.results.csv") - val csvResultFile = path.toFile() - CsvResultWriter(csvResultFile.outputStream()).use { writer -> - writer.write(results) - } + // write + indicator.text = "Writing to $processName.results.csv" + indicator.fraction = 1.0 + val path = Path(containingDirectory.virtualFile.path, "$processName.results.csv") + val csvResultFile = path.toFile() + CsvResultWriter(csvResultFile.outputStream()).use { writer -> + writer.write(results) + } - // done - indicator.text = "Written to $processName.results.csv" - indicator.fraction = 1.0 - val title = "${requests.size} successful assessments of process $processName" - val message = "Results stored in ${processName}.results.csv" - VirtualFileManager.getInstance().refreshAndFindFileByNioPath(path) - NotificationGroupManager.getInstance() - .getNotificationGroup("LcaAsCode") - .createNotification(title, message, NotificationType.INFORMATION) - .notify(project) - } catch (e: EvaluatorException) { - val title = "Error while assessing $processName" - NotificationGroupManager.getInstance() - .getNotificationGroup("LcaAsCode") - .createNotification(title, e.message ?: "unknown error", NotificationType.ERROR) - .notify(project) - LOG.warn("Unable to process computation", e) - } catch (e: NoSuchElementException) { - val title = "Error while assessing $processName" - NotificationGroupManager.getInstance() - .getNotificationGroup("LcaAsCode") - .createNotification(title, e.message ?: "unknown error", NotificationType.ERROR) - .notify(project) - LOG.warn("Unable to process computation", e) - } catch (e: FileNotFoundException) { - val title = "Error while assessing $processName" - NotificationGroupManager.getInstance() - .getNotificationGroup("LcaAsCode") - .createNotification(title, e.message ?: "unknown error", NotificationType.ERROR) - .notify(project) + // done + indicator.text = "Written to $processName.results.csv" + indicator.fraction = 1.0 + val title = "${requests.size} successful assessments of process $processName" + val message = "Results stored in ${processName}.results.csv" + VirtualFileManager.getInstance().refreshAndFindFileByNioPath(path) + NotificationGroupManager.getInstance() + .getNotificationGroup("LcaAsCode") + .createNotification(title, message, NotificationType.INFORMATION) + .notify(project) + } catch (e: EvaluatorException) { + val title = "Error while assessing $processName" + NotificationGroupManager.getInstance() + .getNotificationGroup("LcaAsCode") + .createNotification(title, e.message ?: "unknown error", NotificationType.ERROR) + .notify(project) + LOG.warn("Unable to process computation", e) + } catch (e: NoSuchElementException) { + val title = "Error while assessing $processName" + NotificationGroupManager.getInstance() + .getNotificationGroup("LcaAsCode") + .createNotification(title, e.message ?: "unknown error", NotificationType.ERROR) + .notify(project) + LOG.warn("Unable to process computation", e) + } catch (e: FileNotFoundException) { + val title = "Error while assessing $processName" + NotificationGroupManager.getInstance() + .getNotificationGroup("LcaAsCode") + .createNotification(title, e.message ?: "unknown error", NotificationType.ERROR) + .notify(project) + } } } }) diff --git a/src/main/kotlin/ch/kleis/lcaplugin/actions/sankey/SankeyGraphAction.kt b/src/main/kotlin/ch/kleis/lcaplugin/actions/sankey/SankeyGraphAction.kt index 66eae33d3..29a52289e 100644 --- a/src/main/kotlin/ch/kleis/lcaplugin/actions/sankey/SankeyGraphAction.kt +++ b/src/main/kotlin/ch/kleis/lcaplugin/actions/sankey/SankeyGraphAction.kt @@ -13,6 +13,7 @@ import com.intellij.notification.NotificationType import com.intellij.openapi.actionSystem.AnAction import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.LangDataKeys +import com.intellij.openapi.application.runReadAction import com.intellij.openapi.diagnostic.Logger import com.intellij.openapi.progress.ProgressIndicator import com.intellij.openapi.progress.ProgressManager @@ -47,9 +48,14 @@ class SankeyGraphAction( private var graph: Graph? = null override fun run(progress: ProgressIndicator) { - val trace = traceSystemWithIndicator(progress, file, processName, matchLabels) - val assessment = Assessment(trace.getSystemValue(), trace.getEntryPoint()) - val inventory = assessment.inventory() + val (trace, assessment, inventory) = + runReadAction { + val rTrace = traceSystemWithIndicator(progress, file, processName, matchLabels) + val rAssessment = Assessment(rTrace.getSystemValue(), rTrace.getEntryPoint()) + val rInventory = rAssessment.inventory() + val result = Triple(rTrace, rAssessment, rInventory) + result + } val allocatedSystem = assessment.allocatedSystem indicatorList = inventory.getControllablePorts().getElements()