X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scl.runtime%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fruntime%2FSCLContext.java;h=a9444bcf909f97bd77fde8c47b16ec97996bb0cb;hp=9b3da5232f49f186d2a718fdd983cdd8b2928473;hb=309b4b814a1375c2e2bd67bdee77d62bc9abf073;hpb=9a175feb652b2b7bba7afa540831b9076be3c10e 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 9b3da5232..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,58 +1,76 @@ -package org.simantics.scl.runtime; - -import gnu.trove.map.hash.THashMap; - -public class SCLContext extends THashMap { - private static ThreadLocal CONTEXT = new ThreadLocal(); - private static ThreadLocal OLD_CONTEXT = new ThreadLocal(); - - private static class OldContextNode { - final SCLContext context; - final OldContextNode next; - - public OldContextNode(SCLContext context, OldContextNode next) { - this.context = context; - this.next = next; - } - } - - public static SCLContext getCurrent() { - SCLContext context = CONTEXT.get(); - if(context == null) { - context = new SCLContext(); - CONTEXT.set(context); - } - return context; - } - - public static void push(SCLContext newContext) { - SCLContext oldContext = CONTEXT.get(); - if(oldContext != null) - OLD_CONTEXT.set(new OldContextNode(oldContext, OLD_CONTEXT.get())); - CONTEXT.set(newContext); - } - - public static void pop() { - OldContextNode node = OLD_CONTEXT.get(); - if(node == null) - CONTEXT.set(null); - else { - CONTEXT.set(node.context); - OLD_CONTEXT.set(node.next); - } - } - - /* - @Override - public Object get(Object key) { - Object result = super.get(key); - System.out.println(this + ": " + key + " -> " + result); - return result; - } - - @Override - public Object put(String key, Object value) { - System.out.println(this + ": " + key + " <- " + value); - return super.put(key, value); - }*/ -} +package org.simantics.scl.runtime; + +import org.simantics.scl.runtime.reporting.SCLReportingHandler; + +import gnu.trove.map.hash.THashMap; + +public class SCLContext extends THashMap { + private static ThreadLocal CONTEXT = new ThreadLocal(); + private static ThreadLocal OLD_CONTEXT = new ThreadLocal(); + + private static class OldContextNode { + final SCLContext context; + final OldContextNode next; + + public OldContextNode(SCLContext context, OldContextNode next) { + this.context = context; + this.next = next; + } + } + + public static SCLContext getCurrent() { + SCLContext context = CONTEXT.get(); + if(context == null) { + context = new SCLContext(); + CONTEXT.set(context); + } + return context; + } + + public static void push(SCLContext newContext) { + SCLContext oldContext = CONTEXT.get(); + if(oldContext != null) + OLD_CONTEXT.set(new OldContextNode(oldContext, OLD_CONTEXT.get())); + 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) + CONTEXT.set(null); + else { + CONTEXT.set(node.context); + OLD_CONTEXT.set(node.next); + } + } + + /* + @Override + public Object get(Object key) { + Object result = super.get(key); + System.out.println(this + ": " + key + " -> " + result); + return result; + } + + @Override + public Object put(String key, Object value) { + System.out.println(this + ": " + key + " <- " + value); + return super.put(key, value); + }*/ +}