]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
(refs #7607) Fixed handling of SCLContext in asynchronous requests 23/1223/1
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Wed, 15 Nov 2017 06:54:45 +0000 (08:54 +0200)
committerHannu Niemistö <hannu.niemisto@semantum.fi>
Wed, 15 Nov 2017 06:54:45 +0000 (08:54 +0200)
Change-Id: I3956ee1fa00a8e42a9531aab43b9ba63461ac353

bundles/org.simantics.scl.db/src/org/simantics/scl/db/SCLFunctions.java
bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/SCLContext.java
bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/utils/AsyncUtils.java

index da88b72c063ab2bb2862156f1f39afe5058839c9..59923933f6107f3e8f0d25e928c9b2fdc7229f1e 100644 (file)
@@ -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> 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> T syncWrite(final Function f) throws DatabaseException {
index 8703819776a95f521109b36eadbbbcec847ca6f8..a9444bcf909f97bd77fde8c47b16ec97996bb0cb 100644 (file)
@@ -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<String,Object> {
@@ -32,6 +34,22 @@ public class SCLContext extends THashMap<String,Object> {
         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)
index fcd8a47240d59ddd30d9052f8decb87530bda8aa..29886f21ee960e916f6078d581f4d2032f142414 100644 (file)
@@ -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();
                 }