From: Hannu Niemistö Date: Wed, 15 Nov 2017 06:54:45 +0000 (+0200) Subject: (refs #7607) Fixed handling of SCLContext in asynchronous requests X-Git-Tag: v1.31.0~37^2 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F23%2F1223%2F1;p=simantics%2Fplatform.git (refs #7607) Fixed handling of SCLContext in asynchronous requests Change-Id: I3956ee1fa00a8e42a9531aab43b9ba63461ac353 --- diff --git a/bundles/org.simantics.scl.db/src/org/simantics/scl/db/SCLFunctions.java b/bundles/org.simantics.scl.db/src/org/simantics/scl/db/SCLFunctions.java index da88b72c0..59923933f 100644 --- a/bundles/org.simantics.scl.db/src/org/simantics/scl/db/SCLFunctions.java +++ b/bundles/org.simantics.scl.db/src/org/simantics/scl/db/SCLFunctions.java @@ -53,25 +53,19 @@ public class SCLFunctions { } public static void asyncRead(final Function f) throws DatabaseException { - final SCLContext context = SCLContext.getCurrent(); - Object graph = context.get(GRAPH); - if (graph != null) { - f.apply(Tuple0.INSTANCE); - } else { - Simantics.getSession().asyncRequest(new ReadRequest() { - @Override - public void run(ReadGraph graph) throws DatabaseException { - SCLContext.push(context); - ReadGraph oldGraph = (ReadGraph)context.put(GRAPH, graph); - try { - f.apply(Tuple0.INSTANCE); - } finally { - context.put(GRAPH, oldGraph); - SCLContext.pop(); - } + final SCLContext context = SCLContext.createDerivedContext(); + Simantics.getSession().asyncRequest(new ReadRequest() { + @Override + public void run(ReadGraph graph) throws DatabaseException { + SCLContext.push(context); + context.put(GRAPH, graph); + try { + f.apply(Tuple0.INSTANCE); + } finally { + SCLContext.pop(); } - }); - } + } + }); } public static T syncRead(final Function f) throws DatabaseException { @@ -97,25 +91,19 @@ public class SCLFunctions { } public static void asyncWrite(final Function f) throws DatabaseException { - final SCLContext context = SCLContext.getCurrent(); - Object graph = context.get(GRAPH); - if (graph != null) { - f.apply(Tuple0.INSTANCE); - } else { - Simantics.getSession().asyncRequest(new WriteRequest() { - @Override - public void perform(WriteGraph graph) throws DatabaseException { - SCLContext.push(context); - ReadGraph oldGraph = (ReadGraph)context.put(GRAPH, graph); - try { - f.apply(Tuple0.INSTANCE); - } finally { - context.put(GRAPH, oldGraph); - SCLContext.pop(); - } + SCLContext context = SCLContext.createDerivedContext(); + Simantics.getSession().asyncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + SCLContext.push(context); + context.put(GRAPH, graph); + try { + f.apply(Tuple0.INSTANCE); + } finally { + SCLContext.pop(); } - }); - } + } + }); } public static T syncWrite(final Function f) throws DatabaseException { diff --git a/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/SCLContext.java b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/SCLContext.java index 870381977..a9444bcf9 100644 --- a/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/SCLContext.java +++ b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/SCLContext.java @@ -1,5 +1,7 @@ package org.simantics.scl.runtime; +import org.simantics.scl.runtime.reporting.SCLReportingHandler; + import gnu.trove.map.hash.THashMap; public class SCLContext extends THashMap { @@ -32,6 +34,22 @@ public class SCLContext extends THashMap { CONTEXT.set(newContext); } + /** + * Creates a new context based on some properties of the current context. + * The new context is safe for use in parallel threads. + */ + public static SCLContext createDerivedContext() { + SCLContext newContext = new SCLContext(); + + SCLContext baseContext = CONTEXT.get(); + if(baseContext != null) { + Object reportingHandler = baseContext.get(SCLReportingHandler.REPORTING_HANDLER); + if(reportingHandler != null) + newContext.put(SCLReportingHandler.REPORTING_HANDLER, reportingHandler); + } + return newContext; + } + public static void pop() { OldContextNode node = OLD_CONTEXT.get(); if(node == null) diff --git a/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/utils/AsyncUtils.java b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/utils/AsyncUtils.java index fcd8a4724..29886f21e 100644 --- a/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/utils/AsyncUtils.java +++ b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/utils/AsyncUtils.java @@ -1,18 +1,26 @@ package org.simantics.scl.runtime.utils; +import java.io.PrintWriter; +import java.io.StringWriter; + import org.simantics.scl.runtime.SCLContext; import org.simantics.scl.runtime.function.Function; +import org.simantics.scl.runtime.reporting.SCLReporting; import org.simantics.scl.runtime.tuple.Tuple0; public class AsyncUtils { public static void runAsync(Function f) { - SCLContext context = SCLContext.getCurrent(); + SCLContext context = SCLContext.createDerivedContext(); new Thread() { @Override public void run() { SCLContext.push(context); try { f.apply(Tuple0.INSTANCE); + } catch(Exception e) { + StringWriter sw = new StringWriter(); + e.printStackTrace(new PrintWriter(sw)); + SCLReporting.printError(sw.toString()); } finally { SCLContext.pop(); }