changeset 13163:1baa169508f5

made DebugScope be AutoCloseable so that try-with-resource can be used to more cleanly manage scopes
author Doug Simon <doug.simon@oracle.com>
date Mon, 25 Nov 2013 18:42:51 +0100
parents f9723019584e
children 70e6bb85d802
files graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Debug.java graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugScope.java
diffstat 2 files changed, 40 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Debug.java	Mon Nov 25 17:32:15 2013 +0100
+++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Debug.java	Mon Nov 25 18:42:51 2013 +0100
@@ -107,7 +107,8 @@
 
     public static void sandbox(String name, DebugConfig config, Runnable runnable) {
         if (ENABLED) {
-            DebugScope.getInstance().scope(name, runnable, null, true, config, new Object[0]);
+            DebugConfig sandboxConfig = config == null ? DebugScope.getConfig() : config;
+            DebugScope.getInstance().scope(name, runnable, null, sandboxConfig, new Object[0]);
         } else {
             runnable.run();
         }
@@ -124,7 +125,8 @@
      */
     public static void sandbox(String name, Object[] context, DebugConfig config, Runnable runnable) {
         if (ENABLED) {
-            DebugScope.getInstance().scope(name, runnable, null, true, config, context);
+            DebugConfig sandboxConfig = config == null ? DebugScope.getConfig() : config;
+            DebugScope.getInstance().scope(name, runnable, null, sandboxConfig, context);
         } else {
             runnable.run();
         }
@@ -141,7 +143,8 @@
      */
     public static <T> T sandbox(String name, Object[] context, DebugConfig config, Callable<T> callable) {
         if (ENABLED) {
-            return DebugScope.getInstance().scope(name, null, callable, true, config, context);
+            DebugConfig sandboxConfig = config == null ? DebugScope.getConfig() : config;
+            return DebugScope.getInstance().scope(name, null, callable, sandboxConfig, context);
         } else {
             return DebugScope.call(callable);
         }
@@ -161,7 +164,7 @@
 
     public static void scope(String name, Object[] context, Runnable runnable) {
         if (ENABLED) {
-            DebugScope.getInstance().scope(name, runnable, null, false, null, context);
+            DebugScope.getInstance().scope(name, runnable, null, null, context);
         } else {
             runnable.run();
         }
@@ -181,7 +184,7 @@
 
     public static <T> T scope(String name, Object[] context, Callable<T> callable) {
         if (ENABLED) {
-            return DebugScope.getInstance().scope(name, null, callable, false, null, context);
+            return DebugScope.getInstance().scope(name, null, callable, null, context);
         } else {
             return DebugScope.call(callable);
         }
--- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugScope.java	Mon Nov 25 17:32:15 2013 +0100
+++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugScope.java	Mon Nov 25 18:42:51 2013 +0100
@@ -28,7 +28,7 @@
 
 import com.oracle.graal.debug.*;
 
-public final class DebugScope {
+public final class DebugScope implements AutoCloseable {
 
     private final class IndentImpl implements Indent {
 
@@ -99,6 +99,8 @@
     private static ThreadLocal<Throwable> lastExceptionThrownTL = new ThreadLocal<>();
 
     private final DebugScope parent;
+    private final DebugConfig parentConfig;
+    private final boolean sandbox;
     private IndentImpl lastUsedIndent;
     private boolean logScopeName;
 
@@ -118,7 +120,7 @@
     public static DebugScope getInstance() {
         DebugScope result = instanceTL.get();
         if (result == null) {
-            DebugScope topLevelDebugScope = new DebugScope(Thread.currentThread().getName(), "", null);
+            DebugScope topLevelDebugScope = new DebugScope(Thread.currentThread().getName(), "", null, false);
             instanceTL.set(topLevelDebugScope);
             DebugValueMap.registerTopLevel(topLevelDebugScope.getValueMap());
             return topLevelDebugScope;
@@ -131,8 +133,10 @@
         return configTL.get();
     }
 
-    private DebugScope(String name, String qualifiedName, DebugScope parent, Object... context) {
+    private DebugScope(String name, String qualifiedName, DebugScope parent, boolean sandbox, Object... context) {
         this.parent = parent;
+        this.sandbox = sandbox;
+        this.parentConfig = getConfig();
         this.context = context;
         this.qualifiedName = qualifiedName;
         if (parent != null) {
@@ -162,6 +166,12 @@
         }
     }
 
+    public void close() {
+        context = null;
+        instanceTL.set(parent);
+        setConfig(parentConfig);
+    }
+
     public boolean isDumpEnabled() {
         return dumpEnabled;
     }
@@ -227,32 +237,28 @@
      * @param newName the name of the new scope
      * @param runnable the task to run (must be null iff {@code callable} is not null)
      * @param callable the task to run (must be null iff {@code runnable} is not null)
-     * @param sandbox specifies if the scope is a child of the current scope or a top level scope
-     * @param sandboxConfig the config to use of a new top level scope (ignored if
-     *            {@code sandbox == false})
+     * @param sandboxConfig if non-null, a new top level scope is entered with this configuration
      * @param newContext context objects of the new scope
      * @return the value returned by the task
      */
-    public <T> T scope(String newName, Runnable runnable, Callable<T> callable, boolean sandbox, DebugConfig sandboxConfig, Object[] newContext) {
-        DebugScope oldContext = getInstance();
-        DebugConfig oldConfig = getConfig();
-        DebugScope newChild = null;
-        if (sandbox) {
-            newChild = new DebugScope(newName, newName, null, newContext);
+    public <T> T scope(String newName, Runnable runnable, Callable<T> callable, DebugConfig sandboxConfig, Object[] newContext) {
+        try (DebugScope s = openScope(newName, sandboxConfig, newContext)) {
+            return executeScope(runnable, callable);
+        }
+    }
+
+    public DebugScope openScope(String newName, DebugConfig sandboxConfig, Object... newContext) {
+        DebugScope newScope = null;
+        if (sandboxConfig != null) {
+            newScope = new DebugScope(newName, newName, this, true, newContext);
             setConfig(sandboxConfig);
         } else {
-            newChild = oldContext.createChild(newName, newContext);
+            newScope = this.createChild(newName, newContext);
         }
-        instanceTL.set(newChild);
-        newChild.setLogEnabled(oldContext.isLogEnabled());
-        newChild.updateFlags();
-        try {
-            return executeScope(runnable, callable);
-        } finally {
-            newChild.context = null;
-            instanceTL.set(oldContext);
-            setConfig(oldConfig);
-        }
+        instanceTL.set(newScope);
+        newScope.setLogEnabled(this.isLogEnabled());
+        newScope.updateFlags();
+        return newScope;
     }
 
     private <T> T executeScope(Runnable runnable, Callable<T> callable) {
@@ -315,7 +321,7 @@
                         return new RuntimeException("Exception while intercepting exception", t);
                     }
                 }
-            }, false, null, new Object[]{e});
+            }, null, new Object[]{e});
         }
         return null;
     }
@@ -337,7 +343,7 @@
         if (this.qualifiedName.length() > 0) {
             newQualifiedName = this.qualifiedName + SCOPE_SEP + newName;
         }
-        DebugScope result = new DebugScope(newName, newQualifiedName, this, newContext);
+        DebugScope result = new DebugScope(newName, newQualifiedName, this, false, newContext);
         return result;
     }
 
@@ -360,7 +366,7 @@
 
                     private void selectScope() {
                         while (currentScope != null && currentScope.context.length <= objectIndex) {
-                            currentScope = currentScope.parent;
+                            currentScope = currentScope.sandbox ? null : currentScope.parent;
                             objectIndex = 0;
                         }
                     }